Skip to content

Commit 20cb440

Browse files
author
Siva Chandra Reddy
committed
[libc] Propagate entrypoint deps to downstream targets.
Deps are recrusively evaluated at the place they are needed. With this change, one does not have to list recursive deps of entrypoints when listing test targets. One will still have to explicitly list all entrypoint objects when setting up an "add_entrypoint_library" target. Reviewers: abrachet Differential Revision: https://reviews.llvm.org/D78537
1 parent 5771c98 commit 20cb440

File tree

10 files changed

+123
-150
lines changed

10 files changed

+123
-150
lines changed

libc/cmake/modules/LLVMLibCLibraryRules.cmake

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,46 @@
1+
# This is a helper function and not a build rule. It is to be used by the
2+
# the "add_entrypoint_library" rule to generate the full list of object files
3+
# recursively produced by "add_object_library" targets upstream in the
4+
# dependency tree. This function traverses up through the
5+
# "add_entrypoint_object" targets but does not collect the object files
6+
# produced by them.
7+
# Usage:
8+
# get_object_files_for_test(<result var> <target0> [<target1> ...])
9+
#
10+
# targetN is either an "add_entrypoint_target" target or an
11+
# "add_object_library" target.
12+
function(get_object_files_for_entrypoint_library result)
13+
set(object_files "")
14+
foreach(dep IN LISTS ARGN)
15+
get_target_property(dep_type ${dep} "TARGET_TYPE")
16+
if (NOT dep_type)
17+
continue()
18+
endif()
19+
20+
if(${dep_type} STREQUAL ${OBJECT_LIBRARY_TARGET_TYPE})
21+
get_target_property(dep_object_files ${dep} "OBJECT_FILES")
22+
if(dep_object_files)
23+
list(APPEND object_files ${dep_object_files})
24+
endif()
25+
endif()
26+
27+
get_target_property(indirect_deps ${dep} "DEPS")
28+
get_object_files_for_entrypoint_library(indirect_objfiles ${indirect_deps})
29+
list(APPEND object_files ${indirect_objfiles})
30+
endforeach(dep)
31+
list(REMOVE_DUPLICATES object_files)
32+
set(${result} ${object_files} PARENT_SCOPE)
33+
endfunction()
34+
135
# A rule to build a library from a collection of entrypoint objects.
236
# Usage:
337
# add_entrypoint_library(
438
# DEPENDS <list of add_entrypoint_object targets>
539
# )
40+
#
41+
# NOTE: If one wants an entrypoint to be availabe in a library, then they will
42+
# have to list the entrypoint target explicitly in the DEPENDS list. Implicit
43+
# entrypoint dependencies will not be added to the library.
644
function(add_entrypoint_library target_name)
745
cmake_parse_arguments(
846
"ENTRYPOINT_LIBRARY"
@@ -16,15 +54,16 @@ function(add_entrypoint_library target_name)
1654
"of 'add_entrypoint_object' targets.")
1755
endif()
1856

19-
set(obj_list "")
20-
foreach(dep IN LISTS ENTRYPOINT_LIBRARY_DEPENDS)
57+
get_fq_deps_list(fq_deps_list ${ENTRYPOINT_LIBRARY_DEPENDS})
58+
get_object_files_for_entrypoint_library(obj_list ${fq_deps_list})
59+
foreach(dep IN LISTS fq_deps_list)
2160
get_target_property(dep_type ${dep} "TARGET_TYPE")
2261
if(NOT (${dep_type} STREQUAL ${ENTRYPOINT_OBJ_TARGET_TYPE}))
2362
message(FATAL_ERROR "Dependency '${dep}' of 'add_entrypoint_collection' is "
2463
"not an 'add_entrypoint_object' target.")
2564
endif()
26-
get_target_property(target_obj_files ${dep} "OBJECT_FILES")
27-
list(APPEND obj_list "${target_obj_files}")
65+
get_target_property(objfile ${dep} "OBJECT_FILE")
66+
list(APPEND obj_list ${objfile})
2867
endforeach(dep)
2968
list(REMOVE_DUPLICATES obj_list)
3069

