Skip to content

Commit 51b899c

Browse files
author
Siva Chandra Reddy
committed
[libc] Extend add_object rule to handle helper object libraries.
The rule is now called add_object_library. Reviewers: abrachet Differential Revision: https://reviews.llvm.org/D76826
1 parent 255e634 commit 51b899c

File tree

5 files changed

+77
-47
lines changed

5 files changed

+77
-47
lines changed

libc/cmake/modules/LLVMLibCRules.cmake

Lines changed: 71 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -94,32 +94,35 @@ function(add_gen_header target_name)
9494
)
9595
endfunction(add_gen_header)
9696

97-
set(SINGLE_OBJECT_TARGET_TYPE "LIBC_SINGLE_OBJECT")
97+
set(OBJECT_LIBRARY_TARGET_TYPE "OBJECT_LIBRARY")
9898

99-
# Function to generate single object file.
99+
# Rule which is essentially a wrapper over add_library to compile a set of
100+
# sources to object files.
100101
# Usage:
101-
# add_object(
102+
# add_object_library(
102103
# <target_name>
103-
# SRC <source file to compile>
104+
# HDRS <list of header files>
105+
# SRCS <list of source files>
104106
# DEPENDS <list of dependencies>
105107
# COMPILE_OPTIONS <optional list of special compile options for this target>
106-
function(add_object target_name)
108+
function(add_object_library target_name)
107109
cmake_parse_arguments(
108110
"ADD_OBJECT"
109111
"" # No option arguments
110-
"SRC" # Single value arguments
111-
"COMPILE_OPTIONS;DEPENDS" # Multivalue arguments
112+
"" # Single value arguments
113+
"SRCS;HDRS;COMPILE_OPTIONS;DEPENDS" # Multivalue arguments
112114
${ARGN}
113115
)
114116

115-
if(NOT ADD_OBJECT_SRC)
116-
message(FATAL_ERROR "'add_object' rules requires a SRC to be specified.")
117+
if(NOT ADD_OBJECT_SRCS)
118+
message(FATAL_ERROR "'add_object_library' rule requires SRCS to be specified.")
117119
endif()
118120

119121
add_library(
120122
${target_name}
121123
OBJECT
122-
${ADD_OBJECT_SRC}
124+
${ADD_OBJECT_SRCS}
125+
${ADD_OBJECT_HDRS}
123126
)
124127
target_include_directories(
125128
${target_name}
@@ -132,19 +135,33 @@ function(add_object target_name)
132135
PRIVATE ${ADD_OBJECT_COMPILE_OPTIONS}
133136
)
134137
endif()
138+
139+
set(all_object_files $<TARGET_OBJECTS:${target_name}>)
135140
if(ADD_OBJECT_DEPENDS)
136141
add_dependencies(
137142
${target_name}
138143
${ADD_OBJECT_DEPENDS}
139144
)
145+
foreach(obj_target IN LISTS ADD_ENTRYPOINT_OBJ_SPECIAL_OBJECTS)
146+
get_target_property(obj_type ${obj_target} "TARGET_TYPE")
147+
if((NOT obj_type) OR (NOT (${obj_type} STREQUAL ${OBJECT_LIBRARY_TARGET_TYPE})))
148+
continue()
149+
endif()
150+
# If a dependency is also a object file library, we will collect the list of
151+
# object files from it.
152+
get_target_property(obj_files ${obj_target} "OBJECT_FILES")
153+
list(APPEND all_object_files ${obj_files})
154+
endforeach(obj_target)
140155
endif()
156+
list(REMOVE_DUPLICATES all_object_files)
157+
141158
set_target_properties(
142159
${target_name}
143160
PROPERTIES
144-
"TARGET_TYPE" ${SINGLE_OBJECT_TARGET_TYPE}
145-
"OBJECT_FILE" $<TARGET_OBJECTS:${target_name}>
161+
"TARGET_TYPE" ${OBJECT_LIBRARY_TARGET_TYPE}
162+
"OBJECT_FILES" "${all_object_files}"
146163
)
147-
endfunction(add_object)
164+
endfunction(add_object_library)
148165

149166
set(ENTRYPOINT_OBJ_TARGET_TYPE "ENTRYPOINT_OBJ")
150167

