File: configure

package info (click to toggle)
mes 0.24.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 6,908 kB
  • sloc: ansic: 24,104; lisp: 11,490; sh: 6,609; asm: 187; makefile: 36
file content (733 lines) | stat: -rwxr-xr-x 32,854 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
#! /bin/sh
# -*-scheme-*-
MES_ARENA=100000000 exec ${SCHEME-guile} -L . --no-auto-compile -e '(configure)' -s "$0" ${1+"$@"}
!#

;;; GNU Mes --- Maxwell Equations of Software
;;; Copyright © 2016,2017,2018,2019,2020,2021,2022,2023 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
;;; Copyright © 2020 Vagrant Cascadian <vagrant@reproducible-builds.org>
;;;
;;; configure: This file is part of GNU Mes.
;;;
;;; GNU Mes is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Mes is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Mes.  If not, see <http://www.gnu.org/licenses/>.

(define-module (configure)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-9)
  #:use-module (srfi srfi-9 gnu)
  #:use-module (srfi srfi-26)
  #:use-module (ice-9 getopt-long)
  #:use-module (ice-9 optargs)
  #:use-module (ice-9 popen)
  #:use-module (ice-9 rdelim)
  #:export (main))

(define *shell* "sh")
(define PACKAGE "mes")
(define PACKAGE-NAME "GNU Mes")
(define PACKAGE-BUGREPORT "bug-mes@gnu.org")
(define VERSION "0.24.2")

(cond-expand
 (guile)
 (mes (mes-use-module (srfi srfi-1))
      (mes-use-module (srfi srfi-9))
      (mes-use-module (srfi srfi-9 gnu))
      (mes-use-module (srfi srfi-26))
      (mes-use-module (mes getopt-long))
      (mes-use-module (mes guile))
      (mes-use-module (mes misc))
      (mes-use-module (mes optargs))
      (define %host-type "x86_64-unknown-linux-gnu")
      (define OPEN_READ "r")
      (define (canonicalize-path o)
        (if (string-prefix? "/" o) o
            (string-append (getcwd) "/" o)))
      (define (sort lst less)
        lst)
      (define (close-pipe o) 0)
      (define (open-pipe* OPEN_READ . commands)
        (let ((fake-pipe ".pipe"))
          (with-output-to-file fake-pipe
            (lambda _
              (let ((status (apply system* (append commands))))
                (set! close-pipe (lambda _ status)))))
          (open-input-file fake-pipe)))))

