From dec2cd9635927e1438ba705e7baf9230d63d4aee Mon Sep 17 00:00:00 2001 From: Huang Qi Date: Sun, 2 Apr 2023 22:04:41 +0800 Subject: [PATCH] tools/Wasm: Decouple the wasm compilation and runtime specific AOT compilation Signed-off-by: Huang Qi --- Application.mk | 10 +-- Makefile | 13 +--- interpreters/Wasm.mk | 68 ---------------- interpreters/wamr/Toolchain.defs | 22 +----- tools/Wasm.mk | 128 +++++++++++++++++++++++++++++++ 5 files changed, 138 insertions(+), 103 deletions(-) delete mode 100644 interpreters/Wasm.mk create mode 100644 tools/Wasm.mk diff --git a/Application.mk b/Application.mk index 611355aaa..eda44d9f3 100644 --- a/Application.mk +++ b/Application.mk @@ -264,8 +264,8 @@ distclean:: clean -include Make.dep -ifeq ($(WASM_BUILD),y) - ifneq ($(CONFIG_INTERPRETERS_WAMR)$(CONFIG_INTERPRETERS_WAMR3),) - include $(APPDIR)$(DELIM)interpreters$(DELIM)Wasm.mk - endif # CONFIG_INTERPRETERS_WAMR || CONFIG_INTERPRETERS_WAMR3 -endif # WASM_BUILD +# Default values for WASM_BUILD from Application.mk + +WASM_BUILD ?= n + +include $(APPDIR)/tools/Wasm.mk diff --git a/Makefile b/Makefile index f54bb4b7d..e502fad35 100644 --- a/Makefile +++ b/Makefile @@ -20,10 +20,7 @@ export APPDIR = $(CURDIR) include $(APPDIR)/Make.defs - -ifeq ($(CONFIG_INTERPRETERS_WAMR),y) - include $(APPDIR)$(DELIM)interpreters$(DELIM)wamr$(DELIM)Toolchain.defs -endif +include $(APPDIR)/tools/Wasm.mk # The GNU make CURDIR will always be a POSIX-like path with forward slashes # as path segment separators. This is fine for the above inclusions but @@ -105,9 +102,7 @@ $(BIN): $(foreach SDIR, $(CONFIGURED_APPS), $(SDIR)_all) $(Q) for app in ${CONFIGURED_APPS}; do \ $(MAKE) -C "$${app}" archive ; \ done -ifeq ($(CONFIG_INTERPRETERS_WAMR),y) - $(call LINK_WAMR) -endif + $(call LINK_WASM) endif else @@ -125,9 +120,7 @@ $(SYMTABOBJ): %$(OBJEXT): %.c $(BIN): $(SYMTABOBJ) $(call ARCHIVE_ADD, $(call CONVERT_PATH,$(BIN)), $^) -ifeq ($(CONFIG_INTERPRETERS_WAMR),y) - $(call LINK_WAMR) -endif + $(call LINK_WASM) endif # !CONFIG_BUILD_LOADABLE diff --git a/interpreters/Wasm.mk b/interpreters/Wasm.mk deleted file mode 100644 index 8e7f5890f..000000000 --- a/interpreters/Wasm.mk +++ /dev/null @@ -1,68 +0,0 @@ -############################################################################ -# apps/interpreters/Wasm.mk -# -# Licensed to the Apache Software Foundation (ASF) under one or more -# contributor license agreements. See the NOTICE file distributed with -# this work for additional information regarding copyright ownership. The -# ASF licenses this file to you under the Apache License, Version 2.0 (the -# "License"); you may not use this file except in compliance with the -# License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. -# -############################################################################ - -ifeq ($(WASM_BUILD),y) - ifneq ($(CONFIG_INTERPRETERS_WAMR)$(CONFIG_INTERPRETERS_WAMR3),) - -WASM_INITIAL_MEMORY ?= 65536 -STACKSIZE ?= $(CONFIG_DEFAULT_TASK_STACKSIZE) -PRIORITY ?= SCHED_PRIORITY_DEFAULT - -# Wamr mode: -# INT: Interpreter (Default) -# AOT: Ahead-of-Time -# XIP: Execution In Place -# JIT: Just In Time - -WAMR_MODE ?= INT - -# WebAssembly Micro Runtime Toolchain Setting - --include $(APPDIR)$(DELIM)interpreters$(DELIM)wamr$(DELIM)Toolchain.defs - -# Targets follow - -.PRECIOUS: $(WBIN) - -WSRCS := $(MAINSRC) $(CSRCS) -WOBJS := $(WSRCS:%.c=%.wo) - -all:: $(WBIN) - -$(WOBJS): %.wo : %.c - $(Q) $(WCC) $(WCFLAGS) -c $^ -o $@ - -$(WBIN): $(WOBJS) - $(shell mkdir -p $(APPDIR)/wasm) - $(Q) $(WAR) $@ $(filter-out $(MAINSRC:%.c=%.wo),$^) - $(foreach main,$(MAINSRC), \ - $(eval mainindex=$(strip $(call GETINDEX,$(main),$(MAINSRC)))) \ - $(eval dstname=$(shell echo $(main:%.c=%.wo) | sed -e 's/\//_/g')) \ - $(shell cp -rf $(strip $(main:%.c=%.wo)) \ - $(strip $(APPDIR)/wasm/$(word $(mainindex),$(PROGNAME))#$(WASM_INITIAL_MEMORY)#$(STACKSIZE)#$(PRIORITY)#$(WAMR_MODE)#$(dstname)) \ - ) \ - ) - -clean:: - $(call DELFILE, $(WOBJS)) - $(call DELFILE, $(WBIN)) - - endif # CONFIG_INTERPRETERS_WAMR || CONFIG_INTERPRETERS_WAMR3 -endif # WASM_BUILD diff --git a/interpreters/wamr/Toolchain.defs b/interpreters/wamr/Toolchain.defs index 6623695bc..c00e1707a 100644 --- a/interpreters/wamr/Toolchain.defs +++ b/interpreters/wamr/Toolchain.defs @@ -18,24 +18,9 @@ # ############################################################################ -# Wasm32 toolchain setting -# Target: wasm32-unknown-wasi +# Wamrc toolchain flags -WCC ?= $(WASI_SDK_PATH)/bin/clang -WAR ?= $(WASI_SDK_PATH)/bin/llvm-ar rcs WRC ?= wamrc -WSYSROOT ?= $(TOPDIR) - -CFLAGS_STRIP = -fsanitize=kernel-address -fsanitize=address -fsanitize=undefined -CFLAGS_STRIP += $(ARCHCPUFLAGS) $(ARCHCFLAGS) $(ARCHINCLUDES) $(ARCHDEFINES) $(ARCHOPTIMIZATION) $(EXTRAFLAGS) - -WCFLAGS += $(filter-out $(CFLAGS_STRIP),$(CFLAGS)) -WCFLAGS += --sysroot=$(WSYSROOT) -nostdlib -D__NuttX__ - -WLDFLAGS = -z stack-size=$(STACKSIZE) -Wl,--initial-memory=$(INITIAL_MEMORY) -WLDFLAGS += -Wl,--export=main -Wl,--export=__main_argc_argv -WLDFLAGS += -Wl,--export=__heap_base -Wl,--export=__data_end -WLDFLAGS += -Wl,--no-entry -Wl,--strip-all -Wl,--allow-undefined ifeq ($(CONFIG_ARCH_XTENSA),y) WTARGET = xtensa @@ -63,14 +48,11 @@ endif RCFLAGS += --target=$(WTARGET) --target-abi=$(LLVM_ABITYPE) --cpu=$(WCPU) -define LINK_WAMR +define WAMR_AOT_COMPILE $(if $(wildcard $(APPDIR)$(DELIM)wasm$(DELIM)*.wo), \ $(foreach bin,$(wildcard $(APPDIR)$(DELIM)wasm$(DELIM)*.wo), \ - $(eval INITIAL_MEMORY=$(shell echo $(notdir $(bin)) | cut -d'#' -f2)) \ - $(eval STACKSIZE=$(shell echo $(notdir $(bin)) | cut -d'#' -f3)) \ $(eval PROGNAME=$(shell echo $(notdir $(bin)) | cut -d'#' -f1)) \ $(eval WAMRMODE=$(shell echo $(notdir $(bin)) | cut -d'#' -f5)) \ - $(shell $(WCC) $(bin) $(WBIN) $(WCFLAGS) $(WLDFLAGS) -o $(APPDIR)$(DELIM)wasm$(DELIM)$(PROGNAME).wasm) \ $(if $(CONFIG_INTERPRETERS_WAMR_AOT), \ $(if $(filter AOT,$(WAMRMODE)), \ $(info Wamrc Generate AoT: $(APPDIR)$(DELIM)wasm$(DELIM)$(PROGNAME).aot) \ diff --git a/tools/Wasm.mk b/tools/Wasm.mk new file mode 100644 index 000000000..4517588ae --- /dev/null +++ b/tools/Wasm.mk @@ -0,0 +1,128 @@ + +############################################################################ +# apps/tools/Wasm.mk +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. The +# ASF licenses this file to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance with the +# License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +############################################################################ + +include $(APPDIR)$(DELIM)interpreters$(DELIM)wamr$(DELIM)Toolchain.defs + +# wasi-sdk toolchain setting + +WCC ?= $(WASI_SDK_PATH)/bin/clang +WAR ?= $(WASI_SDK_PATH)/bin/llvm-ar rcs + +# sysroot for building wasm, default is NuttX + +ifeq ($(WSYSROOT),) + WSYSROOT := $(TOPDIR) + + # Force disable wasm build when WASM_SYSROOT is not defined and on specific + # targets that do not support wasm build. + # Since some architecture level inline assembly instructions can not be + # recognized by wasm-clang. For example: + # Error: /github/workspace/sources/nuttx/include/arch/chip/irq.h:220:27: error: invalid output constraint '=a' in asm + # asm volatile("rdtscp" : "=a" (lo), "=d" (hi)::"memory"); + + ifeq ($(CONFIG_ARCH_INTEL64)$(CONFIG_ARCH_SPARC_V8)$(CONFIG_ARCH_AVR)$(CONFIG_ARCH_XTENSA),y) + WASM_BUILD = n + endif + +endif + +# Only build wasm when WCC is exist + +ifneq ($(wildcard $(WCC)),) + +CFLAGS_STRIP = -fsanitize=kernel-address -fsanitize=address -fsanitize=undefined +CFLAGS_STRIP += $(ARCHCPUFLAGS) $(ARCHCFLAGS) $(ARCHINCLUDES) $(ARCHDEFINES) $(ARCHOPTIMIZATION) $(EXTRAFLAGS) + +WCFLAGS += $(filter-out $(CFLAGS_STRIP),$(CFLAGS)) +WCFLAGS += --sysroot=$(WSYSROOT) -nostdlib -D__NuttX__ + +WLDFLAGS = -z stack-size=$(STACKSIZE) -Wl,--initial-memory=$(INITIAL_MEMORY) +WLDFLAGS += -Wl,--export=main -Wl,--export=__main_argc_argv +WLDFLAGS += -Wl,--export=__heap_base -Wl,--export=__data_end +WLDFLAGS += -Wl,--no-entry -Wl,--strip-all -Wl,--allow-undefined + +# If called from $(APPDIR)/Make.defs, WASM_BUILD is not defined +# Provide LINK_WASM, but only execute it when file wasm/*.wo exists + +ifeq ($(WASM_BUILD),) + + +define LINK_WASM + $(if $(wildcard $(APPDIR)$(DELIM)wasm$(DELIM)*), \ + $(foreach bin,$(wildcard $(APPDIR)$(DELIM)wasm$(DELIM)*.wo), \ + $(eval INITIAL_MEMORY=$(shell echo $(notdir $(bin)) | cut -d'#' -f2)) \ + $(eval STACKSIZE=$(shell echo $(notdir $(bin)) | cut -d'#' -f3)) \ + $(eval PROGNAME=$(shell echo $(notdir $(bin)) | cut -d'#' -f1)) \ + $(shell $(WCC) $(bin) $(WBIN) $(WCFLAGS) $(WLDFLAGS) -o $(APPDIR)$(DELIM)wasm$(DELIM)$(PROGNAME).wasm) \ + $(call WAMR_AOT_COMPILE) \ + ) \ + ) +endef + +endif # WASM_BUILD + +# If called from Application.mk, WASM_BUILD is defined (y or n) + +ifeq ($(WASM_BUILD),y) + +WASM_INITIAL_MEMORY ?= 65536 +STACKSIZE ?= $(CONFIG_DEFAULT_TASK_STACKSIZE) +PRIORITY ?= SCHED_PRIORITY_DEFAULT + +# Wamr mode: +# INT: Interpreter (Default) +# AOT: Ahead-of-Time +# XIP: Execution In Place +# JIT: Just In Time + +WAMR_MODE ?= INT + +# Targets follow + +.PRECIOUS: $(WBIN) + +WSRCS := $(MAINSRC) $(CSRCS) +WOBJS := $(WSRCS:%.c=%.wo) + +all:: $(WBIN) + +$(WOBJS): %.wo : %.c + $(Q) $(WCC) $(WCFLAGS) -c $^ -o $@ + +$(WBIN): $(WOBJS) + $(shell mkdir -p $(APPDIR)/wasm) + $(Q) $(WAR) $@ $(filter-out $(MAINSRC:%.c=%.wo),$^) + $(foreach main,$(MAINSRC), \ + $(eval mainindex=$(strip $(call GETINDEX,$(main),$(MAINSRC)))) \ + $(eval dstname=$(shell echo $(main:%.c=%.wo) | sed -e 's/\//_/g')) \ + $(shell cp -rf $(strip $(main:%.c=%.wo)) \ + $(strip $(APPDIR)/wasm/$(word $(mainindex),$(PROGNAME))#$(WASM_INITIAL_MEMORY)#$(STACKSIZE)#$(PRIORITY)#$(WAMR_MODE)#$(dstname)) \ + ) \ + ) + +clean:: + $(call DELFILE, $(WOBJS)) + $(call DELFILE, $(WBIN)) + + +endif # WASM_BUILD + +endif # WCC