build-cpp / cmkr

Modern build system based on CMake and TOML.
https://cmkr.build
MIT License
425 stars 28 forks source link

[feature] Add 'type = "msvc-static"' #63

Closed gmh5225 closed 1 year ago

gmh5225 commented 1 year ago

There are times when we want to use /MT instead of /MD The following code can do this by replacing /MD with /MT

# Enable static msvc runtime.
if (MSVC)
    set(CompilerFlags
        CMAKE_CXX_FLAGS
        CMAKE_CXX_FLAGS_DEBUG
        CMAKE_CXX_FLAGS_RELEASE
        CMAKE_CXX_FLAGS_MINSIZEREL
        CMAKE_CXX_FLAGS_RELWITHDEBINFO
        CMAKE_C_FLAGS
        CMAKE_C_FLAGS_DEBUG
        CMAKE_C_FLAGS_RELEASE
        CMAKE_C_FLAGS_MINSIZEREL
        CMAKE_C_FLAGS_RELWITHDEBINFO
        )
    foreach(CompilerFlag ${CompilerFlags})
        string(REPLACE "/MD" "/MT" ${CompilerFlag} "${${CompilerFlag}}")
        string(REPLACE "/MDd" "/MTd" ${CompilerFlag} "${${CompilerFlag}}")
        set(${CompilerFlag} "${${CompilerFlag}}" CACHE STRING "msvc compiler flags" FORCE)
        message("MSVC flags: ${CompilerFlag}:${${CompilerFlag}}")
    endforeach()
endif(MSVC)

But it's too much trouble to copy and paste every time If possible, add an msvc-static type, it would be much easier :)

[target.hello_world]
type = "msvc-static"
sources = ["src/main.cpp"]
mrexodia commented 1 year ago

Doing it this way is super incorrect. You should use https://cmake.org/cmake/help/latest/prop_tgt/MSVC_RUNTIME_LIBRARY.html (and enable the relevant policy) instead.

Still might be nice to build into cmkr though.

gmh5225 commented 1 year ago

I see. Thank you for your reply. Because set_property(TARGET foo PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>") does not work well in Z3 project. image The replacement method works well.

mrexodia commented 1 year ago

Yeah you need to enable https://cmake.org/cmake/help/latest/policy/CMP0091.html#policy:CMP0091 before the project() call otherwise it does nothing

gmh5225 commented 1 year ago

My bad. Looks like my version of cmake is too old. Thank you for your reply. set_property(TARGET foo PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>") works well.