2020-07-23 08:54:13 +00:00
|
|
|
# Copyright (C) 2019 Intel Corporation. All rights reserved.
|
|
|
|
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
|
2022-10-18 17:51:18 +00:00
|
|
|
cmake_minimum_required (VERSION 3.14)
|
|
|
|
|
|
|
|
include(CheckPIESupported)
|
2020-09-11 09:36:04 +00:00
|
|
|
|
|
|
|
if (NOT WAMR_BUILD_PLATFORM STREQUAL "windows")
|
|
|
|
project(c-api)
|
|
|
|
else()
|
|
|
|
project (c-api C ASM)
|
|
|
|
endif()
|
2020-07-23 08:54:13 +00:00
|
|
|
|
|
|
|
if(NOT CMAKE_BUILD_TYPE)
|
|
|
|
set(CMAKE_BUILD_TYPE Release)
|
|
|
|
endif()
|
2021-08-06 10:12:21 +00:00
|
|
|
|
2023-06-06 05:45:18 +00:00
|
|
|
set(CMAKE_CXX_STANDARD 17)
|
2020-07-23 08:54:13 +00:00
|
|
|
################ runtime settings ################
|
2021-08-06 10:12:21 +00:00
|
|
|
|
2020-08-21 07:14:04 +00:00
|
|
|
string (TOLOWER ${CMAKE_HOST_SYSTEM_NAME} WAMR_BUILD_PLATFORM)
|
|
|
|
if (APPLE)
|
|
|
|
add_definitions(-DBH_PLATFORM_DARWIN)
|
|
|
|
endif ()
|
2020-07-23 08:54:13 +00:00
|
|
|
|
2023-04-26 01:16:29 +00:00
|
|
|
# Reset default linker flags
|
2020-07-23 08:54:13 +00:00
|
|
|
set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
|
|
|
|
set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
|
|
|
|
|
|
|
|
# WAMR features switch
|
2021-08-06 10:12:21 +00:00
|
|
|
|
|
|
|
# Set WAMR_BUILD_TARGET, currently values supported:
|
|
|
|
# "X86_64", "AMD_64", "X86_32", "AARCH64[sub]", "ARM[sub]", "THUMB[sub]",
|
|
|
|
# "MIPS", "XTENSA", "RISCV64[sub]", "RISCV32[sub]"
|
|
|
|
if (NOT DEFINED WAMR_BUILD_TARGET)
|
2022-01-11 06:08:21 +00:00
|
|
|
if (CMAKE_SYSTEM_PROCESSOR MATCHES "^(arm64|aarch64)")
|
2021-08-06 10:12:21 +00:00
|
|
|
set (WAMR_BUILD_TARGET "AARCH64")
|
2024-02-06 12:47:11 +00:00
|
|
|
if (NOT DEFINED WAMR_BUILD_SIMD)
|
|
|
|
set (WAMR_BUILD_SIMD 1)
|
|
|
|
endif ()
|
2021-08-06 10:12:21 +00:00
|
|
|
elseif (CMAKE_SYSTEM_PROCESSOR STREQUAL "riscv64")
|
|
|
|
set (WAMR_BUILD_TARGET "RISCV64")
|
|
|
|
elseif (CMAKE_SIZEOF_VOID_P EQUAL 8)
|
|
|
|
# Build as X86_64 by default in 64-bit platform
|
|
|
|
set (WAMR_BUILD_TARGET "X86_64")
|
2024-02-06 12:47:11 +00:00
|
|
|
if (NOT DEFINED WAMR_BUILD_SIMD)
|
|
|
|
set (WAMR_BUILD_SIMD 1)
|
|
|
|
endif ()
|
2022-06-27 12:30:31 +00:00
|
|
|
elseif (CMAKE_SIZEOF_VOID_P EQUAL 4)
|
2021-08-06 10:12:21 +00:00
|
|
|
# Build as X86_32 by default in 32-bit platform
|
|
|
|
set (WAMR_BUILD_TARGET "X86_32")
|
2022-06-27 12:30:31 +00:00
|
|
|
else ()
|
|
|
|
message(SEND_ERROR "Unsupported build target platform!")
|
2021-08-06 10:12:21 +00:00
|
|
|
endif ()
|
|
|
|
endif ()
|
2020-07-23 08:54:13 +00:00
|
|
|
|
|
|
|
if(NOT DEFINED WAMR_BUILD_INTERP)
|
|
|
|
set(WAMR_BUILD_INTERP 1)
|
|
|
|
endif()
|
|
|
|
|
|
|
|
if(NOT DEFINED WAMR_BUILD_AOT)
|
2021-07-13 01:01:03 +00:00
|
|
|
set(WAMR_BUILD_AOT 0)
|
2020-07-23 08:54:13 +00:00
|
|
|
endif()
|
|
|
|
|
2021-04-27 09:05:40 +00:00
|
|
|
if(NOT DEFINED WAMR_BUILD_JIT)
|
2020-07-23 08:54:13 +00:00
|
|
|
set(WAMR_BUILD_JIT 0)
|
|
|
|
endif()
|
|
|
|
|
|
|
|
set(WAMR_BUILD_LIBC_BUILTIN 1)
|
|
|
|
set(WAMR_BUILD_LIBC_WASI 0)
|
2020-10-16 09:43:57 +00:00
|
|
|
set(WAMR_BUILD_MULTI_MODULE 1)
|
2021-07-06 09:05:59 +00:00
|
|
|
set(WAMR_BUILD_DUMP_CALL_STACK 1)
|
2021-07-28 13:53:37 +00:00
|
|
|
set(WAMR_BUILD_REF_TYPES 1)
|
2020-07-23 08:54:13 +00:00
|
|
|
|
2024-02-06 12:47:11 +00:00
|
|
|
# If not defined WAMR_BUILD_GC, set it to 0
|
|
|
|
if(NOT DEFINED WAMRC_BUILD_WITH_GC)
|
|
|
|
set(WAMRC_BUILD_WITH_GC 0)
|
|
|
|
endif()
|
|
|
|
|
2020-07-23 08:54:13 +00:00
|
|
|
if(NOT DEFINED WAMR_BUILD_FAST_INTERP)
|
2021-04-27 09:05:40 +00:00
|
|
|
set(WAMR_BUILD_FAST_INTERP 1)
|
2020-07-23 08:54:13 +00:00
|
|
|
endif()
|
|
|
|
|
2020-09-11 09:36:04 +00:00
|
|
|
if (NOT MSVC)
|
|
|
|
# compiling and linking flags
|
2020-08-21 07:14:04 +00:00
|
|
|
if (NOT (CMAKE_C_COMPILER MATCHES ".*clang.*" OR CMAKE_C_COMPILER_ID MATCHES ".*Clang"))
|
2020-09-11 09:36:04 +00:00
|
|
|
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")
|
2020-08-21 07:14:04 +00:00
|
|
|
endif ()
|
2020-09-11 09:36:04 +00:00
|
|
|
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wformat -Wformat-security")
|
|
|
|
if (WAMR_BUILD_TARGET MATCHES "X86_.*" OR WAMR_BUILD_TARGET STREQUAL "AMD_64")
|
|
|
|
if (NOT (CMAKE_C_COMPILER MATCHES ".*clang.*" OR CMAKE_C_COMPILER_ID MATCHES ".*Clang"))
|
|
|
|
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mindirect-branch-register")
|
|
|
|
endif ()
|
|
|
|
endif ()
|
|
|
|
endif()
|
2020-07-23 08:54:13 +00:00
|
|
|
# build out vmlib
|
|
|
|
set(WAMR_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR}/../..)
|
|
|
|
include (${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake)
|
|
|
|
|
|
|
|
add_library(vmlib STATIC ${WAMR_RUNTIME_LIB_SOURCE})
|
2020-09-11 09:36:04 +00:00
|
|
|
if (MSVC)
|
|
|
|
target_compile_definitions(vmlib PRIVATE WASM_API_EXTERN=)
|
|
|
|
endif()
|
2021-04-27 09:05:40 +00:00
|
|
|
target_link_libraries (vmlib ${LLVM_AVAILABLE_LIBS} ${UV_A_LIBS} -lm -ldl -lpthread)
|
2022-12-05 04:25:26 +00:00
|
|
|
|
|
|
|
if (WAMR_BUILD_WASM_CACHE EQUAL 1)
|
2022-12-13 06:23:37 +00:00
|
|
|
target_link_libraries(vmlib boringssl_crypto)
|
2022-12-05 04:25:26 +00:00
|
|
|
endif ()
|
2020-07-23 08:54:13 +00:00
|
|
|
################################################
|
|
|
|
|
|
|
|
################ application related ################
|
2020-10-16 09:43:57 +00:00
|
|
|
## locate wat2wasm
|
|
|
|
find_program(WAT2WASM
|
|
|
|
wat2wasm
|
2021-01-28 08:16:02 +00:00
|
|
|
PATHS /opt/wabt/bin
|
2020-10-16 09:43:57 +00:00
|
|
|
REQUIRED
|
2020-07-23 08:54:13 +00:00
|
|
|
)
|
2020-10-16 09:43:57 +00:00
|
|
|
|
|
|
|
if(NOT WAT2WASM)
|
|
|
|
message(SEND_ERROR "can not find wat2wasm")
|
2022-11-15 05:51:03 +00:00
|
|
|
else ()
|
|
|
|
execute_process(COMMAND ${WAT2WASM} --version
|
|
|
|
OUTPUT_VARIABLE WAT2WASM_VERSION_OUTPUT)
|
|
|
|
string(STRIP ${WAT2WASM_VERSION_OUTPUT} WAT2WASM_VERSION)
|
|
|
|
message("-- Found wat2wasm ${WAT2WASM_VERSION}")
|
2020-09-11 09:36:04 +00:00
|
|
|
endif()
|
2020-07-23 08:54:13 +00:00
|
|
|
|
2022-11-15 05:51:03 +00:00
|
|
|
if (${WAT2WASM_VERSION} VERSION_LESS 1.0.26)
|
|
|
|
set(WAT2WASM_FLAGS "--enable-reference-types")
|
|
|
|
endif ()
|
|
|
|
|
2021-07-13 01:01:03 +00:00
|
|
|
if(${WAMR_BUILD_AOT} EQUAL 1)
|
|
|
|
## locate wamrc
|
|
|
|
find_program(WAMRC
|
|
|
|
wamrc
|
|
|
|
PATHS ${WAMR_ROOT_DIR}/wamr-compiler/build/
|
|
|
|
)
|
|
|
|
|
|
|
|
if(NOT WAMRC)
|
|
|
|
message(SEND_ERROR "can not find wamrc. refer to \
|
|
|
|
https://github.com/bytecodealliance/wasm-micro-runtime#build-wamrc-aot-compiler"
|
|
|
|
)
|
|
|
|
endif()
|
|
|
|
endif()
|
2020-10-16 09:43:57 +00:00
|
|
|
include (${SHARED_DIR}/utils/uncommon/shared_uncommon.cmake)
|
|
|
|
|
|
|
|
set(MM_UTIL src/utils/multi_module_utils.c)
|
|
|
|
# build executable for each .c
|
2023-02-02 01:42:25 +00:00
|
|
|
list(APPEND EXAMPLES callback callback_chain empty_imports global hello hostref memory reflect table trap)
|
|
|
|
# FIXME enable both in the future
|
|
|
|
#list(APPEND EXAMPLES clone threads)
|
|
|
|
# FIXME
|
|
|
|
# if(WAMR_BUILD_JIT EQUAL 1 AND WAMR_BUILD_LAZY_JIT EQUAL 0)
|
|
|
|
# list(APPEND EXAMPLES serialize)
|
|
|
|
# endif()
|
2022-10-18 02:59:28 +00:00
|
|
|
|
2022-10-18 17:51:18 +00:00
|
|
|
check_pie_supported()
|
|
|
|
|
2023-01-03 09:16:49 +00:00
|
|
|
include(CTest)
|
|
|
|
enable_testing()
|
|
|
|
|
2021-04-27 09:05:40 +00:00
|
|
|
foreach(EX ${EXAMPLES})
|
|
|
|
set(SRC ${CMAKE_CURRENT_LIST_DIR}/src/${EX}.c)
|
2020-07-23 08:54:13 +00:00
|
|
|
|
2021-04-27 09:05:40 +00:00
|
|
|
add_executable(${EX} ${SRC} ${UNCOMMON_SHARED_SOURCE} ${MM_UTIL})
|
2022-10-18 17:51:18 +00:00
|
|
|
set_target_properties (${EX} PROPERTIES POSITION_INDEPENDENT_CODE ON)
|
2021-04-27 09:05:40 +00:00
|
|
|
target_include_directories(${EX} PRIVATE ${UNCOMMON_SHARED_DIR})
|
2022-12-13 06:23:37 +00:00
|
|
|
target_link_libraries(${EX} vmlib)
|
2020-09-11 09:36:04 +00:00
|
|
|
if (MSVC)
|
2021-04-27 09:05:40 +00:00
|
|
|
target_compile_definitions(${EX} PRIVATE WASM_API_EXTERN=)
|
2020-09-11 09:36:04 +00:00
|
|
|
endif()
|
2020-10-16 09:43:57 +00:00
|
|
|
|
2021-06-28 03:59:49 +00:00
|
|
|
# wat to wasm
|
|
|
|
set(WAT ${CMAKE_CURRENT_LIST_DIR}/src/${EX}.wat)
|
2021-04-27 09:05:40 +00:00
|
|
|
|
2021-06-28 03:59:49 +00:00
|
|
|
add_custom_target(${EX}_WASM
|
2022-11-15 05:51:03 +00:00
|
|
|
COMMAND ${WAT2WASM} ${WAT} ${WAT2WASM_FLAGS} -o ${PROJECT_BINARY_DIR}/${EX}.wasm
|
2021-06-28 03:59:49 +00:00
|
|
|
DEPENDS ${WAT}
|
2021-04-27 09:05:40 +00:00
|
|
|
BYPRODUCTS ${PROJECT_BINARY_DIR}/${EX}.wasm
|
2020-10-16 09:43:57 +00:00
|
|
|
VERBATIM
|
2020-07-23 08:54:13 +00:00
|
|
|
)
|
2021-06-28 03:59:49 +00:00
|
|
|
add_dependencies(${EX} ${EX}_WASM)
|
2021-05-27 05:22:35 +00:00
|
|
|
|
|
|
|
# generate .aot file
|
|
|
|
if(${WAMR_BUILD_AOT} EQUAL 1)
|
2024-02-06 12:47:11 +00:00
|
|
|
if(${WAMRC_BUILD_WITH_GC} EQUAL 1)
|
|
|
|
set(WAMRC_GC_FLAGS "--enable-gc")
|
|
|
|
else()
|
|
|
|
set(WAMRC_GC_FLAGS "")
|
|
|
|
endif()
|
2021-07-13 01:01:03 +00:00
|
|
|
add_custom_target(${EX}_AOT
|
2024-02-06 12:47:11 +00:00
|
|
|
COMMAND ${WAMRC} ${WAMRC_GC_FLAGS} -o ${PROJECT_BINARY_DIR}/${EX}.aot
|
2021-07-13 01:01:03 +00:00
|
|
|
${PROJECT_BINARY_DIR}/${EX}.wasm
|
|
|
|
DEPENDS ${EX}_WASM
|
|
|
|
BYPRODUCTS ${PROJECT_BINARY_DIR}/${EX}.aot
|
|
|
|
VERBATIM
|
|
|
|
COMMENT "generate a aot file ${PROJECT_BINARY_DIR}/${EX}.aot"
|
|
|
|
)
|
|
|
|
add_dependencies(${EX} ${EX}_AOT)
|
2021-05-27 05:22:35 +00:00
|
|
|
endif()
|
2023-01-03 09:16:49 +00:00
|
|
|
|
|
|
|
# run `ctest --test-dir build`
|
|
|
|
add_test(NAME Test_${EX}
|
|
|
|
COMMAND ./${EX}
|
|
|
|
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
|
|
|
|
)
|
2020-10-16 09:43:57 +00:00
|
|
|
endforeach()
|
2021-04-27 09:05:40 +00:00
|
|
|
|
|
|
|
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
|
|
|
|
find_program(VALGRIND
|
|
|
|
valgrind
|
|
|
|
REQUIRED
|
|
|
|
)
|
|
|
|
|
2023-01-03 09:16:49 +00:00
|
|
|
# run `ctest -T memcheck -V --test-dir build`
|
|
|
|
endif()
|