| #!/bin/bash |
| # Configure IREE for Bazel builds. |
| # |
| # Usage: iree-bazel-configure |
| # |
| # This script runs configure_bazel.py to detect platform/compiler and |
| # creates a user.bazelrc with recommended settings. |
| |
| set -e |
| |
| # Source shared library. |
| SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| source "${SCRIPT_DIR}/iree-bazel-lib" |
| iree_bazel_init "iree-bazel-configure" |
| |
| show_help() { |
| cat << 'EOF' |
| iree-bazel-configure - Configure IREE for Bazel builds |
| |
| USAGE |
| iree-bazel-configure |
| IREE_HAL_DRIVER_CUDA=ON iree-bazel-configure |
| |
| WHAT IT DOES |
| 1. Runs configure_bazel.py to detect platform/compiler |
| 2. Writes HAL driver configuration from environment variables |
| 3. Creates user.bazelrc with recommended settings (if not exists) |
| |
| GENERATED FILES |
| configured.bazelrc Platform detection + driver configuration |
| user.bazelrc Local settings (disk cache, debug config) |
| |
| ENVIRONMENT VARIABLES |
| IREE_BAZEL_CACHE_DIR - Bazel disk cache location |
| Unix default: ~/.cache/bazel-iree |
| Windows default: c:/bazelcache (avoids path length issues) |
| |
| HAL Drivers (runtime, matches CMake conventions): |
| IREE_HAL_DRIVER_CUDA=ON - Enable CUDA driver |
| IREE_HAL_DRIVER_VULKAN=ON - Enable Vulkan driver |
| IREE_HAL_DRIVER_HIP=ON - Enable HIP driver |
| IREE_HAL_DRIVER_METAL=ON - Enable Metal driver |
| IREE_HAL_DRIVER_AMDGPU=ON - Enable AMDGPU driver |
| IREE_HAL_DRIVER_LOCAL_SYNC=ON - Enable local-sync (default) |
| IREE_HAL_DRIVER_LOCAL_TASK=ON - Enable local-task (default) |
| IREE_HAL_DRIVER_NULL=ON - Enable null driver |
| |
| Values: ON/YES/TRUE/Y/1 (enabled), OFF/NO/FALSE/N/0 (disabled) |
| Defaults: local-sync, local-task, vulkan enabled (matches CMake) |
| |
| Compiler Plugins: |
| IREE_COMPILER_PLUGINS=all - All buildable plugins (default) |
| IREE_COMPILER_PLUGINS=all,-X - All except plugin X |
| IREE_COMPILER_PLUGINS=X,Y - Only plugins X and Y |
| |
| Plugin detection (for "all"): |
| - CUDA: requires IREE_CUDA_TOOLKIT_ROOT/IREE_CUDA_DEPS_DIR, |
| CUDA_ROOT/CUDA_HOME/CUDA_PATH/CUDA_TOOLKIT_ROOT_DIR, or nvcc in PATH |
| - ROCm: requires ROCM_PATH/ROCM_ROOT/ROCM_HOME/HIP_PATH or hipcc in PATH |
| - MetalSPIRV: always buildable |
| - StableHLO: requires third_party/stablehlo submodule |
| - Torch: requires third_party/torch-mlir submodule |
| |
| SDK detection: |
| VULKAN_SDK - Used when set; otherwise configure probes |
| vulkaninfo and common SDK install paths |
| |
| EXAMPLES |
| # Configure with defaults (all buildable plugins) |
| iree-bazel-configure |
| |
| # Enable CUDA runtime driver |
| IREE_HAL_DRIVER_CUDA=ON iree-bazel-configure |
| |
| # Enable multiple runtime drivers |
| IREE_HAL_DRIVER_CUDA=ON IREE_HAL_DRIVER_VULKAN=ON iree-bazel-configure |
| |
| # Exclude torch plugin (faster builds) |
| IREE_COMPILER_PLUGINS="all,-input_torch" iree-bazel-configure |
| |
| # Only specific plugins |
| IREE_COMPILER_PLUGINS="hal_target_llvm_cpu,hal_target_vulkan_spirv" iree-bazel-configure |
| |
| AFTER CONFIGURATION |
| iree-bazel-build Build IREE targets |
| iree-bazel-test Run tests with configured drivers |
| |
| SEE ALSO |
| iree-bazel-build, iree-bazel-test |
| EOF |
| } |
| |
| # Handle help and agent-md before worktree check. |
| case "${1:-}" in |
| -h|--help) show_help; exit 0 ;; |
| --agent-md|--agent_md) iree_show_agent_md; exit 0 ;; |
| esac |
| |
| # Set up worktree (configure doesn't call iree_ensure_configured - it IS the configure). |
| iree_require_worktree |
| cd "${IREE_BAZEL_WORKTREE_DIR}" |
| |
| iree_info "Configuring IREE for Bazel in ${IREE_BAZEL_WORKTREE_DIR}..." |
| |
| # Run platform detection. |
| if [[ -f "configure_bazel.py" ]]; then |
| iree_info "Running configure_bazel.py..." |
| # Default to all buildable plugins unless explicitly set. |
| # Users who want selective plugins can set IREE_COMPILER_PLUGINS explicitly. |
| if [[ -z "${IREE_COMPILER_PLUGINS:-}" ]]; then |
| export IREE_COMPILER_PLUGINS="all" |
| fi |
| python3 configure_bazel.py |
| else |
| iree_error "configure_bazel.py not found in ${IREE_BAZEL_WORKTREE_DIR}" |
| exit 1 |
| fi |
| |
| # Create user.bazelrc if it doesn't exist. |
| if [[ ! -f "user.bazelrc" ]]; then |
| iree_info "Creating user.bazelrc..." |
| |
| # Platform-specific cache directory defaults. |
| if [[ -n "${IREE_BAZEL_CACHE_DIR:-}" ]]; then |
| CACHE_DIR="${IREE_BAZEL_CACHE_DIR}" |
| elif [[ "${OSTYPE}" == "msys" || "${OSTYPE}" == "cygwin" || "${OSTYPE}" == "win32" ]]; then |
| # Windows: Use shorter path to avoid 260 character limit. |
| CACHE_DIR="c:/bazelcache" |
| else |
| # Unix: Use XDG cache directory. |
| CACHE_DIR="${HOME}/.cache/bazel-iree" |
| fi |
| |
| cat > user.bazelrc << EOF |
| # Local Bazel configuration. |
| # This file is not version-controlled. |
| |
| # Disk cache for faster rebuilds (override with IREE_BAZEL_CACHE_DIR). |
| build --disk_cache=${CACHE_DIR} |
| |
| # Garbage collect disk cache to prevent unbounded growth. |
| # Max size 50GB, GC runs when Bazel is idle for some time. |
| # TODO(benvanik): disabled until we upgrade bazel version. |
| #build --experimental_disk_cache_gc_max_size=50G |
| #build --experimental_disk_cache_gc_idle_delay=120s |
| |
| # Debug config: no optimizations, with assertions. |
| build:debug --config=asserts --compilation_mode=opt '--per_file_copt=iree|llvm@-O0' --strip=never |
| |
| # Enable assertions (optimized but with runtime checks). |
| build:asserts --compilation_mode=opt '--copt=-UNDEBUG' |
| EOF |
| |
| # Add Linux-specific sandbox optimization. |
| if [[ -d "/dev/shm" ]]; then |
| cat >> user.bazelrc << 'EOF' |
| |
| # Linux: use ramdisk for sandbox to reduce I/O with many cores. |
| build --sandbox_base=/dev/shm |
| EOF |
| iree_info "Added /dev/shm sandbox optimization (Linux)" |
| fi |
| |
| iree_info "Created user.bazelrc with recommended settings" |
| else |
| iree_info "user.bazelrc already exists, skipping" |
| fi |
| |
| iree_info "Configuration complete!" |
| echo "" |
| echo "Build with: iree-bazel-build [target]" |
| echo "Test with: iree-bazel-test [target]" |