Skip to content

Commit cd795ab

Browse files
committed
Guard add_library calls in Findtest-drive.cmake
Add if(NOT TARGET) guards around add_library calls to prevent conflicts when the target already exists (e.g., when using stdlib with FetchContent after PR fortran-lang#1033 which adds ALIAS targets automatically). This allows users to use the Find module pattern with libraries that already provide namespaced ALIAS targets.
1 parent 6c56750 commit cd795ab

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

config/cmake/Findtest-drive.cmake

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -88,17 +88,19 @@ foreach(method ${${_pkg}_FIND_METHOD})
8888
if("${_pkg}_FOUND")
8989
message(STATUS "Found ${_lib} via pkg-config")
9090

91-
add_library("${_lib}::${_lib}" INTERFACE IMPORTED)
92-
target_link_libraries(
93-
"${_lib}::${_lib}"
94-
INTERFACE
95-
"${${_pkg}_LINK_LIBRARIES}"
96-
)
97-
target_include_directories(
98-
"${_lib}::${_lib}"
99-
INTERFACE
100-
"${${_pkg}_INCLUDE_DIRS}"
101-
)
91+
if(NOT TARGET "${_lib}::${_lib}")
92+
add_library("${_lib}::${_lib}" INTERFACE IMPORTED)
93+
target_link_libraries(
94+
"${_lib}::${_lib}"
95+
INTERFACE
96+
"${${_pkg}_LINK_LIBRARIES}"
97+
)
98+
target_include_directories(
99+
"${_lib}::${_lib}"
100+
INTERFACE
101+
"${${_pkg}_INCLUDE_DIRS}"
102+
)
103+
endif()
102104

103105
break()
104106
endif()
@@ -118,8 +120,10 @@ foreach(method ${${_pkg}_FIND_METHOD})
118120
"${${_pkg}_BINARY_DIR}"
119121
)
120122

121-
add_library("${_lib}::${_lib}" INTERFACE IMPORTED)
122-
target_link_libraries("${_lib}::${_lib}" INTERFACE "${_lib}")
123+
if(NOT TARGET "${_lib}::${_lib}")
124+
add_library("${_lib}::${_lib}" INTERFACE IMPORTED)
125+
target_link_libraries("${_lib}::${_lib}" INTERFACE "${_lib}")
126+
endif()
123127

124128
# We need the module directory in the subproject before we finish the configure stage
125129
if(NOT EXISTS "${${_pkg}_BINARY_DIR}/include")
@@ -140,8 +144,10 @@ foreach(method ${${_pkg}_FIND_METHOD})
140144
)
141145
FetchContent_MakeAvailable("${_lib}")
142146

143-
add_library("${_lib}::${_lib}" INTERFACE IMPORTED)
144-
target_link_libraries("${_lib}::${_lib}" INTERFACE "${_lib}")
147+
if(NOT TARGET "${_lib}::${_lib}")
148+
add_library("${_lib}::${_lib}" INTERFACE IMPORTED)
149+
target_link_libraries("${_lib}::${_lib}" INTERFACE "${_lib}")
150+
endif()
145151

146152
# We need the module directory in the subproject before we finish the configure stage
147153
FetchContent_GetProperties("${_lib}" SOURCE_DIR "${_pkg}_SOURCE_DIR")

0 commit comments

Comments
 (0)