(define* (PATH-search-path name #:key (default name) warn?)
  (or (search-path (string-split (getenv "PATH") #\:) name)
      (and (and warn? (format (current-error-port) "warning: not found: ~a\n" name))
           default)))

;;; Utility
(define (logf port string . rest)
  (apply format (cons* port string rest))
  (force-output port)
  #t)

(define (stderr string . rest)
  (apply logf (cons* (current-error-port) string rest)))

(define (stdout string . rest)
  (apply logf (cons* (current-output-port) string rest)))

(define %verbose? #f)

(define (verbose string . rest)
  (if %verbose? (apply stderr (cons string rest))))

(define (gulp-pipe command)
  (let* ((err (current-error-port))
         (foo (set-current-error-port (open-output-file ".error")))
         (port (open-pipe command "r"))
         (output (read-string port))
         (status (close-pipe port))
         (error (with-input-from-file ".error" read-string)))
    (when (file-exists? ".error")
      (delete-file ".error"))
    (set-current-error-port err)
    (verbose "command[~a]: ~s => ~a [~a]\n" status command output error)
    (if (not (zero? status)) ""
        (string-trim-right (string-append output error)))))

(define (gulp-pipe* . command)
  (gulp-pipe (string-join command)))

(define (tuple< a b)
  (cond
   ((and (null? a) (null? b)) #t)
   ((null? a) (not (null? b)))
   ((null? b) #f)
   ((and (not (< (car a) (car b)))
         (not (< (car b) (car a))))
    (tuple< (cdr a) (cdr b)))
   (else (< (car a) (car b)))))

(define (tuple<= a b)
  (or (equal? a b) (tuple< a b)))

(define (conjoin . predicates)
  (lambda (. arguments)
    (every (cut apply <> arguments) predicates)))

(define (char->char from to char)
  (if (eq? char from) to char))

(define (string-replace-char string from to)
  (string-map (cut char->char from to <>) string))

(define (string-replace-string string from to)
  (cond ((string-contains string from)
         => (lambda (i) (string-replace string to i (+ i (string-length from)))))
        (else string)))

(define (string-replace-string/all string from to)
  (or (and=> (string-contains string from)
             (lambda (i)
               (string-append
                (substring string 0 i)
                to
                (string-replace-string/all
                 (substring string (+ i (string-length from))) from to))))
      string))

;;; Configure

(define-immutable-record-type <dependency>
  (make-dependency name version-expected optional? version-option commands file-name data version-found)
  dependency?
  (name dependency-name)
  (version-expected dependency-version-expected)
  (optional? dependency-optional?)
  (version-option dependency-version-option)
  (commands dependency-commands)
  (file-name dependency-file-name)
  (data dependency-data)
  (version-found dependency-version-found))

(define* (make-dep name #:key (version '()) optional? (version-option "--version") (commands (list name)) file-name data)
  (let* ((env-var (getenv (name->shell-name name)))
         (commands (if env-var (cons env-var commands) commands)))
    (make-dependency name version optional? version-option commands file-name data #f)))

(define (find-dep name deps)
  (find (compose (cut equal? <> name) dependency-name) deps))

(define (file-name name deps)
  (and=> (find-dep name deps) dependency-file-name))

(define (variable-name dependency)
  (and=>
   (dependency-name dependency)
   name->shell-name))

(define (name->shell-name name)
  (string-upcase (string-replace-char name #\- #\_)))

(define (->string o)
  (cond ((number? o) (number->string o))
        ((string? o) o)
        (else (format #f "~a" o))))

(define (version->string version)
  (and version (string-join (map ->string version) ".")))

(define (string->version string)
  (let ((split (string-tokenize string
                                (char-set-adjoin char-set:digit #\.))))
    (and (pair? split)
         (let* ((version (sort split (lambda (a b)
                                       (let ((len-a (length (string-split a #\.)))
                                             (len-b (length (string-split b #\.))))
                                         (cond ((> len-a len-b) #t)
                                               ((< len-a len-b) #f)
                                               (else (> (string-length a) (string-length b))))))))
                (version (car version))
                (version (string-tokenize version
                                          (char-set-complement (char-set #\.)))))
           (map string->number version)))))

(define (check-program-version dependency)
  (let ((name (dependency-name dependency))
        (expected (dependency-version-expected dependency))
        (version-option (dependency-version-option dependency))
        (commands (dependency-commands dependency)))
    (let loop ((commands commands))
      (if (or (null? commands)
              (not (car commands))) dependency
          (let ((command (car commands)))
            (stdout "checking for ~a~a... " (if (string-index command #\space) name command)
                    (if (null? expected) ""
                        (format #f " [~a]" (version->string expected))))
            (let* ((output (gulp-pipe (string-append command " " (if version-option version-option ""))))
                   (actual (string->version output))
                   (pass? (and actual (tuple< expected actual)))
                   (dependency (set-field dependency (dependency-version-found) actual)))
              (stdout "~a ~a\n" (if pass? (if (pair? actual) "" "yes")
                                    (if actual " no, found" "no"))
                      (or (version->string actual) ""))
              (if pass? (let ((file-name (or (PATH-search-path command)
                                             (dependency-file-name dependency))))
                          (set-field dependency (dependency-file-name) file-name))
                  (loop (cdr commands)))))))))

(define (check-file dependency)
  (stdout "checking for ~a... " (dependency-name dependency))
  (let ((file-name (and (file-exists? (dependency-file-name dependency))
                        (dependency-file-name dependency))))
    (stdout "~a\n" (or file-name ""))
    (set-field dependency (dependency-file-name) file-name)))

(define* (check-header-c cc dependency #:optional (check check-preprocess-header-c))
  (let ((name (dependency-name dependency)))
    (stderr "checking for ~a..." name)
    (let ((result (check cc name)))
      (when (file-exists? ".config.c")
        (delete-file ".config.c"))
      (stderr " ~a\n" (if result "yes" "no"))
      (if result (set-field dependency (dependency-file-name) name)
          dependency))))

(define* (check-compile-c cc dependency #:optional (check check-compile-string-c))
  (let ((name (dependency-name dependency)))
    (stderr "checking for ~a..." name)
    (let ((result (check cc (dependency-data dependency))))
      (when (file-exists? ".config.c")
        (delete-file ".config.c"))
      (stderr " ~a\n" (if result "yes" "no"))
      (if result (set-field dependency (dependency-file-name) name)
          dependency))))

(define* (check-link-c cc dependency #:optional (check check-link-string-c))
  (let ((name (dependency-name dependency)))
    (stderr "checking for ~a..." name)
    (let ((result (check cc (dependency-data dependency))))
      (when (file-exists? ".config.c")
        (delete-file ".config.c"))
      (stderr " ~a\n" (if result "yes" "no"))
      (if result (set-field dependency (dependency-file-name) name)
          dependency))))

(define (cflags-list)
  (let ((cflags (getenv "CFLAGS")))
    (if cflags (list cflags)
        '())))

(define (ldflags-list)
  (let ((ldflags (getenv "LDFLAGS")))
    (if ldflags (list ldflags)
        '())))

(define (check-preprocess-header-c cc header)
  (with-output-to-file ".config.c"
    (cut format #t "#include \"~a\"" header))
  (let ((test (lambda _ (apply system* `(,cc "-E" "-o" ".config.E" ,@(cflags-list) ".config.c")))))
    (zero? (if %verbose? (test)
               (with-error-to-file "/dev/null"
                 test)))))

(define (check-compile-string-c cc string)
  (with-output-to-file ".config.c"
    (cut display string))
  (let ((test (lambda _ (apply system* `(,cc "-std=gnu99" "-c" "-o" ".config.o" ,@(cflags-list) ".config.c")))))
    (zero? (if %verbose? (test)
               (with-error-to-file "/dev/null"
                 test)))))

(define (check-link-string-c cc string)
  (with-output-to-file ".config.c"
    (cut display string))
  (let ((test (lambda _ (apply system* `(,cc "-std=gnu99" "-o" ".config" ,@(cflags-list) ,@(ldflags-list) ".config.c")))))
    (zero? (if %verbose? (test)
               (with-error-to-file "/dev/null"
                 test)))))

(define (parse-opts args)
  (let* ((option-spec
  	  '((build (value #t))
            (host (value #t))

            (prefix (value #t))
            (program-prefix (value #t))
            (bindir (value #t))
            (datadir (value #t))
            (docdir (value #t))
            (includedir (value #t))
            (libdir (value #t))
            (srcdir (value #t))
            (sysconfdir (value #t))

            (mes)
            (help (single-char #\h))
            (verbose (single-char #\v))
            (with-bootstrap)
            (with-cheating)
            (with-courage)
            (infodir (value #t))
            (mandir (value #t))
            (disable-colors)
            (enable-colors)
            (disable-silent-rules)
            (enable-silent-rules)
            (with-system-libc)

            (enable-fast-install)         ; Ignored for Guix
            (disable-dependency-tracking) ; Ignored for Debian
            (disable-maintainer-mode)     ; Ignored for Debian
            (disable-option-checking)     ; Ignored for Debian
            (libexecdir (value #t))       ; Ignored for Debian
            (localstatedir (value #t))    ; Ignored for Debian
            (mandir (value #t))           ; Ignored for Debian
            (runstatedir (value #t)))))   ; Ignored for Debian

    (getopt-long args option-spec)))

(define* (print-help #:optional (port (current-output-port)))
  (format port "\
`configure' configures ~a ~a to adapt to many kinds of systems.

Usage: ./configure [OPTION]... [VAR=VALUE]

To assign environment variables (e.g., CC, CFLAGS...), specify them as
VAR=VALUE.  See below for descriptions of some of the useful variables.

Defaults for the options are specified in brackets.

Options:
  -h, --help           display this help
      --build=BUILD    configure for building on BUILD [guessed]
      --colors         no colorized output
      --disable-silent-rules
                       verbose build output [V=1]
      --host=HOST      cross-compile to build programs to run on HOST [BUILD]
  -v, --verbose        be verbose
  --with-bootstrap     After building mes with CC, build mes with MesCC
  --with-courage       Assert that even if this platform is unsupported,
                       you will be courageous and port GNU Mes to it
                       (see \"Porting GNU Mes\" in the manual.)
  --with-cheating      cheat using Guile instead of Mes
  --with-system-libc   use system libc

Installation directories:
  --prefix=DIR         install in prefix DIR [~a]

  --bindir=DIR         user executables [PREFIX/bin]
  --includedir=DIR     C header files [PREFIX/include]
  --infodir=DIR        info documentation [PREFIX/share/info]
  --libdir=DIR         object code libraries [EPREFIX/lib]
  --mandir=DIR         man pages [PREFIX/share/man]

Program names:
  --program-prefix=PREFIX            prepend PREFIX to installed program names
  --program-suffix=SUFFIX            append SUFFIX to installed program names

Ignored for Guix:
  --enable-fast-install

Ignored for Debian:
  --disable-dependency-tracking
  --disable-maintainer-mode
  --libexecdir=DIR
  --localstatedir=DIR
  --runstatedir=DIR

Some influential environment variables:
  CC                C compiler command
  CFLAGS            C compiler flags
  CPPFLAGS          C preprocessor flags
  LDFLAGS           C linker flags
  GUILE             guile command
  GUILD             guild command
  GUILD_OPTIMIZE    guild compile optimization
  GUILE_LOAD_PATH   guile load path; where to find Nyacc
  MES_FOR_BUILD     build system MES [can be mes or guile]
" PACKAGE VERSION (getenv "prefix")))

(define (main args)
  (let* ((options (parse-opts args))
         (build-type (option-ref options 'build %host-type))
         (host-type (option-ref options 'host build-type))

         (prefix "/usr/local")
         (prefix (option-ref options 'prefix prefix))
         (program-prefix (option-ref options 'program-prefix ""))
         (program-suffix (option-ref options 'program-suffix ""))
         (infodir (option-ref options 'infodir "${prefix}/share/info"))
         (mandir (option-ref options 'mandir "${prefix}/share/man"))
         (sysconfdir (option-ref options 'sysconfdir "${prefix}/etc"))

         (bindir (option-ref options 'bindir "${prefix}/bin"))
         (datadir (option-ref options 'datadir "${prefix}/share"))
         (docdir (option-ref options 'docdir "${datadir}/doc/mes"))
         (includedir (option-ref options 'includedir "${prefix}/include"))
         (libdir (option-ref options 'libdir "${prefix}/lib"))
         (pkgdatadir (string-append datadir "/mes"))
         (guile-load-path (if (and (pair? %load-path) (equal? (car %load-path) ".")) (cdr %load-path)
                              %load-path))
         (guile-effective-version (effective-version))
         (guile-site-dir (if (equal? prefix ".") (canonicalize-path ".")
                             (string-append prefix "/share/guile/site/" guile-effective-version)))
         (guile-site-ccache-dir (if (equal? prefix ".") (canonicalize-path ".")
                                    (string-append prefix "/lib/guile/" guile-effective-version "/site-ccache")))

         (srcdir (dirname (car (command-line))))
         (srcdest (if (equal? srcdir ".") ""
                      (string-append srcdir "/")))
         (abs-top-srcdir (canonicalize-path srcdir))
         (abs-top-builddir (canonicalize-path (getcwd)))
         (top-builddir (if (equal? srcdir ".") "."
                           abs-top-builddir))

         (with-bootstrap? (option-ref options 'with-bootstrap #f))
         (with-cheating? (option-ref options 'with-cheating #f))
         (with-courage? (option-ref options 'with-courage #f))
         (disable-colors? (option-ref options 'disable-colors #f))
         (enable-colors? (option-ref options 'enable-colors #f))
         (disable-silent-rules? (option-ref options 'disable-silent-rules #f))
         (enable-silent-rules? (option-ref options 'enable-silent-rules #f))
         (with-system-libc? (option-ref options 'with-system-libc #f))
         (vars (filter (cut string-index <> #\=) (option-ref options '() '())))
         (help? (option-ref options 'help #f))
         (mes? (option-ref options 'mes #f)))
    (when help?
      (print-help)
      (exit 0))
    (set! %verbose? (option-ref options 'verbose #f))
    (when %verbose?
      (stderr "configure args=~s\n" args))
    (for-each (lambda (v) (apply setenv (string-split v #\=))) vars)
    (let* ((cross? (not (equal? host-type build-type)))
           (gcc (if cross? (string-append host-type "-" "gcc") "gcc"))
           (tcc (if cross? (string-append host-type "-" "tcc") "tcc"))
           (mescc (if cross? (string-append host-type "-" "mescc") "mescc"))
           (deps (fold (lambda (program results)
                         (cons (check-program-version program) results))
                       '()
                       (list (make-dep "hex2" #:version '(1 4 0))
                             (make-dep "M1" #:version '(1 4 0))
                             (make-dep "blood-elf" #:version '(2 0 0))
                             (make-dep "kaem" #:version '(1 0 0) #:optional? #t)
                             (make-dep "M2-Planet" #:version '(1 9 0) #:optional? #t)
                             (make-dep "diff" #:optional? #t)
                             (make-dep "guile" #:version '(2 0) #:commands '("guile-3.0" "guile-3" "guile-2.2" "guile-2.0" "guile-2" "guile") #:optional? #t)
                             (make-dep "mes" #:version '(0 22) #:optional? #t)
                             (make-dep "guix" #:version '() #:optional? #t)
                             (make-dep "ar" #:version '(2 10) #:optional? #t)
                             (make-dep "sh" #:optional? #t)
                             (make-dep "bash" #:version '(2 0) #:optional? #t)
                             (make-dep "guild" #:version '(2 0) #:commands '("guild" "guile-tools" "true"))
                             (make-dep "CC" #:commands `(,(getenv "CC")) #:optional? #t)
                             (make-dep "CC-v" #:commands `(,(getenv "CC")) #:optional? #t #:version-option "-v")
                             (make-dep "cc" #:commands '("cc") #:optional? #t)
                             (make-dep "gcc" #:commands `(,gcc "gcc") #:optional? #t)
                             (make-dep "mescc" #:commands `(,mescc "mescc") #:optional? #t)
                             (make-dep "tcc" #:commands `(,tcc "tcc") #:optional? #t #:version-option "-v")
                             (make-dep "cc-v" #:commands '("cc") #:optional? #t #:version-option "-v")
                             (make-dep "make" #:optional? #t #:commands '("gmake" "make"))
                             (make-dep "makeinfo" #:version '(6) #:optional? #t)
                             (make-dep "dot" #:version-option "-V" #:optional? #t)
                             (make-dep "help2man" #:version '(1 47) #:optional? #t)
                             (make-dep "perl" #:version '(5) #:optional? #t))))
           (guile (file-name "guile" deps))
           ;; See https://bugs.gnu.org/43831; use -O1 with Guile-3
           (guild-optimize (let ((guile-version (and=> (find-dep "guile" deps) dependency-version-found)))
                             (and guile-version (tuple< '(2 9) guile-version) "-O1")))
           (deps (if guile (cons (check-program-version (make-dep "nyacc" #:version '(0 99 0) #:commands (list (string-append guile " -c '(use-modules (nyacc lalr)) (display *nyacc-version*)'")) #:file-name #t #:version-option #f))
                                 deps)
                     deps))
           (guile (or guile "guile"))
           (cc (or (file-name "CC" deps)
                   (file-name "CC-v" deps)
                   (file-name "tcc" deps)
                   (file-name "gcc" deps)
                   (file-name "cc" deps)
                   (file-name "cc-v" deps)
                   (file-name "mescc" deps)))
           (m2-planet (file-name "M2-Planet" deps))
           (deps (if cc
                     (cons* (check-header-c cc (make-dep "limits.h"))
                            (check-header-c cc (make-dep "stdio.h" #:optional? #t))
                            deps)
                     deps))
           (missing (filter (conjoin (negate dependency-file-name)
                                     (negate dependency-optional?)) deps))
           (deps (if cc
                     (cons (check-compile-c cc (make-dep "cc is GNU C" #:data "#if !defined (__GNUC__)
#error no gnuc
#endif
"))
                           deps)
                     deps))
           (gcc? (file-name "cc is GNU C" deps))
           (deps (if cc
                     (cons (check-compile-c cc (make-dep "cc is Mes C" #:data "#if !defined (__MESC__)
#error no mesc
#endif
"))
                           deps)
                     deps))
           (mescc? (file-name "cc is Mes C" deps))
           (deps (if cc
                     (cons (check-compile-c cc (make-dep "cc is Tiny C" #:data "#if !defined (__TINYC__)
#error no tinycc
#endif
"))
                           deps)
                     deps))
           (tcc? (file-name "cc is Tiny C" deps))
           (deps (if cc
                     (cons (check-link-c cc (make-dep "whether cc can create executables" #:data "int main () {return 0;}"))
                           deps)
                     deps))
           (system-libc? (and with-system-libc? (file-name "if cc can create executables" deps)))
           (host-type (or (and cc (let ((dump (gulp-pipe* cc "-dumpmachine")))
                                    (and (not (string-null? dump)) dump))) host-type))
           (host-type-list (string-split host-type #\-))
           (mes-cpu (car host-type-list))
           (mes-cpu (cond ((member mes-cpu '("i386" "i486" "i586" "i686")) "x86")
                          ((member mes-cpu '("arm" "armv4" "armv7l")) "arm")
                          (else mes-cpu)))
           (mes-bits (if (member mes-cpu '("x86_64")) "64"
                         "32"))
           (mes-libc (if system-libc? "system" "mes"))
           (mes-kernel (car (filter
                             (compose not
                                      (cut member <> '("pc" "portbld" "unknown")))
                             (cdr host-type-list))))
           (mes-kernel (if (string-prefix? "freebsd" mes-kernel) "freebsd" mes-kernel))
           (mes-compiler (cond (gcc? "gcc") (tcc? "gcc") (mescc? "mescc") (else "bootstrap")))
           (mes-system (string-join (list mes-cpu mes-kernel "mes") "-"))
           (bash (or (and (file-exists? "/bin/bash") "/bin/bash")
                     (file-name "bash" deps)
                     ""))
           (shell (or (and (file-exists? "/bin/bash") "/bin/bash")
                      (file-name "bash" deps)
                      (and (file-exists? "/bin/sh") "/bin/sh")
                      (file-name "sh" deps)
                      "sh"))
           (hex2 (file-name "hex2" deps))
           (numbered-arch? (begin
                             (stdout "checking for stage0 architecture flag... ")
                             (let ((numbered? (and hex2 (string-contains (gulp-pipe (string-append hex2 " --help 2>&1" )) "--Architecture 12345"))))
                               (stdout (if numbered? "numbered\n" "named\n"))
                               numbered?))))

      (define* (substitute file-name pairs
                           #:key (target (if (string-suffix? ".in" file-name)
                                             (string-drop-right file-name 3) file-name)))
        (system* "mkdir" "-p" (dirname target))
        (with-output-to-file target
          (lambda _
            (let ((in (open-input-file file-name)))
              (let loop ((line (read-line in 'concat)))
                (when (not (eof-object? line))
                  (display (fold (lambda (o result)
                                   (string-replace-string/all result (car o) (cdr o)))
                                 line pairs))
                  (loop (read-line in 'concat))))))))

      (when (and (not (member mes-system '("arm-linux-mes"
                                           "x86-linux-mes"
                                           "x86_64-linux-mes")))
                 (not with-courage?))
        (stderr "platform not supported: ~a
See \"Porting GNU Mes\" in the manual, or try --with-courage\n" mes-system)
        (exit 1))
      (when (pair? missing)
        (stderr "\nMissing dependencies: ~a\n" (string-join (map dependency-name missing)))
        (exit 1))
      (let ((pairs `(("@PACKAGE@" . ,PACKAGE)
                     ("@PACKAGE_NAME@" . ,PACKAGE-NAME)
                     ("@PACKAGE_BUGREPORT@" . ,PACKAGE-BUGREPORT)
                     ("@VERSION@" . ,VERSION)

                     ("@build@" . ,build-type)
                     ("@host@" . ,host-type)

                     ("@bootstrap@" . ,(if with-bootstrap? "true" "false"))
                     ("@courageous@" . ,(if with-courage? "true" "false"))
                     ("@compiler@" . ,mes-compiler)
                     ("@mes_bits@" . ,mes-bits)
                     ("@mes_kernel@" . ,mes-kernel)
                     ("@mes_cpu@" . ,mes-cpu)
                     ("@mes_libc@" . ,mes-libc)
                     ("@mes_system@" . ,mes-system)

                     ("@abs_top_srcdir@" . ,abs-top-srcdir)
                     ("@abs_top_builddir@" . ,abs-top-builddir)
                     ("@top_builddir@" . ,top-builddir)

                     ("@srcdest@" . ,srcdest)
                     ("@srcdir@" . ,srcdir)

                     ("@prefix@" . ,prefix)
                     ("@program_prefix@" . ,program-prefix)
                     ("@bindir@" . ,bindir)
                     ("@datadir@" . ,datadir)
                     ("@pkgdatadir@" . ,pkgdatadir)
                     ("@docdir@" . ,docdir)
                     ("@guile_site_ccache_dir@" . ,guile-site-ccache-dir)
                     ("@guile_site_dir@" . ,guile-site-dir)
                     ("@infodir@" . ,infodir)
                     ("@includedir@" . ,includedir)
                     ("@libdir@" . ,libdir)
                     ("@mandir@" . ,mandir)
                     ("@sysconfdir@" . ,sysconfdir)

                     ("@colors@" . ,(if disable-colors? "no" "yes"))
                     ("@V@" . ,(if disable-silent-rules? "1" "0"))

                     ("@AR@" . ,(or (file-name "ar" deps) ""))
                     ("@BASH@" . ,bash)
                     ("@CC@" . ,(or cc ""))
                     ("@DIFF@" . ,(or (file-name "diff" deps) (string-append abs-top-builddir "/pre-inst-env diff.scm")))
                     ("@DOT@" . ,(or (file-name "dot" deps) ""))
                     ("@GIT@" . ,(or (file-name "git" deps) ""))
                     ("@GUILD_OPTIMIZE@" . ,(or (getenv "GUILD_OPTIMIZE") guild-optimize ""))
                     ("@GUILE@" . ,guile)
                     ("@GUILE_EFFECTIVE_VERSION@" . ,(effective-version))
                     ("@GUILE_LOAD_PATH@" . ,(string-join guile-load-path ":"))
                     ("@GUIX@" . ,(or (file-name "guix" deps) ""))
                     ("@HELP2MAN@" . ,(or (file-name "help2man" deps) ""))
                     ("@KAEM@" . ,(or (file-name "kaem" deps) ""))
                     ("@M2_PLANET@" . ,(or (file-name "M2-Planet" deps) ""))
                     ("@MAKEINFO@" . ,(or (file-name "makeinfo" deps) ""))
                     ("@MES_FOR_BUILD@" . ,(or (file-name "mes" deps)
                                               guile))
                     ("@PERL@" . ,(or (file-name "perl" deps) ""))
                     ("#SCHEME=\"@SCHEME@\"" . ,(if with-cheating? (string-append "\nSCHEME=\"" guile "\"") ""))
                     ("@SCHEME@" . ,(if with-cheating? guile ""))
                     ("@SHELL@" . ,shell)

                     ("@CFLAGS@" . ,(or (getenv "CFLAGS") "-static -g"))
                     ("@CPPFLAGS@" . ,(or (getenv "CPPFLAGS") ""))
                     ("@LDFLAGS@" . ,(or (getenv "LDFLAGS") "-static -g"))
                     ("@HEX2FLAGS@" . ,(or (getenv "HEX2FLAGS") ""))
                     ("@M1FLAGS@" . ,(or (getenv "M1FLAGS") ""))
                     ("@numbered_arch@" . ,(if numbered-arch? "true" "false"))

                     ,@(map
                        (lambda (o)
                          (cons (string-append "@" (variable-name o) "@") (or (format #f "~a" (dependency-file-name o)) "")))
                        deps))))

        (unless (or cc m2-planet)
          (format (current-error-port) "must supply C compiler or M2-Planet\n")
          (exit 2))
        (for-each (lambda (o)
                    (let* ((src (string-append srcdest o))
                           (target (string-drop-right o 3))
                           (target (if (not (string-prefix? "build-aux/" target)) target
                                       (string-drop target (string-length "build-aux/")))))
                      (substitute src pairs #:target target)))
                  '(
                    "build-aux/GNUmakefile.in"
                    "build-aux/config.sh.in"
                    "build-aux/build.sh.in"
                    "build-aux/check.sh.in"
                    "build-aux/install.sh.in"
                    "build-aux/pre-inst-env.in"
                    "build-aux/uninstall.sh.in"
                    "scripts/mesar.in"
                    "scripts/mescc.scm.in"
                    "scripts/mescc.in"
                    ))
        (chmod "pre-inst-env" #o755)
        (chmod "scripts/mesar" #o755)
        (chmod "scripts/mescc" #o755)
        (chmod "scripts/mescc.scm" #o755)
        (chmod "build.sh" #o755)
        (chmod "check.sh" #o755)
        (chmod "install.sh" #o755)
        (chmod "uninstall.sh" #o755)

        (system* "mkdir" "-p" "include/mes")
        (let ((pkgdatadir (gulp-pipe* "sh" "-c" (string-append "prefix=" prefix
                                                               ";datadir=" datadir
                                                               ";echo ${datadir}/mes"))))
          (with-output-to-file "include/mes/config.h"
            (lambda _
              (if system-libc?
                  (display "#define SYSTEM_LIBC 1
")
                  (display "#undef SYSTEM_LIBC
"))
              (display (string-append "
#define MES_VERSION \"" VERSION "\"
")))))
        (substitute (string-append srcdest "build-aux/config.make.in") pairs #:target ".config.make"))
      (let ((arch-dir (string-append srcdest "include/" mes-kernel "/" mes-cpu)))
        (define (copy-header file-name)
          (system* "cp" "-f" "-v"
                   (string-append arch-dir "/" file-name)
                   (string-append "include/arch/" file-name)))
        (system* "mkdir" "-p" "include/arch")
        (for-each copy-header '("kernel-stat.h" "syscall.h")))

      (let ((make (and=> (file-name "make" deps) basename)))
        (display (string-append "
GNU Mes is configured for
   compiler:   " mes-compiler "
   cpu:        " mes-cpu "
   bits:       " mes-bits "
   libc:       " mes-libc "
   kernel:     " mes-kernel "
   system:     " mes-system "
   tools:      " (if numbered-arch? "numbered" "named") " arch
   bootstrap:  " (if with-bootstrap? "yes" "no") "
   courageous: " (if with-courage? "yes" "no") "

Run:
  " (or make "./build.sh") "           to build mes
  " (or make "./build.sh") " help      for help on other targets\n"))))))