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 # A rule for generated header file targets.
73 # DEF_FILE <.h.def file>
74 # GEN_HDR <generated header file name>
75 # PARAMS <list of name=value pairs>
76 # DATA_FILES <list input data files>
78 function(add_gen_header target_name)
79 cmake_parse_arguments(
81 "PUBLIC" # No optional arguments
82 "DEF_FILE;GEN_HDR" # Single value arguments
83 "PARAMS;DATA_FILES;DEPENDS" # Multi value arguments
86 get_fq_target_name(${target_name} fq_target_name)
87 if(NOT LLVM_LIBC_FULL_BUILD)
88 # We don't want to use generated headers if we are doing a non-full-build.
89 add_library(${fq_target_name} INTERFACE)
92 if(NOT ADD_GEN_HDR_DEF_FILE)
93 message(FATAL_ERROR "`add_gen_hdr` rule requires DEF_FILE to be specified.")
95 if(NOT ADD_GEN_HDR_GEN_HDR)
96 message(FATAL_ERROR "`add_gen_hdr` rule requires GEN_HDR to be specified.")
99 set(absolute_path ${CMAKE_CURRENT_SOURCE_DIR}/${ADD_GEN_HDR_GEN_HDR})
100 file(RELATIVE_PATH relative_path ${LIBC_INCLUDE_SOURCE_DIR} ${absolute_path})
101 set(out_file ${LIBC_INCLUDE_DIR}/${relative_path})
102 set(in_file ${CMAKE_CURRENT_SOURCE_DIR}/${ADD_GEN_HDR_DEF_FILE})
104 set(fq_data_files "")
105 if(ADD_GEN_HDR_DATA_FILES)
106 foreach(data_file IN LISTS ADD_GEN_HDR_DATA_FILES)
107 list(APPEND fq_data_files "${CMAKE_CURRENT_SOURCE_DIR}/${data_file}")
108 endforeach(data_file)
111 set(replacement_params "")
112 if(ADD_GEN_HDR_PARAMS)
113 list(APPEND replacement_params "--args" ${ADD_GEN_HDR_PARAMS})
116 set(gen_hdr_script "${LIBC_BUILD_SCRIPTS_DIR}/gen_hdr.py")
118 file(GLOB td_includes ${LIBC_SOURCE_DIR}/spec/*.td)
120 set(ENTRYPOINT_NAME_LIST_ARG ${TARGET_ENTRYPOINT_NAME_LIST})
121 list(TRANSFORM ENTRYPOINT_NAME_LIST_ARG PREPEND "--e=")
124 set(hdrgen_exe ${LIBC_HDRGEN_EXE})
126 set(hdrgen_exe ${LIBC_TABLEGEN_EXE})
127 set(hdrgen_deps "${LIBC_TABLEGEN_EXE};${LIBC_TABLEGEN_TARGET}")
131 COMMAND ${hdrgen_exe} -o ${out_file} --header ${ADD_GEN_HDR_GEN_HDR}
132 --def ${in_file} ${replacement_params} -I ${LIBC_SOURCE_DIR}
133 ${ENTRYPOINT_NAME_LIST_ARG}
134 ${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/api.td
136 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
137 DEPENDS ${in_file} ${fq_data_files} ${td_includes}
138 ${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/api.td
142 if(LIBC_TARGET_ARCHITECTURE_IS_GPU)
143 file(MAKE_DIRECTORY ${LIBC_INCLUDE_DIR}/llvm-libc-decls)
144 set(decl_out_file ${LIBC_INCLUDE_DIR}/llvm-libc-decls/${relative_path})
146 OUTPUT ${decl_out_file}
147 COMMAND ${hdrgen_exe} -o ${decl_out_file}
148 --header ${ADD_GEN_HDR_GEN_HDR} --def ${in_file} --export-decls
149 ${replacement_params} -I ${LIBC_SOURCE_DIR} ${ENTRYPOINT_NAME_LIST_ARG}
150 ${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/api.td
152 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
153 DEPENDS ${in_file} ${fq_data_files} ${td_includes}
154 ${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/api.td
159 if(ADD_GEN_HDR_DEPENDS)
160 get_fq_deps_list(fq_deps_list ${ADD_GEN_HDR_DEPENDS})
161 # Dependencies of a add_header target can only be another add_gen_header target
162 # or an add_header target.
163 foreach(dep IN LISTS fq_deps_list)
164 get_target_property(header_file ${dep} HEADER_FILE_PATH)
166 message(FATAL_ERROR "Invalid dependency '${dep}' for '${fq_target_name}'.")
170 set(generated_hdr_target ${fq_target_name}.__generated_hdr__)
172 ${generated_hdr_target}
173 DEPENDS ${out_file} ${fq_deps_list} ${decl_out_file}
182 # We add the dependencies separately and not list under add_header_library's
183 # DEPENDS option above. This is because, deps of add_header_library are
184 # used with target_link_libraries.
185 add_dependencies(${fq_target_name} ${generated_hdr_target})
187 set_target_properties(
190 HEADER_FILE_PATH ${out_file}
191 DECLS_FILE_PATH "${decl_out_file}"
192 DEPS "${fq_deps_list}"
194 endfunction(add_gen_header)