Skip to content

Commit 6bdbcde

Browse files
Work around stale Xcode version from outer Bazel invocation (#3232)
We were potentially setting a stale `--version` in `runner.sh`, because not everyone (including the local dev environment of this ruleset) has code in `tools/bazel` to force Bazel to re-evaluate its Xcode cache when `xcode-select -p` changes. Now we follow more of https://github.com/bazelbuild/rules_apple/blob/master/doc/common_info.md#xcode-version-selection-and-invalidation. Signed-off-by: Brentley Jones <[email protected]>
1 parent b946dae commit 6bdbcde

File tree

3 files changed

+32
-41
lines changed

3 files changed

+32
-41
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ END_UNRELEASED_TEMPLATE
5656

5757
### Fixed
5858

59-
* TBD
59+
* Fixed potential stale `--xcode_version` in `runner.sh`: [#3232](https://github.com/MobileNativeFoundation/rules_xcodeproj/pull/3232)
6060

6161
### Ruleset Development Changes
6262

xcodeproj/internal/templates/runner.sh

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ readonly execution_root_file="$PWD/%execution_root_file%"
2222
readonly extra_flags_bazelrc="$PWD/%extra_flags_bazelrc%"
2323
readonly generator_build_file="$PWD/%generator_build_file%"
2424
readonly generator_defs_bzl="$PWD/%generator_defs_bzl%"
25-
readonly schemes_json="$PWD/%schemes_json%"
2625
readonly xcodeproj_bazelrc="$PWD/%xcodeproj_bazelrc%"
2726

2827
installer_flags=(
@@ -121,7 +120,7 @@ chmod u+w "$generator_package_directory/BUILD"
121120
cp "$generator_defs_bzl" "$generator_package_directory/defs.bzl"
122121
chmod u+w "$generator_package_directory/defs.bzl"
123122

124-
cat >> "$generator_package_directory/defs.bzl" <<EOF
123+
cat <<EOF >> "$generator_package_directory/defs.bzl"
125124
126125
# Constants
127126
@@ -146,27 +145,47 @@ if [[ -s "$extra_flags_bazelrc" ]]; then
146145
bazelrcs+=("--bazelrc=$extra_flags_bazelrc")
147146
fi
148147

148+
if [[ -n "${DEVELOPER_DIR:-}" ]]; then
149+
if [[ ! -f "${DEVELOPER_DIR%/*}/version.plist" ]]; then
150+
echo >&2 "DEVELOPER_DIR is set to invalid path: $DEVELOPER_DIR"
151+
exit 1
152+
fi
153+
154+
developer_dir="$DEVELOPER_DIR"
155+
156+
# We can use a fast path when `DEVELOPER_DIR` is set for us
157+
xcode_version=$(
158+
/usr/libexec/PlistBuddy \
159+
-c 'print ProductBuildVersion' \
160+
"${DEVELOPER_DIR%/*}/version.plist"
161+
)
162+
elif command -v xcodebuild > /dev/null 2>&1; then
163+
developer_dir="$(/usr/bin/xcode-select -p)"
164+
165+
# Xcode 15.4\nBuild version 15F31d -> 15F31d
166+
xcode_version=$(xcodebuild -version | awk '/Build version/{print $NF}')
167+
else
168+
developer_dir=""
169+
xcode_version=""
170+
fi
171+
149172
# We write to a `.bazelrc` file instead of passing flags directly in order to
150173
# support all Bazel commands via the `common` pseudo-command
151-
cat > "$pre_xcodeproj_bazelrc_dir/pre_xcodeproj.bazelrc" <<EOF
174+
if [[ -n "$xcode_version" ]]; then
175+
cat <<EOF > "$pre_xcodeproj_bazelrc_dir/pre_xcodeproj.bazelrc"
152176
# Be explicit about our desired Xcode version
153-
common:rules_xcodeproj --xcode_version=%xcode_version%
177+
common:rules_xcodeproj --xcode_version=$xcode_version
154178
155179
# Work around https://github.com/bazelbuild/bazel/issues/8902
156180
# \`USE_CLANG_CL\` is only used on Windows, we set it here to cause Bazel to
157181
# re-evaluate the cc_toolchain for a different Xcode version
158-
common:rules_xcodeproj --repo_env=USE_CLANG_CL=%xcode_version%
159-
common:rules_xcodeproj --repo_env=XCODE_VERSION=%xcode_version%
182+
common:rules_xcodeproj --repo_env=USE_CLANG_CL=$xcode_version
183+
common:rules_xcodeproj --repo_env=XCODE_VERSION=$xcode_version
160184
EOF
161-
162-
if command -v /usr/bin/xcode-select >/dev/null 2>&1; then
163-
developer_dir="$(/usr/bin/xcode-select -p)"
164-
else
165-
developer_dir="${DEVELOPER_DIR:-}"
166185
fi
167186

168187
if [[ -n "$developer_dir" ]]; then
169-
cat >> "$pre_xcodeproj_bazelrc_dir/pre_xcodeproj.bazelrc" <<EOF
188+
cat <<EOF>> "$pre_xcodeproj_bazelrc_dir/pre_xcodeproj.bazelrc"
170189
171190
# Set \`DEVELOPER_DIR\` in case a bazel wrapper filters it
172191
common:rules_xcodeproj --repo_env="DEVELOPER_DIR=$developer_dir"

xcodeproj/internal/xcodeproj_runner.bzl

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,6 @@ load(":collections.bzl", "uniq")
66
load(":execution_root.bzl", "write_execution_root_file")
77
load(":providers.bzl", "XcodeProjRunnerOutputInfo")
88

9-
def _get_xcode_product_version(*, xcode_config):
10-
raw_version = str(xcode_config.xcode_version())
11-
if not raw_version:
12-
fail("""\
13-
`xcode_config.xcode_version` was not set. This is a bazel bug. Try again.
14-
""")
15-
16-
version_components = raw_version.split(".")
17-
if len(version_components) < 4:
18-
# This will result in analysis cache misses, but it's better than
19-
# failing
20-
return raw_version
21-
22-
return version_components[3]
23-
249
def _process_extra_flags(*, attr, content, setting, config, config_suffix):
2510
extra_flags = getattr(attr, setting)[BuildSettingInfo].value
2611
if extra_flags:
@@ -272,7 +257,6 @@ def _write_runner(
272257
package,
273258
runner_label,
274259
template,
275-
xcode_version,
276260
xcodeproj_bazelrc):
277261
output = actions.declare_file("{}-runner.sh".format(name))
278262

@@ -358,7 +342,6 @@ def_env+='}}'""".format(
358342
"%generator_package_name%": generator_package_name,
359343
"%install_path%": install_path,
360344
"%runner_label%": runner_label,
361-
"%xcode_version%": xcode_version,
362345
"%xcodeproj_bazelrc%": xcodeproj_bazelrc.short_path,
363346
},
364347
)
@@ -382,10 +365,6 @@ def _xcodeproj_runner_impl(ctx):
382365
"{}.xcodeproj".format(project_name),
383366
)
384367

385-
xcode_version = _get_xcode_product_version(
386-
xcode_config = ctx.attr._xcode_config[apple_common.XcodeVersionConfig],
387-
)
388-
389368
xcodeproj_bazelrc = _write_xcodeproj_bazelrc(
390369
actions = actions,
391370
config = config,
@@ -443,7 +422,6 @@ def _xcodeproj_runner_impl(ctx):
443422
install_path = install_path,
444423
runner_label = runner_label,
445424
template = ctx.file._runner_template,
446-
xcode_version = xcode_version,
447425
xcodeproj_bazelrc = xcodeproj_bazelrc,
448426
)
449427

@@ -548,12 +526,6 @@ xcodeproj_runner = rule(
548526
allow_single_file = True,
549527
default = Label("//xcodeproj/internal/templates:runner.sh"),
550528
),
551-
"_xcode_config": attr.label(
552-
default = configuration_field(
553-
name = "xcode_config_label",
554-
fragment = "apple",
555-
),
556-
),
557529
},
558530
executable = True,
559531
)

0 commit comments

Comments
 (0)