|
1 |
| - |
2 |
| -macro(configure_msvc_runtime) |
3 |
| - if(MSVC) |
4 |
| - # Default to statically-linked runtime. |
5 |
| - if("${MSVC_RUNTIME}" STREQUAL "") |
6 |
| - set(MSVC_RUNTIME "dynamic") |
| 1 | +if (WIN32) |
| 2 | + if (CMAKE_VERSION VERSION_LESS 3.15.0) |
| 3 | + message(FATAL_ERROR "windows builds require CMake >= 3.15") |
| 4 | + endif() |
| 5 | + if(NOT DEFINED CMAKE_MSVC_RUNTIME_LIBRARY) |
| 6 | + if (MSVC_RUNTIME STREQUAL "dynamic") |
| 7 | + set(MSVC_DEFAULT_RUNTIME "MultiThreadedDLL") |
| 8 | + elseif(MSVC_RUNTIME STREQUAL "static") |
| 9 | + set(MSVC_DEFAULT_RUNTIME "MultiThreaded") |
| 10 | + else() |
| 11 | + message(WARNING "invalid MSVC_RUNTIME (deprecated) value '${MSVC_RUNTIME}', ignoring") |
7 | 12 | endif()
|
8 | 13 |
|
9 |
| - # Set compiler options. |
10 |
| - set(variables |
11 |
| - CMAKE_C_FLAGS |
12 |
| - CMAKE_C_FLAGS_DEBUG |
13 |
| - CMAKE_C_FLAGS_MINSIZEREL |
14 |
| - CMAKE_C_FLAGS_RELEASE |
15 |
| - CMAKE_C_FLAGS_RELWITHDEBINFO |
16 |
| - CMAKE_CXX_FLAGS |
17 |
| - CMAKE_CXX_FLAGS_DEBUG |
18 |
| - CMAKE_CXX_FLAGS_MINSIZEREL |
19 |
| - CMAKE_CXX_FLAGS_RELEASE |
20 |
| - CMAKE_CXX_FLAGS_RELWITHDEBINFO) |
21 |
| - |
22 |
| - if(${MSVC_RUNTIME} STREQUAL "static") |
23 |
| - message(STATUS "MSVC: using statically-linked runtime (/MT and /MTd).") |
24 |
| - foreach(variable ${variables}) |
25 |
| - if(${variable} MATCHES "/MD") |
26 |
| - string(REGEX REPLACE "/MD" "/MT" ${variable} "${${variable}}") |
27 |
| - endif() |
28 |
| - endforeach() |
| 14 | + if(MSVC_DEFAULT_RUNTIME) |
| 15 | + message("Using CMAKE_MSVC_RUNTIME_LIBRARY=${MSVC_DEFAULT_RUNTIME} (derived from MSVC_RUNTIME (deprecated) value '${MSVC_RUNTIME}')" ) |
29 | 16 | else()
|
30 |
| - message(STATUS "MSVC: using dynamically-linked runtime (/MD and /MDd).") |
31 |
| - foreach(variable ${variables}) |
32 |
| - if(${variable} MATCHES "/MT") |
33 |
| - string(REGEX REPLACE "/MT" "/MD" ${variable} "${${variable}}") |
34 |
| - endif() |
35 |
| - endforeach() |
36 |
| - endif() |
| 17 | + set(MSVC_DEFAULT_RUNTIME "MultiThreaded") |
| 18 | + |
| 19 | + if (CMAKE_BUILD_TYPE STREQUAL "Debug") |
| 20 | + string(APPEND MSVC_DEFAULT_RUNTIME "Debug") |
| 21 | + endif() |
37 | 22 |
|
38 |
| - foreach(variable ${variables}) |
39 |
| - if(${variable} MATCHES "/Ob0") |
40 |
| - string(REGEX REPLACE "/Ob0" "/Ob2" ${variable} "${${variable}}") |
| 23 | + if (BUILD_SHARED_LIBS) |
| 24 | + string(APPEND MSVC_DEFAULT_RUNTIME "DLL") |
41 | 25 | endif()
|
42 |
| - endforeach() |
| 26 | + message("Using CMAKE_MSVC_RUNTIME_LIBRARY=${MSVC_DEFAULT_RUNTIME}" ) |
| 27 | + endif() |
| 28 | + |
| 29 | + set(CMAKE_MSVC_RUNTIME_LIBRARY ${MSVC_DEFAULT_RUNTIME} CACHE STRING "MSVC runtime") |
| 30 | + endif() |
| 31 | + |
| 32 | + message("build is using MSVC runtime ${CMAKE_MSVC_RUNTIME_LIBRARY}") |
| 33 | + |
| 34 | + string(FIND ${CMAKE_MSVC_RUNTIME_LIBRARY} "DLL" IS_SHARED) |
| 35 | + if(IS_SHARED STREQUAL "-1") |
| 36 | + if(BUILD_SHARED_LIBS) |
| 37 | + message(FATAL_ERROR "Static CRT is only supported in a fully static build") |
| 38 | + endif() |
| 39 | + message(STATUS "Use the MSVC static runtime option carefully!") |
| 40 | + message(STATUS "OpenSSL uses /MD by default, and is very picky") |
| 41 | + message(STATUS "Random freeing errors are a common sign of runtime issues") |
| 42 | + endif() |
43 | 43 |
|
44 |
| - foreach(variable ${variables}) |
45 |
| - set(${variable} "${${variable}}" CACHE STRING "MSVC_${variable}" FORCE) |
46 |
| - endforeach() |
| 44 | + if(NOT DEFINED CMAKE_SUPPRESS_REGENERATION) |
| 45 | + set(CMAKE_SUPPRESS_REGENERATION ON) |
47 | 46 | endif()
|
48 |
| -endmacro(configure_msvc_runtime) |
| 47 | +endif() |
0 commit comments