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
8 # get_object_files_for_test(<result var> <target0> [<target1> ...])
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)
14 foreach(dep IN LISTS ARGN)
15 get_target_property(dep_type ${dep} "TARGET_TYPE")
20 if(${dep_type} STREQUAL ${OBJECT_LIBRARY_TARGET_TYPE})
21 get_target_property(dep_object_files ${dep} "OBJECT_FILES")
23 list(APPEND object_files ${dep_object_files})
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})
31 list(REMOVE_DUPLICATES object_files)
32 set(${result} ${object_files} PARENT_SCOPE)
35 # This is a helper function and not a build rule. Given an entrypoint object
36 # target, it returns the object file produced by this target in |result|.
37 # If the given entrypoint target is an alias, then it traverses up to the
38 # aliasee to get the object file.
39 function(get_entrypoint_object_file entrypoint_target result)
40 get_target_property(target_type ${entrypoint_target} "TARGET_TYPE")
41 if(NOT (${target_type} STREQUAL ${ENTRYPOINT_OBJ_TARGET_TYPE}))
43 "Expected an target added using `add_entrypoint_object` rule.")
46 get_target_property(objfile ${entrypoint_target} "OBJECT_FILE")
48 set(${result} ${objfile} PARENT_SCOPE)
52 # If the entrypoint is an alias, fetch the object file from the aliasee.
53 get_target_property(is_alias ${entrypoint_target} "IS_ALIAS")
55 get_target_property(aliasee ${entrypoint_target} "DEPS")
58 "Entrypoint alias ${entrypoint_target} does not have an aliasee.")
60 get_entrypoint_object_file(${aliasee} objfile)
61 set(${result} ${objfile} PARENT_SCOPE)
66 "Entrypoint ${entrypoint_target} does not produce an object file.")
67 endfunction(get_entrypoint_object_file)
69 # A rule to build a library from a collection of entrypoint objects.
71 # add_entrypoint_library(
72 # DEPENDS <list of add_entrypoint_object targets>
75 # NOTE: If one wants an entrypoint to be availabe in a library, then they will
76 # have to list the entrypoint target explicitly in the DEPENDS list. Implicit
77 # entrypoint dependencies will not be added to the library.
78 function(add_entrypoint_library target_name)
79 cmake_parse_arguments(
81 "" # No optional arguments
82 "" # No single value arguments
83 "DEPENDS" # Multi-value arguments
86 if(NOT ENTRYPOINT_LIBRARY_DEPENDS)
87 message(FATAL_ERROR "'add_entrypoint_library' target requires a DEPENDS list "
88 "of 'add_entrypoint_object' targets.")
91 get_fq_deps_list(fq_deps_list ${ENTRYPOINT_LIBRARY_DEPENDS})
92 get_object_files_for_entrypoint_library(obj_list ${fq_deps_list})
93 foreach(dep IN LISTS fq_deps_list)
94 get_target_property(dep_type ${dep} "TARGET_TYPE")
95 if(NOT (${dep_type} STREQUAL ${ENTRYPOINT_OBJ_TARGET_TYPE}))
96 message(FATAL_ERROR "Dependency '${dep}' of 'add_entrypoint_collection' is "
97 "not an 'add_entrypoint_object' target.")
99 get_entrypoint_object_file(${dep} objfile)
100 list(APPEND obj_list ${objfile})
102 list(REMOVE_DUPLICATES obj_list)
104 set(library_file "${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}${target_name}${CMAKE_STATIC_LIBRARY_SUFFIX}")
106 OUTPUT ${library_file}
107 COMMAND ${CMAKE_AR} -r ${library_file} ${obj_list}
113 DEPENDS ${library_file}
115 endfunction(add_entrypoint_library)
117 # Rule to build a shared library of redirector objects.
118 function(add_redirector_library target_name)
119 cmake_parse_arguments(
128 foreach(dep IN LISTS REDIRECTOR_LIBRARY_DEPENDS)
129 # TODO: Ensure that each dep is actually a add_redirector_object target.
130 list(APPEND obj_files $<TARGET_OBJECTS:${dep}>)
133 # TODO: Call the linker explicitly instead of calling the compiler driver to
134 # prevent DT_NEEDED on C++ runtime.
140 set_target_properties(${target_name} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
142 target_link_libraries(
147 set_target_properties(
152 endfunction(add_redirector_library)
154 set(HDR_LIBRARY_TARGET_TYPE "HDR_LIBRARY")
156 # Rule to add header only libraries.
158 # add_header_library(
160 # HDRS <list of .h files part of the library>
161 # DEPENDS <list of dependencies>
163 function(add_header_library target_name)
164 cmake_parse_arguments(
166 "" # No optional arguments
167 "" # No Single value arguments
168 "HDRS;DEPENDS" # Multi-value arguments
172 if(NOT ADD_HEADER_HDRS)
173 message(FATAL_ERROR "'add_header_library' target requires a HDRS list of .h files.")
176 get_fq_target_name(${target_name} fq_target_name)
178 set(FULL_HDR_PATHS "")
179 # TODO: Remove this foreach block when we can switch to the new
180 # version of the CMake policy CMP0076.
181 foreach(hdr IN LISTS ADD_HEADER_HDRS)
182 list(APPEND FULL_HDR_PATHS ${CMAKE_CURRENT_SOURCE_DIR}/${hdr})
185 set(interface_target_name "${fq_target_name}.__header_library__")
187 add_library(${interface_target_name} INTERFACE)
188 target_sources(${interface_target_name} INTERFACE ${FULL_HDR_PATHS})
189 get_fq_deps_list(fq_deps_list ${ADD_HEADER_DEPENDS})
190 if(ADD_HEADER_DEPENDS)
191 add_dependencies(${interface_target_name} ${fq_deps_list})
194 add_custom_target(${fq_target_name})
195 add_dependencies(${fq_target_name} ${interface_target_name})
196 set_target_properties(
199 "TARGET_TYPE" "${HDR_LIBRARY_TARGET_TYPE}"
200 "DEPS" "${fq_deps_list}"
202 endfunction(add_header_library)