Skip to content

Commit 741d3c2

Browse files
committed
[libc] Add cmake target for linting libc.
Summary: This patch implements running linting on llvm-libc using build rule targets. 1) adds a new target per entrypoint for linting with the naming convention `<qualified_target_name>.__lint__` e.g `libc.src.string.strlen.__lint__`. 2) makes the build target for each entrypoint depend on the linting targets so that they run along with compilation of each entrypoint. 3) adds a lint all target named `lint-libc`. `check-libc` now depends on this new target. 4) linting creates a lot of additional targets from clang and clang-tidy that need to be built so an opt out flag can be passed to cmake: `LLVM_LIBC_ENABLE_LINTING`. Reviewers: sivachandra, abrachet Reviewed By: sivachandra Subscribers: abrachet, mgorny, tschuett, libc-commits Tags: #libc-project Differential Revision: https://reviews.llvm.org/D77861
1 parent 77e3a2e commit 741d3c2

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

libc/CMakeLists.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,28 @@ string(TOLOWER ${LIBC_TARGET_OS} LIBC_TARGET_OS)
1919

2020
set(LIBC_TARGET_MACHINE ${CMAKE_SYSTEM_PROCESSOR})
2121

22+
option(LLVM_LIBC_ENABLE_LINTING "Enables linting of libc source files" ON)
23+
if(LLVM_LIBC_ENABLE_LINTING)
24+
if("clang-tools-extra" IN_LIST LLVM_ENABLE_PROJECTS
25+
AND "clang" IN_LIST LLVM_ENABLE_PROJECTS)
26+
add_custom_target(lint-libc)
27+
else()
28+
message(FATAL_ERROR "
29+
'clang' and 'clang-tools-extra' are required in LLVM_LIBC_ENABLE_PROJECTS to
30+
lint llvm-libc. The linting step performs important checks to help prevent
31+
the introduction of subtle bugs, but it may increase build times.
32+
33+
To disable linting set LLVM_LIBC_ENABLE_LINTING to OFF
34+
(pass -DLLVM_LIBC_ENABLE_LINTING=OFF to cmake).")
35+
endif()
36+
else()
37+
message(WARNING "
38+
Linting for libc is currently disabled.
39+
40+
This is not recommended, to enable set LLVM_LIBC_ENABLE_LINTING to ON
41+
(pass -DLLVM_LIBC_ENABLE_LINTING=ON to cmake).")
42+
endif()
43+
2244
include(CMakeParseArguments)
2345
include(LLVMLibCRules)
2446
include(LLVMLibCCheckCpuFeatures)

libc/cmake/modules/LLVMLibCRules.cmake

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,41 @@ function(add_entrypoint_object target_name)
347347
"OBJECT_FILES" "${all_objects}"
348348
"OBJECT_FILES_RAW" "${all_objects_raw}"
349349
)
350+
351+
if(LLVM_LIBC_ENABLE_LINTING)
352+
set(lint_timestamp "${CMAKE_CURRENT_BINARY_DIR}/.${target_name}.__lint_timestamp__")
353+
354+
add_custom_command(
355+
OUTPUT ${lint_timestamp}
356+
# --quiet is used to surpress warning statistics from clang-tidy like:
357+
# Suppressed X warnings (X in non-user code).
358+
# There seems to be a bug in clang-tidy where by even with --quiet some
359+
# messages from clang's own diagnostics engine leak through:
360+
# X warnings generated.
361+
# Until this is fixed upstream, we use -fno-caret-diagnostics to surpress
362+
# these.
363+
COMMAND $<TARGET_FILE:clang-tidy> "--extra-arg=-fno-caret-diagnostics" --quiet
364+
# Path to directory containing compile_commands.json
365+
-p ${PROJECT_BINARY_DIR}
366+
${ADD_ENTRYPOINT_OBJ_SRCS}
367+
# We have two options for running commands, add_custom_command and
368+
# add_custom_target. We don't want to run the linter unless source files
369+
# have changed. add_custom_target explicitly runs everytime therefore we
370+
# use add_custom_command. This function requires an output file and since
371+
# linting doesn't produce a file, we create a dummy file using a
372+
# crossplatform touch.
373+
COMMAND "${CMAKE_COMMAND}" -E touch ${lint_timestamp}
374+
COMMENT "Linting... ${target_name}"
375+
DEPENDS ${clang-tidy} ${objects_target_name} ${ADD_ENTRYPOINT_OBJ_SRCS}
376+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
377+
)
378+
379+
add_custom_target(${fq_target_name}.__lint__
380+
DEPENDS ${lint_timestamp})
381+
add_dependencies(lint-libc ${fq_target_name}.__lint__)
382+
add_dependencies(${fq_target_name} ${fq_target_name}.__lint__)
383+
endif()
384+
350385
endfunction(add_entrypoint_object)
351386

352387
# A rule to build a library from a collection of entrypoint objects.
@@ -465,7 +500,7 @@ function(add_libc_unittest target_name)
465500
if(NOT LLVM_INCLUDE_TESTS)
466501
return()
467502
endif()
468-
503+
469504
cmake_parse_arguments(
470505
"LIBC_UNITTEST"
471506
"" # No optional arguments

0 commit comments

Comments
 (0)