############################################################################
# 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
# If compiling for a 64-bit platform (X64 or ARM64 is set),
# set the target to wasm64. This ensures that the generated WebAssembly
# module uses the 64-bit WebAssembly target, which is necessary for
# compatibility with 64-bit architectures.
ifeq ($(CONFIG_ARCH_64BIT),y)
WCFLAGS += --target=wasm64
WOPTFLAGS += --enable-memory64
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
# Filter out some flags that wasm-clang does not support,
# -m%: Machine flags, -mcpu=, -mfpu=, -mfloat-abi= etc.
# -Wl,%: Extra linker flags, wasm-ld don't support many of them.
# -fsanitize%: -fsanitize=address, -fsanitize=thread etc.
# -fno-sanitize%: -fno-sanitize=address, -fno-sanitize=thread etc.
# -W%: Warning flags, clang is more strict than gcc
# --param%: Warning flags, wasm-clang does not support
# -fprofile-arcs -ftest-coverage -fprofile-instr-generate -fcoverage-mapping etc
# asan-%: AddressSanitizer flags, e.g. asan-stack, asan-globals, asan-instrumentation-with-call-threshold, etc. (wasm-clang does not implement)
CFLAGS_STRIP = -m% -Wl,% -fsanitize% -fno-sanitize% -W% --param%
CFLAGS_STRIP += -fprofile% -f%coverage% asan-%
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=$(WASM_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