From 0fddd9c495cf8e7927640ca080d84b515fe12c2e Mon Sep 17 00:00:00 2001 From: Thomas Van Lenten Date: Wed, 18 Sep 2024 08:26:05 -0700 Subject: [PATCH] Remove `ctx` parameter from `apple_support.{run,run_shell}` PiperOrigin-RevId: 676001180 --- lib/apple_support.bzl | 86 ++++--------------------------------- test/apple_support_test.bzl | 53 ----------------------- 2 files changed, 9 insertions(+), 130 deletions(-) diff --git a/lib/apple_support.bzl b/lib/apple_support.bzl index 155378b..9765ead 100644 --- a/lib/apple_support.bzl +++ b/lib/apple_support.bzl @@ -129,16 +129,6 @@ done "$TOOLNAME" "${ARGS[@]}" """ -def _validate_ctx_xor_platform_requirements(*, ctx, actions, apple_fragment, xcode_config): - """Raises an error if there is overlap in platform requirements or if they are insufficent.""" - - if ctx != None and any([actions, xcode_config, apple_fragment]): - fail("Can't specific ctx along with actions, xcode_config, apple_fragment.") - if ctx == None and not all([actions, xcode_config, apple_fragment]): - fail("Must specify all of actions, xcode_config, and apple_fragment.") - if ctx != None: - _validate_ctx_attribute_present(ctx, "_xcode_config") - def _platform_frameworks_path_placeholder(*, apple_fragment): """Returns the platform's frameworks directory, anchored to the Xcode path placeholder. @@ -215,29 +205,6 @@ def _kwargs_for_apple_platform( processed_args["execution_requirements"] = merged_execution_requirements return processed_args -def _validate_ctx_attribute_present(ctx, attribute_name): - """Validates that the given attribute is present for the rule, failing otherwise.""" - if not hasattr(ctx.attr, attribute_name): - fail("\n".join([ - "", - "ERROR: This rule requires the '{}' attribute to be present. ".format(attribute_name), - "To add this attribute, modify your rule definition like this:", - "", - "load(\"@bazel_skylib//lib:dicts.bzl\", \"dicts\")", - "load(", - " \"@build_bazel_apple_support//lib:apple_support.bzl\",", - " \"apple_support\",", - ")", - "", - "your_rule_name = rule(", - " attrs = dicts.add(apple_support.action_required_attrs(), {", - " # other attributes", - " }),", - " # other rule arguments", - ")", - "", - ])) - def _action_required_attrs(): """Returns a dictionary with required attributes for registering actions on Apple platforms. @@ -313,12 +280,11 @@ def _platform_constraint_attrs(): } def _run( - ctx = None, - xcode_path_resolve_level = _XCODE_PATH_RESOLVE_LEVEL.none, *, - actions = None, - xcode_config = None, - apple_fragment = None, + actions, + xcode_config, + apple_fragment, + xcode_path_resolve_level = _XCODE_PATH_RESOLVE_LEVEL.none, **kwargs): """Registers an action to run on an Apple machine. @@ -358,30 +324,13 @@ def _run( path argument beginning with `@`) will be replaced. Args: - ctx: The context of the rule registering the action. Deprecated. - xcode_path_resolve_level: The level of Xcode path replacement required for the action. - actions: The actions provider from ctx.actions. Required if ctx is not given. + actions: The actions provider from ctx.actions. xcode_config: The xcode_config as found in the current rule or aspect's context. Typically from `ctx.attr._xcode_config[apple_common.XcodeVersionConfig]`. - Required if ctx is not given. apple_fragment: A reference to the apple fragment. Typically from `ctx.fragments.apple`. - Required if ctx is not given. + xcode_path_resolve_level: The level of Xcode path replacement required for the action. **kwargs: See `ctx.actions.run` for the rest of the available arguments. """ - _validate_ctx_xor_platform_requirements( - ctx = ctx, - actions = actions, - apple_fragment = apple_fragment, - xcode_config = xcode_config, - ) - - if not actions: - actions = ctx.actions - if not xcode_config: - xcode_config = ctx.attr._xcode_config[apple_common.XcodeVersionConfig] - if not apple_fragment: - apple_fragment = ctx.fragments.apple - if xcode_path_resolve_level == _XCODE_PATH_RESOLVE_LEVEL.none: actions.run(**_kwargs_for_apple_platform( xcode_config = xcode_config, @@ -459,11 +408,10 @@ def _run( ) def _run_shell( - ctx = None, *, - actions = None, - xcode_config = None, - apple_fragment = None, + actions, + xcode_config, + apple_fragment, **kwargs): """Registers a shell action to run on an Apple machine. @@ -481,28 +429,12 @@ def _run_shell( please use `run` instead. Args: - ctx: The context of the rule registering the action. Deprecated. actions: The actions provider from ctx.actions. xcode_config: The xcode_config as found in the current rule or aspect's context. Typically from `ctx.attr._xcode_config[apple_common.XcodeVersionConfig]`. - Required if ctx is not given. apple_fragment: A reference to the apple fragment. Typically from `ctx.fragments.apple`. - Required if ctx is not given. **kwargs: See `ctx.actions.run_shell` for the rest of the available arguments. """ - _validate_ctx_xor_platform_requirements( - ctx = ctx, - actions = actions, - apple_fragment = apple_fragment, - xcode_config = xcode_config, - ) - - if not actions: - actions = ctx.actions - if not xcode_config: - xcode_config = ctx.attr._xcode_config[apple_common.XcodeVersionConfig] - if not apple_fragment: - apple_fragment = ctx.fragments.apple actions.run_shell(**_kwargs_for_apple_platform( xcode_config = xcode_config, diff --git a/test/apple_support_test.bzl b/test/apple_support_test.bzl index 22e45d7..684d3aa 100644 --- a/test/apple_support_test.bzl +++ b/test/apple_support_test.bzl @@ -115,18 +115,6 @@ def _apple_support_test_impl(ctx): run_shell_output = ctx.actions.declare_file( "{}_run_shell_output".format(ctx.label.name), ) - run_output_ctx = ctx.actions.declare_file( - "{}_run_output_ctx".format(ctx.label.name), - ) - run_output_xcode_path_in_args_ctx = ctx.actions.declare_file( - "{}_run_output_xcode_path_in_args_ctx".format(ctx.label.name), - ) - run_output_xcode_path_in_file_ctx = ctx.actions.declare_file( - "{}_run_output_xcode_path_in_file_ctx".format(ctx.label.name), - ) - run_shell_output_ctx = ctx.actions.declare_file( - "{}_run_shell_output_ctx".format(ctx.label.name), - ) test_tool = ctx.actions.declare_file("{}_test_tool".format(ctx.label.name)) ctx.actions.write(test_tool, _TEST_TOOL_CONTENTS, is_executable = True) @@ -141,12 +129,6 @@ def _apple_support_test_impl(ctx): executable = test_tool, arguments = [run_output.path], ) - apple_support.run( - ctx, - outputs = [run_output_ctx], - executable = test_tool, - arguments = [run_output_ctx.path], - ) platform_frameworks = apple_support.path_placeholders.platform_frameworks( apple_fragment = ctx.fragments.apple, @@ -166,18 +148,6 @@ def _apple_support_test_impl(ctx): ], xcode_path_resolve_level = apple_support.xcode_path_resolve_level.args, ) - apple_support.run( - ctx, - outputs = [run_output_xcode_path_in_args_ctx], - executable = test_tool, - arguments = [ - run_output_xcode_path_in_args_ctx.path, - "XCODE_PATH_ARG={}".format(apple_support.path_placeholders.xcode()), - "FRAMEWORKS_PATH_ARG={}".format(platform_frameworks), - "SDKROOT_PATH_ARG={}".format(apple_support.path_placeholders.sdkroot()), - ], - xcode_path_resolve_level = apple_support.xcode_path_resolve_level.args, - ) action_args = ctx.actions.args() action_args.add( @@ -204,16 +174,6 @@ def _apple_support_test_impl(ctx): ], xcode_path_resolve_level = apple_support.xcode_path_resolve_level.args_and_files, ) - apple_support.run( - ctx, - outputs = [run_output_xcode_path_in_file_ctx], - executable = test_tool, - arguments = [ - run_output_xcode_path_in_file_ctx.path, - action_args, - ], - xcode_path_resolve_level = apple_support.xcode_path_resolve_level.args_and_files, - ) apple_support.run_shell( actions = ctx.actions, @@ -226,25 +186,12 @@ def _apple_support_test_impl(ctx): tool = test_tool.path, ), ) - apple_support.run_shell( - ctx, - outputs = [run_shell_output_ctx], - tools = [test_tool], - command = "{tool} {output}".format( - output = run_shell_output_ctx.path, - tool = test_tool.path, - ), - ) test_files = [ run_output, - run_output_ctx, run_output_xcode_path_in_args, - run_output_xcode_path_in_args_ctx, run_output_xcode_path_in_file, - run_output_xcode_path_in_file_ctx, run_shell_output, - run_shell_output_ctx, ] test_script = ctx.actions.declare_file("{}_test_script".format(ctx.label.name))