Fix multiple-rules-for-target error
The old logic for BUILD_SHARED_LIBS and BUILD_SHARED_AND_STATIC_LIBS would produce two targets with the same name and different build rules. The Makefile generator tolerated this, but the Ninja generator raised an error. Fix the logic so that only one shared and one static target gets built, and make the pugixml target an ALIAS of the one dictated by BUILD_SHARED_LIBS.
This commit is contained in:
parent
5e64076af9
commit
8c74d8b198
120
CMakeLists.txt
120
CMakeLists.txt
@ -64,24 +64,63 @@ if (CMAKE_VERSION VERSION_LESS 3.15)
|
|||||||
set(msvc-rt-mt-static $<${msvc-rt-mt-static}:-MT>)
|
set(msvc-rt-mt-static $<${msvc-rt-mt-static}:-MT>)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
set(build-shared $<BOOL:${BUILD_SHARED_LIBS}>)
|
|
||||||
set(build-both $<BOOL:${BUILD_SHARED_AND_STATIC_LIBS}>)
|
|
||||||
set(versioned-dir $<$<BOOL:${USE_VERSIONED_LIBDIR}>:/pugixml-${PROJECT_VERSION}>)
|
set(versioned-dir $<$<BOOL:${USE_VERSIONED_LIBDIR}>:/pugixml-${PROJECT_VERSION}>)
|
||||||
|
|
||||||
add_library(pugixml) # Auto selects static or shared based on BUILD_SHARED_LIBS
|
set(libs)
|
||||||
add_library(pugixml-shared SHARED)
|
|
||||||
add_library(pugixml-static STATIC)
|
|
||||||
|
|
||||||
add_library(pugixml::pugixml ALIAS pugixml)
|
if (BUILD_SHARED_LIBS OR BUILD_SHARED_AND_STATIC_LIBS)
|
||||||
|
add_library(pugixml-shared SHARED
|
||||||
|
${PROJECT_SOURCE_DIR}/scripts/pugixml_dll.rc
|
||||||
|
${PROJECT_SOURCE_DIR}/src/pugixml.cpp)
|
||||||
add_library(pugixml::shared ALIAS pugixml-shared)
|
add_library(pugixml::shared ALIAS pugixml-shared)
|
||||||
add_library(pugixml::static ALIAS pugixml-static)
|
list(APPEND libs pugixml-shared)
|
||||||
|
|
||||||
# This means you can use pugixml::shared when a `find_package(pugixml CONFIG)`
|
|
||||||
# is called. This keeps it consistent with the aliases we provide.
|
|
||||||
set_property(TARGET pugixml-shared PROPERTY EXPORT_NAME shared)
|
set_property(TARGET pugixml-shared PROPERTY EXPORT_NAME shared)
|
||||||
set_property(TARGET pugixml-static PROPERTY EXPORT_NAME static)
|
target_include_directories(pugixml-shared
|
||||||
|
PUBLIC
|
||||||
|
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/src>)
|
||||||
|
target_compile_definitions(pugixml-shared
|
||||||
|
PRIVATE
|
||||||
|
$<$<CXX_COMPILER_ID:MSVC>:PUGIXML_API=__declspec\(dllexport\)>)
|
||||||
|
target_compile_options(pugixml-shared
|
||||||
|
PRIVATE
|
||||||
|
${msvc-rt-mtd-shared}
|
||||||
|
${msvc-rt-mtd-static}
|
||||||
|
${msvc-rt-mt-shared}
|
||||||
|
${msvc-rt-mt-static})
|
||||||
|
endif()
|
||||||
|
|
||||||
set_target_properties(pugixml-shared pugixml-static pugixml
|
if (NOT BUILD_SHARED_LIBS OR BUILD_SHARED_AND_STATIC_LIBS)
|
||||||
|
add_library(pugixml-static STATIC
|
||||||
|
${PROJECT_SOURCE_DIR}/src/pugixml.cpp)
|
||||||
|
add_library(pugixml::static ALIAS pugixml-static)
|
||||||
|
list(APPEND libs pugixml-static)
|
||||||
|
|
||||||
|
set_property(TARGET pugixml-static PROPERTY EXPORT_NAME static)
|
||||||
|
target_include_directories(pugixml-static
|
||||||
|
PUBLIC
|
||||||
|
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/src>)
|
||||||
|
target_compile_definitions(pugixml-static
|
||||||
|
PRIVATE
|
||||||
|
$<$<CXX_COMPILER_ID:MSVC>:PUGIXML_API=__declspec\(dllexport\)>)
|
||||||
|
target_compile_options(pugixml-static
|
||||||
|
PRIVATE
|
||||||
|
${msvc-rt-mtd-shared}
|
||||||
|
${msvc-rt-mtd-static}
|
||||||
|
${msvc-rt-mt-shared}
|
||||||
|
${msvc-rt-mt-static})
|
||||||
|
endif()
|
||||||
|
|
||||||
|
if (BUILD_SHARED_LIBS)
|
||||||
|
set(pugixml-alias pugixml-shared)
|
||||||
|
else()
|
||||||
|
set(pugixml-alias pugixml-static)
|
||||||
|
endif()
|
||||||
|
add_library(pugixml INTERFACE)
|
||||||
|
target_link_libraries(pugixml INTERFACE ${pugixml-alias})
|
||||||
|
add_library(pugixml::pugixml ALIAS pugixml)
|
||||||
|
|
||||||
|
set_target_properties(${libs}
|
||||||
PROPERTIES
|
PROPERTIES
|
||||||
MSVC_RUNTIME_LIBRARY ${CMAKE_MSVC_RUNTIME_LIBRARY}
|
MSVC_RUNTIME_LIBRARY ${CMAKE_MSVC_RUNTIME_LIBRARY}
|
||||||
EXCLUDE_FROM_ALL ON
|
EXCLUDE_FROM_ALL ON
|
||||||
@ -90,63 +129,10 @@ set_target_properties(pugixml-shared pugixml-static pugixml
|
|||||||
VERSION ${PROJECT_VERSION}
|
VERSION ${PROJECT_VERSION}
|
||||||
OUTPUT_NAME pugixml)
|
OUTPUT_NAME pugixml)
|
||||||
|
|
||||||
# XXX: EXCLUDE_FROM_ALL cannot be set via a generator expression! :(
|
set_target_properties(${libs}
|
||||||
if (BUILD_SHARED_AND_STATIC_LIBS)
|
|
||||||
set_target_properties(pugixml-shared pugixml-static
|
|
||||||
PROPERTIES
|
PROPERTIES
|
||||||
EXCLUDE_FROM_ALL OFF)
|
EXCLUDE_FROM_ALL OFF)
|
||||||
set(install-targets pugixml-shared pugixml-static)
|
set(install-targets pugixml ${libs})
|
||||||
else()
|
|
||||||
set_target_properties(pugixml
|
|
||||||
PROPERTIES
|
|
||||||
EXCLUDE_FROM_ALL OFF)
|
|
||||||
set(install-targets pugixml)
|
|
||||||
endif()
|
|
||||||
|
|
||||||
target_sources(pugixml-shared
|
|
||||||
PRIVATE
|
|
||||||
$<${build-shared}:${PROJECT_SOURCE_DIR}/scripts/pugixml_dll.rc>
|
|
||||||
${PROJECT_SOURCE_DIR}/src/pugixml.cpp)
|
|
||||||
target_sources(pugixml-static
|
|
||||||
PRIVATE
|
|
||||||
${PROJECT_SOURCE_DIR}/src/pugixml.cpp)
|
|
||||||
target_sources(pugixml
|
|
||||||
PRIVATE
|
|
||||||
$<${build-shared}:${PROJECT_SOURCE_DIR}/scripts/pugixml_dll.rc>
|
|
||||||
${PROJECT_SOURCE_DIR}/src/pugixml.cpp)
|
|
||||||
|
|
||||||
# XXX: INSTALL_INTERFACE is not used here so that INCLUDES DESTINATION can work
|
|
||||||
# correctly if USE_VERSIONED_LIBDIR is set. $<INSTALL_INTERFACE> can't expand
|
|
||||||
# generator expressions otherwise.
|
|
||||||
target_include_directories(pugixml-shared
|
|
||||||
PUBLIC
|
|
||||||
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/src>)
|
|
||||||
target_include_directories(pugixml-static
|
|
||||||
PUBLIC
|
|
||||||
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/src>)
|
|
||||||
target_include_directories(pugixml
|
|
||||||
PUBLIC
|
|
||||||
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/src>)
|
|
||||||
|
|
||||||
target_compile_definitions(pugixml-shared
|
|
||||||
PRIVATE
|
|
||||||
$<$<CXX_COMPILER_ID:MSVC>:PUGIXML_API=__declspec\(dllexport\)>)
|
|
||||||
target_compile_definitions(pugixml
|
|
||||||
PRIVATE
|
|
||||||
$<$<CXX_COMPILER_ID:MSVC>:PUGIXML_API=__declspec\(dllexport\)>)
|
|
||||||
|
|
||||||
target_compile_options(pugixml-shared
|
|
||||||
PRIVATE
|
|
||||||
${msvc-rt-mtd-shared}
|
|
||||||
${msvc-rt-mtd-static}
|
|
||||||
${msvc-rt-mt-shared}
|
|
||||||
${msvc-rt-mt-static})
|
|
||||||
target_compile_options(pugixml
|
|
||||||
PRIVATE
|
|
||||||
${msvc-rt-mtd-shared}
|
|
||||||
${msvc-rt-mtd-static}
|
|
||||||
${msvc-rt-mt-shared}
|
|
||||||
${msvc-rt-mt-static})
|
|
||||||
|
|
||||||
configure_package_config_file(
|
configure_package_config_file(
|
||||||
"${PROJECT_SOURCE_DIR}/scripts/pugixml-config.cmake.in"
|
"${PROJECT_SOURCE_DIR}/scripts/pugixml-config.cmake.in"
|
||||||
@ -208,5 +194,5 @@ if (BUILD_TESTS)
|
|||||||
add_dependencies(check pugixml-check)
|
add_dependencies(check pugixml-check)
|
||||||
target_link_libraries(pugixml-check
|
target_link_libraries(pugixml-check
|
||||||
PRIVATE
|
PRIVATE
|
||||||
$<IF:${build-both},pugixml::static,pugixml::pugixml>)
|
pugixml::pugixml)
|
||||||
endif()
|
endif()
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user