| # Copyright 2026 The Fuchsia Authors. All rights reserved. | |
| # Use of this source code is governed by a BSD-style license that can be | |
| # found in the LICENSE file. | |
| """A macro for defining a Rust static library consumed by C/C++.""" | |
| load( | |
| "@fuchsia_rules_common//build_flags:rust.bzl", | |
| "BUILD_FLAGS_RUST_MACRO_ATTRS", | |
| "wrap_rust_macro_args_with_build_flags", | |
| ) | |
| load("@rules_rust//rust:defs.bzl", "rust_static_library") | |
| load( | |
| "//build/bazel/rules/rust:common.bzl", | |
| "with_fuchsia_rust_link_exec_properties", | |
| "with_fuchsia_rust_stdlib_link_deps", | |
| "with_fuchsia_rust_tags", | |
| "with_fuchsia_rustc_flags", | |
| ) | |
| visibility(["//..."]) | |
| def _rustc_staticlib_impl( | |
| name, | |
| output_name, # buildifier: disable=unused-variable | |
| dynamic_libstd, | |
| lint_config, | |
| disable_clippy, | |
| gn_quiet_clippy, # buildifier: disable=unused-variable | |
| gn_disable_rustdoc, # buildifier: disable=unused-variable | |
| rustc_flags, | |
| build_flags, | |
| validations, | |
| visibility, | |
| **kwargs): | |
| # `output_name` is only consumed by the GN rustc_staticlib() template | |
| # generated by bazel2gn, and is ignored here: rules_rust always names | |
| # staticlibs `lib<crate_name>-<hash>.a`. | |
| if lint_config == None: | |
| lint_config = "//build/config/rust/lints:clippy_warn_production" | |
| kwargs["tags"] = with_fuchsia_rust_tags(kwargs.get("tags"), disable_clippy) | |
| # Validation targets only provide a `_validation` output group and no | |
| # files, so listing them in `compile_data` adds no compile inputs. The | |
| # dependency edge makes Bazel run their validation actions whenever this | |
| # library is built, like GN `validations`. | |
| if validations: | |
| compile_data = kwargs.get("compile_data") | |
| kwargs["compile_data"] = (compile_data if compile_data != None else []) + validations | |
| kwargs["rustc_flags"] = with_fuchsia_rustc_flags(rustc_flags) | |
| library_kwargs = wrap_rust_macro_args_with_build_flags( | |
| kwargs = kwargs, | |
| name = name, | |
| rust_rule_name = "rust_static_library", | |
| build_flags = build_flags, | |
| target_type = "rust_common", | |
| ) | |
| staticlib_link_deps = library_kwargs.get("link_deps") or [] | |
| if dynamic_libstd: | |
| staticlib_link_deps = with_fuchsia_rust_stdlib_link_deps(staticlib_link_deps) | |
| rust_static_library( | |
| name = name, | |
| lint_config = lint_config, | |
| visibility = visibility, | |
| **(library_kwargs | { | |
| "exec_properties": with_fuchsia_rust_link_exec_properties(library_kwargs.get("exec_properties")), | |
| "link_deps": staticlib_link_deps, | |
| }) | |
| ) | |
| rustc_staticlib = macro( | |
| doc = """`rust_static_library` wrapper with Fuchsia-specific features. | |
| Builds a Rust `staticlib` archive that provides `CcInfo`, so C/C++ targets | |
| (`cc_binary`, `fx_cc_binary`, `cc_library`, `fx_cc_library`, `cc_shared_library`, | |
| `idk_cc_shared_library`) can depend on it directly in `deps`. | |
| Applies Fuchsia-specific Rust flags (`crate_features` maps to GN `features`, and | |
| `build_flags` maps to GN `configs +=`). | |
| The default `lint_config` value is `//build/config/rust/lints:clippy_warn_production`. | |
| Also used to sync targets between Bazel and GN via `bazel2gn`, where this maps | |
| to the GN `rustc_staticlib()` template. | |
| """, | |
| implementation = _rustc_staticlib_impl, | |
| inherit_attrs = rust_static_library, | |
| attrs = { | |
| "output_name": attr.string( | |
| doc = """Base name of the library in GN. | |
| Only used by the GN `rustc_staticlib()` template generated by `bazel2gn`, e.g. to | |
| keep output paths stable when migrating an existing GN target. Ignored by Bazel.""", | |
| configurable = False, | |
| ), | |
| "dynamic_libstd": attr.bool( | |
| doc = """Whether the Rust standard library is dynamically linked in GN. | |
| In Bazel, `rust_static_library` always bundles Rust `.rlib` dependencies (including | |
| `libstd`) into the `.a` archive. When `True` (default), `fdio` is automatically added | |
| to `link_deps` on Fuchsia to satisfy `libstd`'s native link dependency; set to `False` | |
| to omit automatic `fdio` linking and emit `dynamic_libstd = false` in GN via `bazel2gn`.""", | |
| default = True, | |
| configurable = False, | |
| ), | |
| "disable_clippy": attr.bool( | |
| doc = "If true, disables clippy lints on this target.", | |
| default = False, | |
| configurable = False, | |
| ), | |
| "gn_quiet_clippy": attr.bool( | |
| doc = "Passed through to GN `rustc_staticlib()` as `quiet_clippy` by `bazel2gn`. Ignored in Bazel.", | |
| default = False, | |
| configurable = False, | |
| ), | |
| "gn_disable_rustdoc": attr.bool( | |
| doc = "Passed through to GN `rustc_staticlib()` as `disable_rustdoc` by `bazel2gn`. Ignored in Bazel.", | |
| default = False, | |
| configurable = False, | |
| ), | |
| "validations": attr.label_list( | |
| doc = """Targets whose validation actions run whenever this library is built. | |
| Equivalent to GN `validations`. The targets must only provide an | |
| `OutputGroupInfo` with a `_validation` output group and no default outputs, | |
| such as `rustc_bindgen_golden()` targets.""", | |
| default = [], | |
| configurable = False, | |
| ), | |
| } | BUILD_FLAGS_RUST_MACRO_ATTRS, | |
| ) |