Advertisement
mattygrogy

Untitled

Mar 23rd, 2022
949
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
CMake 9.03 KB | None | 0 0
  1. cmake_minimum_required(VERSION 3.9)
  2.  
  3. # If vcpkg present as submodule, bring in the toolchain
  4. if( EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/vcpkg/scripts/buildsystems/vcpkg.cmake )
  5.     message(STATUS "Found ${CMAKE_CURRENT_SOURCE_DIR}/vcpkg/scripts/buildsystems/vcpkg.cmake; using it!")
  6.     set(CMAKE_TOOLCHAIN_FILE ${CMAKE_CURRENT_SOURCE_DIR}/vcpkg/scripts/buildsystems/vcpkg.cmake
  7.         CACHE STRING "Vcpkg toolchain file")
  8. endif()
  9.  
  10. include(CheckIPOSupported)
  11. include(CMakeDependentOption)
  12. include(CMakePushCheckState)
  13. include(CheckSymbolExists)
  14.  
  15. # CMP0069: INTERPROCEDURAL_OPTIMIZATION is enforced when enabled.
  16. # This variable is needed for abseil, which has a different
  17. # cmake_minimum_required version set (3.5).
  18. set(CMAKE_POLICY_DEFAULT_CMP0069 NEW)
  19.  
  20. # Put all the output from all projects into the same folder
  21. #set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
  22. set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
  23. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
  24.  
  25. project(GameNetworkingSockets C CXX)
  26.  
  27. set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
  28.  
  29. include(DefaultBuildType)
  30. find_package(Sanitizers)
  31.  
  32. if(SANITIZE_ADDRESS OR SANITIZE_THREAD OR SANITIZE_MEMORY OR SANITIZE_UNDEFINED)
  33.     set(SANITIZE ON)
  34. endif()
  35.  
  36. include(FlagsMSVC)
  37. set(MSVC_RUNTIME "dynamic")
  38. configure_msvc_runtime()
  39. print_default_msvc_flags()
  40.  
  41. add_definitions( -DVALVE_CRYPTO_ENABLE_25519 )
  42. if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
  43.     add_definitions(
  44.         -D_CRT_SECURE_NO_WARNINGS
  45.         -D_CRT_NONSTDC_NO_WARNINGS
  46.         )
  47. endif()
  48.  
  49. option(LTO "Enable Link-Time Optimization" OFF)
  50. option(USE_STEAMWEBRTC "Build Google's WebRTC library to get ICE support for P2P" OFF)
  51. option(Protobuf_USE_STATIC_LIBS "Link with protobuf statically" OFF)
  52. option(LIGHT_TESTS "Use smaller/shorter tests for simple integration testing (e.g. Travis)" OFF)
  53. option(GAMENETWORKINGSOCKETS_BUILD_EXAMPLES "Build the included example chat program" ON)
  54. option(GAMENETWORKINGSOCKETS_BUILD_TESTS "Build crypto, pki and network connection tests" ON)
  55.  
  56. #
  57. # Primary crypto library (for AES, SHA256, etc)
  58. #
  59. set(useCryptoOptions OpenSSL libsodium BCrypt)
  60. set(USE_CRYPTO "OpenSSL" CACHE STRING "Crypto library to use for AES/SHA256")
  61. set_property(CACHE USE_CRYPTO PROPERTY STRINGS ${useCryptoOptions})
  62.  
  63. list(FIND useCryptoOptions ${USE_CRYPTO} useCryptoIndex)
  64. if(useCryptoIndex EQUAL -1)
  65.     message(FATAL_ERROR "USE_CRYPTO must be one of: ${useCryptoOptions}")
  66. endif()
  67. if(USE_CRYPTO STREQUAL "BCrypt" AND NOT WIN32)
  68.     message(FATAL_ERROR "USE_CRYPTO=\"BCrypt\" is only valid on Windows")
  69. endif()
  70.  
  71. if(LTO)
  72.     check_ipo_supported()
  73. endif()
  74.  
  75. if (WIN32)
  76.     #
  77.     # Strip compiler flags which conflict with ones we explicitly set. If we don't
  78.     # do this, then we get a warning on every file we compile for the library.
  79.     #
  80.     string(REPLACE "/EHsc" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
  81.     string(REPLACE "/GR" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
  82.  
  83.     #
  84.     # Check whether BCrypt can be used with this SDK version
  85.     #
  86.     cmake_push_check_state()
  87.         set(CMAKE_REQUIRED_LIBRARIES bcrypt)
  88.         check_symbol_exists(BCryptEncrypt windows.h BCRYPT_AVAILABLE)
  89.     cmake_pop_check_state()
  90.     if (NOT BCRYPT_AVAILABLE AND USE_CRYPTO STREQUAL "BCrypt")
  91.         message(FATAL_ERROR "You're on Windows but BCrypt seems to be unavailable, you will need OpenSSL")
  92.     endif()
  93. endif()
  94.  
  95. if (USE_CRYPTO STREQUAL "BCrypt")
  96.     set(useCrypto25519Default "Reference")
  97. else()
  98.     set(useCrypto25519Default "OpenSSL")
  99. endif()
  100.  
  101. #
  102. # Secondary crypto library (for ed25519/curve25519).
  103. #
  104. set(useCrypto25519Options OpenSSL libsodium Reference)
  105. set(USE_CRYPTO25519 "${useCrypto25519Default}" CACHE STRING "Crypto library to use for ed25519/curve25519")
  106. set_property(CACHE USE_CRYPTO25519 PROPERTY STRINGS ${useCrypto25519Options})
  107.  
  108. list(FIND useCrypto25519Options ${USE_CRYPTO25519} useCrypto25519Index)
  109. if(useCrypto25519Index EQUAL -1)
  110.     message(FATAL_ERROR "USE_CRYPTO25519 must be one of: ${useCrypto25519Options}")
  111. endif()
  112.  
  113. if (USE_CRYPTO25519 STREQUAL "OpenSSL" OR USE_CRYPTO STREQUAL "OpenSSL")
  114.     find_package(OpenSSL REQUIRED)
  115.     message( STATUS "OPENSSL_INCLUDE_DIR = ${OPENSSL_INCLUDE_DIR}" )
  116.  
  117.     # Ensure the OpenSSL version is recent enough. We need a bunch of EVP
  118.     # functionality.
  119.     cmake_push_check_state()
  120.         set(CMAKE_REQUIRED_INCLUDES ${OPENSSL_INCLUDE_DIR})
  121.         set(CMAKE_REQUIRED_LIBRARIES OpenSSL::Crypto)
  122.         if(WIN32 AND OPENSSL_USE_STATIC_LIBS)
  123.             list(APPEND CMAKE_REQUIRED_LIBRARIES ws2_32 crypt32)
  124.         endif()
  125.         check_symbol_exists(EVP_MD_CTX_free openssl/evp.h OPENSSL_NEW_ENOUGH)
  126.         if (NOT OPENSSL_NEW_ENOUGH)
  127.             message(FATAL_ERROR "Cannot find EVP_MD_CTX_free in OpenSSL headers/libs for the target architecture.  Check that you're using OpenSSL 1.1.0 or later.")
  128.         endif()
  129.     cmake_pop_check_state()
  130.     cmake_push_check_state()
  131.         set(CMAKE_REQUIRED_LIBRARIES OpenSSL::Crypto)
  132.         if(WIN32 AND OPENSSL_USE_STATIC_LIBS)
  133.             list(APPEND CMAKE_REQUIRED_LIBRARIES ws2_32 crypt32)
  134.         endif()
  135.         if(USE_CRYPTO25519 STREQUAL "OpenSSL")
  136.             check_symbol_exists(EVP_PKEY_get_raw_public_key openssl/evp.h OPENSSL_HAS_25519_RAW)
  137.         endif()
  138.     cmake_pop_check_state()
  139. endif()
  140.  
  141. if(USE_CRYPTO25519 STREQUAL "OpenSSL" AND NOT OPENSSL_HAS_25519_RAW)
  142.     message(FATAL_ERROR "Cannot find (EVP_PKEY_get_raw_public_key in OpenSSL headers/libs for the target architecture.  Please use -DUSE_CRYPTO25519=Reference or upgrade OpenSSL to 1.1.1 or later")
  143. endif()
  144.  
  145. if(USE_CRYPTO STREQUAL "libsodium" OR USE_CRYPTO25519 STREQUAL "libsodium")
  146.     find_package(sodium REQUIRED)
  147. endif()
  148.  
  149. if(USE_CRYPTO STREQUAL "libsodium")
  150.     if(NOT CMAKE_SYSTEM_PROCESSOR MATCHES "amd64.*|x86_64.*|AMD64.*|i686.*|i386.*|x86.*")
  151.         message(FATAL_ERROR "-DUSE_CRYPTO=libsodium invalid, libsodium AES implementation only works on x86/x86_64 CPUs")
  152.     endif()
  153. endif()
  154.  
  155. # We always need at least sse2 on x86
  156. if(CMAKE_SYSTEM_PROCESSOR MATCHES "amd64.*|x86_64.*|AMD64.*|i686.*|i386.*|x86.*")
  157.        set(TARGET_ARCH_FLAGS "-msse2")
  158. endif()
  159.  
  160. function(set_target_common_gns_properties TGT)
  161.     target_compile_definitions( ${TGT} PRIVATE GOOGLE_PROTOBUF_NO_RTTI )
  162.  
  163.     if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
  164.         # Reduce binary size by allowing for a pseudo-"function-level linking" analog
  165.         target_compile_options(${TGT} PRIVATE -ffunction-sections -fdata-sections ${TARGET_ARCH_FLAGS})
  166.     endif()
  167.  
  168.     if(CMAKE_SYSTEM_NAME MATCHES Linux)
  169.         target_compile_definitions(${TGT} PUBLIC POSIX LINUX)
  170.     elseif(CMAKE_SYSTEM_NAME MATCHES Darwin)
  171.         target_compile_definitions(${TGT} PUBLIC POSIX OSX)
  172.     elseif(CMAKE_SYSTEM_NAME MATCHES FreeBSD)
  173.         target_compile_definitions(${TGT} PUBLIC POSIX FREEBSD)
  174.     elseif(CMAKE_SYSTEM_NAME MATCHES Windows)
  175.         target_compile_definitions(${TGT} PUBLIC _WINDOWS)
  176.         if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
  177.             if(NOT Protobuf_USE_STATIC_LIBS)
  178.                 target_compile_definitions(${TGT} PRIVATE PROTOBUF_USE_DLLS)
  179.             endif()
  180.             target_compile_options(${TGT} PRIVATE
  181.                 /EHs-c-   # Disable C++ exceptions
  182.  
  183.                 # Below are warnings we can't fix and don't want to see (mostly from protobuf, some from MSVC standard library)
  184.                 /wd4146   # include/google/protobuf/wire_format_lite.h(863): warning C4146: unary minus operator applied to unsigned type, result still unsigned
  185.                 /wd4530   # .../xlocale(319): warning C4530: C++ exception handler used, but unwind semantics are not enabled. Specify /EHsc
  186.                 /wd4244   # google/protobuf/wire_format_lite.h(935): warning C4244: 'argument': conversion from 'google::protobuf::uint64' to 'google::protobuf::uint32', possible loss of data
  187.                 /wd4251   # 'google::protobuf::io::CodedOutputStream::default_serialization_deterministic_': struct 'std::atomic<bool>' needs to have dll-interface to be used by clients of class
  188.                 /wd4267   # google/protobuf/has_bits.h(73): warning C4267: 'argument': conversion from 'size_t' to 'int', possible loss of data
  189.                 )
  190.  
  191.             # Disable RTTI except in Debug, because we use dynamic_cast in assert_cast
  192.             target_compile_options(${TGT} PRIVATE $<IF:$<CONFIG:Debug>,/GR,/GR->)
  193.         else()
  194.             target_compile_definitions(${TGT} PRIVATE
  195.                 __STDC_FORMAT_MACROS=1
  196.                 __USE_MINGW_ANSI_STDIO=0
  197.                 )
  198.             target_compile_options(${TGT} PRIVATE -fno-stack-protector)
  199.         endif()
  200.     else()
  201.         message(FATAL_ERROR "Could not identify your target operating system")
  202.     endif()
  203.  
  204.     if(NOT CMAKE_SYSTEM_NAME MATCHES Windows)
  205.         target_compile_options(${TGT} PRIVATE -fstack-protector-strong)
  206.     endif()
  207.  
  208.     if(LTO)
  209.         set_target_properties(${TGT} PROPERTIES INTERPROCEDURAL_OPTIMIZATION TRUE)
  210.     endif()
  211.  
  212.     set_target_properties(${TGT} PROPERTIES
  213.         CXX_STANDARD 11
  214.     )
  215. endfunction()
  216.  
  217. if(GAMENETWORKINGSOCKETS_BUILD_EXAMPLES)
  218.     add_subdirectory(examples)
  219. endif()
  220.  
  221. if(GAMENETWORKINGSOCKETS_BUILD_TESTS)
  222.     add_subdirectory(tests)
  223. endif()
  224.  
  225. add_subdirectory(src)
  226.  
  227. #message(STATUS "---------------------------------------------------------")
  228. message(STATUS "Crypto library for AES/SHA256: ${USE_CRYPTO}")
  229. message(STATUS "Crypto library for ed25519/curve25519: ${USE_CRYPTO25519}")
  230. message(STATUS "Link-time optimization: ${LTO}")
  231. #message(STATUS "---------------------------------------------------------")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement