|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# |
| 3 | +# This source code is licensed under the MIT license found in the |
| 4 | +# LICENSE file in the root directory of this source tree. |
| 5 | + |
| 6 | +load("//antlir/antlir2/bzl:types.bzl", "LayerInfo") |
| 7 | +load("//antlir/antlir2/features:defs.bzl", "FeaturePluginPluginKind") |
| 8 | +load("//antlir/buck2/bzl:ensure_single_output.bzl", "ensure_single_output") |
| 9 | +load(":attrs.bzl", "common_attrs", "default_attrs") |
| 10 | +load(":cfg.bzl", "package_cfg") |
| 11 | +load(":macro.bzl", "package_macro") |
| 12 | +load(":stamp_buildinfo.bzl", "stamp_buildinfo_rule") |
| 13 | + |
| 14 | +def _unprivileged_dir_impl_with_layer( |
| 15 | + layer: [Dependency, ProviderCollection], |
| 16 | + *, |
| 17 | + ctx: AnalysisContext) -> list[Provider]: |
| 18 | + output_name = ctx.attrs.out or ctx.label.name |
| 19 | + package = ctx.actions.declare_output(output_name, dir = True) |
| 20 | + |
| 21 | + encoded_path_mapping = ctx.actions.declare_output("encoded_path_mapping.json") |
| 22 | + if not ctx.attrs.base64_encode_filenames: |
| 23 | + ctx.actions.write_json(encoded_path_mapping.as_output(), {}) |
| 24 | + spec = ctx.actions.write_json( |
| 25 | + "spec.json", |
| 26 | + {"unprivileged_dir": { |
| 27 | + "base64_encoded_filenames": encoded_path_mapping.as_output(), |
| 28 | + } if ctx.attrs.base64_encode_filenames else {}}, |
| 29 | + with_inputs = True, |
| 30 | + ) |
| 31 | + ctx.actions.run( |
| 32 | + cmd_args( |
| 33 | + cmd_args("sudo", "--preserve-env=TMPDIR") if not ctx.attrs._rootless else cmd_args(), |
| 34 | + ctx.attrs._antlir2_packager[RunInfo], |
| 35 | + cmd_args(spec, format = "--spec={}"), |
| 36 | + cmd_args(layer[LayerInfo].contents.subvol_symlink, format = "--layer={}"), |
| 37 | + "--dir", |
| 38 | + cmd_args(package.as_output(), format = "--out={}"), |
| 39 | + "--rootless" if ctx.attrs._rootless else cmd_args(), |
| 40 | + ), |
| 41 | + local_only = True, |
| 42 | + category = "antlir2_package", |
| 43 | + identifier = "unprivileged_dir", |
| 44 | + ) |
| 45 | + |
| 46 | + return [DefaultInfo(package, sub_targets = { |
| 47 | + "base64_encoded_path_mapping": [DefaultInfo(encoded_path_mapping)], |
| 48 | + })] |
| 49 | + |
| 50 | +def _unprivileged_dir_impl(ctx: AnalysisContext): |
| 51 | + if ctx.attrs.dot_meta: |
| 52 | + return ctx.actions.anon_target(stamp_buildinfo_rule, { |
| 53 | + "build_appliance": ctx.attrs.build_appliance, |
| 54 | + "flavor": ctx.attrs.flavor, |
| 55 | + "layer": ctx.attrs.layer, |
| 56 | + "name": str(ctx.label.raw_target()), |
| 57 | + "_analyze_feature": ctx.attrs._analyze_feature, |
| 58 | + "_antlir2": ctx.attrs._antlir2, |
| 59 | + "_dot_meta_feature": ctx.attrs._dot_meta_feature, |
| 60 | + "_plugins": ctx.attrs._plugins + (ctx.plugins[FeaturePluginPluginKind] if FeaturePluginPluginKind in ctx.plugins else []), |
| 61 | + "_rootless": ctx.attrs._rootless, |
| 62 | + "_run_container": ctx.attrs._run_container, |
| 63 | + "_target_arch": ctx.attrs._target_arch, |
| 64 | + "_working_format": ctx.attrs._working_format, |
| 65 | + }).promise.map(partial( |
| 66 | + _unprivileged_dir_impl_with_layer, |
| 67 | + ctx = ctx, |
| 68 | + )) |
| 69 | + else: |
| 70 | + return _unprivileged_dir_impl_with_layer( |
| 71 | + layer = ctx.attrs.layer, |
| 72 | + ctx = ctx, |
| 73 | + ) |
| 74 | + |
| 75 | +_unprivileged_dir_attrs = { |
| 76 | + "base64_encode_filenames": attrs.bool( |
| 77 | + default = False, |
| 78 | + doc = "Encode filenames that contain invalid characters (/ or \\) in base64 so they are legal in buck-out", |
| 79 | + ), |
| 80 | + "dot_meta": attrs.bool(default = True), |
| 81 | +} |
| 82 | + |
| 83 | +_unprivileged_dir = rule( |
| 84 | + impl = _unprivileged_dir_impl, |
| 85 | + cfg = package_cfg, |
| 86 | + uses_plugins = [FeaturePluginPluginKind], |
| 87 | + attrs = default_attrs | common_attrs | _unprivileged_dir_attrs, |
| 88 | +) |
| 89 | + |
| 90 | +unprivileged_dir_anon = anon_rule( |
| 91 | + impl = lambda ctx: _unprivileged_dir_impl_with_layer(ctx.attrs.layer, ctx = ctx), |
| 92 | + artifact_promise_mappings = { |
| 93 | + "base64_encoded_path_mapping": lambda x: ensure_single_output(x[DefaultInfo].sub_targets["base64_encoded_path_mapping"]), |
| 94 | + "package": lambda x: ensure_single_output(x), |
| 95 | + }, |
| 96 | + attrs = default_attrs | common_attrs | _unprivileged_dir_attrs, |
| 97 | +) |
| 98 | + |
| 99 | +unprivileged_dir = package_macro(_unprivileged_dir) |
0 commit comments