makesetup 7.66 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 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 33 34
# - replace _MODBUILT_NAMES_ by the list of *static* and *shared* modules
#   from Setup
# - replace _MODDISABLED_NAMES_ by the list of *disabled* modules from Setup
35
# - replace _MODOBJS_ by the list of objects from Setup (except for
36
#   Setup files after a -n option)
37
# - replace _MODLIBS_ by the list of libraries from Setup
38 39 40 41
# - 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
42 43
# - for each variable definition found in Setup, insert the definition
#   before the comment 'Definitions added by makesetup'
44

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

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

# Newline for sed i and a commands
85 86
NL='\
'
87

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

100 101 102 103
# Main loop
for i in ${*-Setup}
do
	case $i in
104 105
	-n)	echo '*noobjects*';;
	*)	echo '*doconfig*'; cat "$i";;
106 107 108
	esac
done |
sed -e 's/[ 	]*#.*//' -e '/^[ 	]*$/d' |
109
(
110 111 112 113 114
	rulesf="@rules.$$"
	trap 'rm -f $rulesf' 0 1 2 3
	echo "
# Rules appended by makedepend
" >$rulesf
115
	DEFS=
116 117
	BUILT=
	DISABLED=
118
	MODS=
119
	SHAREDMODS=
120 121
	OBJS=
	LIBS=
122 123
	LOCALLIBS=
	BASELIBS=
124 125
	while read line
	do
126 127 128 129 130 131 132 133
		# 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

134
		# Output DEFS in reverse order so first definition overrides
135
		case $line in
136
		*=*)	DEFS="$line$NL$DEFS"; continue;;
137
		'include '*)	DEFS="$line$NL$DEFS"; continue;;
138
		'*noobjects*')
139 140 141 142 143
			case $noobjects in
			yes)	;;
			*)	LOCALLIBS=$LIBS; LIBS=;;
			esac
			noobjects=yes;
144 145
			continue;;
		'*doconfig*')	doconfig=yes; continue;;
146
		'*static*')	doconfig=yes; continue;;
147
		'*noconfig*')	doconfig=no; continue;;
148
		'*shared*')	doconfig=no; continue;;
149
		'*disabled*')	doconfig=disabled; continue;;
150
		esac
151
		srcs=
152
		cpps=
153 154
		libs=
		mods=
155
		skip=
156
		for arg in $line
157
		do
158 159 160 161 162
			case $skip in
			libs)	libs="$libs $arg"; skip=; continue;;
			cpps)	cpps="$cpps $arg"; skip=; continue;;
			srcs)	srcs="$srcs $arg"; skip=; continue;;
			esac
163
			case $arg in
164 165 166 167
			-framework)	libs="$libs $arg"; skip=libs;
				        # OSX/OSXS/Darwin framework link cmd
					;;
			-[IDUCfF]*)	cpps="$cpps $arg";;
168
			-Xcompiler)	skip=cpps;;
169
			-Xlinker)	libs="$libs $arg"; skip=libs;;
170
			-rpath)		libs="$libs $arg"; skip=libs;;
171
			--rpath)	libs="$libs $arg"; skip=libs;;
172 173
			-[A-Zl]*)	libs="$libs $arg";;
			*.a)		libs="$libs $arg";;
174 175
			*.so)		libs="$libs $arg";;
			*.sl)		libs="$libs $arg";;
