diff --git a/CMakeLists.txt b/CMakeLists.txt index 1afed6d4..fcb03984 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -142,6 +142,51 @@ if(USE_CRYPTO STREQUAL "libsodium") endif() endif() + +function(set_target_common_gns_properties TGT) + target_compile_definitions( ${TGT} PRIVATE GOOGLE_PROTOBUF_NO_RTTI ) + + if(CMAKE_SYSTEM_NAME MATCHES Linux) + target_compile_definitions(${TGT} PUBLIC POSIX LINUX) + elseif(CMAKE_SYSTEM_NAME MATCHES Darwin) + target_compile_definitions(${TGT} PUBLIC POSIX OSX) + elseif(CMAKE_SYSTEM_NAME MATCHES Windows) + #target_compile_definitions(${TGT} PUBLIC WIN32) + if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC") + if(NOT Protobuf_USE_STATIC_LIBS) + target_compile_definitions(${TGT} PRIVATE PROTOBUF_USE_DLLS) + endif() + target_compile_options(${TGT} PRIVATE + /EHs-c- # Disable C++ exceptions + /GR- # Disable RTTI + + # Below are warnings we can't fix and don't want to see (mostly from protobuf, some from MSVC standard library) + /wd4146 # include/google/protobuf/wire_format_lite.h(863): warning C4146: unary minus operator applied to unsigned type, result still unsigned + /wd4530 # .../xlocale(319): warning C4530: C++ exception handler used, but unwind semantics are not enabled. Specify /EHsc + /wd4244 # google/protobuf/wire_format_lite.h(935): warning C4244: 'argument': conversion from 'google::protobuf::uint64' to 'google::protobuf::uint32', possible loss of data + /wd4251 # 'google::protobuf::io::CodedOutputStream::default_serialization_deterministic_': struct 'std::atomic' needs to have dll-interface to be used by clients of class + /wd4267 # google/protobuf/has_bits.h(73): warning C4267: 'argument': conversion from 'size_t' to 'int', possible loss of data + ) + else() + target_compile_definitions(${TGT} PRIVATE + __STDC_FORMAT_MACROS=1 + __USE_MINGW_ANSI_STDIO=0 + ) + target_compile_options(${TGT} PRIVATE -fno-stack-protector) + endif() + else() + message(FATAL_ERROR "Could not identify your target operating system") + endif() + + if(NOT CMAKE_SYSTEM_NAME MATCHES Windows) + target_compile_options(${TGT} PRIVATE -fstack-protector-strong) + endif() + + set_target_properties(${TGT} PROPERTIES + CXX_STANDARD 11 + ) +endfunction() + if(GAMENETWORKINGSOCKETS_BUILD_EXAMPLES) add_subdirectory(examples) endif() diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index aea9f8f0..df1a7049 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -30,6 +30,8 @@ add_executable( example_chat example_chat.cpp) +set_target_common_gns_properties( example_chat ) + # If building the example as a standalone project, need to find GameNetworkingSockets if(${CMAKE_PROJECT_NAME} STREQUAL "gns_example") find_package(GameNetworkingSockets CONFIG REQUIRED) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c4495659..04f42044 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -152,6 +152,8 @@ macro(gamenetworkingsockets_common GNS_TARGET) target_sources(${GNS_TARGET} PRIVATE ${GNS_SRCS}) target_sources(${GNS_TARGET} PRIVATE ${GNS_PROTO_SRCS}) + set_target_common_gns_properties( ${GNS_TARGET} ) + target_include_directories(${GNS_TARGET} PUBLIC "$" "$/${CMAKE_INSTALL_INCLUDEDIR}/GameNetworkingSockets>" @@ -213,7 +215,6 @@ macro(gamenetworkingsockets_common GNS_TARGET) STEAMNETWORKINGSOCKETS_FOREXPORT ENABLE_OPENSSLCONNECTION CRYPTO_DISABLE_ENCRYPT_WITH_PASSWORD - GOOGLE_PROTOBUF_NO_RTTI ${GNS_CRYPTO_DEFINES} ) @@ -253,42 +254,19 @@ macro(gamenetworkingsockets_common GNS_TARGET) if(USE_STEAMWEBRTC AND NOT STEAMWEBRTC_USE_STATIC_LIBS) target_link_libraries(${GNS_TARGET} PRIVATE dl) endif() - target_compile_definitions(${GNS_TARGET} PUBLIC POSIX LINUX) elseif(CMAKE_SYSTEM_NAME MATCHES Darwin) if(USE_STEAMWEBRTC AND NOT STEAMWEBRTC_USE_STATIC_LIBS) target_link_libraries(${GNS_TARGET} PRIVATE dl) endif() - target_compile_definitions(${GNS_TARGET} PUBLIC POSIX OSX) elseif(CMAKE_SYSTEM_NAME MATCHES Windows) - target_compile_definitions(${GNS_TARGET} PUBLIC WIN32) if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC") - if(NOT Protobuf_USE_STATIC_LIBS) - target_compile_definitions(${GNS_TARGET} PRIVATE - PROTOBUF_USE_DLLS) - endif() get_target_property(TARGET_TYPE ${GNS_TARGET} TYPE) - target_compile_options(${GNS_TARGET} PRIVATE - /EHs-c- # Disable C++ exceptions - /GR- # Disable RTTI - - # Below are warnings we can't fix and don't want to see (mostly from protobuf, some from MSVC standard library) - /wd4146 # include/google/protobuf/wire_format_lite.h(863): warning C4146: unary minus operator applied to unsigned type, result still unsigned - /wd4530 # .../xlocale(319): warning C4530: C++ exception handler used, but unwind semantics are not enabled. Specify /EHsc - /wd4244 # google/protobuf/wire_format_lite.h(935): warning C4244: 'argument': conversion from 'google::protobuf::uint64' to 'google::protobuf::uint32', possible loss of data - /wd4251 # 'google::protobuf::io::CodedOutputStream::default_serialization_deterministic_': struct 'std::atomic' needs to have dll-interface to be used by clients of class - ) if(NOT TARGET_TYPE STREQUAL STATIC_LIBRARY) target_compile_options(${GNS_TARGET} PRIVATE /GL # Enable link-time code generation ) set_target_properties(${GNS_TARGET} PROPERTIES LINK_FLAGS "/LTCG /SUBSYSTEM:WINDOWS") endif() - else() - target_compile_definitions(${GNS_TARGET} PRIVATE - __STDC_FORMAT_MACROS=1 - __USE_MINGW_ANSI_STDIO=0 - ) - target_compile_options(${GNS_TARGET} PRIVATE -fno-stack-protector) endif() target_link_libraries(${GNS_TARGET} PUBLIC ws2_32 crypt32) if(USE_CRYPTO STREQUAL "BCrypt") @@ -298,14 +276,6 @@ macro(gamenetworkingsockets_common GNS_TARGET) message(FATAL_ERROR "Could not identify your target operating system") endif() - if(NOT CMAKE_SYSTEM_NAME MATCHES Windows) - target_compile_options(${GNS_TARGET} PRIVATE -fstack-protector-strong) - endif() - - set_target_properties(${GNS_TARGET} PROPERTIES - CXX_STANDARD 11 - ) - add_sanitizers(${GNS_TARGET}) endmacro() diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index eea23951..2b297f41 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -31,6 +31,10 @@ target_include_directories(test_crypto PRIVATE ../src ../src/public ../src/commo target_link_libraries(test_crypto GameNetworkingSockets_s) add_sanitizers(test_crypto) +set_target_common_gns_properties( test_connection ) +set_target_common_gns_properties( test_p2p ) +set_target_common_gns_properties( test_crypto ) + file(COPY aesgcmtestvectors DESTINATION ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}) # NOTE: This happens when we generate the projects, and really we'd like to actually