Skip to content

Commit 64e6609

Browse files
authored
fix: ensure the xcodeproj project generation uses the correct Bazel binary in the firebase_example (#1401)
The `rules_xcodeproj` project generator calls Bazel. By default, it looks for `bazel` on the `PATH`. When running under `rules_bazel_integration_test` this will pick up the default Bazel version on the system. If that is Bazelisk, it will pull down the latest Bazel. The fix is to explicitly tell `xcodeproj` which Bazel binary to use. - Add `tools/bazel` to the `firebase_example`. It inspects environment variables to determine the Bazel version to use and writes `tools/bazel_for_xcodeproj` which executes Bazel commands against the correct Bazel binary. - Use `sanbox` for `rules_xcodeproj` builds in firebase example. This avoids the duplicate definition error. - Add `print_and_run` bash function to `examples/firebase_example/do_test` to ease debugging future issues.
1 parent ca8f0da commit 64e6609

File tree

5 files changed

+58
-4
lines changed

5 files changed

+58
-4
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,6 @@ local.bazelrc
2020

2121
# Ignore vscode settings
2222
.vscode
23+
24+
# This is generated by the firebase example's tools/bazel.
25+
examples/firebase_example/tools/bazel_for_xcodeproj

examples/firebase_example/.bazelrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,6 @@ build --cxxopt='-std=gnu++14'
2020
# Firebase SPM support requires `-ObjC` linker option.
2121
# https://github.com/firebase/firebase-ios-sdk/blob/master/SwiftPackageManager.md#requirements
2222
build --linkopt='-ObjC'
23+
24+
# Use sandbox for rules_xcodeproj to avoid duplicate definition errors.
25+
build:rules_xcodeproj --spawn_strategy=remote,worker,sandboxed,local

examples/firebase_example/analytics/AnalyticsExample/BUILD.bazel

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ build_test(
4444

4545
xcodeproj(
4646
name = "xcodeproj",
47+
# The ./tools/bazel_for_xcodeproj is generated by tools/bazel. It should
48+
# not be checked into source control.
49+
bazel_path = "./tools/bazel_for_xcodeproj",
4750
project_name = "AnalyticsExample",
4851
tags = ["manual"],
4952
top_level_targets = [

examples/firebase_example/do_test

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,23 @@
22

33
set -o errexit -o nounset -o pipefail
44

5+
print_and_run() {
6+
local cmd="${1}"
7+
shift 1
8+
printf >&2 "======================\n%s: %s %s\n======================\n" \
9+
"$(basename "${BASH_SOURCE[0]}")" "$(basename "${cmd}")" "$*"
10+
"${cmd}" "$@"
11+
}
12+
513
# Use the Bazel binary specified by the integration test. Otherise, fall back
614
# to bazel.
715
bazel="${BIT_BAZEL_BINARY:-bazel}"
816

917
# Generate Swift external deps and update build files
10-
"${bazel}" run //:tidy
18+
print_and_run "${bazel}" run //:tidy
1119

1220
# Ensure that it builds and tests pass
13-
"${bazel}" test //...
21+
print_and_run "${bazel}" test //...
1422

1523
# The analytics/AnalyticsExample generates an Xcode project using
1624
# rules_xcodeproj. The following ensures that the project generates properly
@@ -19,8 +27,8 @@ bazel="${BIT_BAZEL_BINARY:-bazel}"
1927
# https://github.com/MobileNativeFoundation/rules_xcodeproj/issues/2703
2028

2129
# Generate the Xcode project
22-
"${bazel}" run //analytics/AnalyticsExample:xcodeproj
30+
print_and_run "${bazel}" run //analytics/AnalyticsExample:xcodeproj
2331

2432
# Build the workspace
25-
"${bazel}" run //analytics/AnalyticsExample:xcodeproj -- \
33+
print_and_run "${bazel}" run //analytics/AnalyticsExample:xcodeproj -- \
2634
--generator_output_groups=all_targets 'build --remote_download_minimal'
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env bash
2+
3+
set -o errexit -o nounset -o pipefail
4+
5+
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" > /dev/null && pwd)"
6+
7+
# Order of Bazel selection:
8+
# 1. BAZEL_REAL: Use Bazel specified by Bazelisk
9+
# 2. BIT_BAZEL_BINARY: Use Bazel specified by rules_bazel_integration_test.
10+
# 3: bazel: Try to find Bazel on the PATH
11+
#
12+
# NOTE: Do not put BIT_BAZEL_BINARY first in the evaluation. It can lead to
13+
# recursive loop when using rules_bazel_integration_test.
14+
bazel="${BAZEL_REAL:-${BIT_BAZEL_BINARY:-bazel}}"
15+
16+
# Generate a bazel script for rules_xcodeproj to use. This script ensures that
17+
# the xcodeproj runner uses the Bazel that is under test. We configure the
18+
# xcodeproj macro to find this generated bazel script.
19+
bazel_for_xcodeproj="${script_dir}/bazel_for_xcodeproj"
20+
cat >"${bazel_for_xcodeproj}" <<-EOF
21+
#!/usr/bin/env bash
22+
23+
# NOTE: This file is generated by tools/bazel. It is used by the xcodeproj
24+
# macro.
25+
26+
set -o errexit -o nounset -o pipefail
27+
28+
# Use the Bazel binary that we specify.
29+
bazel="${bazel}"
30+
31+
# Execute the Bazel command
32+
"\${bazel}" "\${@}"
33+
EOF
34+
chmod +x "${bazel_for_xcodeproj}"
35+
36+
# Execute the command
37+
"${bazel}" "${@}"

0 commit comments

Comments
 (0)