CMakeLists.txt 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. # CMake build script for libzerotiercore.a
  2. cmake_minimum_required (VERSION 3.13)
  3. project (zerotier-one LANGUAGES CXX C)
  4. option(ZT1_CENTRAL_CONTROLLER "Build with ZeroTier Central Controller support" OFF)
  5. option(ADDRESS_SANITIZER "Build with Address Sanitizer enabled (only for x86_64/arm64)" OFF
  6. )
  7. set (PROJ_DIR ${PROJECT_SOURCE_DIR})
  8. execute_process(COMMAND uname -m COMMAND tr -d '\n' OUTPUT_VARIABLE CPU_ARCHITECTURE)
  9. set(CPU_ARCHITECTURE ${CPU_ARCHITECTURE} CACHE STRING "Target CPU architecture (detected automatically)")
  10. message(STATUS "Detected CPU architecture: ${CPU_ARCHITECTURE}")
  11. if(CPU_ARCHITECTURE STREQUAL "x86_64")
  12. add_definitions(
  13. -DZT_ARCHITECTURE=2
  14. -DZT_USE_X64_ASM_SALSA=1
  15. -DZT_USE_X64_ASM_ED25519=1
  16. -DZT_SSO_SUPPORTED=1
  17. -DZT_USE_X64_ASM_SALSA2012=1
  18. -DZT_USE_FAST_X64_ED25519=1
  19. )
  20. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -z noexecstack")
  21. if(ADDRESS_SANITIZER)
  22. add_compile_options(-fsanitize=address)
  23. add_link_options(-fsanitize=address)
  24. endif()
  25. elseif(CPU_ARCHITECTURE STREQUAL "aarch64" OR CPU_ARCHITECTURE STREQUAL "arm64")
  26. add_definitions(-DZT_ARCHITECTURE=4 -DZT_ARCH_ARM_HAS_NEON=1 -DZT_SSO_SUPPORTED=1 -DZT_AES_NEON=1)
  27. add_compile_options(-march=armv8-a+crypto -mtune=generic -mstrict-align)
  28. if(ADDRESS_SANITIZER)
  29. add_compile_options(-fsanitize=address)
  30. add_link_options(-fsanitize=address)
  31. endif()
  32. endif()
  33. include(FetchContent)
  34. include(ExternalProject)
  35. add_definitions(-DCMAKE_BUILD)
  36. find_package(OpenSSL REQUIRED)
  37. find_package(nlohmann_json REQUIRED)
  38. find_package(inja REQUIRED)
  39. set(CMAKE_THREDAD_PREFER_PTHREAD TRUE CACHE INTERNAL "Use pthreads" FORCE)
  40. set(THREADS_PREFER_PTHREAD_FLAG TRUE CACHE INTERNAL "Use pthreads" FORCE)
  41. find_package(Threads REQUIRED)
  42. if(CMAKE_USE_PTHREADS_INIT)
  43. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pthread")
  44. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
  45. endif()
  46. if(ZT1_CENTRAL_CONTROLLER)
  47. find_package(PostgreSQL REQUIRED)
  48. find_package(opentelemetry-cpp REQUIRED COMPONENTS api sdk exporters_otlp_grpc exporters_otlp_http exporters_prometheus )
  49. find_package(google_cloud_cpp_bigtable REQUIRED)
  50. find_package(google_cloud_cpp_pubsub REQUIRED)
  51. else()
  52. find_package(opentelemetry-cpp REQUIRED COMPONENTS api)
  53. endif()
  54. if(ZT1_CENTRAL_CONTROLLER)
  55. set(CMAKE_CXX_STANDARD 17 CACHE STRING "C++ standard to conform to" FORCE)
  56. set(CMAKE_CXX_STANDARD_REQUIRED True CACHE BOOL "C++ standard required" FORCE)
  57. set(CMAKE_CXX_EXTENSIONS ON CACHE BOOL "Enable compiler-specific extensions" FORCE)
  58. add_definitions(-DZT_NONFREE_CONTROLLER=1 -DZT_CONTROLLER_USE_LIBPQ=1 -DZT1_CENTRAL_CONTROLLER=1 -DZT_OPENTELEMETRY_ENABLED=1)
  59. set(CONTROLLER_RUST_FEATURES ztcontroller)
  60. set(RUST_BUILD_COMMAND cargo build --release -F ${CONTROLLER_RUST_FEATURES})
  61. else()
  62. set(CMAKE_CXX_STANDARD 11 CACHE STRING "C++ standard to conform to")
  63. set(CMAKE_CXX_STANDARD_REQUIRED True CACHE BOOL "C++ standard required")
  64. set(CMAKE_CXX_EXTENSIONS ON CACHE BOOL "Enable compiler-specific extensions")
  65. set(RUST_BUILD_COMMAND "cargo build --release")
  66. endif()
  67. # build rustybits
  68. ExternalProject_Add(
  69. rustybits_build
  70. DOWNLOAD_COMMAND ""
  71. CONFIGURE_COMMAND ""
  72. # PREFIX ${PROJ_DIR}/rustybits
  73. BUILD_COMMAND cd ${PROJ_DIR}/rustybits && ${RUST_BUILD_COMMAND}
  74. INSTALL_COMMAND ""
  75. BUILD_BYPRODUCTS ${PROJ_DIR}/rustybits/target/release/librustybits.a ${PROJ_DIR}/rustybits/target/rustybits.h
  76. )
  77. set(RUSTYBITS_LIB ${PROJ_DIR}/rustybits/target/release/librustybits.a)
  78. set(RUSTYBITS_INCLUDE_DIR ${PROJ_DIR}/rustybits/target)
  79. add_library(rustybits STATIC IMPORTED GLOBAL)
  80. set_property(TARGET rustybits PROPERTY IMPORTED_LOCATION ${RUSTYBITS_LIB})
  81. add_dependencies(rustybits rustybits_build)
  82. # Get & build dependencies not in conda
  83. include (cmake/cpp-httplib.cmake)
  84. include (cmake/redis-plus-plus.cmake)
  85. include (cmake/miniupnpc.cmake)
  86. add_subdirectory(ext)
  87. add_subdirectory(node)
  88. add_subdirectory(osdep)
  89. add_subdirectory(service)
  90. add_subdirectory(nonfree)
  91. set(LINKED_LIBRARIES
  92. prometheus-cpp-lite
  93. zerotier-service
  94. zerotier-osdep
  95. zerotier-core
  96. zerotier-controller
  97. Threads::Threads
  98. nlohmann_json::nlohmann_json
  99. opentelemetry-cpp::api
  100. rustybits
  101. OpenSSL::Crypto
  102. OpenSSL::SSL
  103. Threads::Threads
  104. )
  105. if(ZT1_CENTRAL_CONTROLLER)
  106. list(APPEND LINKED_LIBRARIES
  107. opentelemetry-cpp::sdk
  108. opentelemetry-cpp::trace
  109. opentelemetry-cpp::proto_grpc
  110. opentelemetry-cpp::otlp_grpc_client
  111. opentelemetry-cpp::otlp_grpc_exporter
  112. opentelemetry-cpp::otlp_grpc_log_record_exporter
  113. opentelemetry-cpp::otlp_grpc_metrics_exporter
  114. opentelemetry-cpp::otlp_http_exporter
  115. opentelemetry-cpp::otlp_http_log_record_exporter
  116. opentelemetry-cpp::otlp_http_metric_exporter
  117. opentelemetry-cpp::prometheus_exporter
  118. )
  119. endif()
  120. if (APPLE)
  121. find_library(COREFOUNDATION_LIBRARY CoreFoundation)
  122. find_library(SECURITY_LIBRARY Security)
  123. find_library(SYSTEM_CONFIGURATION_LIBRARY SystemConfiguration)
  124. find_library(CARBON Carbon)
  125. find_library(CORESERVICES_LIBRARY CoreServices)
  126. list(APPEND LINKED_LIBRARIES ${COREFOUNDATION_LIBRARY} ${SECURITY_LIBRARY} ${CARBON_LIBRARY} ${SYSTEM_CONFIGURATION_LIBRARY} ${CORESERVICES_LIBRARY})
  127. endif()
  128. add_executable(zerotier-one
  129. one.cpp
  130. ext/http-parser/http_parser.c)
  131. target_link_libraries(zerotier-one
  132. ${LINKED_LIBRARIES}
  133. )
  134. add_executable(zerotier-selftest
  135. selftest.cpp
  136. )
  137. target_link_libraries(zerotier-selftest
  138. zerotier-core
  139. zerotier-osdep
  140. Threads::Threads
  141. nlohmann_json::nlohmann_json
  142. prometheus-cpp-lite
  143. Threads::Threads)