| 1 |
#! /usr/bin/env gosh |
| 2 |
|
| 3 |
;;; compile-r7rs - compile R7RS programs |
| 4 |
;;; |
| 5 |
;;; Copyright (c) 2024 Antero Mejr <[email protected]> |
| 6 |
;;; |
| 7 |
;;; Redistribution and use in source and binary forms, with or without |
| 8 |
;;; modification, are permitted provided that the following conditions |
| 9 |
;;; are met: |
| 10 |
;;; |
| 11 |
;;; 1. Redistributions of source code must retain the above copyright |
| 12 |
;;; notice, this list of conditions and the following disclaimer. |
| 13 |
;;; |
| 14 |
;;; 2. Redistributions in binary form must reproduce the above copyright |
| 15 |
;;; notice, this list of conditions and the following disclaimer in the |
| 16 |
;;; documentation and/or other materials provided with the distribution. |
| 17 |
;;; |
| 18 |
;;; 3. Neither the name of the authors nor the names of its contributors |
| 19 |
;;; may be used to endorse or promote products derived from this |
| 20 |
;;; software without specific prior written permission. |
| 21 |
;;; |
| 22 |
;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 23 |
;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 24 |
;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 25 |
;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 26 |
;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 27 |
;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 28 |
;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 29 |
;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 30 |
;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 31 |
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 32 |
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 33 |
;;; |
| 34 |
|
| 35 |
;; Similar to build-standalone, but with an SRFI 138-compliant interface. |
| 36 |
|
| 37 |
(use gauche.cgen.standalone) |
| 38 |
(use gauche.parseopt) |
| 39 |
(use gauche.process) |
| 40 |
(use util.match) |
| 41 |
(use file.util) |
| 42 |
|
| 43 |
;; Needs to return a list of .sld files relative to the supplied dir. |
| 44 |
(define (find-slds dir) |
| 45 |
(let-values (((subdirs files) (directory-list2 dir :children? #t |
| 46 |
:add-path? #f))) |
| 47 |
(let* ((slds (filter (lambda (path) |
| 48 |
(let ((ext (path-extension path))) |
| 49 |
(and ext (string=? "sld" ext)))) |
| 50 |
files)) |
| 51 |
(sd-sld (apply append (map (lambda (sd) |
| 52 |
(map (lambda (sld) |
| 53 |
(build-path sd sld)) |
| 54 |
(find-slds (build-path dir sd)))) |
| 55 |
subdirs)))) |
| 56 |
(append slds sd-sld)))) |
| 57 |
|
| 58 |
;; Needs to return a list of full paths to directories with sld files. |
| 59 |
(define (subdirs-with-slds dir) |
| 60 |
(let-values (((subdirs files) (directory-list2 dir :children? #t |
| 61 |
:add-path? #t))) |
| 62 |
(let* ((slds (filter (lambda (path) |
| 63 |
(let ((ext (path-extension path))) |
| 64 |
(and ext (string=? "sld" ext)))) |
| 65 |
files)) |
| 66 |
(sd-sld (apply append (map subdirs-with-slds subdirs)))) |
| 67 |
(if (null? slds) |
| 68 |
sd-sld |
| 69 |
(cons dir sd-sld))))) |
| 70 |
|
| 71 |
(define (usage) |
| 72 |
(print "Usage: compile-r7rs [options...] [pathname]") |
| 73 |
(print) |
| 74 |
(print "Options:") |
| 75 |
(print (option-parser-help-string)) |
| 76 |
(exit 1)) |
| 77 |
|
| 78 |
(define (main args) |
| 79 |
(and-let1 alt-compiler (sys-getenv "COMPILE_R7RS") |
| 80 |
(print #"compile-r7rs: Environment variable COMPILE_R7RS is set (~|alt-compiler|).") |
| 81 |
(print #" We don't run it as specified in SRFI-138, for it may be confusing") |
| 82 |
(print #" if the variable is inadvertently set. If you want to run ~|alt-compiler|,") |
| 83 |
(print #" invoke it directly.")) |
| 84 |
(do-compile (cdr args))) |
| 85 |
|
| 86 |
(define (do-compile args) |
| 87 |
(let-args args |
| 88 |
([outfile "o=s{OUTFILE}" (cond-expand |
| 89 |
[gauche.os.windows "a.exe"] |
| 90 |
[else "a.out"]) |
| 91 |
? "Specify output file name. When omitted, 'a.out' or 'a.exe' |
| 92 |
is used, depending on the platform."] |
| 93 |
[incdirs "I*=s{DIR}" |
| 94 |
? "Specify a search path of extra files (lib/library.scm ...) |
| 95 |
to prepend if they're not relative to the current directory. |
| 96 |
This option can be given multiple times."] |
| 97 |
[appenddirs "A*=s{DIR}" |
| 98 |
? "Specify a search path of extra files (lib/library.scm ...) |
| 99 |
to append if they're not relative to the current directory. |
| 100 |
This option can be given multiple times."] |
| 101 |
[feature-ids "D*=y{NAME}" |
| 102 |
? "Add {NAME} to the list of feature identifiers maintained |
| 103 |
by the Scheme implementation for the purpose of executing |
| 104 |
the program in the text file pathname. |
| 105 |
This option can be given multiple times."] |
| 106 |
[else _ (usage)] |
| 107 |
. files) |
| 108 |
(match files |
| 109 |
[(script.scm) |
| 110 |
(let* ([dirs (append incdirs appenddirs)] |
| 111 |
[extras (fold-right (lambda (dir acc) |
| 112 |
(append acc (find-slds dir))) |
| 113 |
'() dirs)] |
| 114 |
[incdirs (fold-right (lambda (dir acc) |
| 115 |
(append acc (subdirs-with-slds dir))) |
| 116 |
'() dirs)]) |
| 117 |
(build-standalone script.scm |
| 118 |
:outfile outfile |
| 119 |
:extra-files extras |
| 120 |
:include-dirs incdirs |
| 121 |
:feature-ids feature-ids))] |
| 122 |
[_ (usage)])) |
| 123 |
0) |
| 124 |
|
| 125 |
;; Local variables: |
| 126 |
;; mode: scheme |
| 127 |
;; end: |