bazel: Re-enable platform-based flags

Bug: b/352389854
Change-Id: I3aa7aa6a845359d22a7f87ef65e53f81d9aae254
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/quickstart/bazel/+/232955
Presubmit-Verified: CQ Bot Account <pigweed-scoped@luci-project-accounts.iam.gserviceaccount.com>
Commit-Queue: Taylor Cramer <cramertj@google.com>
Lint: Lint 🤖 <android-build-ayeaye@system.gserviceaccount.com>
Reviewed-by: Ted Pudlik <tpudlik@google.com>
diff --git a/.bazelrc b/.bazelrc
index f28f823..182f3be 100644
--- a/.bazelrc
+++ b/.bazelrc
@@ -125,42 +125,6 @@
 
 # RP2040 platform configuration
 build:rp2040 --platforms=//targets/rp2:rp2040
-build:rp2040 --//system:system=//targets/rp2:system
-build:rp2040 --@pigweed//pw_assert:assert_backend=@pigweed//pw_assert_trap
-build:rp2040 --@pigweed//pw_assert:assert_backend_impl=@pigweed//pw_assert_trap:impl
-build:rp2040 --@pigweed//pw_assert:check_backend=@pigweed//pw_assert_trap
-build:rp2040 --@pigweed//pw_assert:check_backend_impl=@pigweed//pw_assert_trap:impl
-build:rp2040 --@pigweed//pw_cpu_exception:entry_backend=@pigweed//pw_cpu_exception_cortex_m:cpu_exception
-build:rp2040 --@pigweed//pw_cpu_exception:entry_backend_impl=@pigweed//pw_cpu_exception_cortex_m:cpu_exception_impl
-build:rp2040 --@pigweed//pw_cpu_exception:handler_backend=@pigweed//pw_cpu_exception:basic_handler
-build:rp2040 --@pigweed//pw_cpu_exception:support_backend=@pigweed//pw_cpu_exception_cortex_m:support
-build:rp2040 --@pigweed//pw_interrupt:backend=@pigweed//pw_interrupt_cortex_m:context
-build:rp2040 --@pigweed//pw_log:backend=@pigweed//pw_log_tokenized
-build:rp2040 --@pigweed//pw_log:backend_impl=@pigweed//pw_log_tokenized:impl
-build:rp2040 --@pigweed//pw_log_tokenized:handler_backend=@pigweed//pw_system:log_backend
-build:rp2040 --@pigweed//pw_sync:binary_semaphore_backend=@pigweed//pw_sync_freertos:binary_semaphore
-build:rp2040 --@pigweed//pw_sync:interrupt_spin_lock_backend=@pigweed//pw_sync_freertos:interrupt_spin_lock
-build:rp2040 --@pigweed//pw_sync:mutex_backend=@pigweed//pw_sync_freertos:mutex
-build:rp2040 --@pigweed//pw_sync:thread_notification_backend=@pigweed//pw_sync_freertos:thread_notification
-build:rp2040 --@pigweed//pw_sync:timed_thread_notification_backend=@pigweed//pw_sync_freertos:timed_thread_notification
-build:rp2040 --@pigweed//pw_sys_io:backend=@pigweed//pw_sys_io_rp2040
-build:rp2040 --@pigweed//pw_system:device_handler_backend=@pigweed//targets/rp2040:device_handler
-build:rp2040 --@pigweed//pw_system:extra_platform_libs=//targets/rp2:extra_platform_libs
-build:rp2040 --@pigweed//pw_system:io_backend=@pigweed//pw_system:sys_io_target_io
-build:rp2040 --@pigweed//pw_thread_freertos:config_override=//targets/rp2:thread_config_overrides
-build:rp2040 --@pigweed//pw_thread:id_backend=@pigweed//pw_thread_freertos:id
-build:rp2040 --@pigweed//pw_thread:iteration_backend=@pigweed//pw_thread_freertos:thread_iteration
-build:rp2040 --@pigweed//pw_thread:sleep_backend=@pigweed//pw_thread_freertos:sleep
-build:rp2040 --@pigweed//pw_thread:thread_backend=@pigweed//pw_thread_freertos:thread
-build:rp2040 --@pigweed//pw_thread:test_thread_context_backend=@pigweed//pw_thread_freertos:test_thread_context
-build:rp2040 --@pigweed//pw_unit_test:config_override=//targets/rp2:64k_unit_tests
-build:rp2040 --@pigweed//pw_unit_test:main=//targets/rp2:unit_test_rpc_main
-build:rp2040 --@freertos//:freertos_config=//targets/rp2:freertos_config
-build:rp2040 --@pico-sdk//bazel/config:PICO_STDIO_USB=True
-build:rp2040 --@pico-sdk//bazel/config:PICO_STDIO_UART=True
-build:rp2040 --@pico-sdk//bazel/config:PICO_CLIB=llvm_libc
-build:rp2040 --@pico-sdk//bazel/config:PICO_TOOLCHAIN=clang
-build:rp2040 --@pigweed//pw_toolchain:cortex-m_toolchain_kind=clang
 test:rp2040 --run_under=@pigweed//targets/rp2040/py:unit_test_client
 
 # RP2350 is the same as rp2040 but with a different --platforms setting.
