[libc] Add LLVM_LIBC_CLANG_TIDY option and allow LLVM_LIBC_ENABLE_LINTING without full build.

Add LLVM_LIBC_CLANG_TIDY option and allow LLVM_LIBC_ENABLE_LINTING without full build.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D119180
This commit is contained in:
Tue Ly 2022-02-07 11:46:09 -05:00
parent ac616fbb05
commit 4816bfa838
2 changed files with 43 additions and 25 deletions

View File

@ -51,29 +51,48 @@ endif()
option(LLVM_LIBC_FULL_BUILD "Build and test LLVM libc as if it is the full libc" OFF)
option(LLVM_LIBC_ENABLE_LINTING "Enables linting of libc source files" OFF)
if(LLVM_LIBC_ENABLE_LINTING AND (NOT LLVM_LIBC_FULL_BUILD))
message(FATAL_ERROR "Cannot enable linting when full libc build is not enabled.")
if(LLVM_LIBC_CLANG_TIDY)
set(LLVM_LIBC_ENABLE_LINTING ON)
endif()
if(LLVM_LIBC_ENABLE_LINTING)
if("clang-tools-extra" IN_LIST LLVM_ENABLE_PROJECTS
AND "clang" IN_LIST LLVM_ENABLE_PROJECTS)
add_custom_target(lint-libc)
file(COPY ${LIBC_SOURCE_DIR}/.clang-tidy DESTINATION ${LIBC_BUILD_DIR})
if(NOT CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
set(LLVM_LIBC_ENABLE_LINTING OFF)
message(WARNING "C++ compiler is not clang++, linting with be disabled.")
else()
if (NOT LLVM_LIBC_CLANG_TIDY)
find_program(LLVM_LIBC_CLANG_TIDY NAMES clang-tidy)
endif()
if(LLVM_LIBC_CLANG_TIDY)
# Check clang-tidy major version.
execute_process(COMMAND ${LLVM_LIBC_CLANG_TIDY} "--version"
OUTPUT_VARIABLE CLANG_TIDY_OUTPUT)
string(REGEX MATCH "[0-9]+" CLANG_TIDY_VERSION "${CLANG_TIDY_OUTPUT}")
string(REGEX MATCH "[0-9]+" CLANG_MAJOR_VERSION
"${CMAKE_CXX_COMPILER_VERSION}")
if(NOT CLANG_TIDY_VERSION EQUAL CLANG_MAJOR_VERSION)
set(LLVM_LIBC_ENABLE_LINTING OFF)
message(WARNING "
'clang-tidy' (version ${CLANG_TIDY_VERSION}) is not the same as
'clang' (version ${CLANG_MAJOR_VERSION}). Linting will
be disabled.
The path to the clang-tidy binary can be set manually by passing
-DLLVM_LIBC_CLANG_TIDY=<path/to/clang-tidy> to CMake.")
endif()
else()
message(FATAL_ERROR "
'clang' and 'clang-tools-extra' are required in LLVM_ENABLE_PROJECTS to
lint llvm-libc. The linting step performs important checks to help prevent
the introduction of subtle bugs, but it may increase build times.
Linting is enabled but 'clang-tidy' is not found!
The path to the clang-tidy binary can be set manually by passing
-DLLVM_LIBC_CLANG_TIDY=<path/to/clang-tidy> to CMake.
To disable linting set LLVM_LIBC_ENABLE_LINTING to OFF
(pass -DLLVM_LIBC_ENABLE_LINTING=OFF to cmake).")
endif()
elseif(LLVM_LIBC_FULL_BUILD)
message(WARNING "
Linting for libc is currently disabled.
This is not recommended, to enable set LLVM_LIBC_ENABLE_LINTING to ON
(pass -DLLVM_LIBC_ENABLE_LINTING=ON to cmake).")
endif()
endif()
option(LLVM_LIBC_INCLUDE_SCUDO "Include the SCUDO standalone as the allocator for LLVM libc" OFF)

View File

@ -222,6 +222,10 @@ function(add_entrypoint_object target_name)
)
if(LLVM_LIBC_ENABLE_LINTING)
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()
# We only want a second invocation of clang-tidy to run
# restrict-system-libc-headers if the compiler-resource-dir was set in
@ -231,7 +235,7 @@ function(add_entrypoint_object target_name)
# We run restrict-system-libc-headers with --system-headers to prevent
# transitive inclusion through compler provided headers.
set(restrict_system_headers_check_invocation
COMMAND $<TARGET_FILE:clang-tidy> --system-headers
COMMAND ${LLVM_LIBC_CLANG_TIDY} --system-headers
--checks="-*,llvmlibc-restrict-system-libc-headers"
# We explicitly set the resource dir here to match the
# resource dir of the host compiler.
@ -255,7 +259,7 @@ function(add_entrypoint_object target_name)
# X warnings generated.
# Until this is fixed upstream, we use -fno-caret-diagnostics to surpress
# these.
COMMAND $<TARGET_FILE:clang-tidy>
COMMAND ${LLVM_LIBC_CLANG_TIDY}
"--extra-arg=-fno-caret-diagnostics" --quiet
# Path to directory containing compile_commands.json
-p ${PROJECT_BINARY_DIR}
@ -274,11 +278,6 @@ function(add_entrypoint_object target_name)
DEPENDS clang-tidy ${internal_target_name} ${ADD_ENTRYPOINT_OBJ_SRCS}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
add_custom_target(${fq_target_name}.__lint__
DEPENDS ${lint_timestamp})
add_dependencies(lint-libc ${fq_target_name}.__lint__)
add_dependencies(${fq_target_name} ${fq_target_name}.__lint__)
endif()
endfunction(add_entrypoint_object)