blob: fefe3fd129ee3b2440714aeaec91dd075e92699a [file] [log] [blame]
xf.li86118912025-03-19 20:07:27 -07001#! /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 .)
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 _MODDISABLED_NAMES_ by the list of *disabled* modules from Setup
35# - replace _MODOBJS_ by the list of objects from Setup (except for
36# Setup files after a -n option)
37# - replace _MODLIBS_ by the list of libraries from Setup
38# - 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
42# - for each variable definition found in Setup, insert the definition
43# before the comment 'Definitions added by makesetup'
44
45# Loop over command line options
46usage='
47usage: makesetup [-s srcdir] [-l libdir] [-c config.c.in] [-m Makefile.pre]
48 [Setup] ... [-n [Setup] ...]'
49srcdir='.'
50libdir=''
51config=''
52makepre=''
53noobjects=''
54doconfig=yes
55while :
56do
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
67done
68
69# Set default libdir and config if not set by command line
70# (Not all systems have dirname)
71case $libdir in
72'') case $0 in
73 */*) libdir=`echo $0 | sed 's,/[^/]*$,,'`;;
74 *) libdir=.;;
75 esac;;
76esac
77case $config in
78'') config=$libdir/config.c.in;;
79esac
80case $makepre in
81'') makepre=Makefile.pre;;
82esac
83
84# Newline for sed i and a commands
85NL='\
86'
87
88# Setup to link with extra libraries when making shared extensions.
89# Currently, only Cygwin needs this baggage.
90case `uname -s` in
91CYGWIN*) if test $libdir = .
92 then
93 ExtraLibDir=.
94 else
95 ExtraLibDir='$(LIBPL)'
96 fi
97 ExtraLibs="-L$ExtraLibDir -lpython\$(LDVERSION)";;
98esac
99
100# Main loop
101for i in ${*-Setup}
102do
103 case $i in
104 -n) echo '*noobjects*';;
105 *) echo '*doconfig*'; cat "$i";;
106 esac
107done |
108sed -e 's/[ ]*#.*//' -e '/^[ ]*$/d' |
109(
110 rulesf="@rules.$$"
111 trap 'rm -f $rulesf' 0 1 2 3
112 echo "
113# Rules appended by makesetup
114" >$rulesf
115 DEFS=
116 BUILT=
117 DISABLED=
118 MODS=
119 SHAREDMODS=
120 OBJS=
121 LIBS=
122 LOCALLIBS=
123 BASELIBS=
124 while read line
125 do
126 # 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
134 # Output DEFS in reverse order so first definition overrides
135 case $line in
136 *=*) DEFS="$line$NL$DEFS"; continue;;
137 'include '*) DEFS="$line$NL$DEFS"; continue;;
138 '*noobjects*')
139 case $noobjects in
140 yes) ;;
141 *) LOCALLIBS=$LIBS; LIBS=;;
142 esac
143 noobjects=yes;
144 continue;;
145 '*doconfig*') doconfig=yes; continue;;
146 '*static*') doconfig=yes; continue;;
147 '*noconfig*') doconfig=no; continue;;
148 '*shared*') doconfig=no; continue;;
149 '*disabled*') doconfig=disabled; continue;;
150 esac
151 srcs=
152 cpps=
153 libs=
154 mods=
155 skip=
156 for arg in $line
157 do
158 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
163 case $arg in
164 -framework) libs="$libs $arg"; skip=libs;
165 # OSX/OSXS/Darwin framework link cmd
166 ;;
167 -[IDUCfF]*) cpps="$cpps $arg";;
168 -Xcompiler) skip=cpps;;
169 -Xlinker) libs="$libs $arg"; skip=libs;;
170 -rpath) libs="$libs $arg"; skip=libs;;
171 --rpath) libs="$libs $arg"; skip=libs;;
172 -[A-Zl]*) libs="$libs $arg";;
173 *.a) libs="$libs $arg";;
174 *.so) libs="$libs $arg";;
175 *.sl) libs="$libs $arg";;
176 /*.o) libs="$libs $arg";;
177 *.def) libs="$libs $arg";;
178 *.o) srcs="$srcs `basename $arg .o`.c";;
179 *.[cC]) srcs="$srcs $arg";;
180 *.m) srcs="$srcs $arg";; # Objective-C src
181 *.cc) srcs="$srcs $arg";;
182 *.c++) srcs="$srcs $arg";;
183 *.cxx) srcs="$srcs $arg";;
184 *.cpp) srcs="$srcs $arg";;
185 \$*) libs="$libs $arg"
186 cpps="$cpps $arg";;
187 *.*) echo 1>&2 "bad word $arg in $line"
188 exit 1;;
189 -u) skip=libs; libs="$libs -u";;
190 [a-zA-Z_]*) mods="$mods $arg";;
191 *) echo 1>&2 "bad word $arg in $line"
192 exit 1;;
193 esac
194 done
195 case $doconfig in
196 yes)
197 LIBS="$LIBS $libs"
198 MODS="$MODS $mods"
199 BUILT="$BUILT $mods"
200 ;;
201 no)
202 BUILT="$BUILT $mods"
203 ;;
204 disabled)
205 DISABLED="$DISABLED $mods"
206 continue
207 ;;
208 esac
209 case $noobjects in
210 yes) continue;;
211 esac
212 objs=''
213 for src in $srcs
214 do
215 case $src in
216 *.c) obj=`basename $src .c`.o; cc='$(CC)';;
217 *.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)';;
222 *.m) obj=`basename $src .m`.o; cc='$(CC)';; # Obj-C
223 *) continue;;
224 esac
225 obj="$srcdir/$obj"
226 objs="$objs $obj"
227 case $src in
228 glmodule.c) ;;
229 /*) ;;
230 \$*) ;;
231 *) src='$(srcdir)/'"$srcdir/$src";;
232 esac
233 case $doconfig in
234 no) cc="$cc \$(CCSHARED) \$(PY_CFLAGS) \$(PY_CPPFLAGS)";;
235 *)
236 cc="$cc \$(PY_BUILTIN_MODULE_CFLAGS)";;
237 esac
238 rule="$obj: $src; $cc $cpps -c $src -o $obj"
239 echo "$rule" >>$rulesf
240 done
241 case $doconfig in
242 yes) OBJS="$OBJS $objs";;
243 esac
244 for mod in $mods
245 do
246 file="$srcdir/$mod\$(EXT_SUFFIX)"
247 case $doconfig in
248 no) SHAREDMODS="$SHAREDMODS $file";;
249 esac
250 rule="$file: $objs"
251 rule="$rule; \$(BLDSHARED) $objs $libs $ExtraLibs -o $file"
252 echo "$rule" >>$rulesf
253 done
254 done
255
256 case $SHAREDMODS in
257 '') ;;
258 *) DEFS="SHAREDMODS=$SHAREDMODS$NL$DEFS";;
259 esac
260
261 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
269 EXTDECLS=
270 INITBITS=
271 for mod in $MODS
272 do
273 EXTDECLS="${EXTDECLS}extern PyObject* PyInit_$mod(void);$NL"
274 INITBITS="${INITBITS} {\"$mod\", PyInit_$mod},$NL"
275 done
276
277
278 case $config in
279 -) ;;
280 *) sed -e "
281 1i$NL/* Generated automatically from $config by makesetup. */
282 /MARKER 1/i$NL$EXTDECLS
283
284 /MARKER 2/i$NL$INITBITS
285
286 " $config >config.c
287 ;;
288 esac
289
290 case $makepre in
291 -) ;;
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
297 echo "s%_MODBUILT_NAMES_%$BUILT%" >>$sedf
298 echo "s%_MODDISABLED_NAMES_%$DISABLED%" >>$sedf
299 echo "s%_MODOBJS_%$OBJS%" >>$sedf
300 echo "s%_MODLIBS_%$LIBS%" >>$sedf
301 echo "/Definitions added by makesetup/a$NL$NL$DEFS" >>$sedf
302 sed -f $sedf $makepre >Makefile
303 cat $rulesf >>Makefile
304 rm -f $sedf
305 ;;
306 esac
307
308 rm -f $rulesf
309)