diff --git a/MODULE.bazel b/MODULE.bazel
index 6556783..2593d89 100644
--- a/MODULE.bazel
+++ b/MODULE.bazel
@@ -23,6 +23,7 @@
 bazel_dep(name = "platforms", version = "0.0.10")
 bazel_dep(name = "pw_toolchain")
 bazel_dep(name = "rules_cc")
+bazel_dep(name = "rules_platform", version = "0.1.0")
 bazel_dep(name = "rules_python", version = "0.34.0")
 
 bazel_dep(name = "hedron_compile_commands", dev_dependency = True)
diff --git a/targets/rp2/BUILD.bazel b/targets/rp2/BUILD.bazel
index 3cc2ea9..982595d 100644
--- a/targets/rp2/BUILD.bazel
+++ b/targets/rp2/BUILD.bazel
@@ -11,6 +11,8 @@
 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 # License for the specific language governing permissions and limitations under
 # the License.
+load("@pigweed//pw_build:merge_flags.bzl", "flags_from_dict")
+load("@pigweed//targets/rp2040:transition.bzl", "RP2_SYSTEM_FLAGS")
 
 package(default_visibility = ["//visibility:public"])
 
@@ -26,6 +28,18 @@
         "@pigweed//pw_interrupt_cortex_m:backend",
         "@platforms//os:none",
     ],
