From 528c9f927f8e939f797e341d6f6bebfad30a8212 Mon Sep 17 00:00:00 2001 From: Ivey Date: Tue, 19 Nov 2024 11:40:03 -0500 Subject: [PATCH] build: Set MSVC build settings and make sure MSVC is defined --- .../custom-modules/ns3-module-macros.cmake | 4 +++ .../custom-modules/ns3-platform-support.cmake | 34 ++++++++++++++++--- .../custom-modules/ns3-vcpkg-hunter.cmake | 2 +- build-support/macros-and-definitions.cmake | 2 +- 4 files changed, 36 insertions(+), 6 deletions(-) diff --git a/build-support/custom-modules/ns3-module-macros.cmake b/build-support/custom-modules/ns3-module-macros.cmake index 0c70261ba..e9a6118f2 100644 --- a/build-support/custom-modules/ns3-module-macros.cmake +++ b/build-support/custom-modules/ns3-module-macros.cmake @@ -233,6 +233,10 @@ function(build_lib) "${BLIB_LIBNAME}" "${BLIB_IGNORE_PCH}" "${FOLDER}" "${BLIB_TEST_SOURCES}" ) + if(${MSVC}) + target_link_options(${lib${BLIB_LIBNAME}} PUBLIC "/OPT:NOREF") + endif() + # Handle package export install( TARGETS ${BLIB_LIBNAME} diff --git a/build-support/custom-modules/ns3-platform-support.cmake b/build-support/custom-modules/ns3-platform-support.cmake index d640ce228..91007a1a9 100644 --- a/build-support/custom-modules/ns3-platform-support.cmake +++ b/build-support/custom-modules/ns3-platform-support.cmake @@ -50,6 +50,7 @@ if(APPLE) set(CMAKE_FIND_APPBUNDLE "LAST") endif() +set(cat_command cat) if(WIN32) set(NS3_PRECOMPILE_HEADERS OFF CACHE BOOL "Precompile module headers to speed up compilation" FORCE @@ -59,17 +60,24 @@ if(WIN32) # requires this definition # https://docs.microsoft.com/en-us/cpp/c-runtime-library/math-constants?view=vs-2019 add_definitions(/D_USE_MATH_DEFINES) + set(cat_command type) # required until we move to CMake >= 3.18 endif() -set(cat_command cat) - if(CMAKE_XCODE_BUILD_SYSTEM) set(XCODE True) else() set(XCODE False) endif() -if(${XCODE}) +if("${CMAKE_CXX_COMPILER_ID}" MATCHES "MSVC" OR "${CMAKE_CXX_SIMULATE_ID}" + MATCHES "MSVC" +) + set(MSVC True) +else() + set(MSVC False) +endif() + +if(${MSVC} OR ${XCODE}) # Prevent multi-config generators from placing output files into per # configuration directory foreach(OUTPUTCONFIG ${CMAKE_CONFIGURATION_TYPES}) @@ -83,5 +91,23 @@ if(${XCODE}) set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY} ) - endforeach() + endforeach(OUTPUTCONFIG CMAKE_CONFIGURATION_TYPES) +endif() + +if(${MSVC}) + set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON) + + # Enable exceptions + add_definitions(/EHs) + + # Account for lack of valgrind and binary compatibility restrictions on + # Windows https://github.com/microsoft/STL/wiki/Changelog#vs-2022-1710 + add_compile_definitions( + __WIN32__ NVALGRIND _DISABLE_CONSTEXPR_MUTEX_CONSTRUCTOR + _CRT_SECURE_NO_WARNINGS NS_MSVC NOMINMAX + ) + + # Allow unresolved symbols as in most cases they are simply in other ns-3 + # libraries + add_link_options("/FORCE:UNRESOLVED") endif() diff --git a/build-support/custom-modules/ns3-vcpkg-hunter.cmake b/build-support/custom-modules/ns3-vcpkg-hunter.cmake index 997ab3904..3157239f1 100644 --- a/build-support/custom-modules/ns3-vcpkg-hunter.cmake +++ b/build-support/custom-modules/ns3-vcpkg-hunter.cmake @@ -62,7 +62,7 @@ function(setup_vcpkg) ) endif() - if(DEFINED MSVC) + if(${MSVC}) message(FATAL_ERROR "vcpkg: Visual Studio is unsupported") else() # Check if required packages are installed (unzip curl tar) diff --git a/build-support/macros-and-definitions.cmake b/build-support/macros-and-definitions.cmake index 2dfe30296..c2481cd00 100644 --- a/build-support/macros-and-definitions.cmake +++ b/build-support/macros-and-definitions.cmake @@ -212,7 +212,7 @@ macro(process_options) # Set warning level and warning as errors if(${NS3_WARNINGS}) - if(DEFINED MSVC) + if(${MSVC}) add_compile_options(/W3) # /W4 = -Wall + -Wextra if(${NS3_WARNINGS_AS_ERRORS}) add_compile_options(/WX)