makesetup 7.22 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 28
#   + insert 'extern PyObject* PyInit_<module>(void);' before MARKER 1
#   + insert '{"<module>", PyInit_<module>},' before MARKER 2
29 30 31
#
# 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
			-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 167
			-[A-Zl]*)	libs="$libs $arg";;
			*.a)		libs="$libs $arg";;
168 169
			*.so)		libs="$libs $arg";;
			*.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 176
			*.cc)		srcs="$srcs $arg";;
			*.c++)		srcs="$srcs $arg";;
177 178
			*.cxx)		srcs="$srcs $arg";;
			*.cpp)		srcs="$srcs $arg";;
179 180
			\$*)		libs="$libs $arg"
					cpps="$cpps $arg";;
181 182
			*.*)		echo 1>&2 "bad word $arg in $line"
					exit 1;;
183
			-u)		skip=libs; libs="$libs -u";;
184
			[a-zA-Z_]*)	mods="$mods $arg";;
185 186 187 188
			*)		echo 1>&2 "bad word $arg in $line"
					exit 1;;
			esac
		done
189 190 191 192 193 194
		case $doconfig in
		yes)
			LIBS="$LIBS $libs"
			MODS="$MODS $mods"
			;;
		esac
195 196 197
		case $noobjects in
		yes)	continue;;
		esac
198 199
		objs=''
		for src in $srcs
200
		do
201 202
			case $src in
			*.c)   obj=`basename $src .c`.o; cc='$(CC)';;
203 204 205 206 207
			*.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)';;
208
			*.m)   obj=`basename $src .m`.o; cc='$(CC)';; # Obj-C
209 210
			*)     continue;;
			esac
211
			obj="$srcdir/$obj"
212 213 214
			objs="$objs $obj"
			case $src in
			glmodule.c) ;;
215
			/*) ;;
216
			\$*) ;;
217
			*) src='$(srcdir)/'"$srcdir/$src";;
218 219
			esac
			case $doconfig in
220
			no)	cc="$cc \$(CCSHARED) \$(PY_CFLAGS) \$(PY_CPPFLAGS)";;
221
			*)
222
				cc="$cc \$(PY_CORE_CFLAGS)";;
223
			esac
224
			rule="$obj: $src; $cc $cpps -c $src -o $obj"
225
			echo "$rule" >>$rulesf
226
		done
227 228 229 230
		case $doconfig in
		yes)	OBJS="$OBJS $objs";;
		esac
		for mod in $mods
231
		do
232
			file="$srcdir/$mod\$(EXT_SUFFIX)"
233 234 235 236
			case $doconfig in
			no)	SHAREDMODS="$SHAREDMODS $file";;
			esac
			rule="$file: $objs"
237
			rule="$rule; \$(BLDSHARED) $objs $libs $ExtraLibs -o $file"
238
			echo "$rule" >>$rulesf
239
		done
240 241
	done

242 243 244 245 246
	case $SHAREDMODS in
	'')	;;
	*)	DEFS="SHAREDMODS=$SHAREDMODS$NL$DEFS";;
	esac

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

255 256 257 258
	EXTDECLS=
	INITBITS=
	for mod in $MODS
	do
259 260
		EXTDECLS="${EXTDECLS}extern PyObject* PyInit_$mod(void);$NL"
		INITBITS="${INITBITS}	{\"$mod\", PyInit_$mod},$NL"
261 262
	done

263

264 265 266 267 268
	case $config in
	-)  ;;
	*)  sed -e "
		1i$NL/* Generated automatically from $config by makesetup. */
		/MARKER 1/i$NL$EXTDECLS
269

270 271
		/MARKER 2/i$NL$INITBITS

272 273 274
		" $config >config.c
	    ;;
	esac
275

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

292
	rm -f $rulesf
293
)