Skip to content

Commit 3883550

Browse files
aykevldeadprogram
authored andcommitted
build: fix build-library subcommand
This subcommand has been broken for a while, since libraries also use the CPU flag. This commit fixes this. Previously, libraries were usable for most Cortex-M cores. But with the addition of the CPU field, I've limited it to three popular cores: the Cortex-M0 (microbit), Cortex-M0+ (atsamd21), and Cortex-M4 (atsamd21, nrf52, and many others). In the future we might consider also building libraries for the current OS/arch so that libraries like musl are already precompiled.
1 parent 5ce072b commit 3883550

File tree

3 files changed

+21
-20
lines changed

3 files changed

+21
-20
lines changed

Makefile

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -590,9 +590,9 @@ build/release: tinygo gen-device wasi-libc $(if $(filter 1,$(USE_SYSTEM_BINARYEN
590590
@mkdir -p build/release/tinygo/lib/picolibc/newlib/libc
591591
@mkdir -p build/release/tinygo/lib/picolibc/newlib/libm
592592
@mkdir -p build/release/tinygo/lib/wasi-libc
593-
@mkdir -p build/release/tinygo/pkg/armv6m-unknown-unknown-eabi
594-
@mkdir -p build/release/tinygo/pkg/armv7m-unknown-unknown-eabi
595-
@mkdir -p build/release/tinygo/pkg/armv7em-unknown-unknown-eabi
593+
@mkdir -p build/release/tinygo/pkg/thumbv6m-unknown-unknown-eabi-cortex-m0
594+
@mkdir -p build/release/tinygo/pkg/thumbv6m-unknown-unknown-eabi-cortex-m0plus
595+
@mkdir -p build/release/tinygo/pkg/thumbv7em-unknown-unknown-eabi-cortex-m4
596596
@echo copying source files
597597
@cp -p build/tinygo$(EXE) build/release/tinygo/bin
598598
ifneq ($(USE_SYSTEM_BINARYEN),1)
@@ -641,12 +641,12 @@ endif
641641
@cp -rp lib/wasi-libc/sysroot build/release/tinygo/lib/wasi-libc/sysroot
642642
@cp -rp src build/release/tinygo/src
643643
@cp -rp targets build/release/tinygo/targets
644-
./build/tinygo build-library -target=armv6m-unknown-unknown-eabi -o build/release/tinygo/pkg/armv6m-unknown-unknown-eabi/compiler-rt compiler-rt
645-
./build/tinygo build-library -target=armv7m-unknown-unknown-eabi -o build/release/tinygo/pkg/armv7m-unknown-unknown-eabi/compiler-rt compiler-rt
646-
./build/tinygo build-library -target=armv7em-unknown-unknown-eabi -o build/release/tinygo/pkg/armv7em-unknown-unknown-eabi/compiler-rt compiler-rt
647-
./build/tinygo build-library -target=armv6m-unknown-unknown-eabi -o build/release/tinygo/pkg/armv6m-unknown-unknown-eabi/picolibc picolibc
648-
./build/tinygo build-library -target=armv7m-unknown-unknown-eabi -o build/release/tinygo/pkg/armv7m-unknown-unknown-eabi/picolibc picolibc
649-
./build/tinygo build-library -target=armv7em-unknown-unknown-eabi -o build/release/tinygo/pkg/armv7em-unknown-unknown-eabi/picolibc picolibc
644+
./build/tinygo build-library -target=cortex-m0 -o build/release/tinygo/pkg/thumbv6m-unknown-unknown-eabi-cortex-m0/compiler-rt compiler-rt
645+
./build/tinygo build-library -target=cortex-m0plus -o build/release/tinygo/pkg/thumbv6m-unknown-unknown-eabi-cortex-m0plus/compiler-rt compiler-rt
646+
./build/tinygo build-library -target=cortex-m4 -o build/release/tinygo/pkg/thumbv7em-unknown-unknown-eabi-cortex-m4/compiler-rt compiler-rt
647+
./build/tinygo build-library -target=cortex-m0 -o build/release/tinygo/pkg/thumbv6m-unknown-unknown-eabi-cortex-m0/picolibc picolibc
648+
./build/tinygo build-library -target=cortex-m0plus -o build/release/tinygo/pkg/thumbv6m-unknown-unknown-eabi-cortex-m0plus/picolibc picolibc
649+
./build/tinygo build-library -target=cortex-m4 -o build/release/tinygo/pkg/thumbv7em-unknown-unknown-eabi-cortex-m4/picolibc picolibc
650650

651651
release: build/release
652652
tar -czf build/release.tar.gz -C build/release tinygo

compileopts/config.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,13 @@ func MuslArchitecture(triple string) string {
199199
// a precompiled libc shipped with a TinyGo build, or a libc path in the cache
200200
// directory (which might not yet be built).
201201
func (c *Config) LibcPath(name string) (path string, precompiled bool) {
202+
archname := c.Triple()
203+
if c.CPU() != "" {
204+
archname += "-" + c.CPU()
205+
}
206+
202207
// Try to load a precompiled library.
203-
precompiledDir := filepath.Join(goenv.Get("TINYGOROOT"), "pkg", c.Triple(), name)
208+
precompiledDir := filepath.Join(goenv.Get("TINYGOROOT"), "pkg", archname, name)
204209
if _, err := os.Stat(precompiledDir); err == nil {
205210
// Found a precompiled library for this OS/architecture. Return the path
206211
// directly.
@@ -209,13 +214,7 @@ func (c *Config) LibcPath(name string) (path string, precompiled bool) {
209214

210215
// No precompiled library found. Determine the path name that will be used
211216
// in the build cache.
212-
var outname string
213-
if c.CPU() != "" {
214-
outname = name + "-" + c.Triple() + "-" + c.CPU()
215-
} else {
216-
outname = name + "-" + c.Triple()
217-
}
218-
return filepath.Join(goenv.Get("GOCACHE"), outname), false
217+
return filepath.Join(goenv.Get("GOCACHE"), name+"-"+archname), false
219218
}
220219

221220
// CFlags returns the flags to pass to the C compiler. This is necessary for CGo

main.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,11 +1366,13 @@ func main() {
13661366
handleCompilerError(err)
13671367
}
13681368
defer os.RemoveAll(tmpdir)
1369+
spec, err := compileopts.LoadTarget(options)
1370+
if err != nil {
1371+
handleCompilerError(err)
1372+
}
13691373
config := &compileopts.Config{
13701374
Options: options,
1371-
Target: &compileopts.TargetSpec{
1372-
Triple: *target,
1373-
},
1375+
Target: spec,
13741376
}
13751377
path, err := lib.Load(config, tmpdir)
13761378
handleCompilerError(err)

0 commit comments

Comments
 (0)