|
| 1 | +#!/bin/bash |
| 2 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | +# All rights reserved. |
| 4 | +# |
| 5 | +# This source code is licensed under the BSD-style license found in the |
| 6 | +# LICENSE file in the root directory of this source tree. |
| 7 | +# |
| 8 | +# Download and install the licensed Cadence Xtensa toolchain + core config for |
| 9 | +# a given backend, then export the environment that |
| 10 | +# backends/cadence/cadence.cmake and xt-run need. |
| 11 | +# |
| 12 | +# The artifacts (host tools, the core tarball, and the bundled license) cannot |
| 13 | +# be hosted publicly, so they are fetched at runtime from an auth-gated object |
| 14 | +# store. The store location is provided by the caller via XTENSA_S3_BUCKET (set |
| 15 | +# from a CI variable); credentials are obtained out of band before this runs. |
| 16 | +# |
| 17 | +# Usage: |
| 18 | +# XTENSA_S3_BUCKET=<bucket> .ci/scripts/setup-xtensa-tools.sh <backend> |
| 19 | +# backend = hifi4 | vision | fusion_g3 |
| 20 | +# |
| 21 | +# In GitHub Actions this appends the toolchain env to $GITHUB_ENV so later |
| 22 | +# steps inherit it. Run locally to populate a workspace for manual builds. |
| 23 | +# |
| 24 | +# Modeled on .ci/scripts/setup-arm-baremetal-tools.sh. |
| 25 | + |
| 26 | +set -euo pipefail |
| 27 | + |
| 28 | +BACKEND="${1:-}" |
| 29 | +if [[ -z "${BACKEND}" ]]; then |
| 30 | + echo "ERROR: usage: XTENSA_S3_BUCKET=<bucket> $0 <hifi4|vision|fusion_g3>" >&2 |
| 31 | + exit 1 |
| 32 | +fi |
| 33 | + |
| 34 | +S3_BUCKET="${XTENSA_S3_BUCKET:-}" |
| 35 | +if [[ -z "${S3_BUCKET}" ]]; then |
| 36 | + echo "ERROR: XTENSA_S3_BUCKET is not set (provide it from a CI variable)." >&2 |
| 37 | + exit 1 |
| 38 | +fi |
| 39 | +S3_TOOLCHAIN_PREFIX="${XTENSA_S3_TOOLCHAIN_PREFIX:-toolchains}" |
| 40 | +S3_CORE_PREFIX="${XTENSA_S3_CORE_PREFIX:-cores}" |
| 41 | + |
| 42 | +# Per-backend mapping: core tarball, toolchain tarball, core name, OPT flag. |
| 43 | +# The toolchain's clang major must match the core's codegen plugin: |
| 44 | +# hifi4 / fusion_g3 cores (RI-2022.10, clang 10) -> RI-2022.9 host tools |
| 45 | +# vision core (RJ-2025.5, clang 15) -> RJ-2025.5 host tools |
| 46 | +case "${BACKEND}" in |
| 47 | + hifi4) |
| 48 | + CORE_NAME="hifi4_ss_spfpu_7_et_ci2" |
| 49 | + CORE_TARBALL="hifi4_ss_spfpu_7_et_ci2_linux.tgz" |
| 50 | + TOOLCHAIN_TARBALL="XtensaTools_RI_2022_9_linux.tgz" |
| 51 | + TOOLCHAIN_VER="RI-2022.9-linux" |
| 52 | + OPT_FLAG="EXECUTORCH_NNLIB_OPT" |
| 53 | + ;; |
| 54 | + fusion_g3) |
| 55 | + CORE_NAME="XRC_FuG3_TYP_SPVFPU_et_c2" |
| 56 | + CORE_TARBALL="XRC_FuG3_TYP_SPVFPU_et_c2_linux.tgz" |
| 57 | + TOOLCHAIN_TARBALL="XtensaTools_RI_2022_9_linux.tgz" |
| 58 | + TOOLCHAIN_VER="RI-2022.9-linux" |
| 59 | + OPT_FLAG="EXECUTORCH_FUSION_G3_OPT" |
| 60 | + ;; |
| 61 | + vision) |
| 62 | + CORE_NAME="XRC_Vision_110_AO_et_ci2" |
| 63 | + CORE_TARBALL="XRC_Vision_110_AO_et_ci2_linux.tgz" |
| 64 | + TOOLCHAIN_TARBALL="XtensaTools_RJ_2025_5_linux.tgz" |
| 65 | + TOOLCHAIN_VER="RJ-2025.5-linux" |
| 66 | + OPT_FLAG="EXECUTORCH_VISION_OPT" |
| 67 | + ;; |
| 68 | + *) |
| 69 | + echo "ERROR: unknown backend '${BACKEND}' (expected hifi4|vision|fusion_g3)" >&2 |
| 70 | + exit 1 |
| 71 | + ;; |
| 72 | +esac |
| 73 | + |
| 74 | +XTENSA_ROOT="${XTENSA_ROOT:-/tmp/xtensa}" |
| 75 | +TOOLS_ROOT="${XTENSA_ROOT}/tools" # contains <ver>-linux/XtensaTools |
| 76 | +CORES_ROOT="${XTENSA_ROOT}/cores" # contains <corever>-linux/<core> |
| 77 | +REGISTRY_ROOT="${XTENSA_ROOT}/registry/${CORE_NAME}" |
| 78 | +DL_DIR="${XTENSA_ROOT}/download" |
| 79 | +mkdir -p "${TOOLS_ROOT}" "${CORES_ROOT}" "${REGISTRY_ROOT}" "${DL_DIR}" |
| 80 | + |
| 81 | +s3_get() { |
| 82 | + # $1 = s3 key, $2 = local dest |
| 83 | + local key="$1" dest="$2" |
| 84 | + echo "Downloading s3://${S3_BUCKET}/${key} ..." |
| 85 | + aws s3 cp "s3://${S3_BUCKET}/${key}" "${dest}" --only-show-errors |
| 86 | +} |
| 87 | + |
| 88 | +# 1. Toolchain (host xt-clang/xt-run). Skip re-extract if already present. |
| 89 | +if [[ ! -d "${TOOLS_ROOT}/${TOOLCHAIN_VER}/XtensaTools" ]]; then |
| 90 | + s3_get "${S3_TOOLCHAIN_PREFIX}/${TOOLCHAIN_TARBALL}" "${DL_DIR}/${TOOLCHAIN_TARBALL}" |
| 91 | + tar xzf "${DL_DIR}/${TOOLCHAIN_TARBALL}" -C "${TOOLS_ROOT}" |
| 92 | +fi |
| 93 | +TOOLCHAIN_HOME="${TOOLS_ROOT}/${TOOLCHAIN_VER}/XtensaTools" |
| 94 | +if [[ ! -x "${TOOLCHAIN_HOME}/bin/xt-clang" ]]; then |
| 95 | + echo "ERROR: xt-clang not found at ${TOOLCHAIN_HOME}/bin after extract" >&2 |
| 96 | + exit 1 |
| 97 | +fi |
| 98 | + |
| 99 | +# 2. Core config (ISA libs, params, examples, bundled magic-key license). |
| 100 | +s3_get "${S3_CORE_PREFIX}/${CORE_TARBALL}" "${DL_DIR}/${CORE_TARBALL}" |
| 101 | +tar xzf "${DL_DIR}/${CORE_TARBALL}" -C "${CORES_ROOT}" |
| 102 | +CORE_DIR=$(echo "${CORES_ROOT}"/*/"${CORE_NAME}") |
| 103 | +if [[ ! -d "${CORE_DIR}" ]]; then |
| 104 | + echo "ERROR: core dir for ${CORE_NAME} not found under ${CORES_ROOT}" >&2 |
| 105 | + exit 1 |
| 106 | +fi |
| 107 | + |
| 108 | +# 3. Build a local Xtensa core registry with the XPG-internal build paths in |
| 109 | +# the params file rewritten to our extracted toolchain + core locations. |
| 110 | +# The vendor ships params referencing /././home/xpgcust/... build paths. |
| 111 | +PARAMS_SRC="${CORE_DIR}/config/${CORE_NAME}-params" |
| 112 | +TOOLS_PFX=$(sed -n 's/^install-prefix = //p' "${PARAMS_SRC}" | head -1) |
| 113 | +TOOLSUB_PFX=$(sed -n 's/^xtensa-tools = //p' "${PARAMS_SRC}" | head -1) |
| 114 | +CFG_PFX=$(sed -n 's/^config-prefix = //p' "${PARAMS_SRC}" | head -1) |
| 115 | +sed \ |
| 116 | + -e "s|${TOOLS_PFX}|${TOOLCHAIN_HOME}|g" \ |
| 117 | + -e "s|${TOOLSUB_PFX}|${TOOLCHAIN_HOME}/Tools|g" \ |
| 118 | + -e "s|${CFG_PFX}|${CORE_DIR}|g" \ |
| 119 | + "${PARAMS_SRC}" > "${REGISTRY_ROOT}/${CORE_NAME}-params" |
| 120 | +ln -sf "${CORE_NAME}-params" "${REGISTRY_ROOT}/default-params" |
| 121 | + |
| 122 | +LICENSE_FILE="${CORE_DIR}/misc/license.dat" |
| 123 | + |
| 124 | +# 4. Export environment. cadence.cmake reads XTENSA_TOOLCHAIN/TOOLCHAIN_VER; |
| 125 | +# xt-clang/xt-run read XTENSA_SYSTEM/XTENSA_CORE; xtensad reads |
| 126 | +# XTENSAD_LICENSE_FILE (the bundled uncounted magic key, no server needed). |
| 127 | +emit() { |
| 128 | + # Export into the current shell (so callers that `source` this script get the |
| 129 | + # vars) and append to $GITHUB_ENV (so later workflow steps inherit them too). |
| 130 | + echo "$1" |
| 131 | + export "${1?}" |
| 132 | + if [[ -n "${GITHUB_ENV:-}" ]]; then echo "$1" >> "${GITHUB_ENV}"; fi |
| 133 | +} |
| 134 | +echo "=== Xtensa env for backend '${BACKEND}' (core ${CORE_NAME}) ===" |
| 135 | +emit "XTENSA_TOOLCHAIN=${TOOLS_ROOT}" |
| 136 | +emit "TOOLCHAIN_VER=${TOOLCHAIN_VER}" |
| 137 | +emit "XTENSA_SYSTEM=${REGISTRY_ROOT}" |
| 138 | +emit "XTENSA_CORE=${CORE_NAME}" |
| 139 | +emit "XTENSAD_LICENSE_FILE=${LICENSE_FILE}" |
| 140 | +emit "CADENCE_OPT_FLAG=${OPT_FLAG}" |
| 141 | +if [[ -n "${GITHUB_PATH:-}" ]]; then |
| 142 | + echo "${TOOLCHAIN_HOME}/bin" >> "${GITHUB_PATH}" |
| 143 | +fi |
| 144 | +export PATH="${TOOLCHAIN_HOME}/bin:${PATH}" |
| 145 | + |
| 146 | +echo "=== sanity ===" |
| 147 | +xt-clang --version 2>&1 | head -1 |
| 148 | +xt-run --show-config=cores 2>&1 | sed -n '/available/,/registry/p' | head -6 |
| 149 | +echo "Xtensa toolchain ready for ${BACKEND}." |
0 commit comments