Skip to content

Commit

Permalink
fix: ensure the xcodeproj project generation uses the correct Bazel b…
Browse files Browse the repository at this point in the history
…inary 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.
  • Loading branch information
cgrindel authored Dec 16, 2024
1 parent ca8f0da commit 64e6609
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ local.bazelrc

# Ignore vscode settings
.vscode

# This is generated by the firebase example's tools/bazel.
examples/firebase_example/tools/bazel_for_xcodeproj
3 changes: 3 additions & 0 deletions examples/firebase_example/.bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ build --cxxopt='-std=gnu++14'
# Firebase SPM support requires `-ObjC` linker option.
# https://github.com/firebase/firebase-ios-sdk/blob/master/SwiftPackageManager.md#requirements
build --linkopt='-ObjC'

# Use sandbox for rules_xcodeproj to avoid duplicate definition errors.
build:rules_xcodeproj --spawn_strategy=remote,worker,sandboxed,local
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ build_test(

xcodeproj(
name = "xcodeproj",
# The ./tools/bazel_for_xcodeproj is generated by tools/bazel. It should
# not be checked into source control.
bazel_path = "./tools/bazel_for_xcodeproj",
project_name = "AnalyticsExample",
tags = ["manual"],
top_level_targets = [
Expand Down
16 changes: 12 additions & 4 deletions examples/firebase_example/do_test
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,23 @@

set -o errexit -o nounset -o pipefail

print_and_run() {
local cmd="${1}"
shift 1
printf >&2 "======================\n%s: %s %s\n======================\n" \
"$(basename "${BASH_SOURCE[0]}")" "$(basename "${cmd}")" "$*"
"${cmd}" "$@"
}

# Use the Bazel binary specified by the integration test. Otherise, fall back
# to bazel.
bazel="${BIT_BAZEL_BINARY:-bazel}"

# Generate Swift external deps and update build files
"${bazel}" run //:tidy
print_and_run "${bazel}" run //:tidy

# Ensure that it builds and tests pass
"${bazel}" test //...
print_and_run "${bazel}" test //...

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

# Generate the Xcode project
"${bazel}" run //analytics/AnalyticsExample:xcodeproj
print_and_run "${bazel}" run //analytics/AnalyticsExample:xcodeproj

# Build the workspace
"${bazel}" run //analytics/AnalyticsExample:xcodeproj -- \
print_and_run "${bazel}" run //analytics/AnalyticsExample:xcodeproj -- \
--generator_output_groups=all_targets 'build --remote_download_minimal'
37 changes: 37 additions & 0 deletions examples/firebase_example/tools/bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env bash

set -o errexit -o nounset -o pipefail

script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" > /dev/null && pwd)"

# Order of Bazel selection:
# 1. BAZEL_REAL: Use Bazel specified by Bazelisk
# 2. BIT_BAZEL_BINARY: Use Bazel specified by rules_bazel_integration_test.
# 3: bazel: Try to find Bazel on the PATH
#
# NOTE: Do not put BIT_BAZEL_BINARY first in the evaluation. It can lead to
# recursive loop when using rules_bazel_integration_test.
bazel="${BAZEL_REAL:-${BIT_BAZEL_BINARY:-bazel}}"

# Generate a bazel script for rules_xcodeproj to use. This script ensures that
# the xcodeproj runner uses the Bazel that is under test. We configure the
# xcodeproj macro to find this generated bazel script.
bazel_for_xcodeproj="${script_dir}/bazel_for_xcodeproj"
cat >"${bazel_for_xcodeproj}" <<-EOF
#!/usr/bin/env bash
# NOTE: This file is generated by tools/bazel. It is used by the xcodeproj
# macro.
set -o errexit -o nounset -o pipefail
# Use the Bazel binary that we specify.
bazel="${bazel}"
# Execute the Bazel command
"\${bazel}" "\${@}"
EOF
chmod +x "${bazel_for_xcodeproj}"

# Execute the command
"${bazel}" "${@}"

0 comments on commit 64e6609

Please sign in to comment.