aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKyle Edwards <kyle.edwards@kitware.com>2020-04-27 13:26:37 -0400
committerKyle Edwards <kyle.edwards@kitware.com>2020-04-27 13:47:58 -0400
commit8c74d8b198b99e0430702813f3485ca7a444eacc (patch)
tree9afa1abb592f0359b2351cb049009d21f4b06ac8
parent5e64076af9691ce8100aedf7a61cb338f2596151 (diff)
downloadpugixml-8c74d8b198b99e0430702813f3485ca7a444eacc.zip
pugixml-8c74d8b198b99e0430702813f3485ca7a444eacc.tar.gz
pugixml-8c74d8b198b99e0430702813f3485ca7a444eacc.tar.bz2
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.
-rw-r--r--CMakeLists.txt128
1 files changed, 57 insertions, 71 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index d02a01b..704ff43 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -64,24 +64,63 @@ if (CMAKE_VERSION VERSION_LESS 3.15)
set(msvc-rt-mt-static $<${msvc-rt-mt-static}:-MT>)
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}>)
-add_library(pugixml) # Auto selects static or shared based on BUILD_SHARED_LIBS
-add_library(pugixml-shared SHARED)
-add_library(pugixml-static STATIC)
+set(libs)
-add_library(pugixml::pugixml ALIAS pugixml)
-add_library(pugixml::shared ALIAS pugixml-shared)
-add_library(pugixml::static ALIAS pugixml-static)
+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)
+ list(APPEND libs pugixml-shared)
+
+ set_property(TARGET pugixml-shared PROPERTY EXPORT_NAME shared)
+ 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()
-# 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-static PROPERTY EXPORT_NAME static)
+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()
-set_target_properties(pugixml-shared pugixml-static pugixml
+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
MSVC_RUNTIME_LIBRARY ${CMAKE_MSVC_RUNTIME_LIBRARY}
EXCLUDE_FROM_ALL ON
@@ -90,63 +129,10 @@ set_target_properties(pugixml-shared pugixml-static pugixml
VERSION ${PROJECT_VERSION}
OUTPUT_NAME pugixml)
-# XXX: EXCLUDE_FROM_ALL cannot be set via a generator expression! :(
-if (BUILD_SHARED_AND_STATIC_LIBS)
- set_target_properties(pugixml-shared pugixml-static
- PROPERTIES
- EXCLUDE_FROM_ALL OFF)
- set(install-targets pugixml-shared pugixml-static)
-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})
+set_target_properties(${libs}
+ PROPERTIES
+ EXCLUDE_FROM_ALL OFF)
+set(install-targets pugixml ${libs})
configure_package_config_file(
"${PROJECT_SOURCE_DIR}/scripts/pugixml-config.cmake.in"
@@ -208,5 +194,5 @@ if (BUILD_TESTS)
add_dependencies(check pugixml-check)
target_link_libraries(pugixml-check
PRIVATE
- $<IF:${build-both},pugixml::static,pugixml::pugixml>)
+ pugixml::pugixml)
endif()