libcpr / cpr

C++ Requests: Curl for People, a spiritual port of Python Requests.
https://docs.libcpr.org/
Other
6.59k stars 937 forks source link

CMake FetchContent ignore clang-tidy warnings #904

Closed kevinmoloney7 closed 1 year ago

kevinmoloney7 commented 1 year ago

With clang tidy enabled for my project, it is failing when including cpr library. MY understanding was to get FetchContent to treat this dependency as a system library. From the release notes of CMake 3.1.25 it seems like this is possible by adding the system property from this PR image

I also tried setting the target property to system and it failed too. I presume this is something people have encountered before and there is a workaround to?

    FetchContent_Declare(cpr SYSTEM GIT_REPOSITORY https://github.com/libcpr/cpr.git
                         GIT_TAG 0817715923c9705e68994eb52ef9df3f6845beba) # The commit hash for 1.10.x. Replace with the latest from: https://github.com/libcpr/cpr/releases
    set(CPR_BUILD_TESTS OFF CACHE INTERNAL "" FORCE)
    FetchContent_MakeAvailable(cpr)
    set_target_properties(cpr PROPERTIES SYSTEM TRUE)
FAILED: _deps/cpr-build/cpr/CMakeFiles/cpr.dir/cprtypes.cpp.o
/opt/cmake-3.26.3-linux-x86_64/bin/cmake -E __run_co_compile --tidy="clang-tidy;-header-filter=.;-warnings-as-errors=*;--extra-arg-before=-std=c++17;--extra-arg-before=--driver-mode=g++" --cppcheck="/usr/bin/cppcheck;--suppress=missingInclude;--enable=all;--inline-suppr;--inconclusive;--error-exitcode=2" --source=/mnt/c/Users/Test/git/breakout/libraries/test-repo/build/_deps/cpr-src/cpr/cprtypes.cpp -- /usr/bin/clang++ -DCPR_ENABLE_CURL_HTTP_ONLY -DCPR_ENABLE_SSL -DOPENSSL_BACKEND_USED -Dcpr_EXPORTS -I/mnt/c/Users/Test/git/breakout/libraries/test-repo/build/_deps/cpr-src/include -I/mnt/c/Users/Test/git/breakout/libraries/test-repo/build/_deps/cpr-build/cpr_generated_includes -isystem /mnt/c/Users/Test/git/breakout/libraries/test-repo/build/_deps/curl-src/include -Wall -Wextra -Wpedantic -Werror -g -std=gnu++17 -fPIC -MD -MT _deps/cpr-build/cpr/CMakeFiles/cpr.dir/cprtypes.cpp.o -MF _deps/cpr-build/cpr/CMakeFiles/cpr.dir/cprtypes.cpp.o.d -o _deps/cpr-build/cpr/CMakeFiles/cpr.dir/cprtypes.cpp.o -c /mnt/c/Users/Test/git/breakout/libraries/test-repo/build/_deps/cpr-src/cpr/cprtypes.cpp
/mnt/c/Users/Test/git/breakout/libraries/test-repo/build/_deps/cpr-src/include/cpr/cprtypes.h:1:9: error: header guard does not follow preferred style [llvm-header-guard,-warnings-as-errors]
#ifndef CPR_CPR_TYPES_H
        ^~~~~~~~~~~~~~~
        CPR_CPRTYPES_H
/mnt/c/Users/Test/git/breakout/libraries/test-repo/build/_deps/cpr-src/include/cpr/cprtypes.h:97:5: error: function 'str' should be marked [[nodiscard]] [modernize-use-nodiscard,-warnings-as-errors]
    const std::string& str() const {
    ^
    [[nodiscard]]
/mnt/c/Users/Test/git/breakout/libraries/test-repo/build/_deps/cpr-src/include/cpr/cprtypes.h:100:5: error: function 'c_str' should be marked [[nodiscard]] [modernize-use-nodiscard,-warnings-as-errors]
    const char* c_str() const {
    ^
    [[nodiscard]]
/mnt/c/Users/Test/git/breakout/libraries/test-repo/build/_deps/cpr-src/include/cpr/cprtypes.h:103:5: error: function 'data' should be marked [[nodiscard]] [modernize-use-nodiscard,-warnings-as-errors]
    const char* data() const {
    ^
    [[nodiscard]]
10870 warnings generated.
Suppressed 10869 warnings (10863 in non-user code, 6 NOLINT).
Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.
4 warnings treated as errors
[69/188] Building C object _deps/curl-build/lib/CMakeFiles/libcurl.dir/krb5.c.o
ninja: build stopped: subcommand failed.
COM8 commented 1 year ago

Hi @kevinmoloney7 thanks for reporting!

Jup, you are right. This is a common issue and here is a workaround for it (simply disable clang-tidy for fetch content stuff).

# Define two macros for easy clearing and backing up CMake variables
macro(clear_variable)
    cmake_parse_arguments(CLEAR_VAR "" "DESTINATION;BACKUP;REPLACE" "" ${ARGN})
    set(${CLEAR_VAR_BACKUP} ${${CLEAR_VAR_DESTINATION}})
    set(${CLEAR_VAR_DESTINATION} ${CLEAR_VAR_REPLACE})
endmacro()

macro(restore_variable)
    cmake_parse_arguments(CLEAR_VAR "" "DESTINATION;BACKUP" "" ${ARGN})
    set(${CLEAR_VAR_DESTINATION} ${${CLEAR_VAR_BACKUP}})
    unset(${CLEAR_VAR_BACKUP})
endmacro()

# Disable linting for fetch content projects
clear_variable(DESTINATION CMAKE_CXX_CLANG_TIDY BACKUP CMAKE_CXX_CLANG_TIDY_BKP)

FetchContent_Declare(cpr GIT_REPOSITORY https://github.com/libcpr/cpr.git
                         GIT_TAG 1cb247a6dae0fddbb27ffb3ea63506b0fd83e636)
FetchContent_MakeAvailable(cpr)

# Restore clang-tidy config
restore_variable(DESTINATION CMAKE_CXX_CLANG_TIDY BACKUP CMAKE_CXX_CLANG_TIDY_BKP)