CMakeLists.txt 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. cmake_minimum_required (VERSION 3.8)
  2. project(zerotier DESCRIPTION "ZeroTier Network Hypervisor" LANGUAGES CXX C)
  3. set(ZEROTIER_VERSION_MAJOR 1 CACHE INTERNAL "")
  4. set(ZEROTIER_VERSION_MINOR 9 CACHE INTERNAL "")
  5. set(ZEROTIER_VERSION_REVISION 0 CACHE INTERNAL "")
  6. set(ZEROTIER_VERSION_BUILD 0 CACHE INTERNAL "")
  7. if(${CMAKE_VERSION} VERSION_LESS 3.15)
  8. cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION})
  9. else()
  10. cmake_policy(VERSION 3.15)
  11. endif()
  12. find_program(
  13. GO go
  14. HINTS "/usr/local/go/bin" "C:/go/bin"
  15. )
  16. if(NOT GO)
  17. message(FATAL_ERROR "Golang not found")
  18. else(NOT GO)
  19. message(STATUS "Found Golang at ${GO}")
  20. endif(NOT GO)
  21. set(CMAKE_CXX_STANDARD 11)
  22. set(default_build_type "Release")
  23. if(WIN32)
  24. set(CMAKE_SYSTEM_VERSION "7" CACHE STRING INTERNAL FORCE)
  25. endif(WIN32)
  26. set(CMAKE_OSX_DEPLOYMENT_TARGET "10.12" CACHE STRING "Minimum OS X Deployment Version")
  27. if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
  28. message(STATUS "Setting build type to '${default_build_type}' as none was specified.")
  29. set(CMAKE_BUILD_TYPE "${default_build_type}" CACHE STRING "Choose the type of build." FORCE)
  30. set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo")
  31. endif()
  32. option(BUILD_CENTRAL_CONTROLLER "Build ZeroTier Central Controller" OFF)
  33. if(BUILD_CENTRAL_CONTROLLER)
  34. find_package(PkgConfig REQUIRED)
  35. if(APPLE)
  36. set(CMAKE_PREFIX_PATH
  37. ${CMAKE_PREFIX_PATH}
  38. /usr/local/opt/libpq
  39. /usr/local/lib
  40. )
  41. endif(APPLE)
  42. find_package(PostgreSQL REQUIRED)
  43. pkg_check_modules(hiredis REQUIRED IMPORTED_TARGET hiredis)
  44. add_subdirectory(controller/thirdparty/redis-plus-plus-1.1.1)
  45. set(redispp_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/controller/thirdparty/redis-plus-plus-1.1.1/src/sw)
  46. set(redispp_STATIC_LIB redispp_static)
  47. endif(BUILD_CENTRAL_CONTROLLER)
  48. if(CMAKE_BUILD_TYPE STREQUAL "Debug")
  49. add_definitions(-DZT_DEBUG)
  50. endif(CMAKE_BUILD_TYPE STREQUAL "Debug")
  51. set(GOFLAGS
  52. -trimpath
  53. -buildmode=pie
  54. )
  55. if(WIN32)
  56. message("++ Setting Windows Compiler Flags ${CMAKE_BUILD_TYPE}")
  57. add_definitions(-DNOMINMAX)
  58. else(WIN32)
  59. if(APPLE)
  60. message("++ Setting MacOS Compiler Flags ${CMAKE_BUILD_TYPE}")
  61. add_compile_options(
  62. -Wall
  63. -Wno-deprecated
  64. -Wno-unused-function
  65. -mmacosx-version-min=10.12
  66. $<$<CONFIG:DEBUG>:-g>
  67. $<$<CONFIG:DEBUG>:-O0>
  68. $<$<CONFIG:RELEASE>:-Ofast>
  69. $<$<CONFIG:RELEASE>:-ffast-math>
  70. $<$<CONFIG:RELEASE>:-fPIE>
  71. $<$<CONFIG:RELEASE>:-flto>
  72. $<$<CONFIG:RELWITHDEBINFO>:-Ofast>
  73. $<$<CONFIG:RELWITHDEBINFO>:-fPIE>
  74. $<$<CONFIG:RELWITHDEBINFO>:-g>
  75. )
  76. add_link_options(
  77. -mmacosx-version-min=10.12
  78. $<$<CONFIG:RELEASE>:-flto>
  79. )
  80. else(APPLE)
  81. message("++ Setting Linux/BSD/Posix Compiler Flags (${CMAKE_BUILD_TYPE})")
  82. add_compile_options(
  83. -Wall
  84. -Wno-deprecated
  85. -Wno-unused-function
  86. -Wno-format
  87. $<$<CONFIG:DEBUG>:-g>
  88. $<$<CONFIG:DEBUG>:-O0>
  89. $<$<CONFIG:RELEASE>:-O3>
  90. $<$<CONFIG:RELEASE>:-ffast-math>
  91. $<$<CONFIG:RELEASE>:-fPIE>
  92. $<$<CONFIG:RELWITHDEBINFO>:-O3>
  93. $<$<CONFIG:RELWITHDEBINFO>:-fPIE>
  94. $<$<CONFIG:RELWITHDEBINFO>:-g>
  95. )
  96. option(BUILD_32BIT "Force building as 32-bit binary" OFF)
  97. option(BUILD_STATIC "Build statically linked executable" OFF)
  98. if(BUILD_32BIT)
  99. set(CMAKE_SYSTEM_PROCESSOR "x86" CACHE STRING "system processor")
  100. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m32" CACHE STRING "c++ flags")
  101. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -m32" CACHE STRING "c flags")
  102. set(GOARCH "GOARCH=386" CACHE STRING "go architecture")
  103. add_compile_options(
  104. -m32
  105. )
  106. endif(BUILD_32BIT)
  107. if(BUILD_STATIC)
  108. add_link_options(
  109. -static
  110. )
  111. set(CMAKE_EXE_LINKER_FLAGS "-static ${CMAKE_EXE_LINKER_FLAGS}")
  112. set(GOFLAGS
  113. ${GOFLAGS}
  114. -a
  115. -tags netgo
  116. -ldflags '-w -extldflags \"-static\"')
  117. endif(BUILD_STATIC)
  118. endif(APPLE)
  119. endif(WIN32)
  120. if (
  121. CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64" OR
  122. CMAKE_SYSTEM_PROCESSOR MATCHES "amd64"
  123. )
  124. message("++ Adding flags for processor ${CMAKE_SYSTEM_PROCESSOR}")
  125. add_compile_options(-maes -mrdrnd -mpclmul -msse -msse2 -mssse3)
  126. endif()
  127. set(GO_BUILD_TAGS)
  128. if(BUILD_CENTRAL_CONTROLLER)
  129. add_definitions(-DZT_CONTROLLER_USE_LIBPQ=1)
  130. set(GO_BUILD_TAGS -tags central)
  131. endif(BUILD_CENTRAL_CONTROLLER)
  132. add_subdirectory(core)
  133. add_subdirectory(controller)
  134. add_subdirectory(osdep)
  135. add_subdirectory(serviceiocore)
  136. file(GLOB go_src
  137. ${CMAKE_SOURCE_DIR}/cmd/*.go
  138. ${CMAKE_SOURCE_DIR}/cmd/cmd/*.go
  139. ${CMAKE_SOURCE_DIR}/pkg/zerotier/*.go)
  140. add_custom_target(
  141. zerotier ALL
  142. BYPRODUCTS ${CMAKE_BINARY_DIR}/zerotier
  143. SOURCES ${go_src}
  144. COMMAND ${GOARCH} CGO_ENABLED=1 CGO_LDFLAGS=\"$<TARGET_FILE:zt_osdep> $<TARGET_FILE:zt_core> $<TARGET_FILE:zt_controller> $<TARGET_FILE:zt_service_io_core>\" ${GO} build ${GOFLAGS} -o ${CMAKE_BINARY_DIR}/zerotier ${CMAKE_SOURCE_DIR}/cmd/zerotier/zerotier.go
  145. COMMENT "Compiling Go Code..."
  146. )
  147. add_dependencies(zerotier zt_osdep zt_core zt_controller zt_service_io_core)