Skip to content

Commit

Permalink
Refactor target translation own bzl file [4/7]
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Cullen Walsh authored and facebook-github-bot committed Sep 25, 2024
1 parent f02c43c commit 037e667
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 67 deletions.
60 changes: 60 additions & 0 deletions shim/build_defs/lib/oss.bzl
Original file line number Diff line number Diff line change
@@ -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'?")
10 changes: 10 additions & 0 deletions shim/build_defs/lib/test/BUCK
Original file line number Diff line number Diff line change
@@ -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()
15 changes: 15 additions & 0 deletions shim/build_defs/lib/test/oss.bzl
Original file line number Diff line number Diff line change
@@ -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")
72 changes: 5 additions & 67 deletions shim/shims.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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()

0 comments on commit 037e667

Please sign in to comment.