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