1 | # vim:set ft=tcl: |
---|
2 | |
---|
3 | global builddir bindir |
---|
4 | |
---|
5 | set builddir "build" |
---|
6 | set bindir "bin" |
---|
7 | |
---|
8 | define CONFIG_ARGS $::argv |
---|
9 | |
---|
10 | use cc cc-lib cc-db cc-shared |
---|
11 | |
---|
12 | set preconf "src/preconf.tcl" |
---|
13 | set postconf "src/postconf.tcl" |
---|
14 | set postsetup "src/postsetup.tcl" |
---|
15 | set makeextra "src/Makefile.extra" |
---|
16 | |
---|
17 | if {[file exists $preconf]} { |
---|
18 | source $preconf |
---|
19 | } |
---|
20 | |
---|
21 | options { |
---|
22 | with-boehm-gc:=yes => "Use the Boehm GC; the argument can be yes, no, or a path to the GC's installation directory." |
---|
23 | without-re2c => "Do not use re2c." |
---|
24 | adlibdev => "Change Makefile defaults for AdLib core development." |
---|
25 | devel => "Enable debug mode by default (implied by --adlibdev)." |
---|
26 | } |
---|
27 | |
---|
28 | set includes "-I." |
---|
29 | set libs {} |
---|
30 | |
---|
31 | set boehmarg "no" |
---|
32 | opt-str with-boehm-gc boehmarg |
---|
33 | |
---|
34 | if {[opt-bool adlibdev]} { |
---|
35 | define DEFAULT_TARGET "\$(TARGETS)" |
---|
36 | define DEBUG 1 |
---|
37 | } else { |
---|
38 | define DEFAULT_TARGET "prog" |
---|
39 | define DEBUG 0 |
---|
40 | } |
---|
41 | |
---|
42 | if {[opt-bool devel]} { |
---|
43 | define DEBUG 1 |
---|
44 | } |
---|
45 | |
---|
46 | switch -glob -- $boehmarg { |
---|
47 | yes - |
---|
48 | 1 - |
---|
49 | on { |
---|
50 | set boehm 1 |
---|
51 | set libs "-lgc" |
---|
52 | } |
---|
53 | no - |
---|
54 | 0 - |
---|
55 | "" - |
---|
56 | off { |
---|
57 | set boehm 0 |
---|
58 | } |
---|
59 | * { |
---|
60 | set boehm 1 |
---|
61 | if {![file isdirectory $boehmarg]} { |
---|
62 | user-error "Boehm GC option does not specify a valid directory" |
---|
63 | } |
---|
64 | lappend includes "-I$boehmarg/include" |
---|
65 | lappend libs "-L$boehmarg/lib" "-lgc" |
---|
66 | } |
---|
67 | } |
---|
68 | |
---|
69 | define USE_BOEHM_GC $boehm |
---|
70 | |
---|
71 | proc localname {path} { |
---|
72 | return [regsub {[a-z]+/(.*)\.[a-z]+} $path "\\1"] |
---|
73 | } |
---|
74 | |
---|
75 | proc fmt-files {files} { |
---|
76 | set output [join $files] |
---|
77 | if {[string length $output] < 60} { |
---|
78 | return $output |
---|
79 | } |
---|
80 | set output " \\" |
---|
81 | set line " " |
---|
82 | foreach file $files { |
---|
83 | if {[string length $line] > 2 && [string length "$line $file"] > 72} { |
---|
84 | append line "\\" |
---|
85 | append output "\n" $line |
---|
86 | set line " " |
---|
87 | } |
---|
88 | append line $file " " |
---|
89 | } |
---|
90 | if {[string length $line] > 2} { |
---|
91 | append output "\n" [string trimright $line] |
---|
92 | } |
---|
93 | return $output |
---|
94 | } |
---|
95 | |
---|
96 | global inttypes csize iquote |
---|
97 | set inttypes {char short int long} |
---|
98 | |
---|
99 | proc def-int-type {name prefix size} { |
---|
100 | global inttypes csize have_long_long |
---|
101 | foreach type $inttypes { |
---|
102 | if {$csize($type) == $size} { |
---|
103 | define $name "$prefix $type" |
---|
104 | define SIZEOF_[string toupper $name] $size |
---|
105 | return |
---|
106 | } |
---|
107 | } |
---|
108 | if {$have_long_long} { |
---|
109 | define $name "$prefix long long" |
---|
110 | define SIZEOF_[string toupper $name] $csize(long long) |
---|
111 | } else { |
---|
112 | define $name "$prefix long" |
---|
113 | define SIZEOF_[string toupper $name] $csize(long) |
---|
114 | } |
---|
115 | } |
---|
116 | |
---|
117 | proc check-tmp-flags {flags} { |
---|
118 | msg-checking "Checking whether the C++ compiler accepts $flags..." |
---|
119 | set iquote [cctest -cflags "-iquote ."] |
---|
120 | if {$iquote} { |
---|
121 | msg-result yes |
---|
122 | return 1 |
---|
123 | } else { |
---|
124 | msg-result no |
---|
125 | return 0 |
---|
126 | } |
---|
127 | } |
---|
128 | |
---|
129 | cc-check-tools ar |
---|
130 | if {[opt-bool without-re2c]} { |
---|
131 | set re2c 0 |
---|
132 | } else { |
---|
133 | cc-check-progs re2c |
---|
134 | msg-checking "Checking re2c version..." |
---|
135 | if {[catch {exec re2c --version} re2cout]} { |
---|
136 | set re2c 0 |
---|
137 | msg-result "unknown (disable re2c)" |
---|
138 | } else { |
---|
139 | set re2c 1 |
---|
140 | switch -glob -- $re2cout { |
---|
141 | "re2c 1.*.*" { |
---|
142 | msg-result "($re2cout) ok" |
---|
143 | } |
---|
144 | * { |
---|
145 | msg-result "($re2cout) outdated" |
---|
146 | } |
---|
147 | } |
---|
148 | } |
---|
149 | } |
---|
150 | |
---|
151 | define RE2CFLAGS "" |
---|
152 | if {![is-defined RE2C]} { |
---|
153 | define RE2C false |
---|
154 | } |
---|
155 | cc-with {-lang c++} { |
---|
156 | # Does long long exist as a type? |
---|
157 | global have_long_long |
---|
158 | set have_long_long [cc-check-types "long long"] |
---|
159 | if {$have_long_long} { |
---|
160 | lappend inttypes "long long" |
---|
161 | define LongWord "unsigned long long" |
---|
162 | define LongInt "long long" |
---|
163 | define LONG_FMT "ll" |
---|
164 | } else { |
---|
165 | define LongWord "unsigned long" |
---|
166 | define LongInt "long" |
---|
167 | define LONG_FMT "l" |
---|
168 | } |
---|
169 | # Check all int sizes |
---|
170 | foreach type $inttypes { |
---|
171 | set csize($type) [cc-check-sizeof $type] |
---|
172 | } |
---|
173 | set csize(voidp) [cc-check-sizeof "void *"] |
---|
174 | if {$have_long_long && $csize(long long) >= 8} { |
---|
175 | define HAVE_64BIT_SUPPORT 2 |
---|
176 | } |
---|
177 | if {$csize(long) >= 8} { |
---|
178 | define HAVE_64BIT_SUPPORT 1 |
---|
179 | } |
---|
180 | cc-with {-includes sys/types.h} { |
---|
181 | set have_off_t [cc-check-types "off_t"] |
---|
182 | if {$have_off_t} { |
---|
183 | set csize(off_t) [cc-check-sizeof "off_t"] |
---|
184 | } |
---|
185 | } |
---|
186 | if {$csize(long) == $csize(voidp)} { |
---|
187 | define WORD_FMT "l" |
---|
188 | } elseif {$have_long_long && $csize(long long) == $csize(voidp)} { |
---|
189 | define WORD_FMT "ll" |
---|
190 | } else { |
---|
191 | define WORD_FMT "" |
---|
192 | } |
---|
193 | cc-check-includes dirent.h |
---|
194 | # -iquote vs. -I |
---|
195 | set iquote [check-tmp-flags "-iquote"] |
---|
196 | define OPT "" |
---|
197 | foreach opt {{-g -O2} {-g -O} {-g}} { |
---|
198 | if {[check-tmp-flags $opt]} { |
---|
199 | define OPT $opt |
---|
200 | break |
---|
201 | } |
---|
202 | } |
---|
203 | msg-checking "Checking for isatty()..." |
---|
204 | if {[cctest -code {isatty(0);} -includes unistd.h]} { |
---|
205 | msg-result yes |
---|
206 | define HAVE_ISATTY 1 |
---|
207 | } else { |
---|
208 | msg-result no |
---|
209 | } |
---|
210 | } |
---|
211 | |
---|
212 | define RULES "" |
---|
213 | |
---|
214 | global rules |
---|
215 | set rules "" |
---|
216 | |
---|
217 | proc add-rule {rule} { |
---|
218 | global rules |
---|
219 | append rules $rule\n |
---|
220 | } |
---|
221 | |
---|
222 | proc objpath {path} { |
---|
223 | global builddir |
---|
224 | return [regsub {[a-z]+/(.*)\.[a-z]+} $path "\$(BUILDDIR)/\\1.o"] |
---|
225 | } |
---|
226 | |
---|
227 | proc libobjpath {path} { |
---|
228 | global builddir |
---|
229 | return [regsub {[a-z]+/(.*)\.[a-z]+} $path "\$(BUILDDIR)/lib\\1.o"] |
---|
230 | } |
---|
231 | |
---|
232 | proc make-lib-rule {src} { |
---|
233 | set obj [libobjpath $src] |
---|
234 | add-rule "$obj: $src \$(LIBHDR)" |
---|
235 | add-rule "\t\$(MKDIR) \$(BUILDDIR)" |
---|
236 | switch -glob -- $src { |
---|
237 | *.c { |
---|
238 | add-rule "\t\$(CC) \$(CFLAGS) \$(GCFLAGS) \$(INCLUDES) -c -o $obj $src" |
---|
239 | } |
---|
240 | * { |
---|
241 | add-rule "\t\$(CXX) \$(CXXFLAGS) \$(INCLUDES) -c -o $obj $src" |
---|
242 | } |
---|
243 | } |
---|
244 | } |
---|
245 | |
---|
246 | proc make-prog-rule {src} { |
---|
247 | set obj [objpath $src] |
---|
248 | add-rule "$obj: $src \$(LIBHDR) \$(PROGHDR)" |
---|
249 | add-rule "\t\$(MKDIR) \$(BUILDDIR)" |
---|
250 | add-rule "\t\$(CXX) \$(CXXFLAGS) \$(INCLUDES) -c -o $obj $src" |
---|
251 | } |
---|
252 | |
---|
253 | proc test-rule {src} { |
---|
254 | global builddir |
---|
255 | set obj [objpath $src] |
---|
256 | set name [localname $src] |
---|
257 | set test "$builddir/$name" |
---|
258 | add-rule "$test: $obj \$(ADLIB)" |
---|
259 | add-rule "\t\$(MKDIR) \$(BUILDDIR)" |
---|
260 | add-rule "\t\$(CXX) \$(OPT) -o $test $obj \$(ADLIB) \$(LIBS)" |
---|
261 | add-rule "$name: $test .FORCE" |
---|
262 | add-rule "\t@echo === $test ===" |
---|
263 | add-rule "\t@$test" |
---|
264 | } |
---|
265 | |
---|
266 | def-int-type Word unsigned $csize(voidp) |
---|
267 | def-int-type Word8 unsigned 1 |
---|
268 | def-int-type Word16 unsigned 2 |
---|
269 | def-int-type Word32 unsigned 4 |
---|
270 | def-int-type Word64 unsigned 8 |
---|
271 | def-int-type Int signed $csize(voidp) |
---|
272 | def-int-type Int8 signed 1 |
---|
273 | def-int-type Int16 signed 2 |
---|
274 | def-int-type Int32 signed 4 |
---|
275 | def-int-type Int64 signed 8 |
---|
276 | |
---|
277 | if {$have_off_t} { |
---|
278 | def-int-type Offset signed $csize(off_t) |
---|
279 | } |
---|
280 | |
---|
281 | define BUILDDIR $builddir |
---|
282 | define BINDIR $bindir |
---|
283 | set testsrc {} |
---|
284 | set testobj {} |
---|
285 | set libsrc {} |
---|
286 | set libobj {} |
---|
287 | set adlibsrc [glob adlib/*.cc] |
---|
288 | if {!$boehm} { |
---|
289 | lappend adlibsrc "gclib/tinygc.c" |
---|
290 | } |
---|
291 | |
---|
292 | foreach file $adlibsrc { |
---|
293 | switch -glob -- $file { |
---|
294 | */test?* { |
---|
295 | lappend testsrc $file |
---|
296 | lappend testobj [objpath $file] |
---|
297 | } |
---|
298 | * { |
---|
299 | lappend libsrc $file |
---|
300 | lappend libobj [libobjpath $file] |
---|
301 | } |
---|
302 | } |
---|
303 | } |
---|
304 | |
---|
305 | set progsrc {} |
---|
306 | set progobj {} |
---|
307 | |
---|
308 | foreach file [glob -nocomplain src/*.cc] { |
---|
309 | switch -glob -- $file { |
---|
310 | */test?* { |
---|
311 | lappend testsrc $file |
---|
312 | lappend testobj [objpath $file] |
---|
313 | } |
---|
314 | * { |
---|
315 | lappend progsrc $file |
---|
316 | lappend progobj [objpath $file] |
---|
317 | } |
---|
318 | } |
---|
319 | } |
---|
320 | |
---|
321 | set libhdr "[glob adlib/*.h] [glob gclib/*.h] adlib/config.h" |
---|
322 | set libhdr [lsort -unique $libhdr] |
---|
323 | set progre [glob -nocomplain src/*.re] |
---|
324 | set proghdr [glob -nocomplain src/*.h] |
---|
325 | set gensrc {} |
---|
326 | foreach file $progre { |
---|
327 | lappend progsrc "[file rootname $file].cc" |
---|
328 | lappend gensrc "[file rootname $file].cc" |
---|
329 | lappend progobj [objpath $file] |
---|
330 | } |
---|
331 | |
---|
332 | set progsrc [lsort -unique $progsrc] |
---|
333 | set progobj [lsort -unique $progobj] |
---|
334 | |
---|
335 | define LIBSRC [fmt-files $libsrc] |
---|
336 | define LIBHDR [fmt-files $libhdr] |
---|
337 | define LIBOBJ [fmt-files $libobj] |
---|
338 | define PROGSRC [fmt-files $progsrc] |
---|
339 | define PROGOBJ [fmt-files $progobj] |
---|
340 | define PROGHDR [fmt-files $proghdr] |
---|
341 | define TESTOBJ [fmt-files $testobj] |
---|
342 | define GENSRC [fmt-files $gensrc] |
---|
343 | |
---|
344 | set targets "\$(BINDIR)/\$(PROGNAME)" |
---|
345 | |
---|
346 | set tests {} |
---|
347 | foreach file $testsrc { |
---|
348 | lappend tests "$builddir/[localname $file]" |
---|
349 | lappend targets "$builddir/[localname $file]" |
---|
350 | test-rule $file |
---|
351 | } |
---|
352 | |
---|
353 | define TESTS [fmt-files $tests] |
---|
354 | |
---|
355 | foreach file $progsrc { |
---|
356 | make-prog-rule $file |
---|
357 | } |
---|
358 | |
---|
359 | if {$re2c} { |
---|
360 | foreach file $progre { |
---|
361 | set ccfile "[file rootname $file].cc" |
---|
362 | add-rule "$ccfile: $file" |
---|
363 | add-rule "\t\$(RE2C) \$(RE2CFLAGS) -o $ccfile $file" |
---|
364 | } |
---|
365 | } |
---|
366 | |
---|
367 | foreach file $libsrc { |
---|
368 | make-lib-rule $file |
---|
369 | } |
---|
370 | |
---|
371 | foreach file $testsrc { |
---|
372 | make-prog-rule $file |
---|
373 | } |
---|
374 | |
---|
375 | define EXTRA [readfile $makeextra] |
---|
376 | |
---|
377 | define RULES $rules |
---|
378 | define LIBS [join $libs] |
---|
379 | define INCLUDES [join $includes] |
---|
380 | |
---|
381 | define CONFIGITEMS "adlib/config.h adlib/config-defs.h" |
---|
382 | |
---|
383 | if {[file exists $postconf]} { |
---|
384 | source $postconf |
---|
385 | } |
---|
386 | |
---|
387 | make-config-header adlib/config-defs.h -auto USE_* |
---|
388 | make-template cnf/config.h.in adlib/config.h |
---|
389 | make-template cnf/Makefile.in Makefile |
---|
390 | |
---|
391 | if {[file exists $postsetup]} { |
---|
392 | source $postsetup |
---|
393 | } |
---|
394 | |
---|