Skip to content

Commit 4f11f38

Browse files
anchaono1wudi
authored andcommitted
interpreters/wamr: add compile role of WebAssembly
Add compilation rules to support WebAssembly(WASM/WAMR): 1. Compile Toolchain 1> Download WASI sdk and export the WASI_SDK_PATH path: $ wget https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-19/wasi-sdk-19.0-linux.tar.gz $ tar xf wasi-sdk-19.0-linux.tar.gz Put wasi-sdk-19.0 to your host WASI_SDK_PATH environment variable, like: $ export WASI_SDK_PATH=`pwd`/wasi-sdk-19.0 2> Download Wamr "wamrc" AOT compiler and export to the PATH: $ mkdir wamrc $ wget https://github.com/bytecodealliance/wasm-micro-runtime/releases/download/WAMR-1.1.2/wamrc-1.1.2-x86_64-ubuntu-20.04.tar.gz $ tar xf wamrc-1.1.2-x86_64-ubuntu-20.04.tar.gz $ export PATH=$PATH:$PWD 2. Configuring and running 1> Configuring sim/wamr and compile: nuttx$ ./tools/configure.sh sim/wamr nuttx$ make ... Wamrc Generate AoT: /home/archer/code/nuttx/n5/apps/wasm/hello.aot Wamrc Generate AoT: /home/archer/code/nuttx/n5/apps/wasm/coremark.aot LD: nuttx 2> Copy the generated wasm file(Interpreter/AoT) nuttx$ cp ../apps/wasm/hello.aot . nuttx$ cp ../apps/wasm/hello.wasm . nuttx$ cp ../apps/wasm/coremark.wasm . 3> Run iwasm nuttx$ ./nuttx NuttShell (NSH) NuttX-10.4.0 nsh> iwasm /data/hello.wasm Hello, World!! nsh> iwasm /data/hello.aot Hello, World!! nsh> iwasm /data/coremark.wasm 2K performance run parameters for coremark. CoreMark Size : 666 Total ticks : 12000 Total time (secs): 12.000000 Iterations/Sec : 5.000000 Iterations : 60 Compiler version : Clang 15.0.7 Compiler flags : Using NuttX compilation options Memory location : Defined by the NuttX configuration seedcrc : 0xe9f5 [0]crclist : 0xe714 [0]crcmatrix : 0x1fd7 [0]crcstate : 0x8e3a [0]crcfinal : 0xa14c Correct operation validated. See README.md for run and reporting rules. CoreMark 1.0 : 5.000000 / Clang 15.0.7 Using NuttX compilation options / Defined by the NuttX configuration Co-Authored-By: Huang Qi <huangqi3@xiaomi.com> Signed-off-by: chao an <anchao@xiaomi.com>
1 parent 12d3107 commit 4f11f38

File tree

8 files changed

+266
-1
lines changed

8 files changed

+266
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
*.lib
1212
*.lst
1313
*.o
14+
*.wo
1415
*.obj
1516
*.rel
1617
*.src
@@ -28,6 +29,7 @@
2829
/romfs.img
2930
/staging
3031
/symtab_apps.c
32+
/wasm
3133
cscope.out
3234
Make.dep
3335
.context

Application.mk

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,3 +262,9 @@ distclean:: clean
262262
$(call DELFILE, .depend)
263263

264264
-include Make.dep
265+
266+
ifeq ($(WASM_BUILD),y)
267+
ifneq ($(CONFIG_INTERPRETERS_WAMR)$(CONFIG_INTERPRETERS_WAMR3),)
268+
include $(APPDIR)$(DELIM)interpreters$(DELIM)Wasm.mk
269+
endif # CONFIG_INTERPRETERS_WAMR || CONFIG_INTERPRETERS_WAMR3
270+
endif # WASM_BUILD

Make.defs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ BINDIR ?= $(APPDIR)$(DELIM)bin
6565

6666
# The final build target
6767

