diff --git a/libc/cmake/modules/LLVMLibCHeaderRules.cmake b/libc/cmake/modules/LLVMLibCHeaderRules.cmake index 0de5e14359cfb..a95c13389012f 100644 --- a/libc/cmake/modules/LLVMLibCHeaderRules.cmake +++ b/libc/cmake/modules/LLVMLibCHeaderRules.cmake @@ -75,7 +75,7 @@ function(add_gen_header target_name) cmake_parse_arguments( "ADD_GEN_HDR" "PUBLIC" # No optional arguments - "YAML_FILE;DEF_FILE;GEN_HDR" # Single value arguments + "YAML_FILE;GEN_HDR" # Single value arguments "DEPENDS" # Multi value arguments ${ARGN} ) @@ -84,9 +84,6 @@ function(add_gen_header target_name) add_library(${fq_target_name} INTERFACE) return() endif() - if(NOT ADD_GEN_HDR_DEF_FILE) - message(FATAL_ERROR "`add_gen_hdr` rule requires DEF_FILE to be specified.") - endif() if(NOT ADD_GEN_HDR_GEN_HDR) message(FATAL_ERROR "`add_gen_hdr` rule requires GEN_HDR to be specified.") endif() @@ -97,8 +94,8 @@ function(add_gen_header target_name) set(absolute_path ${CMAKE_CURRENT_SOURCE_DIR}/${ADD_GEN_HDR_GEN_HDR}) file(RELATIVE_PATH relative_path ${LIBC_INCLUDE_SOURCE_DIR} ${absolute_path}) set(out_file ${LIBC_INCLUDE_DIR}/${relative_path}) + set(dep_file "${out_file}.d") set(yaml_file ${CMAKE_SOURCE_DIR}/${ADD_GEN_HDR_YAML_FILE}) - set(def_file ${CMAKE_CURRENT_SOURCE_DIR}/${ADD_GEN_HDR_DEF_FILE}) set(fq_data_files "") if(ADD_GEN_HDR_DATA_FILES) @@ -108,18 +105,19 @@ function(add_gen_header target_name) endif() set(entry_points "${TARGET_ENTRYPOINT_NAME_LIST}") - list(TRANSFORM entry_points PREPEND "--e=") + list(TRANSFORM entry_points PREPEND "--entry-point=") - set(LIBC_HDRGEN "${LIBC_SOURCE_DIR}/utils/hdrgen/yaml_to_classes.py") add_custom_command( OUTPUT ${out_file} - COMMAND ${Python3_EXECUTABLE} ${LIBC_HDRGEN} - ${yaml_file} - --h_def_file ${def_file} + WORKING_DIRECTORY ${CMAKE_BINARY_DIR} + COMMAND ${Python3_EXECUTABLE} "${LIBC_SOURCE_DIR}/utils/hdrgen/main.py" + --output ${out_file} + --depfile ${dep_file} ${entry_points} - --output_dir ${out_file} - DEPENDS ${yaml_file} ${def_file} ${fq_data_files} - COMMENT "Generating header ${ADD_GEN_HDR_GEN_HDR} from ${yaml_file} and ${def_file}" + ${yaml_file} + DEPENDS ${yaml_file} ${fq_data_files} + DEPFILE ${dep_file} + COMMENT "Generating header ${ADD_GEN_HDR_GEN_HDR} from ${yaml_file}" ) if(LIBC_TARGET_OS_IS_GPU) file(MAKE_DIRECTORY ${LIBC_INCLUDE_DIR}/llvm-libc-decls) @@ -127,7 +125,7 @@ function(add_gen_header target_name) set(decl_out_file ${LIBC_INCLUDE_DIR}/llvm-libc-decls/${relative_path}) add_custom_command( OUTPUT ${decl_out_file} - COMMAND ${Python3_EXECUTABLE} ${LIBC_HDRGEN} + COMMAND ${Python3_EXECUTABLE} "${LIBC_SOURCE_DIR}/utils/hdrgen/yaml_to_classes.py" ${yaml_file} --export-decls ${entry_points} diff --git a/libc/include/CMakeLists.txt b/libc/include/CMakeLists.txt index eb407183c99f5..568bb05d92302 100644 --- a/libc/include/CMakeLists.txt +++ b/libc/include/CMakeLists.txt @@ -19,11 +19,10 @@ add_header( # TODO: Can we simplify this macro expansion? # https://github.com/llvm/llvm-project/issues/117254 -macro(add_header_macro TARGET_NAME YAML_FILE DEF_FILE GEN_HDR DEPENDS) +macro(add_header_macro TARGET_NAME YAML_FILE GEN_HDR DEPENDS) add_gen_header( ${TARGET_NAME} YAML_FILE ${YAML_FILE} - DEF_FILE ${DEF_FILE} GEN_HDR ${GEN_HDR} ${DEPENDS} ${ARGN} @@ -33,7 +32,6 @@ endmacro() add_header_macro( ctype ../libc/include/ctype.yaml - ctype.h.def ctype.h DEPENDS .llvm_libc_common_h @@ -43,7 +41,6 @@ add_header_macro( add_header_macro( dirent ../libc/include/dirent.yaml - dirent.h.def dirent.h DEPENDS .llvm_libc_common_h @@ -55,7 +52,6 @@ add_header_macro( add_header_macro( fcntl ../libc/include/fcntl.yaml - fcntl.h.def fcntl.h DEPENDS .llvm-libc-macros.fcntl_macros @@ -71,7 +67,6 @@ add_header_macro( add_header_macro( dlfcn ../libc/include/dlfcn.yaml - dlfcn.h.def dlfcn.h DEPENDS .llvm-libc-macros.dlfcn_macros @@ -81,7 +76,6 @@ add_header_macro( add_header_macro( features ../libc/include/features.yaml - features.h.def features.h DEPENDS .llvm_libc_common_h @@ -91,7 +85,6 @@ add_header_macro( add_header_macro( fenv ../libc/include/fenv.yaml - fenv.h.def fenv.h DEPENDS .llvm_libc_common_h @@ -103,7 +96,6 @@ add_header_macro( add_header_macro( inttypes ../libc/include/inttypes.yaml - inttypes.h.def inttypes.h DEPENDS .llvm_libc_common_h @@ -114,7 +106,6 @@ add_header_macro( add_header_macro( float ../libc/include/float.yaml - float.h.def float.h DEPENDS .llvm-libc-macros.float_macros @@ -123,7 +114,6 @@ add_header_macro( add_header_macro( stdint ../libc/include/stdint.yaml - stdint.h.def stdint.h DEPENDS .llvm-libc-macros.stdint_macros @@ -132,7 +122,6 @@ add_header_macro( add_header_macro( limits ../libc/include/limits.yaml - limits.h.def limits.h DEPENDS .llvm-libc-macros.limits_macros @@ -141,7 +130,6 @@ add_header_macro( add_header_macro( malloc ../libc/include/malloc.yaml - malloc.h.def malloc.h DEPENDS .llvm_libc_common_h @@ -151,7 +139,6 @@ add_header_macro( add_header_macro( math ../libc/include/math.yaml - math.h.def math.h DEPENDS .llvm_libc_common_h @@ -166,7 +153,6 @@ add_header_macro( add_header_macro( stdfix ../libc/include/stdfix.yaml - stdfix.h.def stdfix.h DEPENDS .llvm-libc-macros.stdfix_macros @@ -179,7 +165,6 @@ file(MAKE_DIRECTORY ${LIBC_INCLUDE_DIR}/arpa) add_header_macro( arpa_inet ../libc/include/arpa/inet.yaml - arpa/inet.h.def arpa/inet.h DEPENDS .llvm_libc_common_h @@ -188,7 +173,6 @@ add_header_macro( add_header_macro( assert ../libc/include/assert.yaml - assert.h.def assert.h DEPENDS .llvm_libc_common_h @@ -198,7 +182,6 @@ add_header_macro( add_header_macro( complex ../libc/include/complex.yaml - complex.h.def complex.h DEPENDS .llvm_libc_common_h @@ -208,7 +191,6 @@ add_header_macro( add_header_macro( setjmp ../libc/include/setjmp.yaml - setjmp.h.def setjmp.h DEPENDS .llvm_libc_common_h @@ -218,7 +200,6 @@ add_header_macro( add_header_macro( string ../libc/include/string.yaml - string.h.def string.h DEPENDS .llvm_libc_common_h @@ -229,7 +210,6 @@ add_header_macro( add_header_macro( strings ../libc/include/strings.yaml - strings.h.def strings.h DEPENDS .llvm_libc_common_h @@ -239,7 +219,6 @@ add_header_macro( add_header_macro( search ../libc/include/search.yaml - search.h.def search.h DEPENDS .llvm_libc_common_h @@ -253,7 +232,6 @@ add_header_macro( add_header_macro( time ../libc/include/time.yaml - time.h.def time.h DEPENDS .llvm_libc_common_h @@ -269,7 +247,6 @@ add_header_macro( add_header_macro( threads ../libc/include/threads.yaml - threads.h.def threads.h DEPENDS .llvm_libc_common_h @@ -286,7 +263,6 @@ add_header_macro( add_header_macro( errno ../libc/include/errno.yaml - errno.h.def errno.h DEPENDS .llvm-libc-macros.generic_error_number_macros @@ -296,7 +272,6 @@ add_header_macro( add_header_macro( signal ../libc/include/signal.yaml - signal.h.def signal.h DEPENDS .llvm-libc-macros.signal_macros @@ -312,7 +287,6 @@ add_header_macro( add_header_macro( stdbit ../libc/include/stdbit.yaml - stdbit.h.def stdbit.h DEPENDS .llvm_libc_common_h @@ -322,7 +296,6 @@ add_header_macro( add_header_macro( stdckdint ../libc/include/stdckdint.yaml - stdckdint.h.def stdckdint.h DEPENDS .llvm_libc_common_h @@ -332,7 +305,6 @@ add_header_macro( add_header_macro( stdio ../libc/include/stdio.yaml - stdio.h.def stdio.h DEPENDS .llvm-libc-macros.file_seek_macros @@ -348,7 +320,6 @@ add_header_macro( add_header_macro( stdlib ../libc/include/stdlib.yaml - stdlib.h.def stdlib.h DEPENDS .llvm_libc_common_h @@ -367,7 +338,6 @@ add_header_macro( add_header_macro( unistd ../libc/include/unistd.yaml - unistd.h.def unistd.h DEPENDS .llvm_libc_common_h @@ -386,7 +356,6 @@ add_header_macro( add_header_macro( pthread ../libc/include/pthread.yaml - pthread.h.def pthread.h DEPENDS .llvm-libc-macros.pthread_macros @@ -410,7 +379,6 @@ add_header_macro( add_header_macro( sched ../libc/include/sched.yaml - sched.h.def sched.h DEPENDS .llvm_libc_common_h @@ -427,7 +395,6 @@ add_header_macro( add_header_macro( spawn ../libc/include/spawn.yaml - spawn.h.def spawn.h DEPENDS .llvm_libc_common_h @@ -440,7 +407,6 @@ add_header_macro( add_header_macro( link ../libc/include/link.yaml - link.h.def link.h DEPENDS .llvm_libc_common_h @@ -450,7 +416,6 @@ add_header_macro( add_header_macro( elf ../libc/include/elf.yaml - elf.h.def elf.h DEPENDS .llvm-libc-macros.elf_macros @@ -464,7 +429,6 @@ file(MAKE_DIRECTORY ${LIBC_INCLUDE_DIR}/sys) add_header_macro( sys_auxv ../libc/include/sys/auxv.yaml - sys/auxv.h.def sys/auxv.h DEPENDS .llvm_libc_common_h @@ -474,7 +438,6 @@ add_header_macro( add_header_macro( sys_epoll ../libc/include/sys/epoll.yaml - sys/epoll.h.def sys/epoll.h DEPENDS .llvm_libc_common_h @@ -487,7 +450,6 @@ add_header_macro( add_header_macro( sys_ioctl ../libc/include/sys/ioctl.yaml - sys/ioctl.h.def sys/ioctl.h DEPENDS .llvm_libc_common_h @@ -497,7 +459,6 @@ add_header_macro( add_header_macro( sys_mman ../libc/include/sys/mman.yaml - sys/mman.h.def sys/mman.h DEPENDS .llvm_libc_common_h @@ -510,7 +471,6 @@ add_header_macro( add_header_macro( sys_prctl ../libc/include/sys/prctl.yaml - sys/prctl.h.def sys/prctl.h DEPENDS .llvm_libc_common_h @@ -527,7 +487,6 @@ add_header( add_header_macro( sys_random ../libc/include/sys/random.yaml - sys/random.h.def sys/random.h DEPENDS .llvm_libc_common_h @@ -539,7 +498,6 @@ add_header_macro( add_header_macro( sys_resource ../libc/include/sys/resource.yaml - sys/resource.h.def sys/resource.h DEPENDS .llvm_libc_common_h @@ -551,7 +509,6 @@ add_header_macro( add_header_macro( sys_stat ../libc/include/sys/stat.yaml - sys/stat.h.def sys/stat.h DEPENDS .llvm_libc_common_h @@ -573,7 +530,6 @@ add_header_macro( add_header_macro( sys_select ../libc/include/sys/select.yaml - sys/select.h.def sys/select.h DEPENDS .llvm_libc_common_h @@ -589,7 +545,6 @@ add_header_macro( add_header_macro( sys_sendfile ../libc/include/sys/sendfile.yaml - sys/sendfile.h.def sys/sendfile.h DEPENDS .llvm_libc_common_h @@ -601,7 +556,6 @@ add_header_macro( add_header_macro( sys_socket ../libc/include/sys/socket.yaml - sys/socket.h.def sys/socket.h DEPENDS .llvm_libc_common_h @@ -617,7 +571,6 @@ add_header_macro( add_header_macro( sys_statvfs ../libc/include/sys/statvfs.yaml - sys/statvfs.h.def sys/statvfs.h DEPENDS .llvm_libc_common_h @@ -627,7 +580,6 @@ add_header_macro( add_header_macro( sys_syscall ../libc/include/sys/syscall.yaml - sys/syscall.h.def sys/syscall.h DEPENDS ) @@ -635,7 +587,6 @@ add_header_macro( add_header_macro( sys_time ../libc/include/sys/time.yaml - sys/time.h.def sys/time.h DEPENDS .llvm_libc_common_h @@ -646,7 +597,6 @@ add_header_macro( add_header_macro( sys_types ../libc/include/sys/types.yaml - sys/types.h.def sys/types.h DEPENDS .llvm_libc_common_h @@ -676,7 +626,6 @@ add_header_macro( add_header_macro( sys_utsname ../libc/include/sys/utsname.yaml - sys/utsname.h.def sys/utsname.h DEPENDS .llvm_libc_common_h @@ -686,7 +635,6 @@ add_header_macro( add_header_macro( sys_wait ../libc/include/sys/wait.yaml - sys/wait.h.def sys/wait.h DEPENDS .llvm_libc_common_h @@ -699,7 +647,6 @@ add_header_macro( add_header_macro( termios ../libc/include/termios.yaml - termios.h.def termios.h DEPENDS .llvm_libc_common_h @@ -714,7 +661,6 @@ add_header_macro( add_header_macro( uchar ../libc/include/uchar.yaml - uchar.h.def uchar.h DEPENDS .llvm_libc_common_h @@ -727,7 +673,6 @@ add_header_macro( add_header_macro( wchar ../libc/include/wchar.yaml - wchar.h.def wchar.h DEPENDS .llvm_libc_common_h @@ -741,7 +686,6 @@ add_header_macro( add_header_macro( locale ../libc/include/locale.yaml - locale.h.def locale.h DEPENDS .llvm_libc_common_h diff --git a/libc/include/arpa/inet.yaml b/libc/include/arpa/inet.yaml index cb366e0f5d694..10cd56d6ce786 100644 --- a/libc/include/arpa/inet.yaml +++ b/libc/include/arpa/inet.yaml @@ -1,4 +1,5 @@ -header: arpa-inet.h +header: arpa/inet.h +header_template: inet.h.def macros: [] types: [] enums: [] diff --git a/libc/include/assert.yaml b/libc/include/assert.yaml index f740554488ed5..1a3bdeda7e542 100644 --- a/libc/include/assert.yaml +++ b/libc/include/assert.yaml @@ -1,4 +1,5 @@ header: assert.h +header_template: assert.h.def macros: [] types: [] enums: [] diff --git a/libc/include/complex.yaml b/libc/include/complex.yaml index cd81de7dd9e20..05318480a02f1 100644 --- a/libc/include/complex.yaml +++ b/libc/include/complex.yaml @@ -1,4 +1,5 @@ header: complex.h +header_template: complex.h.def macros: [] types: - type_name: cfloat16 diff --git a/libc/include/ctype.yaml b/libc/include/ctype.yaml index b4823c3e53234..6238f1b889986 100644 --- a/libc/include/ctype.yaml +++ b/libc/include/ctype.yaml @@ -1,4 +1,5 @@ header: ctype.h +header_template: ctype.h.def macros: [] types: - type_name: locale_t diff --git a/libc/include/dirent.yaml b/libc/include/dirent.yaml index cdccf6a0c7f29..3fc522fda80e4 100644 --- a/libc/include/dirent.yaml +++ b/libc/include/dirent.yaml @@ -1,4 +1,5 @@ header: dirent.h +header_template: dirent.h.def macros: [] types: - type_name: struct_dirent diff --git a/libc/include/dlfcn.yaml b/libc/include/dlfcn.yaml index 725ee705714a7..9e8803cb5fa78 100644 --- a/libc/include/dlfcn.yaml +++ b/libc/include/dlfcn.yaml @@ -1,4 +1,5 @@ header: dlfcn.h +header_template: dlfcn.h.def macros: - macro_name: RTLD_LAZY macro_value: null diff --git a/libc/include/elf.yaml b/libc/include/elf.yaml index 2e9db329e2297..f78ae82c77850 100644 --- a/libc/include/elf.yaml +++ b/libc/include/elf.yaml @@ -1,4 +1,5 @@ header: elf.h +header_template: elf.h.def standards: - Linux macros: [] diff --git a/libc/include/errno.yaml b/libc/include/errno.yaml index a894063a1ee2c..188a9fa1211a1 100644 --- a/libc/include/errno.yaml +++ b/libc/include/errno.yaml @@ -1,4 +1,5 @@ header: errno.h +header_template: errno.h.def standards: - stdc - Linux diff --git a/libc/include/fcntl.yaml b/libc/include/fcntl.yaml index 71c0df3b0fada..78f93533b84d3 100644 --- a/libc/include/fcntl.yaml +++ b/libc/include/fcntl.yaml @@ -1,4 +1,5 @@ header: fcntl.h +header_template: fcntl.h.def macros: [] types: - type_name: off_t diff --git a/libc/include/features.yaml b/libc/include/features.yaml index a18af22edb743..726320a40881d 100644 --- a/libc/include/features.yaml +++ b/libc/include/features.yaml @@ -1,4 +1,5 @@ header: features.h +header_template: features.h.def standards: - stdc macros: [] diff --git a/libc/include/fenv.yaml b/libc/include/fenv.yaml index 1010efc6402c1..1ecaf63085504 100644 --- a/libc/include/fenv.yaml +++ b/libc/include/fenv.yaml @@ -1,4 +1,5 @@ header: fenv.h +header_template: fenv.h.def macros: [] types: - type_name: fenv_t diff --git a/libc/include/float.yaml b/libc/include/float.yaml index 63639a6e8ed13..21df6513e77e4 100644 --- a/libc/include/float.yaml +++ b/libc/include/float.yaml @@ -1,4 +1,5 @@ header: float.h +header_template: float.h.def standards: - stdc macros: [] diff --git a/libc/include/inttypes.yaml b/libc/include/inttypes.yaml index ad636cc5121a1..d5dec5b465ba4 100644 --- a/libc/include/inttypes.yaml +++ b/libc/include/inttypes.yaml @@ -1,4 +1,5 @@ header: inttypes.h +header_template: inttypes.h.def macros: [] types: - type_name: imaxdiv_t diff --git a/libc/include/limits.yaml b/libc/include/limits.yaml index bf33ed24e7a8d..b664041bb56c2 100644 --- a/libc/include/limits.yaml +++ b/libc/include/limits.yaml @@ -1,4 +1,5 @@ header: limits.h +header_template: limits.h.def standards: - stdc macros: [] diff --git a/libc/include/link.yaml b/libc/include/link.yaml index d1963a86813af..1cd609e292b53 100644 --- a/libc/include/link.yaml +++ b/libc/include/link.yaml @@ -1,4 +1,5 @@ header: link.h +header_template: link.h.def standards: - Linux macros: [] diff --git a/libc/include/locale.yaml b/libc/include/locale.yaml index 7da7966ea730f..9ff53c16398a5 100644 --- a/libc/include/locale.yaml +++ b/libc/include/locale.yaml @@ -1,4 +1,5 @@ header: locale.h +header_template: locale.h.def functions: - name: localeconv standards: diff --git a/libc/include/malloc.yaml b/libc/include/malloc.yaml index 8db4f3aebb9b3..ec73c9090f729 100644 --- a/libc/include/malloc.yaml +++ b/libc/include/malloc.yaml @@ -1,4 +1,5 @@ header: malloc.h +header_template: malloc.h.def macros: [] types: [] enums: [] diff --git a/libc/include/math.yaml b/libc/include/math.yaml index 3b8caec66bbfd..831d045745677 100644 --- a/libc/include/math.yaml +++ b/libc/include/math.yaml @@ -1,4 +1,5 @@ header: math.h +header_template: math.h.def macros: [] types: - type_name: float_t diff --git a/libc/include/pthread.yaml b/libc/include/pthread.yaml index b9068c3f17657..4f386bdd11cfd 100644 --- a/libc/include/pthread.yaml +++ b/libc/include/pthread.yaml @@ -1,4 +1,5 @@ header: pthread.h +header_template: pthread.h.def macros: [] types: - type_name: pthread_t diff --git a/libc/include/sched.yaml b/libc/include/sched.yaml index 2d4876b722ab2..57871f524bf11 100644 --- a/libc/include/sched.yaml +++ b/libc/include/sched.yaml @@ -1,4 +1,5 @@ header: sched.h +header_template: sched.h.def macros: [] types: - type_name: struct_timespec diff --git a/libc/include/search.yaml b/libc/include/search.yaml index a0c73bc679d81..b7ce06d48e704 100644 --- a/libc/include/search.yaml +++ b/libc/include/search.yaml @@ -1,4 +1,5 @@ header: search.h +header_template: search.h.def macros: [] types: - type_name: struct_hsearch_data diff --git a/libc/include/setjmp.yaml b/libc/include/setjmp.yaml index 68e3ff046e4b8..2c4f7fb6dfcc7 100644 --- a/libc/include/setjmp.yaml +++ b/libc/include/setjmp.yaml @@ -1,4 +1,5 @@ header: setjmp.h +header_template: setjmp.h.def macros: [] types: - type_name: jmp_buf diff --git a/libc/include/signal.yaml b/libc/include/signal.yaml index c66abb1a87441..576e77576ac74 100644 --- a/libc/include/signal.yaml +++ b/libc/include/signal.yaml @@ -1,4 +1,5 @@ header: signal.h +header_template: signal.h.def macros: [] types: - type_name: pid_t diff --git a/libc/include/spawn.yaml b/libc/include/spawn.yaml index be3f4e99d27fc..e725ab9719eda 100644 --- a/libc/include/spawn.yaml +++ b/libc/include/spawn.yaml @@ -1,4 +1,5 @@ header: spawn.h +header_template: spawn.h.def macros: [] types: - type_name: posix_spawn_file_actions_t diff --git a/libc/include/stdbit.yaml b/libc/include/stdbit.yaml index 25d2d326c30eb..e9bd6b3918e78 100644 --- a/libc/include/stdbit.yaml +++ b/libc/include/stdbit.yaml @@ -1,4 +1,5 @@ header: stdbit.h +header_template: stdbit.h.def macros: [] types: [] enums: [] diff --git a/libc/include/stdckdint.yaml b/libc/include/stdckdint.yaml index ea8fc47625b03..e8b2e80ee029f 100644 --- a/libc/include/stdckdint.yaml +++ b/libc/include/stdckdint.yaml @@ -1,4 +1,5 @@ header: stdckdint.h +header_template: stdckdint.h.def standards: - stdc macros: [] diff --git a/libc/include/stdfix.yaml b/libc/include/stdfix.yaml index 9787eaba45e4e..7b3bdba082dd5 100644 --- a/libc/include/stdfix.yaml +++ b/libc/include/stdfix.yaml @@ -1,4 +1,5 @@ header: stdfix.h +header_template: stdfix.h.def macros: [] types: - type_name: stdfix-types diff --git a/libc/include/stdint.yaml b/libc/include/stdint.yaml index 8887f596bc8aa..d583a104af374 100644 --- a/libc/include/stdint.yaml +++ b/libc/include/stdint.yaml @@ -1,4 +1,5 @@ header: stdint.h +header_template: stdint.h.def standards: - stdc macros: [] diff --git a/libc/include/stdio.yaml b/libc/include/stdio.yaml index fd116bbc00895..2619984cca264 100644 --- a/libc/include/stdio.yaml +++ b/libc/include/stdio.yaml @@ -1,4 +1,5 @@ header: stdio.h +header_template: stdio.h.def macros: - macro_name: stdout macro_value: stdout diff --git a/libc/include/stdlib.yaml b/libc/include/stdlib.yaml index c6c95e421cee3..4b68f272613b1 100644 --- a/libc/include/stdlib.yaml +++ b/libc/include/stdlib.yaml @@ -1,4 +1,5 @@ header: stdlib.h +header_template: stdlib.h.def standards: - stdc macros: [] diff --git a/libc/include/string.yaml b/libc/include/string.yaml index af1750e91243e..deded309abc2c 100644 --- a/libc/include/string.yaml +++ b/libc/include/string.yaml @@ -1,4 +1,5 @@ header: string.h +header_template: string.h.def macros: [] types: - type_name: size_t diff --git a/libc/include/strings.yaml b/libc/include/strings.yaml index ca91b626740c1..e672dca6a94dd 100644 --- a/libc/include/strings.yaml +++ b/libc/include/strings.yaml @@ -1,4 +1,5 @@ header: strings.h +header_template: strings.h.def macros: [] types: [] enums: [] diff --git a/libc/include/sys/auxv.yaml b/libc/include/sys/auxv.yaml index 9d546b3588243..82ecee75c40a1 100644 --- a/libc/include/sys/auxv.yaml +++ b/libc/include/sys/auxv.yaml @@ -1,4 +1,5 @@ -header: sys-auxv.h +header: sys/auxv.h +header_template: auxv.h.def macros: [] types: [] enums: [] diff --git a/libc/include/sys/epoll.yaml b/libc/include/sys/epoll.yaml index ee188c17fedc6..996eb785bc715 100644 --- a/libc/include/sys/epoll.yaml +++ b/libc/include/sys/epoll.yaml @@ -1,4 +1,5 @@ -header: sys-epoll.h +header: sys/epoll.h +header_template: epoll.h.def macros: [] types: - type_name: struct_epoll_event diff --git a/libc/include/sys/ioctl.yaml b/libc/include/sys/ioctl.yaml index ffe73a84d51b9..5f7b7f333191e 100644 --- a/libc/include/sys/ioctl.yaml +++ b/libc/include/sys/ioctl.yaml @@ -1,4 +1,5 @@ -header: sys-ioctl.h +header: sys/ioctl.h +header_template: ioctl.h.def standards: POSIX macros: [] types: [] diff --git a/libc/include/sys/mman.yaml b/libc/include/sys/mman.yaml index 962ca3591917f..8c207552f9805 100644 --- a/libc/include/sys/mman.yaml +++ b/libc/include/sys/mman.yaml @@ -1,4 +1,5 @@ -header: sys-mman.h +header: sys/mman.h +header_template: mman.h.def macros: [] types: - type_name: mode_t diff --git a/libc/include/sys/prctl.yaml b/libc/include/sys/prctl.yaml index 82374be87d5d0..53f57645b18e2 100644 --- a/libc/include/sys/prctl.yaml +++ b/libc/include/sys/prctl.yaml @@ -1,4 +1,5 @@ -header: sys-prctl.h +header: sys/prctl.h +header_template: prctl.h.def macros: [] types: [] enums: [] diff --git a/libc/include/sys/random.yaml b/libc/include/sys/random.yaml index 228bb50d5db99..4efb2fbb44733 100644 --- a/libc/include/sys/random.yaml +++ b/libc/include/sys/random.yaml @@ -1,4 +1,5 @@ -header: sys-random.h +header: sys/random.h +header_template: random.h.def macros: [] types: - type_name: ssize_t diff --git a/libc/include/sys/resource.yaml b/libc/include/sys/resource.yaml index 85ea1ad12f19f..3652d6d490a49 100644 --- a/libc/include/sys/resource.yaml +++ b/libc/include/sys/resource.yaml @@ -1,4 +1,5 @@ -header: sys-resource.h +header: sys/resource.h +header_template: resource.h.def macros: [] types: - type_name: struct_rlimit diff --git a/libc/include/sys/select.yaml b/libc/include/sys/select.yaml index c6806122aa816..6066fd341f077 100644 --- a/libc/include/sys/select.yaml +++ b/libc/include/sys/select.yaml @@ -1,4 +1,5 @@ -header: sys-select.h +header: sys/select.h +header_template: select.h.def macros: [] types: - type_name: struct_timeval diff --git a/libc/include/sys/sendfile.yaml b/libc/include/sys/sendfile.yaml index 7e45e40e171da..259ab83dff54b 100644 --- a/libc/include/sys/sendfile.yaml +++ b/libc/include/sys/sendfile.yaml @@ -1,4 +1,5 @@ -header: sys-sendfile.h +header: sys/sendfile.h +header_template: sendfile.h.def macros: [] types: - type_name: ssize_t diff --git a/libc/include/sys/socket.yaml b/libc/include/sys/socket.yaml index 47d835fa5f4a1..00d5de6af8a80 100644 --- a/libc/include/sys/socket.yaml +++ b/libc/include/sys/socket.yaml @@ -1,4 +1,5 @@ -header: sys-socket.h +header: sys/socket.h +header_template: socket.h.def macros: [] types: - type_name: struct_sockaddr_un diff --git a/libc/include/sys/stat.yaml b/libc/include/sys/stat.yaml index ed500f832f90e..7f013420818ab 100644 --- a/libc/include/sys/stat.yaml +++ b/libc/include/sys/stat.yaml @@ -1,4 +1,5 @@ -header: sys-stat.h +header: sys/stat.h +header_template: stat.h.def macros: [] types: - type_name: blkcnt_t diff --git a/libc/include/sys/statvfs.yaml b/libc/include/sys/statvfs.yaml index 22e0ef22c4139..8c1d254add37f 100644 --- a/libc/include/sys/statvfs.yaml +++ b/libc/include/sys/statvfs.yaml @@ -1,4 +1,5 @@ -header: sys-statvfs.h +header: sys/statvfs.h +header_template: statvfs.h.def macros: [] types: - type_name: struct_statvfs diff --git a/libc/include/sys/syscall.yaml b/libc/include/sys/syscall.yaml index c0a64338b6f7e..879d95c2ea39c 100644 --- a/libc/include/sys/syscall.yaml +++ b/libc/include/sys/syscall.yaml @@ -1,4 +1,5 @@ -header: sys-syscall.h +header: sys/syscall.h +header_template: syscall.h.def standards: Linux macros: [] types: [] diff --git a/libc/include/sys/time.yaml b/libc/include/sys/time.yaml index eb3dd548389b3..687c1f83028d0 100644 --- a/libc/include/sys/time.yaml +++ b/libc/include/sys/time.yaml @@ -1,4 +1,5 @@ -header: sys-time.h +header: sys/time.h +header_template: time.h.def standards: Linux macros: [] types: [] diff --git a/libc/include/sys/types.yaml b/libc/include/sys/types.yaml index 15eaf107f6915..6fa0b448fcd38 100644 --- a/libc/include/sys/types.yaml +++ b/libc/include/sys/types.yaml @@ -1,4 +1,5 @@ -header: sys-types.h +header: sys/types.h +header_template: types.h.def standards: POSIX macros: [] types: diff --git a/libc/include/sys/utsname.yaml b/libc/include/sys/utsname.yaml index eecd55b180829..6c7cb71f9a34f 100644 --- a/libc/include/sys/utsname.yaml +++ b/libc/include/sys/utsname.yaml @@ -1,4 +1,5 @@ -header: sys-utsname.h +header: sys/utsname.h +header_template: utsname.h.def macros: [] types: - type_name: struct_utsname diff --git a/libc/include/sys/wait.yaml b/libc/include/sys/wait.yaml index 4f0c69baee2c4..6257e34b9e08e 100644 --- a/libc/include/sys/wait.yaml +++ b/libc/include/sys/wait.yaml @@ -1,4 +1,5 @@ -header: sys-wait.h +header: sys/wait.h +header_template: wait.h.def macros: [] types: - type_name: siginfo_t diff --git a/libc/include/termios.yaml b/libc/include/termios.yaml index e9c4cd375d0b9..8815097264f95 100644 --- a/libc/include/termios.yaml +++ b/libc/include/termios.yaml @@ -1,4 +1,5 @@ header: termios.h +header_template: termios.h.def macros: [] types: - type_name: tcflag_t diff --git a/libc/include/threads.yaml b/libc/include/threads.yaml index aadcaf5f66e06..7014822f9251d 100644 --- a/libc/include/threads.yaml +++ b/libc/include/threads.yaml @@ -1,4 +1,5 @@ header: threads.h +header_template: threads.h.def macros: - macro_name: ONCE_FLAG_INIT macro_value: '{0}' diff --git a/libc/include/time.yaml b/libc/include/time.yaml index 3f745e5ee3386..b71b9ab72075b 100644 --- a/libc/include/time.yaml +++ b/libc/include/time.yaml @@ -1,4 +1,5 @@ header: time.h +header_template: time.h.def macros: [] types: - type_name: struct_timeval diff --git a/libc/include/uchar.yaml b/libc/include/uchar.yaml index 18ca840612e07..713919796762d 100644 --- a/libc/include/uchar.yaml +++ b/libc/include/uchar.yaml @@ -1,4 +1,5 @@ header: uchar.h +header_template: uchar.h.def standards: - stdc macros: [] diff --git a/libc/include/unistd.yaml b/libc/include/unistd.yaml index c6441c04ce3a3..fada365e0103d 100644 --- a/libc/include/unistd.yaml +++ b/libc/include/unistd.yaml @@ -1,4 +1,5 @@ header: unistd.h +header_template: unistd.h.def macros: [] types: - type_name: uid_t diff --git a/libc/include/wchar.yaml b/libc/include/wchar.yaml index bc824b21d8be1..27a5926b57455 100644 --- a/libc/include/wchar.yaml +++ b/libc/include/wchar.yaml @@ -1,4 +1,5 @@ header: wchar.h +header_template: wchar.h.def macros: [] types: - type_name: size_t diff --git a/libc/utils/hdrgen/header.py b/libc/utils/hdrgen/header.py index df8ce613bd0f9..9339acceaf7a9 100644 --- a/libc/utils/hdrgen/header.py +++ b/libc/utils/hdrgen/header.py @@ -9,6 +9,7 @@ class HeaderFile: def __init__(self, name): + self.template_file = None self.name = name self.macros = [] self.types = [] @@ -31,7 +32,7 @@ def add_object(self, object): def add_function(self, function): self.functions.append(function) - def __str__(self): + def public_api(self): content = [""] for macro in self.macros: diff --git a/libc/utils/hdrgen/main.py b/libc/utils/hdrgen/main.py new file mode 100755 index 0000000000000..2bba1efa04e42 --- /dev/null +++ b/libc/utils/hdrgen/main.py @@ -0,0 +1,81 @@ +#!/usr/bin/env python3 +# +# ===- Generate headers for libc functions ------------------*- python -*--==# +# +# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +# See https://llvm.org/LICENSE.txt for license information. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# +# ==------------------------------------------------------------------------==# + +import argparse +import sys +from pathlib import Path + +from header import HeaderFile +from yaml_to_classes import load_yaml_file, fill_public_api + + +def main(): + parser = argparse.ArgumentParser(description="Generate header files from YAML") + parser.add_argument( + "yaml_file", + help="Path to the YAML file containing header specification", + metavar="FILE", + type=Path, + nargs=1, + ) + parser.add_argument( + "-o", + "--output", + help="Path to write generated header file", + type=Path, + required=True, + ) + parser.add_argument( + "--depfile", + help="Path to write a depfile", + type=Path, + ) + parser.add_argument( + "-e", + "--entry-point", + help="Entry point to include; may be given many times", + metavar="SYMBOL", + action="append", + ) + args = parser.parse_args() + + [yaml_file] = args.yaml_file + files_read = {yaml_file} + + def write_depfile(): + if not args.depfile: + return + deps = " ".join(str(f) for f in sorted(files_read)) + args.depfile.parent.mkdir(parents=True, exist_ok=True) + with open(args.depfile, "w") as depfile: + depfile.write(f"{args.output}: {deps}\n") + + header = load_yaml_file(yaml_file, HeaderFile, args.entry_point) + + if not header.template_file: + print(f"{yaml_file}: Missing header_template", sys.stderr) + return 2 + + # The header_template path is relative to the containing YAML file. + template_path = yaml_file.parent / header.template_file + + files_read.add(template_path) + with open(template_path) as template: + contents = fill_public_api(header.public_api(), template.read()) + + write_depfile() + + args.output.parent.mkdir(parents=True, exist_ok=True) + with open(args.output, "w") as out: + out.write(contents) + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/libc/utils/hdrgen/tests/input/test_small.yaml b/libc/utils/hdrgen/tests/input/test_small.yaml index 772552faf81dc..1d4b2990a3002 100644 --- a/libc/utils/hdrgen/tests/input/test_small.yaml +++ b/libc/utils/hdrgen/tests/input/test_small.yaml @@ -1,4 +1,5 @@ -header: test_header.h +header: test_small.h +header_template: test_small.h.def macros: - macro_name: MACRO_A macro_value: 1 @@ -62,5 +63,3 @@ functions: - type: float standards: - stdc - - diff --git a/libc/utils/hdrgen/tests/test_integration.py b/libc/utils/hdrgen/tests/test_integration.py index ce80026e7bccd..49cb08cd1b339 100644 --- a/libc/utils/hdrgen/tests/test_integration.py +++ b/libc/utils/hdrgen/tests/test_integration.py @@ -1,36 +1,27 @@ +import argparse import subprocess +import sys import unittest from pathlib import Path -import os -import argparse -import sys class TestHeaderGenIntegration(unittest.TestCase): def setUp(self): - self.output_dir = Path( - args.output_dir if args.output_dir else "libc/utils/hdrgen/tests/output" - ) - - self.maxDiff = None - - self.source_dir = Path(__file__).resolve().parent.parent.parent.parent.parent + self.output_dir = TestHeaderGenIntegration.output_dir + self.source_dir = Path(__file__).parent + self.main_script = self.source_dir.parent / "main.py" - def run_script(self, yaml_file, h_def_file, output_dir, entry_points): - yaml_file = self.source_dir / yaml_file - h_def_file = self.source_dir / h_def_file + def run_script(self, yaml_file, output_file, entry_points): command = [ "python3", - str(self.source_dir / "libc/utils/hdrgen/yaml_to_classes.py"), + str(self.main_script), str(yaml_file), - "--h_def_file", - str(h_def_file), - "--output_dir", - str(output_dir), + "--output", + str(output_file), ] for entry_point in entry_points: - command.extend(["--e", entry_point]) + command.extend(["--entry-point", entry_point]) result = subprocess.run( command, @@ -51,26 +42,23 @@ def compare_files(self, generated_file, expected_file): self.assertEqual(gen_content, exp_content) def test_generate_header(self): - yaml_file = "libc/utils/hdrgen/tests/input/test_small.yaml" - h_def_file = "libc/utils/hdrgen/tests/input/test_small.h.def" - expected_output_file = ( - self.source_dir / "libc/utils/hdrgen/tests/expected_output/test_header.h" - ) + yaml_file = self.source_dir / "input/test_small.yaml" + expected_output_file = self.source_dir / "expected_output/test_header.h" output_file = self.output_dir / "test_small.h" entry_points = {"func_b", "func_a", "func_c", "func_d", "func_e"} - if not self.output_dir.exists(): - self.output_dir.mkdir(parents=True) - - self.run_script(yaml_file, h_def_file, self.output_dir, entry_points) + self.run_script(yaml_file, output_file, entry_points) self.compare_files(output_file, expected_output_file) -if __name__ == "__main__": +def main(): parser = argparse.ArgumentParser(description="TestHeaderGenIntegration arguments") parser.add_argument( - "--output_dir", type=str, help="Output directory for generated headers" + "--output_dir", + type=Path, + help="Output directory for generated headers", + required=True, ) args, remaining_argv = parser.parse_known_args() @@ -79,3 +67,7 @@ def test_generate_header(self): sys.argv[1:] = remaining_argv unittest.main() + + +if __name__ == "__main__": + main() diff --git a/libc/utils/hdrgen/yaml_to_classes.py b/libc/utils/hdrgen/yaml_to_classes.py index ec2441b78aee5..d64feafc260b7 100644 --- a/libc/utils/hdrgen/yaml_to_classes.py +++ b/libc/utils/hdrgen/yaml_to_classes.py @@ -35,6 +35,7 @@ def yaml_to_classes(yaml_data, header_class, entry_points=None): """ header_name = yaml_data.get("header") header = header_class(header_name) + header.template_file = yaml_data.get("header_template") for macro_data in yaml_data.get("macros", []): header.add_macro(Macro(macro_data["macro_name"], macro_data["macro_value"])) @@ -226,10 +227,6 @@ def main(): "--output_dir", help="Directory to output the generated header file", ) - parser.add_argument( - "--h_def_file", - help="Path to the .h.def template file (required if not using --export_decls)", - ) parser.add_argument( "--add_function", nargs=6, @@ -244,7 +241,10 @@ def main(): help="Add a function to the YAML file", ) parser.add_argument( - "--e", action="append", help="Entry point to include", dest="entry_points" + "--entry-point", + action="append", + help="Entry point to include", + dest="entry_points", ) parser.add_argument( "--export-decls", @@ -268,13 +268,7 @@ def main(): else: output_file_path = Path(f"{Path(args.yaml_file).stem}.h") - if not args.export_decls and args.h_def_file: - with open(args.h_def_file, "r") as f: - h_def_content = f.read() - final_header_content = fill_public_api(header_str, h_def_content) - with open(output_file_path, "w") as f: - f.write(final_header_content) - else: + if args.export_decls: with open(output_file_path, "w") as f: f.write(header_str)