Skip to content

Commit 1d3bbec

Browse files
committed
Use workaround function to write aliases to imported targets
CMake does not support calling add_library(name ALIAS lib) with an imported library as aliased library. See https://cmake.org/pipermail/cmake/2015-May/060576.html
1 parent 88f520d commit 1d3bbec

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

thirdparty/CMakeLists.txt

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,29 @@ function(update_submodule submodule)
44
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}")
55
endfunction()
66

7+
function(imported_target_alias ALIAS)
8+
# For some unknown reason CMake does not support creating alias
9+
# libraries from IMPORTED libraries. This function is an ugly workaround
10+
# to get the same
11+
12+
cmake_parse_arguments("__ALIAS"
13+
""
14+
"ALIAS"
15+
""
16+
${ARGN}
17+
)
18+
19+
if(NOT __ALIAS_ALIAS)
20+
message(FATAL_ERROR "imported_target_alias invoked with wrong arguments, missing ALIAS")
21+
endif()
22+
23+
add_library(${ALIAS} INTERFACE)
24+
target_link_libraries(${ALIAS} INTERFACE ${__ALIAS_ALIAS})
25+
endfunction()
26+
727
find_package(expected-lite QUIET)
828
if(expected-lite_FOUND)
9-
add_library(expected-lite ALIAS expected-lite::expected-lite)
29+
imported_target_alias(expected-lite ALIAS expected-lite::expected-lite)
1030
else()
1131
message(STATUS "expected-lite not found, using submodule")
1232
update_submodule(nonstd/expected-light)
@@ -15,7 +35,7 @@ endif()
1535

1636
find_package(variant-lite QUIET)
1737
if(variant-lite_FOUND)
18-
add_library(variant-lite ALIAS variant-lite::variant-lite)
38+
imported_target_alias(variant-lite ALIAS variant-lite::variant-lite)
1939
else()
2040
message(STATUS "variant-lite not found, using submodule")
2141
update_submodule(nonstd/variant-light)
@@ -27,7 +47,7 @@ endif()
2747

2848
find_package(value-ptr-lite QUIET)
2949
if(value-ptr-lite_FOUND)
30-
add_library(value-ptr-lite ALIAS value-ptr-lite::value-ptr-lite)
50+
imported_target_alias(value-ptr-lite ALIAS value-ptr-lite::value-ptr-lite)
3151
else()
3252
message(STATUS "value-ptr-lite not found, using submodule")
3353
update_submodule(nonstd/value-ptr-lite)
@@ -44,10 +64,10 @@ if(boost_filesystem_FOUND AND
4464
boost_algorithm_FOUND AND
4565
boost_variant_FOUND AND
4666
boost_optional_FOUND)
47-
add_library(boost_filesystem ALIAS boost_filesystem::boost_filesystem)
48-
add_library(boost_algorithm ALIAS boost_algorithm::boost_algorithm)
49-
add_library(boost_variant ALIAS boost_variant::boost_variant)
50-
add_library(boost_optional ALIAS boost_optional::boost_optional)
67+
imported_target_alias(boost_filesystem ALIAS boost_filesystem::boost_filesystem)
68+
imported_target_alias(boost_algorithm ALIAS boost_algorithm::boost_algorithm)
69+
imported_target_alias(boost_variant ALIAS boost_variant::boost_variant)
70+
imported_target_alias(boost_optional ALIAS boost_optional::boost_optional)
5171
else()
5272
message(STATUS "One or more boost modules not found, using submodule")
5373
update_submodule(boost)
@@ -77,7 +97,7 @@ if(JINJA2CPP_BUILD_TESTS)
7797
find_package(gtest QUIET)
7898

7999
if(gtest_FOUND)
80-
add_library(gtest ALIAS gtest::gtest)
100+
imported_target_alias(gtest ALIAS gtest::gtest)
81101
else()
82102
message(STATUS "expected-lite not found, using submodule")
83103
update_submodule(gtest)

0 commit comments

Comments
 (0)