-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbuild.sh
More file actions
executable file
·151 lines (129 loc) · 4.37 KB
/
Copy pathbuild.sh
File metadata and controls
executable file
·151 lines (129 loc) · 4.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/bin/bash
# Copyright (c) Joby Aviation 2022
# Original authors: Thulio Ferraz Assis (thulio@aspect.dev), Aspect.dev
#
# Copyright (c) Thulio Ferraz Assis 2024-2025
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
usage() {
echo "Usage: $0 <gcc_version> <arch> <output_dir> [host]"
echo ""
echo " gcc_version GCC version to build"
echo " arch Target architecture (x86_64, armv7, aarch64)"
echo " output_dir Directory where the toolchain archive will be saved"
echo " host Host where the toolchain will run on (x86_64, aarch64, default: x86_64)"
echo ""
echo "Examples:"
echo " $0 14.3.0 x86_64 ."
echo " $0 13.2.0 aarch64 ."
echo " $0 13.2.0 aarch64 . aarch64"
echo ""
echo "Output format: gcc-toolchain-{gcc_version}-{arch}.tar.xz"
echo " gcc-toolchain-{gcc_version}-{arch}-host-{host}.tar.xz (when host != x86_64)"
}
readonly gcc_version=$1
readonly arch=$2
readonly output_dir=$3
readonly DEFAULT_HOST="x86_64"
readonly host="${4:-$DEFAULT_HOST}"
set -o errexit -o nounset -o pipefail
# Handle help flag
if [[ "${1:-}" == "-h" || "${1:-}" == "--help" ]]; then
usage
exit 0
fi
if [ -z "${gcc_version}" ]; then
>&2 echo "ERROR: the first argument of the script must be the GCC version."
>&2 echo ""
usage
exit 1
fi
if [ -z "${arch}" ]; then
>&2 echo "ERROR: the second argument of the script must be the architecture."
>&2 echo ""
usage
exit 1
fi
if [ -z "${output_dir}" ]; then
>&2 echo "ERROR: the third argument of the script must be the output directory."
>&2 echo ""
usage
exit 1
fi
# Validate architecture
case "${arch}" in
x86_64|armv7|aarch64)
;;
*)
>&2 echo "ERROR: unsupported architecture '${arch}'. Supported architectures: x86_64, armv7, aarch64"
>&2 echo ""
usage
exit 1
;;
esac
case "${host}" in
x86_64|aarch64)
;;
*)
>&2 echo "ERROR: unsupported host '${host}'. Supported host architectures: x86_64, aarch64"
>&2 echo ""
usage
exit 1
;;
esac
echo "INFO: Building GCC ${gcc_version} toolchain for ${arch} architecture and ${host}..."
# Archives for the default host keep the historical unqualified name; only non-default
# hosts get the -host-{host} suffix. This avoids shipping duplicate archives.
if [[ "${host}" == "${DEFAULT_HOST}" ]]; then
output_filename="gcc-toolchain-${gcc_version}-${arch}.tar.xz"
else
output_filename="gcc-toolchain-${gcc_version}-${arch}-host-${host}.tar.xz"
fi
container_source_dir="/var/builds/toolchain"
echo "INFO: building toolchain inside container..."
project_dir="$(git rev-parse --show-toplevel)"
build_dir="${project_dir}"
output=$(realpath "${output_dir}/${output_filename}")
image_tag=$(tr '[:upper:]' '[:lower:]' <<<"${arch}-host-${host}")
(cd "${build_dir}"; \
docker build \
--build-arg ARCH="${arch}" \
--build-arg GCC_VERSION="${gcc_version}" \
--build-arg HOST_ARCH="${host}" \
--tag "${image_tag}" \
--target toolchain \
.)
echo "INFO: exporting toolchain to '${output}'..."
tmpdir="${build_dir}/.tmpdir"
function remove_tmpdir {
rm -rf "${tmpdir}"
}
trap remove_tmpdir EXIT
mkdir --parents "${tmpdir}"
container_id="$(docker create "${image_tag}")"
function remove_container {
docker rm "${container_id}"
remove_tmpdir
}
trap remove_container EXIT
docker cp "${container_id}:${container_source_dir}" "${tmpdir}"
readonly os_name="$(uname -s)"
if [[ "${os_name}" == "Linux" ]]; then
readonly cpus="$(nproc --all)"
elif [[ "${os_name}" == "Darwin" ]]; then
readonly cpus="$(sysctl -n hw.ncpu)"
fi
source_dir_name=$(basename "${container_source_dir}")
(cd "${tmpdir}/${source_dir_name}"; tar --create --file /dev/stdout . | XZ_DEFAULTS="--threads ${cpus}" xz -5 > "${output}")
shasum -a 256 "${output}"
echo "INFO: Successfully created ${output_filename}"