CMakeLists.txt 6.1 KB

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