makesetup 7.23 KB
Newer Older
1 2
#! /bin/sh

3 4 5 6 7 8
# Convert templates into Makefile and config.c, based on the module
# definitions found in the file Setup.
#
# Usage: makesetup [-s dir] [-c file] [-m file] [Setup] ... [-n [Setup] ...]
#
# Options:
9 10 11
# -s directory: alternative source directory (default .)
# -l directory: library source directory (default derived from $0)
# -c file:      alternative config.c template (default $libdir/config.c.in)
12 13 14 15 16 17 18 19
# -c -:         don't write config.c
# -m file:      alternative Makefile template (default ./Makefile.pre)
# -m -:         don't write Makefile
#
# Remaining arguments are one or more Setup files (default ./Setup).
# Setup files after a -n option are used for their variables, modules
# and libraries but not for their .o files.
#
20
# See Setup.dist for a description of the format of the Setup file.
21 22 23 24 25
#
# The following edits are made:
#
# Copying config.c.in to config.c:
# - 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 29 30 31
#   + insert '{"<module>", initmodule},' before MARKER 2
#
# Copying Makefile.pre to Makefile:
# - 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 36 37 38
# - for each object file mentioned in Setup, append a rule
#   '<file>.o: <file>.c; <build commands>' to the end of the Makefile
# - for each module mentioned in Setup, append a rule
#   which creates a shared library version to the end of the Makefile
39 40
# - for each variable definition found in Setup, insert the definition
#   before the comment 'Definitions added by makesetup'
41

42 43
# Loop over command line options
usage='
44
usage: makesetup [-s srcdir] [-l libdir] [-c config.c.in] [-m Makefile.pre]
45
                 [Setup] ... [-n [Setup] ...]'
46 47
srcdir='.'
libdir=''
48 49 50
config=''
makepre=''
noobjects=''
51
doconfig=yes
52 53 54 55
while :
do
	case $1 in
	-s)	shift; srcdir=$1; shift;;
56
	-l)	shift; libdir=$1; shift;;
57 58 59 60 61 62 63 64 65
	-c)	shift; config=$1; shift;;
	-m)	shift; makepre=$1; shift;;
	--)	shift; break;;
	-n)	noobjects=yes;;
	-*)	echo "$usage" 1>&2; exit 2;;
	*)	break;;
	esac
done

66
# Set default libdir and config if not set by command line
67
# (Not all systems have dirname)
Neil Schemenauer's avatar
Neil Schemenauer committed
68
case $libdir in
69
'')	case $0 in
70 71
	*/*)	libdir=`echo $0 | sed 's,/[^/]*$,,'`;;
	*)	libdir=.;;
72 73 74
	esac;;
esac
case $config in
75
'')	config=$libdir/config.c.in;;
76 77 78 79 80 81
esac
case $makepre in
'')	makepre=Makefile.pre;;
esac

# Newline for sed i and a commands
82 83
NL='\
'
84

85 86 87
# Setup to link with extra libraries when makeing shared extensions.
# Currently, only Cygwin needs this baggage.
case `uname -s` in
88
CYGWIN*) if test $libdir = .
89
	 then
90
	 	ExtraLibDir=.
91 92 93 94 95 96
	 else
	 	ExtraLibDir='$(LIBPL)'
	 fi
	 ExtraLibs="-L$ExtraLibDir -lpython\$(VERSION)";;
esac

97 98 99 100
# Main loop
for i in ${*-Setup}
do
	case $i in
101 102
	-n)	echo '*noobjects*';;
	*)	echo '*doconfig*'; cat "$i";;
103 104 105
	esac
done |
sed -e 's/[ 	]*#.*//' -e '/^[ 	]*$/d' |
106
(
107 108 109 110 111
	rulesf="@rules.$$"
	trap 'rm -f $rulesf' 0 1 2 3
	echo "
# Rules appended by makedepend
" >$rulesf
112 113
	DEFS=
	MODS=
114
	SHAREDMODS=
115 116
	OBJS=
	LIBS=
117 118
	LOCALLIBS=
	BASELIBS=
119 120
	while read line
	do
121 122 123 124 125 126 127 128
		# to handle backslashes for sh's that don't automatically
		# continue a read when the last char is a backslash
		while echo $line | grep '\\$' > /dev/null
		do
			read extraline
			line=`echo $line| sed s/.$//`$extraline
		done

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 135 136 137 138
			case $noobjects in
			yes)	;;
			*)	LOCALLIBS=$LIBS; LIBS=;;
			esac
			noobjects=yes;
139 140
			continue;;
		'*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 148
		libs=
		mods=
149
		skip=
150
		for arg in $line
151
		do
152 153 154 155 156
			case $skip in
			libs)	libs="$libs $arg"; skip=; continue;;
			cpps)	cpps="$cpps $arg"; skip=; continue;;
			srcs)	srcs="$srcs $arg"; skip=; continue;;
			esac
157
			case $arg in
158 159 160 161
			-framework)	libs="$libs $arg"; skip=libs;
				        # OSX/OSXS/Darwin framework link cmd
					;;
			-[IDUCfF]*)	cpps="$cpps $arg";;
162
			-Xlinker)	libs="$libs $arg"; skip=libs;;
