forked from sellicott/tcc-riscv32
-
Notifications
You must be signed in to change notification settings - Fork 5
/
build.sh
executable file
·198 lines (174 loc) · 4.4 KB
/
build.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/usr/bin/env bash
# Build and Test TCC for WebAssembly
## TODO: Set PATH
export PATH=/workspaces/bookworm/xpack-riscv-none-elf-gcc-13.2.0-2/bin:$PATH
export PATH=/workspaces/bookworm/zig-linux-x86_64-0.12.0-dev.2341+92211135f:$PATH
set -e # Exit when any command fails
set -x # Echo commands
## Compile TCC from C to WebAssembly
function build_wasm {
## Zig Compiler options for TCC
tcc_options=" \
-c \
-target wasm32-freestanding \
-dynamic \
-rdynamic \
-lc \
-DTCC_TARGET_RISCV64 \
-DCONFIG_TCC_CROSSPREFIX="\"riscv64-\"" \
-DCONFIG_TCC_CRTPREFIX="\"/usr/riscv64-linux-gnu/lib\"" \
-DCONFIG_TCC_LIBPATHS="\"{B}:/usr/riscv64-linux-gnu/lib\"" \
-DCONFIG_TCC_SYSINCLUDEPATHS="\"{B}/include:/usr/riscv64-linux-gnu/include\"" \
-DTCC_GITHASH="\"main:b3d10a35\"" \
-Wall \
-O2 \
-Wdeclaration-after-statement \
-fno-strict-aliasing \
-Wno-pointer-sign \
-Wno-sign-compare \
-Wno-unused-result \
-Wno-format-truncation \
-Wno-stringop-truncation \
-I. \
"
## Zig Compiler options for NuttX ROM FS
## Note: All `static` functions become public.
## So that Zig can call the functions.
nuttx_options=" \
-DCONFIG_FS_ROMFS_CACHE_FILE_NSECTORS=1 \
-DDTYPE_DIRECTORY=DT_DIR \
-DDTYPE_FILE=DT_REG \
-DDTYPE_LINK=DT_LNK \
-DERROR=-1 \
-DFAR= \
-DFIOC_FILEPATH=1 \
-DOK=0 \
-DMTD_BREAD=mtd_bread \
-DMTD_IOCTL=mtd_ioctl \
-DROMFS_MAGIC=0x7275 \
\
-Dkmm_free=free \
-Dkmm_malloc=malloc \
-Dkmm_zalloc=zalloc \
-Dstatic= \
\
-DEPERM=1 \
-DENOENT=2 \
-DEINTR=4 \
-DEIO=5 \
-DENXIO=6 \
-DEAGAIN=11 \
-DENOMEM=12 \
-DEACCES=13 \
-DEBUSY=16 \
-DEEXIST=17 \
-DENODEV=19 \
-DENOTDIR=20 \
-DEISDIR=21 \
-DEINVAL=22 \
-DENOTTY=25 \
-DEPIPE=32 \
-DEDOM=33 \
-DERANGE=34 \
-DENOSYS=38 \
-DELOOP=40 \
-DECONNREFUSED=111 \
-DENOTSUP=138 \
-DEWOULDBLOCK=140 \
"
## Compile fs_romfs.c to WebAssembly
zig cc \
$nuttx_options \
$tcc_options \
zig/fs_romfs.c
## Compile fs_romfsutil.c to WebAssembly
zig cc \
$nuttx_options \
$tcc_options \
zig/fs_romfsutil.c
## Compile zig_romfs.c to WebAssembly
zig cc \
$nuttx_options \
$tcc_options \
zig/zig_romfs.c
## Compile tcc.c to WebAssembly
zig cc \
$tcc_options \
tcc.c
## Dump our Compiled WebAssembly
# wasm-objdump -h tcc.o
# wasm-objdump -x tcc.o >/tmp/tcc.txt
## Compile our Zig App `tcc-wasm.zig` for WebAssembly
## and link with TCC compiled for WebAssembly
zig build-exe \
-target wasm32-freestanding \
-rdynamic \
-lc \
-fno-entry \
-freference-trace \
--verbose-cimport \
--export=compile_program \
-I zig \
zig/tcc-wasm.zig \
fs_romfs.o \
fs_romfsutil.o \
zig_romfs.o \
tcc.o
## Dump our Linked WebAssembly
# wasm-objdump -h tcc-wasm.wasm
# wasm-objdump -x tcc-wasm.wasm >/tmp/tcc-wasm.txt
## Copy the Linked TCC WebAssembly to the Web Server
cp tcc-wasm.wasm docs/
cp tcc-wasm.wasm docs/romfs
## Run our Linked WebAssembly
node zig/test.js
node zig/test.js | grep --text "TODO"
## Translate tcc.c to Zig
# zig translate-c \
# -target wasm32-freestanding \
# -rdynamic \
# -lc \
# tcc.c \
# -DTCC_TARGET_RISCV64 \
# -DCONFIG_TCC_CROSSPREFIX="\"riscv64-\"" \
# -DCONFIG_TCC_CRTPREFIX="\"/usr/riscv64-linux-gnu/lib\"" \
# -DCONFIG_TCC_LIBPATHS="\"{B}:/usr/riscv64-linux-gnu/lib\"" \
# -DCONFIG_TCC_SYSINCLUDEPATHS="\"{B}/include:/usr/riscv64-linux-gnu/include\"" \
# -DTCC_GITHASH="\"main:b3d10a35\"" \
# -I. \
# >/tmp/tcc.zig
}
## Test TCC WebAssembly with NuttX QEMU
function test_wasm {
node zig/test-nuttx.js
pushd ../nuttx
cp /tmp/a.out ../apps/bin/
riscv-none-elf-objdump \
--syms --source --reloc --demangle --line-numbers --wide \
--debugging \
../apps/bin/a.out
qemu-system-riscv64 \
-semihosting \
-M virt,aclint=on \
-cpu rv64 \
-smp 8 \
-bios none \
-kernel nuttx \
-nographic
popd
}
## Go to TCC Folder
pushd ..
## Bundle the romfs folder into ROM FS Filesystem romfs.bin
## and label with this Volume Name
genromfs \
-f zig/romfs.bin \
-d zig/romfs \
-V "ROMFS"
cp zig/romfs.bin docs/romfs
## Compile TCC from C to WebAssembly
build_wasm
## Test TCC WebAssembly with NuttX QEMU
test_wasm
## Return to Zig Folder
popd