|
1 | 1 | #!/usr/bin/env bash |
2 | 2 | # |
3 | | -# Initialize Bazel directories for macOS with persistent caches |
| 3 | +# Initialize Bazel macOS Development Environment |
4 | 4 | # |
5 | | -# This script creates the directory structure needed for: |
6 | | -# - Repository cache (survives 'bazel clean --expunge') |
| 5 | +# This script sets up: |
| 6 | +# - Bazel cache directories (repository, build, repo-contents) |
7 | 7 | # - Distribution mirror (optional, for offline builds) |
8 | | -# - Build cache |
| 8 | +# - Development dependencies validation |
| 9 | +# - Optional direnv integration |
| 10 | +# - Environment variable configuration |
9 | 11 | # |
10 | 12 | # Usage: |
11 | 13 | # bash tools/setup/init_bazel_env.sh |
12 | | -# source tools/setup/init_bazel_env.sh (optional, to export variables) |
| 14 | +# source tools/setup/init_bazel_env.sh (to export variables) |
13 | 15 | # |
| 16 | +# This script is designed to run on macOS with Bazel 9+ |
14 | 17 |
|
15 | 18 | set -euo pipefail |
16 | 19 |
|
17 | 20 | # Color output |
18 | 21 | RED='\033[0;31m' |
19 | 22 | GREEN='\033[0;32m' |
20 | 23 | YELLOW='\033[1;33m' |
| 24 | +BLUE='\033[0;34m' |
21 | 25 | NC='\033[0m' # No Color |
22 | 26 |
|
23 | 27 | # Directory configuration |
24 | 28 | BAZEL_HOME="${HOME}/.bazel" |
25 | 29 | REPO_CACHE="${BAZEL_HOME}/repo-cache" |
| 30 | +REPO_CONTENTS_CACHE="${BAZEL_HOME}/repo-contents-cache" |
26 | 31 | DIST_DIR="${BAZEL_HOME}/distdir" |
27 | 32 | BUILD_CACHE="${BAZEL_HOME}/build-cache" |
28 | 33 |
|
| 34 | +# Flag for verbose output |
| 35 | +VERBOSE=${VERBOSE:-false} |
| 36 | + |
29 | 37 | echo -e "${YELLOW}Bazel macOS Environment Initialization${NC}" |
30 | 38 | echo "=======================================" |
31 | 39 | echo "" |
32 | 40 |
|
| 41 | +# Function to check command availability |
| 42 | +check_command() { |
| 43 | + local cmd=$1 |
| 44 | + local friendly_name=${2:-$cmd} |
| 45 | + if command -v "${cmd}" >/dev/null 2>&1; then |
| 46 | + echo -e " ✓ ${cmd} (${friendly_name})" |
| 47 | + return 0 |
| 48 | + else |
| 49 | + echo -e " ${YELLOW}○${NC} ${cmd} (${friendly_name}) - optional" |
| 50 | + return 1 |
| 51 | + fi |
| 52 | +} |
| 53 | + |
| 54 | +# Function to check command availability (required) |
| 55 | +check_command_required() { |
| 56 | + local cmd=$1 |
| 57 | + local friendly_name=${2:-$cmd} |
| 58 | + if command -v "${cmd}" >/dev/null 2>&1; then |
| 59 | + echo -e " ✓ ${cmd} (${friendly_name})" |
| 60 | + return 0 |
| 61 | + else |
| 62 | + echo -e " ${RED}✗${NC} ${cmd} (${friendly_name}) - REQUIRED" |
| 63 | + return 1 |
| 64 | + fi |
| 65 | +} |
| 66 | + |
| 67 | +# Validate dependencies |
| 68 | +echo -e "${GREEN}Checking dependencies...${NC}" |
| 69 | +has_errors=false |
| 70 | + |
| 71 | +if ! check_command_required bazel "Bazel 9+"; then |
| 72 | + echo -e "${YELLOW} → Install from: https://bazel.build/install${NC}" |
| 73 | + has_errors=true |
| 74 | +fi |
| 75 | + |
| 76 | +if [[ "$(uname)" == "Darwin" ]]; then |
| 77 | + if ! check_command_required xcode-select "Xcode Command Line Tools"; then |
| 78 | + echo -e "${YELLOW} → Run: xcode-select --install${NC}" |
| 79 | + has_errors=true |
| 80 | + fi |
| 81 | +fi |
| 82 | + |
| 83 | +# Optional dependencies |
| 84 | +check_command lcov "lcov (for HTML coverage reports)" |
| 85 | +check_command direnv "direnv (for automatic environment loading)" |
| 86 | + |
| 87 | +if [[ "${has_errors}" == true ]]; then |
| 88 | + echo -e "${RED}Please install required dependencies first.${NC}" |
| 89 | + exit 1 |
| 90 | +fi |
| 91 | + |
| 92 | +echo "" |
| 93 | + |
33 | 94 | # Create directories |
34 | | -echo -e "${GREEN}Creating directories...${NC}" |
| 95 | +echo -e "${GREEN}Creating cache directories...${NC}" |
35 | 96 | mkdir -p "${REPO_CACHE}" && echo " ✓ ${REPO_CACHE}" |
| 97 | +mkdir -p "${REPO_CONTENTS_CACHE}" && echo " ✓ ${REPO_CONTENTS_CACHE}" |
36 | 98 | mkdir -p "${DIST_DIR}" && echo " ✓ ${DIST_DIR}" |
37 | 99 | mkdir -p "${BUILD_CACHE}" && echo " ✓ ${BUILD_CACHE}" |
38 | 100 |
|
|
54 | 116 | fi |
55 | 117 |
|
56 | 118 | # Set permissions |
57 | | -chmod 755 "${REPO_CACHE}" "${DIST_DIR}" "${BUILD_CACHE}" |
| 119 | +chmod 755 "${REPO_CACHE}" "${REPO_CONTENTS_CACHE}" "${DIST_DIR}" "${BUILD_CACHE}" |
58 | 120 |
|
59 | 121 | # Export variables for sourcing |
60 | 122 | export BAZEL_REPO_CACHE="${REPO_CACHE}" |
| 123 | +export BAZEL_REPO_CONTENTS_CACHE="${REPO_CONTENTS_CACHE}" |
61 | 124 | export BAZEL_DIST_DIR="${DIST_DIR}" |
62 | 125 | export BAZEL_BUILD_CACHE="${BUILD_CACHE}" |
63 | 126 |
|
64 | 127 | echo "" |
65 | 128 | echo -e "${GREEN}Directories initialized successfully!${NC}" |
66 | 129 | echo "" |
67 | 130 | echo "Configuration:" |
68 | | -echo " Repository Cache: ${REPO_CACHE}" |
69 | | -echo " Distribution Dir: ${DIST_DIR}" |
70 | | -echo " Build Cache: ${BUILD_CACHE}" |
| 131 | +echo " Repository Cache: ${REPO_CACHE}" |
| 132 | +echo " Repo-Contents Cache: ${REPO_CONTENTS_CACHE}" |
| 133 | +echo " Distribution Mirror: ${DIST_DIR}" |
| 134 | +echo " Build Cache: ${BUILD_CACHE}" |
71 | 135 | echo "" |
72 | 136 | echo -e "${YELLOW}Next steps:${NC}" |
73 | | -echo " 1. The .bazelrc already references these directories" |
74 | | -echo " 2. For offline builds, add dependencies to: ${DIST_DIR}" |
75 | | -echo " 3. Run: bazel query //... to populate caches" |
76 | | -echo " 4. Run: bazel clean --expunge (caches will survive)" |
| 137 | +echo " 1. ✓ Cache directories are configured in .bazelrc" |
| 138 | +echo " 2. Build the complete toolchain:" |
| 139 | +echo " bazel build //..." |
| 140 | +echo " 3. Build developer tools (optional):" |
| 141 | +echo " bazel run //tools:bazel_env" |
| 142 | +echo " direnv allow (if using direnv)" |
| 143 | +echo " 4. Run tests and coverage:" |
| 144 | +echo " bazel test //..." |
| 145 | +echo " bazel coverage //... && bazel run //:coverage_html" |
| 146 | +echo "" |
| 147 | +echo -e "${BLUE}For offline builds:${NC}" |
| 148 | +echo " - Add dependencies to: ${DIST_DIR}" |
| 149 | +echo " - Uncomment in .bazelrc: common --distdir=~/.bazel/distdir" |
| 150 | +echo "" |
| 151 | +echo -e "${BLUE}Useful commands:${NC}" |
| 152 | +echo " - Refresh compile_commands.json:" |
| 153 | +echo " bazel run //:refresh_compile_commands" |
| 154 | +echo " - Generate HTML coverage reports:" |
| 155 | +echo " bazel run //:coverage_html" |
| 156 | +echo " - Debug configuration:" |
| 157 | +echo " bazel build --config=debug //..." |
| 158 | +echo " - Optimized build:" |
| 159 | +echo " bazel build --config=opt //..." |
77 | 160 | echo "" |
0 commit comments