Skip to content

Commit

Permalink
[antlir2][migration] move configuration to PACKAGE file
Browse files Browse the repository at this point in the history
Summary: This is available to use now - do this instead of the hacky package comparison.

Test Plan: waitforsandcastle

Reviewed By: pallotron

Differential Revision: D50192650

fbshipit-source-id: 3e715404c4d3b337efe35f0e53699af5048863a0
  • Loading branch information
vmagro authored and facebook-github-bot committed Oct 12, 2023
1 parent f1892df commit ef725e3
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 85 deletions.
26 changes: 26 additions & 0 deletions antlir/bzl/antlir2_migration.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# 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.

load("//antlir/bzl:types.bzl", "types")

mode_t = enum("none", "shadow")

def _configure_package(*, mode: str | mode_t):
if types.is_string(mode):
mode = mode_t(mode)
write_package_value(
"antlir2.migration",
mode,
overwrite = True,
)

def _get_mode() -> mode_t:
return read_package_value("antlir2.migration") or mode_t("none")

antlir2_migration = struct(
configure_package = _configure_package,
get_mode = _get_mode,
mode_t = mode_t,
)
87 changes: 12 additions & 75 deletions antlir/bzl/antlir2_shim.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,14 @@
# LICENSE file in the root directory of this source tree.

load("//antlir/bzl:build_defs.bzl", "export_file")
load(":antlir2_migration.bzl?v2_only", "antlir2_migration")
load(":build_defs.bzl", "is_buck2", "python_unittest")
load(":flavor.shape.bzl", "flavor_t")
load(":target_helpers.bzl", "antlir_dep")
load(":types.bzl", "types")

types.lint_noop(flavor_t)

_DEFAULT_ENABLED_PACKAGES = [
"antlir/bzl/linux",
"antlir/linux",
"bot_generated/antlir/fbpkg/db",
"kernel/kernels",
"metalos",
"netos",
"os_foundation",
"sandcastle/images",
"tupperware/image/features",
"tupperware/image/base",
"tupperware/image/rpmbuild",
"tupperware/image/webfoundation",
"tupperware/image/multifeed",
]

_DEFAULT_DISABLED_PACKAGES = [
# fbcode/antlir just has way too many weird and broken images, don't even
# try to make antlir2 images for them when the goal is to delete it all soon
# enough anyway
"antlir",
"sandcastle/images/runtime",
"sandcastle/images/worker",
"tupperware/cm/antlir/tests",
"tupperware/cm/tests",
"tupperware/image/slimos",
]

def _antlir2_setting_buck1(x):
return x

Expand All @@ -47,7 +20,7 @@ antlir2_setting = native.enum(
"yes", # enable antlir2 shadow
"no", # disable antlir2 without a recorded reason
"chef", # disable antlir2 because chef is natively supported
"debuginfo", # antlir2 does not yet support this TODO(T153698233)
"debuginfo", # antlir2 natively supports this
"extract", # native antlir2 feature
"rpmbuild", # antlir2 does not allow rpm installation during a genrule
# user wants to explicitly alias a built layer instead of downloading it from fbpkg
Expand All @@ -56,50 +29,19 @@ antlir2_setting = native.enum(
"test",
) if is_buck2() else _antlir2_setting_buck1

def _antlir2_or_default(antlir2: antlir2_setting | None, default: bool) -> bool:
if antlir2 != None:
return antlir2 == antlir2_setting("yes")
else:
return default

_FLAVOR_T = types.optional(types.union(
types.str,
types.shape(flavor_t),
))
def _should_shadow(antlir2: str | bool | None) -> bool:
if antlir2 == None and is_buck2():
package_mode = antlir2_migration.get_mode()
return package_mode == antlir2_migration.mode_t("shadow")

types.lint_noop(_FLAVOR_T)
# otherwise, PACKAGE value takes a back-seat to the explicit 'antlir2' flag

def _should_make_parallel(
antlir2: str | bool | None,
*,
flavor: _FLAVOR_T = None,
disabled_packages: list[str] = _DEFAULT_DISABLED_PACKAGES,
enabled_packages: list[str] = _DEFAULT_ENABLED_PACKAGES) -> bool:
if types.is_bool(antlir2):
antlir2 = antlir2_setting("yes" if antlir2 else "no")
else:
antlir2 = antlir2_setting(antlir2) if antlir2 else None

# TODO(vmagro): make True the default
result = False

# Find the "closest" match so that _DEFAULT_{ENABLED,DISABLED}_PACKAGES can
# peacefully co-exist
distance = 999999999
for pkg in enabled_packages:
if native.package_name() == pkg or native.package_name().startswith(pkg + "/"):
this_distance = len(native.package_name()[len(pkg):].split("/"))
if this_distance < distance:
result = _antlir2_or_default(antlir2, True)
distance = this_distance
for pkg in disabled_packages:
if native.package_name() == pkg or native.package_name().startswith(pkg + "/"):
this_distance = len(native.package_name()[len(pkg):].split("/"))
if this_distance < distance:
result = _antlir2_or_default(antlir2, False)
distance = this_distance

return result
return antlir2 == antlir2_setting("yes")

def _fake_buck1_layer(name):
# export a target of the same name to make td happy
Expand All @@ -125,18 +67,13 @@ def _fake_buck1_target(name):
antlir_rule = "user-internal",
)

antlir2_enabled_packages = [
"metalos/imaging_initrd",
"metalos/initrd",
]

