Skip to content

Commit 1b97467

Browse files
committed
improve setup script
1 parent 86208a9 commit 1b97467

File tree

1 file changed

+97
-14
lines changed

1 file changed

+97
-14
lines changed

tools/setup/init_bazel_env.sh

Lines changed: 97 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,100 @@
11
#!/usr/bin/env bash
22
#
3-
# Initialize Bazel directories for macOS with persistent caches
3+
# Initialize Bazel macOS Development Environment
44
#
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)
77
# - Distribution mirror (optional, for offline builds)
8-
# - Build cache
8+
# - Development dependencies validation
9+
# - Optional direnv integration
10+
# - Environment variable configuration
911
#
1012
# Usage:
1113
# 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)
1315
#
16+
# This script is designed to run on macOS with Bazel 9+
1417

1518
set -euo pipefail
1619

1720
# Color output
1821
RED='\033[0;31m'
1922
GREEN='\033[0;32m'
2023
YELLOW='\033[1;33m'
24+
BLUE='\033[0;34m'
2125
NC='\033[0m' # No Color
2226

2327
# Directory configuration
2428
BAZEL_HOME="${HOME}/.bazel"
2529
REPO_CACHE="${BAZEL_HOME}/repo-cache"
30+
REPO_CONTENTS_CACHE="${BAZEL_HOME}/repo-contents-cache"
2631
DIST_DIR="${BAZEL_HOME}/distdir"
2732
BUILD_CACHE="${BAZEL_HOME}/build-cache"
2833

34+
# Flag for verbose output
35+
VERBOSE=${VERBOSE:-false}
36+
2937
echo -e "${YELLOW}Bazel macOS Environment Initialization${NC}"
3038
echo "======================================="
3139
echo ""
3240

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+
3394
# Create directories
34-
echo -e "${GREEN}Creating directories...${NC}"
95+
echo -e "${GREEN}Creating cache directories...${NC}"
3596
mkdir -p "${REPO_CACHE}" && echo "${REPO_CACHE}"
97+
mkdir -p "${REPO_CONTENTS_CACHE}" && echo "${REPO_CONTENTS_CACHE}"
3698
mkdir -p "${DIST_DIR}" && echo "${DIST_DIR}"
3799
mkdir -p "${BUILD_CACHE}" && echo "${BUILD_CACHE}"
38100

@@ -54,24 +116,45 @@ EOF
54116
fi
55117

56118
# Set permissions
57-
chmod 755 "${REPO_CACHE}" "${DIST_DIR}" "${BUILD_CACHE}"
119+
chmod 755 "${REPO_CACHE}" "${REPO_CONTENTS_CACHE}" "${DIST_DIR}" "${BUILD_CACHE}"
58120

59121
# Export variables for sourcing
60122
export BAZEL_REPO_CACHE="${REPO_CACHE}"
123+
export BAZEL_REPO_CONTENTS_CACHE="${REPO_CONTENTS_CACHE}"
61124
export BAZEL_DIST_DIR="${DIST_DIR}"
62125
export BAZEL_BUILD_CACHE="${BUILD_CACHE}"
63126

64127
echo ""
65128
echo -e "${GREEN}Directories initialized successfully!${NC}"
66129
echo ""
67130
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}"
71135
echo ""
72136
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 //..."
77160
echo ""

0 commit comments

Comments
 (0)