[mlir][python][NFC] Clean up nanobind compile options (#206559)

Follow-up to #204230.

Refactor nanobind warning suppression flags into `build_nanobind_lib`.
Drop duplicate RTTI and exception flags.
This commit is contained in:
Diego Caballero 2026-06-29 21:20:59 -07:00 committed by GitHub
parent 442c59c75c
commit 45974112fd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -329,6 +329,16 @@ function(_mlir_python_install_sources name source_root_dir destination)
)
endfunction()
# Define exception handling and RTTI flags.
macro(_mlir_python_eh_rtti_flags)
set(eh_rtti_enable)
if(MSVC)
set(eh_rtti_enable /EHsc /GR)
elseif(LLVM_COMPILER_IS_GCC_COMPATIBLE OR CLANG_CL)
set(eh_rtti_enable -frtti -fexceptions)
endif()
endmacro()
function(build_nanobind_lib)
cmake_parse_arguments(ARG
""
@ -360,6 +370,27 @@ function(build_nanobind_lib)
PRIVATE
NB_DOMAIN=${ARG_MLIR_BINDINGS_PYTHON_NB_DOMAIN}
)
if(NOT MLIR_DISABLE_CONFIGURE_PYTHON_DEV_PACKAGES
AND (LLVM_COMPILER_IS_GCC_COMPATIBLE OR CLANG_CL))
# Avoid some warnings from upstream nanobind.
# If a superproject set MLIR_DISABLE_CONFIGURE_PYTHON_DEV_PACKAGES, let
# the super project handle compile options as it wishes.
_mlir_python_eh_rtti_flags()
target_compile_options(${NB_LIBRARY_TARGET_NAME}
PRIVATE
-Wno-c++98-compat-extra-semi
-Wno-cast-qual
-Wno-covered-switch-default
-Wno-deprecated-literal-operator
-Wno-nested-anon-types
-Wno-unused-parameter
-Wno-zero-length-array
-Wno-missing-field-initializers
${eh_rtti_enable})
endif()
# Apply caller-provided extra options last so they have higher precedence.
target_compile_options(${NB_LIBRARY_TARGET_NAME}
PRIVATE
${ARG_EXTRA_COMPILE_OPTIONS}
@ -960,12 +991,7 @@ function(add_mlir_python_extension libname extname nb_library_target_name)
# The extension itself must be compiled with RTTI and exceptions enabled.
# Also, some warning classes triggered by nanobind are disabled.
set(eh_rtti_enable)
if (MSVC)
set(eh_rtti_enable /EHsc /GR)
elseif(LLVM_COMPILER_IS_GCC_COMPATIBLE OR CLANG_CL)
set(eh_rtti_enable -frtti -fexceptions)
endif ()
_mlir_python_eh_rtti_flags()
if(ARG__PRIVATE_SUPPORT_LIB)
add_library(${libname} SHARED ${ARG_SOURCES})
@ -1021,18 +1047,6 @@ function(add_mlir_python_extension libname extname nb_library_target_name)
# Avoid some warnings from upstream nanobind.
# If a superproject set MLIR_DISABLE_CONFIGURE_PYTHON_DEV_PACKAGES, let
# the super project handle compile options as it wishes.
target_compile_options(${nb_library_target_name}
PRIVATE
-Wno-c++98-compat-extra-semi
-Wno-cast-qual
-Wno-covered-switch-default
-Wno-deprecated-literal-operator
-Wno-nested-anon-types
-Wno-unused-parameter
-Wno-zero-length-array
-Wno-missing-field-initializers
${eh_rtti_enable})
target_compile_options(${libname}
PRIVATE
-Wno-c++98-compat-extra-semi
@ -1042,8 +1056,7 @@ function(add_mlir_python_extension libname extname nb_library_target_name)
-Wno-nested-anon-types
-Wno-unused-parameter
-Wno-zero-length-array
-Wno-missing-field-initializers
${eh_rtti_enable})
-Wno-missing-field-initializers)
endif()
if(APPLE)