[libc-commits] [libc] 4816bfa - [libc] Add LLVM_LIBC_CLANG_TIDY option and allow LLVM_LIBC_ENABLE_LINTING without full build.

Tue Ly via libc-commits libc-commits at lists.llvm.org
Tue Mar 1 08:04:50 PST 2022


Author: Tue Ly
Date: 2022-03-01T11:04:29-05:00
New Revision: 4816bfa83881f4a34eb95d6c7a47258c7e49ef71

URL: https://github.com/llvm/llvm-project/commit/4816bfa83881f4a34eb95d6c7a47258c7e49ef71
DIFF: https://github.com/llvm/llvm-project/commit/4816bfa83881f4a34eb95d6c7a47258c7e49ef71.diff

LOG: [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

Added: 
    

Modified: 
    libc/CMakeLists.txt
    libc/cmake/modules/LLVMLibCObjectRules.cmake

Removed: 
    


################################################################################
diff  --git a/libc/CMakeLists.txt b/libc/CMakeLists.txt
index 75e82ea979b80..6ce6c149113ef 100644
--- a/libc/CMakeLists.txt
+++ b/libc/CMakeLists.txt
@@ -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()
-    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.
-
-      To disable linting set LLVM_LIBC_ENABLE_LINTING to OFF
-      (pass -DLLVM_LIBC_ENABLE_LINTING=OFF to cmake).")
+    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 "
+        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()
   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()
 
 option(LLVM_LIBC_INCLUDE_SCUDO "Include the SCUDO standalone as the allocator for LLVM libc" OFF)

diff  --git a/libc/cmake/modules/LLVMLibCObjectRules.cmake b/libc/cmake/modules/LLVMLibCObjectRules.cmake
index 0ddf8a19f5896..8e2f489752a64 100644
--- a/libc/cmake/modules/LLVMLibCObjectRules.cmake
+++ b/libc/cmake/modules/LLVMLibCObjectRules.cmake
@@ -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)


        


More information about the libc-commits mailing list