home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 15 / AACD15.ISO / AACD / Programming / Python2 / Python20_source / Modules / makesetup < prev    next >
Encoding:
Text File  |  2000-10-25  |  6.5 KB  |  273 lines

  1. #! /bin/sh
  2.  
  3. # Convert templates into Makefile and config.c, based on the module
  4. # definitions found in the file Setup.
  5. #
  6. # Usage: makesetup [-s dir] [-c file] [-m file] [Setup] ... [-n [Setup] ...]
  7. #
  8. # Options:
  9. # -s directory: alternative source directory (default derived from $0)
  10. # -c file:      alternative config.c template (default $srcdir/config.c.in)
  11. # -c -:         don't write config.c
  12. # -m file:      alternative Makefile template (default ./Makefile.pre)
  13. # -m -:         don't write Makefile
  14. #
  15. # Remaining arguments are one or more Setup files (default ./Setup).
  16. # Setup files after a -n option are used for their variables, modules
  17. # and libraries but not for their .o files.
  18. #
  19. # See Setup.in for a description of the format of the Setup file.
  20. #
  21. # The following edits are made:
  22. #
  23. # Copying config.c.in to config.c:
  24. # - insert an identifying comment at the start
  25. # - for each <module> mentioned in Setup before *noconfig*:
  26. #   + insert 'extern void init<module>(void);' before MARKER 1
  27. #   + insert '{"<module>", initmodule},' before MARKER 2
  28. #
  29. # Copying Makefile.pre to Makefile:
  30. # - insert an identifying comment at the start
  31. # - replace _MODOBJS_ by the list of objects from Setup (except for
  32. #   Setup files after a -n option)
  33. # - replace _MODLIBS_ by the list of libraries from Setup
  34. # - for each object file mentioned in Setup, append a rule
  35. #   '<file>.o: <file>.c; <build commands>' to the end of the Makefile
  36. # - for each module mentioned in Setup, append a rule
  37. #   which creates a shared library version to the end of the Makefile
  38. # - for each variable definition found in Setup, insert the definition
  39. #   before the comment 'Definitions added by makesetup'
  40.  
  41. # Loop over command line options
  42. usage='
  43. usage: makesetup [-s srcdir] [-c config.c.in] [-m Makefile.pre]
  44.                  [Setup] ... [-n [Setup] ...]'
  45. srcdir=''
  46. config=''
  47. makepre=''
  48. noobjects=''
  49. doconfig=yes
  50. while :
  51. do
  52.     case $1 in
  53.     -s)    shift; srcdir=$1; shift;;
  54.     -c)    shift; config=$1; shift;;
  55.     -m)    shift; makepre=$1; shift;;
  56.     --)    shift; break;;
  57.     -n)    noobjects=yes;;
  58.     -*)    echo "$usage" 1>&2; exit 2;;
  59.     *)    break;;
  60.     esac
  61. done
  62.  
  63. # Set default srcdir and config if not set by command line
  64. # (Not all systems have dirname)
  65. case $srcdir in
  66. '')    case $0 in
  67.     */*)    srcdir=`echo $0 | sed 's,/[^/]*$,,'`;;
  68.     *)    srcdir=.;;
  69.     esac;;
  70. esac
  71. case $config in
  72. '')    config=$srcdir/config.c.in;;
  73. esac
  74. case $makepre in
  75. '')    makepre=Makefile.pre;;
  76. esac
  77.  
  78. # Newline for sed i and a commands
  79. NL='\
  80. '
  81.  
  82. # Main loop
  83. for i in ${*-Setup}
  84. do
  85.     case $i in
  86.     -n)    echo '*noobjects*';;
  87.     *)    echo '*doconfig*'; cat "$i";;
  88.     esac
  89. done |
  90. sed -e 's/[     ]*#.*//' -e '/^[     ]*$/d' |
  91. (
  92.     rulesf="@rules.$$"
  93.     trap 'rm -f $rulesf' 0 1 2 3
  94.     echo "
  95. # Rules appended by makedepend
  96. " >$rulesf
  97.     DEFS=
  98.     MODS=
  99.     SHAREDMODS=
  100.     OBJS=
  101.     LIBS=
  102.     LOCALLIBS=
  103.     BASELIBS=
  104.     while read line
  105.     do
  106.         # to handle backslashes for sh's that don't automatically
  107.         # continue a read when the last char is a backslash
  108.         while echo $line | grep '\\$' > /dev/null
  109.         do
  110.             read extraline
  111.             line=`echo $line| sed s/.$//`$extraline
  112.         done
  113.  
  114.         # Output DEFS in reverse order so first definition overrides
  115.         case $line in
  116.         *=*)    DEFS="$line$NL$DEFS"; continue;;
  117.         'include '*)    DEFS="$line$NL$DEFS"; continue;;
  118.         '*noobjects*')
  119.             case $noobjects in
  120.             yes)    ;;
  121.             *)    LOCALLIBS=$LIBS; LIBS=;;
  122.             esac
  123.             noobjects=yes;
  124.             continue;;
  125.         '*doconfig*')    doconfig=yes; continue;;
  126.         '*static*')    doconfig=yes; continue;;
  127.         '*noconfig*')    doconfig=no; continue;;
  128.         '*shared*')    doconfig=no; continue;;
  129.         esac
  130.         srcs=
  131.         cpps=
  132.         libs=
  133.         mods=
  134.         skip=
  135.         for arg in $line
  136.         do
  137.             case $skip in
  138.             libs)    libs="$libs $arg"; skip=; continue;;
  139.             cpps)    cpps="$cpps $arg"; skip=; continue;;
  140.             srcs)    srcs="$srcs $arg"; skip=; continue;;
  141.             esac
  142.             case $arg in
  143.             -[IDUCf]*)    cpps="$cpps $arg";;
  144.             -Xlinker)    libs="$libs $arg"; skip=libs;;
  145.             -rpath)        libs="$libs $arg"; skip=libs;;
  146.             --rpath)    libs="$libs $arg"; skip=libs;;
  147.             -[A-Zl]*)    libs="$libs $arg";;
  148.             *.a)        libs="$libs $arg";;
  149.             *.so)        libs="$libs $arg";;
  150.             *.sl)        libs="$libs $arg";;
  151.             /*.o)        libs="$libs $arg";;
  152.             *.o)        srcs="$srcs `basename $arg .o`.c";;
  153.             *.[cC])        srcs="$srcs $arg";;
  154.             *.cc)        srcs="$srcs $arg";;
  155.             *.c++)        srcs="$srcs $arg";;
  156.             *.cxx)        srcs="$srcs $arg";;
  157.             *.cpp)        srcs="$srcs $arg";;
  158.             \$*)        libs="$libs $arg"
  159.                     cpps="$cpps $arg";;
  160.             *.*)        echo 1>&2 "bad word $arg in $line"
  161.                     exit 1;;
  162.             -u)        skip=libs; libs="$libs -u";;
  163.             [a-zA-Z_]*)    mods="$mods $arg";;
  164.             *)        echo 1>&2 "bad word $arg in $line"
  165.                     exit 1;;
  166.             esac
  167.         done
  168.         case $doconfig in
  169.         yes)
  170.             LIBS="$LIBS $libs"
  171.             MODS="$MODS $mods"
  172.             ;;
  173.         esac
  174.         case $noobjects in
  175.         yes)    continue;;
  176.         esac
  177.         objs=''
  178.         for src in $srcs
  179.         do
  180.             case $src in
  181.             *.c)   obj=`basename $src .c`.o; cc='$(CC)';;
  182.             *.cc)  obj=`basename $src .cc`.o; cc='$(CCC)';;
  183.             *.c++) obj=`basename $src .c++`.o; cc='$(CCC)';;
  184.             *.C)   obj=`basename $src .C`.o; cc='$(CCC)';;
  185.             *.cxx) obj=`basename $src .cxx`.o; cc='$(CCC)';;
  186.             *.cpp) obj=`basename $src .cpp`.o; cc='$(CCC)';;
  187.             *)     continue;;
  188.             esac
  189.             objs="$objs $obj"
  190.             case $src in
  191.             glmodule.c) ;;
  192.             /*) ;;
  193.             \$*) ;;
  194.             *) src='$(srcdir)/'$src;;
  195.             esac
  196.             case $doconfig in
  197.             no)    cc="$cc \$(CCSHARED)";;
  198.             esac
  199.             rule="$obj: $src; $cc $cpps \$(CFLAGS) -c $src"
  200.             echo "$rule" >>$rulesf
  201.         done
  202.         case $doconfig in
  203.         yes)    OBJS="$OBJS $objs";;
  204.         esac
  205.         for mod in $mods
  206.         do
  207.             case $objs in
  208.             *$mod.o*)    base=$mod;;
  209.             *)        base=${mod}module;;
  210.             esac
  211.             file="$base\$(SO)"
  212.             case $doconfig in
  213.             no)    SHAREDMODS="$SHAREDMODS $file";;
  214.             esac
  215.             rule="$file: $objs"
  216.             rule="$rule; \$(LDSHARED) $objs $libs -o $file"
  217.             echo "$rule" >>$rulesf
  218.         done
  219.     done
  220.  
  221.     case $SHAREDMODS in
  222.     '')    ;;
  223.     *)    DEFS="SHAREDMODS=$SHAREDMODS$NL$DEFS";;
  224.     esac
  225.  
  226.     case $noobjects in
  227.     yes)    BASELIBS=$LIBS;;
  228.     *)    LOCALLIBS=$LIBS;;
  229.     esac
  230.     LIBS='$(LOCALMODLIBS) $(BASEMODLIBS)'
  231.     DEFS="BASEMODLIBS=$BASELIBS$NL$DEFS"
  232.     DEFS="LOCALMODLIBS=$LOCALLIBS$NL$DEFS"
  233.  
  234.     EXTDECLS=
  235.     INITBITS=
  236.     for mod in $MODS
  237.     do
  238.         EXTDECLS="${EXTDECLS}extern void init$mod(void);$NL"
  239.         INITBITS="${INITBITS}    {\"$mod\", init$mod},$NL"
  240.     done
  241.  
  242.  
  243.     case $config in
  244.     -)  ;;
  245.     *)  sed -e "
  246.         1i$NL/* Generated automatically from $config by makesetup. */
  247.         /MARKER 1/i$NL$EXTDECLS
  248.  
  249.         /MARKER 2/i$NL$INITBITS
  250.  
  251.         " $config >config.c
  252.         ;;
  253.     esac
  254.  
  255.     case $makepre in
  256.     -)    ;;
  257.     *)    sedf="@sed.in.$$"
  258.         trap 'rm -f $sedf' 0 1 2 3
  259.         echo "1i\\" >$sedf
  260.         str="# Generated automatically from $makepre by makesetup."
  261.         echo "$str" >>$sedf
  262.         echo "s%_MODOBJS_%$OBJS%" >>$sedf
  263.         echo "s%_MODLIBS_%$LIBS%" >>$sedf
  264.         echo "/Definitions added by makesetup/a$NL$NL$DEFS" >>$sedf
  265.         sed -f $sedf $makepre >Makefile
  266.         cat $rulesf >>Makefile
  267.         rm -f $sedf
  268.         ;;
  269.     esac
  270.  
  271.     rm -f $rulesf
  272. )
  273.