xref: /aosp_15_r20/external/executorch/build/install_flatc.sh (revision 523fa7a60841cd1ecfb9cc4201f1ca8b03ed023a)
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# This file builds the `flatc` commandline tool from the
9# `third-party/flatbuffers` directory and help users install it correctly.
10
11set -o errexit
12set -o nounset
13set -o pipefail
14
15EXECUTORCH_ROOT="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)"
16readonly EXECUTORCH_ROOT
17
18readonly FLATBUFFERS_PATH="${EXECUTORCH_ROOT}/third-party/flatbuffers"
19readonly BUILD_DIR="${FLATBUFFERS_PATH}/cmake-out"
20readonly BUILT_FLATC="${BUILD_DIR}/flatc"
21
22# Must use "echo -e" to expand these escape sequences.
23readonly GREEN="\033[0;32m" # GREEN Color
24readonly RED="\033[0;31m" # Red Color
25readonly NC="\033[0m" # No Color
26
27# Prints the flatbuffers version of the git submodule.
28print_flatbuffers_version(){
29    local version_file="${FLATBUFFERS_PATH}/package.json"
30    local version
31    # Extract the version from the first line like `"version": "23.5.26",`
32    # First remove the final double quote, then remove everything
33    # before the now-final double quote.
34    version="$(
35        grep '"version"\s*:' "${version_file}" \
36        | head -1 \
37        | sed -e 's/"[^"]*$//' \
38        | sed -e 's/.*"//'
39        )"
40    if [[ ${version} =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
41        echo "${version}"
42    else
43        echo "ERROR: Bad version '${version}'; could not find version in ${version_file}" >&2
44        exit 1
45    fi
46}
47
48main() {
49    local flatbuffers_version
50    flatbuffers_version="$(print_flatbuffers_version)"
51    echo "Version of ${FLATBUFFERS_PATH} is ${flatbuffers_version}"
52
53    local flatc_path
54    flatc_path="$(which flatc 2>/dev/null || echo '')"
55    if [[ -f "${flatc_path}" ]]; then
56        # A flatc is already on the PATH.
57        if { "${flatc_path}" --version | grep -q "${flatbuffers_version}"; }; then
58            echo -e "${GREEN}A compatible version of flatc is on the PATH" \
59                "and ready to use.${NC}"
60            return 0
61        else
62            echo -e "${RED}WARNING: An incompatible version of flatc" \
63                "is on the PATH at ${flatc_path}."
64            echo -e "  Required version: flatc version ${flatbuffers_version}"
65            echo -e "  Actual version: $("${flatc_path}" --version)${NC}"
66
67            if [[ "${flatc_path}" == *miniconda* ]]; then
68                echo -e "${RED}ERROR: ${flatc_path} appears to be installed" \
69                    "with conda, which can cause consistency problems."
70                echo -e "Please run the following command to remove it: "
71                echo -e "  conda uninstall flatbuffers${NC}"
72                return 1
73            fi
74
75            # Continue to build a compatible version.
76        fi
77    fi
78
79    if [[ -f "${BUILT_FLATC}" ]]; then
80        echo -e "${BUILT_FLATC} is already built."
81    else
82        # Build the tool if not already built.
83        echo "Building flatc under ${FLATBUFFERS_PATH}..."
84        # Generate cache.
85        (rm -rf "${BUILD_DIR}" && mkdir "${BUILD_DIR}" && cd "${BUILD_DIR}" && cmake -DCMAKE_BUILD_TYPE=Release ..)
86        # Build.
87        (cd "${FLATBUFFERS_PATH}" && cmake --build "${BUILD_DIR}" --target flatc -j9)
88
89        echo -e "Finished building ${BUILT_FLATC}."
90    fi
91
92    echo -e ""
93    echo -e "***** Run the following commands to add a compatible flatc"\
94        "to the PATH and re-run this script:"
95    echo -e "  ${RED}export PATH=\"${BUILD_DIR}:\${PATH}\""
96    echo -e "  bash ${EXECUTORCH_ROOT}/build/install_flatc.sh${NC}"
97}
98
99main "$@"
100