Skip to content

Commit

Permalink
[libc] newheadergen: cmake config newhdrgen (#99543)
Browse files Browse the repository at this point in the history
- revert to revert for patch
#98828
- revert to revert #99410
- revert to revert #99413
  • Loading branch information
aaryanshukla authored Jul 18, 2024
1 parent a066217 commit b1fd6f0
Show file tree
Hide file tree
Showing 3 changed files with 329 additions and 155 deletions.
2 changes: 2 additions & 0 deletions libc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ if(LIBC_BUILD_GPU_LOADER OR (LLVM_LIBC_GPU_BUILD AND NOT LLVM_RUNTIMES_BUILD))
add_subdirectory(utils/gpu)
endif()

option(LIBC_USE_NEW_HEADER_GEN "Generate header files using new headergen instead of the old one" OFF)

set(NEED_LIBC_HDRGEN FALSE)
if(NOT LLVM_RUNTIMES_BUILD)
if("libc" IN_LIST LLVM_ENABLE_RUNTIMES)
Expand Down
101 changes: 100 additions & 1 deletion libc/cmake/modules/LLVMLibCHeaderRules.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,106 @@ function(add_header target_name)
)
endfunction(add_header)

# A rule for generated header file targets.
function(add_gen_header2 target_name)
cmake_parse_arguments(
"ADD_GEN_HDR2"
"PUBLIC" # No optional arguments
"YAML_FILE;DEF_FILE;GEN_HDR" # Single value arguments
"DEPENDS" # Multi value arguments
${ARGN}
)
get_fq_target_name(${target_name} fq_target_name)
if(NOT LLVM_LIBC_FULL_BUILD)
add_library(${fq_target_name} INTERFACE)
return()
endif()
if(NOT ADD_GEN_HDR2_DEF_FILE)
message(FATAL_ERROR "`add_gen_hdr2` rule requires DEF_FILE to be specified.")
endif()
if(NOT ADD_GEN_HDR2_GEN_HDR)
message(FATAL_ERROR "`add_gen_hdr2` rule requires GEN_HDR to be specified.")
endif()
if(NOT ADD_GEN_HDR2_YAML_FILE)
message(FATAL_ERROR "`add_gen_hdr2` rule requires YAML_FILE to be specified.")
endif()

set(absolute_path ${CMAKE_CURRENT_SOURCE_DIR}/${ADD_GEN_HDR2_GEN_HDR})
file(RELATIVE_PATH relative_path ${LIBC_INCLUDE_SOURCE_DIR} ${absolute_path})
set(out_file ${LIBC_INCLUDE_DIR}/${relative_path})
set(yaml_file ${CMAKE_SOURCE_DIR}/${ADD_GEN_HDR2_YAML_FILE})
set(def_file ${CMAKE_CURRENT_SOURCE_DIR}/${ADD_GEN_HDR2_DEF_FILE})

set(fq_data_files "")
if(ADD_GEN_HDR2_DATA_FILES)
foreach(data_file IN LISTS ADD_GEN_HDR2_DATA_FILES)
list(APPEND fq_data_files "${CMAKE_CURRENT_SOURCE_DIR}/${data_file}")
endforeach(data_file)
endif()

set(entry_points "${TARGET_ENTRYPOINT_NAME_LIST}")
list(TRANSFORM entry_points PREPEND "--e=")

add_custom_command(
OUTPUT ${out_file}
COMMAND ${Python3_EXECUTABLE} ${LIBC_SOURCE_DIR}/newhdrgen/yaml_to_classes.py
${yaml_file}
--h_def_file ${def_file}
${entry_points}
--output_dir ${out_file}
DEPENDS ${yaml_file} ${def_file} ${fq_data_files}
COMMENT "Generating header ${ADD_GEN_HDR2_GE2N_HDR} from ${yaml_file} and ${def_file}"
)
if(LIBC_TARGET_OS_IS_GPU)
file(MAKE_DIRECTORY ${LIBC_INCLUDE_DIR}/llvm-libc-decls)
file(MAKE_DIRECTORY ${LIBC_INCLUDE_DIR}/llvm-libc-decls/gpu)
set(decl_out_file ${LIBC_INCLUDE_DIR}/llvm-libc-decls/${relative_path})
add_custom_command(
OUTPUT ${decl_out_file}
COMMAND ${Python3_EXECUTABLE} ${LIBC_SOURCE_DIR}/newhdrgen/yaml_to_classes.py
${yaml_file}
--export-decls
${entry_points}
--output_dir ${decl_out_file}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
DEPENDS ${yaml_file} ${fq_data_files}
)
endif()

if(ADD_GEN_HDR2_DEPENDS)
get_fq_deps_list(fq_deps_list ${ADD_GEN_HDR2_DEPENDS})
# Dependencies of a add_header target can only be another add_gen_header target
# or an add_header target.
foreach(dep IN LISTS fq_deps_list)
get_target_property(header_file ${dep} HEADER_FILE_PATH)
if(NOT header_file)
message(FATAL_ERROR "Invalid dependency '${dep}' for '${fq_target_name}'.")
endif()
endforeach()
endif()
set(generated_hdr_target ${fq_target_name}.__generated_hdr__)
add_custom_target(
${generated_hdr_target}
DEPENDS ${out_file} ${fq_deps_list} ${decl_out_file}
)

add_header_library(
${target_name}
HDRS
${out_file}
)

add_dependencies(${fq_target_name} ${generated_hdr_target})

set_target_properties(
${fq_target_name}
PROPERTIES
HEADER_FILE_PATH ${out_file}
DEPS "${fq_deps_list}"
)


endfunction(add_gen_header2)

# Usage:
# add_gen_header(
# <target name>
Expand Down
Loading

0 comments on commit b1fd6f0

Please sign in to comment.