+    flags = flags_from_dict(RP2_SYSTEM_FLAGS | {
+        "//system:system": "//targets/rp2:system",
+        "@freertos//:freertos_config": "//targets/rp2:freertos_config",
+        "@pico-sdk//bazel/config:PICO_CLIB": "llvm_libc",
+        "@pico-sdk//bazel/config:PICO_TOOLCHAIN": "clang",
+        "@pigweed//pw_build:default_module_config": "//system:module_config",
+        "@pigweed//pw_system:extra_platform_libs": "//targets/rp2:extra_platform_libs",
+        "@pigweed//pw_system:io_backend": "@pigweed//pw_system:sys_io_target_io",
+        "@pigweed//pw_toolchain:cortex-m_toolchain_kind": "clang",
+        "@pigweed//pw_unit_test:config_override": "//targets/rp2:64k_unit_tests",
+        "@pigweed//pw_unit_test:main": "//targets/rp2:unit_test_rpc_main",
+    }),
 )
 
 platform(
diff --git a/targets/rp2/binary.bzl b/targets/rp2/binary.bzl
index 46c146f..06fe4e9 100644
--- a/targets/rp2/binary.bzl
+++ b/targets/rp2/binary.bzl
@@ -12,92 +12,12 @@
 # License for the specific language governing permissions and limitations under
 # the License.
 
-"""Project-specific bazel transitions for rp2xxx-series chips.
+"""Project-specific bazel transitions for rp2xxx-series chips."""
 
-TODO:  b/301334234 - Use platform-based flags and retire these transitions.
-"""
+load("@rules_platform//platform_data:defs.bzl", "platform_data")
 
-load("@pigweed//pw_build:merge_flags.bzl", "merge_flags_for_transition_impl", "merge_flags_for_transition_outputs")
-load("@pigweed//targets/rp2040:transition.bzl", "RP2_SYSTEM_FLAGS")
+def rp2040_binary(name = "", binary = ""):
+    return platform_data(name = name, target = binary, platform = "//targets/rp2:rp2040")
 
-_COMMON_FLAGS = merge_flags_for_transition_impl(
-    base = RP2_SYSTEM_FLAGS,
-    override = {
-        "//system:system": "//targets/rp2:system",
-        "@freertos//:freertos_config": "//targets/rp2:freertos_config",
-        "@pico-sdk//bazel/config:PICO_CLIB": "llvm_libc",
-        "@pico-sdk//bazel/config:PICO_TOOLCHAIN": "clang",
-        "@pigweed//pw_build:default_module_config": "//system:module_config",
-        "@pigweed//pw_system:extra_platform_libs": "//targets/rp2:extra_platform_libs",
-        "@pigweed//pw_system:io_backend": "@pigweed//pw_system:sys_io_target_io",
-        "@pigweed//pw_toolchain:cortex-m_toolchain_kind": "clang",
-        "@pigweed//pw_unit_test:config_override": "//targets/rp2:64k_unit_tests",
-    },
-)
-
-_RP2040_FLAGS = {
-    "//command_line_option:platforms": "//targets/rp2:rp2040",
-}
-
-_RP2350_FLAGS = {
-    "//command_line_option:platforms": "//targets/rp2:rp2350",
-}
-
-def _rp2_transition(device_specific_flags):
-    def _rp2_transition_impl(settings, attr):
-        # buildifier: disable=unused-variable
-        _ignore = settings, attr
-        return merge_flags_for_transition_impl(
-            base = _COMMON_FLAGS,
-            override = device_specific_flags,
-        )
-
-    return transition(
-        implementation = _rp2_transition_impl,
-        inputs = [],
-        outputs = merge_flags_for_transition_outputs(
-            base = _COMMON_FLAGS,
-            override = device_specific_flags,
-        ),
-    )
-
-def _rp2_binary_impl(ctx):
-    out = ctx.actions.declare_file(ctx.label.name)
-    ctx.actions.symlink(output = out, is_executable = True, target_file = ctx.executable.binary)
-    return [DefaultInfo(files = depset([out]), executable = out)]
-
-rp2040_binary = rule(
-    _rp2_binary_impl,
-    attrs = {
-        "binary": attr.label(
-            doc = "cc_binary to build for the rp2040",
-            cfg = _rp2_transition(_RP2040_FLAGS),
-            executable = True,
-            mandatory = True,
-        ),
-        "_allowlist_function_transition": attr.label(
-            default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
-        ),
-    },
-    doc = "Builds the specified binary for the rp2040 platform",
-    # This target is for rp2040 and can't be run on host.
-    executable = False,
-)
-
-rp2350_binary = rule(
-    _rp2_binary_impl,
-    attrs = {
-        "binary": attr.label(
-            doc = "cc_binary to build for the rp2040",
-            cfg = _rp2_transition(_RP2350_FLAGS),
-            executable = True,
-            mandatory = True,
-        ),
-        "_allowlist_function_transition": attr.label(
-            default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
-        ),
-    },
-    doc = "Builds the specified binary for the rp2350 platform",
-    # This target is for rp2350 and can't be run on host.
-    executable = False,
-)
+def rp2350_binary(name = "", binary = ""):
+    return platform_data(name = name, target = binary, platform = "//targets/rp2:rp2350")