Skip to content

Commit

Permalink
[antlir2][buck] define a transition that completely strips all target…
Browse files Browse the repository at this point in the history
… platform configuration

Summary:
Similar to D64136720, but take it even further and strip *all* target platform configuration.

This is useful for rules that we know will always be 100% identical (eg `rpm`
snapshot targets), but is dangerous for almost any other use case.

Test Plan:
```
❯ buck2 test fbcode//antlir/antlir2/cfg/strip_configuration/...
Buck UI: https://www.internalfb.com/buck2/0b9cb2fa-90b5-431f-b467-896aab5ed8d1
Test UI: https://www.internalfb.com/intern/testinfra/testrun/2533275057090435
Tests finished: Pass 2. Fail 0. Fatal 0. Skip 0. Build failure 0
```

Reviewed By: doon

Differential Revision: D68527501

fbshipit-source-id: 9e69f7f1508765c3bfb1875b316950013ff119b4
  • Loading branch information
vmagro authored and facebook-github-bot committed Jan 23, 2025
1 parent 05176e0 commit 1c5b7c1
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 68 deletions.
51 changes: 0 additions & 51 deletions antlir/antlir2/cfg/strip_antlir_config/strip_antlir_config.bzl

This file was deleted.

52 changes: 52 additions & 0 deletions antlir/antlir2/cfg/strip_configuration/strip_configuration.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

# WARNING: THAR BE DRAGONS
# DO NOT USE THIS UNLESS YOU *REALLY* KNOW WHAT YOU ARE DOING.
#
# Define a transition that strips *ALL* platform configuration, which can be
# used to reduce some duplicate actions where we know that certain rules will
# never use `select`-ed attrs based on the target platform (exec platform
# configuration is still preserved).
#
# This works because buck2 caches artifacts based on the hash of the *entire*
# target platform configuration and does not know what parts are actually used.
#
# This is useful almost exclusively for antlir2 rules that exist very low in the
# dependency chain where leaf rules will have often applied their own
# configuration that we know will be irrelevant - prime example is the `rpm`
# snapshot rules - the output of that rule is always 100% identical no matter
# what the platform configuration may be, so that work should be shared.
def _strip_configuration_transition_impl(
platform: PlatformInfo, # @unused
refs: struct) -> PlatformInfo: # @unused
return PlatformInfo(
label = "<stripped>",
configuration = ConfigurationInfo(constraints = {}, values = {}),
)

_strip_configuration_transition = transition(
impl = _strip_configuration_transition_impl,
refs = {},
)

def _strip_configuration_impl(ctx: AnalysisContext) -> list[Provider]:
if ctx.label.package not in [
"antlir/antlir2/cfg/strip_configuration/tests",
# @oss-disable
]:
fail("""
target is in {} which is not an allowed package.
You MUST talk to twimage to get an exception to this rule
""".format(ctx.label.package))
return ctx.attrs.actual.providers

strip_configuration_alias = rule(
impl = _strip_configuration_impl,
attrs = {
"actual": attrs.transition_dep(cfg = _strip_configuration_transition),
"labels": attrs.list(attrs.string(), default = []),
},
)
Original file line number Diff line number Diff line change
@@ -1,37 +1,38 @@
load("//antlir/antlir2/bzl/feature:defs.bzl", "feature")
load("//antlir/antlir2/bzl/image:defs.bzl", "image")
load("//antlir/antlir2/cfg/strip_antlir_config:strip_antlir_config.bzl", "strip_antlir_config")
load("//antlir/antlir2/cfg/strip_configuration:strip_configuration.bzl", "strip_configuration_alias")
load("//antlir/antlir2/testing:image_test.bzl", "image_rust_test")

oncall("antlir")

prelude = native

prelude.write_file(
name = "os-name",
out = "os-name",
name = "cpu-arch",
out = "cpu-arch",
content = select({
"//antlir/antlir2/os:centos9": ["centos9"],
"DEFAULT": ["no-configuration"],
"ovr_config//cpu:arm64": ["aarch64"],
"ovr_config//cpu:x86_64": ["x86_64"],
}),
)

strip_antlir_config(
name = "os-name.unconfigured",
actual = ":os-name",
strip_configuration_alias(
name = "cpu-arch.unconfigured",
actual = ":cpu-arch",
)

image.layer(
name = "layer",
features = [
feature.rpms_install(rpms = ["basesystem"]),
feature.install(
src = ":os-name",
dst = "/os-name",
src = ":cpu-arch",
dst = "/cpu-arch",
),
feature.install(
src = ":os-name.unconfigured",
dst = "/os-name.unconfigured",
src = ":cpu-arch.unconfigured",
dst = "/cpu-arch.unconfigured",
),
],
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@
*/

#[test]
fn test_strip_antlir_config() {
fn test_strip_configuration() {
assert_eq!(
"centos9",
std::fs::read_to_string("/os-name")
.expect("failed to read /os-name")
std::env::consts::ARCH,
std::fs::read_to_string("/cpu-arch")
.expect("failed to read /cpu-arch")
.trim()
);
assert_eq!(
"no-configuration",
std::fs::read_to_string("/os-name.unconfigured")
.expect("failed to read /os-name.unconfigured")
std::fs::read_to_string("/cpu-arch.unconfigured")
.expect("failed to read /cpu-arch.unconfigured")
.trim()
)
}

0 comments on commit 1c5b7c1

Please sign in to comment.