CMakeLists.txt 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. project (iwasm)
  6. option(BUILD_SHARED_LIBS "Build using shared libraries" OFF)
  7. set (CMAKE_VERBOSE_MAKEFILE OFF)
  8. set (WAMR_BUILD_PLATFORM "linux")
  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 (CMAKE_CXX_STANDARD 17)
  14. # Set WAMR_BUILD_TARGET, currently values supported:
  15. # "X86_64", "AMD_64", "X86_32", "AARCH64[sub]", "ARM[sub]", "THUMB[sub]",
  16. # "MIPS", "XTENSA", "RISCV64[sub]", "RISCV32[sub]"
  17. if (NOT DEFINED WAMR_BUILD_TARGET)
  18. if (CMAKE_SYSTEM_PROCESSOR MATCHES "^(arm64|aarch64)")
  19. set (WAMR_BUILD_TARGET "AARCH64")
  20. if (NOT DEFINED WAMR_BUILD_SIMD)
  21. set (WAMR_BUILD_SIMD 1)
  22. endif ()
  23. elseif (CMAKE_SYSTEM_PROCESSOR STREQUAL "riscv64")
  24. set (WAMR_BUILD_TARGET "RISCV64")
  25. elseif (CMAKE_SIZEOF_VOID_P EQUAL 8)
  26. # Build as X86_64 by default in 64-bit platform
  27. set (WAMR_BUILD_TARGET "X86_64")
  28. if (NOT DEFINED WAMR_BUILD_SIMD)
  29. set (WAMR_BUILD_SIMD 1)
  30. endif ()
  31. elseif (CMAKE_SIZEOF_VOID_P EQUAL 4)
  32. # Build as X86_32 by default in 32-bit platform
  33. set (WAMR_BUILD_TARGET "X86_32")
  34. else ()
  35. message(SEND_ERROR "Unsupported build target platform!")
  36. endif ()
  37. endif ()
  38. if (NOT CMAKE_BUILD_TYPE)
  39. set(CMAKE_BUILD_TYPE Release)
  40. endif ()
  41. if (NOT DEFINED WAMR_BUILD_INTERP)
  42. # Enable Interpreter by default
  43. set (WAMR_BUILD_INTERP 1)
  44. endif ()
  45. if (NOT DEFINED WAMR_BUILD_AOT)
  46. # Enable AOT by default.
  47. set (WAMR_BUILD_AOT 1)
  48. endif ()
  49. if (NOT DEFINED WAMR_BUILD_JIT)
  50. # Disable JIT by default.
  51. set (WAMR_BUILD_JIT 0)
  52. endif ()
  53. if (NOT DEFINED WAMR_BUILD_FAST_JIT)
  54. # Disable Fast JIT by default
  55. set (WAMR_BUILD_FAST_JIT 0)
  56. endif ()
  57. if (NOT DEFINED WAMR_BUILD_LIBC_BUILTIN)
  58. # Enable libc builtin support by default
  59. set (WAMR_BUILD_LIBC_BUILTIN 1)
  60. endif ()
  61. if (NOT DEFINED WAMR_BUILD_LIBC_WASI)
  62. # Enable libc wasi support by default
  63. set (WAMR_BUILD_LIBC_WASI 1)
  64. endif ()
  65. if (NOT DEFINED WAMR_BUILD_FAST_INTERP)
  66. # Enable fast interpreter
  67. set (WAMR_BUILD_FAST_INTERP 1)
  68. endif ()
  69. if (NOT DEFINED WAMR_BUILD_MULTI_MODULE)
  70. # Disable multiple modules by default
  71. set (WAMR_BUILD_MULTI_MODULE 0)
  72. endif ()
  73. if (NOT DEFINED WAMR_BUILD_LIB_PTHREAD)
  74. # Disable pthread library by default
  75. set (WAMR_BUILD_LIB_PTHREAD 0)
  76. endif ()
  77. if (NOT DEFINED WAMR_BUILD_LIB_WASI_THREADS)
  78. # Disable wasi threads library by default
  79. set (WAMR_BUILD_LIB_WASI_THREADS 0)
  80. endif()
  81. if (NOT DEFINED WAMR_BUILD_MINI_LOADER)
  82. # Disable wasm mini loader by default
  83. set (WAMR_BUILD_MINI_LOADER 0)
  84. endif ()
  85. if (NOT DEFINED WAMR_BUILD_SIMD)
  86. # Enable SIMD by default
  87. set (WAMR_BUILD_SIMD 1)
  88. endif ()
  89. if (NOT DEFINED WAMR_BUILD_REF_TYPES)
  90. # Enable reference types by default
  91. set (WAMR_BUILD_REF_TYPES 1)
  92. endif ()
  93. if (NOT DEFINED WAMR_BUILD_DEBUG_INTERP)
  94. # Disable Debug feature by default
  95. set (WAMR_BUILD_DEBUG_INTERP 0)
  96. endif ()
  97. if (WAMR_BUILD_DEBUG_INTERP EQUAL 1)
  98. set (WAMR_BUILD_FAST_INTERP 0)
  99. set (WAMR_BUILD_MINI_LOADER 0)
  100. set (WAMR_BUILD_SIMD 0)
  101. endif ()
  102. # if enable wasi-nn, both wasi-nn-backends and iwasm
  103. # need to use same WAMR (dynamic) libraries
  104. if (WAMR_BUILD_WASI_NN EQUAL 1)
  105. set (BUILD_SHARED_LIBS ON)
  106. endif ()
  107. set (WAMR_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../..)
  108. include (${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake)
  109. check_pie_supported()
  110. set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")
  111. set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wformat -Wformat-security -Wshadow")
  112. # set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wconversion -Wsign-conversion")
  113. set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wformat -Wformat-security -Wno-unused")
  114. if (WAMR_BUILD_TARGET MATCHES "X86_.*" OR WAMR_BUILD_TARGET STREQUAL "AMD_64")
  115. if (NOT (CMAKE_C_COMPILER MATCHES ".*clang.*" OR CMAKE_C_COMPILER_ID MATCHES ".*Clang"))
  116. set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mindirect-branch-register")
  117. set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mindirect-branch-register")
  118. # UNDEFINED BEHAVIOR, refer to https://en.cppreference.com/w/cpp/language/ub
  119. endif ()
  120. endif ()
  121. # The following flags are to enhance security, but it may impact performance,
  122. # we disable them by default.
  123. #if (WAMR_BUILD_TARGET MATCHES "X86_.*" OR WAMR_BUILD_TARGET STREQUAL "AMD_64")
  124. # set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ftrapv -D_FORTIFY_SOURCE=2")
  125. #endif ()
  126. #set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fstack-protector-strong --param ssp-buffer-size=4")
  127. #set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wl,-z,noexecstack,-z,relro,-z,now")
  128. include (${SHARED_DIR}/utils/uncommon/shared_uncommon.cmake)
  129. add_executable (iwasm main.c ${UNCOMMON_SHARED_SOURCE})
  130. set_version_info (iwasm)
  131. set_target_properties (iwasm PROPERTIES POSITION_INDEPENDENT_CODE ON)
  132. target_link_libraries(iwasm vmlib)
  133. install (TARGETS iwasm DESTINATION bin)
  134. add_library (vmlib ${WAMR_RUNTIME_LIB_SOURCE})
  135. set_version_info (vmlib)
  136. target_include_directories(vmlib INTERFACE
  137. $<INSTALL_INTERFACE:include>
  138. )
  139. set (WAMR_PUBLIC_HEADERS
  140. ${WAMR_ROOT_DIR}/core/iwasm/include/wasm_c_api.h
  141. ${WAMR_ROOT_DIR}/core/iwasm/include/wasm_export.h
  142. ${WAMR_ROOT_DIR}/core/iwasm/include/lib_export.h
  143. )
  144. set_target_properties (vmlib PROPERTIES
  145. OUTPUT_NAME iwasm
  146. PUBLIC_HEADER "${WAMR_PUBLIC_HEADERS}"
  147. POSITION_INDEPENDENT_CODE ON
  148. )
  149. target_link_libraries (vmlib ${LLVM_AVAILABLE_LIBS} ${UV_A_LIBS} -lm -ldl -lpthread)
  150. install (TARGETS vmlib
  151. EXPORT iwasmTargets
  152. DESTINATION lib
  153. PUBLIC_HEADER DESTINATION include
  154. )
  155. install_iwasm_package ()