@@ -165,7 +182,7 @@ function(add_entrypoint_object target_name)
165182
"ADD_ENTRYPOINT_OBJ"
166183
"REDIRECTED" # Optional argument
167184
"NAME" # Single value arguments
168-
"SRCS;HDRS;SPECIAL_OBJECTS;DEPENDS;COMPILE_OPTIONS" # Multi value arguments
185+
"SRCS;HDRS;DEPENDS;COMPILE_OPTIONS" # Multi value arguments
169186
${ARGN}
170187
)
171188
if(NOT ADD_ENTRYPOINT_OBJ_SRCS)
@@ -203,12 +220,34 @@ function(add_entrypoint_object target_name)
203220
${target_name}_objects
204221
support_common_h
205222
)
223+
set(dep_objects "")
206224
if(ADD_ENTRYPOINT_OBJ_DEPENDS)
207225
add_dependencies(
208226
${target_name}_objects
209227
${ADD_ENTRYPOINT_OBJ_DEPENDS}
210228
)
229+
foreach(dep_target IN LISTS ADD_ENTRYPOINT_OBJ_DEPENDS)
230+
if(NOT TARGET ${dep_target})
231+
# Not all targets will be visible. So, we will ignore those which aren't
232+
# visible yet.
233+
continue()
234+
endif()
235+
get_target_property(obj_type ${dep_target} "TARGET_TYPE")
236+
if((NOT obj_type) OR (NOT (${obj_type} STREQUAL ${OBJECT_LIBRARY_TARGET_TYPE})))
237+
# Even from among the visible targets, we will collect object files
238+
# only from add_object_library targets.
239+
continue()
240+
endif()
241+
# Calling get_target_property requires that the target be visible at this
242+
# point. For object library dependencies, this is a reasonable requirement.
243+
# We can revisit this in future if we need cases which break under this
244+
# requirement.
245+
get_target_property(obj_files ${dep_target} "OBJECT_FILES")
246+
list(APPEND dep_objects ${obj_files})
247+
endforeach(dep_target)
211248
endif()
249+
list(REMOVE_DUPLICATES dep_objects)
250+
212251
if(ADD_ENTRYPOINT_OBJ_COMPILE_OPTIONS)
213252
target_compile_options(
214253
${target_name}_objects
@@ -220,16 +259,6 @@ function(add_entrypoint_object target_name)
220259
set(object_file "${CMAKE_CURRENT_BINARY_DIR}/${target_name}.o")
221260

222261
set(input_objects $<TARGET_OBJECTS:${target_name}_objects>)
223-
if(ADD_ENTRYPOINT_OBJ_SPECIAL_OBJECTS)
224-
foreach(obj_target IN LISTS ADD_ENTRYPOINT_OBJ_SPECIAL_OBJECTS)
225-
get_target_property(obj_type ${obj_target} "TARGET_TYPE")
226-
if((NOT obj_type) OR (NOT (${obj_type} STREQUAL ${SINGLE_OBJECT_TARGET_TYPE})))
227-
message(FATAL_ERROR "Unexpected target type for 'SPECIAL_OBJECT' - should be a target introduced by the `add_object` rule.")
228-
endif()
229-
list(APPEND input_objects $<TARGET_OBJECTS:${obj_target}>)
230-
endforeach(obj_target)
231-
endif()
232-
233262
add_custom_command(
234263
OUTPUT ${object_file_raw}
235264
DEPENDS ${input_objects}
@@ -253,12 +282,16 @@ function(add_entrypoint_object target_name)
253282
ALL
254283
DEPENDS ${object_file}
255284
)
285+
set(all_objects ${object_file})
286+
list(APPEND all_objects ${dep_objects})
287+
set(all_objects_raw ${object_file_raw})
288+
list(APPEND all_objects_raw ${dep_objects})
256289
set_target_properties(
257290
${target_name}
258291
PROPERTIES
259292
"TARGET_TYPE" ${ENTRYPOINT_OBJ_TARGET_TYPE}
260-
"OBJECT_FILE" ${object_file}
261-
"OBJECT_FILE_RAW" ${object_file_raw}
293+
"OBJECT_FILES" "${all_objects}"
294+
"OBJECT_FILES_RAW" "${all_objects_raw}"
262295
)
263296
endfunction(add_entrypoint_object)
264297

@@ -282,13 +315,13 @@ function(add_entrypoint_library target_name)
282315
set(obj_list "")
283316
foreach(dep IN LISTS ENTRYPOINT_LIBRARY_DEPENDS)
284317
get_target_property(dep_type ${dep} "TARGET_TYPE")
285-
string(COMPARE EQUAL ${dep_type} ${ENTRYPOINT_OBJ_TARGET_TYPE} dep_is_entrypoint)
286-
if(NOT dep_is_entrypoint)
318+
if(NOT (${dep_type} STREQUAL ${ENTRYPOINT_OBJ_TARGET_TYPE}))
287319
message(FATAL_ERROR "Dependency '${dep}' of 'add_entrypoint_collection' is not an 'add_entrypoint_object' target.")
288320
endif()
289-
get_target_property(target_obj_file ${dep} "OBJECT_FILE")
290-
list(APPEND obj_list "${target_obj_file}")
321+
get_target_property(target_obj_files ${dep} "OBJECT_FILES")
322+
list(APPEND obj_list "${target_obj_files}")
291323
endforeach(dep)
324+
list(REMOVE_DUPLICATES obj_list)
292325

293326
set(library_file "${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}${target_name}${CMAKE_STATIC_LIBRARY_SUFFIX}")
294327
add_custom_command(
@@ -396,17 +429,17 @@ function(add_libc_unittest target_name)
396429
set(library_deps "")
397430
foreach(dep IN LISTS LIBC_UNITTEST_DEPENDS)
398431
get_target_property(dep_type ${dep} "TARGET_TYPE")
399-
if (dep_type)
400-
string(COMPARE EQUAL ${dep_type} ${ENTRYPOINT_OBJ_TARGET_TYPE} dep_is_entrypoint)
401-
if(dep_is_entrypoint)
402-
get_target_property(obj_file ${dep} "OBJECT_FILE_RAW")
403-
list(APPEND library_deps ${obj_file})
404-
continue()
405-
endif()
432+
if(${dep_type} STREQUAL ${ENTRYPOINT_OBJ_TARGET_TYPE})
433+
get_target_property(obj_files ${dep} "OBJECT_FILES_RAW")
434+
list(APPEND library_deps ${obj_files})
435+
elseif(${dep_type} STREQUAL ${OBJECT_LIBRARY_TARGET_TYPE})
436+
get_target_property(obj_files ${dep} "OBJECT_FILES")
437+
list(APPEND library_deps ${obj_files})
406438
endif()
407439
# TODO: Check if the dep is a normal CMake library target. If yes, then add it
408440
# to the list of library_deps.
409441
endforeach(dep)
442+
list(REMOVE_DUPLICATES library_deps)
410443

411444
add_executable(
412445
${target_name}
@@ -488,7 +521,7 @@ function(add_libc_fuzzer target_name)
488521
if (dep_type)
489522
string(COMPARE EQUAL ${dep_type} ${ENTRYPOINT_OBJ_TARGET_TYPE} dep_is_entrypoint)
490523
if(dep_is_entrypoint)
491-
get_target_property(obj_file ${dep} "OBJECT_FILE_RAW")
524+
get_target_property(obj_file ${dep} "OBJECT_FILES_RAW")
492525
list(APPEND library_deps ${obj_file})
493526
continue()
494527
endif()

libc/loader/linux/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ function(add_loader_object name)
66
"DEPENDS;COMPILE_OPTIONS" # Multi value arguments
77
${ARGN}
88
)
9-
add_object(
9+
add_object_library(
1010
${name}_object
11-
SRC ${ADD_LOADER_OBJECT_SRC}
11+
SRCS ${ADD_LOADER_OBJECT_SRC}
1212
DEPENDS ${ADD_LOADER_OBJECT_DEPENDS}
1313
COMPILE_OPTIONS ${ADD_LOADER_OBJECT_COMPILE_OPTIONS}
1414
)
@@ -27,7 +27,7 @@ function(add_loader_object name)
2727
${name}
2828
PROPERTIES
2929
"TARGET_TYPE" "LOADER_OBJECT"
30-
"OBJECT_FILE" ${objfile}
30+
"OBJECT_FILES" ${objfile}
3131
)
3232
endfunction()
3333

libc/src/signal/linux/CMakeLists.txt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ add_entrypoint_object(
1212
signal_h
1313
)
1414

15-
add_object(
15+
add_object_library(
1616
__restore
17-
SRC
17+
SRCS
1818
__restore.cpp
1919
COMPILE_OPTIONS
2020
-fomit-frame-pointer
@@ -41,8 +41,6 @@ add_entrypoint_object(
4141
sys_syscall_h
4242
linux_syscall_h
4343
signal_h
44-
SPECIAL_OBJECTS
45-
__restore
4644
)
4745

4846
add_entrypoint_object(

libc/test/loader/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function(add_loader_test target_name)
3232
if(ADD_LOADER_TEST_DEPENDS)
3333
add_dependencies(${target_name} ${ADD_LOADER_TEST_DEPENDS})
3434
foreach(dep IN LISTS ADD_LOADER_TEST_DEPENDS)
35-
get_target_property(objfile ${dep} "OBJECT_FILE")
35+
get_target_property(objfile ${dep} "OBJECT_FILES")
3636
if(NOT objfile)
3737
message(
3838
FATAL_ERROR

libc/test/src/signal/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ add_libc_unittest(
2323
signal_h
2424
errno_h
2525
__errno_location
26-
__restore
2726
)
2827

2928
add_libc_unittest(

0 commit comments

Comments
 (0)