Skip to content

Commit c85eb5a

Browse files
committed
wasm: include wasi-libc
This allows CGo code to call some libc functions. Additionally, by putting memset/memmove/memcpy in an archive they're not included anymore when not necessary, reducing code size for small programs.
1 parent 4d5dafd commit c85eb5a

File tree

7 files changed

+67
-33
lines changed

7 files changed

+67
-33
lines changed

.circleci/config.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,18 @@ commands:
5151
key: llvm-source-9-v0
5252
paths:
5353
- llvm-project
54+
build-wasi-libc:
55+
steps:
56+
- restore_cache:
57+
keys:
58+
- wasi-libc-sysroot-v0
59+
- run:
60+
name: "Build wasi-libc"
61+
command: make wasi-libc CLANG=$PWD/llvm-build/bin/clang LLVM_AR=$PWD/llvm-build/bin/llvm-ar LLVM_NM=$PWD/llvm-build/bin/llvm-nm
62+
- save_cache:
63+
key: wasi-libc-sysroot-v0
64+
paths:
65+
- lib/wasi-libc/sysroot
5466
test-linux:
5567
steps:
5668
- checkout
@@ -64,6 +76,14 @@ commands:
6476
- go-cache-v2-{{ checksum "go.mod" }}
6577
- llvm-source-linux
6678
- run: go install .
79+
- restore_cache:
80+
keys:
81+
- wasi-libc-sysroot-systemclang-v0
82+
- run: make wasi-libc
83+
- save_cache:
84+
key: wasi-libc-sysroot-systemclang-v0
85+
paths:
86+
- lib/wasi-libc/sysroot
6787
- run: go test -v ./cgo ./compileopts ./interp ./transform .
6888
- run: make gen-device -j4
6989
- run: make smoketest
@@ -121,6 +141,7 @@ commands:
121141
paths:
122142
llvm-build
123143
- run: make ASSERT=1
144+
- build-wasi-libc
124145
- run:
125146
name: "Test TinyGo"
126147
command: make ASSERT=1 test
@@ -178,6 +199,7 @@ commands:
178199
key: llvm-build-9-linux-v0
179200
paths:
180201
llvm-build
202+
- build-wasi-libc
181203
- run:
182204
name: "Test TinyGo"
183205
command: make test
@@ -244,6 +266,16 @@ commands:
244266
key: llvm-build-9-macos-v0
245267
paths:
246268
llvm-build
269+
- restore_cache:
270+
keys:
271+
- wasi-libc-sysroot-macos-v0
272+
- run:
273+
name: "Build wasi-libc"
274+
command: make wasi-libc CLANG=$PWD/llvm-build/bin/clang LLVM_AR=$PWD/llvm-build/bin/llvm-ar LLVM_NM=$PWD/llvm-build/bin/llvm-nm
275+
- save_cache:
276+
key: wasi-libc-sysroot-macos-v0
277+
paths:
278+
- lib/wasi-libc/sysroot
247279
- run:
248280
name: "Test TinyGo"
249281
command: make test

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,6 @@
1414
path = lib/compiler-rt
1515
url = https://github.com/llvm-mirror/compiler-rt.git
1616
branch = release_80
17+
[submodule "lib/wasi-libc"]
18+
path = lib/wasi-libc
19+
url = https://github.com/CraneStation/wasi-libc

Makefile

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ LLVM_BUILDDIR ?= llvm-build
77
CLANG_SRC ?= llvm-project/clang
88
LLD_SRC ?= llvm-project/lld
99

10+
# Default tool selection.
11+
CLANG ?= clang-9
12+
LLVM_AR ?= llvm-ar-9
13+
LLVM_NM ?= llvm-nm-9
14+
1015
# Go binary and GOROOT to select
1116
GO ?= go
1217
export GOROOT = $(shell $(GO) env GOROOT)
@@ -139,12 +144,19 @@ $(LLVM_BUILDDIR): $(LLVM_BUILDDIR)/build.ninja
139144
cd $(LLVM_BUILDDIR); ninja
140145

141146

147+
# Build wasi-libc sysroot
148+
.PHONY: wasi-libc
149+
wasi-libc: lib/wasi-libc/sysroot/lib/wasm32-wasi/libc.a
150+
lib/wasi-libc/sysroot/lib/wasm32-wasi/libc.a:
151+
cd lib/wasi-libc && make -j4 WASM_CC=$(CLANG) WASM_AR=$(LLVM_AR) WASM_NM=$(LLVM_NM)
152+
153+
142154
# Build the Go compiler.
143155
tinygo:
144156
@if [ ! -f "$(LLVM_BUILDDIR)/bin/llvm-config" ]; then echo "Fetch and build LLVM first by running:"; echo " make llvm-source"; echo " make $(LLVM_BUILDDIR)"; exit 1; fi
145157
CGO_CPPFLAGS="$(CGO_CPPFLAGS)" CGO_CXXFLAGS="$(CGO_CXXFLAGS)" CGO_LDFLAGS="$(CGO_LDFLAGS)" $(GO) build -o build/tinygo$(EXE) -tags byollvm .
146158

