Skip to content

Commit 4d79a72

Browse files
committed
Add generator_bazel_env
Allows setting environment variables that affect only the nested generator call, but not the Xcode based calls. An example use case is setting `tools/bazel` output affecting env variables (e.g. color or verbosity). Signed-off-by: Brentley Jones <github@brentleyjones.com>
1 parent bf72498 commit 4d79a72

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

xcodeproj/internal/xcodeproj_runner.bzl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ def _write_runner(
272272
execution_root_file,
273273
extra_flags_bazelrc,
274274
extra_generator_flags,
275+
generator_bazel_env,
275276
generator_build_file,
276277
generator_defs_bzl,
277278
install_path,
@@ -323,6 +324,25 @@ fi
323324
),
324325
))
325326

327+
for key, value in generator_bazel_env.items():
328+
if value == "\0":
329+
collect_statements.append("""\
330+
if [[ -n "${{{key}:-}}" ]]; then
331+
envs+=("{key}=${key}")
332+
fi
333+
""".format(key = key))
334+
else:
335+
base_envs_values.append(" \"{}={}\"".format(
336+
key,
337+
(
338+
value.replace(
339+
# Escape double quotes for bash
340+
"\"",
341+
"\\\"",
342+
)
343+
),
344+
))
345+
326346
collect_bazel_env = """\
327347
envs=(
328348
{base_envs_values}
@@ -439,6 +459,7 @@ def _xcodeproj_runner_impl(ctx):
439459
extra_generator_flags = (
440460
ctx.attr._extra_generator_flags[BuildSettingInfo].value
441461
),
462+
generator_bazel_env = ctx.attr.generator_bazel_env,
442463
generator_build_file = generator_build_file,
443464
generator_defs_bzl = generator_defs_bzl,
444465
install_path = install_path,
@@ -474,6 +495,7 @@ xcodeproj_runner = rule(
474495
"config": attr.string(mandatory = True),
475496
"default_xcode_configuration": attr.string(),
476497
"focused_labels": attr.string_list(default = []),
498+
"generator_bazel_env": attr.string_dict(mandatory = True),
477499
"generation_shard_count": attr.int(mandatory = True),
478500
"import_index_build_indexstores": attr.bool(mandatory = True),
479501
"install_directory": attr.string(mandatory = True),

xcodeproj/xcodeproj.bzl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def xcodeproj(
3131
default_xcode_configuration = None,
3232
extra_files = [],
3333
focused_targets = [],
34+
generator_bazel_env = {},
3435
import_index_build_indexstores = True,
3536
install_directory = None,
3637
ios_device_cpus = "arm64",
@@ -150,6 +151,13 @@ def xcodeproj(
150151
listed explicitly in the `unfocused_targets` argument. The labels
151152
must match transitive dependencies of the targets specified in the
152153
`top_level_targets` argument.
154+
generator_bazel_env: Optional. A `dict` of environment variables to
155+
set when invoking `bazel_path` during project generation only.
156+
157+
This behaves the same as `bazel_env`, but only applies to the
158+
generated runner's Bazel invocations while generating the project.
159+
These values are not written into the generated `.xcodeproj`, and
160+
they do not apply to Bazel builds launched later from Xcode.
153161
import_index_build_indexstores: Optional. Whether to import the index
154162
stores generated by Index Build.
155163
@@ -365,6 +373,7 @@ def xcodeproj(
365373
if not bazel_path:
366374
bazel_path = "bazel"
367375
bazel_env = dict(bazel_env) if bazel_env else {}
376+
generator_bazel_env = dict(generator_bazel_env) if generator_bazel_env else {}
368377
if "PATH" not in bazel_env:
369378
bazel_env["PATH"] = "/bin:/usr/bin"
370379
if "LANG" not in bazel_env:
@@ -391,6 +400,12 @@ def xcodeproj(
391400
key: "\0" if value == None else value
392401
for key, value in sorted(bazel_env.items())
393402
}
403+
generator_bazel_env = {
404+
# Null character is used to represent `None`, since `attr.string_dict`
405+
# requires non-`None` values.
406+
key: "\0" if value == None else value
407+
for key, value in sorted(generator_bazel_env.items())
408+
}
394409

395410
if default_xcode_configuration and default_xcode_configuration not in xcode_configurations:
396411
keys = sorted(xcode_configurations.keys())
@@ -530,6 +545,7 @@ for {configuration} ({new_keys}) do not match keys of other configurations \
530545
config = config,
531546
default_xcode_configuration = default_xcode_configuration,
532547
focused_labels = focused_labels,
548+
generator_bazel_env = generator_bazel_env,
533549
generation_shard_count = generation_shard_count,
534550
import_index_build_indexstores = import_index_build_indexstores,
535551
install_directory = install_directory,

0 commit comments

Comments
 (0)