blob: e78ff4f6f0e9c141a20ef5655fa5e507bb245ef2 [file] [log] [blame]
Guido van Rossumfba715a1994-01-02 00:26:09 +00001#! /bin/sh
2
Guido van Rossumb6775db1994-08-01 11:34:53 +00003# 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 derived from $0)
10# -c file: alternative config.c template (default $srcdir/config.c.in)
11# -c -: don't write config.c
12# -m file: alternative Makefile template (default ./Makefile.pre)
13# -m -: don't write Makefile
14#
15# Remaining arguments are one or more Setup files (default ./Setup).
16# Setup files after a -n option are used for their variables, modules
17# and libraries but not for their .o files.
18#
19# See Setup.in for a description of the format of the Setup file.
20#
21# The following edits are made:
22#
23# Copying config.c.in to config.c:
24# - insert an identifying comment at the start
25# - for each <module> mentioned in Setup:
26# + insert 'extern void init<module>();' before MARKER 1
27# + insert '{"<module>", initmodule},' before MARKER 2
28#
29# Copying Makefile.pre to Makefile:
30# - insert an identifying comment at the start
31# - replace @MODOBJS@ by the list of objects from Setup (except for
32# Setup files after a -n option)
33# - replace @MODLIBS@ by the list of libraries from Setup
34# - for each object file mentioned in Setup, insert a rule
35# '<file>.o: <file>.c; <build commands>' before the comment
36# 'Rules added by makesetup'
37# - for each variable definition found in Setup, insert the definition
38# before the comment 'Definitions added by makesetup'
Guido van Rossumfba715a1994-01-02 00:26:09 +000039
Guido van Rossumb6775db1994-08-01 11:34:53 +000040# Loop over command line options
41usage='
42usage: makesetup [-s srcdir] [-c config.c.in] [-m Makefile.pre]
43 [Setup] ... [-n [Setup] ...]'
44srcdir=''
45config=''
46makepre=''
47noobjects=''
48while :
49do
50 case $1 in
51 -s) shift; srcdir=$1; shift;;
52 -c) shift; config=$1; shift;;
53 -m) shift; makepre=$1; shift;;
54 --) shift; break;;
55 -n) noobjects=yes;;
56 -*) echo "$usage" 1>&2; exit 2;;
57 *) break;;
58 esac
59done
60
61# Set default srcdir and config if not set by command line
62# (Not all systems have dirname)
63case $srcdir in
64'') case $0 in
65 */*) srcdir=`echo $0 | sed 's,/[^/]*$,,'`;;
66 *) srcdir=.;;
67 esac;;
68esac
69case $config in
70'') config=$srcdir/config.c.in;;
71esac
72case $makepre in
73'') makepre=Makefile.pre;;
74esac
75
76# Newline for sed i and a commands
Guido van Rossumfba715a1994-01-02 00:26:09 +000077NL="\\
78"
79
Guido van Rossumb6775db1994-08-01 11:34:53 +000080# Main loop
81for i in ${*-Setup}
82do
83 case $i in
84 -n) echo '<noobjects>';;
85 *) cat "$i";;
86 esac
87done |
88sed -e 's/[ ]*#.*//' -e '/^[ ]*$/d' |
Guido van Rossumfba715a1994-01-02 00:26:09 +000089(
90 DEFS=
91 MODS=
92 OBJS=
93 LIBS=
94 RULES=
Guido van Rossum0b498be1994-08-23 13:49:15 +000095 LOCALLIBS=
96 BASELIBS=
Guido van Rossumfba715a1994-01-02 00:26:09 +000097 while read line
98 do
Guido van Rossumb6775db1994-08-01 11:34:53 +000099 # Output DEFS in reverse order so first definition overrides
Guido van Rossumfba715a1994-01-02 00:26:09 +0000100 case $line in
Guido van Rossumb6775db1994-08-01 11:34:53 +0000101 *=*) DEFS="$line$NL$DEFS"; continue;;
Guido van Rossum0b498be1994-08-23 13:49:15 +0000102 '<noobjects>')
103 case $noobjects in
104 yes) ;;
105 *) LOCALLIBS=$LIBS; LIBS=;;
106 esac
107 noobjects=yes;
108 continue;;
Guido van Rossumfba715a1994-01-02 00:26:09 +0000109 esac
110 objs=
Guido van Rossum0b498be1994-08-23 13:49:15 +0000111 srcs=
Guido van Rossumfba715a1994-01-02 00:26:09 +0000112 cpps=
113 set $line
114 for arg
115 do
116 case $arg in
117 -[IDUC]*) cpps="$cpps $arg";;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000118 -[A-Zl]*) LIBS="$LIBS $arg";;
Guido van Rossumfba715a1994-01-02 00:26:09 +0000119 *.a) LIBS="$LIBS $arg";;
120 *.o) objs="$objs $arg";;
Guido van Rossum0b498be1994-08-23 13:49:15 +0000121 *.[cC]) srcs="$srcs $arg";;
122 *.cc) srcs="$srcs $arg";;
123 *.c++) srcs="$srcs $arg";;
Guido van Rossumfba715a1994-01-02 00:26:09 +0000124 *.*) echo 1>&2 "bad word $arg in $line"
125 exit 1;;
126 [a-zA-Z_]*) MODS="$MODS $arg";;
127 *) echo 1>&2 "bad word $arg in $line"
128 exit 1;;
129 esac
130 done
Guido van Rossumb6775db1994-08-01 11:34:53 +0000131 case $noobjects in
132 yes) continue;;
133 esac
Guido van Rossumfba715a1994-01-02 00:26:09 +0000134 for obj in $objs
135 do
136 src=`basename $obj .o`.c
Guido van Rossumb6775db1994-08-01 11:34:53 +0000137 case $src in
138 glmodule.c) ;;
139 *) src='$(srcdir)/'$src;;
140 esac
Guido van Rossumfba715a1994-01-02 00:26:09 +0000141 RULES="$RULES$obj: $src; \$(CC) \$(CFLAGS) $cpps -c $src$NL"
142 done
143 OBJS="$OBJS $objs"
Guido van Rossum0b498be1994-08-23 13:49:15 +0000144 for src in $srcs
145 do
146 case $src in
147 *.c) obj=`basename $src .c`.o; cc='$(CC)';;
148 *.cc) obj=`basename $src .cc`.o; cc='$(CCC)';;
149 *.c++) obj=`basename $src .c++`.o; cc='$(CCC)';;
150 *.C) obj=`basename $src .C`.o; cc='$(CCC)';;
151 *) continue;;
152 esac
153 case $src in
154 glmodule.c) ;;
155 *) src='$(srcdir)/'$src;;
156 esac
157 RULES="$RULES$obj: $src; $cc \$(CFLAGS) $cpps -c $src$NL"
158 done
Guido van Rossumfba715a1994-01-02 00:26:09 +0000159 done
160
Guido van Rossum0b498be1994-08-23 13:49:15 +0000161 case $noobjects in
162 yes) BASELIBS=$LIBS;;
163 *) LOCALLIBS=$LIBS;;
164 esac
165 LIBS='$(LOCALMODLIBS) $(BASEMODLIBS)'
166 DEFS="BASEMODLIBS=$BASELIBS$NL$DEFS"
167 DEFS="LOCALMODLIBS=$LOCALLIBS$NL$DEFS"
168
Guido van Rossumfba715a1994-01-02 00:26:09 +0000169 EXTDECLS=
170 INITBITS=
171 for mod in $MODS
172 do
173 EXTDECLS="${EXTDECLS}extern void init$mod();$NL"
174 INITBITS="${INITBITS} {\"$mod\", init$mod},$NL"
175 done
Guido van Rossumfba715a1994-01-02 00:26:09 +0000176
Guido van Rossumb6775db1994-08-01 11:34:53 +0000177 case $config in
178 -) ;;
179 *) sed -e "
180 1i$NL/* Generated automatically from $config by makesetup. */
181 /MARKER 1/i$NL$EXTDECLS
Guido van Rossumfba715a1994-01-02 00:26:09 +0000182 /MARKER 2/i$NL$INITBITS
183
Guido van Rossumb6775db1994-08-01 11:34:53 +0000184 " $config >config.c
185 ;;
186 esac
Guido van Rossumfba715a1994-01-02 00:26:09 +0000187
Guido van Rossumb6775db1994-08-01 11:34:53 +0000188 case $makepre in
189 -) ;;
190 *) sed -e "
191 1i$NL# Generated automatically from $makepre by makesetup.
Guido van Rossumfba715a1994-01-02 00:26:09 +0000192 s%@MODOBJS@%$OBJS%
193 s%@MODLIBS@%$LIBS%
Guido van Rossumb6775db1994-08-01 11:34:53 +0000194 /Rules added by makesetup/a$NL$NL$RULES
195 /Definitions added by makesetup/a$NL$NL$DEFS
196
197 " $makepre >Makefile
198 ;;
199 esac
Guido van Rossumfba715a1994-01-02 00:26:09 +0000200
201)