Debian Codegraph @

Modules/makesetup

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