pw_build: Make build warnings configurable in CMake

Allow configuring which warnings to use in builds with the
pw_build_WARNINGS variable.

Change-Id: Ib9dbb4314976bf78b9f720cadd8e797c5118ed03
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/79480
Pigweed-Auto-Submit: Wyatt Hepler <hepler@google.com>
Reviewed-by: Ewout van Bekkum <ewout@google.com>
Commit-Queue: Wyatt Hepler <hepler@google.com>
diff --git a/pw_build/CMakeLists.txt b/pw_build/CMakeLists.txt
index d4ad2c3..b2a9b36 100644
--- a/pw_build/CMakeLists.txt
+++ b/pw_build/CMakeLists.txt
@@ -48,6 +48,23 @@
     $<$<COMPILE_LANGUAGE:CXX>:-fno-rtti>
 )
 
+# Define the standard Pigweed compile options.
+#
+# The pw_build.warnings library is used by upstream Pigweed targets to add
+# compiler warnings to the build.
+#
+# Toolchains may override these warnings by setting pw_build_WARNINGS:
+#
+#   set(pw_build_WARNINGS my_warnings CACHE STRING "" FORCE)
+#
+set(pw_build_WARNINGS pw_build.strict_warnings
+    CACHE STRING "Warnings libraries to use for Pigweed upstream code")
+
+add_library(pw_build.warnings INTERFACE)
+target_link_libraries(pw_build.warnings INTERFACE ${pw_build_WARNINGS})
+
+# TODO(hepler): These Zephyr exceptions should be made by overriding
+#     pw_build_WARNINGS.
 add_library(pw_build.strict_warnings INTERFACE)
 if(NOT ZEPHYR_PIGWEED_MODULE_DIR)
   # Only include these flags if we're not building with Zephyr.
diff --git a/pw_build/docs.rst b/pw_build/docs.rst
index 7d2033c..ff95b5a 100644
--- a/pw_build/docs.rst
+++ b/pw_build/docs.rst
@@ -662,6 +662,13 @@
 For Pigweed embedded builds, set ``CMAKE_SYSTEM_NAME`` to the empty string
 (``""``).
 
+Toolchains may set the ``pw_build_WARNINGS`` variable to a list of ``INTERFACE``
+libraries with compilation options for Pigweed's upstream libraries. This
+defaults to a strict set of warnings. Projects may need to use less strict
+compilation warnings to compile backends exposed to Pigweed code (such as
+``pw_log``) that cannot compile with Pigweed's flags. If desired, Projects can
+access these warnings by depending on ``pw_build.warnings``.
+
 Third party libraries
 ---------------------
 The CMake build includes third-party libraries similarly to the GN build. A
diff --git a/pw_build/pigweed.cmake b/pw_build/pigweed.cmake
index 1102220..3266f49 100644
--- a/pw_build/pigweed.cmake
+++ b/pw_build/pigweed.cmake
@@ -214,8 +214,7 @@
       pw_build
       ${arg_PUBLIC_DEPS}
     PRIVATE
-      pw_build.strict_warnings
-      pw_build.extra_strict_warnings
+      pw_build.warnings
       ${arg_PRIVATE_DEPS}
   )
 
diff --git a/pw_toolchain/host_clang/toolchain.cmake b/pw_toolchain/host_clang/toolchain.cmake
index da66dc2..b58cd8b 100644
--- a/pw_toolchain/host_clang/toolchain.cmake
+++ b/pw_toolchain/host_clang/toolchain.cmake
@@ -16,6 +16,9 @@
 # Regardless of whether it's set or not the following include will ensure it is.
 include(${CMAKE_CURRENT_LIST_DIR}/../../pw_build/pigweed.cmake)
 
+set(CMAKE_C_COMPILER clang)
+set(CMAKE_CXX_COMPILER clang++)
+
 pw_set_backend(pw_assert pw_assert_log)
 pw_set_backend(pw_chrono.system_clock pw_chrono_stl.system_clock)
 pw_set_backend(pw_log pw_log_basic)
@@ -23,5 +26,5 @@
 pw_set_backend(pw_sync.mutex pw_sync_stl.mutex_backend)
 pw_set_backend(pw_sys_io pw_sys_io_stdio)
 
-set(CMAKE_C_COMPILER clang)
-set(CMAKE_CXX_COMPILER clang++)
+set(pw_build_WARNINGS pw_build.strict_warnings pw_build.extra_strict_warnings
+    CACHE STRING "" FORCE)
diff --git a/pw_toolchain/host_gcc/toolchain.cmake b/pw_toolchain/host_gcc/toolchain.cmake
index 5d07ab1..38744fc 100644
--- a/pw_toolchain/host_gcc/toolchain.cmake
+++ b/pw_toolchain/host_gcc/toolchain.cmake
@@ -16,6 +16,9 @@
 # Regardless of whether it's set or not the following include will ensure it is.
 include(${CMAKE_CURRENT_LIST_DIR}/../../pw_build/pigweed.cmake)
 
+set(CMAKE_C_COMPILER gcc)
+set(CMAKE_CXX_COMPILER g++)
+
 pw_set_backend(pw_assert pw_assert_log)
 pw_set_backend(pw_chrono.system_clock pw_chrono_stl.system_clock)
 pw_set_backend(pw_log pw_log_basic)
@@ -23,5 +26,5 @@
 pw_set_backend(pw_sync.mutex pw_sync_stl.mutex_backend)
 pw_set_backend(pw_sys_io pw_sys_io_stdio)
 
-set(CMAKE_C_COMPILER gcc)
-set(CMAKE_CXX_COMPILER g++)
+set(pw_build_WARNINGS pw_build.strict_warnings pw_build.extra_strict_warnings
+    CACHE STRING "" FORCE)