@@ -78,6 +117,8 @@ function(add_redirector_library target_name)
78117
)
79118
endfunction(add_redirector_library)
80119

120+
set(HDR_LIBRARY_TARGET_TYPE "HDR_LIBRARY")
121+
81122
# Rule to add header only libraries.
82123
# Usage
83124
# add_header_library(
@@ -107,12 +148,12 @@ function(add_header_library target_name)
107148
list(APPEND FULL_HDR_PATHS ${CMAKE_CURRENT_SOURCE_DIR}/${hdr})
108149
endforeach()
109150

110-
set(interface_target_name "${fq_target_name}_header_library__")
151+
set(interface_target_name "${fq_target_name}.__header_library__")
111152

112153
add_library(${interface_target_name} INTERFACE)
113154
target_sources(${interface_target_name} INTERFACE ${FULL_HDR_PATHS})
155+
get_fq_deps_list(fq_deps_list ${ADD_HEADER_DEPENDS})
114156
if(ADD_HEADER_DEPENDS)
115-
get_fq_deps_list(fq_deps_list ${ADD_HEADER_DEPENDS})
116157
add_dependencies(${interface_target_name} ${fq_deps_list})
117158
endif()
118159

@@ -121,6 +162,7 @@ function(add_header_library target_name)
121162
set_target_properties(
122163
${fq_target_name}
123164
PROPERTIES
124-
"TARGET_TYPE" "HDR_LIBRARY"
165+
"TARGET_TYPE" "${HDR_LIBRARY_TARGET_TYPE}"
166+
"DEPS" "${fq_deps_list}"
125167
)
126168
endfunction(add_header_library)

libc/cmake/modules/LLVMLibCObjectRules.cmake

Lines changed: 15 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -43,36 +43,17 @@ function(add_object_library target_name)
4343
)
4444
endif()
4545

46-
set(all_object_files $<TARGET_OBJECTS:${fq_target_name}>)
47-
if(ADD_OBJECT_DEPENDS)
48-
get_fq_deps_list(fq_deps_list ${ADD_OBJECT_DEPENDS})
49-
add_dependencies(
50-
${fq_target_name}
51-
${fq_deps_list}
52-
)
53-
foreach(obj_target IN LISTS fq_deps_list)
54-
if(NOT TARGET obj_target)
55-
# Not all targets will be visible. So, we will ignore those which aren't
56-
# visible yet.
57-
continue()
58-
endif()
59-
get_target_property(obj_type ${obj_target} "TARGET_TYPE")
60-
if((NOT obj_type) OR (NOT (${obj_type} STREQUAL ${OBJECT_LIBRARY_TARGET_TYPE})))
61-
continue()
62-
endif()
63-
# If a dependency is also a object file library, we will collect the list of
64-
# object files from it.
65-
get_target_property(obj_files ${obj_target} "OBJECT_FILES")
66-
list(APPEND all_object_files ${obj_files})
67-
endforeach(obj_target)
46+
get_fq_deps_list(fq_deps_list ${ADD_OBJECT_DEPENDS})
47+
if(fq_deps_list)
48+
add_dependencies(${fq_target_name} ${fq_deps_list})
6849
endif()
69-
list(REMOVE_DUPLICATES all_object_files)
7050

7151
set_target_properties(
7252
${fq_target_name}
7353
PROPERTIES
7454
"TARGET_TYPE" ${OBJECT_LIBRARY_TARGET_TYPE}
75-
"OBJECT_FILES" "${all_object_files}"
55+
"OBJECT_FILES" "$<TARGET_OBJECTS:${fq_target_name}>"
56+
"DEPS" "${fq_deps_list}"
7657
)
7758
endfunction(add_object_library)
7859

@@ -123,14 +104,15 @@ function(add_entrypoint_object target_name)
123104

