Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically create alias targets in cmake [ESD-2467] #124

Merged
Merged
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions SwiftTargets.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ define_property(TARGET
BRIEF_DOCS "Target's source directory"
FULL_DOCS "Identical use as SOURCE_DIR except that this applies to ALL target types, including INTERFACE")

set(project_alias "swiftnav" CACHE STRING "Alias for project")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't believe you need to make this a cache variable, the following will suffice:

set(SWIFTNAV_ALIAS swiftnav)

Also global variables are captialized


macro(swift_collate_arguments prefix name)
set(exclusion_list ${ARGN})
set(${name}_args "")
Expand Down Expand Up @@ -316,6 +318,10 @@ function(swift_add_target target type)
message(FATAL_ERROR "Unknown Swift target type ${type}")
endif()

if((type STREQUAL "library") OR (type STREQUAL "test_library") OR (type STREQUAL "tool_library"))
add_library(${project_alias}::${target} ALIAS ${target})
endif()

#
# This edge case is needed for cmake version < 3.19.0 where INTERFACE
# classes cannot contain any property other than those prefixed with
Expand Down Expand Up @@ -380,4 +386,35 @@ function(swift_validate_targets)
message(FATAL_ERROR "Can't identify type of target ${target}, was it added with the correct Swift function (swift_add_*)?")
endif()
endforeach()

# Check for the use of alias for swift dependencies.
swift_list_compilable_targets(all_targets ONLY_THIS_REPO SWIFT_TYPES)
foreach(target ${all_targets})
get_target_property(target_dependencies ${target} LINK_LIBRARIES)
if (NOT target_dependencies)
continue()
endif()

foreach(target_dependency ${target_dependencies})
if (TARGET ${target_dependency})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to avoid the nesting you use the following technique:

if (TARGET ${target_dependency})
  continue()
endif()

get_target_property(swift_dep_alias ${target_dependency} ALIASED_TARGET)

if (NOT swift_dep_alias)
  continue()
endif()

get_target_property(target_dep_type ${target_dependency} TYPE)
...

get_target_property(swift_dep_alias ${target_dependency} ALIASED_TARGET)
if (NOT swift_dep_alias)
get_target_property(target_dep_type ${target_dependency} TYPE)
if (target_dep_type STREQUAL "INTERFACE_LIBRARY")
get_target_property(swift_dep_type ${target_dependency} INTERFACE_SWIFT_TYPE)
else()
get_target_property(swift_dep_type ${target_dependency} SWIFT_TYPE)
endif()

if (swift_dep_type)
get_target_property(_swift_dep_alias ${project_alias}::${target_dependency} ALIASED_TARGET)
if (${_swift_dep_alias} STREQUAL ${target_dependency})
message(WARNING "Linking \"${target_dependency}\" as a dependency of target \"${target}\". Please use alias \"${project_alias}::${target_dependency}\" instead.")
endif()
endif()
endif()
endif()
endforeach()
endforeach()

endfunction()