CXX_CLANG_TIDY not running for library

I am writing a library, and I cannot get clang-tidy to run on build. This is a headers-only library because all of the classes provided have templates, and you cannot define template class members outside of the template class. I suppose this means that compiling the library alone will produce a mostly empty result, but I would still expect it to run clang-tidy. Or maybe Iā€™m missing something key in how this works?

CMakeLists.txt:

cmake_minimum_required(VERSION 3.18)
project(
    somelib
    LANGUAGES CXX
)

set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS YES CACHE BOOL "Export all symbols")

ADD_DEFINITIONS(-DBOOST_LOG_DYN_LINK)
find_package(Boost 1.74.0 REQUIRED COMPONENTS log)
find_package(OpenSSL 1.1.0 REQUIRED)
find_program(CLANG_TIDY_EXE NAMES "clang-tidy" REQUIRED)

add_library(
    ${CMAKE_PROJECT_NAME}
    include/somelib/A.hpp
    include/somelib/B.hpp
)
target_compile_features(${CMAKE_PROJECT_NAME} PUBLIC cxx_std_17)
target_compile_options(${CMAKE_PROJECT_NAME} PRIVATE -Werror)
set_target_properties(
    ${CMAKE_PROJECT_NAME}
    PROPERTIES
        LINKER_LANGUAGE CXX
        CXX_EXTENSIONS OFF
        CXX_CLANG_TIDY "${CLANG_TIDY_EXE};")

target_include_directories(
    ${CMAKE_PROJECT_NAME}
    PUBLIC
        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
        $<INSTALL_INTERFACE:include>
)

install(
    TARGETS ${CMAKE_PROJECT_NAME}
    LIBRARY DESTINATION lib
    ARCHIVE DESTINATION lib
    RUNTIME DESTINATION bin
)

.clang-tidy:

Checks: "*"
WarningsAsErrors: "*"
HeaderFilterRegex: ".*"

File tree:

.
ā”œā”€ā”€ .clang-tidy
ā”œā”€ā”€ CMakeLists.txt
ā”œā”€ā”€ .gitignore
ā”œā”€ā”€ include
ā”‚   ā””ā”€ā”€ somelib
ā”‚       ā”œā”€ā”€ A.hpp
ā”‚       ā”œā”€ā”€ B.hpp
ā””ā”€ā”€ README.md

Commands:

$ cmake -H. -B_builds/Debug -DCMAKE_BUILD_TYPE=Debug
-- The CXX compiler identification is GNU 10.2.1
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found Boost: /usr/lib/x86_64-linux-gnu/cmake/Boost-1.74.0/BoostConfig.cmake (found suitable version "1.74.0", minimum required is "1.74.0") found components: log 
-- Found OpenSSL: /usr/lib/x86_64-linux-gnu/libcrypto.so (found suitable version "1.1.1n", minimum required is "1.1.0")  
-- Configuring done
-- Generating done
-- Build files have been written to: /home/myuser/libsomelib/_builds/Debug
$ cmake --build _builds/Debug
Scanning dependencies of target somelib
[100%] Linking CXX static library libsomelib.a
[100%] Built target somelib

What am I missing?

Thanks!

clang-tidy takes the role of the compiler, so it only runs on source files. You need at least one source file that include the various header files of the library.

1 Like

Ah, thank you! That did it!

Changes made:

add_library(
    ${CMAKE_PROJECT_NAME}
    include/somelib/A.hpp
    include/somelib/B.hpp
    src/lintHelper.cpp
)

src/lintHelper.cpp:

#include <somelib/A.hpp>
#include <somelib/B.hpp>