124105
add_custom_target(${fq_target_name})
125106
add_dependencies(${fq_target_name} ${fq_dep_name})
126-
get_target_property(all_objects ${fq_dep_name} "OBJECT_FILES")
127-
get_target_property(all_objects_raw ${fq_dep_name} "OBJECT_FILES_RAW")
107+
get_target_property(object_file ${fq_dep_name} "OBJECT_FILE")
108+
get_target_property(object_file_raw ${fq_dep_name} "OBJECT_FILE_RAW")
128109
set_target_properties(
129110
${fq_target_name}
130111
PROPERTIES
131112
"TARGET_TYPE" ${ENTRYPOINT_OBJ_TARGET_TYPE}
132-
"OBJECT_FILES" "${all_objects}"
133-
"OBJECT_FILES_RAW" "${all_objects_raw}"
113+
"OBJECT_FILE"
114+
"OBJECT_FILE_RAW"
115+
"DEPS" "${fq_dep_name}"
134116
)
135117
return()
136118
endif()
@@ -170,38 +152,12 @@ function(add_entrypoint_object target_name)
170152
${LIBC_SOURCE_DIR}
171153
${LIBC_BUILD_DIR}
172154
)
155+
get_fq_deps_list(fq_deps_list ${ADD_ENTRYPOINT_OBJ_DEPENDS})
173156
add_dependencies(
174157
${objects_target_name}
175158
libc.src.__support.common
159+
${fq_deps_list}
176160
)
177-
set(dep_objects "")
178-
if(ADD_ENTRYPOINT_OBJ_DEPENDS)
179-
get_fq_deps_list(fq_deps_list ${ADD_ENTRYPOINT_OBJ_DEPENDS})
180-
add_dependencies(
181-
${objects_target_name}
182-
${fq_deps_list}
183-
)
184-
foreach(dep_target IN LISTS fq_deps_list)
185-
if(NOT TARGET ${dep_target})
186-
# Not all targets will be visible. So, we will ignore those which aren't
187-
# visible yet.
188-
continue()
189-
endif()
190-
get_target_property(obj_type ${dep_target} "TARGET_TYPE")
191-
if((NOT obj_type) OR (NOT (${obj_type} STREQUAL ${OBJECT_LIBRARY_TARGET_TYPE})))
192-
# Even from among the visible targets, we will collect object files
193-
# only from add_object_library targets.
194-
continue()
195-
endif()
196-
# Calling get_target_property requires that the target be visible at this
197-
# point. For object library dependencies, this is a reasonable requirement.
198-
# We can revisit this in future if we need cases which break under this
199-
# requirement.
200-
get_target_property(obj_files ${dep_target} "OBJECT_FILES")
201-
list(APPEND dep_objects ${obj_files})
202-
endforeach(dep_target)
203-
endif()
204-
list(REMOVE_DUPLICATES dep_objects)
205161

206162
if(ADD_ENTRYPOINT_OBJ_COMPILE_OPTIONS)
207163
target_compile_options(
@@ -239,16 +195,13 @@ function(add_entrypoint_object target_name)
239195
ALL
240196
DEPENDS ${object_file}
241197
)
242-
set(all_objects ${object_file})
243-
list(APPEND all_objects ${dep_objects})
244-
set(all_objects_raw ${object_file_raw})
245-
list(APPEND all_objects_raw ${dep_objects})
246198
set_target_properties(
247199
${fq_target_name}
248200
PROPERTIES
249201
"TARGET_TYPE" ${ENTRYPOINT_OBJ_TARGET_TYPE}
250-
"OBJECT_FILES" "${all_objects}"
251-
"OBJECT_FILES_RAW" "${all_objects_raw}"
202+
"OBJECT_FILE" "${object_file}"
203+
"OBJECT_FILE_RAW" "${object_file_raw}"
204+
"DEPS" "${fq_deps_list}"
252205
)
253206

254207
if(LLVM_LIBC_ENABLE_LINTING)
@@ -310,4 +263,3 @@ function(add_redirector_object target_name)
310263
BEFORE PRIVATE -fPIC
311264
)
312265
endfunction(add_redirector_object)
313-

libc/cmake/modules/LLVMLibCTestRules.cmake