147-
test:
159+
test: wasi-libc
148160
CGO_CPPFLAGS="$(CGO_CPPFLAGS)" CGO_CXXFLAGS="$(CGO_CXXFLAGS)" CGO_LDFLAGS="$(CGO_LDFLAGS)" $(GO) test -v -tags byollvm ./cgo ./compileopts ./interp ./transform .
149161

150162
tinygo-test:
@@ -261,12 +273,13 @@ endif
261273
$(TINYGO) build -o wasm.wasm -target=wasm examples/wasm/export
262274
$(TINYGO) build -o wasm.wasm -target=wasm examples/wasm/main
263275

264-
release: tinygo gen-device
276+
release: tinygo gen-device wasi-libc
265277
@mkdir -p build/release/tinygo/bin
266278
@mkdir -p build/release/tinygo/lib/clang/include
267279
@mkdir -p build/release/tinygo/lib/CMSIS/CMSIS
268280
@mkdir -p build/release/tinygo/lib/compiler-rt/lib
269281
@mkdir -p build/release/tinygo/lib/nrfx
282+
@mkdir -p build/release/tinygo/lib/wasi-libc
270283
@mkdir -p build/release/tinygo/pkg/armv6m-none-eabi
271284
@mkdir -p build/release/tinygo/pkg/armv7m-none-eabi
272285
@mkdir -p build/release/tinygo/pkg/armv7em-none-eabi
@@ -279,6 +292,7 @@ release: tinygo gen-device
279292
@cp -rp lib/compiler-rt/LICENSE.TXT build/release/tinygo/lib/compiler-rt
280293
@cp -rp lib/compiler-rt/README.txt build/release/tinygo/lib/compiler-rt
281294
@cp -rp lib/nrfx/* build/release/tinygo/lib/nrfx
295+
@cp -rp lib/wasi-libc/sysroot build/release/tinygo/lib/wasi-libc/sysroot
282296
@cp -rp src build/release/tinygo/src
283297
@cp -rp targets build/release/tinygo/targets
284298
./build/tinygo build-builtins -target=armv6m-none-eabi -o build/release/tinygo/pkg/armv6m-none-eabi/compiler-rt.a

azure-pipelines.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ jobs:
4545
inputs:
4646
targetType: inline
4747
script: choco install qemu
48+
- task: CacheBeta@0
49+
displayName: Cache wasi-libc sysroot
50+
inputs:
51+
key: wasi-libc-sysroot-v0
52+
path: lib/wasi-libc/sysroot
53+
- task: Bash@3
54+
displayName: Build wasi-libc
55+
inputs:
56+
targetType: inline
57+
script: make wasi-libc CLANG=$PWD/llvm-build/bin/clang LLVM_AR=$PWD/llvm-build/bin/llvm-ar LLVM_NM=$PWD/llvm-build/bin/llvm-nm
4858
- task: Bash@3
4959
displayName: Test TinyGo
5060
inputs:

lib/wasi-libc

Submodule wasi-libc added at a280fea

src/runtime/runtime_wasm.go

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22

33
package runtime
44

5-
import (
6-
"unsafe"
7-
)
8-
95
type timeUnit float64 // time in milliseconds, just like Date.now() in JavaScript
106

117
const tickMicros = 1000000
@@ -68,25 +64,3 @@ func ticks() timeUnit
6864
func abort() {
6965
trap()
7066
}
71-
72-
//go:export memset
73-
func memset(ptr unsafe.Pointer, c byte, size uintptr) unsafe.Pointer {
74-
for i := uintptr(0); i < size; i++ {
75-
*(*byte)(unsafe.Pointer(uintptr(ptr) + i)) = c
76-
}
77-
return ptr
78-
}
79-
80-
// Implement memmove for LLVM and compiler-rt.
81-
//go:export memmove
82-
func libc_memmove(dst, src unsafe.Pointer, size uintptr) unsafe.Pointer {
83-
memmove(dst, src, size)
84-
return dst
85-
}
86-
87-
// Implement memcpy for LLVM and compiler-rt.
88-
//go:export memcpy
89-
func libc_memcpy(dst, src unsafe.Pointer, size uintptr) unsafe.Pointer {
90-
memcpy(dst, src, size)
91-
return dst
92-
}

targets/wasm.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
{
2-
"llvm-target": "wasm32-unknown-unknown-wasm",
2+
"llvm-target": "wasm32--wasi",
33
"build-tags": ["js", "wasm"],
44
"goos": "js",
55
"goarch": "wasm",
66
"compiler": "clang",
77
"linker": "wasm-ld",
88
"cflags": [
9-
"--target=wasm32",
10-
"-nostdlibinc",
11-
"-Wno-macro-redefined",
9+
"--target=wasm32--wasi",
10+
"--sysroot={root}/lib/wasi-libc/sysroot",
1211
"-Oz"
1312
],
1413
"ldflags": [
1514
"--allow-undefined",
1615
"--no-threads",
1716
"--stack-first",
18-
"--export-all"
17+
"--export-all",
18+
"{root}/lib/wasi-libc/sysroot/lib/wasm32-wasi/libc.a"
1919
],
2020
"emulator": ["node", "targets/wasm_exec.js"]
2121
}

0 commit comments

Comments
 (0)