| 1 |
;; Scan extensions directories and generate staticinit.c |
| 2 |
;; gosh gen-staticinit.scm $(top_srcdir) $(top_builddir) |
| 3 |
|
| 4 |
;; This script may be executed by BUILD_GOSH. Be careful not to depend |
| 5 |
;; on compiled extensions other than listed in preload.scm. |
| 6 |
|
| 7 |
;; NB: At this moment, we assume each extension DSO has single |
| 8 |
;; initfn, with the default name. It is the case in all bundled |
| 9 |
;; extensions now. However, Gauche allows to precompile multiple |
| 10 |
;; modules into one DSO with multiple initfns for each module. |
| 11 |
;; If we use that in modules under ext/*, we need to change this file. |
| 12 |
|
| 13 |
;; NB: We treat gdbm specially, for statically linking it causes |
| 14 |
;; the entire binary to be under GPL. Here's what we do: |
| 15 |
;; - We split initializer function for gdbm libs into separate file |
| 16 |
;; - If the user defines GAUCHE_STATIC_EXCLUDE_GDBM before SCM_INIT_STATIC, |
| 17 |
;; we don't call gdbm initializer function, which causes object files |
| 18 |
;; related to gdbm would be excluded from the final executable. |
| 19 |
;; This is kinda ad-hockery but we expect we won't add more dependencies |
| 20 |
;; to GPL libs in the main distribution. |
| 21 |
|
| 22 |
;; NB: Similar case for rfc.tls.mbed. The license of mbedTLS is |
| 23 |
;; Apache 2.0, so it's not so much of a problem. It's just that if |
| 24 |
;; you don't want the standalone binary to depend on libmbedtls DSO. |
| 25 |
|
| 26 |
(use gauche.generator) |
| 27 |
(use gauche.process) |
| 28 |
(use gauche.config) |
| 29 |
(use gauche.cgen.precomp) |
| 30 |
(use file.util) |
| 31 |
(use util.match) |
| 32 |
(use gauche.cgen) |
| 33 |
(use text.tr) |
| 34 |
(use srfi.13) |
| 35 |
(use srfi.42) |
| 36 |
|
| 37 |
(define top-srcdir (make-parameter "..")) |
| 38 |
(define top-builddir (make-parameter "..")) |
| 39 |
(define abi-version (make-parameter "")) |
| 40 |
|
| 41 |
(define (usage) |
| 42 |
(exit 1 |
| 43 |
"Usage: gen-staticinit.scm $(top_srcdir) $(top_builddir) $(GAUCHE_ABI_VERSION)\n\ |
| 44 |
Create statically linked version of libgauche, with all Scheme libraries\n\ |
| 45 |
and extensions packaged in. After building and installing Gauche as\n\ |
| 46 |
usual, invoke this script via 'make static' in src directory.\n\ |
| 47 |
If you want to exclude some libraries/extensions to keep the binary\n\ |
| 48 |
size down, set the environment variable LIBGAUCHE_STATIC_EXCLUDES\n\ |
| 49 |
with the module name delimited by comma, colon or space, e.g.:\n\ |
| 50 |
\n\ |
| 51 |
LIBGAUCHE_STATIC_EXCLUDES=rfc.tls,os.windows make static\n\ |
| 52 |
\n")) |
| 53 |
|
| 54 |
(define read-line/continuation |
| 55 |
(gbuffer-filter (^[v s] |
| 56 |
(if-let1 m (#/\\$/ v) |
| 57 |
(values '() (cons (m 'before) s)) |
| 58 |
(values `(,(string-concatenate-reverse (cons v s))) '()))) |
| 59 |
'() |
| 60 |
read-line |
| 61 |
(^[s] `(,(string-concatenate-reverse s))))) |
| 62 |
|
| 63 |
;; This returns ("") if the definition is empty. get-scheme-paths counts on it. |
| 64 |
(define (mfvar-ref makefile var :optional default) |
| 65 |
(if-let1 line (with-input-from-file (build-path (top-builddir) makefile) |
| 66 |
(cute generator-find (string->regexp #"^~|var|\\b") |
| 67 |
read-line/continuation)) |
| 68 |
(remove string-null? |
| 69 |
(string-split |
| 70 |
(rxmatch->string (string->regexp #"^~|var|\\s*=\\s*") line 'after) |
| 71 |
#[\s+])) |
| 72 |
(if (undefined? default) |
| 73 |
(errorf "Cannot find ~a definition in ~a" var makefile) |
| 74 |
default))) |
| 75 |
|
| 76 |
;; |
| 77 |
;; Scan ext/* directories to gather extension initfns, and generate |
| 78 |
;; Scm_InitPrelinked() function. |
| 79 |
;; |
| 80 |
|
| 81 |
;; returns a list of shared library file names in ext/SUBDIR |
| 82 |
(define (get-dso-names subdir) |
| 83 |
($ map path-sans-extension |
| 84 |
$ mfvar-ref (build-path "ext" subdir "Makefile") "LIBFILES" '())) |
| 85 |
|
| 86 |
;; Returns ((<dso> <module-name>) ...) |
| 87 |
(define (gather-dsos modules-to-exclude) |
| 88 |
(list-ec [: subdir (mfvar-ref "ext/Makefile" "SUBDIRS")] |
| 89 |
[: dso (get-dso-names subdir)] |
| 90 |
[if (not (string-null? dso))] |
| 91 |
;; NB: This relies on the current DSO naming scheme. |
| 92 |
[:let module-name (string->symbol (regexp-replace-all "--" dso "."))] |
| 93 |
[if (not (and (any (cut eq? module-name <>) modules-to-exclude) |
| 94 |
(print "INFO: Skipping " dso) |
| 95 |
#t))] |
| 96 |
(list dso module-name))) |
| 97 |
|
| 98 |
;; Classify dsos |
| 99 |
;; Returns a hashtable of the following keys: |
| 100 |
;; #t => ((dso module-name) ...) ordinary modules |
| 101 |
;; gdbm => ((dso module-name) ...) gdbm linked modules |
| 102 |
;; mbed => ((dso module-name) ...) mbedTLS linked modules |
| 103 |
;; Caveat: If we use gdbm_compat, dbm.ndbm and dbm.odbm is likely to be |
| 104 |
;; linked with gdbm. |
| 105 |
(define (classify-dsos dsos&mods) |
| 106 |
(define gdbm-linked? |
| 107 |
(if (#/-lgdbm_compat/ (gauche-config "--static-libs")) |
| 108 |
(^m (memq m '(dbm.gdbm dbm.ndbm dbm.odbm))) |
| 109 |
(^m (eq? m 'dbm.gdbm)))) |
| 110 |
(define (mbed-linked? m) (eq? m 'rfc.tls.mbed)) |
| 111 |
(fold (^[dso&mod ht] |
| 112 |
(cond [(gdbm-linked? (cadr dso&mod)) |
| 113 |
(hash-table-push! ht 'gdbm dso&mod) ht] |
| 114 |
[(mbed-linked? (cadr dso&mod)) |
| 115 |
(hash-table-push! ht 'mbed dso&mod) ht] |
| 116 |
[else |
| 117 |
(hash-table-push! ht #t dso&mod) ht])) |
| 118 |
(make-hash-table 'eqv?) dsos&mods)) |
| 119 |
|
| 120 |
(define (generate-staticinit dsos&mods) |
| 121 |
(do-ec [: dso&mod dsos&mods] |
| 122 |
[:let dso (car dso&mod)] |
| 123 |
(let ([initfn (cgen-c-file->initfn dso)] |
| 124 |
[str (cgen-literal dso)]) |
| 125 |
(cgen-decl #"extern void ~initfn(void);") |
| 126 |
(cgen-init |
| 127 |
#" {" |
| 128 |
#" const char *initfn_names[] = { ~(cgen-safe-string (string-append \"_\" initfn)), NULL };" |
| 129 |
#" void (*initfns[])(void) = { ~initfn, NULL };" |
| 130 |
|
| 131 |
#" Scm_RegisterPrelinked(SCM_STRING(~(cgen-cexpr str)), initfn_names, initfns);" |
| 132 |
#" }")))) |
| 133 |
|
| 134 |
;; |
| 135 |
;; Gather *.scm and *.sci files |
| 136 |
;; |
| 137 |
|
| 138 |
;; Returns ((<module-name> <partial-path> <path-to-the-file>) ...) |
| 139 |
;; <module-name> : symbol |
| 140 |
;; <partial-path> : "parser/peg.sci" etc. |
| 141 |
;; <path-to-the-file> : "../ext/peg/peg.sci" etc. |
| 142 |
(define (get-scheme-paths) |
| 143 |
(append-map |
| 144 |
find-scm-source |
| 145 |
(append (map (^p (cons p (build-path "lib" p))) |
| 146 |
(mfvar-ref "lib/Makefile" "SCMFILES")) |
| 147 |
(append-ec [: subdir (mfvar-ref "ext/Makefile" "SUBDIRS")] |
| 148 |
[:let mf (build-path "ext"subdir"Makefile")] |
| 149 |
[:let cat (let1 v (mfvar-ref mf "SCM_CATEGORY") |
| 150 |
(if (null? v) "" (car v)))] |
| 151 |
[:let files (mfvar-ref mf "SCMFILES")] |
| 152 |
(map (^s (cons (build-path cat s) |
| 153 |
(build-path "ext" subdir s))) |
| 154 |
files))))) |
| 155 |
|
| 156 |
;; p : ((<partial-path> . <path-to-look-for>) ...) |
| 157 |
;; expand glob pattern in the basename part |
| 158 |
;; returns ((<module-name> <partial-path> <path-to-the-file>) ...) |
| 159 |
(define (find-scm-source p) |
| 160 |
(map (^[file] (let* ([dir (sys-dirname (car p))] |
| 161 |
[partial-path (string-tr |
| 162 |
(if (equal? dir ".") |
| 163 |
(sys-basename file) |
| 164 |
(build-path dir (sys-basename file))) |
| 165 |
"\\\\" "/")] |
| 166 |
[modname ($ path->module-name |
| 167 |
$ path-sans-extension partial-path)]) |
| 168 |
(list modname partial-path file))) |
| 169 |
(delete-duplicates |
| 170 |
(glob (list (build-path (top-srcdir) (cdr p)) |
| 171 |
(build-path (top-builddir) (cdr p))))))) |
| 172 |
|
| 173 |
;; Classify scheme files |
| 174 |
;; scms : ((module partial-path path) ...) |
| 175 |
;; dsos&mods-map : key => ((dso module) ...) |
| 176 |
;; returns key => ((module partial-path path) ...) |
| 177 |
(define (classify-scms scms dsos&mods-map) |
| 178 |
(define mod-key ; module => key |
| 179 |
(hash-table-fold dsos&mods-map |
| 180 |
(^[k vs m] |
| 181 |
(dolist [v vs] |
| 182 |
(hash-table-put! m (cadr v) k)) |
| 183 |
m) |
| 184 |
(make-hash-table 'eq?))) |
| 185 |
(define (scm-key scm-entry) ; scm-entry : (module partial-path path) |
| 186 |
(hash-table-get mod-key (car scm-entry) #t)) |
| 187 |
(fold (^[scm-entry m] |
| 188 |
(hash-table-push! m (scm-key scm-entry) scm-entry) |
| 189 |
m) |
| 190 |
(make-hash-table 'eqv?) |
| 191 |
scms)) |
| 192 |
|
| 193 |
(define (get-scm-content path) |
| 194 |
;; NB: We can just do (file->string p), but the code below eliminates |
| 195 |
;; comments and unnecessary spaces. Using read/write would break if |
| 196 |
;; the source code contains weird read-time constructor, though. We |
| 197 |
;; know Gauche sources don't have one, but should be careful if we ever |
| 198 |
;; extend this functionality to cover other libraries. |
| 199 |
;; NB: unwrap-syntax is to strip identifier info inserted by reader macros, |
| 200 |
;; if any. |
| 201 |
(with-output-to-string |
| 202 |
(cute for-each ($ write $ unwrap-syntax $) (file->sexp-list path)))) |
| 203 |
|
| 204 |
(define (embed-scm name scmfiles) |
| 205 |
(cgen-decl "static ScmHashTable *scmtab;") |
| 206 |
(cgen-init "scmtab = SCM_HASH_TABLE(Scm_MakeHashTableSimple(SCM_HASH_STRING, 0));") |
| 207 |
;; Set up load hook |
| 208 |
(do-ec [: scmfile scmfiles] |
| 209 |
[:let name (cgen-literal (cadr scmfile))] |
| 210 |
[:let lit (cgen-literal (get-scm-content (caddr scmfile)))] |
| 211 |
(cgen-init (format "Scm_HashTableSet(scmtab, ~a, ~a, 0);" |
| 212 |
(cgen-cexpr name) |
| 213 |
(cgen-cexpr lit)))) |
| 214 |
(cgen-init "static ScmObj add_embedded_code_loader = SCM_UNDEFINED;" |
| 215 |
"SCM_BIND_PROC(add_embedded_code_loader," |
| 216 |
" \"add-embedded-code-loader!\"," |
| 217 |
" Scm_GaucheModule());" |
| 218 |
"Scm_Apply(add_embedded_code_loader, SCM_LIST1(SCM_OBJ(scmtab))," |
| 219 |
" NULL);") |
| 220 |
) |
| 221 |
|
| 222 |
;; MinGW specific stuff |
| 223 |
;; The extension modules are compiled to be linked against libgauche.dll, |
| 224 |
;; so they refer to use import stubs (e.g. the call to Scm_Cons is actually |
| 225 |
;; compiled to an indirect call via _imp_Scm_Cons). We don't want to recompile |
| 226 |
;; extension modules again, so we fake those import entries. |
| 227 |
(define (generate-imp-stub libgauche.dll) |
| 228 |
(define exptab (make-hash-table 'equal?)) |
| 229 |
(dolist [entry (process-output->string-list `("nm" ,libgauche.dll))] |
| 230 |
(rxmatch-case entry |
| 231 |
;; NB: Symbols in 32bit dll have a leading underscore. |
| 232 |
[#/[TD] (_?Scm\w+)/ (_ sym) (set! (~ exptab sym) #t)])) |
| 233 |
($ hash-table-for-each exptab |
| 234 |
(^[k v] |
| 235 |
(if (string-prefix? "_" k) |
| 236 |
(cgen-decl #"void *_imp_~|k|;") |
| 237 |
(cgen-decl #"void *__imp_~|k|;")))) |
| 238 |
(cgen-body "static void unexported_procedure() {" |
| 239 |
" fprintf(stderr, \"[Gauche Internal Error] Unexported procedure is called. It is likely that the code is calling obsoleted C API.\");" |
| 240 |
" exit(1);" |
| 241 |
"}") |
| 242 |
(cgen-body "static void *get_proc_addr(HMODULE m, const char *name) {" |
| 243 |
" void *p = (void*)GetProcAddress(m, name);" |
| 244 |
" if (p == NULL) p = unexported_procedure;" |
| 245 |
" return p;" |
| 246 |
"}") |
| 247 |
(cgen-body "static void populate_imp_pointers() {" |
| 248 |
" HMODULE m = GetModuleHandle(NULL);" |
| 249 |
" if (m == NULL) {" |
| 250 |
" printf(\"Couldn't get module handle. Aborting.\");" |
| 251 |
" exit(1);" |
| 252 |
" }") |
| 253 |
($ hash-table-for-each exptab |
| 254 |
(^[k v] |
| 255 |
(if (string-prefix? "_" k) |
| 256 |
(cgen-body #" _imp_~|k| = get_proc_addr(m, \"~(string-drop k 1)\");") |
| 257 |
(cgen-body #" __imp_~|k| = get_proc_addr(m, \"~|k|\");")))) |
| 258 |
(cgen-body "}") |
| 259 |
(cgen-init " populate_imp_pointers();")) |
| 260 |
|
| 261 |
(define (generate-c name initfn main? scmfiles dsos&mods) |
| 262 |
(cgen-current-unit (make <cgen-unit> |
| 263 |
:name name |
| 264 |
:init-prologue #"void ~initfn() {\n" |
| 265 |
:init-epilogue "}\n")) |
| 266 |
(cgen-decl "#include <gauche.h>") |
| 267 |
(cgen-decl "extern void Scm_RegisterPrelinked(ScmString*, const char *ns[], void (*fns[])(void));") |
| 268 |
(when main? |
| 269 |
(cond-expand |
| 270 |
[gauche.os.windows (generate-imp-stub #"libgauche-~(abi-version).dll")] |
| 271 |
[else]) |
| 272 |
(cgen-init "Scm_AddLoadPath(\"@\", FALSE);")) |
| 273 |
(embed-scm name scmfiles) |
| 274 |
(generate-staticinit dsos&mods) |
| 275 |
(cgen-emit-c (cgen-current-unit))) |
| 276 |
|
| 277 |
(define (do-everything) |
| 278 |
(let* ([modules-to-exclude |
| 279 |
(if-let1 x (sys-getenv "LIBGAUCHE_STATIC_EXCLUDES") |
| 280 |
(map string->symbol (string-split x #[\s:,])) |
| 281 |
'())] |
| 282 |
;; dso-map : key => ((dso mod-name) ...) |
| 283 |
[dso-map (classify-dsos (gather-dsos modules-to-exclude))] |
| 284 |
;; scm-map : key => ((module partialpath path) ...) |
| 285 |
[scm-map (classify-scms (get-scheme-paths) dso-map)]) |
| 286 |
(generate-c "staticinit" "Scm_InitPrelinked" #t |
| 287 |
(hash-table-get scm-map #t) |
| 288 |
(hash-table-get dso-map #t)) |
| 289 |
(generate-c "staticinit_gdbm" "Scm_InitPrelinked_gdbm" #f |
| 290 |
(hash-table-get scm-map 'gdbm '()) |
| 291 |
(hash-table-get dso-map 'gdbm '())) |
| 292 |
(generate-c "staticinit_mbed" "Scm_InitPrelinked_mbed" #f |
| 293 |
(hash-table-get scm-map 'mbed '()) |
| 294 |
(hash-table-get dso-map 'mbed '())) |
| 295 |
)) |
| 296 |
|
| 297 |
(define (main args) |
| 298 |
(match (cdr args) |
| 299 |
[(%top-srcdir %top-builddir %abi-version) |
| 300 |
(parameterize ([top-srcdir %top-srcdir] |
| 301 |
[top-builddir %top-builddir] |
| 302 |
[abi-version %abi-version]) |
| 303 |
(do-everything))] |
| 304 |
[_ (usage)]) |
| 305 |
0) |