-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathListTargets.cmake
128 lines (112 loc) · 4.6 KB
/
ListTargets.cmake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#
# Copyright (C) 2021 Swift Navigation Inc. Contact: Swift Navigation <[email protected]>
#
# This source is subject to the license found in the file 'LICENSE' which must be be distributed together with this source. All other rights reserved.
#
# THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY
# AND/OR FITNESS FOR A PARTICULAR PURPOSE.
#
#
# A helper module to enumerate all targets created in a source tree
#
# Calling swift_list_targets will populate the given variable with the list of targets which meet the given criteria. It will report all targets created in the current source dir
# and all of its subdirectories
#
# Use the ONLY_THIS_REPO option to exclude targets which were created in submodules, returns a list of targets which were created in the caller's repository. This is fairly dumb
# test which simply looks for 'third_party' anywhere in the path of the candidate target and excludes it
#
# Use the TYPES option to filter the returned list of targets based on the cmake target type (EXECUTABLE, DYNAMIC_LIBRARY and so on). Only targets which match one of the entries
# in this list will be returned
#
# Use the SWIFT_TYPES option to filter the returned list of targets based on the Swift target type. This should be a list of target types which match one of the 'swift_add_*'
# functions from SwiftTargets.cmake and TestTargets.cmake (ie, "executable", "library", "test", and so on)
#
# Example usage:
#
# Populate 'interfaces' with the set of interface library targets created within this repository
#
# swift_list_targets(interfaces ONLY_THIS_REPO TYPES "INTERFACE")
#
# Populate 'all_tests' with the set of tests defined anywhere within the source tree under the calling function's directory
#
# swift_list_targets(all_tests SWIFT_TYPE "test")
#
# A helper function swift_list_compilable_targets is provided which is equivalent to calling swift_list_targets with the option "TYPES EXECUTABLE DYNAMIC_LIBRARY STATIC_LIBRARY
# OBJECT_LIBRARY", ie the set of all targets which can be compiled (not interface libraries). Other options can be passed in to this function (ONLY_THIS_REPO, SWIFT_TYPES) as for
# swift_list_targets
#
cmake_minimum_required(VERSION 3.7)
function(get_all_targets result dir)
get_property(
subdirs
DIRECTORY "${dir}"
PROPERTY SUBDIRECTORIES)
foreach(subdir IN LISTS subdirs)
get_all_targets(${result} "${subdir}")
endforeach()
get_directory_property(sub_targets DIRECTORY "${dir}" BUILDSYSTEM_TARGETS)
set(${result}
${${result}} ${sub_targets}
PARENT_SCOPE)
endfunction()
function(swift_list_targets out_var)
set(argOption "ONLY_THIS_REPO")
set(argSingle "")
set(argMulti "TYPES" "SWIFT_TYPES")
cmake_parse_arguments(x "${argOption}" "${argSingle}" "${argMulti}" ${ARGN})
if(x_UNPARSED_ARGUMENTS)
message(FATAL_ERROR "Unparsed arguments to swift_list_targets ${x_UNPARSED_ARGUMENTS}")
endif()
set(x_all_targets)
get_all_targets(x_all_targets ${CMAKE_CURRENT_SOURCE_DIR})
set(all_targets)
foreach(target IN LISTS x_all_targets)
get_target_property(type ${target} TYPE)
if(x_TYPES)
if(NOT ${type} IN_LIST x_TYPES)
continue()
endif()
endif()
if(x_SWIFT_TYPES)
if (type STREQUAL "INTERFACE_LIBRARY")
get_target_property(swift_type ${target} INTERFACE_SWIFT_TYPE)
else()
get_target_property(swift_type ${target} SWIFT_TYPE)
endif()
if(NOT ${swift_type} IN_LIST x_SWIFT_TYPES)
continue()
endif()
endif()
if(x_ONLY_THIS_REPO)
if (type STREQUAL "INTERFACE_LIBRARY")
get_target_property(target_dir ${target} INTERFACE_SOURCE_DIR)
else()
get_target_property(target_dir ${target} SOURCE_DIR)
endif()
# This replacement makes sure that we only filter out third_party subdirectories which actually exist in the root project source dir - ie, a git repo cloned in to a path
# which just so happens to contain third_party should not break this function
string(REPLACE ${CMAKE_SOURCE_DIR} "" target_dir ${target_dir})
if(${target_dir} MATCHES ".*third_party.*")
continue()
endif()
endif()
set(all_targets ${all_targets} ${target})
endforeach()
set(${out_var}
${all_targets}
PARENT_SCOPE)
endfunction()
function(swift_list_compilable_targets out_var)
swift_list_targets(
${out_var}
TYPES
"EXECUTABLE"
"MODULE_LIBRARY"
"SHARED_LIBRARY"
"STATIC_LIBRARY"
"OBJECT_LIBRARY"
${ARGN})
set(${out_var}
${${out_var}}
PARENT_SCOPE)
endfunction()