| 1 |
;;; |
| 2 |
;;; precomp - Precompiler |
| 3 |
;;; |
| 4 |
;;; Copyright (c) 2004-2009 Shiro Kawai, All rights reserved. |
| 5 |
;;; |
| 6 |
;;; Redistribution and use in source and binary forms, with or without |
| 7 |
;;; modification, are permitted provided that the following conditions |
| 8 |
;;; are met: |
| 9 |
;;; |
| 10 |
;;; 1. Redistributions of source code must retain the above copyright |
| 11 |
;;; notice, this list of conditions and the following disclaimer. |
| 12 |
;;; |
| 13 |
;;; 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
;;; notice, this list of conditions and the following disclaimer in the |
| 15 |
;;; documentation and/or other materials provided with the distribution. |
| 16 |
;;; |
| 17 |
;;; 3. Neither the name of the authors nor the names of its contributors |
| 18 |
;;; may be used to endorse or promote products derived from this |
| 19 |
;;; software without specific prior written permission. |
| 20 |
;;; |
| 21 |
;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 |
;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 |
;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 |
;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 |
;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 |
;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 27 |
;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 28 |
;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 29 |
;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 |
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 |
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 |
;;; |
| 33 |
|
| 34 |
;;; |
| 35 |
;;; This is a hack to "compile" the Scheme-written compiler into static |
| 36 |
;;; C data structure, so that it can be linked into libgauche. |
| 37 |
;;; |
| 38 |
|
| 39 |
(use file.util) |
| 40 |
(use gauche.cgen.precomp) |
| 41 |
(use gauche.parseopt) |
| 42 |
(use scheme.list) |
| 43 |
(use srfi.13) |
| 44 |
(use util.match) |
| 45 |
|
| 46 |
(define (main args) |
| 47 |
(let-args (cdr args) |
| 48 |
([predef-syms "D*=s{NAME}" |
| 49 |
? "Insert '#define NAME' at the beginning of |
| 50 |
output file. This option can be specified |
| 51 |
multiple times."] |
| 52 |
[dso-name "d|dso-name=s{FILE.SO}"] |
| 53 |
[ext-main "e|ext-main" |
| 54 |
? "Generate source for an extension module rather |
| 55 |
than a standalone executable. The initialization |
| 56 |
function follows the protocol of Gauche extension |
| 57 |
initializer."] |
| 58 |
[ext-module "ext-module=s{NAME}" #f |
| 59 |
? "This is recognized for the backward compatibility. |
| 60 |
It works just as '--ext-main' is given, regardless |
| 61 |
of the value of {NAME}."] |
| 62 |
[#f "h|help" => (^[] (usage #f)) |
| 63 |
? "Show this message and exit"] |
| 64 |
[includes "I*=s{DIR}" |
| 65 |
? "Specify additional load path, a directory from |
| 66 |
which Scheme files are searched. This option |
| 67 |
can be specified multiple times."] |
| 68 |
[out.sci "i|interface=s{FILE.SCI}" |
| 69 |
? "Specify output interface file. Valid only for |
| 70 |
single input file. If omitted, input filename |
| 71 |
with '.sci' extension is used."] |
| 72 |
[keep-private-macro "M|keep-private-macro=s{NAME,NAME,...}" |
| 73 |
? "If a macro is not exported, it won't be emitted |
| 74 |
to the precompiled file by default. With this |
| 75 |
option, the named macros are kept in the output |
| 76 |
even if they're private to the module."] |
| 77 |
[out.c "o|output=s{FILE.C}" |
| 78 |
? "Specifies output file name. If omitted, the |
| 79 |
input file name with '.c' extension is used. |
| 80 |
Valid only for single input file."] |
| 81 |
[omit-debug-source-info "omit-debug-source-info" |
| 82 |
? "Do not include debug source info to the |
| 83 |
precompiled code. This does not affect the |
| 84 |
behavior of the code, but disassembling |
| 85 |
won't show the source info."] |
| 86 |
[xprefix-all "P|strip-prefix-all" |
| 87 |
? "Remove all directory names from the input file |
| 88 |
names, just use their basenames, to produce |
| 89 |
output file names."] |
| 90 |
[xprefix "p|strip-prefix=s{PREFIX}" |
| 91 |
? "Remove {PREFIX} from the input file names to |
| 92 |
produce output file names. Useful if the |
| 93 |
source files are in a separate directory."] |
| 94 |
[single-sci "single-interface" |
| 95 |
? "Generate single interface file, instead of one |
| 96 |
for each input file. Valid only for multiple |
| 97 |
input files. The first source file name is used, |
| 98 |
except the extension is swapped for '.sci'."] |
| 99 |
[subinits "s|sub-initializers=s{NAME,NAME,...}"] |
| 100 |
[target-config "target-config=s{FILE}" |
| 101 |
? "Give the target parameter configuration, if it |
| 102 |
is different from the compiling gosh. The file |
| 103 |
must contain a single keyword-value list, |
| 104 |
suitable for TARGET-PARAMS argument for |
| 105 |
cgen-precompile and compile. Currently the |
| 106 |
following keyword is recognized: |
| 107 |
|
| 108 |
:env-header-size Size of environment frame |
| 109 |
header in words. |
| 110 |
|
| 111 |
:cont-frame-size Size of continuation |
| 112 |
frame in words."] |
| 113 |
[else (opt . _) (usage #"Unrecognized option: ~opt")] |
| 114 |
. args) |
| 115 |
(let ([mtk (split-to-symbols keep-private-macro)] |
| 116 |
[subinits (split-to-symbols subinits)] |
| 117 |
[extini (or ext-module ext-main)] |
| 118 |
[prefix (or xprefix-all xprefix)] |
| 119 |
[omit-line-directives |
| 120 |
(sys-getenv "GAUCHE_PRECOMP_OMIT_LINE_DIRECTIVES")] |
| 121 |
[tparams (if target-config |
| 122 |
(load-target-config target-config) |
| 123 |
'())]) |
| 124 |
(match args |
| 125 |
[() (usage #f)] |
| 126 |
[(src) |
| 127 |
(when single-sci |
| 128 |
(usage "The `--single-interface' option is only valid with multiple input files")) |
| 129 |
(cgen-precompile src |
| 130 |
:out.c out.c |
| 131 |
:out.sci (or out.sci ext-module) |
| 132 |
:load-paths includes |
| 133 |
:strip-prefix prefix |
| 134 |
:ext-initializer extini |
| 135 |
:sub-initializers subinits |
| 136 |
:dso-name dso-name |
| 137 |
:omit-line-directives omit-line-directives |
| 138 |
:omit-debug-source-info omit-debug-source-info |
| 139 |
:predef-syms predef-syms |
| 140 |
:target-parameters tparams |
| 141 |
:macros-to-keep mtk)] |
| 142 |
[(srcs ...) |
| 143 |
(when out.sci |
| 144 |
(usage "The `-i' or `--interface' option is only valid with single input file")) |
| 145 |
(cgen-precompile-multi srcs |
| 146 |
:ext-initializer extini |
| 147 |
:strip-prefix prefix |
| 148 |
:single-sci-file single-sci |
| 149 |
:dso-name dso-name |
| 150 |
:load-paths includes |
| 151 |
:omit-line-directives omit-line-directives |
| 152 |
:omit-debug-source-info omit-debug-source-info |
| 153 |
:predef-syms predef-syms |
| 154 |
:target-parameters tparams |
| 155 |
:macros-to-keep mtk)]))) |
| 156 |
0) |
| 157 |
|
| 158 |
(define (usage msg) |
| 159 |
(when msg (print msg)) |
| 160 |
(print "Usage: gosh tools/precomp [options] <file.scm> ...") |
| 161 |
(print "Options:") |
| 162 |
(print (option-parser-help-string)) |
| 163 |
(exit 0)) |
| 164 |
|
| 165 |
(define (split-to-symbols arg) |
| 166 |
(if arg |
| 167 |
($ map string->symbol $ string-split arg #\,) |
| 168 |
'())) |
| 169 |
|
| 170 |
|
| 171 |
(define (load-target-config file) |
| 172 |
(guard (e [(<read-error> e) |
| 173 |
(error "Invalid target-parameter file ~s: ~a" file (~ e'message))] |
| 174 |
[else |
| 175 |
(error "Can't read target-parameter file ~s" file)]) |
| 176 |
(with-input-from-file file read))) |
| 177 |
|
| 178 |
;; Local variables: |
| 179 |
;; mode: scheme |
| 180 |
;; end: |