From 037e667e695f390be258db96af01df9a3d0013c1 Mon Sep 17 00:00:00 2001 From: Cullen Walsh Date: Tue, 24 Sep 2024 20:59:25 -0700 Subject: [PATCH] Refactor target translation own bzl file [4/7] Summary: There are some cases where the OSS target paths do not match the internal target paths, and we need to translate the targets in OSS projects in order to build successfully. This diff refactors all the logic related to this translation into it's own file, so it is easier to reason about and doesn't get mixed up with the shim implementations. Reviewed By: stepancheg Differential Revision: D63295974 fbshipit-source-id: 40274c190367566b562af9bca4e0dbaea2822b3e --- shim/build_defs/lib/oss.bzl | 60 ++++++++++++++++++++++++++ shim/build_defs/lib/test/BUCK | 10 +++++ shim/build_defs/lib/test/oss.bzl | 15 +++++++ shim/shims.bzl | 72 +++----------------------------- 4 files changed, 90 insertions(+), 67 deletions(-) create mode 100644 shim/build_defs/lib/oss.bzl create mode 100644 shim/build_defs/lib/test/BUCK create mode 100644 shim/build_defs/lib/test/oss.bzl diff --git a/shim/build_defs/lib/oss.bzl b/shim/build_defs/lib/oss.bzl new file mode 100644 index 000000000..7ae858b13 --- /dev/null +++ b/shim/build_defs/lib/oss.bzl @@ -0,0 +1,60 @@ +# Copyright (c) Meta Platforms, Inc. and affiliates. +# +# This source code is licensed under both the MIT license found in the +# LICENSE-MIT file in the root directory of this source tree and the Apache +# License, Version 2.0 found in the LICENSE-APACHE file in the root directory +# of this source tree. + +def translate_target(target: str) -> str: + def remove_version(target: str) -> str: + # When upgrading libraries we either suffix them as `-old` or with a version, e.g. `-1-08` + # Strip those so we grab the right one in open source. + if target.endswith(":md-5"): # md-5 is the one exception + return target + xs = target.split("-") + for i in reversed(range(len(xs))): + s = xs[i] + if s == "old" or s.isdigit(): + xs.pop(i) + else: + break + return "-".join(xs) + + if target == "//common/rust/shed/fbinit:fbinit": + return "fbsource//third-party/rust:fbinit" + elif target == "//common/rust/shed/sorted_vector_map:sorted_vector_map": + return "fbsource//third-party/rust:sorted_vector_map" + elif target == "//watchman/rust/watchman_client:watchman_client": + return "fbsource//third-party/rust:watchman_client" + elif target.startswith("fbsource//third-party/rust:"): + return remove_version(target) + elif target.startswith(":"): + return target + elif target.startswith("//buck2/"): + return "root//" + target.removeprefix("//buck2/") + elif target.startswith("fbcode//common/ocaml/interop/"): + return "root//" + target.removeprefix("fbcode//common/ocaml/interop/") + elif target.startswith("fbcode//third-party-buck/platform010/build/supercaml"): + return "shim//third-party/ocaml" + target.removeprefix("fbcode//third-party-buck/platform010/build/supercaml") + elif target.startswith("fbcode//third-party-buck/platform010/build"): + return "shim//third-party" + target.removeprefix("fbcode//third-party-buck/platform010/build") + elif target.startswith("fbsource//third-party"): + return "shim//third-party" + target.removeprefix("fbsource//third-party") + elif target.startswith("third-party//"): + return "shim//third-party/" + target.removeprefix("third-party//") + elif target.startswith("//folly"): + oss_depends_on_folly = read_config("oss_depends_on", "folly", False) + if oss_depends_on_folly: + return "root//folly/" + target.removeprefix("//") + return "root//" + target.removeprefix("//") + elif target.startswith("root//folly"): + return target + elif target.startswith("//fizz"): + return "root//" + target.removeprefix("//") + elif target.startswith("shim//"): + return target + elif target.startswith("prelude//"): + return target + else: + fail("Dependency is unaccounted for `{}`.\n".format(target) + + "Did you forget 'oss-disable'?") diff --git a/shim/build_defs/lib/test/BUCK b/shim/build_defs/lib/test/BUCK new file mode 100644 index 000000000..a2f6ef3a1 --- /dev/null +++ b/shim/build_defs/lib/test/BUCK @@ -0,0 +1,10 @@ +# Copyright (c) Meta Platforms, Inc. and affiliates. +# +# This source code is licensed under both the MIT license found in the +# LICENSE-MIT file in the root directory of this source tree and the Apache +# License, Version 2.0 found in the LICENSE-APACHE file in the root directory +# of this source tree. + +load("@shim//build_defs/lib/test:oss.bzl", "test_translate_target") + +test_translate_target() diff --git a/shim/build_defs/lib/test/oss.bzl b/shim/build_defs/lib/test/oss.bzl new file mode 100644 index 000000000..64ceededa --- /dev/null +++ b/shim/build_defs/lib/test/oss.bzl @@ -0,0 +1,15 @@ +# Copyright (c) Meta Platforms, Inc. and affiliates. +# +# This source code is licensed under both the MIT license found in the +# LICENSE-MIT file in the root directory of this source tree and the Apache +# License, Version 2.0 found in the LICENSE-APACHE file in the root directory +# of this source tree. + +load("@shim//build_defs/lib:oss.bzl", "translate_target") + +def _assert_eq(x, y): + if x != y: + fail("Expected {} == {}".format(x, y)) + +def test_translate_target(): + _assert_eq(translate_target("fbsource//third-party/rust:derive_more-1"), "fbsource//third-party/rust:derive_more") diff --git a/shim/shims.bzl b/shim/shims.bzl index 9c9cefe8b..328b31d17 100644 --- a/shim/shims.bzl +++ b/shim/shims.bzl @@ -12,6 +12,7 @@ load("@prelude//utils:selects.bzl", "selects") load("@prelude//utils:type_defs.bzl", "is_dict", "is_list", "is_select", "is_tuple") load("@shim//build_defs:auto_headers.bzl", "AutoHeaders", "get_auto_headers") +load("@shim//build_defs/lib:oss.bzl", "translate_target") prelude = native @@ -393,76 +394,22 @@ def _fix_dict_deps(xss): def _fix_mapped_srcs(xs: dict[str, str]): # For reasons, this is source -> file path, which is the opposite of what # it should be. - return {_fix_dep(k): v for (k, v) in xs.items()} + return {translate_target(k): v for (k, v) in xs.items()} def _fix_deps(xs): if is_select(xs): return select_map(xs, lambda child_targets: _fix_deps(child_targets)) - return map(_fix_dep, xs) + return map(translate_target, xs) def _fix_resources(resources): if is_list(resources): - return [_fix_dep(r) for r in resources] + return [translate_target(r) for r in resources] if is_dict(resources): - return {k: _fix_dep(v) for k, v in resources.items()} + return {k: translate_target(v) for k, v in resources.items()} fail("Unexpected type {} for resources".format(type(resources))) -def _fix_dep(x: str) -> str: - def remove_version(x: str) -> str: - # When upgrading libraries we either suffix them as `-old` or with a version, e.g. `-1-08` - # Strip those so we grab the right one in open source. - if x.endswith(":md-5"): # md-5 is the one exception - return x - xs = x.split("-") - for i in reversed(range(len(xs))): - s = xs[i] - if s == "old" or s.isdigit(): - xs.pop(i) - else: - break - return "-".join(xs) - - if x == "//common/rust/shed/fbinit:fbinit": - return "fbsource//third-party/rust:fbinit" - elif x == "//common/rust/shed/sorted_vector_map:sorted_vector_map": - return "fbsource//third-party/rust:sorted_vector_map" - elif x == "//watchman/rust/watchman_client:watchman_client": - return "fbsource//third-party/rust:watchman_client" - elif x.startswith("fbsource//third-party/rust:"): - return remove_version(x) - elif x.startswith(":"): - return x - elif x.startswith("//buck2/"): - return "root//" + x.removeprefix("//buck2/") - elif x.startswith("fbcode//common/ocaml/interop/"): - return "root//" + x.removeprefix("fbcode//common/ocaml/interop/") - elif x.startswith("fbcode//third-party-buck/platform010/build/supercaml"): - return "shim//third-party/ocaml" + x.removeprefix("fbcode//third-party-buck/platform010/build/supercaml") - elif x.startswith("fbcode//third-party-buck/platform010/build"): - return "shim//third-party" + x.removeprefix("fbcode//third-party-buck/platform010/build") - elif x.startswith("fbsource//third-party"): - return "shim//third-party" + x.removeprefix("fbsource//third-party") - elif x.startswith("third-party//"): - return "shim//third-party/" + x.removeprefix("third-party//") - elif x.startswith("//folly"): - oss_depends_on_folly = read_config("oss_depends_on", "folly", False) - if oss_depends_on_folly: - return "root//folly/" + x.removeprefix("//") - return "root//" + x.removeprefix("//") - elif x.startswith("root//folly"): - return x - elif x.startswith("//fizz"): - return "root//" + x.removeprefix("//") - elif x.startswith("shim//"): - return x - elif x.startswith("prelude//"): - return x - else: - fail("Dependency is unaccounted for `{}`.\n".format(x) + - "Did you forget 'oss-disable'?") - def _fix_dep_in_string(x: str) -> str: """Replace internal labels in string values such as env-vars.""" return (x @@ -479,12 +426,3 @@ def external_dep_to_target(t): def external_deps_to_targets(ts): return [external_dep_to_target(t) for t in ts] - -def _assert_eq(x, y): - if x != y: - fail("Expected {} == {}".format(x, y)) - -def _test(): - _assert_eq(_fix_dep("fbsource//third-party/rust:derive_more-1"), "fbsource//third-party/rust:derive_more") - -_test()