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 @@ -104,3 +104,37 @@ DEPENDS ${out_file} ${fq_deps_list} ) endfunction(add_gen_header) + +# This function is use to retrieve the host compiler's resource dir. +function(get_compiler_resource_dir) + if(COMPILER_RESOURCE_DIR) + return() + endif() + + # Use --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 + ) + 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") + return() + endif() + + # Fallback if --print-resource-dir is not supported by the compiler. + # TODO, see if there is a better way to do this. + find_path(stddef_file_loc stddef.h) + set(COMPILER_RESOURCE_DIR + "${stddef_file_loc}/../" CACHE PATH "path to compiler resource dir" + ) + message(STATUS "Set COMPILER_RESOURCE_DIR to + ${COMPILER_RESOURCE_DIR} using location of stddef.h") + +endfunction(get_compiler_resource_dir) 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 @@ -207,7 +207,7 @@ if(LLVM_LIBC_ENABLE_LINTING) set(lint_timestamp "${CMAKE_CURRENT_BINARY_DIR}/.${target_name}.__lint_timestamp__") - + get_compiler_resource_dir() add_custom_command( OUTPUT ${lint_timestamp} # --quiet is used to surpress warning statistics from clang-tidy like: @@ -218,8 +218,11 @@ # Until this is fixed upstream, we use -fno-caret-diagnostics to surpress # these. COMMAND $ --system-headers - "--extra-arg=-fno-caret-diagnostics" --quiet - # Path to directory containing compile_commands.json + "--extra-arg=-fno-caret-diagnostics" + # 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} # We have two options for running commands, add_custom_command and