Skip to content

Commit ab61a38

Browse files
committed
[executorch] Refactor install_flatc.sh
Fixes various problems I ran into while running it on my system. - Sets up the cmake cache for flatc so it can build - Doesn't look at stderr when examining the flatc path - Runs properly even when the working directory is not at the root of the tree - Uses `echo -e` to print colors correctly - Use double quotes and braces on all variables - Add strict error and undefined var checking - Refactor as a set of functions instead of running at the top level Differential Revision: [D49897273](https://our.internmc.facebook.com/intern/diff/D49897273/) ghstack-source-id: 202829279 Pull Request resolved: #606
1 parent fdaf6fa commit ab61a38

File tree

1 file changed

+72
-45
lines changed

1 file changed

+72
-45
lines changed

build/install_flatc.sh

Lines changed: 72 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -5,54 +5,81 @@
55
# This source code is licensed under the BSD-style license found in the
66
# LICENSE file in the root directory of this source tree.
77

8-
# This file is meant to build flatc from the `third-party/flatbuffers` folder and help
9-
# users to ensure it's installed correctly
8+
# This file builds the `flatc` commandline tool from the
9+
# `third-party/flatbuffers` directory and help users install it correctly.
1010

11-
ROOT=$(cd -- "$(dirname "$(dirname -- "${BASH_SOURCE[0]}")")" &> /dev/null && pwd)
12-
FLATBUFFERS_PATH="$ROOT/third-party/flatbuffers"
11+
set -o errexit
12+
set -o nounset
13+
set -o pipefail
1314

14-
# Get the flatbuffers version from the git submodule
15-
get_flatbuffers_version(){
16-
pushd "$FLATBUFFERS_PATH" || exit
17-
FLATBUFFERS_VERSION=$(git describe --tags "$(git rev-list --tags --max-count=1)" | sed 's/^v//')
18-
popd || exit
15+
EXECUTORCH_ROOT="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)"
16+
readonly EXECUTORCH_ROOT
17+
18+
readonly FLATBUFFERS_PATH="${EXECUTORCH_ROOT}/third-party/flatbuffers"
19+
readonly BUILD_DIR="${FLATBUFFERS_PATH}/cmake-out"
20+
readonly BUILT_FLATC="${BUILD_DIR}/flatc"
21+
22+
# Must use "echo -e" to expand these escape sequences.
23+
readonly GREEN="\033[0;32m" # GREEN Color
24+
readonly RED="\033[0;31m" # Red Color
25+
readonly NC="\033[0m" # No Color
26+
27+
# Prints the flatbuffers version of the git submodule.
28+
print_flatbuffers_version(){
29+
pushd "${FLATBUFFERS_PATH}" > /dev/null
30+
git describe --tags "$(git rev-list --tags --max-count=1)" | sed 's/^v//'
31+
popd > /dev/null
1932
}
2033

21-
FLATC_PATH="$(which flatc 2>&1)"
22-
23-
GREEN='\033[0;32m' # GREEN Color
24-
RED='\033[0;31m' # Red Color
25-
NC='\033[0m' # No Color
26-
27-
get_flatbuffers_version
28-
echo "flatbuffers version in $FLATBUFFERS_PATH is: $FLATBUFFERS_VERSION"
29-
30-
# Warn the users to uninstall flatc from conda
31-
if [[ "$FLATC_PATH" == *miniconda3* ]]; then
32-
echo "${RED}From the flatc path: $FLATC_PATH, it seems it's installed with conda. The flatc from conda is problematic and please avoid using it."
33-
echo "Please run the following lines to remove it: "
34-
echo " conda uninstall flatbuffers"
35-
exit
36-
fi
37-
38-
# Check the following:
39-
# 1. the path to the flatc command if it exists in the system's PATH
40-
# 2. execute the flatc command and check the version
41-
if { command -v flatc; } && { flatc --version | grep "$FLATBUFFERS_VERSION" &> /dev/null; }; then
42-
echo "${GREEN}flatc is installed successfully and ready to use. ${NC}"
43-
else
44-
# Check the existence of flatc in flatbuffers path, if it exists, instruct users to export it to path
45-
if [[ -f "$FLATBUFFERS_PATH/flatc" ]]; then
46-
echo "${RED}flatc is built in $FLATBUFFERS_PATH/flatc but hasn't added to path, run following lines: "
47-
echo " export PATH=$FLATBUFFERS_PATH:\$PATH ${NC}"
34+
main() {
35+
local flatbuffers_version
36+
flatbuffers_version="$(print_flatbuffers_version)"
37+
echo "Version of ${FLATBUFFERS_PATH} is ${flatbuffers_version}"
38+
39+
local flatc_path
40+
flatc_path="$(which flatc 2>/dev/null || /bin/true)"
41+
if [[ -f "${flatc_path}" ]]; then
42+
# A flatc is already on the PATH.
43+
if { "${flatc_path}" --version | grep -q "${flatbuffers_version}"; }; then
44+
echo -e "${GREEN}A compatible version of flatc is on the PATH" \
45+
"and ready to use.${NC}"
46+
return 0
47+
else
48+
echo -e "${RED}WARNING: An incompatible version of flatc" \
49+
"is on the PATH at ${flatc_path}."
50+
echo -e " Required version: flatc version ${flatbuffers_version}"
51+
echo -e " Actual version: $("${flatc_path}" --version)${NC}"
52+
53+
if [[ "${flatc_path}" == *miniconda* ]]; then
54+
echo -e "${RED}ERROR: ${flatc_path} appears to be installed" \
55+
"with conda, which can cause consistency problems."
56+
echo -e "Please run the following command to remove it: "
57+
echo -e " conda uninstall flatbuffers${NC}"
58+
return 1
59+
fi
60+
61+
# Continue to build a compatible version.
62+
fi
63+
fi
64+
65+
if [[ -f "${BUILT_FLATC}" ]]; then
66+
echo -e "${BUILT_FLATC} is already built."
4867
else
49-
# flatc is not in flatbuffers path, build it and instruct users to export it to path
50-
pushd "$FLATBUFFERS_PATH" || exit
51-
cmake --build . --target flatc
52-
echo "flatc path: $FLATBUFFERS_PATH/flatc"
53-
popd || exit
54-
echo "${RED}Finished building flatc. Run the following lines to add it to PATH, then re-run this script:"
55-
echo " export PATH=\"$FLATBUFFERS_PATH:\$PATH\" "
56-
echo " bash build/install_flatc.sh${NC}"
68+
# Build the tool if not already built.
69+
echo "Building flatc under ${FLATBUFFERS_PATH}..."
70+
# Generate cache.
71+
(rm -rf "${BUILD_DIR}" && mkdir "${BUILD_DIR}" && cd "${BUILD_DIR}" && cmake ..)
72+
# Build.
73+
(cd "${FLATBUFFERS_PATH}" && cmake --build "${BUILD_DIR}" --target flatc -j9)
74+
75+
echo -e "Finished building ${BUILT_FLATC}."
5776
fi
58-
fi
77+
78+
echo -e ""
79+
echo -e "***** Run the following commands to add a compatible flatc"\
80+
"to the PATH and re-run this script:"
81+
echo -e " ${RED}export PATH=\"${BUILD_DIR}:\${PATH}\""
82+
echo -e " bash ${EXECUTORCH_ROOT}/build/install_flatc.sh${NC}"
83+
}
84+
85+
main "$@"

0 commit comments

Comments
 (0)