bazel: Simplify pw_system usage

Bug: 341144405
Change-Id: I7a05c08818616b65202b31dc2ff3b1ca6528ccbc
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/examples/+/210121
Commit-Queue: Ted Pudlik <tpudlik@google.com>
Lint: Lint 🤖 <android-build-ayeaye@system.gserviceaccount.com>
Presubmit-Verified: CQ Bot Account <pigweed-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Anthony DiGirolamo <tonymd@google.com>
diff --git a/.bazelrc b/.bazelrc
index 1b362fa..768ed3d 100644
--- a/.bazelrc
+++ b/.bazelrc
@@ -23,6 +23,8 @@
 # our toolchains, and CI VMs may not have any local toolchain to detect.
 common --repo_env=BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1
 
+common --custom_malloc=//targets:malloc
+
 # Ignore all warnings in third-party code.
 common --per_file_copt=external/.*@-w
 common --host_per_file_copt=external/.*@-w
diff --git a/examples/01_blinky/BUILD.bazel b/examples/01_blinky/BUILD.bazel
index a2e8dd1..f5ac3f8 100644
--- a/examples/01_blinky/BUILD.bazel
+++ b/examples/01_blinky/BUILD.bazel
@@ -20,40 +20,13 @@
 cc_binary(
     name = "blinky",
     srcs = ["main.cc"],
-    malloc = select({
-        "@pico-sdk//bazel/constraint:rp2": "@pigweed//pw_malloc",
-        "@platforms//cpu:armv7e-m": "@pigweed//pw_malloc",
-        "//conditions:default": "@bazel_tools//tools/cpp:malloc",
-    }),
     deps = [
         "//libraries/pw_board_led",
-        "@pigweed//pw_assert:backend_impl",
         "@pigweed//pw_chrono:system_clock",
         "@pigweed//pw_chrono:system_timer",
         "@pigweed//pw_log",
-        "@pigweed//pw_log:backend_impl",
-        "@pigweed//pw_system:init",
-        "@pigweed//pw_system:io",
-        "@pigweed//pw_system:rpc_server",
-        "@pigweed//pw_system:target_hooks",
-        "@pigweed//pw_system:work_queue",
-    ] + select({
-        "@pico-sdk//bazel/constraint:rp2": [
-            "@pico-sdk//src/common/pico_stdlib:pico_stdlib",
-            "@pigweed//pw_tokenizer:linker_script",
-            "@pigweed//pw_toolchain/arm_gcc:arm_none_eabi_gcc_support",
-            "@pigweed//targets/rp2040:pre_init",
-        ],
-        "@platforms//cpu:armv7e-m": [
-            "//targets/stm32f429i_disc1_stm32cube:linker_script",
-            "@pigweed//pw_tokenizer:linker_script",
-            "@pigweed//pw_toolchain/arm_gcc:arm_none_eabi_gcc_support",
-            "@pigweed//targets/stm32f429i_disc1_stm32cube:pre_init",
-        ],
-        "//conditions:default": [
-            "@pigweed//targets/host_device_simulator:boot",
-        ],
-    }),
+        "@pigweed//pw_system",
+    ],
 )
 
 stm32_binary(
diff --git a/examples/02_unit_testing/BUILD.bazel b/examples/02_unit_testing/BUILD.bazel
index 4afa384..88c50da 100644
--- a/examples/02_unit_testing/BUILD.bazel
+++ b/examples/02_unit_testing/BUILD.bazel
@@ -42,41 +42,14 @@
     name = "test_runner_app",
     testonly = True,
     srcs = ["main.cc"],
-    malloc = select({
-        "@pico-sdk//bazel/constraint:rp2": "@pigweed//pw_malloc",
-        "@platforms//cpu:armv7e-m": "@pigweed//pw_malloc",
-        "//conditions:default": "@bazel_tools//tools/cpp:malloc",
-    }),
     deps = [
         ":bitops_test.lib",
-        "@pigweed//pw_assert:backend_impl",
         "@pigweed//pw_chrono:system_clock",
         "@pigweed//pw_chrono:system_timer",
         "@pigweed//pw_log",
-        "@pigweed//pw_log:backend_impl",
-        "@pigweed//pw_system:init",
-        "@pigweed//pw_system:io",
-        "@pigweed//pw_system:rpc_server",
-        "@pigweed//pw_system:target_hooks",
-        "@pigweed//pw_system:work_queue",
+        "@pigweed//pw_system",
         "@pigweed//pw_unit_test:rpc_service",
-    ] + select({
-        "@pico-sdk//bazel/constraint:rp2": [
-            "@pico-sdk//src/common/pico_stdlib:pico_stdlib",
-            "@pigweed//pw_tokenizer:linker_script",
-            "@pigweed//pw_toolchain/arm_gcc:arm_none_eabi_gcc_support",
-            "@pigweed//targets/rp2040:pre_init",
-        ],
-        "@platforms//cpu:armv7e-m": [
-            "//targets/stm32f429i_disc1_stm32cube:linker_script",
-            "@pigweed//pw_tokenizer:linker_script",
-            "@pigweed//pw_toolchain/arm_gcc:arm_none_eabi_gcc_support",
-            "@pigweed//targets/stm32f429i_disc1_stm32cube:pre_init",
-        ],
-        "//conditions:default": [
-            "@pigweed//targets/host_device_simulator:boot",
-        ],
-    }),
+    ],
 )
 
 stm32_binary(
diff --git a/examples/03_rpc/BUILD.bazel b/examples/03_rpc/BUILD.bazel
index 2cb9f5c..ef092d6 100644
--- a/examples/03_rpc/BUILD.bazel
+++ b/examples/03_rpc/BUILD.bazel
@@ -62,38 +62,11 @@
 cc_binary(
     name = "rpc_main",
     srcs = ["main.cc"],
-    malloc = select({
-        "@pico-sdk//bazel/constraint:rp2": "@pigweed//pw_malloc",
-        "@platforms//cpu:armv7e-m": "@pigweed//pw_malloc",
-        "//conditions:default": "@bazel_tools//tools/cpp:malloc",
-    }),
     deps = [
         ":rpc_example_service_nanopb",
-        "@pigweed//pw_assert:backend_impl",
         "@pigweed//pw_log",
-        "@pigweed//pw_log:backend_impl",
-        "@pigweed//pw_system:init",
-        "@pigweed//pw_system:io",
-        "@pigweed//pw_system:rpc_server",
-        "@pigweed//pw_system:target_hooks",
-        "@pigweed//pw_system:work_queue",
-    ] + select({
-        "@pico-sdk//bazel/constraint:rp2": [
-            "@pico-sdk//src/common/pico_stdlib:pico_stdlib",
-            "@pigweed//pw_tokenizer:linker_script",
-            "@pigweed//pw_toolchain/arm_gcc:arm_none_eabi_gcc_support",
-            "@pigweed//targets/rp2040:pre_init",
-        ],
-        "@platforms//cpu:armv7e-m": [
-            "//targets/stm32f429i_disc1_stm32cube:linker_script",
-            "@pigweed//pw_tokenizer:linker_script",
-            "@pigweed//pw_toolchain/arm_gcc:arm_none_eabi_gcc_support",
-            "@pigweed//targets/stm32f429i_disc1_stm32cube:pre_init",
-        ],
-        "//conditions:default": [
-            "@pigweed//targets/host_device_simulator:boot",
-        ],
-    }),
+        "@pigweed//pw_system",
+    ],
 )
 
 stm32_binary(
diff --git a/targets/BUILD.bazel b/targets/BUILD.bazel
index 0690f37..5c8cecb 100644
--- a/targets/BUILD.bazel
+++ b/targets/BUILD.bazel
@@ -12,5 +12,13 @@
 # License for the specific language governing permissions and limitations under
 # the License.
 
-# Empty BUILD.bazel file, needed because "Every .bzl file must have a
-# corresponding package".
+# TODO: https://github.com/bazelbuild/bazel/issues/22457 - Move this into the
+# platform once it no longer propagates to the exec configuration.
+alias(
+    name = "malloc",
+    actual = select({
+        "@pico-sdk//bazel/constraint:rp2": "@pigweed//pw_malloc",
+        "@platforms//cpu:armv7e-m": "@pigweed//pw_malloc",
+        "//conditions:default": "@bazel_tools//tools/cpp:malloc",
+    }),
+)
diff --git a/targets/rp2040/BUILD.bazel b/targets/rp2040/BUILD.bazel
index 9727547..4730191 100644
--- a/targets/rp2040/BUILD.bazel
+++ b/targets/rp2040/BUILD.bazel
@@ -33,3 +33,13 @@
         "@pw_toolchain//constraints/arm_mcpu:cortex-m0",
     ],
 )
