CMakeLists.txt 6.0 KB

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