Make treating warnings as errors configurable
Introduce the CMAKE_COMPILE_WARNING_AS_ERROR build option to control
whether the project treats compiler warnings as errors. This option is
enabled by default (On) and can be overridden via the CMake command line
using the -D flag or with an environment variable with the same name.
If both are specified, the command-line option takes precedence over the
environment variable.
Enforcing warnings as errors helps maintain code quality, which is
especially important for security-focused projects. However, this can be
problematic for system integrators using newer compiler versions than
those used in upstream CI, as new compilers may introduce additional
warnings that cause builds to fail.
This option provides a temporary workaround by allowing such builds to
proceed until the warnings are addressed properly.
Change-Id: I20c816887b0f02257f8cf2b198fb98e949b3a7fd
Signed-off-by: Gyorgy Szing <gyorgy.szing@arm.com>
diff --git a/environments/arm-linux/default_toolchain_file.cmake b/environments/arm-linux/default_toolchain_file.cmake
index 7e565dd..d5900b0 100644
--- a/environments/arm-linux/default_toolchain_file.cmake
+++ b/environments/arm-linux/default_toolchain_file.cmake
@@ -1,5 +1,5 @@
#-------------------------------------------------------------------------------
-# Copyright (c) 2020-2022, Arm Limited and Contributors. All rights reserved.
+# Copyright (c) 2020-2025, Arm Limited and Contributors. All rights reserved.
#
# SPDX-License-Identifier: BSD-3-Clause
#
@@ -19,9 +19,29 @@
set(TS_DEBUG_INFO_FLAGS "-fdiagnostics-show-option -gdwarf-2" CACHE STRING "Compiler flags to add debug information.")
set(TS_MANDATORY_AARCH_FLAGS "-mstrict-align -march=armv8-a+crc -DARM64=1" CACHE STRING "Compiler flags configuring architecture specific ")
-set(TS_WARNING_FLAGS "-Wall -Werror" CACHE STRING "Compiler flags affecting generating warning messages.")
+set(TS_WARNING_FLAGS "-Wall" CACHE STRING "Compiler flags affecting generating warning messages.")
set(TS_MANDATORY_LINKER_FLAGS "" CACHE STRING "Linker flags needed for correct builds.")
+# Allow defining the "warning as error behavior" using an environment variable. But prioritize command line
+# definition if present.
+# If a cache variable is not present
+if(NOT DEFINED CACHE{CMAKE_COMPILE_WARNING_AS_ERROR})
+ # And an environment variable is, copy its value to the cache
+ if (DEFINED ENV{CMAKE_COMPILE_WARNING_AS_ERROR})
+ set(CMAKE_COMPILE_WARNING_AS_ERROR $ENV{CMAKE_COMPILE_WARNING_AS_ERROR} CACHE Bool "If compilation warnings should be treated as errors.")
+ endif()
+endif()
+
+# By default warnings should be treated as errors.
+set(CMAKE_COMPILE_WARNING_AS_ERROR On CACHE BOOL "If compilation warnings should be treated as errors.")
+
+# Cmake v3.24 + shall set the warning flag automatically, but does not when processing our deployments. As a workaround
+# set -Werror manually always as setting it twice shall have no ill effect.
+if (CMAKE_COMPILE_WARNING_AS_ERROR)
+ string(APPEND TS_WARNING_FLAGS " -Werror")
+endif()
+
+
# branch-protection enables bti/pac while compile force-bti tells the linker to
# warn if some object files lack the .note.gnu.property section with the BTI
# flag, and to turn on the BTI flag in the output anyway.
diff --git a/environments/linux-pc/default_toolchain_file.cmake b/environments/linux-pc/default_toolchain_file.cmake
index 2215d6b..010105c 100644
--- a/environments/linux-pc/default_toolchain_file.cmake
+++ b/environments/linux-pc/default_toolchain_file.cmake
@@ -1,5 +1,5 @@
#-------------------------------------------------------------------------------
-# Copyright (c) 2020-2022, Arm Limited and Contributors. All rights reserved.
+# Copyright (c) 2020-2025, Arm Limited and Contributors. All rights reserved.
#
# SPDX-License-Identifier: BSD-3-Clause
#
@@ -11,9 +11,28 @@
set(TS_DEBUG_INFO_FLAGS "-fdiagnostics-show-option -gdwarf-2" CACHE STRING "Compiler flags to add debug information.")
set(TS_MANDATORY_AARCH_FLAGS "" CACHE STRING "Compiler flags configuring architecture specific ")
-set(TS_WARNING_FLAGS "-Wall -Werror" CACHE STRING "Compiler flags affecting generating warning messages.")
+set(TS_WARNING_FLAGS "-Wall" CACHE STRING "Compiler flags affecting generating warning messages.")
set(TS_MANDATORY_LINKER_FLAGS "" CACHE STRING "Linker flags needed for correct builds.")
+# Allow defining the "warning as error behavior" using an environment variable. But prioritize command line
+# definition if present.
+# If a cache variable is not present
+if(NOT DEFINED CACHE{CMAKE_COMPILE_WARNING_AS_ERROR})
+ # And an environment variable is, copy its value to the cache
+ if (DEFINED ENV{CMAKE_COMPILE_WARNING_AS_ERROR})
+ set(CMAKE_COMPILE_WARNING_AS_ERROR $ENV{CMAKE_COMPILE_WARNING_AS_ERROR} CACHE Bool "If compilation warnings should be treated as errors.")
+ endif()
+endif()
+
+# By default warnings should be treated as errors.
+set(CMAKE_COMPILE_WARNING_AS_ERROR On CACHE BOOL "If compilation warnings should be treated as errors.")
+
+# Cmake v3.24 + shall set the warning flag automatically, but does not when processing our deployments. As a workaround
+# set -Werror manually always as setting it twice shall have no ill effect.
+if (CMAKE_COMPILE_WARNING_AS_ERROR)
+ string(APPEND TS_WARNING_FLAGS " -Werror")
+endif()
+
# Set flags affecting all build types
string(APPEND CMAKE_C_FLAGS_INIT " ${TS_MANDATORY_AARCH_FLAGS}")
string(APPEND CMAKE_CXX_FLAGS_INIT " ${TS_MANDATORY_AARCH_FLAGS}")
diff --git a/environments/opteesp/default_toolchain_file.cmake b/environments/opteesp/default_toolchain_file.cmake
index b150b85..677e92f 100644
--- a/environments/opteesp/default_toolchain_file.cmake
+++ b/environments/opteesp/default_toolchain_file.cmake
@@ -1,5 +1,5 @@
#-------------------------------------------------------------------------------
-# Copyright (c) 2020-2022, Arm Limited and Contributors. All rights reserved.
+# Copyright (c) 2020-2025, Arm Limited and Contributors. All rights reserved.
#
# SPDX-License-Identifier: BSD-3-Clause
#
@@ -21,10 +21,29 @@
set(TS_DEBUG_INFO_FLAGS "-fdiagnostics-show-option -gdwarf-2" CACHE STRING "Compiler flags to add debug information.")
set(TS_MANDATORY_AARCH_FLAGS "-fpic -mstrict-align -march=armv8-a+crc" CACHE STRING "Compiler flags configuring architecture specific ")
-set(TS_WARNING_FLAGS "-Wall -Werror" CACHE STRING "Compiler flags affecting generating warning messages.")
+set(TS_WARNING_FLAGS "-Wall" CACHE STRING "Compiler flags affecting generating warning messages.")
set(TS_MANDATORY_LINKER_FLAGS "-pie -Wl,--as-needed -Wl,--sort-section=alignment -zmax-page-size=4096"
CACHE STRING "Linker flags needed for correct builds.")
+# Allow defining the "warning as error behavior" using an environment variable. But prioritize command line
+# definition if present.
+# If a cache variable is not present
+if(NOT DEFINED CACHE{CMAKE_COMPILE_WARNING_AS_ERROR})
+ # And an environment variable is, copy its value to the cache
+ if (DEFINED ENV{CMAKE_COMPILE_WARNING_AS_ERROR})
+ set(CMAKE_COMPILE_WARNING_AS_ERROR $ENV{CMAKE_COMPILE_WARNING_AS_ERROR} CACHE Bool "If compilation warnings should be treated as errors.")
+ endif()
+endif()
+
+# By default warnings should be treated as errors.
+set(CMAKE_COMPILE_WARNING_AS_ERROR On CACHE BOOL "If compilation warnings should be treated as errors.")
+
+# Cmake v3.24 + shall set the warning flag automatically, but does not when processing our deployments. As a workaround
+# set -Werror manually always as setting it twice shall have no ill effect.
+if (CMAKE_COMPILE_WARNING_AS_ERROR)
+ string(APPEND TS_WARNING_FLAGS " -Werror")
+endif()
+
# branch-protection enables bti/pac while compile force-bti tells the linker to
# warn if some object files lack the .note.gnu.property section with the BTI
# flag, and to turn on the BTI flag in the output anyway.
diff --git a/environments/sp/default_toolchain_file.cmake b/environments/sp/default_toolchain_file.cmake
index d41ecec..bbe4fb9 100644
--- a/environments/sp/default_toolchain_file.cmake
+++ b/environments/sp/default_toolchain_file.cmake
@@ -1,5 +1,5 @@
#-------------------------------------------------------------------------------
-# Copyright (c) 2020-2022, Arm Limited and Contributors. All rights reserved.
+# Copyright (c) 2020-2025, Arm Limited and Contributors. All rights reserved.
#
# SPDX-License-Identifier: BSD-3-Clause
#
@@ -24,6 +24,25 @@
set(TS_WARNING_FLAGS "-Wall" CACHE STRING "Compiler flags affecting generating warning messages.")
set(TS_MANDATORY_LINKER_FLAGS "-Wl,-pie -Wl,--no-dynamic-linker -Wl,--sort-section=alignment -zmax-page-size=4096" CACHE STRING "Linker flags needed for correct builds.")
+# Allow defining the "warning as error behavior" using an environment variable. But prioritize command line
+# definition if present.
+# If a cache variable is not present
+if(NOT DEFINED CACHE{CMAKE_COMPILE_WARNING_AS_ERROR})
+ # And an environment variable is, copy its value to the cache
+ if (DEFINED ENV{CMAKE_COMPILE_WARNING_AS_ERROR})
+ set(CMAKE_COMPILE_WARNING_AS_ERROR $ENV{CMAKE_COMPILE_WARNING_AS_ERROR} CACHE Bool "If compilation warnings should be treated as errors.")
+ endif()
+endif()
+
+# By default warnings should be treated as errors.
+set(CMAKE_COMPILE_WARNING_AS_ERROR On CACHE BOOL "If compilation warnings should be treated as errors.")
+
+# Cmake v3.24 + shall set the warning flag automatically, but does not when processing our deployments. As a workaround
+# set -Werror manually always as setting it twice shall have no ill effect.
+if (CMAKE_COMPILE_WARNING_AS_ERROR)
+ string(APPEND TS_WARNING_FLAGS " -Werror")
+endif()
+
# branch-protection enables bti/pac while compile force-bti tells the linker to
# warn if some object files lack the .note.gnu.property section with the BTI
# flag, and to turn on the BTI flag in the output anyway.