68-
BIN ?= $(APPDIR)$(DELIM)libapps$(LIBEXT)
68+
BIN ?= $(APPDIR)$(DELIM)libapps$(LIBEXT)
69+
WBIN ?= $(APPDIR)$(DELIM)wasm$(DELIM)libwasm$(LIBEXT)
6970

7071
# Tools
7172

@@ -105,3 +106,19 @@ CFLAGS += ${INCDIR_PREFIX}"$(APPDIR)$(DELIM)include"
105106
CXXFLAGS += ${INCDIR_PREFIX}"$(APPDIR)$(DELIM)include"
106107

107108
NUTTXLIB ?= $(call CONVERT_PATH,$(TOPDIR)$(DELIM)staging)
109+
110+
# Function to get entry index
111+
112+
define GETINDEX
113+
$(eval i=1)
114+
$(eval entry=)
115+
$(foreach e,$(2), \
116+
$(if $(filter $(notdir $(1)),$(notdir $(e))), \
117+
$(eval entry=$(1)), \
118+
$(if $(entry), \
119+
,$(eval i=$(shell expr $(i) + 1)) \
120+
) \
121+
) \
122+
)
123+
$(i)
124+
endef

Makefile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
export APPDIR = $(CURDIR)
2222
include $(APPDIR)/Make.defs
2323

24+
ifeq ($(CONFIG_INTERPRETERS_WAMR),y)
25+
include $(APPDIR)$(DELIM)interpreters$(DELIM)wamr$(DELIM)Toolchain.defs
26+
endif
27+
2428
# The GNU make CURDIR will always be a POSIX-like path with forward slashes
2529
# as path segment separators. This is fine for the above inclusions but
2630
# will cause problems later for the native build. If we know that this is
@@ -101,6 +105,9 @@ $(BIN): $(foreach SDIR, $(CONFIGURED_APPS), $(SDIR)_all)
101105
$(Q) for app in ${CONFIGURED_APPS}; do \
102106
$(MAKE) -C "$${app}" archive ; \
103107
done
108+
ifeq ($(CONFIG_INTERPRETERS_WAMR),y)
109+
$(call LINK_WAMR)
110+
endif
104111
endif
105112

106113
else
@@ -118,6 +125,9 @@ $(SYMTABOBJ): %$(OBJEXT): %.c
118125

119126
$(BIN): $(SYMTABOBJ)
120127
$(call ARCHIVE_ADD, $(call CONVERT_PATH,$(BIN)), $^)
128+
ifeq ($(CONFIG_INTERPRETERS_WAMR),y)
129+
$(call LINK_WAMR)
130+
endif
121131

122132
endif # !CONFIG_BUILD_LOADABLE
123133

@@ -211,4 +221,5 @@ distclean: $(foreach SDIR, $(CLEANDIRS), $(SDIR)_distclean)
211221
$(call DELFILE, $(BIN))
212222
$(call DELFILE, Kconfig)
213223
$(call DELDIR, $(BINDIR))
224+
$(call DELDIR, wasm)
214225
$(call CLEAN)

benchmarks/coremark/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,8 @@ STACKSIZE += $(CONFIG_COREMARK_STACKSIZE)
105105

106106
MAINSRC += core_main.c
107107

108+
# Build with WebAssembly when CONFIG_INTERPRETERS_WAMR is enabled
109+
110+
WASM_BUILD = y
111+
108112
include $(APPDIR)/Application.mk

examples/hello/Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,12 @@ MODULE = $(CONFIG_EXAMPLES_HELLO)
3131

3232
MAINSRC = hello_main.c
3333

34+
# Build with WebAssembly when CONFIG_INTERPRETERS_WAMR is enabled
35+
36+
WASM_BUILD = y
37+
38+
# Mode of WebAssembly Micro Runtime
39+
40+
WAMR_MODE = AOT
41+
3442
include $(APPDIR)/Application.mk

