From 51ed6ec5209e23c62fff74bc811ebb881c07a454 Mon Sep 17 00:00:00 2001 From: Andrew Harper Date: Wed, 26 Jun 2024 16:52:08 +0000 Subject: [PATCH] pw_build_android: Enable function dynamic alloc MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Set the PW_FUNCTION_ENABLE_DYNAMIC_ALLOCATION define for Android builds. This allows `pw::Function`s to exceed the inline size. The motivation for setting this across the board is that components built with dynamic allocation disabled (i.e. PW_FUNCTION_ENABLE_DYNAMIC_ALLOCATION unset or set to 0) cannot be linked against components with dynamic allocation enabled. Since even the smallest Android environment is not nearly as constrained as a typical embedded environment, the solution is to always set this flag on Android. Bug: 349352849 Test: Built Android locally. Change-Id: I7209151c2508026258c23f931a5c12c64c733a1e Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/218219 Reviewed-by: Eli Lipsitz Reviewed-by: Carlos Chinchilla Reviewed-by: Jonathon Reinhart Commit-Queue: Andrew Harper Lint: Lint 🤖 --- pw_build_android/Android.bp | 10 +++++++++- pw_build_android/docs.rst | 17 +++++++++++++++++ pw_function/docs.rst | 2 ++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/pw_build_android/Android.bp b/pw_build_android/Android.bp index 6fa4d1d58..31cc9510f 100644 --- a/pw_build_android/Android.bp +++ b/pw_build_android/Android.bp @@ -16,13 +16,21 @@ package { default_applicable_licenses: ["external_pigweed_license"], } -// List of Common Android backends used by Pigweed modules. +// List of Common Android backends and build flags used by Pigweed modules. // Do not modify this to swap a different backend. Instead see the // pw_build_android documentation for how to use custom backends. cc_defaults { name: "pw_android_common_backends", cpp_std: "c++20", + // Note for maintainers: + // Update the build flags in the documentation when updating this list. + cflags: [ + // Allow `pw::Function`s to exceed the inline size. This assumes all + // targets built with this backend can use dynamic allocation. + "-DPW_FUNCTION_ENABLE_DYNAMIC_ALLOCATION=1", + ], + // Note for maintainers: // Update the common backends list in the documentation when updating this // list. diff --git a/pw_build_android/docs.rst b/pw_build_android/docs.rst index ef2d4c0e6..84b205671 100644 --- a/pw_build_android/docs.rst +++ b/pw_build_android/docs.rst @@ -376,3 +376,20 @@ Backends are defined the same way as :ref:`module-pw_build_android-module-libraries`. They must follow the ``pw_._`` name format or ``pw__`` if applicable. + +----------- +Build flags +----------- +Some build flags should be set for all Android targets; these flags are +specified in ``pw_android_common_backends``. These flags are as follows: + +``PW_FUNCTION_ENABLE_DYNAMIC_ALLOCATION`` +----------------------------------------- +As discussed in :ref:`module-pw_function-dynamic-allocation`, this flag enables +dynamic allocation of :cpp:type:`pw::Function`, allowing it to exceed the +inline size limit. + +Android targets support dynamic allocation since the Android environment is not +memory constrained. Thus, ``PW_FUNCTION_ENABLE_DYNAMIC_ALLOCATION`` is enabled +in ``pw_android_common_backends``. Components built with dynamic allocation +disabled cannot be linked against components with dynamic allocation enabled. diff --git a/pw_function/docs.rst b/pw_function/docs.rst index 3d2539a96..f9a171290 100644 --- a/pw_function/docs.rst +++ b/pw_function/docs.rst @@ -327,6 +327,8 @@ is a compile-time error unless dynamic allocation is enabled. // Compiler error: sizeof(MyCallable) exceeds function's inline storage size. pw::Function function((MyCallable())); +.. _module-pw_function-dynamic-allocation: + Dynamic allocation ================== You can configure the inline allocation size of ``pw::Function`` and whether it