Skip to content

Commit c5b0e71

Browse files
keithbrentleyjones
authored andcommitted
Pass default_shell_env through actions
Through the investigation of bazelbuild/bazel#12049 one of the things I discovered was that when using `actions.run` there are 2 options for environment variables. `use_default_shell_env = True` is recommended, but cannot be used if you want to also pass `env` to the actions. To support Xcode version selection we have to pass `env` with those variables. Without the default shell env, we only get the environment variables defined in the crosstool, but not those passed with `--action_env`. This now adds variables passed as `--action_env=FOO=BAR`, but not those passed as `--action_env=FOO` (where the value is supposed to pass through). This is useful to ensure a few things: 1. The default PATH things are executed with includes /usr/local/bin. This can result in pollution of binaries from homebrew. Previously there was no way to limit this 2. This should be a good replacement for using custom Swift toolchains. Currently those environment variables only apply to some actions (excluding those from bazel) using `--action_env=TOOLCHAINS=foo` should work better than the current solution (this change can be made as a followup)
1 parent 3d6141f commit c5b0e71

6 files changed

+21
-5
lines changed

swift/internal/linking.bzl

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"""Implementation of linking logic for Swift."""
1616

1717
load("@bazel_skylib//lib:collections.bzl", "collections")
18+
load("@bazel_skylib//lib:dicts.bzl", "dicts")
1819
load("@bazel_skylib//lib:partial.bzl", "partial")
1920
load(
2021
"@bazel_tools//tools/build_defs/cc:action_names.bzl",
@@ -27,13 +28,15 @@ def _register_static_library_link_action(
2728
cc_feature_configuration,
2829
objects,
2930
output,
31+
env,
3032
swift_toolchain):
3133
"""Registers an action that creates a static library.
3234
3335
Args:
3436
actions: The object used to register actions.
3537
cc_feature_configuration: The C++ feature configuration to use when
3638
constructing the action.
39+
env: Extra environment to run archive command with.
3740
objects: A list of `File`s denoting object (`.o`) files that will be
3841
linked.
3942
output: A `File` to which the output library will be written.
@@ -68,10 +71,13 @@ def _register_static_library_link_action(
6871
else:
6972
args.add_all(objects)
7073

71-
env = cc_common.get_environment_variables(
72-
action_name = CPP_LINK_STATIC_LIBRARY_ACTION_NAME,
73-
feature_configuration = cc_feature_configuration,
74-
variables = archiver_variables,
74+
env = dicts.add(
75+
env,
76+
cc_common.get_environment_variables(
77+
action_name = CPP_LINK_STATIC_LIBRARY_ACTION_NAME,
78+
feature_configuration = cc_feature_configuration,
79+
variables = archiver_variables,
80+
),
7581
)
7682

7783
execution_requirements_list = cc_common.get_execution_requirements(
@@ -105,6 +111,7 @@ def create_linker_input(
105111
library_name,
106112
objects,
107113
owner,
114+
env,
108115
swift_toolchain,
109116
additional_inputs = [],
110117
user_link_flags = []):
@@ -126,6 +133,7 @@ def create_linker_input(
126133
objects: A list of `File`s denoting object (`.o`) files that will be
127134
linked.
128135
owner: The `Label` of the target that owns this linker input.
136+
env: Extra environment to launch the link command with.
129137
swift_toolchain: The Swift toolchain provider to use when constructing
130138
the action.
131139
additional_inputs: A list of extra `File` inputs passed to the linking
@@ -154,6 +162,7 @@ def create_linker_input(
154162
cc_feature_configuration = cc_feature_configuration,
155163
objects = objects,
156164
output = static_library,
165+
env = env,
157166
swift_toolchain = swift_toolchain,
158167
)
159168
else:

swift/internal/swift_grpc_library.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ def _swift_grpc_library_impl(ctx):
300300
feature_configuration = feature_configuration,
301301
),
302302
compilation_outputs = compilation_outputs,
303+
env = ctx.configuration.default_shell_env,
303304
is_dynamic = False,
304305
is_static = True,
305306
library_name = ctx.label.name,

swift/internal/swift_library.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ def _swift_library_impl(ctx):
211211
feature_configuration = feature_configuration,
212212
),
213213
compilation_outputs = compilation_outputs,
214+
env = ctx.configuration.default_shell_env,
214215
is_dynamic = False,
215216
is_static = True,
216217
library_name = ctx.label.name,

swift/internal/swift_module_alias.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ def _swift_module_alias_impl(ctx):
8080
feature_configuration = feature_configuration,
8181
),
8282
compilation_outputs = compilation_outputs,
83+
env = ctx.configuration.default_shell_env,
8384
is_dynamic = False,
8485
is_static = True,
8586
library_name = ctx.label.name,

swift/internal/swift_protoc_gen_aspect.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,7 @@ def _swift_protoc_gen_aspect_impl(target, aspect_ctx):
433433
feature_configuration = feature_configuration,
434434
),
435435
compilation_outputs = compilation_outputs,
436+
env = aspect_ctx.configuration.default_shell_env,
436437
is_dynamic = False,
437438
is_static = True,
438439
# Prevent conflicts with C++ protos in the same output directory,

swift/internal/xcode_swift_toolchain.bzl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,10 @@ def _xcode_swift_toolchain_impl(ctx):
742742
if _is_xcode_at_least_version(xcode_config, "12.5"):
743743
requested_features.append(SWIFT_FEATURE_SUPPORTS_SYSTEM_MODULE_FLAG)
744744

745-
env = _xcode_env(platform = platform, xcode_config = xcode_config)
745+
env = dicts.add(
746+
ctx.configuration.default_shell_env,
747+
_xcode_env(platform = platform, xcode_config = xcode_config),
748+
)
746749
execution_requirements = xcode_config.execution_info()
747750
generated_header_rewriter = ctx.executable.generated_header_rewriter
748751

0 commit comments

Comments
 (0)