Ditched '_find_SET()', since it was a no-value-added wrapper around
[python/dscho.git] / Modules / makesetup
blob18fb682d5a77baea6ed85cd8606a23be3cedcf31
1 #! /bin/sh
3 # Convert templates into Makefile and config.c, based on the module
4 # definitions found in the file Setup.
6 # Usage: makesetup [-s dir] [-c file] [-m file] [Setup] ... [-n [Setup] ...]
8 # Options:
9 # -s directory: alternative source directory (default derived from $0)
10 # -c file: alternative config.c template (default $srcdir/config.c.in)
11 # -c -: don't write config.c
12 # -m file: alternative Makefile template (default ./Makefile.pre)
13 # -m -: don't write Makefile
15 # Remaining arguments are one or more Setup files (default ./Setup).
16 # Setup files after a -n option are used for their variables, modules
17 # and libraries but not for their .o files.
19 # See Setup.in for a description of the format of the Setup file.
21 # The following edits are made:
23 # Copying config.c.in to config.c:
24 # - insert an identifying comment at the start
25 # - for each <module> mentioned in Setup before *noconfig*:
26 # + insert 'extern void init<module>();' before MARKER 1
27 # + insert '{"<module>", initmodule},' before MARKER 2
29 # Copying Makefile.pre to Makefile:
30 # - insert an identifying comment at the start
31 # - replace _MODOBJS_ by the list of objects from Setup (except for
32 # Setup files after a -n option)
33 # - replace _MODLIBS_ by the list of libraries from Setup
34 # - for each object file mentioned in Setup, append a rule
35 # '<file>.o: <file>.c; <build commands>' to the end of the Makefile
36 # - for each module mentioned in Setup, append a rule
37 # which creates a shared library version to the end of the Makefile
38 # - for each variable definition found in Setup, insert the definition
39 # before the comment 'Definitions added by makesetup'
41 # Loop over command line options
42 usage='
43 usage: makesetup [-s srcdir] [-c config.c.in] [-m Makefile.pre]
44 [Setup] ... [-n [Setup] ...]'
45 srcdir=''
46 config=''
47 makepre=''
48 noobjects=''
49 doconfig=yes
50 while :
52 case $1 in
53 -s) shift; srcdir=$1; shift;;
54 -c) shift; config=$1; shift;;
55 -m) shift; makepre=$1; shift;;
56 --) shift; break;;
57 -n) noobjects=yes;;
58 -*) echo "$usage" 1>&2; exit 2;;
59 *) break;;
60 esac
61 done
63 # Set default srcdir and config if not set by command line
64 # (Not all systems have dirname)
65 case $srcdir in
66 '') case $0 in
67 */*) srcdir=`echo $0 | sed 's,/[^/]*$,,'`;;
68 *) srcdir=.;;
69 esac;;
70 esac
71 case $config in
72 '') config=$srcdir/config.c.in;;
73 esac
74 case $makepre in
75 '') makepre=Makefile.pre;;
76 esac
78 # Newline for sed i and a commands
79 NL='\
82 # Main loop
83 for i in ${*-Setup}
85 case $i in
86 -n) echo '*noobjects*';;
87 *) echo '*doconfig*'; cat "$i";;
88 esac
89 done |
90 sed -e 's/[ ]*#.*//' -e '/^[ ]*$/d' |
92 rulesf="@rules.$$"
93 trap 'rm -f $rulesf' 0 1 2 3
94 echo "
95 # Rules appended by makedepend
96 " >$rulesf
97 DEFS=
98 MODS=
99 SHAREDMODS=
100 OBJS=
101 LIBS=
102 LOCALLIBS=
103 BASELIBS=
104 while read line
106 # to handle backslashes for sh's that don't automatically
107 # continue a read when the last char is a backslash
108 while echo $line | grep '\\$' > /dev/null
110 read extraline
111 line=`echo $line| sed s/.$//`$extraline
112 done
114 # Output DEFS in reverse order so first definition overrides
115 case $line in
116 *=*) DEFS="$line$NL$DEFS"; continue;;
117 'include '*) DEFS="$line$NL$DEFS"; continue;;
118 '*noobjects*')
119 case $noobjects in
120 yes) ;;
121 *) LOCALLIBS=$LIBS; LIBS=;;
122 esac
123 noobjects=yes;
124 continue;;
125 '*doconfig*') doconfig=yes; continue;;
126 '*static*') doconfig=yes; continue;;
127 '*noconfig*') doconfig=no; continue;;
128 '*shared*') doconfig=no; continue;;
129 esac
130 srcs=
131 cpps=
132 libs=
133 mods=
134 skip=
135 for arg in $line
137 case $skip in
138 libs) libs="$libs $arg"; skip=; continue;;
139 cpps) cpps="$cpps $arg"; skip=; continue;;
140 srcs) srcs="$srcs $arg"; skip=; continue;;
141 esac
142 case $arg in
143 -[IDUC]*) cpps="$cpps $arg";;
144 -Xlinker) libs="$libs $arg"; skip=libs;;
145 -rpath) libs="$libs $arg"; skip=libs;;
146 -[A-Zl]*) libs="$libs $arg";;
147 *.a) libs="$libs $arg";;
148 *.so) libs="$libs $arg";;
149 *.sl) libs="$libs $arg";;
150 /*.o) libs="$libs $arg";;
151 *.o) srcs="$srcs `basename $arg .o`.c";;
152 *.[cC]) srcs="$srcs $arg";;
153 *.cc) srcs="$srcs $arg";;
154 *.c++) srcs="$srcs $arg";;
155 *.cxx) srcs="$srcs $arg";;
156 *.cpp) srcs="$srcs $arg";;
157 \$*) libs="$libs $arg"
158 cpps="$cpps $arg";;
159 *.*) echo 1>&2 "bad word $arg in $line"
160 exit 1;;
161 -u) skip=libs; libs="$libs -u";;
162 [a-zA-Z_]*) mods="$mods $arg";;
163 *) echo 1>&2 "bad word $arg in $line"
164 exit 1;;
165 esac
166 done
167 case $doconfig in
168 yes)
169 LIBS="$LIBS $libs"
170 MODS="$MODS $mods"
172 esac
173 case $noobjects in
174 yes) continue;;
175 esac
176 objs=''
177 for src in $srcs
179 case $src in
180 *.c) obj=`basename $src .c`.o; cc='$(CC)';;
181 *.cc) obj=`basename $src .cc`.o; cc='$(CCC)';;
182 *.c++) obj=`basename $src .c++`.o; cc='$(CCC)';;
183 *.C) obj=`basename $src .C`.o; cc='$(CCC)';;
184 *.cxx) obj=`basename $src .cxx`.o; cc='$(CCC)';;
185 *.cpp) obj=`basename $src .cpp`.o; cc='$(CCC)';;
186 *) continue;;
187 esac
188 objs="$objs $obj"
189 case $src in
190 glmodule.c) ;;
191 /*) ;;
192 *) src='$(srcdir)/'$src;;
193 esac
194 case $doconfig in
195 no) cc="$cc \$(CCSHARED)";;
196 esac
197 rule="$obj: $src; $cc $cpps \$(CFLAGS) -c $src"
198 echo "$rule" >>$rulesf
199 done
200 case $doconfig in
201 yes) OBJS="$OBJS $objs";;
202 esac
203 for mod in $mods
205 case $objs in
206 *$mod.o*) base=$mod;;
207 *) base=${mod}module;;
208 esac
209 file="$base\$(SO)"
210 case $doconfig in
211 no) SHAREDMODS="$SHAREDMODS $file";;
212 esac
213 rule="$file: $objs"
214 rule="$rule; \$(LDSHARED) $objs $libs -o $file"
215 echo "$rule" >>$rulesf
216 done
217 done
219 case $SHAREDMODS in
220 '') ;;
221 *) DEFS="SHAREDMODS=$SHAREDMODS$NL$DEFS";;
222 esac
224 case $noobjects in
225 yes) BASELIBS=$LIBS;;
226 *) LOCALLIBS=$LIBS;;
227 esac
228 LIBS='$(LOCALMODLIBS) $(BASEMODLIBS)'
229 DEFS="BASEMODLIBS=$BASELIBS$NL$DEFS"
230 DEFS="LOCALMODLIBS=$LOCALLIBS$NL$DEFS"
232 EXTDECLS=
233 INITBITS=
234 for mod in $MODS
236 EXTDECLS="${EXTDECLS}extern void init$mod();$NL"
237 INITBITS="${INITBITS} {\"$mod\", init$mod},$NL"
238 done
241 case $config in
242 -) ;;
243 *) sed -e "
244 1i$NL/* Generated automatically from $config by makesetup. */
245 /MARKER 1/i$NL$EXTDECLS
247 /MARKER 2/i$NL$INITBITS
249 " $config >config.c
251 esac
253 case $makepre in
254 -) ;;
255 *) sedf="@sed.in.$$"
256 trap 'rm -f $sedf' 0 1 2 3
257 echo "1i\\" >$sedf
258 str="# Generated automatically from $makepre by makesetup."
259 echo "$str" >>$sedf
260 echo "s%_MODOBJS_%$OBJS%" >>$sedf
261 echo "s%_MODLIBS_%$LIBS%" >>$sedf
262 echo "/Definitions added by makesetup/a$NL$NL$DEFS" >>$sedf
263 sed -f $sedf $makepre >Makefile
264 cat $rulesf >>Makefile
265 rm -f $sedf
267 esac
269 rm -f $rulesf