tools: Split origin Wasm.mk into Wasm.mk and WASI-SDK.defs

Try to follow current NuttX's toolchain parttern:
Wasm.mk: Provide target rule for building wasm module
WASI-SDK.defs: Provide compile flags for building

Signed-off-by: Huang Qi <huangqi3@xiaomi.com>
This commit is contained in:
Huang Qi 2023-11-17 10:44:48 +08:00 committed by Xiang Xiao
parent f55b8face6
commit bd159e103c
2 changed files with 93 additions and 60 deletions

86
tools/WASI-SDK.defs Normal file
View File

@ -0,0 +1,86 @@
############################################################################
# apps/tools/WASI-SDK.defs
#
# 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.
#
############################################################################
# wasi-sdk toolchain
WCC ?= $(WASI_SDK_PATH)/bin/clang
WAR ?= $(WASI_SDK_PATH)/bin/llvm-ar rcs
# Sysroot for building wasm module, default is NuttX
WSYSROOT ?= $(TOPDIR)
# Force disable wasm build when WCC is not exist
ifeq ($(wildcard $(WCC)),)
WASM_BUILD = n
else
WASM_BUILD ?= n
endif
# 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
# Build optimization flags from scratch
ifeq ($(CONFIG_DEBUG_FULLOPT),y)
WCFLAGS += -Oz
else ifeq ($(CONFIG_DEBUG_CUSTOMOPT),y)
WCFLAGS += $(CONFIG_DEBUG_OPTLEVEL)
endif
ifneq ($(CONFIG_LTO_FULL)$(CONFIG_LTO_THIN),)
WCFLAGS += -flto
WLDFLAGS += -flto
endif
# Build other compiler flags from native compiler
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__
# If CONFIG_LIBM not defined, then define it to 1
ifeq ($(CONFIG_LIBM),)
WCFLAGS += -DCONFIG_LIBM=1 -I$(APPDIR)$(DELIM)include$(DELIM)wasm
endif
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
WCC_COMPILER_RT_LIB = $(shell $(WCC) --print-libgcc-file-name)
ifeq ($(wildcard $(WCC_COMPILER_RT_LIB)),)
# if "--print-libgcc-file-name" unable to find the correct libgcc PATH
# then go ahead and try "--print-file-name"
WCC_COMPILER_RT_LIB := $(wildcard $(shell $(WCC) --print-file-name $(notdir $(WCC_COMPILER_RT_LIB))))
endif

View File

@ -22,63 +22,13 @@
# Only build wasm if one of the following runtime is enabled
ifneq ($(CONFIG_INTERPRETERS_WAMR)$(CONFIG_INTERPRETERS_WASM)$(CONFIG_INTERPRETERS_TOYWASM),)
include $(APPDIR)$(DELIM)tools$(DELIM)WASI-SDK.defs
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__
# If CONFIG_LIBM not defined, then define it to 1
ifeq ($(CONFIG_LIBM),)
WCFLAGS += -DCONFIG_LIBM=1 -I$(APPDIR)$(DELIM)include$(DELIM)wasm
endif
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
COMPILER_RT_LIB = $(shell $(WCC) --print-libgcc-file-name)
ifeq ($(wildcard $(COMPILER_RT_LIB)),)
# if "--print-libgcc-file-name" unable to find the correct libgcc PATH
# then go ahead and try "--print-file-name"
COMPILER_RT_LIB := $(wildcard $(shell $(WCC) --print-file-name $(notdir $(COMPILER_RT_LIB))))
endif
# If called from $(APPDIR)/Make.defs, WASM_BUILD is not defined
# If called from $(APPDIR)/Makefile,
# Provide LINK_WASM, but only execute it when file wasm/*.wo exists
ifeq ($(WASM_BUILD),)
ifeq ($(CURDIR),$(APPDIR))
define LINK_WASM
$(if $(wildcard $(APPDIR)$(DELIM)wasm$(DELIM)*), \
@ -86,7 +36,7 @@ define LINK_WASM
$(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 RETVAL=$(shell $(WCC) $(bin) $(WBIN) $(WCFLAGS) $(WLDFLAGS) $(COMPILER_RT_LIB) \
$(eval RETVAL=$(shell $(WCC) $(bin) $(WBIN) $(WCFLAGS) $(WLDFLAGS) $(WCC_COMPILER_RT_LIB) \
-Wl,--Map=$(APPDIR)$(DELIM)wasm$(DELIM)$(PROGNAME).map \
-o $(APPDIR)$(DELIM)wasm$(DELIM)$(PROGNAME).wasm || echo 1;)) \
$(if $(RETVAL), \
@ -97,15 +47,13 @@ define LINK_WASM
)
endef
endif # WASM_BUILD
endif # CURDIR
# Default values for WASM_BUILD, it's a three state variable:
# y - build wasm module only
# n - don't build wasm module
# n - don't build wasm module, default
# both - build wasm module and native module
WASM_BUILD ?= n
ifneq ($(WASM_BUILD),n)
WASM_INITIAL_MEMORY ?= 65536
@ -160,5 +108,4 @@ clean::
endif # WASM_BUILD
endif # WCC
endif
endif # CONFIG_INTERPRETERS_WAMR || CONFIG_INTERPRETERS_WASM || CONFIG_INTERPRETERS_TOYWASM