From d1462f31200a8e5e0d63fc052a87adbefe653830 Mon Sep 17 00:00:00 2001 From: Martin Stadler Date: Thu, 10 Jul 2025 01:31:42 +0200 Subject: [PATCH] Fix cmake `-Wno` compiler flag testing (#6145) --- verilator-config.cmake.in | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/verilator-config.cmake.in b/verilator-config.cmake.in index 13e386511..6214c02d4 100644 --- a/verilator-config.cmake.in +++ b/verilator-config.cmake.in @@ -68,15 +68,45 @@ function(_verilator_check_cxx_libraries LIBRARIES RESVAR) set("${RESVAR}" "${${RESVAR}}" PARENT_SCOPE) endfunction() +function(_verilator_check_cxx_compiler_flag FLAG RESVAR) + # Check whether the compiler understands the flag FLAG + set(_VERILATOR_CHECK_CXX_COMPILER_FLAG_SRC + " + #warning warning + int main() { return 0; } + " + ) + set(CMAKE_REQUIRED_FLAGS ${FLAG}) + # Make sure the compiler warning is not turned into an error + string(APPEND CMAKE_REQUIRED_FLAGS " -Wno-error=cpp") + set(CMAKE_REQUIRED_DEFINITIONS) + set(CMAKE_REQUIRED_INCLUDES) + set(CMAKE_REQUIRED_LINK_OPTIONS) + set(CMAKE_REQUIRED_LIBRARIES) + set(CMAKE_REQUIRED_QUIET) + + check_compiler_flag_common_patterns(_common_patterns) + check_cxx_source_compiles( + "${_VERILATOR_CHECK_CXX_COMPILER_FLAG_SRC}" + "${RESVAR}" + FAIL_REGEX + "command[ -]line option .* is valid for .* but not for C[+][+]" + FAIL_REGEX + "-Werror=.* argument .* is not valid for C[+][+]" + ${_common_patterns} + ) + set("${RESVAR}" "${${RESVAR}}" PARENT_SCOPE) +endfunction() + # Check compiler flag support. Skip on MSVC, these are all GCC flags. if(NOT CMAKE_CXX_COMPILER_ID MATCHES MSVC) if(NOT DEFINED VERILATOR_CFLAGS OR NOT DEFINED VERILATOR_MT_CFLAGS) include(CheckCXXCompilerFlag) foreach(FLAG @CFG_CXX_FLAGS_CMAKE@) string(MAKE_C_IDENTIFIER ${FLAG} FLAGNAME) - check_cxx_compiler_flag(${FLAG} ${FLAGNAME}) + _verilator_check_cxx_compiler_flag(${FLAG} ${FLAGNAME}) if(${FLAGNAME}) - list(APPEND VERILATOR_CFLAGS ${FLAG}) + list(APPEND VERILATOR_CFLAGS $<$:${FLAG}>) endif() endforeach() foreach(FLAG @CFG_LDFLAGS_THREADS_CMAKE@)