interpreters/Wasm.mk

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
############################################################################
2+
# apps/interpreters/Wasm.mk
3+
#
4+
# Licensed to the Apache Software Foundation (ASF) under one or more
5+
# contributor license agreements. See the NOTICE file distributed with
6+
# this work for additional information regarding copyright ownership. The
7+
# ASF licenses this file to you under the Apache License, Version 2.0 (the
8+
# "License"); you may not use this file except in compliance with the
9+
# License. You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16+
# License for the specific language governing permissions and limitations
17+
# under the License.
18+
#
19+
############################################################################
20+
21+
ifeq ($(WASM_BUILD),y)
22+
ifneq ($(CONFIG_INTERPRETERS_WAMR)$(CONFIG_INTERPRETERS_WAMR3),)
23+
24+
WASM_INITIAL_MEMORY ?= 65536
25+
STACKSIZE ?= $(CONFIG_DEFAULT_TASK_STACKSIZE)
26+
PRIORITY ?= SCHED_PRIORITY_DEFAULT
27+
28+
# Wamr mode:
29+
# INT: Interpreter (Default)
30+
# AOT: Ahead-of-Time
31+
# XIP: Execution In Place
32+
# JIT: Just In Time
33+
34+
WAMR_MODE ?= INT
35+
36+
# WebAssembly Micro Runtime Toolchain Setting
37+
38+
-include $(APPDIR)$(DELIM)interpreters$(DELIM)wamr$(DELIM)Toolchain.defs
39+
40+
# Targets follow
41+
42+
.PRECIOUS: $(WBIN)
43+
44+
WSRCS := $(MAINSRC) $(CSRCS)
45+
WOBJS := $(WSRCS:%.c=%.wo)
46+
47+
all:: $(WBIN)
48+
49+
$(WOBJS): %.wo : %.c
50+
$(Q) $(WCC) $(WCFLAGS) -c $^ -o $@
51+
52+
$(WBIN): $(WOBJS)
53+
$(shell mkdir -p $(APPDIR)/wasm)
54+
$(Q) $(WAR) $@ $(filter-out $(MAINSRC:%.c=%.wo),$^)
55+
$(foreach main,$(MAINSRC), \
56+
$(eval mainindex=$(strip $(call GETINDEX,$(main),$(MAINSRC)))) \
57+
$(eval dstname=$(shell echo $(main:%.c=%.wo) | sed -e 's/\//_/g')) \
58+
$(shell cp -rf $(strip $(main:%.c=%.wo)) \
59+
$(strip $(APPDIR)/wasm/$(word $(mainindex),$(PROGNAME))#$(WASM_INITIAL_MEMORY)#$(STACKSIZE)#$(PRIORITY)#$(WAMR_MODE)#$(dstname)) \
60+
) \
61+
)
62+
63+
clean::
64+
$(call DELFILE, $(WOBJS))
65+
$(call DELFILE, $(WBIN))
66+
67+
endif # CONFIG_INTERPRETERS_WAMR || CONFIG_INTERPRETERS_WAMR3
68+
endif # WASM_BUILD

interpreters/wamr/Toolchain.defs

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
############################################################################
2+
# apps/interpreters/wamr/Toolchain.defs
3+
#
4+
# Licensed to the Apache Software Foundation (ASF) under one or more
5+
# contributor license agreements. See the NOTICE file distributed with
6+
# this work for additional information regarding copyright ownership. The
7+
# ASF licenses this file to you under the Apache License, Version 2.0 (the
8+
# "License"); you may not use this file except in compliance with the
9+
# License. You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16+
# License for the specific language governing permissions and limitations
17+
# under the License.
18+
#
19+
############################################################################
20+
21+
# Wasm32 toolchain setting
22+
# Target: wasm32-unknown-wasi
23+
24+
WCC ?= $(WASI_SDK_PATH)/bin/clang
25+
WAR ?= $(WASI_SDK_PATH)/bin/llvm-ar rcs
26+
WRC ?= wamrc
27+
WSYSROOT ?= $(TOPDIR)
28+
29+
CFLAGS_STRIP = -fsanitize=kernel-address -fsanitize=address -fsanitize=undefined
30+
CFLAGS_STRIP += $(ARCHCPUFLAGS) $(ARCHCFLAGS) $(ARCHINCLUDES) $(ARCHDEFINES) $(ARCHOPTIMIZATION) $(EXTRAFLAGS)
31+
32+
WCFLAGS += $(filter-out $(CFLAGS_STRIP),$(CFLAGS))
33+
WCFLAGS += --sysroot=$(WSYSROOT) -nostdlib -D__NuttX__
34+
35+
WLDFLAGS = -z stack-size=$(STACKSIZE) -Wl,--initial-memory=$(INITIAL_MEMORY)
36+
WLDFLAGS += -Wl,--export=main -Wl,--export=__main_argc_argv
37+
WLDFLAGS += -Wl,--export=__heap_base -Wl,--export=__data_end
38+
WLDFLAGS += -Wl,--no-entry -Wl,--strip-all -Wl,--allow-undefined
39+
40+
ifeq ($(CONFIG_ARCH_ARM),y)
41+
ifeq ($(CONFIG_ARM_THUMB),y)
42+
WTARGET = thumb
43+
else
44+
WTARGET = arm
45+
endif
46+
47+
ifeq ($(CONFIG_ARCH_CORTEXM85),y)
48+
WTARGET := $(WTARGET)v8.1m.main
49+
WCPU = --cpu=cortex-m85
50+
else ifeq ($(CONFIG_ARCH_CORTEXM55),y)
51+
WTARGET := $(WTARGET)v8.1m.main
52+
WCPU = --cpu=cortex-m55
53+
else ifeq ($(CONFIG_ARCH_CORTEXM35P),y)
54+
WTARGET := $(WTARGET)v8m.main
55+
WCPU = --cpu=cortex-m35p
56+
else ifeq ($(CONFIG_ARCH_CORTEXM33),y)
57+
WTARGET := $(WTARGET)v8m.main
58+
WCPU = --cpu=cortex-m33
59+
else ifeq ($(CONFIG_ARCH_CORTEXM23),y)
60+
WTARGET := $(WTARGET)v8m.main
61+
WCPU = --cpu=cortex-m23
62+
else ifeq ($(CONFIG_ARCH_CORTEXM7),y)
63+
WTARGET := $(WTARGET)v7em
64+
WCPU = --cpu=cortex-m7
65+
else ifeq ($(CONFIG_ARCH_CORTEXM4),y)
66+
WTARGET := $(WTARGET)v7em
67+
WCPU = --cpu=cortex-m4
68+
else ifeq ($(CONFIG_ARCH_CORTEXM3),y)
69+
WTARGET := $(WTARGET)v7em
70+
WCPU = --cpu=cortex-m3
71+
else ifeq ($(CONFIG_ARCH_CORTEXM0),y)
72+
WTARGET := $(WTARGET)v6m
73+
WCPU = --cpu=cortex-m0
74+
endif
75+
76+
ifeq ($(CONFIG_ARCH_CORTEXA5),y)
77+
WTARGET := $(WTARGET)armv7
78+
WCPU = --cpu=cortex-a5
79+
else ifeq ($(CONFIG_ARCH_CORTEXA7),y)
80+
WTARGET := $(WTARGET)armv7
81+
WCPU = --cpu=cortex-a7
82+
else ifeq ($(CONFIG_ARCH_CORTEXA8),y)
83+
WTARGET := $(WTARGET)armv8a
84+
WCPU = --cpu=cortex-a8
85+
else ifeq ($(CONFIG_ARCH_CORTEXA9),y)
86+
WTARGET := $(WTARGET)armv7
87+
WCPU = --cpu=cortex-a9
88+
endif
89+
90+
ifeq ($(CONFIG_ARCH_CORTEXR4),y)
91+
WTARGET := $(WTARGET)armv7
92+
WCPU = --cpu=cortex-r4
93+
else ifeq ($(CONFIG_ARCH_CORTEXR5),y)
94+
WTARGET := $(WTARGET)armv7
95+
WCPU = --cpu=cortex-r5
96+
else ifeq ($(CONFIG_ARCH_CORTEXR7),y)
97+
WTARGET := $(WTARGET)armv7
98+
WCPU = --cpu=cortex-r7
99+
endif
100+
101+
else ifeq ($(CONFIG_ARCH_RISCV),y)
102+
ifeq ($(CONFIG_ARCH_RV64),y)
103+
WTARGET = riscv64
104+
else
105+
WTARGET = riscv32
106+
endif
107+
else ifeq ($(CONFIG_ARCH_XTENSA),y)
108+
WTARGET = xtensa
109+
else ifeq ($(CONFIG_ARCH_X86_64),y)
110+
WTARGET = x86_64
111+
else ifeq ($(CONFIG_ARCH_X86),y)
112+
WTARGET = i386
113+
else ifeq ($(CONFIG_ARCH_MIPS),y)
114+
WTARGET = mips
115+
else ifeq ($(CONFIG_ARCH_SIM),y)
116+
RCFLAGS += --disable-simd
117+
ifeq ($(CONFIG_SIM_M32),y)
118+
WTARGET = i386
119+
else
120+
WTARGET = x86_64
121+
endif
122+
endif
123+
124+
RCFLAGS += --target=$(WTARGET) $(WCPU)
125+
126+
define LINK_WAMR
127+
$(if $(wildcard $(APPDIR)$(DELIM)wasm$(DELIM)*.wo), \
128+
$(foreach bin,$(wildcard $(APPDIR)$(DELIM)wasm$(DELIM)*.wo), \
129+
$(eval INITIAL_MEMORY=$(shell echo $(notdir $(bin)) | cut -d'#' -f2)) \
130+
$(eval STACKSIZE=$(shell echo $(notdir $(bin)) | cut -d'#' -f3)) \
131+
$(eval PROGNAME=$(shell echo $(notdir $(bin)) | cut -d'#' -f1)) \
132+
$(eval WAMRMODE=$(shell echo $(notdir $(bin)) | cut -d'#' -f5)) \
133+
$(shell $(WCC) $(bin) $(WBIN) $(WCFLAGS) $(WLDFLAGS) -o $(APPDIR)$(DELIM)wasm$(DELIM)$(PROGNAME).wasm) \
134+
$(if $(CONFIG_INTERPRETERS_WAMR_AOT), \
135+
$(if $(filter AOT,$(WAMRMODE)), \
136+
$(info Wamrc Generate AoT: $(APPDIR)$(DELIM)wasm$(DELIM)$(PROGNAME).aot) \
137+
$(shell $(WRC) $(RCFLAGS) -o $(APPDIR)$(DELIM)wasm$(DELIM)$(PROGNAME).aot \
138+
$(APPDIR)$(DELIM)wasm$(DELIM)$(PROGNAME).wasm > /dev/null), \
139+
$(if $(filter XIP,$(WAMRMODE)), \
140+
$(info Wamrc Generate XiP: $(APPDIR)$(DELIM)wasm$(DELIM)$(PROGNAME).xip) \
141+
$(shell $(WRC) $(RCFLAGS) --enable-indirect-mode --disable-llvm-intrinsics \
142+
-o $(APPDIR)$(DELIM)wasm$(DELIM)$(PROGNAME).xip \
143+
$(APPDIR)$(DELIM)wasm$(DELIM)$(PROGNAME).wasm > /dev/null) \
144+
) \
145+
) \
146+
) \
147+
) \
148+
)
149+
endef

0 commit comments

Comments
 (0)