Skip to content

Commit e4718f5

Browse files
author
Rujia Liu
committed
Preliminary native windows support, part 1: building bach with MSVC (sys-type: visualc)
1 parent 6771e03 commit e4718f5

File tree

2 files changed

+63
-17
lines changed

2 files changed

+63
-17
lines changed

src/build/build-bach.ss

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
(getenv "GERBIL_GSC" default-gerbil-gsc))
1414

1515
(def (gerbil-gcc)
16-
(getenv "GERBIL_GCC" "gcc"))
16+
(getenv "GERBIL_GCC"
17+
(cond-expand
18+
(visualc "cl")
19+
(else "gcc"))))
1720

1821
(def gerbil-bindir
1922
(path-expand "bin" build-home))
@@ -28,9 +31,39 @@
2831
(def default-ld-options ["-lutil" "-lm"]))
2932
(netbsd
3033
(def default-ld-options ["-lm"]))
34+
(visualc
35+
(def default-ld-options ["Kernel32.Lib" "User32.Lib" "Gdi32.Lib" "WS2_32.Lib" "/subsystem:console" "/entry:WinMainCRTStartup"]))
3136
(else
3237
(def default-ld-options ["-ldl" "-lm"])))
3338

39+
(def compiler-obj-suffix
40+
(cond-expand
41+
(visualc ".obj")
42+
(else ".o")))
43+
44+
(def (path->string-literal path)
45+
(string-append
46+
"\""
47+
(string-map
48+
(lambda (c) (if (char=? c #\\) #\/ c))
49+
path)
50+
"\""))
51+
52+
(def (link-output-options output-bin)
53+
(cond-expand
54+
(visualc [(string-append "/Fe" output-bin)])
55+
(else ["-o" output-bin])))
56+
57+
(def (link-with-libgambit-options)
58+
(cond-expand
59+
(visualc ["/link" (string-append "/LIBPATH:" "\"" gerbil-libdir "\"") "libgambit.lib"])
60+
(else ["-L" gerbil-libdir "-lgambit"])))
61+
62+
(def compiler-debug-option
63+
(cond-expand
64+
(visualc "/Zi")
65+
(else "-g")))
66+
3467
(def builtin-modules
3568
'(;; :gerbil/runtime
3669
"gerbil/runtime/gambit"
@@ -109,15 +142,15 @@
109142
;; and then compile the binary
110143
(let* ((builtin-modules-scm (map static-file-name builtin-modules))
111144
(builtin-modules-c (map (cut replace-extension <> ".c") builtin-modules-scm))
112-
(builtin-modules-o (map (cut replace-extension <> ".o") builtin-modules-scm))
145+
(builtin-modules-o (map (cut replace-extension <> compiler-obj-suffix) builtin-modules-scm))
113146
(bach-main-scm (static-file-name bach-main))
114147
(bach-main-c (replace-extension bach-main-scm ".c"))
115-
(bach-main-o (replace-extension bach-main-scm ".o"))
148+
(bach-main-o (replace-extension bach-main-scm compiler-obj-suffix))
116149
(bach-link-c (path-expand "gerbil-link.c" gerbil-libdir))
117-
(bach-link-o (replace-extension bach-link-c ".o"))
150+
(bach-link-o (replace-extension bach-link-c compiler-obj-suffix))
118151
(gambit-sharp (path-expand "_gambit#.scm" gerbil-libdir))
119152
(include-gambit-sharp
120-
(string-append "(include \"" gambit-sharp "\")"))
153+
(string-append "(include " (path->string-literal gambit-sharp) ")"))
121154
(gsc-gx-macros
122155
(if (gerbil-runtime-smp?)
123156
["-e" "(define-cond-expand-feature|enable-smp|)"
@@ -141,18 +174,18 @@
141174
bach-main-scm])
142175
(for-each (lambda (path-c)
143176
(add-compile-job!
144-
(lambda () (invoke (gerbil-gsc) ["-obj" "-cc-options" "-g" path-c]))
177+
(lambda () (invoke (gerbil-gsc) ["-obj" "-cc-options" compiler-debug-option path-c]))
145178
`(compile ,path-c)))
146179
[builtin-modules-c ... bach-main-c bach-link-c])
147180
(execute-pending-compile-jobs!)
148181
(displayln "... build " output-bin)
149182
(invoke (gerbil-gcc)
150-
["-o" output-bin
183+
[(link-output-options output-bin) ...
151184
rpath-options ...
152185
builtin-modules-o ...
153186
bach-main-o
154187
bach-link-o
155-
"-L" gerbil-libdir "-lgambit"
188+
(link-with-libgambit-options) ...
156189
default-ld-options ...])
157190
;; clean up
158191
(delete-file bach-main-scm)

src/gerbil/compiler/driver.ss

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,19 @@ namespace: gxc
5858
(cond-expand (darwin "-Wl,-rpath,") (else "-Wl,-rpath="))
5959
gerbil-libdir))
6060

61+
(def compiler-obj-suffix
62+
(cond-expand
63+
(visualc ".obj")
64+
(else ".o")))
65+
66+
(def (path->string-literal path)
67+
(string-append
68+
"\""
69+
(string-map
70+
(lambda (c) (if (char=? c #\\) #\/ c))
71+
path)
72+
"\""))
73+
6174
(def gerbil-runtime-modules
6275
'("gerbil/runtime/gambit"
6376
"gerbil/runtime/util"
@@ -222,23 +235,23 @@ namespace: gxc
222235
(libgerbil-scm (map find-static-module-file libgerbil-deps))
223236
(libgerbil-scm (fold-libgerbil-runtime-scm gerbil-staticdir libgerbil-scm))
224237
(libgerbil-c (map (cut replace-extension <> ".c") libgerbil-scm))
225-
(libgerbil-o (map (cut replace-extension <> ".o") libgerbil-scm))
238+
(libgerbil-o (map (cut replace-extension <> compiler-obj-suffix) libgerbil-scm))
226239
(src-deps (filter userlib-module? deps))
227240
(src-deps-scm (map find-static-module-file src-deps))
228241
(src-deps-scm (filter not-file-empty? src-deps-scm))
229242
(src-deps-scm (map path-expand src-deps-scm))
230243
(src-deps-c (map (cut replace-extension <> ".c") src-deps-scm))
231-
(src-deps-o (map (cut replace-extension <> ".o") src-deps-scm))
244+
(src-deps-o (map (cut replace-extension <> compiler-obj-suffix) src-deps-scm))
232245
(src-bin-scm (find-static-module-file ctx))
233246
(src-bin-scm (path-expand src-bin-scm))
234247
(src-bin-c (replace-extension src-bin-scm ".c"))
235-
(src-bin-o (replace-extension src-bin-scm ".o"))
248+
(src-bin-o (replace-extension src-bin-scm compiler-obj-suffix))
236249
(output-bin (path-expand output-bin))
237250
(output-scm (path-expand output-scm))
238251
(output-c (replace-extension output-scm ".c"))
239-
(output-o (replace-extension output-scm ".o"))
252+
(output-o (replace-extension output-scm compiler-obj-suffix))
240253
(output_-c (replace-extension output-scm "_.c"))
241-
(output_-o (replace-extension output-scm "_.o"))
254+
(output_-o (replace-extension output-scm (string-append "_" compiler-obj-suffix)))
242255
(gsc-link-opts (gsc-link-options))
243256
(gsc-cc-opts (gsc-cc-options static: #t))
244257
(gsc-static-opts (gsc-static-include-options gerbil-staticdir))
@@ -252,7 +265,7 @@ namespace: gxc
252265
(cons ctx deps))))))
253266

254267
(def (compile-obj scm-path c-path)
255-
(let (o-path (replace-extension c-path ".o"))
268+
(let (o-path (replace-extension c-path compiler-obj-suffix))
256269
(let* ((lock (string-append o-path ".lock"))
257270
(locked #f)
258271
(unlock
@@ -393,17 +406,17 @@ namespace: gxc
393406
(gerbil-libdir (path-expand "lib" gerbil-home))
394407
(runtime (map find-static-module-file gerbil-runtime-modules))
395408
(gambit-sharp (path-expand "lib/_gambit#.scm" gerbil-home))
396-
(include-gambit-sharp (string-append "(include \"" gambit-sharp "\")"))
409+
(include-gambit-sharp (string-append "(include " (path->string-literal gambit-sharp) ")"))
397410
(bin-scm (find-static-module-file ctx))
398411
(deps (find-runtime-module-deps ctx))
399412
(deps (map find-static-module-file deps))
400413
(deps (filter (? (not file-empty?)) deps))
401414
(deps (filter (lambda (f) (not (member f runtime))) deps))
402415
(output-base (string-append (path-strip-extension output-scm)))
403416
(output-c (string-append output-base ".c"))
404-
(output-o (string-append output-base ".o"))
417+
(output-o (string-append output-base compiler-obj-suffix))
405418
(output-c_ (string-append output-base "_.c"))
406-
(output-o_ (string-append output-base "_.o"))
419+
(output-o_ (string-append output-base (string-append "_" compiler-obj-suffix)))
407420
(gsc-link-opts (gsc-link-options))
408421
(gsc-cc-opts (gsc-cc-options static: #t))
409422
(gsc-static-opts (gsc-static-include-options (path-expand "static" gerbil-libdir)))

0 commit comments

Comments
 (0)