Lines changed: 45 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,42 @@
1+
# This is a helper function and not a build rule. It is to be used by the
2+
# various test rules to generate the full list of object files
3+
# recursively produced by "add_entrypoint_object" and "add_object_library"
4+
# targets.
5+
# Usage:
6+
# get_object_files_for_test(<result var> <target0> [<target1> ...])
7+
#
8+
# targetN is either an "add_entrypoint_target" target or an
9+
# "add_object_library" target.
10+
function(get_object_files_for_test result)
11+
set(object_files "")
12+
foreach(dep IN LISTS ARGN)
13+
get_target_property(dep_type ${dep} "TARGET_TYPE")
14+
if(NOT dep_type)
15+
# Target for which TARGET_TYPE property is not set do not
16+
# provide any object files.
17+
continue()
18+
endif()
19+
20+
if(${dep_type} STREQUAL ${OBJECT_LIBRARY_TARGET_TYPE})
21+
get_target_property(dep_object_files ${dep} "OBJECT_FILES")
22+
if(dep_object_files)
23+
list(APPEND object_files ${dep_object_files})
24+
endif()
25+
elseif(${dep_type} STREQUAL ${ENTRYPOINT_OBJ_TARGET_TYPE})
26+
get_target_property(object_file_raw ${dep} "OBJECT_FILE_RAW")
27+
if(object_file_raw)
28+
list(APPEND object_files ${object_file_raw})
29+
endif()
30+
endif()
31+
32+
get_target_property(indirect_deps ${dep} "DEPS")
33+
get_object_files_for_test(indirect_objfiles ${indirect_deps})
34+
list(APPEND object_files ${indirect_objfiles})
35+
endforeach(dep)
36+
list(REMOVE_DUPLICATES object_files)
37+
set(${result} ${object_files} PARENT_SCOPE)
38+
endfunction(get_object_files_for_test)
39+
140
# Rule to add a libc unittest.
241
# Usage
342
# add_libc_unittest(
@@ -29,21 +68,6 @@ function(add_libc_unittest target_name)
2968
"'add_entrypoint_object' targets.")
3069
endif()
3170

32-
set(library_deps "")
33-
get_fq_deps_list(fq_deps_list ${LIBC_UNITTEST_DEPENDS})
34-
foreach(dep IN LISTS fq_deps_list)
35-
get_target_property(dep_type ${dep} "TARGET_TYPE")
36-
if(${dep_type} STREQUAL ${ENTRYPOINT_OBJ_TARGET_TYPE})
37-
get_target_property(obj_files ${dep} "OBJECT_FILES_RAW")
38-
list(APPEND library_deps ${obj_files})
39-
elseif(${dep_type} STREQUAL ${OBJECT_LIBRARY_TARGET_TYPE})
40-
get_target_property(obj_files ${dep} "OBJECT_FILES")
41-
list(APPEND library_deps ${obj_files})
42-
endif()
43-
# TODO: Check if the dep is a normal CMake library target. If yes, then add it
44-
# to the list of library_deps.
45-
endforeach(dep)
46-
list(REMOVE_DUPLICATES library_deps)
4771

4872
get_fq_target_name(${target_name} fq_target_name)
4973
add_executable(
@@ -66,9 +90,9 @@ function(add_libc_unittest target_name)
6690
)
6791
endif()
6892

69-
if(library_deps)
70-
target_link_libraries(${fq_target_name} PRIVATE ${library_deps})
71-
endif()
93+
get_fq_deps_list(fq_deps_list ${LIBC_UNITTEST_DEPENDS})
94+
get_object_files_for_test(link_object_files ${fq_deps_list})
95+
target_link_libraries(${fq_target_name} PRIVATE ${link_object_files})
7296

7397
set_target_properties(${fq_target_name}
7498
PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
@@ -123,22 +147,6 @@ function(add_libc_fuzzer target_name)
123147
"'add_entrypoint_object' targets.")
124148
endif()
125149