176
			/*.o)		libs="$libs $arg";;
177
			*.def)		libs="$libs $arg";;
178
			*.o)		srcs="$srcs `basename $arg .o`.c";;
179
			*.[cC])		srcs="$srcs $arg";;
180
			*.m)		srcs="$srcs $arg";; # Objective-C src
181 182
			*.cc)		srcs="$srcs $arg";;
			*.c++)		srcs="$srcs $arg";;
183 184
			*.cxx)		srcs="$srcs $arg";;
			*.cpp)		srcs="$srcs $arg";;
185 186
			\$*)		libs="$libs $arg"
					cpps="$cpps $arg";;
187 188
			*.*)		echo 1>&2 "bad word $arg in $line"
					exit 1;;
189
			-u)		skip=libs; libs="$libs -u";;
190
			[a-zA-Z_]*)	mods="$mods $arg";;
191 192 193 194
			*)		echo 1>&2 "bad word $arg in $line"
					exit 1;;
			esac
		done
195 196 197 198
		case $doconfig in
		yes)
			LIBS="$LIBS $libs"
			MODS="$MODS $mods"
199 200 201 202 203 204 205 206
			BUILT="$BUILT $mods"
			;;
		no)
			BUILT="$BUILT $mods"
			;;
		disabled)
			DISABLED="$DISABLED $mods"
			continue
207 208
			;;
		esac
209 210 211
		case $noobjects in
		yes)	continue;;
		esac
212 213
		objs=''
		for src in $srcs
214
		do
215 216
			case $src in
			*.c)   obj=`basename $src .c`.o; cc='$(CC)';;
217 218 219 220 221
			*.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)';;
222
			*.m)   obj=`basename $src .m`.o; cc='$(CC)';; # Obj-C
223 224
			*)     continue;;
			esac
225
			obj="$srcdir/$obj"
226 227 228
			objs="$objs $obj"
			case $src in
			glmodule.c) ;;
229
			/*) ;;
230
			\$*) ;;
231
			*) src='$(srcdir)/'"$srcdir/$src";;
232 233
			esac
			case $doconfig in
234
			no)	cc="$cc \$(CCSHARED) \$(PY_CFLAGS) \$(PY_CPPFLAGS)";;
235
			*)
236
				cc="$cc \$(PY_STDMODULE_CFLAGS)";;
237
			esac
238
			rule="$obj: $src; $cc $cpps -c $src -o $obj"
239
			echo "$rule" >>$rulesf
240
		done
241 242 243 244
		case $doconfig in
		yes)	OBJS="$OBJS $objs";;
		esac
		for mod in $mods
245
		do
246
			file="$srcdir/$mod\$(EXT_SUFFIX)"
247 248 249 250
			case $doconfig in
			no)	SHAREDMODS="$SHAREDMODS $file";;
			esac
			rule="$file: $objs"
251
			rule="$rule; \$(BLDSHARED) $objs $libs $ExtraLibs -o $file"
252
			echo "$rule" >>$rulesf
253
		done
254 255
	done

256 257 258 259 260
	case $SHAREDMODS in
	'')	;;
	*)	DEFS="SHAREDMODS=$SHAREDMODS$NL$DEFS";;
	esac

261 262 263 264 265 266 267 268
	case $noobjects in
	yes)	BASELIBS=$LIBS;;
	*)	LOCALLIBS=$LIBS;;
	esac
	LIBS='$(LOCALMODLIBS) $(BASEMODLIBS)'
	DEFS="BASEMODLIBS=$BASELIBS$NL$DEFS"
	DEFS="LOCALMODLIBS=$LOCALLIBS$NL$DEFS"

269 270 271 272
	EXTDECLS=
	INITBITS=
	for mod in $MODS
	do
273
		EXTDECLS="${EXTDECLS}extern PyObject* PyInit_$mod(void);$NL"
274
		INITBITS="${INITBITS}    {\"$mod\", PyInit_$mod},$NL"
275 276
	done

277

278 279 280 281 282
	case $config in
	-)  ;;
	*)  sed -e "
		1i$NL/* Generated automatically from $config by makesetup. */
		/MARKER 1/i$NL$EXTDECLS
283

284 285
		/MARKER 2/i$NL$INITBITS

286 287 288
		" $config >config.c
	    ;;
	esac
289

290
	case $makepre in
291 292 293 294 295 296
	-)	;;
	*)	sedf="@sed.in.$$"
		trap 'rm -f $sedf' 0 1 2 3
		echo "1i\\" >$sedf
		str="# Generated automatically from $makepre by makesetup."
		echo "$str" >>$sedf
297 298
		echo "s%_MODBUILT_NAMES_%$BUILT%" >>$sedf
		echo "s%_MODDISABLED_NAMES_%$DISABLED%" >>$sedf
299 300
		echo "s%_MODOBJS_%$OBJS%" >>$sedf
		echo "s%_MODLIBS_%$LIBS%" >>$sedf
301 302 303 304
		echo "/Definitions added by makesetup/a$NL$NL$DEFS" >>$sedf
		sed -f $sedf $makepre >Makefile
		cat $rulesf >>Makefile
		rm -f $sedf
305 306
	    ;;
	esac
307

308
	rm -f $rulesf
309
)