1 # A rule for self contained header file targets.
2 # This rule merely copies the header file from the current source directory to
3 # the current binary directory.
9 function(add_header target_name)
10 cmake_parse_arguments(
12 "" # No optional arguments
13 "HDR" # Single value arguments
17 if(NOT ADD_HEADER_HDR)
18 message(FATAL_ERROR "'add_header' rules requires the HDR argument specifying a headef file.")
21 set(absolute_path ${CMAKE_CURRENT_SOURCE_DIR}/${ADD_HEADER_HDR})
22 file(RELATIVE_PATH relative_path ${LIBC_INCLUDE_SOURCE_DIR} ${absolute_path})
23 set(dest_file ${LIBC_INCLUDE_DIR}/${relative_path})
24 set(src_file ${CMAKE_CURRENT_SOURCE_DIR}/${ADD_HEADER_HDR})
28 COMMAND ${CMAKE_COMMAND} -E copy_if_different ${src_file} ${dest_file}
32 get_fq_target_name(${target_name} fq_target_name)
33 set(copied_hdr_target ${fq_target_name}.__copied_hdr__)
39 if(ADD_HEADER_DEPENDS)
40 get_fq_deps_list(fq_deps_list ${ADD_HEADER_DEPENDS})
41 # Dependencies of a add_header target can only be another add_header target
42 # or an add_gen_header target.
43 foreach(dep IN LISTS fq_deps_list)
44 get_target_property(header_file ${dep} HEADER_FILE_PATH)
46 message(FATAL_ERROR "Invalid dependency '${dep}' for '${fq_target_name}'.")
50 ${copied_hdr_target} ${fq_deps_list}
61 set_target_properties(
64 HEADER_FILE_PATH ${dest_file}
65 DEPS "${fq_deps_list}"
67 endfunction(add_header)
69 function(add_gen_header2 target_name)
70 cmake_parse_arguments(
72 "PUBLIC" # No optional arguments
73 "YAML_FILE;DEF_FILE;GEN_HDR" # Single value arguments
74 "DEPENDS" # Multi value arguments
77 get_fq_target_name(${target_name} fq_target_name)
78 if(NOT LLVM_LIBC_FULL_BUILD)
79 add_library(${fq_target_name} INTERFACE)
82 if(NOT ADD_GEN_HDR2_DEF_FILE)
83 message(FATAL_ERROR "`add_gen_hdr2` rule requires DEF_FILE to be specified.")
85 if(NOT ADD_GEN_HDR2_GEN_HDR)
86 message(FATAL_ERROR "`add_gen_hdr2` rule requires GEN_HDR to be specified.")
88 if(NOT ADD_GEN_HDR2_YAML_FILE)
89 message(FATAL_ERROR "`add_gen_hdr2` rule requires YAML_FILE to be specified.")
92 set(absolute_path ${CMAKE_CURRENT_SOURCE_DIR}/${ADD_GEN_HDR2_GEN_HDR})
93 file(RELATIVE_PATH relative_path ${LIBC_INCLUDE_SOURCE_DIR} ${absolute_path})
94 set(out_file ${LIBC_INCLUDE_DIR}/${relative_path})
95 set(yaml_file ${CMAKE_SOURCE_DIR}/${ADD_GEN_HDR2_YAML_FILE})
96 set(def_file ${CMAKE_CURRENT_SOURCE_DIR}/${ADD_GEN_HDR2_DEF_FILE})
99 if(ADD_GEN_HDR2_DATA_FILES)
100 foreach(data_file IN LISTS ADD_GEN_HDR2_DATA_FILES)
101 list(APPEND fq_data_files "${CMAKE_CURRENT_SOURCE_DIR}/${data_file}")
102 endforeach(data_file)
105 set(entry_points "${TARGET_ENTRYPOINT_NAME_LIST}")
106 list(TRANSFORM entry_points PREPEND "--e=")
110 COMMAND ${Python3_EXECUTABLE} ${LIBC_SOURCE_DIR}/newhdrgen/yaml_to_classes.py
112 --h_def_file ${def_file}
114 --output_dir ${out_file}
115 DEPENDS ${yaml_file} ${def_file} ${fq_data_files}
116 COMMENT "Generating header ${ADD_GEN_HDR2_GE2N_HDR} from ${yaml_file} and ${def_file}"
118 if(LIBC_TARGET_OS_IS_GPU)
119 file(MAKE_DIRECTORY ${LIBC_INCLUDE_DIR}/llvm-libc-decls)
120 file(MAKE_DIRECTORY ${LIBC_INCLUDE_DIR}/llvm-libc-decls/gpu)
121 set(decl_out_file ${LIBC_INCLUDE_DIR}/llvm-libc-decls/${relative_path})
123 OUTPUT ${decl_out_file}
124 COMMAND ${Python3_EXECUTABLE} ${LIBC_SOURCE_DIR}/newhdrgen/yaml_to_classes.py
128 --output_dir ${decl_out_file}
129 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
130 DEPENDS ${yaml_file} ${fq_data_files}
134 if(ADD_GEN_HDR2_DEPENDS)
135 get_fq_deps_list(fq_deps_list ${ADD_GEN_HDR2_DEPENDS})
136 # Dependencies of a add_header target can only be another add_gen_header target
137 # or an add_header target.
138 foreach(dep IN LISTS fq_deps_list)
139 get_target_property(header_file ${dep} HEADER_FILE_PATH)
141 message(FATAL_ERROR "Invalid dependency '${dep}' for '${fq_target_name}'.")
145 set(generated_hdr_target ${fq_target_name}.__generated_hdr__)
147 ${generated_hdr_target}
148 DEPENDS ${out_file} ${fq_deps_list} ${decl_out_file}
157 add_dependencies(${fq_target_name} ${generated_hdr_target})
159 set_target_properties(
162 HEADER_FILE_PATH ${out_file}
163 DECLS_FILE_PATH "${decl_out_file}"
164 DEPS "${fq_deps_list}"
168 endfunction(add_gen_header2)
173 # DEF_FILE <.h.def file>
174 # GEN_HDR <generated header file name>
175 # PARAMS <list of name=value pairs>
176 # DATA_FILES <list input data files>
178 function(add_gen_header target_name)
179 cmake_parse_arguments(
181 "PUBLIC" # No optional arguments
182 "DEF_FILE;GEN_HDR" # Single value arguments
183 "PARAMS;DATA_FILES;DEPENDS" # Multi value arguments
186 get_fq_target_name(${target_name} fq_target_name)
187 if(NOT LLVM_LIBC_FULL_BUILD)
188 # We don't want to use generated headers if we are doing a non-full-build.
189 add_library(${fq_target_name} INTERFACE)
192 if(NOT ADD_GEN_HDR_DEF_FILE)
193 message(FATAL_ERROR "`add_gen_hdr` rule requires DEF_FILE to be specified.")
195 if(NOT ADD_GEN_HDR_GEN_HDR)
196 message(FATAL_ERROR "`add_gen_hdr` rule requires GEN_HDR to be specified.")
199 set(absolute_path ${CMAKE_CURRENT_SOURCE_DIR}/${ADD_GEN_HDR_GEN_HDR})
200 file(RELATIVE_PATH relative_path ${LIBC_INCLUDE_SOURCE_DIR} ${absolute_path})
201 set(out_file ${LIBC_INCLUDE_DIR}/${relative_path})
202 set(in_file ${CMAKE_CURRENT_SOURCE_DIR}/${ADD_GEN_HDR_DEF_FILE})
204 set(fq_data_files "")
205 if(ADD_GEN_HDR_DATA_FILES)
206 foreach(data_file IN LISTS ADD_GEN_HDR_DATA_FILES)
207 list(APPEND fq_data_files "${CMAKE_CURRENT_SOURCE_DIR}/${data_file}")
208 endforeach(data_file)
211 set(replacement_params "")
212 if(ADD_GEN_HDR_PARAMS)
213 list(APPEND replacement_params "--args" ${ADD_GEN_HDR_PARAMS})
216 set(gen_hdr_script "${LIBC_BUILD_SCRIPTS_DIR}/gen_hdr.py")
218 file(GLOB td_includes ${LIBC_SOURCE_DIR}/spec/*.td)
220 set(ENTRYPOINT_NAME_LIST_ARG ${TARGET_ENTRYPOINT_NAME_LIST})
221 list(TRANSFORM ENTRYPOINT_NAME_LIST_ARG PREPEND "--e=")
224 set(hdrgen_exe ${LIBC_HDRGEN_EXE})
226 set(hdrgen_exe ${LIBC_TABLEGEN_EXE})
227 set(hdrgen_deps "${LIBC_TABLEGEN_EXE};${LIBC_TABLEGEN_TARGET}")
231 COMMAND ${hdrgen_exe} -o ${out_file} --header ${ADD_GEN_HDR_GEN_HDR}
232 --def ${in_file} ${replacement_params} -I ${LIBC_SOURCE_DIR}
233 ${ENTRYPOINT_NAME_LIST_ARG}
234 ${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/api.td
236 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
237 DEPENDS ${in_file} ${fq_data_files} ${td_includes}
238 ${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/api.td
242 if(LIBC_TARGET_OS_IS_GPU)
243 file(MAKE_DIRECTORY ${LIBC_INCLUDE_DIR}/llvm-libc-decls)
244 file(MAKE_DIRECTORY ${LIBC_INCLUDE_DIR}/llvm-libc-decls/gpu)
245 set(decl_out_file ${LIBC_INCLUDE_DIR}/llvm-libc-decls/${relative_path})
247 OUTPUT ${decl_out_file}
248 COMMAND ${hdrgen_exe} -o ${decl_out_file}
249 --header ${ADD_GEN_HDR_GEN_HDR} --def ${in_file} --export-decls
250 ${replacement_params} -I ${LIBC_SOURCE_DIR} ${ENTRYPOINT_NAME_LIST_ARG}
251 ${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/api.td
253 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
254 DEPENDS ${in_file} ${fq_data_files} ${td_includes}
255 ${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/api.td
260 if(ADD_GEN_HDR_DEPENDS)
261 get_fq_deps_list(fq_deps_list ${ADD_GEN_HDR_DEPENDS})
262 # Dependencies of a add_header target can only be another add_gen_header target
263 # or an add_header target.
264 foreach(dep IN LISTS fq_deps_list)
265 get_target_property(header_file ${dep} HEADER_FILE_PATH)
267 message(FATAL_ERROR "Invalid dependency '${dep}' for '${fq_target_name}'.")
271 set(generated_hdr_target ${fq_target_name}.__generated_hdr__)
273 ${generated_hdr_target}
274 DEPENDS ${out_file} ${fq_deps_list} ${decl_out_file}
283 # We add the dependencies separately and not list under add_header_library's
284 # DEPENDS option above. This is because, deps of add_header_library are
285 # used with target_link_libraries.
286 add_dependencies(${fq_target_name} ${generated_hdr_target})
288 set_target_properties(
291 HEADER_FILE_PATH ${out_file}
292 DECLS_FILE_PATH "${decl_out_file}"
293 DEPS "${fq_deps_list}"
295 endfunction(add_gen_header)