126-
get_fq_deps_list(fq_deps_list ${LIBC_FUZZER_DEPENDS})
127-
set(library_deps "")
128-
foreach(dep IN LISTS fq_deps_list)
129-
get_target_property(dep_type ${dep} "TARGET_TYPE")
130-
if (dep_type)
131-
string(COMPARE EQUAL ${dep_type} ${ENTRYPOINT_OBJ_TARGET_TYPE} dep_is_entrypoint)
132-
if(dep_is_entrypoint)
133-
get_target_property(obj_file ${dep} "OBJECT_FILES_RAW")
134-
list(APPEND library_deps ${obj_file})
135-
continue()
136-
endif()
137-
endif()
138-
# TODO: Check if the dep is a normal CMake library target. If yes, then add it
139-
# to the list of library_deps.
140-
endforeach(dep)
141-
142150
get_fq_target_name(${target_name} fq_target_name)
143151
add_executable(
144152
${fq_target_name}
@@ -154,9 +162,9 @@ function(add_libc_fuzzer target_name)
154162
${LIBC_BUILD_DIR}/include
155163
)
156164

157-
if(library_deps)
158-
target_link_libraries(${fq_target_name} PRIVATE ${library_deps})
159-
endif()
165+
get_fq_deps_list(fq_deps_list ${LIBC_FUZZER_DEPENDS})
166+
get_object_files_for_test(link_object_files ${fq_deps_list})
167+
target_link_libraries(${fq_target_name} PRIVATE ${link_object_files})
160168

161169
set_target_properties(${fq_target_name}
162170
PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})

libc/loader/linux/CMakeLists.txt

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ function(add_loader_object name)
88
)
99

1010
get_fq_target_name(${name} fq_target_name)
11-
11+
get_fq_deps_list(fq_deps_list ${ADD_LOADER_OBJECT_DEPENDS})
1212
if(ADD_LOADER_OBJECT_ALIAS)
1313
list(LENGTH ADD_LOADER_OBJECT_DEPENDS deps_size)
1414
if(NOT (${deps_size} EQUAL "1"))
@@ -23,14 +23,15 @@ function(add_loader_object name)
2323
set_target_properties(
2424
${fq_target_name}
2525
PROPERTIES
26-
"TARGET_TYPE" "LOADER_OBJECT"
27-
"OBJECT_FILES" ${dep_objfile}
26+
"TARGET_TYPE" "${OBJECT_LIBRARY_TARGET_TYPE}"
27+
"OBJECT_FILES" ""
28+
"DEPS" "${fq_dep_name}"
2829
)
2930
return()
3031
endif()
3132

3233
add_object_library(
33-
${name}_objects
34+
${name}.__objects__
3435
SRCS ${ADD_LOADER_OBJECT_SRC}
3536
DEPENDS ${ADD_LOADER_OBJECT_DEPENDS}
3637
COMPILE_OPTIONS ${ADD_LOADER_OBJECT_COMPILE_OPTIONS}
@@ -39,8 +40,8 @@ function(add_loader_object name)
3940
set(objfile ${LIBC_BUILD_DIR}/lib/${name}.o)
4041
add_custom_command(
4142
OUTPUT ${objfile}
42-
COMMAND cp $<TARGET_OBJECTS:${fq_target_name}_objects> ${objfile}
43-
DEPENDS $<TARGET_OBJECTS:${fq_target_name}_objects>
43+
COMMAND cp $<TARGET_OBJECTS:${fq_target_name}.__objects__> ${objfile}
44+
DEPENDS $<TARGET_OBJECTS:${fq_target_name}.__objects__>
4445
)
4546
add_custom_target(
4647
${fq_target_name}
@@ -49,8 +50,9 @@ function(add_loader_object name)
4950
set_target_properties(
5051
${fq_target_name}
5152
PROPERTIES
52-
"TARGET_TYPE" "LOADER_OBJECT"
53-
"OBJECT_FILES" ${objfile}
53+
"TARGET_TYPE" "${OBJECT_LIBRARY_TARGET_TYPE}"
54+
"OBJECT_FILES" ""
55+
"DEPS" "${fq_target_name}.__objects__"
5456
)
5557
endfunction()
5658

0 commit comments

Comments
 (0)