1#! /bin/sh 2set -e 3 4# Convert templates into Makefile and config.c, based on the module 5# definitions found in the file Setup. 6# 7# Usage: makesetup [-s dir] [-c file] [-m file] [Setup] ... [-n [Setup] ...] 8# 9# Options: 10# -s directory: alternative source directory (default .) 11# -l directory: library source directory (default derived from $0) 12# -c file: alternative config.c template (default $libdir/config.c.in) 13# -c -: don't write config.c 14# -m file: alternative Makefile template (default ./Makefile.pre) 15# -m -: don't write Makefile 16# 17# Remaining arguments are one or more Setup files (default ./Setup). 18# Setup files after a -n option are used for their variables, modules 19# and libraries but not for their .o files. 20# 21# See Setup for a description of the format of the Setup file. 22# 23# The following edits are made: 24# 25# Copying config.c.in to config.c: 26# - insert an identifying comment at the start 27# - for each <module> mentioned in Setup before *noconfig*: 28# + insert 'extern PyObject* PyInit_<module>(void);' before MARKER 1 29# + insert '{"<module>", PyInit_<module>},' before MARKER 2 30# 31# Copying Makefile.pre to Makefile: 32# - insert an identifying comment at the start 33# - replace _MODBUILT_NAMES_ by the list of *static* and *shared* modules 34# from Setup 35# - replace _MODBSHARED_NAMES_ by the list of *shared* modules from Setup 36# - replace _MODDISABLED_NAMES_ by the list of *disabled* modules from Setup 37# - replace _MODOBJS_ by the list of objects from Setup (except for 38# Setup files after a -n option) 39# - replace _MODLIBS_ by the list of libraries from Setup 40# - for each object file mentioned in Setup, append a rule 41# '<file>.o: <file>.c; <build commands>' to the end of the Makefile 42# - for each module mentioned in Setup, append a rule 43# which creates a shared library version to the end of the Makefile 44# - for each variable definition found in Setup, insert the definition 45# before the comment 'Definitions added by makesetup' 46 47# Loop over command line options 48usage=' 49usage: makesetup [-s srcdir] [-l libdir] [-c config.c.in] [-m Makefile.pre] 50 [Setup] ... [-n [Setup] ...]' 51srcdir='.' 52libdir='' 53config='' 54makepre='' 55noobjects='' 56doconfig=yes 57while : 58do 59 case $1 in 60 -s) shift; srcdir=$1; shift;; 61 -l) shift; libdir=$1; shift;; 62 -c) shift; config=$1; shift;; 63 -m) shift; makepre=$1; shift;; 64 --) shift; break;; 65 -n) noobjects=yes;; 66 -*) echo "$usage" 1>&2; exit 2;; 67 *) break;; 68 esac 69done 70 71# Set default libdir and config if not set by command line 72# (Not all systems have dirname) 73case $libdir in 74'') case $0 in 75 */*) libdir=`echo $0 | sed 's,/[^/]*$,,'`;; 76 *) libdir=.;; 77 esac;; 78esac 79case $config in 80'') config=$libdir/config.c.in;; 81esac 82case $makepre in 83'') makepre=Makefile.pre;; 84esac 85 86# Newline for sed i and a commands 87NL='\ 88' 89 90# Main loop 91for i in ${*-Setup} 92do 93 case $i in 94 -n) echo '*noobjects*';; 95 *) echo '*doconfig*'; cat "$i";; 96 esac 97done | 98sed -e 's/[ ]*#.*//' -e '/^[ ]*$/d' | 99( 100 rulesf="@rules.$$" 101 trap 'rm -f $rulesf' 0 1 2 3 102 echo " 103# Rules appended by makesetup 104" >$rulesf 105 DEFS= 106 BUILT= 107 BUILT_SHARED= 108 DISABLED= 109 CONFIGURED= 110 MODS= 111 SHAREDMODS= 112 OBJS= 113 LIBS= 114 LOCALLIBS= 115 BASELIBS= 116 while read line 117 do 118 # to handle backslashes for sh's that don't automatically 119 # continue a read when the last char is a backslash 120 while echo $line | grep '\\$' > /dev/null 121 do 122 read extraline 123 line=`echo $line| sed s/.$//`$extraline 124 done 125 126 # Output DEFS in reverse order so first definition overrides 127 case $line in 128 *=*) DEFS="$line$NL$DEFS"; continue;; 129 'include '*) DEFS="$line$NL$DEFS"; continue;; 130 '*noobjects*') 131 case $noobjects in 132 yes) ;; 133 *) LOCALLIBS=$LIBS; LIBS=;; 134 esac 135 noobjects=yes; 136 continue;; 137 '*doconfig*') doconfig=yes; continue;; 138 '*static*') doconfig=yes; continue;; 139 '*noconfig*') doconfig=no; continue;; 140 '*shared*') doconfig=no; continue;; 141 '*disabled*') doconfig=disabled; continue;; 142 esac 143 srcs= 144 cpps= 145 libs= 146 mods= 147 mods_upper= 148 skip= 149 for arg in $line 150 do 151 case $skip in 152 libs) libs="$libs $arg"; skip=; continue;; 153 cpps) cpps="$cpps $arg"; skip=; continue;; 154 srcs) srcs="$srcs $arg"; skip=; continue;; 155 esac 156 case $arg in 157 -framework) libs="$libs $arg"; skip=libs; 158 # OSX/OSXS/Darwin framework link cmd 159 ;; 160 -[IDUCfF]*) cpps="$cpps $arg";; 161 -Xcompiler) skip=cpps;; 162 -Xlinker) libs="$libs $arg"; skip=libs;; 163 -rpath) libs="$libs $arg"; skip=libs;; 164 --rpath) libs="$libs $arg"; skip=libs;; 165 -[A-Zl]*) libs="$libs $arg";; 166 *.a) libs="$libs $arg";; 167 *.so) libs="$libs $arg";; 168 *.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 *.cc) srcs="$srcs $arg";; 175 *.c++) srcs="$srcs $arg";; 176 *.cxx) srcs="$srcs $arg";; 177 *.cpp) srcs="$srcs $arg";; 178 \$\(*_CFLAGS\)) cpps="$cpps $arg";; 179 \$\(*_INCLUDES\)) cpps="$cpps $arg";; 180 \$\(*_LIBS\)) libs="$libs $arg";; 181 \$\(*_LDFLAGS\)) libs="$libs $arg";; 182 \$\(*_RPATH\)) libs="$libs $arg";; 183 \$*) libs="$libs $arg" 184 cpps="$cpps $arg";; 185 *.*) echo 1>&2 "bad word $arg in $line" 186 exit 1;; 187 -u) skip=libs; libs="$libs -u";; 188 [a-zA-Z_]*) 189 mods="$mods $arg" 190 mods_upper=$(echo $mods | tr '[a-z]' '[A-Z]');; 191 *) echo 1>&2 "bad word $arg in $line" 192 exit 1;; 193 esac 194 done 195 if test -z "$cpps" -a -z "$libs"; then 196 cpps="\$(MODULE_${mods_upper}_CFLAGS)" 197 libs="\$(MODULE_${mods_upper}_LDFLAGS)" 198 fi 199 for mod in $mods 200 do 201 case $CONFIGURED in 202 *,${mod},*) 203 # Detected multiple rules for a module, first rule wins. This 204 # allows users to disable modules in Setup.local. 205 echo 1>&2 "maksetup: '$mod' was handled by previous rule." 206 continue 2;; 207 esac 208 CONFIGURED="$CONFIGURED,${mod}," 209 done 210 case $doconfig in 211 yes) 212 LIBS="$LIBS $libs" 213 MODS="$MODS $mods" 214 BUILT="$BUILT $mods" 215 ;; 216 no) 217 BUILT="$BUILT $mods" 218 ;; 219 disabled) 220 DISABLED="$DISABLED $mods" 221 continue 222 ;; 223 esac 224 case $noobjects in 225 yes) continue;; 226 esac 227 objs='' 228 for src in $srcs 229 do 230 case $src in 231 *.c) obj=`basename $src .c`.o; cc='$(CC)';; 232 *.cc) obj=`basename $src .cc`.o; cc='$(CXX)';; 233 *.c++) obj=`basename $src .c++`.o; cc='$(CXX)';; 234 *.C) obj=`basename $src .C`.o; cc='$(CXX)';; 235 *.cxx) obj=`basename $src .cxx`.o; cc='$(CXX)';; 236 *.cpp) obj=`basename $src .cpp`.o; cc='$(CXX)';; 237 *.m) obj=`basename $src .m`.o; cc='$(CC)';; # Obj-C 238 *) continue;; 239 esac 240 case $src in 241 */*) obj="$srcdir/`dirname $src`/$obj";; 242 *) obj="$srcdir/$obj";; 243 esac 244 objs="$objs $obj" 245 case $src in 246 glmodule.c) ;; 247 /*) ;; 248 \$*) ;; 249 *) src='$(srcdir)/'"$srcdir/$src";; 250 esac 251 # custom flags first, PY_STDMODULE_CFLAGS may contain -I with system libmpdec 252 case $doconfig in 253 no) 254 cc="$cc $cpps \$(PY_STDMODULE_CFLAGS) \$(CCSHARED)" 255 rule="$obj: $src \$(MODULE_${mods_upper}_DEPS) \$(MODULE_DEPS_SHARED) \$(PYTHON_HEADERS); $cc -c $src -o $obj" 256 ;; 257 *) 258 cc="$cc $cpps \$(PY_BUILTIN_MODULE_CFLAGS)" 259 rule="$obj: $src \$(MODULE_${mods_upper}_DEPS) \$(MODULE_DEPS_STATIC) \$(PYTHON_HEADERS); $cc -c $src -o $obj" 260 ;; 261 esac 262 echo "$rule" >>$rulesf 263 done 264 case $doconfig in 265 yes) OBJS="$OBJS $objs";; 266 esac 267 for mod in $mods 268 do 269 file="$srcdir/$mod\$(EXT_SUFFIX)" 270 case $doconfig in 271 no) 272 SHAREDMODS="$SHAREDMODS $file" 273 BUILT_SHARED="$BUILT_SHARED $mod" 274 ;; 275 esac 276 rule="$file: $objs" 277 rule="$rule; \$(BLDSHARED) $objs $libs \$(LIBPYTHON) -o $file" 278 echo "$rule" >>$rulesf 279 done 280 done 281 282 case $SHAREDMODS in 283 '') ;; 284 *) DEFS="SHAREDMODS=$SHAREDMODS$NL$DEFS";; 285 esac 286 287 case $noobjects in 288 yes) BASELIBS=$LIBS;; 289 *) LOCALLIBS=$LIBS;; 290 esac 291 LIBS='$(LOCALMODLIBS) $(BASEMODLIBS)' 292 DEFS="BASEMODLIBS=$BASELIBS$NL$DEFS" 293 DEFS="LOCALMODLIBS=$LOCALLIBS$NL$DEFS" 294 295 EXTDECLS= 296 INITBITS= 297 for mod in $MODS 298 do 299 EXTDECLS="${EXTDECLS}extern PyObject* PyInit_$mod(void);$NL" 300 INITBITS="${INITBITS} {\"$mod\", PyInit_$mod},$NL" 301 done 302 303 304 case $config in 305 -) ;; 306 *) sed -e " 307 1i$NL/* Generated automatically from $config by makesetup. */ 308 /MARKER 1/i$NL$EXTDECLS 309 310 /MARKER 2/i$NL$INITBITS 311 312 " $config >config.c 313 ;; 314 esac 315 316 case $makepre in 317 -) ;; 318 *) 319 # macOS' sed has issues with 'a' command. Use 'r' command with an 320 # external replacement file instead. 321 sedf="@sed.in.$$" 322 sedr="@sed.replace.$$" 323 trap 'rm -f $sedf $sedr' 0 1 2 3 324 echo "$NL$NL$DEFS" | sed 's/\\$//' > $sedr 325 echo "1i\\" >$sedf 326 str="# Generated automatically from $makepre by makesetup." 327 echo "$str" >>$sedf 328 echo "s%_MODBUILT_NAMES_%$BUILT%" >>$sedf 329 echo "s%_MODSHARED_NAMES_%$BUILT_SHARED%" >>$sedf 330 echo "s%_MODDISABLED_NAMES_%$DISABLED%" >>$sedf 331 echo "s%_MODOBJS_%$OBJS%" >>$sedf 332 echo "s%_MODLIBS_%$LIBS%" >>$sedf 333 echo "/Definitions added by makesetup/r $sedr" >>$sedf 334 sed -f $sedf $makepre >Makefile 335 cat $rulesf >>Makefile 336 rm -f $sedf $sedr 337 ;; 338 esac 339 340 rm -f $rulesf 341) 342