Skip to content

Commit f4d7e90

Browse files
dcpleungstephanosio
authored andcommitted
contrib: bash script to build one toolchain locally on Linux
A very primitive script to build one toolchain locally on Linux. Signed-off-by: Daniel Leung <[email protected]>
1 parent 15daabb commit f4d7e90

File tree

2 files changed

+148
-3
lines changed

2 files changed

+148
-3
lines changed

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,8 @@ automatically build and test the Zephyr SDK with the changes in the pull
5151
request and upload the binaries to the pull request check run, which you can
5252
download for further local testing as necessary.
5353

54-
Locally building the Zephyr SDK is currently not supported because setting up
55-
the environment to do so is highly complex and the resource requirements far
56-
exceed what is found on common developer machines.
54+
To aid in verifying changes and introduction of a new toolchain, a helper script,
55+
contrib/linux_build_toolchain.sh, can be used to build one toolchain under Linux.
5756

5857
### Workflow to Test Patches with Zephyr SDK
5958

contrib/linux_build_toolchain.sh

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
#!/usr/bin/env bash
2+
3+
# Script to mimic how CI is building individual toolchain.
4+
5+
# Default is to use current directory as the working directory.
6+
# SDK is under ${WORKING_ROOT_DIR}/sdk-ng/
7+
# Build directory is ${WORKING_ROOT_DIR}/build-workspace/
8+
WORKING_ROOT_DIR="${PWD}"
9+
SDK_DIR="${WORKING_ROOT_DIR}/sdk-ng"
10+
BUILD_DIR="${WORKING_ROOT_DIR}/build-workspace"
11+
12+
function usage()
13+
{
14+
echo "Usage: $0 -c <config file name> [-s SDK directory] [-o Build directory] [-h]"
15+
echo ""
16+
echo "Arguments:"
17+
echo ""
18+
echo " -c Name of config file (without extension .config)"
19+
echo ""
20+
echo " -s SDK directory. Optional."
21+
echo " Default is ${GITHUB_WORKSPACE}"
22+
echo ""
23+
echo " -o Build directory, for building artifacts and final output. Optional."
24+
echo " Default is ${WORKSPACE}"
25+
echo ""
26+
echo " -h This help."
27+
echo ""
28+
}
29+
30+
while getopts "hc:s:o:" ARGS; do
31+
case ${ARGS} in
32+
h)
33+
usage
34+
exit 1
35+
;;
36+
c)
37+
CONFIG_FILE_NAME="${OPTARG}"
38+
;;
39+
s)
40+
SDK_DIR="${OPTARG}"
41+
;;
42+
o)
43+
BUILD_DIR="${OPTARG}"
44+
;;
45+
*)
46+
echo "Unknown arguments: $1"
47+
exit 1
48+
;;
49+
esac
50+
done
51+
52+
if [[ "${CONFIG_FILE_NAME}" == "" ]]; then
53+
usage
54+
exit 1
55+
fi
56+
57+
# These variables are the same as the CI script.
58+
export GITHUB_WORKSPACE="${SDK_DIR}"
59+
export WORKSPACE="${BUILD_DIR}"
60+
61+
export CT_PREFIX="${WORKSPACE}"/output
62+
export CT_NG="${WORKSPACE}"/crosstool-ng/bin/ct-ng
63+
64+
TOOLCHAIN_OUTPUT_DIR="${WORKSPACE}"/output/${CONFIG_FILE_NAME}
65+
66+
echo "SDK directory: ${GITHUB_WORKSPACE}"
67+
echo "Build directory: ${WORKSPACE}"
68+
69+
SDK_CONFIG_FILE="${GITHUB_WORKSPACE}/configs/${CONFIG_FILE_NAME}.config"
70+
71+
if [[ ! -f "${SDK_CONFIG_FILE}" ]]; then
72+
echo "ERROR: ${SDK_CONFIG_FILE} does not exist!"
73+
exit 1
74+
fi
75+
76+
echo "Build config file: ${SDK_CONFIG_FILE}"
77+
echo "Toolchain output directory: ${TOOLCHAIN_OUTPUT_DIR}"
78+
79+
if [[ ! -x "${WORKSPACE}/crosstool-ng/bin/ct-ng" ]]; then
80+
echo "========== Building ct-ng =========="
81+
82+
# Create build directory
83+
pushd "${WORKSPACE}" || exit 1
84+
mkdir -p crosstool-ng-build
85+
cd crosstool-ng-build || exit 1
86+
87+
# Bootstrap crosstool-ng
88+
pushd "${GITHUB_WORKSPACE}"/crosstool-ng || exit 1
89+
./bootstrap
90+
popd || exit 1
91+
92+
# Build and install crosstool-ng
93+
"${GITHUB_WORKSPACE}"/crosstool-ng/configure --prefix="${WORKSPACE}"/crosstool-ng
94+
make
95+
make install
96+
97+
popd || exit 1
98+
99+
${CT_NG} version
100+
fi
101+
102+
echo "========== Building Toolchain for ${CONFIG_FILE_NAME} =========="
103+
104+
BUILD_DIR="${WORKSPACE}"/build-${CONFIG_FILE_NAME}
105+
106+
pushd "${WORKSPACE}" || exit 1
107+
108+
mkdir -p "${BUILD_DIR}"
109+
pushd "${BUILD_DIR}" || exit 1
110+
111+
cat "${GITHUB_WORKSPACE}"/configs/common.config "${SDK_CONFIG_FILE}" > .config
112+
113+
cat <<EOF >> .config
114+
CT_SHOW_CT_VERSION=n
115+
CT_LOCAL_TARBALLS_DIR="\$\{WORKSPACE\}/sources"
116+
CT_OVERLAY_LOCATION="\$\{GITHUB_WORKSPACE\}/overlays"
117+
CT_LOG_PROGRESS_BAR=n
118+
CT_LOG_EXTRA=y
119+
CT_LOG_LEVEL_MAX="EXTRA"
120+
CT_GDB_CROSS_PYTHON=y
121+
CT_GDB_CROSS_PYTHON_VARIANT=y
122+
CT_GDB_CROSS_PYTHON_BINARY="python3.8"
123+
CT_EXPERIMENTAL=y
124+
CT_ALLOW_BUILD_AS_ROOT=y
125+
CT_ALLOW_BUILD_AS_ROOT_SURE=y
126+
EOF
127+
128+
"${CT_NG}" savedefconfig DEFCONFIG=build.config
129+
"${CT_NG}" distclean
130+
"${CT_NG}" defconfig DEFCONFIG=build.config
131+
132+
"${CT_NG}" build
133+
134+
chmod -R u+w "${TOOLCHAIN_OUTPUT_DIR}"
135+
136+
pushd "${TOOLCHAIN_OUTPUT_DIR}" || exit 1
137+
rm -rf newlib-nano
138+
rm -f build.log.bz2
139+
popd || exit 1
140+
141+
popd || exit 1
142+
143+
popd || exit 1
144+
145+
echo "========== Done =========="
146+
echo "Toolchain is available under: ${TOOLCHAIN_OUTPUT_DIR}"

0 commit comments

Comments
 (0)