makesetup 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. #! /bin/sh
  2. set -e
  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 .)
  10. # -l directory: library source directory (default derived from $0)
  11. # -c file: alternative config.c template (default $libdir/config.c.in)
  12. # -c -: don't write config.c
  13. # -m file: alternative Makefile template (default ./Makefile.pre)
  14. # -m -: don't write Makefile
  15. #
  16. # Remaining arguments are one or more Setup files (default ./Setup).
  17. # Setup files after a -n option are used for their variables, modules
  18. # and libraries but not for their .o files.
  19. #
  20. # See Setup for a description of the format of the Setup file.
  21. #
  22. # The following edits are made:
  23. #
  24. # Copying config.c.in to config.c:
  25. # - insert an identifying comment at the start
  26. # - for each <module> mentioned in Setup before *noconfig*:
  27. # + insert 'extern PyObject* PyInit_<module>(void);' before MARKER 1
  28. # + insert '{"<module>", PyInit_<module>},' before MARKER 2
  29. #
  30. # Copying Makefile.pre to Makefile:
  31. # - insert an identifying comment at the start
  32. # - replace _MODBUILT_NAMES_ by the list of *static* and *shared* modules
  33. # from Setup
  34. # - replace _MODBSHARED_NAMES_ by the list of *shared* modules from Setup
  35. # - replace _MODDISABLED_NAMES_ by the list of *disabled* modules from Setup
  36. # - replace _MODOBJS_ by the list of objects from Setup (except for
  37. # Setup files after a -n option)
  38. # - replace _MODLIBS_ by the list of libraries from Setup
  39. # - for each object file mentioned in Setup, append a rule
  40. # '<file>.o: <file>.c; <build commands>' to the end of the Makefile
  41. # - for each module mentioned in Setup, append a rule
  42. # which creates a shared library version to the end of the Makefile
  43. # - for each variable definition found in Setup, insert the definition
  44. # before the comment 'Definitions added by makesetup'
  45. # Loop over command line options
  46. usage='
  47. usage: makesetup [-s srcdir] [-l libdir] [-c config.c.in] [-m Makefile.pre]
  48. [Setup] ... [-n [Setup] ...]'
  49. srcdir='.'
  50. libdir=''
  51. config=''
  52. makepre=''
  53. noobjects=''
  54. doconfig=yes
  55. while :
  56. do
  57. case $1 in
  58. -s) shift; srcdir=$1; shift;;
  59. -l) shift; libdir=$1; shift;;
  60. -c) shift; config=$1; shift;;
  61. -m) shift; makepre=$1; shift;;
  62. --) shift; break;;
  63. -n) noobjects=yes;;
  64. -*) echo "$usage" 1>&2; exit 2;;
  65. *) break;;
  66. esac
  67. done
  68. # Set default libdir and config if not set by command line
  69. # (Not all systems have dirname)
  70. case $libdir in
  71. '') case $0 in
  72. */*) libdir=`echo $0 | sed 's,/[^/]*$,,'`;;
  73. *) libdir=.;;
  74. esac;;
  75. esac
  76. case $config in
  77. '') config=$libdir/config.c.in;;
  78. esac
  79. case $makepre in
  80. '') makepre=Makefile.pre;;
  81. esac
  82. # Newline for sed i and a commands
  83. NL='\
  84. '
  85. # Setup to link with extra libraries when making shared extensions.
  86. # Currently, only Cygwin needs this baggage.
  87. case `uname -s` in
  88. CYGWIN*) if test $libdir = .
  89. then
  90. ExtraLibDir=.
  91. else
  92. ExtraLibDir='$(LIBPL)'
  93. fi
  94. ExtraLibs="-L$ExtraLibDir -lpython\$(LDVERSION)";;
  95. esac
  96. # Main loop
  97. for i in ${*-Setup}
  98. do
  99. case $i in
  100. -n) echo '*noobjects*';;
  101. *) echo '*doconfig*'; cat "$i";;
  102. esac
  103. done |
  104. sed -e 's/[ ]*#.*//' -e '/^[ ]*$/d' |
  105. (
  106. rulesf="@rules.$$"
  107. trap 'rm -f $rulesf' 0 1 2 3
  108. echo "
  109. # Rules appended by makesetup
  110. " >$rulesf
  111. DEFS=
  112. BUILT=
  113. BUILT_SHARED=
  114. DISABLED=
  115. CONFIGURED=
  116. MODS=
  117. SHAREDMODS=
  118. OBJS=
  119. LIBS=
  120. LOCALLIBS=
  121. BASELIBS=
  122. while read line
  123. do
  124. # to handle backslashes for sh's that don't automatically
  125. # continue a read when the last char is a backslash
  126. while echo $line | grep '\\$' > /dev/null
  127. do
  128. read extraline
  129. line=`echo $line| sed s/.$//`$extraline
  130. done
  131. # Output DEFS in reverse order so first definition overrides
  132. case $line in
  133. *=*) DEFS="$line$NL$DEFS"; continue;;
  134. 'include '*) DEFS="$line$NL$DEFS"; continue;;
  135. '*noobjects*')
  136. case $noobjects in
  137. yes) ;;
  138. *) LOCALLIBS=$LIBS; LIBS=;;
  139. esac
  140. noobjects=yes;
  141. continue;;
  142. '*doconfig*') doconfig=yes; continue;;
  143. '*static*') doconfig=yes; continue;;
  144. '*noconfig*') doconfig=no; continue;;
  145. '*shared*') doconfig=no; continue;;
  146. '*disabled*') doconfig=disabled; continue;;
  147. esac
  148. srcs=
  149. cpps=
  150. libs=
  151. mods=
  152. mods_upper=
  153. skip=
  154. for arg in $line
  155. do
  156. case $skip in
  157. libs) libs="$libs $arg"; skip=; continue;;
  158. cpps) cpps="$cpps $arg"; skip=; continue;;
  159. srcs) srcs="$srcs $arg"; skip=; continue;;
  160. esac
  161. case $arg in
  162. -framework) libs="$libs $arg"; skip=libs;
  163. # OSX/OSXS/Darwin framework link cmd
  164. ;;
  165. -[IDUCfF]*) cpps="$cpps $arg";;
  166. -Xcompiler) skip=cpps;;
  167. -Xlinker) libs="$libs $arg"; skip=libs;;
  168. -rpath) libs="$libs $arg"; skip=libs;;
  169. --rpath) libs="$libs $arg"; skip=libs;;
  170. -[A-Zl]*) libs="$libs $arg";;
  171. *.a) libs="$libs $arg";;
  172. *.so) libs="$libs $arg";;
  173. *.sl) libs="$libs $arg";;
  174. /*.o) libs="$libs $arg";;
  175. *.def) libs="$libs $arg";;
  176. *.o) srcs="$srcs `basename $arg .o`.c";;
  177. *.[cC]) srcs="$srcs $arg";;
  178. *.m) srcs="$srcs $arg";; # Objective-C src
  179. *.cc) srcs="$srcs $arg";;
  180. *.c++) srcs="$srcs $arg";;
  181. *.cxx) srcs="$srcs $arg";;
  182. *.cpp) srcs="$srcs $arg";;
  183. \$\(*_CFLAGS\)) cpps="$cpps $arg";;
  184. \$\(*_INCLUDES\)) cpps="$cpps $arg";;
  185. \$\(*_LIBS\)) libs="$libs $arg";;
  186. \$\(*_LDFLAGS\)) libs="$libs $arg";;
  187. \$\(*_RPATH\)) libs="$libs $arg";;
  188. \$*) libs="$libs $arg"
  189. cpps="$cpps $arg";;
  190. *.*) echo 1>&2 "bad word $arg in $line"
  191. exit 1;;
  192. -u) skip=libs; libs="$libs -u";;
  193. [a-zA-Z_]*)
  194. mods="$mods $arg"
  195. mods_upper=$(echo $mods | tr '[a-z]' '[A-Z]');;
  196. *) echo 1>&2 "bad word $arg in $line"
  197. exit 1;;
  198. esac
  199. done
  200. if test -z "$cpps" -a -z "$libs"; then
  201. cpps="\$(MODULE_${mods_upper}_CFLAGS)"
  202. libs="\$(MODULE_${mods_upper}_LDFLAGS)"
  203. fi
  204. for mod in $mods
  205. do
  206. case $CONFIGURED in
  207. *,${mod},*)
  208. # Detected multiple rules for a module, first rule wins. This
  209. # allows users to disable modules in Setup.local.
  210. echo 1>&2 "maksetup: '$mod' was handled by previous rule."
  211. continue 2;;
  212. esac
  213. CONFIGURED="$CONFIGURED,${mod},"
  214. done
  215. case $doconfig in
  216. yes)
  217. LIBS="$LIBS $libs"
  218. MODS="$MODS $mods"
  219. BUILT="$BUILT $mods"
  220. ;;
  221. no)
  222. BUILT="$BUILT $mods"
  223. ;;
  224. disabled)
  225. DISABLED="$DISABLED $mods"
  226. continue
  227. ;;
  228. esac
  229. case $noobjects in
  230. yes) continue;;
  231. esac
  232. objs=''
  233. for src in $srcs
  234. do
  235. case $src in
  236. *.c) obj=`basename $src .c`.o; cc='$(CC)';;
  237. *.cc) obj=`basename $src .cc`.o; cc='$(CXX)';;
  238. *.c++) obj=`basename $src .c++`.o; cc='$(CXX)';;
  239. *.C) obj=`basename $src .C`.o; cc='$(CXX)';;
  240. *.cxx) obj=`basename $src .cxx`.o; cc='$(CXX)';;
  241. *.cpp) obj=`basename $src .cpp`.o; cc='$(CXX)';;
  242. *.m) obj=`basename $src .m`.o; cc='$(CC)';; # Obj-C
  243. *) continue;;
  244. esac
  245. case $src in
  246. */*) obj="$srcdir/`dirname $src`/$obj";;
  247. *) obj="$srcdir/$obj";;
  248. esac
  249. objs="$objs $obj"
  250. case $src in
  251. glmodule.c) ;;
  252. /*) ;;
  253. \$*) ;;
  254. *) src='$(srcdir)/'"$srcdir/$src";;
  255. esac
  256. # custom flags first, PY_STDMODULE_CFLAGS may contain -I with system libmpdec
  257. case $doconfig in
  258. no) cc="$cc $cpps \$(PY_STDMODULE_CFLAGS) \$(CCSHARED)";;
  259. *)
  260. cc="$cc $cpps \$(PY_BUILTIN_MODULE_CFLAGS)";;
  261. esac
  262. # force rebuild when header file or module build flavor (static/shared) is changed
  263. rule="$obj: $src \$(MODULE_${mods_upper}_DEPS) \$(PYTHON_HEADERS) Modules/config.c; $cc -c $src -o $obj"
  264. echo "$rule" >>$rulesf
  265. done
  266. case $doconfig in
  267. yes) OBJS="$OBJS $objs";;
  268. esac
  269. for mod in $mods
  270. do
  271. file="$srcdir/$mod\$(EXT_SUFFIX)"
  272. case $doconfig in
  273. no)
  274. SHAREDMODS="$SHAREDMODS $file"
  275. BUILT_SHARED="$BUILT_SHARED $mod"
  276. ;;
  277. esac
  278. rule="$file: $objs"
  279. rule="$rule; \$(BLDSHARED) $objs $libs $ExtraLibs -o $file"
  280. echo "$rule" >>$rulesf
  281. done
  282. done
  283. case $SHAREDMODS in
  284. '') ;;
  285. *) DEFS="SHAREDMODS=$SHAREDMODS$NL$DEFS";;
  286. esac
  287. case $noobjects in
  288. yes) BASELIBS=$LIBS;;
  289. *) LOCALLIBS=$LIBS;;
  290. esac
  291. LIBS='$(LOCALMODLIBS) $(BASEMODLIBS)'
  292. DEFS="BASEMODLIBS=$BASELIBS$NL$DEFS"
  293. DEFS="LOCALMODLIBS=$LOCALLIBS$NL$DEFS"
  294. EXTDECLS=
  295. INITBITS=
  296. for mod in $MODS
  297. do
  298. EXTDECLS="${EXTDECLS}extern PyObject* PyInit_$mod(void);$NL"
  299. INITBITS="${INITBITS} {\"$mod\", PyInit_$mod},$NL"
  300. done
  301. case $config in
  302. -) ;;
  303. *) sed -e "
  304. 1i$NL/* Generated automatically from $config by makesetup. */
  305. /MARKER 1/i$NL$EXTDECLS
  306. /MARKER 2/i$NL$INITBITS
  307. " $config >config.c
  308. ;;
  309. esac
  310. case $makepre in
  311. -) ;;
  312. *)
  313. # macOS' sed has issues with 'a' command. Use 'r' command with an
  314. # external replacement file instead.
  315. sedf="@sed.in.$$"
  316. sedr="@sed.replace.$$"
  317. trap 'rm -f $sedf $sedr' 0 1 2 3
  318. echo "$NL$NL$DEFS" | sed 's/\\$//' > $sedr
  319. echo "1i\\" >$sedf
  320. str="# Generated automatically from $makepre by makesetup."
  321. echo "$str" >>$sedf
  322. echo "s%_MODBUILT_NAMES_%$BUILT%" >>$sedf
  323. echo "s%_MODSHARED_NAMES_%$BUILT_SHARED%" >>$sedf
  324. echo "s%_MODDISABLED_NAMES_%$DISABLED%" >>$sedf
  325. echo "s%_MODOBJS_%$OBJS%" >>$sedf
  326. echo "s%_MODLIBS_%$LIBS%" >>$sedf
  327. echo "/Definitions added by makesetup/r $sedr" >>$sedf
  328. sed -f $sedf $makepre >Makefile
  329. cat $rulesf >>Makefile
  330. rm -f $sedf $sedr
  331. ;;
  332. esac
  333. rm -f $rulesf
  334. )