makesetup 7.7 KB

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