Skip to content

Commit b9bf0aa

Browse files
aykevldeadprogram
authored andcommitted
windows: add support for the Boehm-Demers-Weiser GC
A few small changes were needed to make this work. In particular, I found a critical bug (see the previous commit) that needed to be fixed to make this work on Windows.
1 parent fd3976b commit b9bf0aa

File tree

5 files changed

+23
-5
lines changed

5 files changed

+23
-5
lines changed

GNUmakefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,6 +1007,7 @@ endif
10071007
@cp -rp lib/mingw-w64/mingw-w64-crt/stdio/ucrt_* build/release/tinygo/lib/mingw-w64/mingw-w64-crt/stdio
10081008
@cp -rp lib/mingw-w64/mingw-w64-headers/crt/ build/release/tinygo/lib/mingw-w64/mingw-w64-headers
10091009
@cp -rp lib/mingw-w64/mingw-w64-headers/defaults/include build/release/tinygo/lib/mingw-w64/mingw-w64-headers/defaults
1010+
@cp -rp lib/mingw-w64/mingw-w64-headers/include build/release/tinygo/lib/mingw-w64/mingw-w64-headers
10101011
@cp -rp lib/nrfx/* build/release/tinygo/lib/nrfx
10111012
@cp -rp lib/picolibc/newlib/libc/ctype build/release/tinygo/lib/picolibc/newlib/libc
10121013
@cp -rp lib/picolibc/newlib/libc/include build/release/tinygo/lib/picolibc/newlib/libc

builder/bdwgc.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package builder
55

66
import (
77
"path/filepath"
8+
"strings"
89

910
"github.com/tinygo-org/tinygo/goenv"
1011
)
@@ -25,6 +26,10 @@ var BoehmGC = Library{
2526
"-DIGNORE_DYNAMIC_LOADING", // we don't support dynamic loading at the moment
2627
"-DNO_GETCONTEXT", // musl doesn't support getcontext()
2728

29+
// Use a minimal environment.
30+
"-DNO_MSGBOX_ON_ERROR", // don't call MessageBoxA on Windows
31+
"-DDONT_USE_ATEXIT",
32+
2833
// Special flag to work around the lack of __data_start in ld.lld.
2934
// TODO: try to fix this in LLVM/lld directly so we don't have to
3035
// work around it anymore.
@@ -47,7 +52,7 @@ var BoehmGC = Library{
4752
return filepath.Join(goenv.Get("TINYGOROOT"), "lib/bdwgc")
4853
},
4954
librarySources: func(target string) ([]string, error) {
50-
return []string{
55+
sources := []string{
5156
"allchblk.c",
5257
"alloc.c",
5358
"blacklst.c",
@@ -66,6 +71,15 @@ var BoehmGC = Library{
6671
"pthread_stop_world.c",
6772
"pthread_support.c",
6873
"reclaim.c",
69-
}, nil
74+
}
75+
if strings.Split(target, "-")[2] == "windows" {
76+
// Due to how the linker on Windows works (that doesn't allow
77+
// undefined functions), we need to include these extra files.
78+
sources = append(sources,
79+
"mallocx.c",
80+
"ptr_chck.c",
81+
)
82+
}
83+
return sources, nil
7084
},
7185
}

builder/build.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,12 +182,13 @@ func Build(pkgName, outpath, tmpdir string, config *compileopts.Config) (BuildRe
182182
defer unlock()
183183
libcDependencies = append(libcDependencies, libcJob)
184184
case "mingw-w64":
185-
job, unlock, err := libMinGW.load(config, tmpdir, nil)
185+
var unlock func()
186+
libcJob, unlock, err = libMinGW.load(config, tmpdir, nil)
186187
if err != nil {
187188
return BuildResult{}, err
188189
}
189190
defer unlock()
190-
libcDependencies = append(libcDependencies, job)
191+
libcDependencies = append(libcDependencies, libcJob)
191192
libcDependencies = append(libcDependencies, makeMinGWExtraLibs(tmpdir, config.GOARCH())...)
192193
case "":
193194
// no library specified, so nothing to do

compileopts/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,8 +395,10 @@ func (c *Config) LibcCFlags() []string {
395395
"-nostdlibinc",
396396
"-isystem", filepath.Join(path, "include"),
397397
"-isystem", filepath.Join(root, "lib", "mingw-w64", "mingw-w64-headers", "crt"),
398+
"-isystem", filepath.Join(root, "lib", "mingw-w64", "mingw-w64-headers", "include"),
398399
"-isystem", filepath.Join(root, "lib", "mingw-w64", "mingw-w64-headers", "defaults", "include"),
399400
"-D_UCRT",
401+
"-D_WIN32_WINNT=0x0a00", // target Windows 10
400402
}
401403
case "":
402404
// No libc specified, nothing to add.

compileopts/target.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ func defaultTarget(options *Options) (*TargetSpec, error) {
427427
"src/runtime/runtime_unix.c",
428428
"src/runtime/signal.c")
429429
case "windows":
430-
spec.GC = "precise"
430+
spec.GC = "boehm"
431431
spec.Linker = "ld.lld"
432432
spec.Libc = "mingw-w64"
433433
// Note: using a medium code model, low image base and no ASLR

0 commit comments

Comments
 (0)