CMakeLists.txt 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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.0)
  4. if(ESP_PLATFORM)
  5. include (${COMPONENT_DIR}/build-scripts/esp-idf/wamr/CMakeLists.txt)
  6. return()
  7. endif()
  8. project (iwasm LANGUAGES C)
  9. set(CMAKE_CXX_STANDARD 17)
  10. set (CMAKE_VERBOSE_MAKEFILE OFF)
  11. if (NOT DEFINED WAMR_BUILD_PLATFORM)
  12. string (TOLOWER ${CMAKE_HOST_SYSTEM_NAME} WAMR_BUILD_PLATFORM)
  13. endif ()
  14. if (NOT DEFINED WAMR_BUILD_STATIC)
  15. set (WAMR_BUILD_STATIC 1)
  16. endif ()
  17. if (NOT DEFINED WAMR_BUILD_SHARED)
  18. set (WAMR_BUILD_SHARED 1)
  19. endif ()
  20. # Reset default linker flags
  21. set (CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
  22. set (CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
  23. set (CMAKE_C_STANDARD 99)
  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 CMAKE_BUILD_TYPE)
  43. set(CMAKE_BUILD_TYPE Release)
  44. endif ()
  45. if (NOT DEFINED WAMR_BUILD_INTERP)
  46. # Enable Interpreter by default
  47. set (WAMR_BUILD_INTERP 1)
  48. endif ()
  49. if (NOT DEFINED WAMR_BUILD_AOT)
  50. # Enable AOT by default.
  51. set (WAMR_BUILD_AOT 1)
  52. endif ()
  53. if (NOT DEFINED WAMR_BUILD_JIT)
  54. # Disable JIT by default.
  55. set (WAMR_BUILD_JIT 0)
  56. endif ()
  57. if (NOT DEFINED WAMR_BUILD_FAST_JIT)
  58. # Disable Fast JIT by default
  59. set (WAMR_BUILD_FAST_JIT 0)
  60. endif ()
  61. if (NOT DEFINED WAMR_BUILD_LIBC_BUILTIN)
  62. # Enable libc builtin support by default
  63. set (WAMR_BUILD_LIBC_BUILTIN 1)
  64. endif ()
  65. if (NOT DEFINED WAMR_BUILD_LIBC_WASI)
  66. # Enable libc wasi support by default
  67. set (WAMR_BUILD_LIBC_WASI 1)
  68. endif ()
  69. if (NOT DEFINED WAMR_BUILD_FAST_INTERP)
  70. # Enable fast interpreter
  71. set (WAMR_BUILD_FAST_INTERP 1)
  72. endif ()
  73. if (NOT DEFINED WAMR_BUILD_MULTI_MODULE)
  74. # Disable multiple modules by default
  75. set (WAMR_BUILD_MULTI_MODULE 0)
  76. endif ()
  77. if (NOT DEFINED WAMR_BUILD_LIB_PTHREAD)
  78. # Disable pthread library by default
  79. set (WAMR_BUILD_LIB_PTHREAD 0)
  80. endif ()
  81. if (NOT DEFINED WAMR_BUILD_LIB_WASI_THREADS)
  82. # Disable wasi threads library by default
  83. set (WAMR_BUILD_LIB_WASI_THREADS 0)
  84. endif ()
  85. if (NOT DEFINED WAMR_BUILD_MINI_LOADER)
  86. # Disable wasm mini loader by default
  87. set (WAMR_BUILD_MINI_LOADER 0)
  88. endif ()
  89. if (NOT DEFINED WAMR_BUILD_SIMD)
  90. # Enable SIMD by default
  91. set (WAMR_BUILD_SIMD 1)
  92. endif ()
  93. if (NOT DEFINED WAMR_BUILD_REF_TYPES)
  94. # Enable reference types by default
  95. set (WAMR_BUILD_REF_TYPES 1)
  96. endif ()
  97. set (WAMR_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR})
  98. include (${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake)
  99. if (NOT WIN32)
  100. set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wformat -Wformat-security \
  101. -ffunction-sections -fdata-sections \
  102. -Wno-unused-parameter -Wno-pedantic \
  103. -fvisibility=hidden")
  104. # Remove the extra spaces for better make log
  105. string (REGEX REPLACE " *" " " CMAKE_C_FLAGS ${CMAKE_C_FLAGS})
  106. set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wformat -Wformat-security -Wno-unused")
  107. endif()
  108. if (WAMR_BUILD_TARGET MATCHES "X86_.*" OR WAMR_BUILD_TARGET STREQUAL "AMD_64")
  109. if (NOT (CMAKE_C_COMPILER MATCHES ".*clang.*" OR CMAKE_C_COMPILER_ID MATCHES ".*Clang"))
  110. set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mindirect-branch-register")
  111. endif ()
  112. endif ()
  113. # The following flags are to enhance security, but it may impact performance,
  114. # we disable them by default.
  115. #if (WAMR_BUILD_TARGET MATCHES "X86_.*" OR WAMR_BUILD_TARGET STREQUAL "AMD_64")
  116. # set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ftrapv -D_FORTIFY_SOURCE=2")
  117. #endif ()
  118. #set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fstack-protector-strong --param ssp-buffer-size=4")
  119. #set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wl,-z,noexecstack,-z,relro,-z,now")
  120. include (${SHARED_DIR}/utils/uncommon/shared_uncommon.cmake)
  121. set (THREADS_PREFER_PTHREAD_FLAG ON)
  122. find_package(Threads REQUIRED)
  123. if (MSVC)
  124. add_definitions(-DCOMPILING_WASM_RUNTIME_API=1)
  125. endif ()
  126. # STATIC LIBRARY
  127. if (WAMR_BUILD_STATIC)
  128. add_library(iwasm_static STATIC ${WAMR_RUNTIME_LIB_SOURCE})
  129. set_target_properties (iwasm_static PROPERTIES OUTPUT_NAME vmlib)
  130. target_include_directories(iwasm_static INTERFACE ${WAMR_ROOT_DIR}/core/iwasm/include)
  131. target_link_libraries (iwasm_static INTERFACE ${LLVM_AVAILABLE_LIBS} ${UV_A_LIBS} -lm -ldl ${CMAKE_THREAD_LIBS_INIT})
  132. if (WAMR_BUILD_WASM_CACHE EQUAL 1)
  133. target_link_libraries(iwasm_static INTERFACE boringssl_crypto)
  134. endif ()
  135. if (MINGW)
  136. target_link_libraries (iwasm_static PRIVATE ws2_32)
  137. endif ()
  138. if (WIN32)
  139. target_link_libraries(iwasm_static PRIVATE ntdll)
  140. endif()
  141. install (TARGETS iwasm_static ARCHIVE DESTINATION lib)
  142. endif ()
  143. # SHARED LIBRARY
  144. if (WAMR_BUILD_SHARED)
  145. add_library (iwasm_shared SHARED ${WAMR_RUNTIME_LIB_SOURCE})
  146. set_target_properties (iwasm_shared PROPERTIES OUTPUT_NAME iwasm)
  147. target_include_directories(iwasm_shared INTERFACE ${WAMR_ROOT_DIR}/core/iwasm/include)
  148. target_link_libraries (iwasm_shared PUBLIC ${LLVM_AVAILABLE_LIBS} ${UV_A_LIBS} -lm -ldl ${CMAKE_THREAD_LIBS_INIT})
  149. if (WAMR_BUILD_WASM_CACHE EQUAL 1)
  150. target_link_libraries(iwasm_shared INTERFACE boringssl_crypto)
  151. endif ()
  152. if (MINGW)
  153. target_link_libraries(iwasm_shared INTERFACE -lWs2_32 -lwsock32)
  154. target_link_libraries(iwasm_shared PRIVATE ws2_32)
  155. endif ()
  156. if (WIN32)
  157. target_link_libraries(iwasm_shared PRIVATE ntdll)
  158. endif()
  159. install (TARGETS iwasm_shared LIBRARY DESTINATION lib)
  160. endif ()
  161. # HEADERS
  162. install (FILES
  163. ${WAMR_ROOT_DIR}/core/iwasm/include/wasm_c_api.h
  164. ${WAMR_ROOT_DIR}/core/iwasm/include/wasm_export.h
  165. ${WAMR_ROOT_DIR}/core/iwasm/include/lib_export.h
  166. DESTINATION include)