163
			-rpath)		libs="$libs $arg"; skip=libs;;
164
			--rpath)	libs="$libs $arg"; skip=libs;;
165 166
			-[A-Zl]*)	libs="$libs $arg";;
			*.a)		libs="$libs $arg";;
167 168
			*.so)		libs="$libs $arg";;
			*.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 175
			*.cc)		srcs="$srcs $arg";;
			*.c++)		srcs="$srcs $arg";;
176 177
			*.cxx)		srcs="$srcs $arg";;
			*.cpp)		srcs="$srcs $arg";;
178 179
			\$*)		libs="$libs $arg"
					cpps="$cpps $arg";;
180 181
			*.*)		echo 1>&2 "bad word $arg in $line"
					exit 1;;
182
			-u)		skip=libs; libs="$libs -u";;
183
			[a-zA-Z_]*)	mods="$mods $arg";;
184 185 186 187
			*)		echo 1>&2 "bad word $arg in $line"
					exit 1;;
			esac
		done
188 189 190 191 192 193
		case $doconfig in
		yes)
			LIBS="$LIBS $libs"
			MODS="$MODS $mods"
			;;
		esac
194 195 196
		case $noobjects in
		yes)	continue;;
		esac
197 198
		objs=''
		for src in $srcs
199
		do
200 201
			case $src in
			*.c)   obj=`basename $src .c`.o; cc='$(CC)';;
202 203 204 205 206
			*.cc)  obj=`basename $src .cc`.o; cc='$(CXX)';;
			*.c++) obj=`basename $src .c++`.o; cc='$(CXX)';;
			*.C)   obj=`basename $src .C`.o; cc='$(CXX)';;
			*.cxx) obj=`basename $src .cxx`.o; cc='$(CXX)';;
			*.cpp) obj=`basename $src .cpp`.o; cc='$(CXX)';;
207
			*.m)   obj=`basename $src .m`.o; cc='$(CC)';; # Obj-C
208 209
			*)     continue;;
			esac
210
			obj="$srcdir/$obj"
211 212 213
			objs="$objs $obj"
			case $src in
			glmodule.c) ;;
214
			/*) ;;
215
			\$*) ;;
216
			*) src='$(srcdir)/'"$srcdir/$src";;
217 218
			esac
			case $doconfig in
219
			no)	cc="$cc \$(CCSHARED) \$(CFLAGS) \$(CPPFLAGS)";;
220 221
			*)
				cc="$cc \$(PY_CFLAGS)";;
222
			esac
223
			rule="$obj: $src; $cc $cpps -c $src -o $obj"
224
			echo "$rule" >>$rulesf
225
		done
226 227 228 229
		case $doconfig in
		yes)	OBJS="$OBJS $objs";;
		esac
		for mod in $mods
230
		do
231 232 233 234
			case $objs in
			*$mod.o*)	base=$mod;;
			*)		base=${mod}module;;
			esac
235
			file="$srcdir/$base\$(SO)"
236 237 238 239
			case $doconfig in
			no)	SHAREDMODS="$SHAREDMODS $file";;
			esac
			rule="$file: $objs"
240
			rule="$rule; \$(LDSHARED) $objs $libs $ExtraLibs -o $file"
241
			echo "$rule" >>$rulesf
242
		done
243 244
	done

245 246 247 248 249
	case $SHAREDMODS in
	'')	;;
	*)	DEFS="SHAREDMODS=$SHAREDMODS$NL$DEFS";;
	esac

250 251 252 253 254 255 256 257
	case $noobjects in
	yes)	BASELIBS=$LIBS;;
	*)	LOCALLIBS=$LIBS;;
	esac
	LIBS='$(LOCALMODLIBS) $(BASEMODLIBS)'
	DEFS="BASEMODLIBS=$BASELIBS$NL$DEFS"
	DEFS="LOCALMODLIBS=$LOCALLIBS$NL$DEFS"

258 259 260 261
	EXTDECLS=
	INITBITS=
	for mod in $MODS
	do
262
		EXTDECLS="${EXTDECLS}extern void init$mod(void);$NL"
263 264 265
		INITBITS="${INITBITS}	{\"$mod\", init$mod},$NL"
	done

266

267 268 269 270 271
	case $config in
	-)  ;;
	*)  sed -e "
		1i$NL/* Generated automatically from $config by makesetup. */
		/MARKER 1/i$NL$EXTDECLS
272
 
273 274
		/MARKER 2/i$NL$INITBITS

275 276 277
		" $config >config.c
	    ;;
	esac
278

279
	case $makepre in
280 281 282 283 284 285
	-)	;;
	*)	sedf="@sed.in.$$"
		trap 'rm -f $sedf' 0 1 2 3
		echo "1i\\" >$sedf
		str="# Generated automatically from $makepre by makesetup."
		echo "$str" >>$sedf
286 287
		echo "s%_MODOBJS_%$OBJS%" >>$sedf
		echo "s%_MODLIBS_%$LIBS%" >>$sedf
288 289 290 291
		echo "/Definitions added by makesetup/a$NL$NL$DEFS" >>$sedf
		sed -f $sedf $makepre >Makefile
		cat $rulesf >>Makefile
		rm -f $sedf
292 293
	    ;;
	esac
294

295
	rm -f $rulesf
296
)