From baec453b9f5e63c83a04bb92e410aba0aae92ba4 Mon Sep 17 00:00:00 2001 From: mijoku <39005456+mijoku@users.noreply.github.com> Date: Mon, 20 Jan 2025 15:33:09 +0100 Subject: [PATCH 1/3] Make public include directories SYSTEM-includes With this change compile commands generated by cmake for targets consuming nanobind use `-isystem` instead of `-I` for defining nanobind's header locations, which makes modern analysis tools (e.g. clang analyzer) ignore them. --- cmake/nanobind-config.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmake/nanobind-config.cmake b/cmake/nanobind-config.cmake index 312e828f..9040cbcd 100644 --- a/cmake/nanobind-config.cmake +++ b/cmake/nanobind-config.cmake @@ -236,7 +236,7 @@ function (nanobind_build_library TARGET_NAME) ${NB_DIR}/ext/robin_map/include) endif() - target_include_directories(${TARGET_NAME} PUBLIC + target_include_directories(${TARGET_NAME} SYSTEM PUBLIC ${Python_INCLUDE_DIRS} ${NB_DIR}/include) From 25b5a17177703896f8747c75ce509953c881b740 Mon Sep 17 00:00:00 2001 From: mijoku <39005456+mijoku@users.noreply.github.com> Date: Mon, 27 Jan 2025 10:27:14 +0100 Subject: [PATCH 2/3] Header dirs as SYSTEM shall not be default but an opt-in Using option `NB_SUPPRESS_WARNINGS` on function `add_nanobind_module` to enable this behaviour. --- cmake/nanobind-config.cmake | 17 ++++++++++++++--- docs/api_cmake.rst | 3 +++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/cmake/nanobind-config.cmake b/cmake/nanobind-config.cmake index 9040cbcd..e0871886 100644 --- a/cmake/nanobind-config.cmake +++ b/cmake/nanobind-config.cmake @@ -104,6 +104,9 @@ endfunction() # --------------------------------------------------------------------------- function (nanobind_build_library TARGET_NAME) + cmake_parse_arguments(PARSE_ARGV 1 ARG + "AS_SYSINCLUDE" "" "") + if (TARGET ${TARGET_NAME}) return() endif() @@ -114,6 +117,10 @@ function (nanobind_build_library TARGET_NAME) set (TARGET_TYPE SHARED) endif() + if (${ARG_AS_SYSINCLUDE}) + set (AS_SYSINCLUDE SYSTEM) + endif() + add_library(${TARGET_NAME} ${TARGET_TYPE} EXCLUDE_FROM_ALL ${NB_DIR}/include/nanobind/make_iterator.h @@ -236,7 +243,7 @@ function (nanobind_build_library TARGET_NAME) ${NB_DIR}/ext/robin_map/include) endif() - target_include_directories(${TARGET_NAME} SYSTEM PUBLIC + target_include_directories(${TARGET_NAME} ${AS_SYSINCLUDE} PUBLIC ${Python_INCLUDE_DIRS} ${NB_DIR}/include) @@ -310,7 +317,7 @@ endfunction() function(nanobind_add_module name) cmake_parse_arguments(PARSE_ARGV 1 ARG - "STABLE_ABI;FREE_THREADED;NB_STATIC;NB_SHARED;PROTECT_STACK;LTO;NOMINSIZE;NOSTRIP;MUSL_DYNAMIC_LIBCPP" + "STABLE_ABI;FREE_THREADED;NB_STATIC;NB_SHARED;PROTECT_STACK;LTO;NOMINSIZE;NOSTRIP;MUSL_DYNAMIC_LIBCPP;NB_SUPPRESS_WARNINGS" "NB_DOMAIN" "") add_library(${name} MODULE ${ARG_UNPARSED_ARGUMENTS}) @@ -355,7 +362,11 @@ function(nanobind_add_module name) set(libname ${libname}-${ARG_NB_DOMAIN}) endif() - nanobind_build_library(${libname}) + if (ARG_NB_SUPPRESS_WARNINGS) + set(EXTRA_LIBRARY_PARAMS AS_SYSINCLUDE) + endif() + + nanobind_build_library(${libname} ${EXTRA_LIBRARY_PARAMS}) if (ARG_NB_DOMAIN) target_compile_definitions(${name} PRIVATE NB_DOMAIN=${ARG_NB_DOMAIN}) diff --git a/docs/api_cmake.rst b/docs/api_cmake.rst index 17a15b67..d250c676 100644 --- a/docs/api_cmake.rst +++ b/docs/api_cmake.rst @@ -83,6 +83,9 @@ The high-level interface consists of just one CMake command: - The opposite of ``NB_STATIC``: compile the core nanobind library as a shared library for use in projects that consist of multiple extensions. + * - ``NB_SUPPRESS_WARNINGS`` + - Mark nanobind's include directories as system includes, suppressing + warnings generated from nanobind's and Python's header files. * - ``PROTECT_STACK`` - Don't remove stack smashing-related protections. * - ``LTO`` From a1e65d2d981256d0cd631d48f6a5c1e5746f8d07 Mon Sep 17 00:00:00 2001 From: mijoku <39005456+mijoku@users.noreply.github.com> Date: Mon, 27 Jan 2025 13:18:11 +0100 Subject: [PATCH 3/3] change NB_SUPPRESS_WARNINGS documentation as suggested --- docs/api_cmake.rst | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/docs/api_cmake.rst b/docs/api_cmake.rst index d250c676..eab178ae 100644 --- a/docs/api_cmake.rst +++ b/docs/api_cmake.rst @@ -84,8 +84,12 @@ The high-level interface consists of just one CMake command: as a shared library for use in projects that consist of multiple extensions. * - ``NB_SUPPRESS_WARNINGS`` - - Mark nanobind's include directories as system includes, suppressing - warnings generated from nanobind's and Python's header files. + - Mark the include directories of nanobind and Python as + [SYSTEM](https://cmake.org/cmake/help/latest/command/include_directories.html) + include directories, which suppresses any potential warning messages + originating there. This is mainly of relevance if your project artificially + raises the warning level via flags like `-pedantic`, ``-Wcast-qual``, + ``-Wsign-conversion``. * - ``PROTECT_STACK`` - Don't remove stack smashing-related protections. * - ``LTO``