Ignore 3rd party headers from clang-tidy in cmake

499 views Asked by At

How can I avoid clang tidy warnings on my external library I am downloading using FetchContent. Is their an easy way to treat these as system libraries.

I have done this in the past with my own header lib that I wanted to ignore using:

target_include_directories(${target} SYSTEM PUBLIC ${CMAKE_SOURCE_DIR}<path_to_dir>)

but even adding this did not work

target_include_directories(test SYSTEM PUBLIC ${CMAKE_SOURCE_DIR}/build/_deps/spdlog-src/include)

Example CMake:

cmake_minimum_required(VERSION 3.14)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

project(test)

option(ENABLE_CLANG_TIDY "Build the unit tests" OFF)

find_program(CLANGTIDY clang-tidy)
if(CLANGTIDY)
  set(CMAKE_CXX_CLANG_TIDY clang-tidy; -header-filter=.;
    -warnings-as-errors=*; --extra-arg-before=-std=c++17)
else()
  message(SEND_ERROR "clang-tidy exe not found")
endif()

include(FetchContent)
FetchContent_Declare(
        spdlog
        GIT_REPOSITORY https://github.com/gabime/spdlog.git
        GIT_TAG        v1.10.0
)
FetchContent_MakeAvailable(spdlog)

add_executable(test main.cpp)
target_include_directories(test SYSTEM PUBLIC ${CMAKE_SOURCE_DIR}/build/_deps/spdlog-src/include)
target_link_libraries(test spdlog::spdlog_header_only)

Example Source File:

#include <spdlog/spdlog.h>
#include <spdlog/sinks/basic_file_sink.h>
#include <string>

int main()
{
    auto _logger{spdlog::basic_logger_mt("default_logger", "logger.txt")};
    _logger->flush_on(spdlog::level::info);
    spdlog::set_default_logger(_logger);
    spdlog::set_level(spdlog::level::info);

    spdlog::info("hello world");
}
1

There are 1 answers

1
user2020 On

Managed to solve this by doing the following:

include(FetchContent)
FetchContent_Declare(
        spdlog
        GIT_REPOSITORY https://github.com/gabime/spdlog.git
        GIT_TAG        v1.10.0
)
FetchContent_GetProperties(spdlog)
if(NOT spdlog_POPULATED)
  FetchContent_Populate(spdlog)
  # create a header only library
  add_library(spdlog INTERFACE)
  add_library(spdlog::libspdlog ALIAS spdlog)
  target_include_directories(
      spdlog
      SYSTEM INTERFACE
      ${spdlog_SOURCE_DIR}/include)
endif()