CMakeLists.txt 5.9 KB

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