CMakeLists.txt 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. cmake_minimum_required(VERSION 3.14)
  2. cmake_policy(SET CMP0091 NEW)
  3. project(MSVCRuntimeLibrary)
  4. function(verify_combinations threads lang src)
  5. set(verify_tc_config_ Release)
  6. set(verify_tc_config_Debug Debug)
  7. set(verify_def_MultiThreaded -DVERIFY_MT)
  8. set(verify_def_Debug -DVERIFY_DEBUG)
  9. set(verify_def_DLL -DVERIFY_DLL)
  10. foreach(dbg "" Debug)
  11. foreach(dll "" DLL)
  12. # Construct the name of this runtime library combination.
  13. set(rtl "${threads}${dbg}${dll}")
  14. # Test that try_compile builds with this RTL.
  15. set(CMAKE_MSVC_RUNTIME_LIBRARY "${rtl}")
  16. set(CMAKE_TRY_COMPILE_CONFIGURATION "${verify_tc_config_${dbg}}")
  17. set(CMAKE_TRY_COMPILE_TARGET_TYPE "STATIC_LIBRARY")
  18. try_compile(${rtl}_COMPILES
  19. ${CMAKE_CURRENT_BINARY_DIR}/try_compile/${rtl}
  20. ${CMAKE_CURRENT_SOURCE_DIR}/${src}
  21. COMPILE_DEFINITIONS ${verify_def_${threads}} ${verify_def_${dbg}} ${verify_def_${dll}}
  22. OUTPUT_VARIABLE ${rtl}_OUTPUT
  23. )
  24. if(${rtl}_COMPILES)
  25. message(STATUS "try_compile with ${rtl} worked")
  26. else()
  27. string(REPLACE "\n" "\n " ${rtl}_OUTPUT " ${${rtl}_OUTPUT}")
  28. message(SEND_ERROR "try_compile with ${rtl} failed:\n${${rtl}_OUTPUT}")
  29. endif()
  30. # Test that targets build with this RTL.
  31. set(CMAKE_MSVC_RUNTIME_LIBRARY "$<$<BOOL:$<TARGET_PROPERTY:BOOL_TRUE>>:${rtl}>$<$<BOOL:$<TARGET_PROPERTY:BOOL_FALSE>>:BadContent>")
  32. add_library(${rtl}-${lang} ${src})
  33. set_property(TARGET ${rtl}-${lang} PROPERTY BOOL_TRUE TRUE)
  34. target_compile_definitions(${rtl}-${lang} PRIVATE ${verify_def_${threads}} ${verify_def_${dbg}} ${verify_def_${dll}})
  35. endforeach()
  36. endforeach()
  37. endfunction()
  38. function(verify lang src)
  39. add_library(default-${lang} ${src})
  40. target_compile_definitions(default-${lang} PRIVATE VERIFY_MT VERIFY_DLL "$<$<CONFIG:Debug>:VERIFY_DEBUG>")
  41. verify_combinations(MultiThreaded ${lang} ${src})
  42. endfunction()
  43. verify(C verify.c)
  44. verify(CXX verify.cxx)