Skip to content

Commit 2ab46ff

Browse files
authored
Automatically create alias targets in cmake [ESD-2467] (#124)
* Update function swift_add_target() to automatically create alias for all library targets, when calling swift_add_***_library(). * Update function swift_validate_targets() to return warning if swift target links to "not alias" dependencies.
1 parent ea15fa9 commit 2ab46ff

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

SwiftTargets.cmake

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ define_property(TARGET
184184
BRIEF_DOCS "Target's source directory"
185185
FULL_DOCS "Identical use as SOURCE_DIR except that this applies to ALL target types, including INTERFACE")
186186

187+
set(SWIFTNAV_ALIAS "swiftnav" CACHE STRING "Alias for Swiftnav's projects")
188+
187189
macro(swift_collate_arguments prefix name)
188190
set(exclusion_list ${ARGN})
189191
set(${name}_args "")
@@ -316,6 +318,10 @@ function(swift_add_target target type)
316318
message(FATAL_ERROR "Unknown Swift target type ${type}")
317319
endif()
318320

321+
if((type STREQUAL "library") OR (type STREQUAL "test_library") OR (type STREQUAL "tool_library"))
322+
add_library(${SWIFTNAV_ALIAS}::${target} ALIAS ${target})
323+
endif()
324+
319325
#
320326
# This edge case is needed for cmake version < 3.19.0 where INTERFACE
321327
# classes cannot contain any property other than those prefixed with
@@ -380,4 +386,50 @@ function(swift_validate_targets)
380386
message(FATAL_ERROR "Can't identify type of target ${target}, was it added with the correct Swift function (swift_add_*)?")
381387
endif()
382388
endforeach()
389+
390+
# Check for the use of alias for swift dependencies.
391+
swift_list_compilable_targets(all_targets ONLY_THIS_REPO SWIFT_TYPES)
392+
foreach(target ${all_targets})
393+
get_target_property(target_dependencies ${target} LINK_LIBRARIES)
394+
if (NOT target_dependencies)
395+
continue()
396+
endif()
397+
398+
foreach(target_dependency ${target_dependencies})
399+
if (NOT TARGET ${target_dependency})
400+
continue()
401+
endif()
402+
403+
get_target_property(swift_dep_alias ${target_dependency} ALIASED_TARGET)
404+
if (swift_dep_alias)
405+
continue()
406+
endif()
407+
408+
get_target_property(target_dep_type ${target_dependency} TYPE)
409+
if (target_dep_type STREQUAL "INTERFACE_LIBRARY")
410+
get_target_property(swift_dep_type ${target_dependency} INTERFACE_SWIFT_TYPE)
411+
else()
412+
get_target_property(swift_dep_type ${target_dependency} SWIFT_TYPE)
413+
endif()
414+
415+
if (NOT swift_dep_type)
416+
continue()
417+
endif()
418+
419+
# Check if swift's ${target_dependency} has an ALIAS following the pattern ${SWIFTNAV_ALIAS}::${target_dependency}
420+
if (NOT TARGET ${SWIFTNAV_ALIAS}::${target_dependency})
421+
continue()
422+
endif()
423+
424+
get_target_property(_swift_dep_alias ${SWIFTNAV_ALIAS}::${target_dependency} ALIASED_TARGET)
425+
if (NOT _swift_dep_alias)
426+
continue()
427+
endif()
428+
429+
if (${_swift_dep_alias} STREQUAL ${target_dependency})
430+
message(WARNING "Linking \"${target_dependency}\" as a dependency of target \"${target}\". Please use alias \"${SWIFTNAV_ALIAS}::${target_dependency}\" instead.")
431+
endif()
432+
endforeach()
433+
endforeach()
434+
383435
endfunction()

0 commit comments

Comments
 (0)