CMakeLists.txt 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. project (iwasm)
  5. set (CMAKE_VERBOSE_MAKEFILE OFF)
  6. if (NOT DEFINED WAMR_BUILD_PLATFORM)
  7. string (TOLOWER ${CMAKE_HOST_SYSTEM_NAME} WAMR_BUILD_PLATFORM)
  8. endif ()
  9. # Reset default linker flags
  10. set (CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
  11. set (CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
  12. set (CMAKE_C_STANDARD 99)
  13. # Set WAMR_BUILD_TARGET, currently values supported:
  14. # "X86_64", "AMD_64", "X86_32", "AARCH64[sub]", "ARM[sub]", "THUMB[sub]",
  15. # "MIPS", "XTENSA", "RISCV64[sub]", "RISCV32[sub]"
  16. if (NOT DEFINED WAMR_BUILD_TARGET)
  17. if (CMAKE_SYSTEM_PROCESSOR MATCHES "^(arm64|aarch64)")
  18. set (WAMR_BUILD_TARGET "AARCH64")
  19. elseif (CMAKE_SYSTEM_PROCESSOR STREQUAL "riscv64")
  20. set (WAMR_BUILD_TARGET "RISCV64")
  21. elseif (CMAKE_SIZEOF_VOID_P EQUAL 8)
  22. # Build as X86_64 by default in 64-bit platform
  23. set (WAMR_BUILD_TARGET "X86_64")
  24. elseif (CMAKE_SIZEOF_VOID_P EQUAL 4)
  25. # Build as X86_32 by default in 32-bit platform
  26. set (WAMR_BUILD_TARGET "X86_32")
  27. else ()
  28. message(SEND_ERROR "Unsupported build target platform!")
  29. endif ()
  30. endif ()
  31. if (NOT CMAKE_BUILD_TYPE)
  32. set(CMAKE_BUILD_TYPE Release)
  33. endif ()
  34. if (NOT DEFINED WAMR_BUILD_INTERP)
  35. # Enable Interpreter by default
  36. set (WAMR_BUILD_INTERP 1)
  37. endif ()
  38. if (NOT DEFINED WAMR_BUILD_AOT)
  39. # Enable AOT by default.
  40. set (WAMR_BUILD_AOT 1)
  41. endif ()
  42. if (NOT DEFINED WAMR_BUILD_JIT)
  43. # Disable JIT by default.
  44. set (WAMR_BUILD_JIT 0)
  45. endif ()
  46. if (NOT DEFINED WAMR_BUILD_FAST_JIT)
  47. # Disable Fast JIT by default
  48. set (WAMR_BUILD_FAST_JIT 0)
  49. endif ()
  50. if (NOT DEFINED WAMR_BUILD_LIBC_BUILTIN)
  51. # Enable libc builtin support by default
  52. set (WAMR_BUILD_LIBC_BUILTIN 1)
  53. endif ()
  54. if (NOT DEFINED WAMR_BUILD_LIBC_WASI)
  55. # Enable libc wasi support by default
  56. set (WAMR_BUILD_LIBC_WASI 1)
  57. endif ()
  58. if (NOT DEFINED WAMR_BUILD_FAST_INTERP)
  59. # Enable fast interpreter
  60. set (WAMR_BUILD_FAST_INTERP 1)
  61. endif ()
  62. if (NOT DEFINED WAMR_BUILD_MULTI_MODULE)
  63. # Disable multiple modules by default
  64. set (WAMR_BUILD_MULTI_MODULE 0)
  65. endif ()
  66. if (NOT DEFINED WAMR_BUILD_LIB_PTHREAD)
  67. # Disable pthread library by default
  68. set (WAMR_BUILD_LIB_PTHREAD 0)
  69. endif ()
  70. if (NOT DEFINED WAMR_BUILD_LIB_WASI_THREADS)
  71. # Disable wasi threads library by default
  72. set (WAMR_BUILD_LIB_WASI_THREADS 0)
  73. endif ()
  74. if (NOT DEFINED WAMR_BUILD_MINI_LOADER)
  75. # Disable wasm mini loader by default
  76. set (WAMR_BUILD_MINI_LOADER 0)
  77. endif ()
  78. if (NOT DEFINED WAMR_BUILD_SIMD)
  79. # Enable SIMD by default
  80. set (WAMR_BUILD_SIMD 1)
  81. endif ()
  82. if (NOT DEFINED WAMR_BUILD_REF_TYPES)
  83. # Disable reference types by default
  84. set (WAMR_BUILD_REF_TYPES 0)
  85. endif ()
  86. set (WAMR_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR})
  87. # Set the strip command based on the system (GNU or Clang)
  88. if (CMAKE_STRIP)
  89. set (CMAKE_STRIP_FLAGS "--strip-all")
  90. endif ()
  91. include (${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake)
  92. set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wformat -Wformat-security -Wshadow -Wno-unused-parameter")
  93. # set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wconversion -Wsign-conversion")
  94. set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wformat -Wformat-security -Wno-unused")
  95. if (WAMR_BUILD_TARGET MATCHES "X86_.*" OR WAMR_BUILD_TARGET STREQUAL "AMD_64")
  96. if (NOT (CMAKE_C_COMPILER MATCHES ".*clang.*" OR CMAKE_C_COMPILER_ID MATCHES ".*Clang"))
  97. set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mindirect-branch-register")
  98. endif ()
  99. endif ()
  100. # The following flags are to enhance security, but it may impact performance,
  101. # we disable them by default.
  102. #if (WAMR_BUILD_TARGET MATCHES "X86_.*" OR WAMR_BUILD_TARGET STREQUAL "AMD_64")
  103. # set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ftrapv -D_FORTIFY_SOURCE=2")
  104. #endif ()
  105. #set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fstack-protector-strong --param ssp-buffer-size=4")
  106. #set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wl,-z,noexecstack,-z,relro,-z,now")
  107. include (${SHARED_DIR}/utils/uncommon/shared_uncommon.cmake)
  108. # STATIC LIBRARY
  109. add_library(iwasm_static STATIC ${WAMR_RUNTIME_LIB_SOURCE})
  110. set_target_properties (iwasm_static PROPERTIES OUTPUT_NAME vmlib)
  111. target_include_directories(iwasm_static INTERFACE ${WAMR_ROOT_DIR}/core/iwasm/include)
  112. target_link_libraries (iwasm_static INTERFACE ${LLVM_AVAILABLE_LIBS} ${UV_A_LIBS} -lm -ldl -lpthread)
  113. if (WAMR_BUILD_WASM_CACHE EQUAL 1)
  114. target_link_libraries(iwasm_static INTERFACE boringssl_crypto)
  115. endif ()
  116. install (TARGETS iwasm_static ARCHIVE DESTINATION lib)
  117. # If it's a Release build, strip the static library
  118. if (CMAKE_STRIP AND CMAKE_BUILD_TYPE STREQUAL "Release")
  119. # Strip static library
  120. message (STATUS "Stripping static library after build!")
  121. add_custom_command (TARGET iwasm_static POST_BUILD
  122. COMMAND ${CMAKE_STRIP} ${CMAKE_STRIP_FLAGS} $<TARGET_FILE:iwasm_static>
  123. )
  124. endif ()
  125. # SHARED LIBRARY
  126. add_library (iwasm_shared SHARED ${WAMR_RUNTIME_LIB_SOURCE})
  127. set_target_properties (iwasm_shared PROPERTIES OUTPUT_NAME iwasm)
  128. target_include_directories(iwasm_shared INTERFACE ${WAMR_ROOT_DIR}/core/iwasm/include)
  129. target_link_libraries (iwasm_shared INTERFACE ${LLVM_AVAILABLE_LIBS} ${UV_A_LIBS} -lm -ldl -lpthread)
  130. if (WAMR_BUILD_WASM_CACHE EQUAL 1)
  131. target_link_libraries(iwasm_shared INTERFACE boringssl_crypto)
  132. endif ()
  133. if (MINGW)
  134. target_link_libraries (iwasm_shared -lWs2_32)
  135. endif ()
  136. install (TARGETS iwasm_shared LIBRARY DESTINATION lib)
  137. # HEADERS
  138. install (FILES
  139. ${WAMR_ROOT_DIR}/core/iwasm/include/wasm_c_api.h
  140. ${WAMR_ROOT_DIR}/core/iwasm/include/wasm_export.h
  141. ${WAMR_ROOT_DIR}/core/iwasm/include/lib_export.h
  142. DESTINATION include)
  143. # If it's a Release build, strip the shared library
  144. if (CMAKE_STRIP AND CMAKE_BUILD_TYPE STREQUAL "Release")
  145. # Strip shared library
  146. message (STATUS "Stripping shared library after build!")
  147. add_custom_command (TARGET iwasm_shared POST_BUILD
  148. COMMAND ${CMAKE_STRIP} ${CMAKE_STRIP_FLAGS} $<TARGET_FILE:iwasm_shared>
  149. )
  150. endif ()