Skip to content

Commit

Permalink
Make modifier buck1-proof
Browse files Browse the repository at this point in the history
Summary: Unfortunately, buck1 can read PACKAGE files but cannot support most of the things we do in them. To make modifiers not break buck1, add support for buck1 no-ops in `set_cfg_modifiers` and `modifier_select`.

Reviewed By: stepancheg

Differential Revision: D51323010

fbshipit-source-id: cbf427fba596b7fb5a2bbce99aa2ad49eabce7ac
  • Loading branch information
Scott Cao authored and facebook-github-bot committed Nov 16, 2023
1 parent af486d4 commit 902b6a9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
8 changes: 6 additions & 2 deletions cfg/experimental/modifier_select.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
# License, Version 2.0 found in the LICENSE-APACHE file in the root directory
# of this source tree.

load(":common.bzl", "verify_normalized_modifier", "verify_normalized_target")
load("@fbsource//tools/build_defs/buck2:is_buck2.bzl", "is_buck2")
load(":common.bzl?v2_only", "verify_normalized_modifier", "verify_normalized_target")
load(
":types.bzl",
":types.bzl?v2_only",
"Modifier", # @unused Used in type annotation
"ModifierSelect",
)
Expand Down Expand Up @@ -64,6 +65,9 @@ def modifier_select(
```
"""

if not is_buck2():
return {}

for key, sub_modifier in selector.items():
if key != "DEFAULT":
verify_normalized_target(key)
Expand Down
21 changes: 18 additions & 3 deletions cfg/experimental/set_cfg_modifiers.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
# License, Version 2.0 found in the LICENSE-APACHE file in the root directory
# of this source tree.

load(":common.bzl", "get_tagged_modifiers", "tagged_modifiers_to_json")
load(":set_cfg_constructor.bzl", "MODIFIER_METADATA_KEY")
load(":types.bzl", "Modifier", "ModifierPackageLocation")
load("@fbsource//tools/build_defs/buck2:is_buck2.bzl", "is_buck2")
load(":common.bzl?v2_only", "get_tagged_modifiers", "tagged_modifiers_to_json")
load(":set_cfg_constructor.bzl?v2_only", "MODIFIER_METADATA_KEY")
load(":types.bzl?v2_only", "Modifier", "ModifierPackageLocation")

def set_cfg_modifiers(modifiers: list[Modifier]):
"""
Expand All @@ -23,6 +24,12 @@ def set_cfg_modifiers(modifiers: list[Modifier]):
For example, to change the OS to linux in fbsource, this can be specified as "ovr_config//os/constraints:linux".
TODO(scottcao): Add support for modifier select types.
"""
if not is_buck2():
return

# Make this buck1-proof
write_package_value = getattr(native, "write_package_value", None)
read_parent_package_value = getattr(native, "read_parent_package_value", None)

merged_modifier_jsons = read_parent_package_value(MODIFIER_METADATA_KEY)

Expand All @@ -41,9 +48,17 @@ def set_cfg_modifiers(modifiers: list[Modifier]):
overwrite = True,
)

_BUCK1_COMPAT_STR = "Internal error: Modifiers are for buck2 only"

def _get_package_path() -> str:
"""
Returns the cell-relative path of the current PACKAGE file.
Ex. `foo//bar/PACKAGE`
"""
if not is_buck2():
return _BUCK1_COMPAT_STR

# Make this buck1-proof
get_cell_name = getattr(native, "get_cell_name", None)
get_base_path = getattr(native, "get_base_path", None)
return "{}//{}/PACKAGE".format(get_cell_name(), get_base_path())

0 comments on commit 902b6a9

Please sign in to comment.