Skip to content

Commit

Permalink
pw_function: Dynamic allocation for upstream host
Browse files Browse the repository at this point in the history
Enable dynamic allocation in pw_function, for Bazel upstream host builds
only. This has no effect on downstream projects or non-host builds.

Change-Id: Ife7eef3e13e533867c923c5c2dc9dd966f637ceb
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/221871
Reviewed-by: Erik Gilling <[email protected]>
Commit-Queue: Rob Mohr <[email protected]>
Lint: Lint 🤖 <[email protected]>
Reviewed-by: Taylor Cramer <[email protected]>
Commit-Queue: Ted Pudlik <[email protected]>
  • Loading branch information
tpudlik authored and CQ Bot Account committed Jul 11, 2024
1 parent 61c6b9a commit cd7e34b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ common --@stm32f4xx_hal_driver//:cmsis_init=@cmsis_device_f4//:default_cmsis_ini

common --//pw_env_setup/py:pigweed_json=//:pigweed.json

build --//pw_function:config_override=//pw_function:upstream_default_config

# Config for the rp2040 platform.
#
Expand Down
27 changes: 27 additions & 0 deletions pw_function/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# License for the specific language governing permissions and limitations under
# the License.

load("@bazel_skylib//lib:selects.bzl", "selects")
load(
"//pw_build:pigweed.bzl",
"pw_cc_test",
Expand Down Expand Up @@ -84,3 +85,29 @@ pw_cc_test(
":scope_guard",
],
)

# pw_function config for upstream builds.
#
# See https://pigweed.dev/pw_function/#dynamic-allocation for more context.
alias(
name = "upstream_default_config",
actual = selects.with_or({
(
"@platforms//os:android",
"@platforms//os:chromiumos",
"@platforms//os:linux",
"@platforms//os:macos",
"@platforms//os:windows",
): ":config_for_host",
"//conditions:default": "//pw_build:default_module_config",
}),
visibility = ["//visibility:private"],
)

cc_library(
name = "config_for_host",
defines = [
"PW_FUNCTION_ENABLE_DYNAMIC_ALLOCATION=1",
],
visibility = ["//visibility:private"],
)
9 changes: 9 additions & 0 deletions pw_function/docs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,15 @@ enabled but a compile-time check for the inlining is still required,
cast from :cpp:type:`pw::InlineFunction` to a regular
:cpp:type:`pw::Function` will **ALWAYS** allocate memory.

.. note::

When building Pigweed itself for host platforms, we enable dynamic
allocation. This is required for some modules that use ``pw::Function``,
like :ref:`module-pw_bluetooth_sapphire`. But it is *not* the default for
downstream projects because it introduces a difference between host and
non-host builds. This difference has the potential to cause breakages if
code is built for host first, and then later ported to device.

Invoking ``pw::Function`` from a C-style API
============================================
.. _trampoline layers: https://en.wikipedia.org/wiki/Trampoline_(computing)
Expand Down

0 comments on commit cd7e34b

Please sign in to comment.