Skip to content

Commit

Permalink
Fix: [MSVC] generate static libraries / executables correctly with vc…
Browse files Browse the repository at this point in the history
…pkg -static target
  • Loading branch information
TrueBrain committed Aug 18, 2023
1 parent 5ac333c commit f2a9a30
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 17 deletions.
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,14 @@ set_target_properties(openttd PROPERTIES OUTPUT_NAME "${BINARY_NAME}")
if(MSVC)
# Add DPI manifest to project; other WIN32 targets get this via ottdres.rc
target_sources(openttd PRIVATE "${CMAKE_SOURCE_DIR}/os/windows/openttd.manifest")

# If target -static is used, switch our project to static (/MT) too.
# If the target ends on -static-md, it will remain dynamic (/MD).
if(VCPKG_TARGET_TRIPLET MATCHES "-static" AND NOT VCPKG_TARGET_TRIPLET MATCHES "-md")
set_property(TARGET openttd_lib PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
set_property(TARGET openttd PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
set_property(TARGET openttd_test PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
endif()
endif()

target_precompile_headers(openttd_lib
Expand Down
17 changes: 0 additions & 17 deletions cmake/CompileFlags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,6 @@
#
macro(compile_flags)
if(MSVC)
if(VCPKG_TARGET_TRIPLET MATCHES "-static" AND NOT VCPKG_TARGET_TRIPLET MATCHES "-md")
# Switch to MT (static) instead of MD (dynamic) binary

# For MSVC two generators are available
# - a command line generator (Ninja) using CMAKE_BUILD_TYPE to specify the
# configuration of the build tree
# - an IDE generator (Visual Studio) using CMAKE_CONFIGURATION_TYPES to
# specify all configurations that will be available in the generated solution
list(APPEND MSVC_CONFIGS "${CMAKE_BUILD_TYPE}" "${CMAKE_CONFIGURATION_TYPES}")

# Set usage of static runtime for all configurations
foreach(MSVC_CONFIG ${MSVC_CONFIGS})
string(TOUPPER "CMAKE_CXX_FLAGS_${MSVC_CONFIG}" MSVC_FLAGS)
string(REPLACE "/MD" "/MT" ${MSVC_FLAGS} "${${MSVC_FLAGS}}")
endforeach()
endif()

# "If /Zc:rvalueCast is specified, the compiler follows section 5.4 of the
# C++11 standard". We need C++11 for the way we use threads.
add_compile_options(/Zc:rvalueCast)
Expand Down

0 comments on commit f2a9a30

Please sign in to comment.