2020-04-18 09:42:40 +08:00
|
|
|
set(OBJECT_LIBRARY_TARGET_TYPE "OBJECT_LIBRARY")
|
|
|
|
|
2021-05-10 15:53:48 +08:00
|
|
|
function(_get_common_compile_options output_var)
|
2021-07-09 01:44:10 +08:00
|
|
|
set(compile_options ${LLVM_CXX_STD_default} ${LIBC_COMPILE_OPTIONS_DEFAULT} ${ARGN})
|
|
|
|
if(NOT ${LIBC_TARGET_OS} STREQUAL "windows")
|
|
|
|
set(compile_options ${compile_options} -fpie -ffreestanding)
|
|
|
|
endif()
|
2022-02-03 20:36:05 +08:00
|
|
|
if(LLVM_COMPILER_IS_GCC_COMPATIBLE)
|
|
|
|
list(APPEND compile_options "-fno-exceptions")
|
|
|
|
list(APPEND compile_options "-fno-unwind-tables")
|
|
|
|
list(APPEND compile_options "-fno-asynchronous-unwind-tables")
|
|
|
|
list(APPEND compile_options "-fno-rtti")
|
|
|
|
elseif(MSVC)
|
|
|
|
list(APPEND compile_options "/EHs-c-")
|
|
|
|
list(APPEND compile_options "/GR-")
|
|
|
|
endif()
|
2021-07-09 01:44:10 +08:00
|
|
|
set(${output_var} ${compile_options} PARENT_SCOPE)
|
2021-05-10 15:53:48 +08:00
|
|
|
endfunction()
|
|
|
|
|
2020-04-18 09:42:40 +08:00
|
|
|
# Rule which is essentially a wrapper over add_library to compile a set of
|
|
|
|
# sources to object files.
|
|
|
|
# Usage:
|
|
|
|
# add_object_library(
|
|
|
|
# <target_name>
|
|
|
|
# HDRS <list of header files>
|
|
|
|
# SRCS <list of source files>
|
|
|
|
# DEPENDS <list of dependencies>
|
|
|
|
# COMPILE_OPTIONS <optional list of special compile options for this target>
|
|
|
|
function(add_object_library target_name)
|
|
|
|
cmake_parse_arguments(
|
|
|
|
"ADD_OBJECT"
|
2022-02-03 13:03:27 +08:00
|
|
|
"" # No optional arguments
|
|
|
|
"CXX_STANDARD" # Single value arguments
|
2020-04-18 09:42:40 +08:00
|
|
|
"SRCS;HDRS;COMPILE_OPTIONS;DEPENDS" # Multivalue arguments
|
|
|
|
${ARGN}
|
|
|
|
)
|
|
|
|
|
|
|
|
if(NOT ADD_OBJECT_SRCS)
|
|
|
|
message(FATAL_ERROR "'add_object_library' rule requires SRCS to be specified.")
|
|
|
|
endif()
|
|
|
|
|
|
|
|
get_fq_target_name(${target_name} fq_target_name)
|
|
|
|
add_library(
|
|
|
|
${fq_target_name}
|
2021-02-24 23:22:58 +08:00
|
|
|
EXCLUDE_FROM_ALL
|
2020-04-18 09:42:40 +08:00
|
|
|
OBJECT
|
|
|
|
${ADD_OBJECT_SRCS}
|
|
|
|
${ADD_OBJECT_HDRS}
|
|
|
|
)
|
|
|
|
target_include_directories(
|
|
|
|
${fq_target_name}
|
|
|
|
PRIVATE
|
|
|
|
${LIBC_BUILD_DIR}/include
|
|
|
|
${LIBC_SOURCE_DIR}
|
|
|
|
${LIBC_BUILD_DIR}
|
|
|
|
)
|
2021-05-10 15:53:48 +08:00
|
|
|
_get_common_compile_options(compile_options ${ADD_OBJECT_COMPILE_OPTIONS})
|
|
|
|
target_compile_options(${fq_target_name} PRIVATE ${compile_options})
|
2020-04-18 09:42:40 +08:00
|
|
|
|
2020-04-22 01:10:58 +08:00
|
|
|
get_fq_deps_list(fq_deps_list ${ADD_OBJECT_DEPENDS})
|
|
|
|
if(fq_deps_list)
|
|
|
|
add_dependencies(${fq_target_name} ${fq_deps_list})
|
2020-04-18 09:42:40 +08:00
|
|
|
endif()
|
|
|
|
|
2022-02-03 13:03:27 +08:00
|
|
|
if(ADD_OBJECT_CXX_STANDARD)
|
|
|
|
set_target_properties(
|
|
|
|
${fq_target_name}
|
|
|
|
PROPERTIES
|
|
|
|
CXX_STANDARD ${ADD_OBJECT_CXX_STANDARD}
|
|
|
|
)
|
|
|
|
endif()
|
|
|
|
|
2020-04-18 09:42:40 +08:00
|
|
|
set_target_properties(
|
|
|
|
${fq_target_name}
|
|
|
|
PROPERTIES
|
|
|
|
"TARGET_TYPE" ${OBJECT_LIBRARY_TARGET_TYPE}
|
2020-04-22 01:10:58 +08:00
|
|
|
"OBJECT_FILES" "$<TARGET_OBJECTS:${fq_target_name}>"
|
|
|
|
"DEPS" "${fq_deps_list}"
|
2020-04-18 09:42:40 +08:00
|
|
|
)
|
|
|
|
endfunction(add_object_library)
|
|
|
|
|
|
|
|
set(ENTRYPOINT_OBJ_TARGET_TYPE "ENTRYPOINT_OBJ")
|
|
|
|
|
|
|
|
# A rule for entrypoint object targets.
|
|
|
|
# Usage:
|
|
|
|
# add_entrypoint_object(
|
|
|
|
# <target_name>
|
|
|
|
# [ALIAS|REDIRECTED] # Specified if the entrypoint is redirected or an alias.
|
|
|
|
# [NAME] <the C name of the entrypoint if different from target_name>
|
|
|
|
# SRCS <list of .cpp files>
|
|
|
|
# HDRS <list of .h files>
|
|
|
|
# DEPENDS <list of dependencies>
|
|
|
|
# COMPILE_OPTIONS <optional list of special compile options for this target>
|
|
|
|
# SPECIAL_OBJECTS <optional list of special object targets added by the rule `add_object`>
|
|
|
|
# )
|
|
|
|
function(add_entrypoint_object target_name)
|
|
|
|
cmake_parse_arguments(
|
|
|
|
"ADD_ENTRYPOINT_OBJ"
|
|
|
|
"ALIAS;REDIRECTED" # Optional argument
|
2022-02-03 13:03:27 +08:00
|
|
|
"NAME;CXX_STANDARD" # Single value arguments
|
2020-04-18 09:42:40 +08:00
|
|
|
"SRCS;HDRS;DEPENDS;COMPILE_OPTIONS" # Multi value arguments
|
|
|
|
${ARGN}
|
|
|
|
)
|
|
|
|
|
|
|
|
get_fq_target_name(${target_name} fq_target_name)
|
2020-06-03 03:13:57 +08:00
|
|
|
set(entrypoint_name ${target_name})
|
|
|
|
if(ADD_ENTRYPOINT_OBJ_NAME)
|
|
|
|
set(entrypoint_name ${ADD_ENTRYPOINT_OBJ_NAME})
|
|
|
|
endif()
|
2020-04-18 09:42:40 +08:00
|
|
|
|
2020-06-09 15:31:48 +08:00
|
|
|
list(FIND TARGET_ENTRYPOINT_NAME_LIST ${entrypoint_name} entrypoint_name_index)
|
|
|
|
if(${entrypoint_name_index} EQUAL -1)
|
|
|
|
add_custom_target(${fq_target_name})
|
|
|
|
set_target_properties(
|
|
|
|
${fq_target_name}
|
|
|
|
PROPERTIES
|
|
|
|
"ENTRYPOINT_NAME" ${entrypoint_name}
|
|
|
|
"TARGET_TYPE" ${ENTRYPOINT_OBJ_TARGET_TYPE}
|
|
|
|
"OBJECT_FILE" ""
|
|
|
|
"OBJECT_FILE_RAW" ""
|
|
|
|
"DEPS" ""
|
|
|
|
"SKIPPED" "YES"
|
|
|
|
)
|
|
|
|
message(STATUS "Skipping libc entrypoint ${fq_target_name}.")
|
|
|
|
return()
|
|
|
|
endif()
|
|
|
|
|
2020-04-18 09:42:40 +08:00
|
|
|
if(ADD_ENTRYPOINT_OBJ_ALIAS)
|
|
|
|
# Alias targets help one add aliases to other entrypoint object targets.
|
|
|
|
# One can use alias targets setup OS/machine independent entrypoint targets.
|
|
|
|
list(LENGTH ADD_ENTRYPOINT_OBJ_DEPENDS deps_size)
|
|
|
|
if(NOT (${deps_size} EQUAL "1"))
|
|
|
|
message(FATAL_ERROR "An entrypoint alias should have exactly one dependency.")
|
|
|
|
endif()
|
|
|
|
list(GET ADD_ENTRYPOINT_OBJ_DEPENDS 0 dep_target)
|
|
|
|
get_fq_dep_name(fq_dep_name ${dep_target})
|
|
|
|
if(NOT TARGET ${fq_dep_name})
|
|
|
|
message(WARNING "Aliasee ${fq_dep_name} for entrypoint alias ${target_name} missing; "
|
|
|
|
"Target ${target_name} will be ignored.")
|
|
|
|
return()
|
|
|
|
endif()
|
|
|
|
|
|
|
|
get_target_property(obj_type ${fq_dep_name} "TARGET_TYPE")
|
|
|
|
if((NOT obj_type) OR (NOT (${obj_type} STREQUAL ${ENTRYPOINT_OBJ_TARGET_TYPE})))
|
|
|
|
message(FATAL_ERROR "The aliasee of an entrypoint alias should be an entrypoint.")
|
|
|
|
endif()
|
|
|
|
|
|
|
|
add_custom_target(${fq_target_name})
|
|
|
|
add_dependencies(${fq_target_name} ${fq_dep_name})
|
2020-04-22 01:10:58 +08:00
|
|
|
get_target_property(object_file ${fq_dep_name} "OBJECT_FILE")
|
|
|
|
get_target_property(object_file_raw ${fq_dep_name} "OBJECT_FILE_RAW")
|
2020-04-18 09:42:40 +08:00
|
|
|
set_target_properties(
|
|
|
|
${fq_target_name}
|
|
|
|
PROPERTIES
|
2020-06-03 03:13:57 +08:00
|
|
|
"ENTRYPOINT_NAME" ${entrypoint_name}
|
2020-04-18 09:42:40 +08:00
|
|
|
"TARGET_TYPE" ${ENTRYPOINT_OBJ_TARGET_TYPE}
|
2020-05-01 01:46:49 +08:00
|
|
|
"IS_ALIAS" "YES"
|
2020-04-22 01:10:58 +08:00
|
|
|
"OBJECT_FILE" ""
|
|
|
|
"OBJECT_FILE_RAW" ""
|
|
|
|
"DEPS" "${fq_dep_name}"
|
2020-04-18 09:42:40 +08:00
|
|
|
)
|
|
|
|
return()
|
|
|
|
endif()
|
|
|
|
|
|
|
|
if(NOT ADD_ENTRYPOINT_OBJ_SRCS)
|
|
|
|
message(FATAL_ERROR "`add_entrypoint_object` rule requires SRCS to be specified.")
|
|
|
|
endif()
|
|
|
|
if(NOT ADD_ENTRYPOINT_OBJ_HDRS)
|
|
|
|
message(FATAL_ERROR "`add_entrypoint_object` rule requires HDRS to be specified.")
|
|
|
|
endif()
|
|
|
|
|
2021-05-10 15:53:48 +08:00
|
|
|
_get_common_compile_options(common_compile_options ${ADD_ENTRYPOINT_OBJ_COMPILE_OPTIONS})
|
2020-12-24 02:46:09 +08:00
|
|
|
set(internal_target_name ${fq_target_name}.__internal__)
|
|
|
|
set(include_dirs ${LIBC_BUILD_DIR}/include ${LIBC_SOURCE_DIR} ${LIBC_BUILD_DIR})
|
|
|
|
get_fq_deps_list(fq_deps_list ${ADD_ENTRYPOINT_OBJ_DEPENDS})
|
|
|
|
set(full_deps_list ${fq_deps_list} libc.src.__support.common)
|
|
|
|
|
|
|
|
add_library(
|
|
|
|
${internal_target_name}
|
|
|
|
# TODO: We don't need an object library for internal consumption.
|
|
|
|
# A future change should switch this to a normal static library.
|
2021-02-24 23:22:58 +08:00
|
|
|
EXCLUDE_FROM_ALL
|
2020-12-24 02:46:09 +08:00
|
|
|
OBJECT
|
|
|
|
${ADD_ENTRYPOINT_OBJ_SRCS}
|
|
|
|
${ADD_ENTRYPOINT_OBJ_HDRS}
|
|
|
|
)
|
|
|
|
target_compile_options(${internal_target_name} BEFORE PRIVATE ${common_compile_options})
|
|
|
|
target_include_directories(${internal_target_name} PRIVATE ${include_dirs})
|
|
|
|
add_dependencies(${internal_target_name} ${full_deps_list})
|
2020-04-18 09:42:40 +08:00
|
|
|
|
|
|
|
add_library(
|
2020-12-24 02:46:09 +08:00
|
|
|
${fq_target_name}
|
2020-04-18 09:42:40 +08:00
|
|
|
# We want an object library as the objects will eventually get packaged into
|
|
|
|
# an archive (like libc.a).
|
2021-02-24 23:22:58 +08:00
|
|
|
EXCLUDE_FROM_ALL
|
2020-04-18 09:42:40 +08:00
|
|
|
OBJECT
|
|
|
|
${ADD_ENTRYPOINT_OBJ_SRCS}
|
|
|
|
${ADD_ENTRYPOINT_OBJ_HDRS}
|
|
|
|
)
|
2021-05-10 15:53:48 +08:00
|
|
|
target_compile_options(${fq_target_name} BEFORE PRIVATE ${common_compile_options} -DLLVM_LIBC_PUBLIC_PACKAGING)
|
2020-12-24 02:46:09 +08:00
|
|
|
target_include_directories(${fq_target_name} PRIVATE ${include_dirs})
|
|
|
|
add_dependencies(${fq_target_name} ${full_deps_list})
|
2020-04-18 09:42:40 +08:00
|
|
|
|
2022-02-03 13:03:27 +08:00
|
|
|
if(ADD_ENTRYPOINT_OBJ_CXX_STANDARD)
|
|
|
|
set_target_properties(
|
|
|
|
${fq_target_name} ${internal_target_name}
|
|
|
|
PROPERTIES
|
|
|
|
CXX_STANDARD ${ADD_ENTRYPOINT_OBJ_CXX_STANDARD}
|
|
|
|
)
|
|
|
|
endif()
|
|
|
|
|
2020-04-18 09:42:40 +08:00
|
|
|
set_target_properties(
|
|
|
|
${fq_target_name}
|
|
|
|
PROPERTIES
|
2020-06-03 03:13:57 +08:00
|
|
|
"ENTRYPOINT_NAME" ${entrypoint_name}
|
2020-04-18 09:42:40 +08:00
|
|
|
"TARGET_TYPE" ${ENTRYPOINT_OBJ_TARGET_TYPE}
|
2020-12-24 02:46:09 +08:00
|
|
|
"OBJECT_FILE" $<TARGET_OBJECTS:${fq_target_name}>
|
|
|
|
# TODO: We don't need to list internal object files if the internal
|
|
|
|
# target is a normal static library.
|
|
|
|
"OBJECT_FILE_RAW" $<TARGET_OBJECTS:${internal_target_name}>
|
2020-04-22 01:10:58 +08:00
|
|
|
"DEPS" "${fq_deps_list}"
|
2020-04-18 09:42:40 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
if(LLVM_LIBC_ENABLE_LINTING)
|
2022-02-08 00:46:09 +08:00
|
|
|
if(NOT LLVM_LIBC_CLANG_TIDY)
|
|
|
|
message(FATAL_ERROR "Something is wrong! LLVM_LIBC_ENABLE_LINTING is "
|
|
|
|
"ON but LLVM_LIBC_CLANG_TIDY is not set.")
|
|
|
|
endif()
|
2020-04-18 09:42:40 +08:00
|
|
|
|
2020-05-22 08:39:03 +08:00
|
|
|
# We only want a second invocation of clang-tidy to run
|
|
|
|
# restrict-system-libc-headers if the compiler-resource-dir was set in
|
|
|
|
# order to prevent false-positives due to a mismatch between the host
|
|
|
|
# compiler and the compiled clang-tidy.
|
|
|
|
if(COMPILER_RESOURCE_DIR)
|
|
|
|
# We run restrict-system-libc-headers with --system-headers to prevent
|
|
|
|
# transitive inclusion through compler provided headers.
|
|
|
|
set(restrict_system_headers_check_invocation
|
2022-02-08 00:46:09 +08:00
|
|
|
COMMAND ${LLVM_LIBC_CLANG_TIDY} --system-headers
|
2020-05-22 08:39:03 +08:00
|
|
|
--checks="-*,llvmlibc-restrict-system-libc-headers"
|
|
|
|
# We explicitly set the resource dir here to match the
|
|
|
|
# resource dir of the host compiler.
|
|
|
|
"--extra-arg=-resource-dir=${COMPILER_RESOURCE_DIR}"
|
|
|
|
--quiet
|
|
|
|
-p ${PROJECT_BINARY_DIR}
|
|
|
|
${ADD_ENTRYPOINT_OBJ_SRCS}
|
|
|
|
)
|
|
|
|
else()
|
|
|
|
set(restrict_system_headers_check_invocation
|
|
|
|
COMMAND ${CMAKE_COMMAND} -E echo "Header file check skipped")
|
|
|
|
endif()
|
|
|
|
|
|
|
|
set(lint_timestamp "${CMAKE_CURRENT_BINARY_DIR}/.${target_name}.__lint_timestamp__")
|
2020-04-18 09:42:40 +08:00
|
|
|
add_custom_command(
|
|
|
|
OUTPUT ${lint_timestamp}
|
|
|
|
# --quiet is used to surpress warning statistics from clang-tidy like:
|
|
|
|
# Suppressed X warnings (X in non-user code).
|
|
|
|
# There seems to be a bug in clang-tidy where by even with --quiet some
|
|
|
|
# messages from clang's own diagnostics engine leak through:
|
|
|
|
# X warnings generated.
|
|
|
|
# Until this is fixed upstream, we use -fno-caret-diagnostics to surpress
|
|
|
|
# these.
|
2022-02-08 00:46:09 +08:00
|
|
|
COMMAND ${LLVM_LIBC_CLANG_TIDY}
|
2020-05-21 16:21:07 +08:00
|
|
|
"--extra-arg=-fno-caret-diagnostics" --quiet
|
2020-04-18 09:42:40 +08:00
|
|
|
# Path to directory containing compile_commands.json
|
|
|
|
-p ${PROJECT_BINARY_DIR}
|
|
|
|
${ADD_ENTRYPOINT_OBJ_SRCS}
|
2020-05-22 08:39:03 +08:00
|
|
|
# See above: this might be a second invocation of clang-tidy depending on
|
|
|
|
# the conditions above.
|
|
|
|
${restrict_system_headers_check_invocation}
|
2020-04-18 09:42:40 +08:00
|
|
|
# We have two options for running commands, add_custom_command and
|
|
|
|
# add_custom_target. We don't want to run the linter unless source files
|
|
|
|
# have changed. add_custom_target explicitly runs everytime therefore we
|
|
|
|
# use add_custom_command. This function requires an output file and since
|
|
|
|
# linting doesn't produce a file, we create a dummy file using a
|
|
|
|
# crossplatform touch.
|
|
|
|
COMMAND "${CMAKE_COMMAND}" -E touch ${lint_timestamp}
|
|
|
|
COMMENT "Linting... ${target_name}"
|
2020-12-24 02:46:09 +08:00
|
|
|
DEPENDS clang-tidy ${internal_target_name} ${ADD_ENTRYPOINT_OBJ_SRCS}
|
2020-04-18 09:42:40 +08:00
|
|
|
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
|
|
|
|
)
|
|
|
|
endif()
|
|
|
|
|
|
|
|
endfunction(add_entrypoint_object)
|
|
|
|
|
2021-10-20 07:06:52 +08:00
|
|
|
set(ENTRYPOINT_EXT_TARGET_TYPE "ENTRYPOINT_EXT")
|
|
|
|
|
|
|
|
# A rule for external entrypoint targets.
|
|
|
|
# Usage:
|
|
|
|
# add_entrypoint_external(
|
|
|
|
# <target_name>
|
|
|
|
# DEPENDS <list of dependencies>
|
|
|
|
# )
|
|
|
|
function(add_entrypoint_external target_name)
|
|
|
|
cmake_parse_arguments(
|
|
|
|
"ADD_ENTRYPOINT_EXT"
|
|
|
|
"" # No optional arguments
|
|
|
|
"" # No single value arguments
|
|
|
|
"DEPENDS" # Multi value arguments
|
|
|
|
${ARGN}
|
|
|
|
)
|
|
|
|
get_fq_target_name(${target_name} fq_target_name)
|
|
|
|
set(entrypoint_name ${target_name})
|
|
|
|
|
|
|
|
add_custom_target(${fq_target_name})
|
|
|
|
set_target_properties(
|
|
|
|
${fq_target_name}
|
|
|
|
PROPERTIES
|
|
|
|
"ENTRYPOINT_NAME" ${entrypoint_name}
|
|
|
|
"TARGET_TYPE" ${ENTRYPOINT_EXT_TARGET_TYPE}
|
|
|
|
"DEPS" "${ADD_ENTRYPOINT_EXT_DEPENDS}"
|
|
|
|
)
|
|
|
|
|
|
|
|
endfunction(add_entrypoint_external)
|
|
|
|
|
2020-04-18 09:42:40 +08:00
|
|
|
# Rule build a redirector object file.
|
|
|
|
function(add_redirector_object target_name)
|
|
|
|
cmake_parse_arguments(
|
|
|
|
"REDIRECTOR_OBJECT"
|
|
|
|
"" # No optional arguments
|
|
|
|
"SRC" # The cpp file in which the redirector is defined.
|
|
|
|
"" # No multivalue arguments
|
|
|
|
${ARGN}
|
|
|
|
)
|
|
|
|
if(NOT REDIRECTOR_OBJECT_SRC)
|
|
|
|
message(FATAL_ERROR "'add_redirector_object' rule requires SRC option listing one source file.")
|
|
|
|
endif()
|
|
|
|
|
|
|
|
add_library(
|
|
|
|
${target_name}
|
2021-02-24 23:22:58 +08:00
|
|
|
EXCLUDE_FROM_ALL
|
2020-04-18 09:42:40 +08:00
|
|
|
OBJECT
|
|
|
|
${REDIRECTOR_OBJECT_SRC}
|
|
|
|
)
|
|
|
|
target_compile_options(
|
|
|
|
${target_name}
|
2021-05-10 15:53:48 +08:00
|
|
|
BEFORE PRIVATE -fPIC ${LIBC_COMPILE_OPTIONS_DEFAULT}
|
2020-04-18 09:42:40 +08:00
|
|
|
)
|
|
|
|
endfunction(add_redirector_object)
|