+
+cc_library(
+    name = "extra_platform_libs",
+    deps = [
+        "@pico-sdk//src/common/pico_stdlib:pico_stdlib",
+        "@pigweed//pw_tokenizer:linker_script",
+        "@pigweed//pw_toolchain/arm_gcc:arm_none_eabi_gcc_support",
+        "@pigweed//targets/rp2040:pre_init",
+    ],
+)
diff --git a/targets/stm32f429i_disc1_stm32cube/BUILD.bazel b/targets/stm32f429i_disc1_stm32cube/BUILD.bazel
index e538e2d..565475c 100644
--- a/targets/stm32f429i_disc1_stm32cube/BUILD.bazel
+++ b/targets/stm32f429i_disc1_stm32cube/BUILD.bazel
@@ -47,3 +47,13 @@
     ],
     linker_script = "@pigweed//pw_boot_cortex_m:basic_cortex_m.ld",
 )
+
+cc_library(
+    name = "extra_platform_libs",
+    deps = [
+        ":linker_script",
+        "@pigweed//pw_tokenizer:linker_script",
+        "@pigweed//pw_toolchain/arm_gcc:arm_none_eabi_gcc_support",
+        "@pigweed//targets/stm32f429i_disc1_stm32cube:pre_init",
+    ],
+)
diff --git a/targets/transition.bzl b/targets/transition.bzl
index bdceaf9..1bd4015 100644
--- a/targets/transition.bzl
+++ b/targets/transition.bzl
@@ -28,6 +28,7 @@
         "@pigweed//pw_log:backend": "@pigweed//pw_log_tokenized",
         "@pigweed//pw_log:backend_impl": "@pigweed//pw_log_tokenized:impl",
         "@pigweed//pw_log_tokenized:handler_backend": "@pigweed//pw_system:log_backend",
+        "@pigweed//pw_system:extra_platform_libs": "//targets/stm32f429i_disc1_stm32cube:extra_platform_libs",
     }
 
 _stm32_transition = transition(
@@ -40,6 +41,7 @@
         "@pigweed//pw_log:backend_impl",
         "@pigweed//pw_log:backend",
         "@pigweed//pw_log_tokenized:handler_backend",
+        "@pigweed//pw_system:extra_platform_libs",
     ],
 )
 
@@ -73,6 +75,7 @@
         "@pigweed//pw_log:backend": "@pigweed//pw_log_tokenized",
         "@pigweed//pw_log:backend_impl": "@pigweed//pw_log_tokenized:impl",
         "@pigweed//pw_log_tokenized:handler_backend": "@pigweed//pw_system:log_backend",
+        "@pigweed//pw_system:extra_platform_libs": "//targets/rp2040:extra_platform_libs",
     }
 
 _rp2040_transition = transition(
@@ -83,6 +86,7 @@
         "@pigweed//pw_log:backend_impl",
         "@pigweed//pw_log:backend",
         "@pigweed//pw_log_tokenized:handler_backend",
+        "@pigweed//pw_system:extra_platform_libs",
     ],
 )