blob: 8862c36e7b282566d5ccc953b28af12cbfa3a97f [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#
Fred Drakecf3bc8c2000-10-26 17:07:40 +000020# See Setup.dist 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*:
Thomas Wouters53e7fd72000-07-23 00:02:15 +000027# + insert 'extern void init<module>(void);' before MARKER 1
Guido van Rossumb6775db1994-08-01 11:34:53 +000028# + insert '{"<module>", initmodule},' before MARKER 2
29#
30# Copying Makefile.pre to Makefile:
31# - insert an identifying comment at the start
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000032# - replace _MODOBJS_ by the list of objects from Setup (except for
Guido van Rossumb6775db1994-08-01 11:34:53 +000033# Setup files after a -n option)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +000034# - replace _MODLIBS_ by the list of libraries from Setup
Guido van Rossum7cc5abd1994-09-12 10:42:20 +000035# - for each object file mentioned in Setup, append a rule
36# '<file>.o: <file>.c; <build commands>' to the end of the Makefile
37# - for each module mentioned in Setup, append a rule
38# which creates a shared library version to the end of the Makefile
Guido van Rossumb6775db1994-08-01 11:34:53 +000039# - for each variable definition found in Setup, insert the definition
40# before the comment 'Definitions added by makesetup'
Guido van Rossumfba715a1994-01-02 00:26:09 +000041
Guido van Rossumb6775db1994-08-01 11:34:53 +000042# Loop over command line options
43usage='
Neil Schemenauerd32a3372001-01-24 17:16:22 +000044usage: makesetup [-s srcdir] [-l libdir] [-c config.c.in] [-m Makefile.pre]
Guido van Rossumb6775db1994-08-01 11:34:53 +000045 [Setup] ... [-n [Setup] ...]'
Neil Schemenauerd32a3372001-01-24 17:16:22 +000046srcdir='.'
47libdir=''
Guido van Rossumb6775db1994-08-01 11:34:53 +000048config=''
49makepre=''
50noobjects=''
Guido van Rossum7cc5abd1994-09-12 10:42:20 +000051doconfig=yes
Guido van Rossumb6775db1994-08-01 11:34:53 +000052while :
53do
54 case $1 in
55 -s) shift; srcdir=$1; shift;;
Neil Schemenauerd32a3372001-01-24 17:16:22 +000056 -l) shift; libdir=$1; shift;;
Guido van Rossumb6775db1994-08-01 11:34:53 +000057 -c) shift; config=$1; shift;;
58 -m) shift; makepre=$1; shift;;
59 --) shift; break;;
60 -n) noobjects=yes;;
61 -*) echo "$usage" 1>&2; exit 2;;
62 *) break;;
63 esac
64done
65
Neil Schemenauerd32a3372001-01-24 17:16:22 +000066# Set default libdir and config if not set by command line
Guido van Rossumb6775db1994-08-01 11:34:53 +000067# (Not all systems have dirname)
Neil Schemenauer09ac1fd2001-01-27 21:43:40 +000068case $libdir in
Guido van Rossumb6775db1994-08-01 11:34:53 +000069'') case $0 in
Neil Schemenauerd32a3372001-01-24 17:16:22 +000070 */*) libdir=`echo $0 | sed 's,/[^/]*$,,'`;;
71 *) libdir=.;;
Guido van Rossumb6775db1994-08-01 11:34:53 +000072 esac;;
73esac
74case $config in
Neil Schemenauerd32a3372001-01-24 17:16:22 +000075'') config=$libdir/config.c.in;;
Guido van Rossumb6775db1994-08-01 11:34:53 +000076esac
77case $makepre in
78'') makepre=Makefile.pre;;
79esac
80
81# Newline for sed i and a commands
Guido van Rossum4a269861995-06-14 22:52:06 +000082NL='\
83'
Guido van Rossumfba715a1994-01-02 00:26:09 +000084
Guido van Rossumb9619202001-01-10 21:12:18 +000085# Setup to link with extra libraries when makeing shared extensions.
86# Currently, only Cygwin needs this baggage.
87case `uname -s` in
Neil Schemenauerd32a3372001-01-24 17:16:22 +000088CYGWIN*) if test $libdir = .
Guido van Rossumb9619202001-01-10 21:12:18 +000089 then
Neil Schemenauerd32a3372001-01-24 17:16:22 +000090 ExtraLibDir=.
Guido van Rossumb9619202001-01-10 21:12:18 +000091 else
92 ExtraLibDir='$(LIBPL)'
93 fi
94 ExtraLibs="-L$ExtraLibDir -lpython\$(VERSION)";;
95esac
96
Guido van Rossumb6775db1994-08-01 11:34:53 +000097# Main loop
98for i in ${*-Setup}
99do
100 case $i in
Guido van Rossum7cc5abd1994-09-12 10:42:20 +0000101 -n) echo '*noobjects*';;
102 *) echo '*doconfig*'; cat "$i";;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000103 esac
104done |
105sed -e 's/[ ]*#.*//' -e '/^[ ]*$/d' |
Guido van Rossumfba715a1994-01-02 00:26:09 +0000106(
Guido van Rossum7cc5abd1994-09-12 10:42:20 +0000107 rulesf="@rules.$$"
108 trap 'rm -f $rulesf' 0 1 2 3
109 echo "
110# Rules appended by makedepend
111" >$rulesf
Guido van Rossumfba715a1994-01-02 00:26:09 +0000112 DEFS=
113 MODS=
Guido van Rossum7cc5abd1994-09-12 10:42:20 +0000114 SHAREDMODS=
Guido van Rossumfba715a1994-01-02 00:26:09 +0000115 OBJS=
116 LIBS=
Guido van Rossum0b498be1994-08-23 13:49:15 +0000117 LOCALLIBS=
118 BASELIBS=
Guido van Rossumfba715a1994-01-02 00:26:09 +0000119 while read line
120 do
Guido van Rossumae84e8d1999-04-19 17:16:12 +0000121 # to handle backslashes for sh's that don't automatically
122 # continue a read when the last char is a backslash
123 while echo $line | grep '\\$' > /dev/null
124 do
125 read extraline
126 line=`echo $line| sed s/.$//`$extraline
127 done
128
Guido van Rossumb6775db1994-08-01 11:34:53 +0000129 # Output DEFS in reverse order so first definition overrides
Guido van Rossumfba715a1994-01-02 00:26:09 +0000130 case $line in
Guido van Rossumb6775db1994-08-01 11:34:53 +0000131 *=*) DEFS="$line$NL$DEFS"; continue;;
Guido van Rossum9bf84451997-04-02 05:33:00 +0000132 'include '*) DEFS="$line$NL$DEFS"; continue;;
Guido van Rossum7cc5abd1994-09-12 10:42:20 +0000133 '*noobjects*')
Guido van Rossum0b498be1994-08-23 13:49:15 +0000134 case $noobjects in
135 yes) ;;
136 *) LOCALLIBS=$LIBS; LIBS=;;
137 esac
138 noobjects=yes;
Guido van Rossum7cc5abd1994-09-12 10:42:20 +0000139 continue;;
140 '*doconfig*') doconfig=yes; continue;;
Guido van Rossum43554541996-08-20 19:49:13 +0000141 '*static*') doconfig=yes; continue;;
Guido van Rossum7cc5abd1994-09-12 10:42:20 +0000142 '*noconfig*') doconfig=no; continue;;
Guido van Rossum43554541996-08-20 19:49:13 +0000143 '*shared*') doconfig=no; continue;;
Guido van Rossumfba715a1994-01-02 00:26:09 +0000144 esac
Guido van Rossum0b498be1994-08-23 13:49:15 +0000145 srcs=
Guido van Rossumfba715a1994-01-02 00:26:09 +0000146 cpps=
Guido van Rossum7cc5abd1994-09-12 10:42:20 +0000147 libs=
148 mods=
Guido van Rossum4a269861995-06-14 22:52:06 +0000149 skip=
Guido van Rossum7cc5abd1994-09-12 10:42:20 +0000150 for arg in $line
Guido van Rossumfba715a1994-01-02 00:26:09 +0000151 do
Guido van Rossum4a269861995-06-14 22:52:06 +0000152 case $skip in
153 libs) libs="$libs $arg"; skip=; continue;;
154 cpps) cpps="$cpps $arg"; skip=; continue;;
155 srcs) srcs="$srcs $arg"; skip=; continue;;
156 esac
Guido van Rossumfba715a1994-01-02 00:26:09 +0000157 case $arg in
Guido van Rossum2a78cf22001-01-10 21:46:05 +0000158 -framework) libs="$libs $arg"; skip=libs;
159 # OSX/OSXS/Darwin framework link cmd
160 ;;
161 -[IDUCfF]*) cpps="$cpps $arg";;
Andrew M. Kuchlingf4a4fb92002-03-29 18:00:19 +0000162 -Xcompiler) skip=cpps;;
Guido van Rossumb447d111997-12-02 20:38:11 +0000163 -Xlinker) libs="$libs $arg"; skip=libs;;
Guido van Rossum8bfefce1999-05-21 19:18:49 +0000164 -rpath) libs="$libs $arg"; skip=libs;;
Fred Drake091ec2c2000-08-11 13:58:37 +0000165 --rpath) libs="$libs $arg"; skip=libs;;
Guido van Rossum7cc5abd1994-09-12 10:42:20 +0000166 -[A-Zl]*) libs="$libs $arg";;
167 *.a) libs="$libs $arg";;
Guido van Rossuma29756c1996-08-19 22:00:05 +0000168 *.so) libs="$libs $arg";;
169 *.sl) libs="$libs $arg";;
Guido van Rossumfa1fb7d1998-05-14 02:37:45 +0000170 /*.o) libs="$libs $arg";;
Guido van Rossumb9619202001-01-10 21:12:18 +0000171 *.def) libs="$libs $arg";;
Guido van Rossum7cc5abd1994-09-12 10:42:20 +0000172 *.o) srcs="$srcs `basename $arg .o`.c";;
Guido van Rossum0b498be1994-08-23 13:49:15 +0000173 *.[cC]) srcs="$srcs $arg";;
Guido van Rossum2a78cf22001-01-10 21:46:05 +0000174 *.m) srcs="$srcs $arg";; # Objective-C src
Guido van Rossum0b498be1994-08-23 13:49:15 +0000175 *.cc) srcs="$srcs $arg";;
176 *.c++) srcs="$srcs $arg";;
Guido van Rossumbabacb01997-02-14 16:44:04 +0000177 *.cxx) srcs="$srcs $arg";;
178 *.cpp) srcs="$srcs $arg";;
Sjoerd Mullender4237efc1995-10-11 08:45:15 +0000179 \$*) libs="$libs $arg"
180 cpps="$cpps $arg";;
Guido van Rossumfba715a1994-01-02 00:26:09 +0000181 *.*) echo 1>&2 "bad word $arg in $line"
182 exit 1;;
Guido van Rossum4a269861995-06-14 22:52:06 +0000183 -u) skip=libs; libs="$libs -u";;
Guido van Rossum7cc5abd1994-09-12 10:42:20 +0000184 [a-zA-Z_]*) mods="$mods $arg";;
Guido van Rossumfba715a1994-01-02 00:26:09 +0000185 *) echo 1>&2 "bad word $arg in $line"
186 exit 1;;
187 esac
188 done
Guido van Rossum7cc5abd1994-09-12 10:42:20 +0000189 case $doconfig in
190 yes)
191 LIBS="$LIBS $libs"
192 MODS="$MODS $mods"
193 ;;
194 esac
Guido van Rossumb6775db1994-08-01 11:34:53 +0000195 case $noobjects in
196 yes) continue;;
197 esac
Guido van Rossum7cc5abd1994-09-12 10:42:20 +0000198 objs=''
Guido van Rossum0b498be1994-08-23 13:49:15 +0000199 for src in $srcs
200 do
Guido van Rossum7cc5abd1994-09-12 10:42:20 +0000201 case $src in
202 *.c) obj=`basename $src .c`.o; cc='$(CC)';;
Guido van Rossum37da22b2000-12-06 23:46:29 +0000203 *.cc) obj=`basename $src .cc`.o; cc='$(CXX)';;
204 *.c++) obj=`basename $src .c++`.o; cc='$(CXX)';;
205 *.C) obj=`basename $src .C`.o; cc='$(CXX)';;
206 *.cxx) obj=`basename $src .cxx`.o; cc='$(CXX)';;
207 *.cpp) obj=`basename $src .cpp`.o; cc='$(CXX)';;
Guido van Rossumad2c3c72001-03-02 07:09:54 +0000208 *.m) obj=`basename $src .m`.o; cc='$(CC)';; # Obj-C
Guido van Rossum7cc5abd1994-09-12 10:42:20 +0000209 *) continue;;
210 esac
Neil Schemenauerd32a3372001-01-24 17:16:22 +0000211 obj="$srcdir/$obj"
Guido van Rossum7cc5abd1994-09-12 10:42:20 +0000212 objs="$objs $obj"
213 case $src in
214 glmodule.c) ;;
Guido van Rossum76d1f961998-09-09 14:04:51 +0000215 /*) ;;
Skip Montanarofd022672000-07-06 17:58:06 +0000216 \$*) ;;
Neil Schemenauerd32a3372001-01-24 17:16:22 +0000217 *) src='$(srcdir)/'"$srcdir/$src";;
Guido van Rossum7cc5abd1994-09-12 10:42:20 +0000218 esac
219 case $doconfig in
Neil Schemenauer89e90d62001-06-02 06:16:02 +0000220 no) cc="$cc \$(CCSHARED) \$(CFLAGS) \$(CPPFLAGS)";;
Neil Schemenauer83356ef2001-01-26 16:22:26 +0000221 *)
222 cc="$cc \$(PY_CFLAGS)";;
Guido van Rossum7cc5abd1994-09-12 10:42:20 +0000223 esac
Neil Schemenauer83356ef2001-01-26 16:22:26 +0000224 rule="$obj: $src; $cc $cpps -c $src -o $obj"
Guido van Rossum7cc5abd1994-09-12 10:42:20 +0000225 echo "$rule" >>$rulesf
Guido van Rossum0b498be1994-08-23 13:49:15 +0000226 done
Guido van Rossum7cc5abd1994-09-12 10:42:20 +0000227 case $doconfig in
228 yes) OBJS="$OBJS $objs";;
229 esac
230 for mod in $mods
231 do
232 case $objs in
233 *$mod.o*) base=$mod;;
234 *) base=${mod}module;;
235 esac
Neil Schemenauer83356ef2001-01-26 16:22:26 +0000236 file="$srcdir/$base\$(SO)"
Guido van Rossum7cc5abd1994-09-12 10:42:20 +0000237 case $doconfig in
238 no) SHAREDMODS="$SHAREDMODS $file";;
239 esac
240 rule="$file: $objs"
Martin v. Löwisd44a4e92007-09-05 11:47:34 +0000241 rule="$rule; \$(BLDSHARED) $objs $libs $ExtraLibs -o $file"
Guido van Rossum7cc5abd1994-09-12 10:42:20 +0000242 echo "$rule" >>$rulesf
243 done
Guido van Rossumfba715a1994-01-02 00:26:09 +0000244 done
245
Guido van Rossum7cc5abd1994-09-12 10:42:20 +0000246 case $SHAREDMODS in
247 '') ;;
248 *) DEFS="SHAREDMODS=$SHAREDMODS$NL$DEFS";;
249 esac
250
Guido van Rossum0b498be1994-08-23 13:49:15 +0000251 case $noobjects in
252 yes) BASELIBS=$LIBS;;
253 *) LOCALLIBS=$LIBS;;
254 esac
255 LIBS='$(LOCALMODLIBS) $(BASEMODLIBS)'
256 DEFS="BASEMODLIBS=$BASELIBS$NL$DEFS"
257 DEFS="LOCALMODLIBS=$LOCALLIBS$NL$DEFS"
258
Guido van Rossumfba715a1994-01-02 00:26:09 +0000259 EXTDECLS=
260 INITBITS=
261 for mod in $MODS
262 do
Thomas Wouters53e7fd72000-07-23 00:02:15 +0000263 EXTDECLS="${EXTDECLS}extern void init$mod(void);$NL"
Guido van Rossumfba715a1994-01-02 00:26:09 +0000264 INITBITS="${INITBITS} {\"$mod\", init$mod},$NL"
265 done
Guido van Rossumfba715a1994-01-02 00:26:09 +0000266
Guido van Rossum7cc5abd1994-09-12 10:42:20 +0000267
Guido van Rossumb6775db1994-08-01 11:34:53 +0000268 case $config in
269 -) ;;
270 *) sed -e "
271 1i$NL/* Generated automatically from $config by makesetup. */
272 /MARKER 1/i$NL$EXTDECLS
Guido van Rossum3886bb61998-12-04 18:50:17 +0000273
Guido van Rossumfba715a1994-01-02 00:26:09 +0000274 /MARKER 2/i$NL$INITBITS
275
Guido van Rossumb6775db1994-08-01 11:34:53 +0000276 " $config >config.c
277 ;;
278 esac
Guido van Rossumfba715a1994-01-02 00:26:09 +0000279
Guido van Rossumb6775db1994-08-01 11:34:53 +0000280 case $makepre in
Guido van Rossum7cc5abd1994-09-12 10:42:20 +0000281 -) ;;
282 *) sedf="@sed.in.$$"
283 trap 'rm -f $sedf' 0 1 2 3
284 echo "1i\\" >$sedf
285 str="# Generated automatically from $makepre by makesetup."
286 echo "$str" >>$sedf
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000287 echo "s%_MODOBJS_%$OBJS%" >>$sedf
288 echo "s%_MODLIBS_%$LIBS%" >>$sedf
Guido van Rossum7cc5abd1994-09-12 10:42:20 +0000289 echo "/Definitions added by makesetup/a$NL$NL$DEFS" >>$sedf
290 sed -f $sedf $makepre >Makefile
291 cat $rulesf >>Makefile
292 rm -f $sedf
Guido van Rossumb6775db1994-08-01 11:34:53 +0000293 ;;
294 esac
Guido van Rossumfba715a1994-01-02 00:26:09 +0000295
Guido van Rossum7cc5abd1994-09-12 10:42:20 +0000296 rm -f $rulesf
Guido van Rossumfba715a1994-01-02 00:26:09 +0000297)