CMake: Function for adding global compilation options in CMake
- pw_add_global_compile_options() adds compilation options by directly
updating the CMAKE_C_FLAGS and CMAKE_CXX_FLAGS variables. This is a
low-level feature that should only be used for truly global flags,
such as those that affect the ABI.
- Specify a minimum CMake version in pigweed.cmake so the IN_LIST
operator can be used.
- Add missing pw_log deps. Somehow, specifying the minimum CMake version
caused these missing deps to become errors.
Change-Id: I379980bc167f655751c2d0349c8812791a64e771
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/79441
Commit-Queue: Auto-Submit <auto-submit@pigweed.google.com.iam.gserviceaccount.com>
Commit-Queue: Wyatt Hepler <hepler@google.com>
Pigweed-Auto-Submit: Wyatt Hepler <hepler@google.com>
Reviewed-by: Ewout van Bekkum <ewout@google.com>
diff --git a/pw_build/docs.rst b/pw_build/docs.rst
index baeb87c..b55ea5b 100644
--- a/pw_build/docs.rst
+++ b/pw_build/docs.rst
@@ -725,8 +725,10 @@
automatically declare the library and its tests. This has been deprecated,
please use ``pw_add_module_library`` instead.
* ``pw_add_test`` -- Declare a test target.
-* ``pw_auto_add_module_tests`` -- Create test targets for all tests in a module.
- This has been deprecated, please use ``pw_add_test`` instead.
+* ``pw_add_global_compile_options`` -- Applies compilation options to all
+ targets in the build. This should only be used to add essential compilation
+ options, such as those that affect the ABI. Use ``pw_add_library`` or
+ ``target_compile_options`` to apply other compile options.
See ``pw_build/pigweed.cmake`` for the complete documentation of these
functions.
diff --git a/pw_build/pigweed.cmake b/pw_build/pigweed.cmake
index 4cf62dd..e40bac9 100644
--- a/pw_build/pigweed.cmake
+++ b/pw_build/pigweed.cmake
@@ -13,6 +13,8 @@
# the License.
include_guard(GLOBAL)
+cmake_minimum_required(VERSION 3.16)
+
# The PW_ROOT environment variable should be set in bootstrap. If it is not set,
# set it to the root of the Pigweed repository.
if("$ENV{PW_ROOT}" STREQUAL "")
@@ -567,3 +569,44 @@
add_dependencies("pw_run_tests.${group}" "${TEST_NAME}.run")
endforeach()
endfunction(pw_add_test_to_groups)
+
+# Adds compiler options to all targets built by CMake. Flags may be added any
+# time after this function is defined. The effect is global; all targets added
+# before or after a pw_add_global_compile_options call will be built with the
+# flags, regardless of where the files are located.
+#
+# pw_add_global_compile_options takes one optional named argument:
+#
+# LANGUAGES: Which languages (ASM, C, CXX) to apply the options to. Flags
+# apply to all languages by default.
+#
+# All other arguments are interpreted as compiler options.
+function(pw_add_global_compile_options)
+ cmake_parse_arguments(PARSE_ARGV 0 args "" "" "LANGUAGES")
+
+ set(supported_build_languages ASM C CXX)
+
+ if(NOT args_LANGUAGES)
+ set(args_LANGUAGES ${supported_build_languages})
+ endif()
+
+ # Check the selected language.
+ foreach(lang IN LISTS args_LANGUAGES)
+ if(NOT "${lang}" IN_LIST supported_build_languages)
+ message(FATAL_ERROR "'${lang}' is not a supported language. "
+ "Supported languages: ${supported_build_languages}")
+ endif()
+ endforeach()
+
+ # Enumerate which flags variables to set.
+ foreach(lang IN LISTS args_LANGUAGES)
+ list(APPEND cmake_flags_variables "CMAKE_${lang}_FLAGS")
+ endforeach()
+
+ # Set each flag for each specified flags variable.
+ foreach(variable IN LISTS cmake_flags_variables)
+ foreach(flag IN LISTS args_UNPARSED_ARGUMENTS)
+ set(${variable} "${${variable}} ${flag}" CACHE INTERNAL "" FORCE)
+ endforeach()
+ endforeach()
+endfunction(pw_add_global_compile_options)
diff --git a/pw_rpc/CMakeLists.txt b/pw_rpc/CMakeLists.txt
index 9c372ec..4f3391b 100644
--- a/pw_rpc/CMakeLists.txt
+++ b/pw_rpc/CMakeLists.txt
@@ -109,6 +109,8 @@
pw_bytes
pw_rpc.client
pw_rpc.server
+ PRIVATE_DEPS
+ pw_log
)
target_include_directories(pw_rpc.test_utils PUBLIC .)
diff --git a/targets/host/CMakeLists.txt b/targets/host/CMakeLists.txt
index dc7d487..83c4e11 100644
--- a/targets/host/CMakeLists.txt
+++ b/targets/host/CMakeLists.txt
@@ -21,6 +21,7 @@
system_rpc_server.cc
PRIVATE_DEPS
pw_hdlc
+ pw_log
pw_rpc.server
pw_stream.socket_stream
)