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. 4# Usage: 5# add_header( 6# <target name> 7# HDR <header file> 8# ) 9function(add_header target_name) 10 cmake_parse_arguments( 11 "ADD_HEADER" 12 "" # No optional arguments 13 "HDR" # Single value arguments 14 "DEPENDS" 15 ${ARGN} 16 ) 17 if(NOT ADD_HEADER_HDR) 18 message(FATAL_ERROR "'add_header' rules requires the HDR argument specifying a headef file.") 19 endif() 20 21 set(dest_file ${CMAKE_CURRENT_BINARY_DIR}/${ADD_HEADER_HDR}) 22 set(src_file ${CMAKE_CURRENT_SOURCE_DIR}/${ADD_HEADER_HDR}) 23 24 add_custom_command( 25 OUTPUT ${dest_file} 26 COMMAND cp ${src_file} ${dest_file} 27 DEPENDS ${src_file} 28 ) 29 30 get_fq_target_name(${target_name} fq_target_name) 31 add_custom_target( 32 ${fq_target_name} 33 DEPENDS ${dest_file} 34 ) 35 36 if(ADD_HEADER_DEPENDS) 37 get_fq_deps_list(fq_deps_list ${ADD_HEADER_DEPENDS}) 38 add_dependencies( 39 ${fq_target_name} ${fq_deps_list} 40 ) 41 endif() 42endfunction(add_header) 43 44# A rule for generated header file targets. 45# Usage: 46# add_gen_header( 47# <target name> 48# DEF_FILE <.h.def file> 49# GEN_HDR <generated header file name> 50# PARAMS <list of name=value pairs> 51# DATA_FILES <list input data files> 52# ) 53function(add_gen_header target_name) 54 cmake_parse_arguments( 55 "ADD_GEN_HDR" 56 "" # No optional arguments 57 "DEF_FILE;GEN_HDR" # Single value arguments 58 "PARAMS;DATA_FILES;DEPENDS" # Multi value arguments 59 ${ARGN} 60 ) 61 if(NOT ADD_GEN_HDR_DEF_FILE) 62 message(FATAL_ERROR "`add_gen_hdr` rule requires DEF_FILE to be specified.") 63 endif() 64 if(NOT ADD_GEN_HDR_GEN_HDR) 65 message(FATAL_ERROR "`add_gen_hdr` rule requires GEN_HDR to be specified.") 66 endif() 67 68 set(out_file ${CMAKE_CURRENT_BINARY_DIR}/${ADD_GEN_HDR_GEN_HDR}) 69 set(in_file ${CMAKE_CURRENT_SOURCE_DIR}/${ADD_GEN_HDR_DEF_FILE}) 70 71 set(fq_data_files "") 72 if(ADD_GEN_HDR_DATA_FILES) 73 foreach(data_file IN LISTS ADD_GEN_HDR_DATA_FILES) 74 list(APPEND fq_data_files "${CMAKE_CURRENT_SOURCE_DIR}/${data_file}") 75 endforeach(data_file) 76 endif() 77 78 set(replacement_params "") 79 if(ADD_GEN_HDR_PARAMS) 80 list(APPEND replacement_params "--args" ${ADD_GEN_HDR_PARAMS}) 81 endif() 82 83 set(gen_hdr_script "${LIBC_BUILD_SCRIPTS_DIR}/gen_hdr.py") 84 85 file(GLOB td_includes ${LIBC_SOURCE_DIR}/spec/*.td) 86 87 set(ENTRYPOINT_NAME_LIST_ARG ${TARGET_ENTRYPOINT_NAME_LIST}) 88 list(TRANSFORM ENTRYPOINT_NAME_LIST_ARG PREPEND "--e=") 89 90 add_custom_command( 91 OUTPUT ${out_file} 92 COMMAND $<TARGET_FILE:libc-hdrgen> -o ${out_file} --header ${ADD_GEN_HDR_GEN_HDR} 93 --def ${in_file} ${replacement_params} -I ${LIBC_SOURCE_DIR} 94 ${ENTRYPOINT_NAME_LIST_ARG} 95 ${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/api.td 96 97 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} 98 DEPENDS ${in_file} ${fq_data_files} ${td_includes} 99 ${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/api.td libc-hdrgen 100 ) 101 102 get_fq_target_name(${target_name} fq_target_name) 103 if(ADD_GEN_HDR_DEPENDS) 104 get_fq_deps_list(fq_deps_list ${ADD_GEN_HDR_DEPENDS}) 105 endif() 106 add_custom_target( 107 ${fq_target_name} 108 DEPENDS ${out_file} ${fq_deps_list} 109 ) 110endfunction(add_gen_header) 111