forked from swiftlang/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[libc] [NFC] Split the CMake rules into multiple files.
Summary: The single file was getting too long to be convenient to navigate. This patch splits it up two into 4 files one each for header rules, object rules, library rules, and test rules. Reviewers: abrachet, alexshap Subscribers: mgorny, tschuett, libc-commits Tags: #libc-project Differential Revision: https://reviews.llvm.org/D78536
- Loading branch information
Siva Chandra Reddy
committed
Apr 21, 2020
1 parent
177c065
commit 8c2e662
Showing
5 changed files
with
718 additions
and
703 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
# A rule for self contained header file targets. | ||
# This rule merely copies the header file from the current source directory to | ||
# the current binary directory. | ||
# Usage: | ||
# add_header( | ||
# <target name> | ||
# HDR <header file> | ||
# ) | ||
function(add_header target_name) | ||
cmake_parse_arguments( | ||
"ADD_HEADER" | ||
"" # No optional arguments | ||
"HDR" # Single value arguments | ||
"DEPENDS" | ||
${ARGN} | ||
) | ||
if(NOT ADD_HEADER_HDR) | ||
message(FATAL_ERROR "'add_header' rules requires the HDR argument specifying a headef file.") | ||
endif() | ||
|
||
set(dest_file ${CMAKE_CURRENT_BINARY_DIR}/${ADD_HEADER_HDR}) | ||
set(src_file ${CMAKE_CURRENT_SOURCE_DIR}/${ADD_HEADER_HDR}) | ||
|
||
add_custom_command( | ||
OUTPUT ${dest_file} | ||
COMMAND cp ${src_file} ${dest_file} | ||
DEPENDS ${src_file} | ||
) | ||
|
||
get_fq_target_name(${target_name} fq_target_name) | ||
add_custom_target( | ||
${fq_target_name} | ||
DEPENDS ${dest_file} | ||
) | ||
|
||
if(ADD_HEADER_DEPENDS) | ||
get_fq_deps_list(fq_deps_list ${ADD_HEADER_DEPENDS}) | ||
add_dependencies( | ||
${fq_target_name} ${fq_deps_list} | ||
) | ||
endif() | ||
endfunction(add_header) | ||
|
||
# A rule for generated header file targets. | ||
# Usage: | ||
# add_gen_header( | ||
# <target name> | ||
# DEF_FILE <.h.def file> | ||
# GEN_HDR <generated header file name> | ||
# PARAMS <list of name=value pairs> | ||
# DATA_FILES <list input data files> | ||
# ) | ||
function(add_gen_header target_name) | ||
cmake_parse_arguments( | ||
"ADD_GEN_HDR" | ||
"" # No optional arguments | ||
"DEF_FILE;GEN_HDR" # Single value arguments | ||
"PARAMS;DATA_FILES;DEPENDS" # Multi value arguments | ||
${ARGN} | ||
) | ||
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() | ||
|
||
set(out_file ${CMAKE_CURRENT_BINARY_DIR}/${ADD_GEN_HDR_GEN_HDR}) | ||
set(in_file ${CMAKE_CURRENT_SOURCE_DIR}/${ADD_GEN_HDR_DEF_FILE}) | ||
|
||
set(fq_data_files "") | ||
if(ADD_GEN_HDR_DATA_FILES) | ||
foreach(data_file IN LISTS ADD_GEN_HDR_DATA_FILES) | ||
list(APPEND fq_data_files "${CMAKE_CURRENT_SOURCE_DIR}/${data_file}") | ||
endforeach(data_file) | ||
endif() | ||
|
||
set(replacement_params "") | ||
if(ADD_GEN_HDR_PARAMS) | ||
list(APPEND replacement_params "--args" ${ADD_GEN_HDR_PARAMS}) | ||
endif() | ||
|
||
set(gen_hdr_script "${LIBC_BUILD_SCRIPTS_DIR}/gen_hdr.py") | ||
|
||
file(GLOB td_includes ${LIBC_SOURCE_DIR}/spec/*.td) | ||
|
||
add_custom_command( | ||
OUTPUT ${out_file} | ||
COMMAND $<TARGET_FILE:libc-hdrgen> -o ${out_file} --header ${ADD_GEN_HDR_GEN_HDR} | ||
--def ${in_file} ${replacement_params} -I ${LIBC_SOURCE_DIR} | ||
${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/api.td | ||
|
||
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} | ||
DEPENDS ${in_file} ${fq_data_files} ${td_includes} | ||
${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/api.td libc-hdrgen | ||
) | ||
|
||
get_fq_target_name(${target_name} fq_target_name) | ||
if(ADD_GEN_HDR_DEPENDS) | ||
get_fq_deps_list(fq_deps_list ${ADD_GEN_HDR_DEPENDS}) | ||
endif() | ||
add_custom_target( | ||
${fq_target_name} | ||
DEPENDS ${out_file} ${fq_deps_list} | ||
) | ||
endfunction(add_gen_header) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
# A rule to build a library from a collection of entrypoint objects. | ||
# Usage: | ||
# add_entrypoint_library( | ||
# DEPENDS <list of add_entrypoint_object targets> | ||
# ) | ||
function(add_entrypoint_library target_name) | ||
cmake_parse_arguments( | ||
"ENTRYPOINT_LIBRARY" | ||
"" # No optional arguments | ||
"" # No single value arguments | ||
"DEPENDS" # Multi-value arguments | ||
${ARGN} | ||
) | ||
if(NOT ENTRYPOINT_LIBRARY_DEPENDS) | ||
message(FATAL_ERROR "'add_entrypoint_library' target requires a DEPENDS list " | ||
"of 'add_entrypoint_object' targets.") | ||
endif() | ||
|
||
set(obj_list "") | ||
foreach(dep IN LISTS ENTRYPOINT_LIBRARY_DEPENDS) | ||
get_target_property(dep_type ${dep} "TARGET_TYPE") | ||
if(NOT (${dep_type} STREQUAL ${ENTRYPOINT_OBJ_TARGET_TYPE})) | ||
message(FATAL_ERROR "Dependency '${dep}' of 'add_entrypoint_collection' is " | ||
"not an 'add_entrypoint_object' target.") | ||
endif() | ||
get_target_property(target_obj_files ${dep} "OBJECT_FILES") | ||
list(APPEND obj_list "${target_obj_files}") | ||
endforeach(dep) | ||
list(REMOVE_DUPLICATES obj_list) | ||
|
||
set(library_file "${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}${target_name}${CMAKE_STATIC_LIBRARY_SUFFIX}") | ||
add_custom_command( | ||
OUTPUT ${library_file} | ||
COMMAND ${CMAKE_AR} -r ${library_file} ${obj_list} | ||
DEPENDS ${obj_list} | ||
) | ||
add_custom_target( | ||
${target_name} | ||
ALL | ||
DEPENDS ${library_file} | ||
) | ||
endfunction(add_entrypoint_library) | ||
|
||
# Rule to build a shared library of redirector objects. | ||
function(add_redirector_library target_name) | ||
cmake_parse_arguments( | ||
"REDIRECTOR_LIBRARY" | ||
"" | ||
"" | ||
"DEPENDS" | ||
${ARGN} | ||
) | ||
|
||
set(obj_files "") | ||
foreach(dep IN LISTS REDIRECTOR_LIBRARY_DEPENDS) | ||
# TODO: Ensure that each dep is actually a add_redirector_object target. | ||
list(APPEND obj_files $<TARGET_OBJECTS:${dep}>) | ||
endforeach(dep) | ||
|
||
# TODO: Call the linker explicitly instead of calling the compiler driver to | ||
# prevent DT_NEEDED on C++ runtime. | ||
add_library( | ||
${target_name} | ||
SHARED | ||
${obj_files} | ||
) | ||
set_target_properties(${target_name} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) | ||
|
||
target_link_libraries( | ||
${target_name} | ||
-nostdlib -lc -lm | ||
) | ||
|
||
set_target_properties( | ||
${target_name} | ||
PROPERTIES | ||
LINKER_LANGUAGE "C" | ||
) | ||
endfunction(add_redirector_library) | ||
|
||
# Rule to add header only libraries. | ||
# Usage | ||
# add_header_library( | ||
# <target name> | ||
# HDRS <list of .h files part of the library> | ||
# DEPENDS <list of dependencies> | ||
# ) | ||
function(add_header_library target_name) | ||
cmake_parse_arguments( | ||
"ADD_HEADER" | ||
"" # No optional arguments | ||
"" # No Single value arguments | ||
"HDRS;DEPENDS" # Multi-value arguments | ||
${ARGN} | ||
) | ||
|
||
if(NOT ADD_HEADER_HDRS) | ||
message(FATAL_ERROR "'add_header_library' target requires a HDRS list of .h files.") | ||
endif() | ||
|
||
get_fq_target_name(${target_name} fq_target_name) | ||
|
||
set(FULL_HDR_PATHS "") | ||
# TODO: Remove this foreach block when we can switch to the new | ||
# version of the CMake policy CMP0076. | ||
foreach(hdr IN LISTS ADD_HEADER_HDRS) | ||
list(APPEND FULL_HDR_PATHS ${CMAKE_CURRENT_SOURCE_DIR}/${hdr}) | ||
endforeach() | ||
|
||
set(interface_target_name "${fq_target_name}_header_library__") | ||
|
||
add_library(${interface_target_name} INTERFACE) | ||
target_sources(${interface_target_name} INTERFACE ${FULL_HDR_PATHS}) | ||
if(ADD_HEADER_DEPENDS) | ||
get_fq_deps_list(fq_deps_list ${ADD_HEADER_DEPENDS}) | ||
add_dependencies(${interface_target_name} ${fq_deps_list}) | ||
endif() | ||
|
||
add_custom_target(${fq_target_name}) | ||
add_dependencies(${fq_target_name} ${interface_target_name}) | ||
set_target_properties( | ||
${fq_target_name} | ||
PROPERTIES | ||
"TARGET_TYPE" "HDR_LIBRARY" | ||
) | ||
endfunction(add_header_library) |
Oops, something went wrong.