blob: 69159f7989ced37b8c9d854ac202b287925ebcbd [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 Rossumfba715a1994-01-02 00:26:09 +000095 while read line
96 do
Guido van Rossumb6775db1994-08-01 11:34:53 +000097 # Output DEFS in reverse order so first definition overrides
Guido van Rossumfba715a1994-01-02 00:26:09 +000098 case $line in
Guido van Rossumb6775db1994-08-01 11:34:53 +000099 *=*) DEFS="$line$NL$DEFS"; continue;;
100 '<noobjects>') noobjects=yes; continue;;
Guido van Rossumfba715a1994-01-02 00:26:09 +0000101 esac
102 objs=
103 cpps=
104 set $line
105 for arg
106 do
107 case $arg in
108 -[IDUC]*) cpps="$cpps $arg";;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000109 -[A-Zl]*) LIBS="$LIBS $arg";;
Guido van Rossumfba715a1994-01-02 00:26:09 +0000110 *.a) LIBS="$LIBS $arg";;
111 *.o) objs="$objs $arg";;
112 *.*) echo 1>&2 "bad word $arg in $line"
113 exit 1;;
114 [a-zA-Z_]*) MODS="$MODS $arg";;
115 *) echo 1>&2 "bad word $arg in $line"
116 exit 1;;
117 esac
118 done
Guido van Rossumb6775db1994-08-01 11:34:53 +0000119 case $noobjects in
120 yes) continue;;
121 esac
Guido van Rossumfba715a1994-01-02 00:26:09 +0000122 for obj in $objs
123 do
124 src=`basename $obj .o`.c
Guido van Rossumb6775db1994-08-01 11:34:53 +0000125 case $src in
126 glmodule.c) ;;
127 *) src='$(srcdir)/'$src;;
128 esac
Guido van Rossumfba715a1994-01-02 00:26:09 +0000129 RULES="$RULES$obj: $src; \$(CC) \$(CFLAGS) $cpps -c $src$NL"
130 done
131 OBJS="$OBJS $objs"
132 done
133
134 EXTDECLS=
135 INITBITS=
136 for mod in $MODS
137 do
138 EXTDECLS="${EXTDECLS}extern void init$mod();$NL"
139 INITBITS="${INITBITS} {\"$mod\", init$mod},$NL"
140 done
Guido van Rossumfba715a1994-01-02 00:26:09 +0000141
Guido van Rossumb6775db1994-08-01 11:34:53 +0000142 case $config in
143 -) ;;
144 *) sed -e "
145 1i$NL/* Generated automatically from $config by makesetup. */
146 /MARKER 1/i$NL$EXTDECLS
Guido van Rossumfba715a1994-01-02 00:26:09 +0000147 /MARKER 2/i$NL$INITBITS
148
Guido van Rossumb6775db1994-08-01 11:34:53 +0000149 " $config >config.c
150 ;;
151 esac
Guido van Rossumfba715a1994-01-02 00:26:09 +0000152
Guido van Rossumb6775db1994-08-01 11:34:53 +0000153 case $makepre in
154 -) ;;
155 *) sed -e "
156 1i$NL# Generated automatically from $makepre by makesetup.
Guido van Rossumfba715a1994-01-02 00:26:09 +0000157 s%@MODOBJS@%$OBJS%
158 s%@MODLIBS@%$LIBS%
Guido van Rossumb6775db1994-08-01 11:34:53 +0000159 /Rules added by makesetup/a$NL$NL$RULES
160 /Definitions added by makesetup/a$NL$NL$DEFS
161
162 " $makepre >Makefile
163 ;;
164 esac
Guido van Rossumfba715a1994-01-02 00:26:09 +0000165
166)