CMakeLists.txt 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. # Copyright (C) 2019 Intel Corporation. All rights reserved.
  2. # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  3. cmake_minimum_required (VERSION 3.14)
  4. include(CheckPIESupported)
  5. if (NOT WAMR_BUILD_PLATFORM STREQUAL "windows")
  6. project(c-api)
  7. else()
  8. project (c-api C ASM)
  9. enable_language (ASM_MASM)
  10. endif()
  11. if(NOT CMAKE_BUILD_TYPE)
  12. set(CMAKE_BUILD_TYPE Release)
  13. endif()
  14. set(CMAKE_CXX_STANDARD 14)
  15. ################ runtime settings ################
  16. string (TOLOWER ${CMAKE_HOST_SYSTEM_NAME} WAMR_BUILD_PLATFORM)
  17. if (APPLE)
  18. add_definitions(-DBH_PLATFORM_DARWIN)
  19. endif ()
  20. # Reset default linker flags
  21. set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
  22. set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
  23. # WAMR features switch
  24. # Set WAMR_BUILD_TARGET, currently values supported:
  25. # "X86_64", "AMD_64", "X86_32", "AARCH64[sub]", "ARM[sub]", "THUMB[sub]",
  26. # "MIPS", "XTENSA", "RISCV64[sub]", "RISCV32[sub]"
  27. if (NOT DEFINED WAMR_BUILD_TARGET)
  28. if (CMAKE_SYSTEM_PROCESSOR MATCHES "^(arm64|aarch64)")
  29. set (WAMR_BUILD_TARGET "AARCH64")
  30. elseif (CMAKE_SYSTEM_PROCESSOR STREQUAL "riscv64")
  31. set (WAMR_BUILD_TARGET "RISCV64")
  32. elseif (CMAKE_SIZEOF_VOID_P EQUAL 8)
  33. # Build as X86_64 by default in 64-bit platform
  34. set (WAMR_BUILD_TARGET "X86_64")
  35. elseif (CMAKE_SIZEOF_VOID_P EQUAL 4)
  36. # Build as X86_32 by default in 32-bit platform
  37. set (WAMR_BUILD_TARGET "X86_32")
  38. else ()
  39. message(SEND_ERROR "Unsupported build target platform!")
  40. endif ()
  41. endif ()
  42. if(NOT DEFINED WAMR_BUILD_INTERP)
  43. set(WAMR_BUILD_INTERP 1)
  44. endif()
  45. if(NOT DEFINED WAMR_BUILD_AOT)
  46. set(WAMR_BUILD_AOT 0)
  47. endif()
  48. if(NOT DEFINED WAMR_BUILD_JIT)
  49. set(WAMR_BUILD_JIT 0)
  50. endif()
  51. set(WAMR_BUILD_LIBC_BUILTIN 1)
  52. set(WAMR_BUILD_LIBC_WASI 0)
  53. set(WAMR_BUILD_MULTI_MODULE 1)
  54. set(WAMR_BUILD_DUMP_CALL_STACK 1)
  55. set(WAMR_BUILD_REF_TYPES 1)
  56. if(NOT DEFINED WAMR_BUILD_FAST_INTERP)
  57. set(WAMR_BUILD_FAST_INTERP 1)
  58. endif()
  59. if (NOT MSVC)
  60. # compiling and linking flags
  61. if (NOT (CMAKE_C_COMPILER MATCHES ".*clang.*" OR CMAKE_C_COMPILER_ID MATCHES ".*Clang"))
  62. set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")
  63. endif ()
  64. set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wformat -Wformat-security")
  65. if (WAMR_BUILD_TARGET MATCHES "X86_.*" OR WAMR_BUILD_TARGET STREQUAL "AMD_64")
  66. if (NOT (CMAKE_C_COMPILER MATCHES ".*clang.*" OR CMAKE_C_COMPILER_ID MATCHES ".*Clang"))
  67. set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mindirect-branch-register")
  68. endif ()
  69. endif ()
  70. endif()
  71. # build out vmlib
  72. set(WAMR_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR}/../..)
  73. include (${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake)
  74. if (NOT DEFINED SANITIZER)
  75. set(SANITIZER "")
  76. elseif (SANITIZER STREQUAL "ubsan")
  77. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -O2 -fno-omit-frame-pointer -fsanitize=undefined -fno-sanitize-recover=all -fno-sanitize=alignment" )
  78. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=undefined")
  79. elseif (NOT (SANITIZER STREQUAL "") )
  80. message(SEND_ERROR "Unsupported sanitizer: ${SANITIZER}")
  81. endif()
  82. add_library(vmlib STATIC ${WAMR_RUNTIME_LIB_SOURCE})
  83. if (MSVC)
  84. target_compile_definitions(vmlib PRIVATE WASM_API_EXTERN=)
  85. endif()
  86. target_link_libraries (vmlib ${LLVM_AVAILABLE_LIBS} ${UV_A_LIBS} -lm -ldl -lpthread)
  87. if (WAMR_BUILD_WASM_CACHE EQUAL 1)
  88. target_link_libraries(vmlib boringssl_crypto)
  89. endif ()
  90. ################################################
  91. ################ application related ################
  92. ## locate wat2wasm
  93. find_program(WAT2WASM
  94. wat2wasm
  95. PATHS /opt/wabt/bin
  96. REQUIRED
  97. )
  98. if(NOT WAT2WASM)
  99. message(SEND_ERROR "can not find wat2wasm")
  100. else ()
  101. execute_process(COMMAND ${WAT2WASM} --version
  102. OUTPUT_VARIABLE WAT2WASM_VERSION_OUTPUT)
  103. string(STRIP ${WAT2WASM_VERSION_OUTPUT} WAT2WASM_VERSION)
  104. message("-- Found wat2wasm ${WAT2WASM_VERSION}")
  105. endif()
  106. if (${WAT2WASM_VERSION} VERSION_LESS 1.0.26)
  107. set(WAT2WASM_FLAGS "--enable-reference-types")
  108. endif ()
  109. if(${WAMR_BUILD_AOT} EQUAL 1)
  110. ## locate wamrc
  111. find_program(WAMRC
  112. wamrc
  113. PATHS ${WAMR_ROOT_DIR}/wamr-compiler/build/
  114. )
  115. if(NOT WAMRC)
  116. message(SEND_ERROR "can not find wamrc. refer to \
  117. https://github.com/bytecodealliance/wasm-micro-runtime#build-wamrc-aot-compiler"
  118. )
  119. endif()
  120. endif()
  121. include (${SHARED_DIR}/utils/uncommon/shared_uncommon.cmake)
  122. set(MM_UTIL src/utils/multi_module_utils.c)
  123. # build executable for each .c
  124. list(APPEND EXAMPLES callback callback_chain empty_imports global hello hostref memory reflect table trap)
  125. # FIXME enable both in the future
  126. #list(APPEND EXAMPLES clone threads)
  127. # FIXME
  128. # if(WAMR_BUILD_JIT EQUAL 1 AND WAMR_BUILD_LAZY_JIT EQUAL 0)
  129. # list(APPEND EXAMPLES serialize)
  130. # endif()
  131. check_pie_supported()
  132. include(CTest)
  133. enable_testing()
  134. foreach(EX ${EXAMPLES})
  135. set(SRC ${CMAKE_CURRENT_LIST_DIR}/src/${EX}.c)
  136. add_executable(${EX} ${SRC} ${UNCOMMON_SHARED_SOURCE} ${MM_UTIL})
  137. set_target_properties (${EX} PROPERTIES POSITION_INDEPENDENT_CODE ON)
  138. target_include_directories(${EX} PRIVATE ${UNCOMMON_SHARED_DIR})
  139. target_link_libraries(${EX} vmlib)
  140. if (MSVC)
  141. target_compile_definitions(${EX} PRIVATE WASM_API_EXTERN=)
  142. endif()
  143. # wat to wasm
  144. set(WAT ${CMAKE_CURRENT_LIST_DIR}/src/${EX}.wat)
  145. add_custom_target(${EX}_WASM
  146. COMMAND ${WAT2WASM} ${WAT} ${WAT2WASM_FLAGS} -o ${PROJECT_BINARY_DIR}/${EX}.wasm
  147. DEPENDS ${WAT}
  148. BYPRODUCTS ${PROJECT_BINARY_DIR}/${EX}.wasm
  149. VERBATIM
  150. )
  151. add_dependencies(${EX} ${EX}_WASM)
  152. # generate .aot file
  153. if(${WAMR_BUILD_AOT} EQUAL 1)
  154. add_custom_target(${EX}_AOT
  155. COMMAND ${WAMRC} -o ${PROJECT_BINARY_DIR}/${EX}.aot
  156. ${PROJECT_BINARY_DIR}/${EX}.wasm
  157. DEPENDS ${EX}_WASM
  158. BYPRODUCTS ${PROJECT_BINARY_DIR}/${EX}.aot
  159. VERBATIM
  160. COMMENT "generate a aot file ${PROJECT_BINARY_DIR}/${EX}.aot"
  161. )
  162. add_dependencies(${EX} ${EX}_AOT)
  163. endif()
  164. # run `ctest --test-dir build`
  165. add_test(NAME Test_${EX}
  166. COMMAND ./${EX}
  167. WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
  168. )
  169. endforeach()
  170. if (CMAKE_BUILD_TYPE STREQUAL "Debug")
  171. find_program(VALGRIND
  172. valgrind
  173. REQUIRED
  174. )
  175. # run `ctest -T memcheck -V --test-dir build`
  176. endif()