CMakeLists.txt 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. cmake_minimum_required(VERSION 3.0)
  2. get_property(_isMultiConfig GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
  3. if(NOT _isMultiConfig AND NOT CMAKE_BUILD_TYPE)
  4. set(CMAKE_BUILD_TYPE Debug CACHE STRING "Choose the type of build")
  5. endif()
  6. project(ConfigSources CXX)
  7. # Source file(s) named with the configuration(s).
  8. file(GENERATE
  9. OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/config_$<CONFIG>.cpp"
  10. CONTENT [[
  11. #if defined(_WIN32) && defined(OBJ_SHARED)
  12. __declspec(dllexport)
  13. #endif
  14. void config_$<CONFIG>() {}
  15. ]]
  16. )
  17. # Per-config sources via INTERFACE_SOURCES.
  18. add_library(iface INTERFACE)
  19. target_sources(iface INTERFACE
  20. "${CMAKE_CURRENT_SOURCE_DIR}/iface_src.cpp"
  21. "$<$<CONFIG:Debug>:${CMAKE_CURRENT_SOURCE_DIR}/iface_debug_src.cpp>"
  22. "$<$<NOT:$<CONFIG:Debug>>:${CMAKE_CURRENT_SOURCE_DIR}/iface_other_src.cpp>"
  23. "$<$<CONFIG:NotAConfig>:${CMAKE_CURRENT_SOURCE_DIR}/does_not_exist.cpp>"
  24. )
  25. target_compile_definitions(iface INTERFACE
  26. "$<$<CONFIG:Debug>:CFG_DEBUG>"
  27. "$<$<NOT:$<CONFIG:Debug>>:CFG_OTHER>"
  28. )
  29. add_executable(ConfigSources
  30. $<$<CONFIG:Debug>:main_debug.cpp>
  31. $<$<NOT:$<CONFIG:Debug>>:main_other.cpp>
  32. $<$<CONFIG:NotAConfig>:does_not_exist.cpp>
  33. ${CMAKE_CURRENT_BINARY_DIR}/config_$<CONFIG>.cpp
  34. )
  35. target_link_libraries(ConfigSources iface)
  36. # Per-config sources via LINK_LIBRARIES.
  37. add_library(iface_debug INTERFACE)
  38. target_sources(iface_debug INTERFACE
  39. "${CMAKE_CURRENT_SOURCE_DIR}/iface_src.cpp"
  40. "${CMAKE_CURRENT_SOURCE_DIR}/iface_debug_src.cpp"
  41. )
  42. add_library(iface_other INTERFACE)
  43. target_sources(iface_other INTERFACE
  44. "${CMAKE_CURRENT_SOURCE_DIR}/iface_src.cpp"
  45. "${CMAKE_CURRENT_SOURCE_DIR}/iface_other_src.cpp"
  46. )
  47. add_executable(ConfigSourcesLink main.cpp)
  48. target_compile_definitions(ConfigSourcesLink PRIVATE
  49. "$<$<CONFIG:Debug>:CFG_DEBUG>"
  50. "$<$<NOT:$<CONFIG:Debug>>:CFG_OTHER>"
  51. )
  52. target_link_libraries(ConfigSourcesLink PRIVATE
  53. "$<$<CONFIG:Debug>:iface_debug>"
  54. "$<$<NOT:$<CONFIG:Debug>>:iface_other>"
  55. "$<$<CONFIG:NotAConfig>:iface_does_not_exist>"
  56. )
  57. # Per-config sources via INTERFACE_LINK_LIBRARIES.
  58. add_library(ConfigSourcesIface INTERFACE)
  59. target_link_libraries(ConfigSourcesIface INTERFACE
  60. "$<$<CONFIG:Debug>:iface_debug>"
  61. "$<$<NOT:$<CONFIG:Debug>>:iface_other>"
  62. "$<$<CONFIG:NotAConfig>:iface_does_not_exist>"
  63. )
  64. add_executable(ConfigSourcesLinkIface main.cpp)
  65. target_compile_definitions(ConfigSourcesLinkIface PRIVATE
  66. "$<$<CONFIG:Debug>:CFG_DEBUG>"
  67. "$<$<NOT:$<CONFIG:Debug>>:CFG_OTHER>"
  68. )
  69. target_link_libraries(ConfigSourcesLinkIface ConfigSourcesIface)
  70. # A target with sources in only one configuration that is not the
  71. # first in CMAKE_CONFIGURATION_TYPES.
  72. if(CMAKE_CONFIGURATION_TYPES MATCHES ";([^;]+)")
  73. set(one_config "${CMAKE_MATCH_1}")
  74. else()
  75. set(one_config "${CMAKE_BUILD_TYPE}")
  76. endif()
  77. add_library(OneConfigOnly OBJECT "$<$<CONFIG:${one_config}>:${CMAKE_CURRENT_SOURCE_DIR}/iface_src.cpp>")
  78. set_property(TARGET OneConfigOnly PROPERTY LINKER_LANGUAGE CXX)
  79. # ---------------------------------------------------------------------------
  80. # Makes sure that each configuration uses the correct generated file.
  81. add_library(ObjLibFromGeneratedSources OBJECT)
  82. set_property(TARGET ObjLibFromGeneratedSources PROPERTY POSITION_INDEPENDENT_CODE 1)
  83. target_compile_definitions(ObjLibFromGeneratedSources PRIVATE OBJ_SHARED)
  84. target_sources(ObjLibFromGeneratedSources PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/config_$<CONFIG>.cpp)
  85. add_library(SharedLibFromObjLibFromGeneratedSources SHARED shared.cpp)
  86. target_link_libraries(SharedLibFromObjLibFromGeneratedSources PRIVATE ObjLibFromGeneratedSources)