antlir2_shim = struct(
fake_buck1_layer = _fake_buck1_layer,
fake_buck1_feature = _fake_buck1_target,
fake_buck1_target = _fake_buck1_target,
fake_buck1_test = _fake_buck1_test,
should_make_parallel_feature = _should_make_parallel,
should_make_parallel_layer = _should_make_parallel,
should_make_parallel_test = _should_make_parallel,
should_make_parallel_package = partial(_should_make_parallel, enabled_packages = antlir2_enabled_packages),
should_shadow_feature = _should_shadow,
should_shadow_layer = _should_shadow,
should_shadow_test = _should_shadow,
should_shadow_package = _should_shadow,
)
2 changes: 1 addition & 1 deletion antlir/bzl/genrule/extractor/extract.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ cp "${{source_layer_path}}{output_dir}/feature.json" "$OUT"
deps = ["//antlir/bzl/genrule/extractor:extract"],
)

if antlir2_shim.should_make_parallel_feature(antlir2):
if antlir2_shim.should_shadow_feature(antlir2):
if is_buck2():
antlir2_feature.new(
name = name,
Expand Down
2 changes: 1 addition & 1 deletion antlir/bzl/image/feature/new.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ def feature_new(
# If we're on buck2, instantiate an antlir2 feature rule. This does not
# conflict with the feature above, since antlir1 adds a "private" suffix to
# all feature targets
if antlir2_shim.should_make_parallel_layer(antlir2):
if antlir2_shim.should_shadow_layer(antlir2):
if is_buck2():
antlir2_feature.new(
name,
Expand Down
2 changes: 1 addition & 1 deletion antlir/bzl/image/package/new.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def package_new(
if format in _SENDSTREAM_FORMATS and not subvol_name:
subvol_name = "volume"

if antlir2_shim.should_make_parallel_package(antlir2 = antlir2):
if antlir2_shim.should_shadow_package(antlir2 = antlir2):
if is_buck2():
antlir2_opts = shape.as_dict_shallow(loopback_opts) if loopback_opts else None
opts_kwargs = {
Expand Down
2 changes: 1 addition & 1 deletion antlir/bzl/image_cpp_unittest.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def image_cpp_unittest(
**wrapper_props.outer_test_kwargs
)

if antlir2_shim.should_make_parallel_test(antlir2):
if antlir2_shim.should_shadow_test(antlir2):
if is_buck2():
antlir2_image_cpp_test(
name = name + ".antlir2",
Expand Down
2 changes: 1 addition & 1 deletion antlir/bzl/image_genrule_layer.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ Optional arguments:
- See the `_image_layer_impl` signature (in `image_layer_utils.bzl`)
for supported, but less commonly used, kwargs.
"""
if antlir2_shim.should_make_parallel_layer(image_layer_kwargs.pop("antlir2", None), flavor = flavor):
if antlir2_shim.should_shadow_layer(image_layer_kwargs.pop("antlir2", None)):
if is_buck2():
antlir2_image.layer(
name = name + ".antlir2",
Expand Down
2 changes: 1 addition & 1 deletion antlir/bzl/image_layer.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def image_layer(
[docs](/docs/tutorials/helper-buck-targets#imagelayer) for the list of
possible helpers, their respective behaviours, and how to invoke them.
"""
if antlir2_shim.should_make_parallel_layer(image_layer_kwargs.pop("antlir2", None), flavor = flavor):
if antlir2_shim.should_shadow_layer(image_layer_kwargs.pop("antlir2", None)):
if is_buck2():
if not antlir2_allow_ignored_flavor_config_override and flavor_config_override:
fail("antlir2 does not support flavor_config_override: {}".format(flavor_config_override))
Expand Down
2 changes: 1 addition & 1 deletion antlir/bzl/image_layer_alias.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def image_layer_alias(name, layer, runtime = None, visibility = None, antlir2 =

add_runtime_targets(name, runtime)

if antlir2_shim.should_make_parallel_layer(antlir2):
if antlir2_shim.should_shadow_layer(antlir2):
if is_buck2():
alias(
name = name + ".antlir2",
Expand Down
2 changes: 1 addition & 1 deletion antlir/bzl/image_layer_from_package.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def image_layer_from_package_helper(
antlir2_src):
flavor = flavor_to_struct(flavor)
target = normalize_target(":" + name)
antlir2 = antlir2_shim.should_make_parallel_layer(image_layer_kwargs.pop("antlir2", None), flavor = flavor)
antlir2 = antlir2_shim.should_shadow_layer(image_layer_kwargs.pop("antlir2", None))

# Do argument validation
for bad_kwarg in ["parent_layer", "features"]:
Expand Down
2 changes: 1 addition & 1 deletion antlir/bzl/image_python_unittest.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def image_python_unittest(
**wrapper_props.outer_test_kwargs
)

if antlir2_shim.should_make_parallel_test(antlir2):
if antlir2_shim.should_shadow_test(antlir2):
if is_buck2():
antlir2_image_python_test(
name = name + ".antlir2",
Expand Down
2 changes: 1 addition & 1 deletion antlir/bzl/image_rust_unittest.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def image_rust_unittest(
**wrapper_props.outer_test_kwargs
)

if antlir2_shim.should_make_parallel_test(antlir2):
if antlir2_shim.should_shadow_test(antlir2):
if is_buck2():
antlir2_image_rust_test(
name = name + ".antlir2",
Expand Down

0 comments on commit ef725e3

Please sign in to comment.