blob: fefe3fd129ee3b2440714aeaec91dd075e92699a [file] [log] [blame]
Guido van Rossumfba715a1994-01-02 00:26:09 +00001#! /bin/sh
2
Guido van Rossumb6775db1994-08-01 11:34:53 +00003# 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:
Neil Schemenauerd32a3372001-01-24 17:16:22 +00009# -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)
Guido van Rossumb6775db1994-08-01 11:34:53 +000012# -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#
Antoine Pitrou961d54c2018-07-16 19:03:03 +020020# See Setup for a description of the format of the Setup file.
Guido van Rossumb6775db1994-08-01 11:34:53 +000021#
22# The following edits are made:
23#
24# Copying config.c.in to config.c:
25# - insert an identifying comment at the start
Guido van Rossum7cc5abd1994-09-12 10:42:20 +000026# - for each <module> mentioned in Setup before *noconfig*:
Martin v. Löwis1a214512008-06-11 05:26:20 +000027# + insert 'extern PyObject* PyInit_<module>(void);' before MARKER 1
28# + insert '{"<module>", PyInit_<module>},' before MARKER 2
Guido van Rossumb6775db1994-08-01 11:34:53 +000029#
30# Copying Makefile.pre to Makefile:
31# - insert an identifying comment at the start
xdegayec0364fc2017-05-27 18:25:03 +020032# - replace _MODBUILT_NAMES_ by the list of *static* and *shared* modules
33# from Setup
34# - replace _MODDISABLED_NAMES_ by the list of *disabled* modules from Setup
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000035# - replace _MODOBJS_ by the list of objects from Setup (except for
Guido van Rossumb6775db1994-08-01 11:34:53 +000036# Setup files after a -n option)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000037# - replace _MODLIBS_ by the list of libraries from Setup
Guido van Rossum7cc5abd1994-09-12 10:42:20 +000038# - for each object file mentioned in Setup, append a rule
39# '<file>.o: <file>.c; <build commands>' to the end of the Makefile
40# - for each module mentioned in Setup, append a rule
41# which creates a shared library version to the end of the Makefile
Guido van Rossumb6775db1994-08-01 11:34:53 +000042# - for each variable definition found in Setup, insert the definition
43# before the comment 'Definitions added by makesetup'
Guido van Rossumfba715a1994-01-02 00:26:09 +000044
Guido van Rossumb6775db1994-08-01 11:34:53 +000045# Loop over command line options
46usage='
Neil Schemenauerd32a3372001-01-24 17:16:22 +000047usage: makesetup [-s srcdir] [-l libdir] [-c config.c.in] [-m Makefile.pre]
Guido van Rossumb6775db1994-08-01 11:34:53 +000048 [Setup] ... [-n [Setup] ...]'
Neil Schemenauerd32a3372001-01-24 17:16:22 +000049srcdir='.'
50libdir=''
Guido van Rossumb6775db1994-08-01 11:34:53 +000051config=''
52makepre=''
53noobjects=''
Guido van Rossum7cc5abd1994-09-12 10:42:20 +000054doconfig=yes
Guido van Rossumb6775db1994-08-01 11:34:53 +000055while :
56do
57 case $1 in
58 -s) shift; srcdir=$1; shift;;
Neil Schemenauerd32a3372001-01-24 17:16:22 +000059 -l) shift; libdir=$1; shift;;
Guido van Rossumb6775db1994-08-01 11:34:53 +000060 -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
67done
68
Neil Schemenauerd32a3372001-01-24 17:16:22 +000069# Set default libdir and config if not set by command line
Guido van Rossumb6775db1994-08-01 11:34:53 +000070# (Not all systems have dirname)
Neil Schemenauer09ac1fd2001-01-27 21:43:40 +000071case $libdir in
Guido van Rossumb6775db1994-08-01 11:34:53 +000072'') case $0 in
Neil Schemenauerd32a3372001-01-24 17:16:22 +000073 */*) libdir=`echo $0 | sed 's,/[^/]*$,,'`;;
74 *) libdir=.;;
Guido van Rossumb6775db1994-08-01 11:34:53 +000075 esac;;
76esac
77case $config in
Neil Schemenauerd32a3372001-01-24 17:16:22 +000078'') config=$libdir/config.c.in;;
Guido van Rossumb6775db1994-08-01 11:34:53 +000079esac
80case $makepre in
81'') makepre=Makefile.pre;;
82esac
83
84# Newline for sed i and a commands
Guido van Rossum4a269861995-06-14 22:52:06 +000085NL='\
86'
Guido van Rossumfba715a1994-01-02 00:26:09 +000087
Martin Panter2dc77f02016-09-16 00:46:05 +000088# Setup to link with extra libraries when making shared extensions.
Guido van Rossumb9619202001-01-10 21:12:18 +000089# Currently, only Cygwin needs this baggage.
90case `uname -s` in
Neil Schemenauerd32a3372001-01-24 17:16:22 +000091CYGWIN*) if test $libdir = .
Guido van Rossumb9619202001-01-10 21:12:18 +000092 then
Neil Schemenauerd32a3372001-01-24 17:16:22 +000093 ExtraLibDir=.
Guido van Rossumb9619202001-01-10 21:12:18 +000094 else
95 ExtraLibDir='$(LIBPL)'
96 fi
Zachary Ware3839d992016-10-01 16:15:09 -050097 ExtraLibs="-L$ExtraLibDir -lpython\$(LDVERSION)";;
Guido van Rossumb9619202001-01-10 21:12:18 +000098esac
99
Guido van Rossumb6775db1994-08-01 11:34:53 +0000100# Main loop
101for i in ${*-Setup}
102do
103 case $i in
Guido van Rossum7cc5abd1994-09-12 10:42:20 +0000104 -n) echo '*noobjects*';;
105 *) echo '*doconfig*'; cat "$i";;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000106 esac
107done |
108sed -e 's/[ ]*#.*//' -e '/^[ ]*$/d' |
Guido van Rossumfba715a1994-01-02 00:26:09 +0000109(
Guido van Rossum7cc5abd1994-09-12 10:42:20 +0000110 rulesf="@rules.$$"
111 trap 'rm -f $rulesf' 0 1 2 3
112 echo "
Victor Stinner27e2d1f2018-11-01 00:52:28 +0100113# Rules appended by makesetup
Guido van Rossum7cc5abd1994-09-12 10:42:20 +0000114" >$rulesf
Guido van Rossumfba715a1994-01-02 00:26:09 +0000115 DEFS=
xdegayec0364fc2017-05-27 18:25:03 +0200116 BUILT=
117 DISABLED=
Guido van Rossumfba715a1994-01-02 00:26:09 +0000118 MODS=
Guido van Rossum7cc5abd1994-09-12 10:42:20 +0000119 SHAREDMODS=
Guido van Rossumfba715a1994-01-02 00:26:09 +0000120 OBJS=
121 LIBS=
Guido van Rossum0b498be1994-08-23 13:49:15 +0000122 LOCALLIBS=
123 BASELIBS=
Guido van Rossumfba715a1994-01-02 00:26:09 +0000124 while read line
125 do
Guido van Rossumae84e8d1999-04-19 17:16:12 +0000126 # to handle backslashes for sh's that don't automatically
127 # continue a read when the last char is a backslash
128 while echo $line | grep '\\$' > /dev/null
129 do
130 read extraline
131 line=`echo $line| sed s/.$//`$extraline
132 done
133
Guido van Rossumb6775db1994-08-01 11:34:53 +0000134 # Output DEFS in reverse order so first definition overrides
Guido van Rossumfba715a1994-01-02 00:26:09 +0000135 case $line in
Guido van Rossumb6775db1994-08-01 11:34:53 +0000136 *=*) DEFS="$line$NL$DEFS"; continue;;
Guido van Rossum9bf84451997-04-02 05:33:00 +0000137 'include '*) DEFS="$line$NL$DEFS"; continue;;
Guido van Rossum7cc5abd1994-09-12 10:42:20 +0000138 '*noobjects*')
Guido van Rossum0b498be1994-08-23 13:49:15 +0000139 case $noobjects in
140 yes) ;;
141 *) LOCALLIBS=$LIBS; LIBS=;;
142 esac
143 noobjects=yes;
Guido van Rossum7cc5abd1994-09-12 10:42:20 +0000144 continue;;
145 '*doconfig*') doconfig=yes; continue;;
Guido van Rossum43554541996-08-20 19:49:13 +0000146 '*static*') doconfig=yes; continue;;
Guido van Rossum7cc5abd1994-09-12 10:42:20 +0000147 '*noconfig*') doconfig=no; continue;;
Guido van Rossum43554541996-08-20 19:49:13 +0000148 '*shared*') doconfig=no; continue;;
xdegayec0364fc2017-05-27 18:25:03 +0200149 '*disabled*') doconfig=disabled; continue;;
Guido van Rossumfba715a1994-01-02 00:26:09 +0000150 esac
Guido van Rossum0b498be1994-08-23 13:49:15 +0000151 srcs=
Guido van Rossumfba715a1994-01-02 00:26:09 +0000152 cpps=
Guido van Rossum7cc5abd1994-09-12 10:42:20 +0000153 libs=
154 mods=
Guido van Rossum4a269861995-06-14 22:52:06 +0000155 skip=
Guido van Rossum7cc5abd1994-09-12 10:42:20 +0000156 for arg in $line
Guido van Rossumfba715a1994-01-02 00:26:09 +0000157 do
Guido van Rossum4a269861995-06-14 22:52:06 +0000158 case $skip in
159 libs) libs="$libs $arg"; skip=; continue;;
160 cpps) cpps="$cpps $arg"; skip=; continue;;
161 srcs) srcs="$srcs $arg"; skip=; continue;;
162 esac
Guido van Rossumfba715a1994-01-02 00:26:09 +0000163 case $arg in
Guido van Rossum2a78cf22001-01-10 21:46:05 +0000164 -framework) libs="$libs $arg"; skip=libs;
165 # OSX/OSXS/Darwin framework link cmd
166 ;;
167 -[IDUCfF]*) cpps="$cpps $arg";;
Andrew M. Kuchlingf4a4fb92002-03-29 18:00:19 +0000168 -Xcompiler) skip=cpps;;
Guido van Rossumb447d111997-12-02 20:38:11 +0000169 -Xlinker) libs="$libs $arg"; skip=libs;;
Guido van Rossum8bfefce1999-05-21 19:18:49 +0000170 -rpath) libs="$libs $arg"; skip=libs;;
Fred Drake091ec2c2000-08-11 13:58:37 +0000171 --rpath) libs="$libs $arg"; skip=libs;;
Guido van Rossum7cc5abd1994-09-12 10:42:20 +0000172 -[A-Zl]*) libs="$libs $arg";;
173 *.a) libs="$libs $arg";;
Guido van Rossuma29756c1996-08-19 22:00:05 +0000174 *.so) libs="$libs $arg";;
175 *.sl) libs="$libs $arg";;
Guido van Rossumfa1fb7d1998-05-14 02:37:45 +0000176 /*.o) libs="$libs $arg";;
Guido van Rossumb9619202001-01-10 21:12:18 +0000177 *.def) libs="$libs $arg";;
Guido van Rossum7cc5abd1994-09-12 10:42:20 +0000178 *.o) srcs="$srcs `basename $arg .o`.c";;
Guido van Rossum0b498be1994-08-23 13:49:15 +0000179 *.[cC]) srcs="$srcs $arg";;
Guido van Rossum2a78cf22001-01-10 21:46:05 +0000180 *.m) srcs="$srcs $arg";; # Objective-C src
Guido van Rossum0b498be1994-08-23 13:49:15 +0000181 *.cc) srcs="$srcs $arg";;
182 *.c++) srcs="$srcs $arg";;
Guido van Rossumbabacb01997-02-14 16:44:04 +0000183 *.cxx) srcs="$srcs $arg";;
184 *.cpp) srcs="$srcs $arg";;
Sjoerd Mullender4237efc1995-10-11 08:45:15 +0000185 \$*) libs="$libs $arg"
186 cpps="$cpps $arg";;
Guido van Rossumfba715a1994-01-02 00:26:09 +0000187 *.*) echo 1>&2 "bad word $arg in $line"
188 exit 1;;
Guido van Rossum4a269861995-06-14 22:52:06 +0000189 -u) skip=libs; libs="$libs -u";;
xdegayec0364fc2017-05-27 18:25:03 +0200190 [a-zA-Z_]*) mods="$mods $arg";;
Guido van Rossumfba715a1994-01-02 00:26:09 +0000191 *) echo 1>&2 "bad word $arg in $line"
192 exit 1;;
193 esac
194 done
Guido van Rossum7cc5abd1994-09-12 10:42:20 +0000195 case $doconfig in
196 yes)
197 LIBS="$LIBS $libs"
198 MODS="$MODS $mods"
xdegayec0364fc2017-05-27 18:25:03 +0200199 BUILT="$BUILT $mods"
200 ;;
201 no)
202 BUILT="$BUILT $mods"
203 ;;
204 disabled)
205 DISABLED="$DISABLED $mods"
206 continue
Guido van Rossum7cc5abd1994-09-12 10:42:20 +0000207 ;;
208 esac
Guido van Rossumb6775db1994-08-01 11:34:53 +0000209 case $noobjects in
210 yes) continue;;
211 esac
Guido van Rossum7cc5abd1994-09-12 10:42:20 +0000212 objs=''
Guido van Rossum0b498be1994-08-23 13:49:15 +0000213 for src in $srcs
214 do
Guido van Rossum7cc5abd1994-09-12 10:42:20 +0000215 case $src in
216 *.c) obj=`basename $src .c`.o; cc='$(CC)';;
Guido van Rossum37da22b2000-12-06 23:46:29 +0000217 *.cc) obj=`basename $src .cc`.o; cc='$(CXX)';;
218 *.c++) obj=`basename $src .c++`.o; cc='$(CXX)';;
219 *.C) obj=`basename $src .C`.o; cc='$(CXX)';;
220 *.cxx) obj=`basename $src .cxx`.o; cc='$(CXX)';;
221 *.cpp) obj=`basename $src .cpp`.o; cc='$(CXX)';;
Guido van Rossumad2c3c72001-03-02 07:09:54 +0000222 *.m) obj=`basename $src .m`.o; cc='$(CC)';; # Obj-C
Guido van Rossum7cc5abd1994-09-12 10:42:20 +0000223 *) continue;;
224 esac
Neil Schemenauerd32a3372001-01-24 17:16:22 +0000225 obj="$srcdir/$obj"
Guido van Rossum7cc5abd1994-09-12 10:42:20 +0000226 objs="$objs $obj"
227 case $src in
228 glmodule.c) ;;
Guido van Rossum76d1f961998-09-09 14:04:51 +0000229 /*) ;;
Skip Montanarofd022672000-07-06 17:58:06 +0000230 \$*) ;;
Neil Schemenauerd32a3372001-01-24 17:16:22 +0000231 *) src='$(srcdir)/'"$srcdir/$src";;
Guido van Rossum7cc5abd1994-09-12 10:42:20 +0000232 esac
233 case $doconfig in
Ned Deilyb1661912014-06-02 01:05:29 -0700234 no) cc="$cc \$(CCSHARED) \$(PY_CFLAGS) \$(PY_CPPFLAGS)";;
Neil Schemenauer83356ef2001-01-26 16:22:26 +0000235 *)
E. M. Brayda324d52018-11-27 03:21:31 +0100236 cc="$cc \$(PY_BUILTIN_MODULE_CFLAGS)";;
Guido van Rossum7cc5abd1994-09-12 10:42:20 +0000237 esac
Neil Schemenauer83356ef2001-01-26 16:22:26 +0000238 rule="$obj: $src; $cc $cpps -c $src -o $obj"
Guido van Rossum7cc5abd1994-09-12 10:42:20 +0000239 echo "$rule" >>$rulesf
Guido van Rossum0b498be1994-08-23 13:49:15 +0000240 done
Guido van Rossum7cc5abd1994-09-12 10:42:20 +0000241 case $doconfig in
242 yes) OBJS="$OBJS $objs";;
243 esac
244 for mod in $mods
245 do
Ned Deilyb1661912014-06-02 01:05:29 -0700246 file="$srcdir/$mod\$(EXT_SUFFIX)"
Guido van Rossum7cc5abd1994-09-12 10:42:20 +0000247 case $doconfig in
248 no) SHAREDMODS="$SHAREDMODS $file";;
249 esac
250 rule="$file: $objs"
Thomas Wouters89d996e2007-09-08 17:39:28 +0000251 rule="$rule; \$(BLDSHARED) $objs $libs $ExtraLibs -o $file"
Guido van Rossum7cc5abd1994-09-12 10:42:20 +0000252 echo "$rule" >>$rulesf
253 done
Guido van Rossumfba715a1994-01-02 00:26:09 +0000254 done
255
Guido van Rossum7cc5abd1994-09-12 10:42:20 +0000256 case $SHAREDMODS in
257 '') ;;
258 *) DEFS="SHAREDMODS=$SHAREDMODS$NL$DEFS";;
259 esac
260
Guido van Rossum0b498be1994-08-23 13:49:15 +0000261 case $noobjects in
262 yes) BASELIBS=$LIBS;;
263 *) LOCALLIBS=$LIBS;;
264 esac
265 LIBS='$(LOCALMODLIBS) $(BASEMODLIBS)'
266 DEFS="BASEMODLIBS=$BASELIBS$NL$DEFS"
267 DEFS="LOCALMODLIBS=$LOCALLIBS$NL$DEFS"
268
Guido van Rossumfba715a1994-01-02 00:26:09 +0000269 EXTDECLS=
270 INITBITS=
271 for mod in $MODS
272 do
Martin v. Löwis1a214512008-06-11 05:26:20 +0000273 EXTDECLS="${EXTDECLS}extern PyObject* PyInit_$mod(void);$NL"
Benjamin Petersona105dd32017-01-16 00:05:12 -0800274 INITBITS="${INITBITS} {\"$mod\", PyInit_$mod},$NL"
Guido van Rossumfba715a1994-01-02 00:26:09 +0000275 done
Guido van Rossumfba715a1994-01-02 00:26:09 +0000276
Guido van Rossum7cc5abd1994-09-12 10:42:20 +0000277
Guido van Rossumb6775db1994-08-01 11:34:53 +0000278 case $config in
279 -) ;;
280 *) sed -e "
281 1i$NL/* Generated automatically from $config by makesetup. */
282 /MARKER 1/i$NL$EXTDECLS
Serhiy Storchakabfbfc8d2015-03-29 19:12:58 +0300283
Guido van Rossumfba715a1994-01-02 00:26:09 +0000284 /MARKER 2/i$NL$INITBITS
285
Guido van Rossumb6775db1994-08-01 11:34:53 +0000286 " $config >config.c
287 ;;
288 esac
Guido van Rossumfba715a1994-01-02 00:26:09 +0000289
Guido van Rossumb6775db1994-08-01 11:34:53 +0000290 case $makepre in
Guido van Rossum7cc5abd1994-09-12 10:42:20 +0000291 -) ;;
292 *) sedf="@sed.in.$$"
293 trap 'rm -f $sedf' 0 1 2 3
294 echo "1i\\" >$sedf
295 str="# Generated automatically from $makepre by makesetup."
296 echo "$str" >>$sedf
xdegayec0364fc2017-05-27 18:25:03 +0200297 echo "s%_MODBUILT_NAMES_%$BUILT%" >>$sedf
298 echo "s%_MODDISABLED_NAMES_%$DISABLED%" >>$sedf
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000299 echo "s%_MODOBJS_%$OBJS%" >>$sedf
300 echo "s%_MODLIBS_%$LIBS%" >>$sedf
Guido van Rossum7cc5abd1994-09-12 10:42:20 +0000301 echo "/Definitions added by makesetup/a$NL$NL$DEFS" >>$sedf
302 sed -f $sedf $makepre >Makefile
303 cat $rulesf >>Makefile
304 rm -f $sedf
Guido van Rossumb6775db1994-08-01 11:34:53 +0000305 ;;
306 esac
Guido van Rossumfba715a1994-01-02 00:26:09 +0000307
Guido van Rossum7cc5abd1994-09-12 10:42:20 +0000308 rm -f $rulesf
Guido van Rossumfba715a1994-01-02 00:26:09 +0000309)