diff --git a/libc/CMakeLists.txt b/libc/CMakeLists.txt --- a/libc/CMakeLists.txt +++ b/libc/CMakeLists.txt @@ -19,6 +19,27 @@ set(LIBC_TARGET_MACHINE ${CMAKE_SYSTEM_PROCESSOR}) +# Check --print-resource-dir to find the compiler resource dir if this flag +# is supported by the compiler. +execute_process( + OUTPUT_STRIP_TRAILING_WHITESPACE + COMMAND ${CMAKE_CXX_COMPILER} --print-resource-dir + RESULT_VARIABLE COMMAND_RETURN_CODE + OUTPUT_VARIABLE COMPILER_RESOURCE_DIR +) +# Retrieve the host compiler's resource dir. +if(COMMAND_RETURN_CODE EQUAL 0) + set(COMPILER_RESOURCE_DIR + "${COMPILER_RESOURCE_DIR}" CACHE PATH "path to compiler resource dir" + ) + message(STATUS "Set COMPILER_RESOURCE_DIR to + ${COMPILER_RESOURCE_DIR} using --print-resource-dir") +else() + set(COMPILER_RESOURCE_DIR OFF) + message(STATUS "COMPILER_RESOURCE_DIR not set + --print-resource-dir not supported by host compiler") +endif() + option(LLVM_LIBC_ENABLE_LINTING "Enables linting of libc source files" ON) if(LLVM_LIBC_ENABLE_LINTING) if("clang-tools-extra" IN_LIST LLVM_ENABLE_PROJECTS diff --git a/libc/cmake/modules/LLVMLibCHeaderRules.cmake b/libc/cmake/modules/LLVMLibCHeaderRules.cmake --- a/libc/cmake/modules/LLVMLibCHeaderRules.cmake +++ b/libc/cmake/modules/LLVMLibCHeaderRules.cmake @@ -91,7 +91,7 @@ ${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/api.td WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} - DEPENDS ${in_file} ${fq_data_files} ${td_includes} + DEPENDS ${in_file} ${fq_data_files} ${td_includes} ${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/api.td libc-hdrgen ) diff --git a/libc/cmake/modules/LLVMLibCObjectRules.cmake b/libc/cmake/modules/LLVMLibCObjectRules.cmake --- a/libc/cmake/modules/LLVMLibCObjectRules.cmake +++ b/libc/cmake/modules/LLVMLibCObjectRules.cmake @@ -206,8 +206,30 @@ ) if(LLVM_LIBC_ENABLE_LINTING) - set(lint_timestamp "${CMAKE_CURRENT_BINARY_DIR}/.${target_name}.__lint_timestamp__") + # 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 + COMMAND $ --system-headers + --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__") add_custom_command( OUTPUT ${lint_timestamp} # --quiet is used to surpress warning statistics from clang-tidy like: @@ -222,13 +244,9 @@ # Path to directory containing compile_commands.json -p ${PROJECT_BINARY_DIR} ${ADD_ENTRYPOINT_OBJ_SRCS} - # We run restrict-system-libc-headers with --system-headers to prevent - # transitive inclusion through compler provided headers. - COMMAND $ --system-headers - --checks="-*,llvmlibc-restrict-system-libc-headers" - --quiet - -p ${PROJECT_BINARY_DIR} - ${ADD_ENTRYPOINT_OBJ_SRCS} + # See above: this might be a second invocation of clang-tidy depending on + # the conditions above. + ${restrict_system_headers_check_invocation} # 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