1#! /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.dist 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 void init<module>(void);' before MARKER 1
28#   + insert '{"<module>", initmodule},' before MARKER 2
29#
30# Copying Makefile.pre to Makefile:
31# - insert an identifying comment at the start
32# - replace _MODOBJS_ by the list of objects from Setup (except for
33#   Setup files after a -n option)
34# - replace _MODLIBS_ by the list of libraries from Setup
35# - 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
39# - for each variable definition found in Setup, insert the definition
40#   before the comment 'Definitions added by makesetup'
41
42# Loop over command line options
43usage='
44usage: makesetup [-s srcdir] [-l libdir] [-c config.c.in] [-m Makefile.pre]
45                 [Setup] ... [-n [Setup] ...]'
46srcdir='.'
47libdir=''
48config=''
49makepre=''
50noobjects=''
51doconfig=yes
52while :
53do
54	case $1 in
55	-s)	shift; srcdir=$1; shift;;
56	-l)	shift; libdir=$1; shift;;
57	-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
66# Set default libdir and config if not set by command line
67# (Not all systems have dirname)
68case $libdir in
69'')	case $0 in
70	*/*)	libdir=`echo $0 | sed 's,/[^/]*$,,'`;;
71	*)	libdir=.;;
72	esac;;
73esac
74case $config in
75'')	config=$libdir/config.c.in;;
76esac
77case $makepre in
78'')	makepre=Makefile.pre;;
79esac
80
81# Newline for sed i and a commands
82NL='\
83'
84
85# Setup to link with extra libraries when makeing shared extensions.
86# Currently, only Cygwin needs this baggage.
87case `uname -s` in
88CYGWIN*) if test $libdir = .
89	 then
90	 	ExtraLibDir=.
91	 else
92	 	ExtraLibDir='$(LIBPL)'
93	 fi
94	 ExtraLibs="-L$ExtraLibDir -lpython\$(VERSION)";;
95esac
96
97# Main loop
98for i in ${*-Setup}
99do
100	case $i in
101	-n)	echo '*noobjects*';;
102	*)	echo '*doconfig*'; cat "$i";;
103	esac
104done |
105sed -e 's/[ 	]*#.*//' -e '/^[ 	]*$/d' |
106(
107	rulesf="@rules.$$"
108	trap 'rm -f $rulesf' 0 1 2 3
109	echo "
110# Rules appended by makedepend
111" >$rulesf
112	DEFS=
113	MODS=
114	SHAREDMODS=
115	OBJS=
116	LIBS=
117	LOCALLIBS=
118	BASELIBS=
119	while read line
120	do
121		# 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
129		# Output DEFS in reverse order so first definition overrides
130		case $line in
131		*=*)	DEFS="$line$NL$DEFS"; continue;;
132		'include '*)	DEFS="$line$NL$DEFS"; continue;;
133		'*noobjects*')
134			case $noobjects in
135			yes)	;;
136			*)	LOCALLIBS=$LIBS; LIBS=;;
137			esac
138			noobjects=yes;
139			continue;;
140		'*doconfig*')	doconfig=yes; continue;;
141		'*static*')	doconfig=yes; continue;;
142		'*noconfig*')	doconfig=no; continue;;
143		'*shared*')	doconfig=no; continue;;
144		esac
145		srcs=
146		cpps=
147		libs=
148		mods=
149		skip=
150		for arg in $line
151		do
152			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
157			case $arg in
158			-framework)	libs="$libs $arg"; skip=libs;
159				        # OSX/OSXS/Darwin framework link cmd
160					;;
161			-[IDUCfF]*)	cpps="$cpps $arg";;
162			-Xcompiler)	skip=cpps;;
163			-Xlinker)	libs="$libs $arg"; skip=libs;;
164			-rpath)		libs="$libs $arg"; skip=libs;;
165			--rpath)	libs="$libs $arg"; skip=libs;;
166			-[A-Zl]*)	libs="$libs $arg";;
167			*.a)		libs="$libs $arg";;
168			*.so)		libs="$libs $arg";;
169			*.sl)		libs="$libs $arg";;
170			/*.o)		libs="$libs $arg";;
171			*.def)		libs="$libs $arg";;
172			*.o)		srcs="$srcs `basename $arg .o`.c";;
173			*.[cC])		srcs="$srcs $arg";;
174			*.m)		srcs="$srcs $arg";; # Objective-C src
175			*.cc)		srcs="$srcs $arg";;
176			*.c++)		srcs="$srcs $arg";;
177			*.cxx)		srcs="$srcs $arg";;
178			*.cpp)		srcs="$srcs $arg";;
179			\$*)		libs="$libs $arg"
180					cpps="$cpps $arg";;
181			*.*)		echo 1>&2 "bad word $arg in $line"
182					exit 1;;
183			-u)		skip=libs; libs="$libs -u";;
184			[a-zA-Z_]*)	mods="$mods $arg";;
185			*)		echo 1>&2 "bad word $arg in $line"
186					exit 1;;
187			esac
188		done
189		case $doconfig in
190		yes)
191			LIBS="$LIBS $libs"
192			MODS="$MODS $mods"
193			;;
194		esac
195		case $noobjects in
196		yes)	continue;;
197		esac
198		objs=''
199		for src in $srcs
200		do
201			case $src in
202			*.c)   obj=`basename $src .c`.o; cc='$(CC)';;
203			*.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)';;
208			*.m)   obj=`basename $src .m`.o; cc='$(CC)';; # Obj-C
209			*)     continue;;
210			esac
211			obj="$srcdir/$obj"
212			objs="$objs $obj"
213			case $src in
214			glmodule.c) ;;
215			/*) ;;
216			\$*) ;;
217			*) src='$(srcdir)/'"$srcdir/$src";;
218			esac
219			case $doconfig in
220			no)	cc="$cc \$(CCSHARED) \$(CFLAGS) \$(CPPFLAGS)";;
221			*)
222				cc="$cc \$(PY_CFLAGS)";;
223			esac
224			rule="$obj: $src; $cc $cpps -c $src -o $obj"
225			echo "$rule" >>$rulesf
226		done
227		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
236			file="$srcdir/$base\$(SO)"
237			case $doconfig in
238			no)	SHAREDMODS="$SHAREDMODS $file";;
239			esac
240			rule="$file: $objs"
241			rule="$rule; \$(BLDSHARED) $objs $libs $ExtraLibs -o $file"
242			echo "$rule" >>$rulesf
243		done
244	done
245
246	case $SHAREDMODS in
247	'')	;;
248	*)	DEFS="SHAREDMODS=$SHAREDMODS$NL$DEFS";;
249	esac
250
251	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
259	EXTDECLS=
260	INITBITS=
261	for mod in $MODS
262	do
263		EXTDECLS="${EXTDECLS}extern void init$mod(void);$NL"
264		INITBITS="${INITBITS}	{\"$mod\", init$mod},$NL"
265	done
266
267
268	case $config in
269	-)  ;;
270	*)  sed -e "
271		1i$NL/* Generated automatically from $config by makesetup. */
272		/MARKER 1/i$NL$EXTDECLS
273 
274		/MARKER 2/i$NL$INITBITS
275
276		" $config >config.c
277	    ;;
278	esac
279
280	case $makepre in
281	-)	;;
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
287		echo "s%_MODOBJS_%$OBJS%" >>$sedf
288		echo "s%_MODLIBS_%$LIBS%" >>$sedf
289		echo "/Definitions added by makesetup/a$NL$NL$DEFS" >>$sedf
290		sed -f $sedf $makepre >Makefile
291		cat $rulesf >>Makefile
292		rm -f $sedf
293	    ;;
294	esac
295
296	rm -f $rulesf
297)
298