yann@1
|
1 |
# This file contains some usefull common functions
|
yann@1
|
2 |
# Copyright 2007 Yann E. MORIN
|
yann@1
|
3 |
# Licensed under the GPL v2. See COPYING in the root of this package
|
yann@1
|
4 |
|
yann@136
|
5 |
# Prepare the fault handler
|
yann@1
|
6 |
CT_OnError() {
|
yann@1
|
7 |
ret=$?
|
yann@726
|
8 |
# Bail out early in subshell, the upper level shell will act accordingly.
|
yann@726
|
9 |
[ ${BASH_SUBSHELL} -eq 0 ] || exit $ret
|
yann@523
|
10 |
CT_DoLog ERROR "Build failed in step '${CT_STEP_MESSAGE[${CT_STEP_COUNT}]}'"
|
yann@1
|
11 |
for((step=(CT_STEP_COUNT-1); step>1; step--)); do
|
yann@523
|
12 |
CT_DoLog ERROR " called in step '${CT_STEP_MESSAGE[${step}]}'"
|
yann@1
|
13 |
done
|
yann@523
|
14 |
CT_DoLog ERROR "Error happened in '${BASH_SOURCE[1]}' in function '${FUNCNAME[1]}' (line unknown, sorry)"
|
yann@1
|
15 |
for((depth=2; ${BASH_LINENO[$((${depth}-1))]}>0; depth++)); do
|
yann@523
|
16 |
CT_DoLog ERROR " called from '${BASH_SOURCE[${depth}]}' at line # ${BASH_LINENO[${depth}-1]} in function '${FUNCNAME[${depth}]}'"
|
yann@1
|
17 |
done
|
yann@523
|
18 |
[ "${CT_LOG_TO_FILE}" = "y" ] && CT_DoLog ERROR "Look at '${CT_LOG_FILE}' for more info on this error."
|
yann@96
|
19 |
CT_STEP_COUNT=1
|
yann@96
|
20 |
CT_DoEnd ERROR
|
yann@1
|
21 |
exit $ret
|
yann@1
|
22 |
}
|
yann@136
|
23 |
|
yann@136
|
24 |
# Install the fault handler
|
yann@1
|
25 |
trap CT_OnError ERR
|
yann@1
|
26 |
|
yann@136
|
27 |
# Inherit the fault handler in subshells and functions
|
yann@1
|
28 |
set -E
|
yann@136
|
29 |
|
yann@136
|
30 |
# Make pipes fail on the _first_ failed command
|
yann@136
|
31 |
# Not supported on bash < 3.x, but we need it, so drop the obsoleting bash-2.x
|
yann@1
|
32 |
set -o pipefail
|
yann@1
|
33 |
|
yann@136
|
34 |
# Don't hash commands' locations, and search every time it is requested.
|
yann@136
|
35 |
# This is slow, but needed because of the static/shared core gcc which shall
|
yann@136
|
36 |
# always match to shared if it exists, and only fallback to static if the
|
yann@136
|
37 |
# shared is not found
|
yann@136
|
38 |
set +o hashall
|
yann@136
|
39 |
|
yann@165
|
40 |
# Log policy:
|
yann@165
|
41 |
# - first of all, save stdout so we can see the live logs: fd #6
|
yann@165
|
42 |
exec 6>&1
|
yann@165
|
43 |
# - then point stdout to the log file (temporary for now)
|
yann@165
|
44 |
tmp_log_file="${CT_TOP_DIR}/log.$$"
|
yann@165
|
45 |
exec >>"${tmp_log_file}"
|
yann@165
|
46 |
|
yann@1
|
47 |
# The different log levels:
|
yann@1
|
48 |
CT_LOG_LEVEL_ERROR=0
|
yann@1
|
49 |
CT_LOG_LEVEL_WARN=1
|
yann@1
|
50 |
CT_LOG_LEVEL_INFO=2
|
yann@1
|
51 |
CT_LOG_LEVEL_EXTRA=3
|
yann@1
|
52 |
CT_LOG_LEVEL_DEBUG=4
|
yann@78
|
53 |
CT_LOG_LEVEL_ALL=5
|
yann@1
|
54 |
|
yann@1083
|
55 |
# Make it easy to use \n and !
|
yann@1081
|
56 |
CR=$(printf "\n")
|
yann@1083
|
57 |
BANG='!'
|
yann@1081
|
58 |
|
yann@1
|
59 |
# A function to log what is happening
|
yann@1
|
60 |
# Different log level are available:
|
yann@1
|
61 |
# - ERROR: A serious, fatal error occurred
|
yann@1
|
62 |
# - WARN: A non fatal, non serious error occurred, take your responsbility with the generated build
|
yann@1
|
63 |
# - INFO: Informational messages
|
yann@1
|
64 |
# - EXTRA: Extra informational messages
|
yann@1
|
65 |
# - DEBUG: Debug messages
|
yann@78
|
66 |
# - ALL: Component's build messages
|
yann@1
|
67 |
# Usage: CT_DoLog <level> [message]
|
yann@1
|
68 |
# If message is empty, then stdin will be logged.
|
yann@1
|
69 |
CT_DoLog() {
|
yann@47
|
70 |
local max_level LEVEL level cur_l cur_L
|
yann@47
|
71 |
local l
|
yann@1
|
72 |
eval max_level="\${CT_LOG_LEVEL_${CT_LOG_LEVEL_MAX}}"
|
yann@1
|
73 |
# Set the maximum log level to DEBUG if we have none
|
yann@78
|
74 |
[ -z "${max_level}" ] && max_level=${CT_LOG_LEVEL_DEBUG}
|
yann@1
|
75 |
|
yann@47
|
76 |
LEVEL="$1"; shift
|
yann@1
|
77 |
eval level="\${CT_LOG_LEVEL_${LEVEL}}"
|
yann@1
|
78 |
|
yann@1
|
79 |
if [ $# -eq 0 ]; then
|
yann@1
|
80 |
cat -
|
yann@1
|
81 |
else
|
yann@646
|
82 |
echo "${@}"
|
yann@1081
|
83 |
fi |( IFS="${CR}" # We want the full lines, even leading spaces
|
yann@523
|
84 |
_prog_bar_cpt=0
|
yann@523
|
85 |
_prog_bar[0]='/'
|
yann@523
|
86 |
_prog_bar[1]='-'
|
yann@523
|
87 |
_prog_bar[2]='\'
|
yann@523
|
88 |
_prog_bar[3]='|'
|
yann@1
|
89 |
indent=$((2*CT_STEP_COUNT))
|
yann@1
|
90 |
while read line; do
|
yann@47
|
91 |
case "${CT_LOG_SEE_TOOLS_WARN},${line}" in
|
yann@47
|
92 |
y,*"warning:"*) cur_L=WARN; cur_l=${CT_LOG_LEVEL_WARN};;
|
yann@112
|
93 |
y,*"WARNING:"*) cur_L=WARN; cur_l=${CT_LOG_LEVEL_WARN};;
|
yann@47
|
94 |
*"error:"*) cur_L=ERROR; cur_l=${CT_LOG_LEVEL_ERROR};;
|
yann@919
|
95 |
*"make["*"]: *** ["*) cur_L=ERROR; cur_l=${CT_LOG_LEVEL_ERROR};;
|
yann@47
|
96 |
*) cur_L="${LEVEL}"; cur_l="${level}";;
|
yann@47
|
97 |
esac
|
yann@523
|
98 |
# There will always be a log file (stdout, fd #1), be it /dev/null
|
yann@523
|
99 |
printf "[%-5s]%*s%s%s\n" "${cur_L}" "${indent}" " " "${line}"
|
yann@47
|
100 |
if [ ${cur_l} -le ${max_level} ]; then
|
yann@523
|
101 |
# Only print to console (fd #6) if log level is high enough.
|
yann@523
|
102 |
printf "\r[%-5s]%*s%s%s\n" "${cur_L}" "${indent}" " " "${line}" >&6
|
yann@82
|
103 |
fi
|
yann@82
|
104 |
if [ "${CT_LOG_PROGRESS_BAR}" = "y" ]; then
|
yann@523
|
105 |
printf "\r[%02d:%02d] %s " $((SECONDS/60)) $((SECONDS%60)) "${_prog_bar[$((_prog_bar_cpt/10))]}" >&6
|
yann@523
|
106 |
_prog_bar_cpt=$(((_prog_bar_cpt+1)%40))
|
yann@1
|
107 |
fi
|
yann@1
|
108 |
done
|
yann@1
|
109 |
)
|
yann@1
|
110 |
|
yann@1
|
111 |
return 0
|
yann@1
|
112 |
}
|
yann@1
|
113 |
|
yann@535
|
114 |
# Execute an action, and log its messages
|
yann@535
|
115 |
# Usage: CT_DoExecLog <level> <[VAR=val...] command [parameters...]>
|
yann@535
|
116 |
CT_DoExecLog() {
|
yann@535
|
117 |
local level="$1"
|
yann@535
|
118 |
shift
|
yann@657
|
119 |
CT_DoLog DEBUG "==> Executing: '${@}'"
|
yann@668
|
120 |
"${@}" 2>&1 |CT_DoLog "${level}"
|
yann@535
|
121 |
}
|
yann@535
|
122 |
|
yann@96
|
123 |
# Tail message to be logged whatever happens
|
yann@96
|
124 |
# Usage: CT_DoEnd <level>
|
yann@96
|
125 |
CT_DoEnd()
|
yann@96
|
126 |
{
|
yann@145
|
127 |
local level="$1"
|
yann@523
|
128 |
CT_STOP_DATE=$(CT_DoDate +%s%N)
|
yann@523
|
129 |
CT_STOP_DATE_HUMAN=$(CT_DoDate +%Y%m%d.%H%M%S)
|
yann@599
|
130 |
if [ "${level}" != "ERROR" ]; then
|
yann@582
|
131 |
CT_DoLog "${level:-INFO}" "Build completed at ${CT_STOP_DATE_HUMAN}"
|
yann@582
|
132 |
fi
|
yann@96
|
133 |
elapsed=$((CT_STOP_DATE-CT_STAR_DATE))
|
yann@96
|
134 |
elapsed_min=$((elapsed/(60*1000*1000*1000)))
|
yann@523
|
135 |
elapsed_sec=$(printf "%02d" $(((elapsed%(60*1000*1000*1000))/(1000*1000*1000))))
|
yann@523
|
136 |
elapsed_csec=$(printf "%02d" $(((elapsed%(1000*1000*1000))/(10*1000*1000))))
|
yann@145
|
137 |
CT_DoLog ${level:-INFO} "(elapsed: ${elapsed_min}:${elapsed_sec}.${elapsed_csec})"
|
yann@96
|
138 |
}
|
yann@96
|
139 |
|
yann@96
|
140 |
# Abort the execution with an error message
|
yann@1
|
141 |
# Usage: CT_Abort <message>
|
yann@1
|
142 |
CT_Abort() {
|
yann@128
|
143 |
CT_DoLog ERROR "$1"
|
yann@1
|
144 |
exit 1
|
yann@1
|
145 |
}
|
yann@1
|
146 |
|
yann@1
|
147 |
# Test a condition, and print a message if satisfied
|
yann@1
|
148 |
# Usage: CT_Test <message> <tests>
|
yann@1
|
149 |
CT_Test() {
|
yann@1
|
150 |
local ret
|
yann@1
|
151 |
local m="$1"
|
yann@1
|
152 |
shift
|
yann@1
|
153 |
test "$@" && CT_DoLog WARN "$m"
|
yann@1
|
154 |
return 0
|
yann@1
|
155 |
}
|
yann@1
|
156 |
|
yann@1
|
157 |
# Test a condition, and abort with an error message if satisfied
|
yann@1
|
158 |
# Usage: CT_TestAndAbort <message> <tests>
|
yann@1
|
159 |
CT_TestAndAbort() {
|
yann@1
|
160 |
local m="$1"
|
yann@1
|
161 |
shift
|
yann@1
|
162 |
test "$@" && CT_Abort "$m"
|
yann@1
|
163 |
return 0
|
yann@1
|
164 |
}
|
yann@1
|
165 |
|
yann@1
|
166 |
# Test a condition, and abort with an error message if not satisfied
|
yann@1
|
167 |
# Usage: CT_TestAndAbort <message> <tests>
|
yann@1
|
168 |
CT_TestOrAbort() {
|
yann@1
|
169 |
local m="$1"
|
yann@1
|
170 |
shift
|
yann@1
|
171 |
test "$@" || CT_Abort "$m"
|
yann@1
|
172 |
return 0
|
yann@1
|
173 |
}
|
yann@1
|
174 |
|
yann@1
|
175 |
# Test the presence of a tool, or abort if not found
|
yann@1
|
176 |
# Usage: CT_HasOrAbort <tool>
|
yann@1
|
177 |
CT_HasOrAbort() {
|
yann@773
|
178 |
CT_TestAndAbort "'${1}' not found and needed for successful toolchain build." -z "$(CT_Which "${1}")"
|
yann@1
|
179 |
return 0
|
yann@1
|
180 |
}
|
yann@1
|
181 |
|
yann@210
|
182 |
# Search a program: wrap "which" for those system where
|
yann@210
|
183 |
# "which" verbosely says there is no match (Mdk are such
|
yann@210
|
184 |
# suckers...)
|
yann@210
|
185 |
# Usage: CT_Which <filename>
|
yann@210
|
186 |
CT_Which() {
|
yann@210
|
187 |
which "$1" 2>/dev/null || true
|
yann@210
|
188 |
}
|
yann@210
|
189 |
|
yann@1
|
190 |
# Get current date with nanosecond precision
|
yann@1
|
191 |
# On those system not supporting nanosecond precision, faked with rounding down
|
yann@1
|
192 |
# to the highest entire second
|
yann@1
|
193 |
# Usage: CT_DoDate <fmt>
|
yann@1
|
194 |
CT_DoDate() {
|
yann@1
|
195 |
date "$1" |sed -r -e 's/%N$/000000000/;'
|
yann@1
|
196 |
}
|
yann@1
|
197 |
|
yann@1
|
198 |
CT_STEP_COUNT=1
|
yann@1
|
199 |
CT_STEP_MESSAGE[${CT_STEP_COUNT}]="<none>"
|
yann@1
|
200 |
# Memorise a step being done so that any error is caught
|
yann@1
|
201 |
# Usage: CT_DoStep <loglevel> <message>
|
yann@1
|
202 |
CT_DoStep() {
|
yann@523
|
203 |
local start=$(CT_DoDate +%s%N)
|
yann@1
|
204 |
CT_DoLog "$1" "================================================================="
|
yann@1
|
205 |
CT_DoLog "$1" "$2"
|
yann@1
|
206 |
CT_STEP_COUNT=$((CT_STEP_COUNT+1))
|
yann@1
|
207 |
CT_STEP_LEVEL[${CT_STEP_COUNT}]="$1"; shift
|
yann@1
|
208 |
CT_STEP_START[${CT_STEP_COUNT}]="${start}"
|
yann@1
|
209 |
CT_STEP_MESSAGE[${CT_STEP_COUNT}]="$1"
|
yann@1
|
210 |
return 0
|
yann@1
|
211 |
}
|
yann@1
|
212 |
|
yann@1
|
213 |
# End the step just being done
|
yann@1
|
214 |
# Usage: CT_EndStep
|
yann@1
|
215 |
CT_EndStep() {
|
yann@523
|
216 |
local stop=$(CT_DoDate +%s%N)
|
yann@523
|
217 |
local duration=$(printf "%032d" $((stop-${CT_STEP_START[${CT_STEP_COUNT}]})) |sed -r -e 's/([[:digit:]]{2})[[:digit:]]{7}$/\.\1/; s/^0+//; s/^\./0\./;')
|
yann@582
|
218 |
local elapsed=$(printf "%02d:%02d" $((SECONDS/60)) $((SECONDS%60)))
|
yann@1
|
219 |
local level="${CT_STEP_LEVEL[${CT_STEP_COUNT}]}"
|
yann@1
|
220 |
local message="${CT_STEP_MESSAGE[${CT_STEP_COUNT}]}"
|
yann@1
|
221 |
CT_STEP_COUNT=$((CT_STEP_COUNT-1))
|
yann@582
|
222 |
CT_DoLog "${level}" "${message}: done in ${duration}s (at ${elapsed})"
|
yann@1
|
223 |
return 0
|
yann@1
|
224 |
}
|
yann@1
|
225 |
|
yann@1
|
226 |
# Pushes into a directory, and pops back
|
yann@1
|
227 |
CT_Pushd() {
|
yann@1
|
228 |
pushd "$1" >/dev/null 2>&1
|
yann@1
|
229 |
}
|
yann@1
|
230 |
CT_Popd() {
|
yann@1
|
231 |
popd >/dev/null 2>&1
|
yann@1
|
232 |
}
|
yann@1
|
233 |
|
yann@1
|
234 |
# Makes a path absolute
|
yann@1
|
235 |
# Usage: CT_MakeAbsolutePath path
|
yann@1
|
236 |
CT_MakeAbsolutePath() {
|
yann@1
|
237 |
# Try to cd in that directory
|
yann@1
|
238 |
if [ -d "$1" ]; then
|
yann@1
|
239 |
CT_Pushd "$1"
|
yann@1
|
240 |
pwd
|
yann@1
|
241 |
CT_Popd
|
yann@1
|
242 |
else
|
yann@1
|
243 |
# No such directory, fail back to guessing
|
yann@1
|
244 |
case "$1" in
|
yann@1
|
245 |
/*) echo "$1";;
|
yann@523
|
246 |
*) echo "$(pwd)/$1";;
|
yann@1
|
247 |
esac
|
yann@1
|
248 |
fi
|
yann@1
|
249 |
|
yann@1
|
250 |
return 0
|
yann@1
|
251 |
}
|
yann@1
|
252 |
|
yann@1
|
253 |
# Creates a temporary directory
|
yann@1
|
254 |
# $1: variable to assign to
|
yann@1
|
255 |
# Usage: CT_MktempDir foo
|
yann@1
|
256 |
CT_MktempDir() {
|
yann@1
|
257 |
# Some mktemp do not allow more than 6 Xs
|
yann@523
|
258 |
eval "$1"=$(mktemp -q -d "${CT_BUILD_DIR}/.XXXXXX")
|
yann@1
|
259 |
CT_TestOrAbort "Could not make temporary directory" -n "${!1}" -a -d "${!1}"
|
yann@787
|
260 |
CT_DoLog DEBUG "Made temporary directory '${!1}'"
|
yann@787
|
261 |
return 0
|
yann@1
|
262 |
}
|
yann@1
|
263 |
|
yann@1
|
264 |
# Echoes the specified string on stdout until the pipe breaks.
|
yann@1
|
265 |
# Doesn't fail
|
yann@1
|
266 |
# $1: string to echo
|
yann@1
|
267 |
# Usage: CT_DoYes "" |make oldconfig
|
yann@1
|
268 |
CT_DoYes() {
|
yann@1
|
269 |
yes "$1" || true
|
yann@1
|
270 |
}
|
yann@63
|
271 |
|
yann@85
|
272 |
# Get the file name extension of a component
|
yann@719
|
273 |
# Usage: CT_GetFileExtension <component_name-component_version> [extension]
|
yann@85
|
274 |
# If found, echoes the extension to stdout
|
yann@85
|
275 |
# If not found, echoes nothing on stdout.
|
yann@85
|
276 |
CT_GetFileExtension() {
|
yann@85
|
277 |
local ext
|
yann@85
|
278 |
local file="$1"
|
yann@719
|
279 |
shift
|
yann@719
|
280 |
local first_ext="$1"
|
yann@85
|
281 |
|
yann@85
|
282 |
CT_Pushd "${CT_TARBALLS_DIR}"
|
yann@160
|
283 |
# we need to also check for an empty extension for those very
|
yann@160
|
284 |
# peculiar components that don't have one (such as sstrip from
|
yann@160
|
285 |
# buildroot).
|
yann@719
|
286 |
for ext in ${first_ext} .tar.gz .tar.bz2 .tgz .tar ''; do
|
yann@85
|
287 |
if [ -f "${file}${ext}" ]; then
|
yann@85
|
288 |
echo "${ext}"
|
yann@85
|
289 |
break
|
yann@85
|
290 |
fi
|
yann@85
|
291 |
done
|
yann@85
|
292 |
CT_Popd
|
yann@85
|
293 |
|
yann@85
|
294 |
return 0
|
yann@85
|
295 |
}
|
yann@85
|
296 |
|
yann@754
|
297 |
# Set environment for proxy access
|
yann@754
|
298 |
# Usage: CT_DoSetProxy <proxy_type>
|
yann@775
|
299 |
# where proxy_type is one of 'http', 'sockssys', 'socks4' or 'socks5',
|
yann@775
|
300 |
# or empty (to not change proxy settings).
|
yann@754
|
301 |
CT_DoSetProxy() {
|
yann@775
|
302 |
case "${1}" in
|
yann@754
|
303 |
http)
|
yann@754
|
304 |
http_proxy="http://"
|
yann@754
|
305 |
case "${CT_PROXY_USER}:${CT_PROXY_PASS}" in
|
yann@754
|
306 |
:) ;;
|
yann@754
|
307 |
:*) http_proxy="${http_proxy}:${CT_PROXY_PASS}@";;
|
yann@754
|
308 |
*:) http_proxy="${http_proxy}${CT_PROXY_USER}@";;
|
yann@754
|
309 |
*:*) http_proxy="${http_proxy}${CT_PROXY_USER}:${CT_PROXY_PASS}@";;
|
yann@754
|
310 |
esac
|
yann@754
|
311 |
export http_proxy="${http_proxy}${CT_PROXY_HOST}:${CT_PROXY_PORT}/"
|
yann@754
|
312 |
export https_proxy="${http_proxy}"
|
yann@754
|
313 |
export ftp_proxy="${http_proxy}"
|
yann@754
|
314 |
CT_DoLog DEBUG "http_proxy='${http_proxy}'"
|
yann@754
|
315 |
;;
|
yann@754
|
316 |
sockssys)
|
yann@754
|
317 |
CT_HasOrAbort tsocks
|
yann@754
|
318 |
. tsocks -on
|
yann@754
|
319 |
;;
|
yann@754
|
320 |
socks*)
|
yann@754
|
321 |
# Remove any lingering config file from any previous run
|
yann@754
|
322 |
rm -f "${CT_BUILD_DIR}/tsocks.conf"
|
yann@754
|
323 |
# Find all interfaces and build locally accessible networks
|
yann@754
|
324 |
server_ip=$(ping -c 1 -W 2 "${CT_PROXY_HOST}" |head -n 1 |sed -r -e 's/^[^\(]+\(([^\)]+)\).*$/\1/;' || true)
|
yann@754
|
325 |
CT_TestOrAbort "SOCKS proxy '${CT_PROXY_HOST}' has no IP." -n "${server_ip}"
|
yann@1017
|
326 |
/sbin/ifconfig |gawk -v server_ip="${server_ip}" '
|
yann@754
|
327 |
BEGIN {
|
yann@754
|
328 |
split( server_ip, tmp, "\\." );
|
yann@754
|
329 |
server_ip_num = tmp[1] * 2^24 + tmp[2] * 2^16 + tmp[3] * 2^8 + tmp[4] * 2^0;
|
yann@754
|
330 |
pairs = 0;
|
yann@754
|
331 |
}
|
yann@754
|
332 |
|
yann@754
|
333 |
$0 ~ /^[[:space:]]*inet addr:/ {
|
yann@754
|
334 |
split( $2, tmp, ":|\\." );
|
yann@754
|
335 |
if( ( tmp[2] == 127 ) && ( tmp[3] == 0 ) && ( tmp[4] == 0 ) && ( tmp[5] == 1 ) ) {
|
yann@754
|
336 |
/* Skip 127.0.0.1, it'\''s taken care of by tsocks itself */
|
yann@754
|
337 |
next;
|
yann@754
|
338 |
}
|
yann@754
|
339 |
ip_num = tmp[2] * 2^24 + tmp[3] * 2^16 + tmp[4] * 2 ^8 + tmp[5] * 2^0;
|
yann@754
|
340 |
i = 32;
|
yann@754
|
341 |
do {
|
yann@754
|
342 |
i--;
|
yann@754
|
343 |
mask = 2^32 - 2^i;
|
yann@754
|
344 |
} while( (i!=0) && ( and( server_ip_num, mask ) == and( ip_num, mask ) ) );
|
yann@754
|
345 |
mask = and( 0xFFFFFFFF, lshift( mask, 1 ) );
|
yann@754
|
346 |
if( (i!=0) && (mask!=0) ) {
|
yann@754
|
347 |
masked_ip = and( ip_num, mask );
|
yann@754
|
348 |
for( i=0; i<pairs; i++ ) {
|
yann@754
|
349 |
if( ( masked_ip == ips[i] ) && ( mask == masks[i] ) ) {
|
yann@754
|
350 |
next;
|
yann@754
|
351 |
}
|
yann@754
|
352 |
}
|
yann@754
|
353 |
ips[pairs] = masked_ip;
|
yann@754
|
354 |
masks[pairs] = mask;
|
yann@754
|
355 |
pairs++;
|
yann@754
|
356 |
printf( "local = %d.%d.%d.%d/%d.%d.%d.%d\n",
|
yann@754
|
357 |
and( 0xFF, masked_ip / 2^24 ),
|
yann@754
|
358 |
and( 0xFF, masked_ip / 2^16 ),
|
yann@754
|
359 |
and( 0xFF, masked_ip / 2^8 ),
|
yann@754
|
360 |
and( 0xFF, masked_ip / 2^0 ),
|
yann@754
|
361 |
and( 0xFF, mask / 2^24 ),
|
yann@754
|
362 |
and( 0xFF, mask / 2^16 ),
|
yann@754
|
363 |
and( 0xFF, mask / 2^8 ),
|
yann@754
|
364 |
and( 0xFF, mask / 2^0 ) );
|
yann@754
|
365 |
}
|
yann@754
|
366 |
}
|
yann@754
|
367 |
' >"${CT_BUILD_DIR}/tsocks.conf"
|
yann@754
|
368 |
( echo "server = ${server_ip}";
|
yann@754
|
369 |
echo "server_port = ${CT_PROXY_PORT}";
|
yann@754
|
370 |
[ -n "${CT_PROXY_USER}" ] && echo "default_user=${CT_PROXY_USER}";
|
yann@754
|
371 |
[ -n "${CT_PROXY_PASS}" ] && echo "default_pass=${CT_PROXY_PASS}";
|
yann@754
|
372 |
) >>"${CT_BUILD_DIR}/tsocks.conf"
|
yann@754
|
373 |
case "${CT_PROXY_TYPE/socks}" in
|
yann@754
|
374 |
4|5) proxy_type="${CT_PROXY_TYPE/socks}";;
|
yann@754
|
375 |
auto)
|
yann@754
|
376 |
reply=$(inspectsocks "${server_ip}" "${CT_PROXY_PORT}" 2>&1 || true)
|
yann@754
|
377 |
case "${reply}" in
|
yann@754
|
378 |
*"server is a version 4 socks server") proxy_type=4;;
|
yann@754
|
379 |
*"server is a version 5 socks server") proxy_type=5;;
|
yann@754
|
380 |
*) CT_Abort "Unable to determine SOCKS proxy type for '${CT_PROXY_HOST}:${CT_PROXY_PORT}'"
|
yann@754
|
381 |
esac
|
yann@754
|
382 |
;;
|
yann@754
|
383 |
esac
|
yann@754
|
384 |
echo "server_type = ${proxy_type}" >> "${CT_BUILD_DIR}/tsocks.conf"
|
yann@754
|
385 |
CT_HasOrAbort tsocks
|
yann@754
|
386 |
# If tsocks was found, then validateconf is present (distributed with tsocks).
|
yann@754
|
387 |
CT_DoExecLog DEBUG validateconf -f "${CT_BUILD_DIR}/tsocks.conf"
|
yann@754
|
388 |
export TSOCKS_CONF_FILE="${CT_BUILD_DIR}/tsocks.conf"
|
yann@754
|
389 |
. tsocks -on
|
yann@754
|
390 |
;;
|
yann@754
|
391 |
esac
|
yann@754
|
392 |
}
|
yann@754
|
393 |
|
yann@63
|
394 |
# Download an URL using wget
|
yann@63
|
395 |
# Usage: CT_DoGetFileWget <URL>
|
yann@63
|
396 |
CT_DoGetFileWget() {
|
yann@63
|
397 |
# Need to return true because it is legitimate to not find the tarball at
|
yann@63
|
398 |
# some of the provided URLs (think about snapshots, different layouts for
|
yann@63
|
399 |
# different gcc versions, etc...)
|
yann@63
|
400 |
# Some (very old!) FTP server might not support the passive mode, thus
|
yann@63
|
401 |
# retry without
|
yann@63
|
402 |
# With automated download as we are doing, it can be very dangerous to use
|
yann@63
|
403 |
# -c to continue the downloads. It's far better to simply overwrite the
|
yann@63
|
404 |
# destination file
|
yann@492
|
405 |
# Some company networks have firewalls to connect to the internet, but it's
|
yann@492
|
406 |
# not easy to detect them, and wget does not timeout by default while
|
yann@492
|
407 |
# connecting, so force a global ${CT_CONNECT_TIMEOUT}-second timeout.
|
yann@492
|
408 |
wget -T ${CT_CONNECT_TIMEOUT} -nc --progress=dot:binary --tries=3 --passive-ftp "$1" \
|
yann@492
|
409 |
|| wget -T ${CT_CONNECT_TIMEOUT} -nc --progress=dot:binary --tries=3 "$1" \
|
yann@439
|
410 |
|| true
|
yann@63
|
411 |
}
|
yann@63
|
412 |
|
yann@63
|
413 |
# Download an URL using curl
|
yann@63
|
414 |
# Usage: CT_DoGetFileCurl <URL>
|
yann@63
|
415 |
CT_DoGetFileCurl() {
|
yann@492
|
416 |
# Note: comments about wget method (above) are also valid here
|
yann@439
|
417 |
# Plus: no good progress indicator is available with curl,
|
yann@439
|
418 |
# so output is consigned to oblivion
|
yann@492
|
419 |
curl --ftp-pasv -O --retry 3 "$1" --connect-timeout ${CT_CONNECT_TIMEOUT} >/dev/null \
|
yann@492
|
420 |
|| curl -O --retry 3 "$1" --connect-timeout ${CT_CONNECT_TIMEOUT} >/dev/null \
|
yann@439
|
421 |
|| true
|
yann@63
|
422 |
}
|
yann@63
|
423 |
|
yann@523
|
424 |
_wget=$(CT_Which wget)
|
yann@523
|
425 |
_curl=$(CT_Which curl)
|
yann@63
|
426 |
# Wrapper function to call one of curl or wget
|
yann@63
|
427 |
# Usage: CT_DoGetFile <URL>
|
yann@63
|
428 |
CT_DoGetFile() {
|
yann@63
|
429 |
case "${_wget},${_curl}" in
|
yann@63
|
430 |
,) CT_DoError "Could find neither wget nor curl";;
|
yann@776
|
431 |
,*) CT_DoExecLog ALL CT_DoGetFileCurl "$1" 2>&1;;
|
yann@776
|
432 |
*) CT_DoExecLog ALL CT_DoGetFileWget "$1" 2>&1;;
|
yann@63
|
433 |
esac
|
yann@63
|
434 |
}
|
yann@63
|
435 |
|
yann@63
|
436 |
# Download the file from one of the URLs passed as argument
|
yann@1104
|
437 |
# Usage: CT_GetFile <filename> [.extension] <url> [url ...]
|
yann@63
|
438 |
CT_GetFile() {
|
yann@63
|
439 |
local ext
|
yann@63
|
440 |
local url
|
yann@63
|
441 |
local file="$1"
|
yann@242
|
442 |
local first_ext=""
|
yann@63
|
443 |
shift
|
yann@712
|
444 |
# If next argument starts with a dot, then this is not an URL,
|
yann@712
|
445 |
# and we can consider that it is a preferred extension.
|
yann@242
|
446 |
case "$1" in
|
yann@712
|
447 |
.*) first_ext="$1"
|
yann@242
|
448 |
shift
|
yann@242
|
449 |
;;
|
yann@242
|
450 |
esac
|
yann@63
|
451 |
|
yann@63
|
452 |
# Do we already have it?
|
yann@719
|
453 |
ext=$(CT_GetFileExtension "${file}" ${first_ext})
|
yann@63
|
454 |
if [ -n "${ext}" ]; then
|
yann@523
|
455 |
CT_DoLog DEBUG "Already have '${file}'"
|
yann@85
|
456 |
return 0
|
yann@63
|
457 |
fi
|
yann@63
|
458 |
|
yann@754
|
459 |
# Try to retrieve the file
|
yann@788
|
460 |
CT_DoLog EXTRA "Retrieving '${file}'"
|
yann@63
|
461 |
CT_Pushd "${CT_TARBALLS_DIR}"
|
yann@754
|
462 |
|
yann@754
|
463 |
if [ -n "${CT_LOCAL_TARBALLS_DIR}" ]; then
|
yann@754
|
464 |
CT_DoLog DEBUG "Trying to retrieve an already downloaded copy of '${file}'"
|
yann@754
|
465 |
# We'd rather have a bzip2'ed tarball, then gzipped tarball, plain tarball,
|
yann@754
|
466 |
# or, as a failover, a file without extension.
|
yann@754
|
467 |
# Try local copy first, if it exists
|
yann@754
|
468 |
for ext in ${first_ext} .tar.bz2 .tar.gz .tgz .tar ''; do
|
yann@754
|
469 |
CT_DoLog DEBUG "Trying '${CT_LOCAL_TARBALLS_DIR}/${file}${ext}'"
|
yann@754
|
470 |
if [ -r "${CT_LOCAL_TARBALLS_DIR}/${file}${ext}" -a \
|
yann@754
|
471 |
"${CT_FORCE_DOWNLOAD}" != "y" ]; then
|
yann@799
|
472 |
CT_DoLog DEBUG "Got '${file}' from local storage"
|
yann@776
|
473 |
CT_DoExecLog ALL ln -s "${CT_LOCAL_TARBALLS_DIR}/${file}${ext}" "${file}${ext}"
|
yann@754
|
474 |
return 0
|
yann@754
|
475 |
fi
|
yann@754
|
476 |
done
|
yann@754
|
477 |
fi
|
yann@695
|
478 |
|
yann@695
|
479 |
# Not found locally, try from the network
|
yann@695
|
480 |
|
yann@1022
|
481 |
# Add URLs on the LAN mirror
|
yann@1022
|
482 |
LAN_URLS=
|
yann@1022
|
483 |
if [ "${CT_USE_MIRROR}" = "y" ]; then
|
yann@1022
|
484 |
CT_DoLog DEBUG "Trying to retrieve a copy of '${file}' from LAN mirror '${CT_MIRROR_HOSTNAME}'"
|
yann@1022
|
485 |
CT_TestOrAbort "Please set the LAN mirror hostname" -n "${CT_MIRROR_HOSTNAME}"
|
yann@1022
|
486 |
CT_TestOrAbort "Please tell me where to find tarballs on the LAN mirror '${CT_MIRROR_HOSTNAME}'" -n "${CT_MIRROR_BASE}"
|
yann@1022
|
487 |
LAN_URLS="${LAN_URLS} ${CT_MIRROR_SCHEME}://${CT_MIRROR_HOSTNAME}/${CT_MIRROR_BASE}/${file%-*}"
|
yann@1022
|
488 |
LAN_URLS="${LAN_URLS} ${CT_MIRROR_SCHEME}://${CT_MIRROR_HOSTNAME}/${CT_MIRROR_BASE}"
|
yann@695
|
489 |
fi
|
yann@695
|
490 |
|
yann@1022
|
491 |
if [ "${CT_PREFER_MIRROR}" = "y" ]; then
|
yann@1022
|
492 |
URLS="${LAN_URLS} ${@}"
|
yann@1022
|
493 |
else
|
yann@1022
|
494 |
URLS="${@} ${LAN_URLS}"
|
yann@1022
|
495 |
fi
|
yann@1022
|
496 |
|
yann@1022
|
497 |
# Scan all URLs in turn, and try to grab a tarball from there
|
yann@754
|
498 |
CT_DoSetProxy ${CT_PROXY_TYPE}
|
yann@242
|
499 |
for ext in ${first_ext} .tar.bz2 .tar.gz .tgz .tar ''; do
|
yann@102
|
500 |
# Try all urls in turn
|
yann@1022
|
501 |
for url in ${URLS}; do
|
yann@523
|
502 |
CT_DoLog DEBUG "Trying '${url}/${file}${ext}'"
|
yann@265
|
503 |
CT_DoGetFile "${url}/${file}${ext}"
|
yann@265
|
504 |
if [ -f "${file}${ext}" ]; then
|
yann@799
|
505 |
CT_DoLog DEBUG "Got '${file}' from the Internet"
|
yann@265
|
506 |
if [ "${CT_SAVE_TARBALLS}" = "y" ]; then
|
yann@777
|
507 |
# The file may already exist if downloads are forced: remove it first
|
yann@523
|
508 |
CT_DoLog EXTRA "Saving '${file}' to local storage"
|
yann@777
|
509 |
CT_DoExecLog ALL rm -f "${CT_LOCAL_TARBALLS_DIR}/${file}${ext}"
|
yann@777
|
510 |
CT_DoExecLog ALL mv -f "${file}${ext}" "${CT_LOCAL_TARBALLS_DIR}"
|
yann@776
|
511 |
CT_DoExecLog ALL ln -s "${CT_LOCAL_TARBALLS_DIR}/${file}${ext}" "${file}${ext}"
|
yann@265
|
512 |
fi
|
yann@265
|
513 |
return 0
|
yann@265
|
514 |
fi
|
yann@102
|
515 |
done
|
yann@102
|
516 |
done
|
yann@63
|
517 |
CT_Popd
|
yann@63
|
518 |
|
yann@754
|
519 |
CT_Abort "Could not retrieve '${file}'."
|
yann@63
|
520 |
}
|
yann@63
|
521 |
|
yann@63
|
522 |
# Extract a tarball and patch the resulting sources if necessary.
|
yann@63
|
523 |
# Some tarballs need to be extracted in specific places. Eg.: glibc addons
|
yann@63
|
524 |
# must be extracted in the glibc directory; uCLibc locales must be extracted
|
yann@63
|
525 |
# in the extra/locale sub-directory of uClibc.
|
yann@63
|
526 |
CT_ExtractAndPatch() {
|
yann@63
|
527 |
local file="$1"
|
yann@523
|
528 |
local base_file=$(echo "${file}" |cut -d - -f 1)
|
yann@523
|
529 |
local ver_file=$(echo "${file}" |cut -d - -f 2-)
|
yann@63
|
530 |
local official_patch_dir
|
yann@63
|
531 |
local custom_patch_dir
|
yann@63
|
532 |
local libc_addon
|
yann@523
|
533 |
local ext=$(CT_GetFileExtension "${file}")
|
yann@523
|
534 |
CT_TestAndAbort "'${file}' not found in '${CT_TARBALLS_DIR}'" -z "${ext}"
|
yann@63
|
535 |
local full_file="${CT_TARBALLS_DIR}/${file}${ext}"
|
yann@63
|
536 |
|
yann@63
|
537 |
CT_Pushd "${CT_SRC_DIR}"
|
yann@63
|
538 |
|
yann@63
|
539 |
# Add-ons need a little love, really.
|
yann@63
|
540 |
case "${file}" in
|
yann@787
|
541 |
glibc-[a-z]*-*|eglibc-[a-z]*-*)
|
yann@523
|
542 |
CT_TestAndAbort "Trying to extract the C-library addon/locales '${file}' when C-library not yet extracted" ! -d "${CT_LIBC_FILE}"
|
yann@63
|
543 |
cd "${CT_LIBC_FILE}"
|
yann@63
|
544 |
libc_addon=y
|
yann@63
|
545 |
[ -f ".${file}.extracted" ] && return 0
|
yann@63
|
546 |
touch ".${file}.extracted"
|
yann@63
|
547 |
;;
|
yann@63
|
548 |
uClibc-locale-*)
|
yann@523
|
549 |
CT_TestAndAbort "Trying to extract the C-library addon/locales '${file}' when C-library not yet extracted" ! -d "${CT_LIBC_FILE}"
|
yann@63
|
550 |
cd "${CT_LIBC_FILE}/extra/locale"
|
yann@63
|
551 |
libc_addon=y
|
yann@63
|
552 |
[ -f ".${file}.extracted" ] && return 0
|
yann@63
|
553 |
touch ".${file}.extracted"
|
yann@63
|
554 |
;;
|
yann@63
|
555 |
esac
|
yann@63
|
556 |
|
yann@63
|
557 |
# If the directory exists, then consider extraction and patching done
|
yann@77
|
558 |
if [ -d "${file}" ]; then
|
yann@523
|
559 |
CT_DoLog DEBUG "Already extracted '${file}'"
|
yann@77
|
560 |
return 0
|
yann@77
|
561 |
fi
|
yann@63
|
562 |
|
yann@788
|
563 |
CT_DoLog EXTRA "Extracting and patching '${file}'"
|
yann@63
|
564 |
case "${ext}" in
|
yann@776
|
565 |
.tar.bz2) CT_DoExecLog ALL tar xvjf "${full_file}";;
|
yann@776
|
566 |
.tar.gz|.tgz) CT_DoExecLog ALL tar xvzf "${full_file}";;
|
yann@776
|
567 |
.tar) CT_DoExecLog ALL tar xvf "${full_file}";;
|
yann@523
|
568 |
*) CT_Abort "Don't know how to handle '${file}': unknown extension" ;;
|
yann@63
|
569 |
esac
|
yann@63
|
570 |
|
yann@63
|
571 |
# Snapshots might not have the version number in the extracted directory
|
yann@305
|
572 |
# name. This is also the case for some (odd) packages, such as D.U.M.A.
|
yann@63
|
573 |
# Overcome this issue by symlink'ing the directory.
|
yann@63
|
574 |
if [ ! -d "${file}" -a "${libc_addon}" != "y" ]; then
|
yann@63
|
575 |
case "${ext}" in
|
yann@523
|
576 |
.tar.bz2) base=$(tar tjf "${full_file}" |head -n 1 |cut -d / -f 1 || true);;
|
yann@523
|
577 |
.tar.gz|.tgz) base=$(tar tzf "${full_file}" |head -n 1 |cut -d / -f 1 || true);;
|
yann@523
|
578 |
.tar) base=$(tar tf "${full_file}" |head -n 1 |cut -d / -f 1 || true);;
|
yann@63
|
579 |
esac
|
yann@523
|
580 |
CT_TestOrAbort "There was a problem when extracting '${file}'" -d "${base}" -o "${base}" != "${file}"
|
yann@63
|
581 |
ln -s "${base}" "${file}"
|
yann@63
|
582 |
fi
|
yann@63
|
583 |
|
yann@63
|
584 |
# Kludge: outside this function, we wouldn't know if we had just extracted
|
yann@63
|
585 |
# a libc addon, or a plain package. Apply patches now.
|
yann@136
|
586 |
if [ "${libc_addon}" = "y" ]; then
|
yann@508
|
587 |
# Some addon tarballs directly contain the correct addon directory,
|
yann@508
|
588 |
# while others have the addon directory named after the tarball.
|
yann@365
|
589 |
# Fix that by always using the short name (eg: linuxthreads, ports, etc...)
|
yann@919
|
590 |
addon_short_name=$(echo "${file}" |sed -r -e 's/^[^-]+-([^-]+)-.*$/\1/;')
|
yann@919
|
591 |
if [ ! -d "${addon_short_name}" ]; then
|
yann@919
|
592 |
mv "${file}" "${addon_short_name}"
|
yann@919
|
593 |
# Keep a symlink to avoid re-extracting later on.
|
yann@919
|
594 |
ln -s "${addon_short_name}" "${file}"
|
yann@919
|
595 |
fi
|
yann@136
|
596 |
# If libc addon, we're already in the correct place
|
yann@136
|
597 |
else
|
yann@136
|
598 |
cd "${file}"
|
yann@136
|
599 |
fi
|
yann@63
|
600 |
|
yann@182
|
601 |
official_patch_dir=
|
yann@182
|
602 |
custom_patch_dir=
|
yann@956
|
603 |
[ "${CT_CUSTOM_PATCH_ONLY}" = "y" ] || official_patch_dir="${CT_LIB_DIR}/patches/${base_file}/${ver_file}"
|
yann@63
|
604 |
[ "${CT_CUSTOM_PATCH}" = "y" ] && custom_patch_dir="${CT_CUSTOM_PATCH_DIR}/${base_file}/${ver_file}"
|
yann@63
|
605 |
for patch_dir in "${official_patch_dir}" "${custom_patch_dir}"; do
|
yann@63
|
606 |
if [ -n "${patch_dir}" -a -d "${patch_dir}" ]; then
|
yann@63
|
607 |
for p in "${patch_dir}"/*.patch; do
|
yann@63
|
608 |
if [ -f "${p}" ]; then
|
yann@523
|
609 |
CT_DoLog DEBUG "Applying patch '${p}'"
|
yann@776
|
610 |
CT_DoExecLog ALL patch -g0 -F1 -p1 -f <"${p}"
|
yann@523
|
611 |
CT_TestAndAbort "Failed while applying patch file '${p}'" ${PIPESTATUS[0]} -ne 0
|
yann@63
|
612 |
fi
|
yann@63
|
613 |
done
|
yann@63
|
614 |
fi
|
yann@63
|
615 |
done
|
yann@63
|
616 |
|
yann@508
|
617 |
if [ "${CT_OVERIDE_CONFIG_GUESS_SUB}" = "y" ]; then
|
yann@508
|
618 |
CT_DoLog ALL "Overiding config.guess and config.sub"
|
yann@508
|
619 |
for cfg in config_guess config_sub; do
|
yann@1101
|
620 |
eval ${cfg}="${CT_LIB_DIR}/scripts/${cfg/_/.}"
|
yann@1101
|
621 |
[ -e "${CT_TOP_DIR}/scripts/${cfg/_/.}" ] && eval ${cfg}="${CT_TOP_DIR}/scripts/${cfg/_/.}"
|
yann@776
|
622 |
# Can't use CT_DoExecLog because of the '{} \;' to be passed un-mangled to find
|
yann@508
|
623 |
find . -type f -name "${cfg/_/.}" -exec cp -v "${!cfg}" {} \; |CT_DoLog ALL
|
yann@508
|
624 |
done
|
yann@508
|
625 |
fi
|
yann@508
|
626 |
|
yann@63
|
627 |
CT_Popd
|
yann@63
|
628 |
}
|
yann@63
|
629 |
|
yann@182
|
630 |
# Two wrappers to call config.(guess|sub) either from CT_TOP_DIR or CT_LIB_DIR.
|
yann@182
|
631 |
# Those from CT_TOP_DIR, if they exist, will be be more recent than those from CT_LIB_DIR.
|
yann@182
|
632 |
CT_DoConfigGuess() {
|
yann@1101
|
633 |
if [ -x "${CT_TOP_DIR}/scripts/config.guess" ]; then
|
yann@1101
|
634 |
"${CT_TOP_DIR}/scripts/config.guess"
|
yann@182
|
635 |
else
|
yann@1101
|
636 |
"${CT_LIB_DIR}/scripts/config.guess"
|
yann@182
|
637 |
fi
|
yann@182
|
638 |
}
|
yann@182
|
639 |
|
yann@182
|
640 |
CT_DoConfigSub() {
|
yann@1101
|
641 |
if [ -x "${CT_TOP_DIR}/scripts/config.sub" ]; then
|
yann@1101
|
642 |
"${CT_TOP_DIR}/scripts/config.sub" "$@"
|
yann@182
|
643 |
else
|
yann@1101
|
644 |
"${CT_LIB_DIR}/scripts/config.sub" "$@"
|
yann@182
|
645 |
fi
|
yann@182
|
646 |
}
|
yann@182
|
647 |
|
yann@335
|
648 |
# Compute the target tuple from what is provided by the user
|
yann@335
|
649 |
# Usage: CT_DoBuildTargetTuple
|
yann@63
|
650 |
# In fact this function takes the environment variables to build the target
|
yann@335
|
651 |
# tuple. It is needed both by the normal build sequence, as well as the
|
yann@63
|
652 |
# sample saving sequence.
|
yann@335
|
653 |
CT_DoBuildTargetTuple() {
|
yann@383
|
654 |
# Set the endianness suffix, and the default endianness gcc option
|
yann@63
|
655 |
case "${CT_ARCH_BE},${CT_ARCH_LE}" in
|
yann@383
|
656 |
y,) target_endian_eb=eb
|
yann@383
|
657 |
target_endian_el=
|
yann@391
|
658 |
CT_ARCH_ENDIAN_CFLAG="-mbig-endian"
|
yann@527
|
659 |
CT_ARCH_ENDIAN_LDFLAG="-EB"
|
yann@383
|
660 |
;;
|
yann@383
|
661 |
,y) target_endian_eb=
|
yann@383
|
662 |
target_endian_el=el
|
yann@391
|
663 |
CT_ARCH_ENDIAN_CFLAG="-mlittle-endian"
|
yann@527
|
664 |
CT_ARCH_ENDIAN_LDFLAG="-EL"
|
yann@383
|
665 |
;;
|
yann@63
|
666 |
esac
|
yann@383
|
667 |
|
yann@965
|
668 |
# Build the default architecture tuple part
|
yann@965
|
669 |
CT_TARGET_ARCH="${CT_ARCH}"
|
yann@965
|
670 |
|
yann@383
|
671 |
# Set defaults for the system part of the tuple. Can be overriden
|
yann@383
|
672 |
# by architecture-specific values.
|
yann@383
|
673 |
case "${CT_LIBC}" in
|
yann@850
|
674 |
none) CT_TARGET_SYS=elf;;
|
yann@787
|
675 |
*glibc) CT_TARGET_SYS=gnu;;
|
yann@383
|
676 |
uClibc) CT_TARGET_SYS=uclibc;;
|
yann@63
|
677 |
esac
|
yann@383
|
678 |
|
yann@387
|
679 |
# Transform the ARCH into a kernel-understandable ARCH
|
yann@387
|
680 |
CT_KERNEL_ARCH="${CT_ARCH}"
|
yann@387
|
681 |
|
yann@391
|
682 |
# Set the default values for ARCH, ABI, CPU, TUNE, FPU and FLOAT
|
yann@497
|
683 |
unset CT_ARCH_ARCH_CFLAG CT_ARCH_ABI_CFLAG CT_ARCH_CPU_CFLAG CT_ARCH_TUNE_CFLAG CT_ARCH_FPU_CFLAG CT_ARCH_FLOAT_CFLAG
|
yann@497
|
684 |
unset CT_ARCH_WITH_ARCH CT_ARCH_WITH_ABI CT_ARCH_WITH_CPU CT_ARCH_WITH_TUNE CT_ARCH_WITH_FPU CT_ARCH_WITH_FLOAT
|
yann@391
|
685 |
[ "${CT_ARCH_ARCH}" ] && { CT_ARCH_ARCH_CFLAG="-march=${CT_ARCH_ARCH}"; CT_ARCH_WITH_ARCH="--with-arch=${CT_ARCH_ARCH}"; }
|
yann@391
|
686 |
[ "${CT_ARCH_ABI}" ] && { CT_ARCH_ABI_CFLAG="-mabi=${CT_ARCH_ABI}"; CT_ARCH_WITH_ABI="--with-abi=${CT_ARCH_ABI}"; }
|
yann@391
|
687 |
[ "${CT_ARCH_CPU}" ] && { CT_ARCH_CPU_CFLAG="-mcpu=${CT_ARCH_CPU}"; CT_ARCH_WITH_CPU="--with-cpu=${CT_ARCH_CPU}"; }
|
yann@405
|
688 |
[ "${CT_ARCH_TUNE}" ] && { CT_ARCH_TUNE_CFLAG="-mtune=${CT_ARCH_TUNE}"; CT_ARCH_WITH_TUNE="--with-tune=${CT_ARCH_TUNE}"; }
|
yann@391
|
689 |
[ "${CT_ARCH_FPU}" ] && { CT_ARCH_FPU_CFLAG="-mfpu=${CT_ARCH_FPU}"; CT_ARCH_WITH_FPU="--with-fpu=${CT_ARCH_FPU}"; }
|
yann@412
|
690 |
[ "${CT_ARCH_FLOAT_SW}" ] && { CT_ARCH_FLOAT_CFLAG="-msoft-float"; CT_ARCH_WITH_FLOAT="--with-float=soft"; }
|
yann@391
|
691 |
|
yann@965
|
692 |
# Build the default kernel tuple part
|
yann@965
|
693 |
CT_TARGET_KERNEL="${CT_KERNEL}"
|
yann@964
|
694 |
|
yann@965
|
695 |
# Overide the default values with the components specific settings
|
yann@964
|
696 |
CT_DoArchTupleValues
|
yann@965
|
697 |
CT_DoKernelTupleValues
|
yann@383
|
698 |
|
yann@391
|
699 |
# Finish the target tuple construction
|
yann@1094
|
700 |
CT_TARGET="${CT_TARGET_ARCH}-${CT_TARGET_VENDOR:-unknown}-${CT_TARGET_KERNEL}${CT_TARGET_KERNEL:+-}${CT_TARGET_SYS}"
|
yann@1094
|
701 |
|
yann@1094
|
702 |
# Sanity checks
|
yann@1094
|
703 |
__sed_alias=""
|
yann@1094
|
704 |
if [ -n "${CT_TARGET_ALIAS_SED_EXPR}" ]; then
|
yann@1094
|
705 |
__sed_alias=$(echo "${CT_TARGET}" |sed -r -e "${CT_TARGET_ALIAS_SED_EXPR}")
|
yann@1094
|
706 |
fi
|
yann@1094
|
707 |
case ":${CT_TARGET_VENDOR}:${CT_TARGET_ALIAS}:${__sed_alias}:" in
|
yann@1094
|
708 |
:*" "*:*:*:) CT_Abort "Don't use spaces in the vendor string, it breaks things.";;
|
yann@1094
|
709 |
:*"-"*:*:*:) CT_Abort "Don't use dashes in the vendor string, it breaks things.";;
|
yann@1094
|
710 |
:*:*" "*:*:) CT_Abort "Don't use spaces in the target alias, it breaks things.";;
|
yann@1094
|
711 |
:*:*:*" "*:) CT_Abort "Don't use spaces in the target sed transform, it breaks things.";;
|
yann@1094
|
712 |
esac
|
yann@1094
|
713 |
|
yann@1094
|
714 |
# Canonicalise it
|
yann@1094
|
715 |
CT_TARGET=$(CT_DoConfigSub "${CT_TARGET}")
|
yann@391
|
716 |
|
yann@391
|
717 |
# Prepare the target CFLAGS
|
yann@767
|
718 |
CT_ARCH_TARGET_CFLAGS="${CT_ARCH_TARGET_CFLAGS} ${CT_ARCH_ENDIAN_CFLAG}"
|
yann@527
|
719 |
CT_ARCH_TARGET_CFLAGS="${CT_ARCH_TARGET_CFLAGS} ${CT_ARCH_ARCH_CFLAG}"
|
yann@397
|
720 |
CT_ARCH_TARGET_CFLAGS="${CT_ARCH_TARGET_CFLAGS} ${CT_ARCH_ABI_CFLAG}"
|
yann@397
|
721 |
CT_ARCH_TARGET_CFLAGS="${CT_ARCH_TARGET_CFLAGS} ${CT_ARCH_CPU_CFLAG}"
|
yann@397
|
722 |
CT_ARCH_TARGET_CFLAGS="${CT_ARCH_TARGET_CFLAGS} ${CT_ARCH_TUNE_CFLAG}"
|
yann@397
|
723 |
CT_ARCH_TARGET_CFLAGS="${CT_ARCH_TARGET_CFLAGS} ${CT_ARCH_FPU_CFLAG}"
|
yann@397
|
724 |
CT_ARCH_TARGET_CFLAGS="${CT_ARCH_TARGET_CFLAGS} ${CT_ARCH_FLOAT_CFLAG}"
|
yann@527
|
725 |
|
yann@527
|
726 |
# Now on for the target LDFLAGS
|
yann@767
|
727 |
CT_ARCH_TARGET_LDFLAGS="${CT_ARCH_TARGET_LDFLAGS} ${CT_ARCH_ENDIAN_LDFLAG}"
|
yann@63
|
728 |
}
|
yann@121
|
729 |
|
yann@121
|
730 |
# This function does pause the build until the user strikes "Return"
|
yann@121
|
731 |
# Usage: CT_DoPause [optional_message]
|
yann@121
|
732 |
CT_DoPause() {
|
yann@121
|
733 |
local foo
|
yann@121
|
734 |
local message="${1:-Pausing for your pleasure}"
|
yann@121
|
735 |
CT_DoLog INFO "${message}"
|
yann@523
|
736 |
read -p "Press 'Enter' to continue, or Ctrl-C to stop..." foo >&6
|
yann@121
|
737 |
return 0
|
yann@121
|
738 |
}
|
yann@121
|
739 |
|
yann@121
|
740 |
# This function saves the state of the toolchain to be able to restart
|
yann@121
|
741 |
# at any one point
|
yann@121
|
742 |
# Usage: CT_DoSaveState <next_step_name>
|
yann@121
|
743 |
CT_DoSaveState() {
|
yann@121
|
744 |
[ "${CT_DEBUG_CT_SAVE_STEPS}" = "y" ] || return 0
|
yann@121
|
745 |
local state_name="$1"
|
yann@121
|
746 |
local state_dir="${CT_STATE_DIR}/${state_name}"
|
yann@121
|
747 |
|
yann@523
|
748 |
CT_DoLog DEBUG "Saving state to restart at step '${state_name}'..."
|
yann@121
|
749 |
rm -rf "${state_dir}"
|
yann@121
|
750 |
mkdir -p "${state_dir}"
|
yann@121
|
751 |
|
yann@121
|
752 |
case "${CT_DEBUG_CT_SAVE_STEPS_GZIP}" in
|
yann@327
|
753 |
y) tar_opt=z; tar_ext=.gz;;
|
yann@327
|
754 |
*) tar_opt=; tar_ext=;;
|
yann@121
|
755 |
esac
|
yann@121
|
756 |
|
yann@121
|
757 |
CT_DoLog DEBUG " Saving environment and aliases"
|
yann@738
|
758 |
# We must omit shell functions, and some specific bash variables
|
yann@738
|
759 |
# that break when restoring the environment, later. We could do
|
yann@1017
|
760 |
# all the processing in the gawk script, but a sed is easier...
|
yann@1017
|
761 |
set |gawk '
|
yann@1017
|
762 |
BEGIN { _p = 1; }
|
yann@1017
|
763 |
$0~/^[^ ]+ \(\)/ { _p = 0; }
|
yann@1017
|
764 |
_p == 1
|
yann@1017
|
765 |
$0 == "}" { _p = 1; }
|
yann@1017
|
766 |
' |sed -r -e '/^BASH_(ARGC|ARGV|LINENO|SOURCE|VERSINFO)=/d;
|
yann@738
|
767 |
/^(UID|EUID)=/d;
|
yann@738
|
768 |
/^(FUNCNAME|GROUPS|PPID|SHELLOPTS)=/d;' >"${state_dir}/env.sh"
|
yann@121
|
769 |
|
yann@523
|
770 |
CT_DoLog DEBUG " Saving CT_CC_CORE_STATIC_PREFIX_DIR='${CT_CC_CORE_STATIC_PREFIX_DIR}'"
|
yann@136
|
771 |
CT_Pushd "${CT_CC_CORE_STATIC_PREFIX_DIR}"
|
yann@776
|
772 |
CT_DoExecLog DEBUG tar cv${tar_opt}f "${state_dir}/cc_core_static_prefix_dir.tar${tar_ext}" .
|
yann@136
|
773 |
CT_Popd
|
yann@136
|
774 |
|
yann@523
|
775 |
CT_DoLog DEBUG " Saving CT_CC_CORE_SHARED_PREFIX_DIR='${CT_CC_CORE_SHARED_PREFIX_DIR}'"
|
yann@136
|
776 |
CT_Pushd "${CT_CC_CORE_SHARED_PREFIX_DIR}"
|
yann@776
|
777 |
CT_DoExecLog DEBUG tar cv${tar_opt}f "${state_dir}/cc_core_shared_prefix_dir.tar${tar_ext}" .
|
yann@121
|
778 |
CT_Popd
|
yann@121
|
779 |
|
yann@523
|
780 |
CT_DoLog DEBUG " Saving CT_PREFIX_DIR='${CT_PREFIX_DIR}'"
|
yann@121
|
781 |
CT_Pushd "${CT_PREFIX_DIR}"
|
yann@776
|
782 |
CT_DoExecLog DEBUG tar cv${tar_opt}f "${state_dir}/prefix_dir.tar${tar_ext}" --exclude '*.log' .
|
yann@121
|
783 |
CT_Popd
|
yann@121
|
784 |
|
yann@121
|
785 |
if [ "${CT_LOG_TO_FILE}" = "y" ]; then
|
yann@121
|
786 |
CT_DoLog DEBUG " Saving log file"
|
yann@121
|
787 |
exec >/dev/null
|
yann@121
|
788 |
case "${CT_DEBUG_CT_SAVE_STEPS_GZIP}" in
|
yann@121
|
789 |
y) gzip -3 -c "${CT_LOG_FILE}" >"${state_dir}/log.gz";;
|
yann@121
|
790 |
*) cat "${CT_LOG_FILE}" >"${state_dir}/log";;
|
yann@121
|
791 |
esac
|
yann@121
|
792 |
exec >>"${CT_LOG_FILE}"
|
yann@121
|
793 |
fi
|
yann@121
|
794 |
}
|
yann@121
|
795 |
|
yann@136
|
796 |
# This function restores a previously saved state
|
yann@121
|
797 |
# Usage: CT_DoLoadState <state_name>
|
yann@121
|
798 |
CT_DoLoadState(){
|
yann@121
|
799 |
local state_name="$1"
|
yann@121
|
800 |
local state_dir="${CT_STATE_DIR}/${state_name}"
|
yann@135
|
801 |
local old_RESTART="${CT_RESTART}"
|
yann@135
|
802 |
local old_STOP="${CT_STOP}"
|
yann@121
|
803 |
|
yann@523
|
804 |
CT_TestOrAbort "The previous build did not reach the point where it could be restarted at '${CT_RESTART}'" -d "${state_dir}"
|
yann@141
|
805 |
|
yann@121
|
806 |
# We need to do something special with the log file!
|
yann@121
|
807 |
if [ "${CT_LOG_TO_FILE}" = "y" ]; then
|
yann@121
|
808 |
exec >"${state_dir}/tail.log"
|
yann@121
|
809 |
fi
|
yann@523
|
810 |
CT_DoLog INFO "Restoring state at step '${state_name}', as requested."
|
yann@121
|
811 |
|
yann@121
|
812 |
case "${CT_DEBUG_CT_SAVE_STEPS_GZIP}" in
|
yann@327
|
813 |
y) tar_opt=z; tar_ext=.gz;;
|
yann@327
|
814 |
*) tar_opt=; tar_ext=;;
|
yann@121
|
815 |
esac
|
yann@121
|
816 |
|
yann@121
|
817 |
CT_DoLog DEBUG " Removing previous build directories"
|
yann@136
|
818 |
chmod -R u+rwX "${CT_PREFIX_DIR}" "${CT_CC_CORE_SHARED_PREFIX_DIR}" "${CT_CC_CORE_STATIC_PREFIX_DIR}"
|
yann@136
|
819 |
rm -rf "${CT_PREFIX_DIR}" "${CT_CC_CORE_SHARED_PREFIX_DIR}" "${CT_CC_CORE_STATIC_PREFIX_DIR}"
|
yann@136
|
820 |
mkdir -p "${CT_PREFIX_DIR}" "${CT_CC_CORE_SHARED_PREFIX_DIR}" "${CT_CC_CORE_STATIC_PREFIX_DIR}"
|
yann@121
|
821 |
|
yann@523
|
822 |
CT_DoLog DEBUG " Restoring CT_PREFIX_DIR='${CT_PREFIX_DIR}'"
|
yann@121
|
823 |
CT_Pushd "${CT_PREFIX_DIR}"
|
yann@776
|
824 |
CT_DoExecLog DEBUG tar xv${tar_opt}f "${state_dir}/prefix_dir.tar${tar_ext}"
|
yann@121
|
825 |
CT_Popd
|
yann@121
|
826 |
|
yann@523
|
827 |
CT_DoLog DEBUG " Restoring CT_CC_CORE_SHARED_PREFIX_DIR='${CT_CC_CORE_SHARED_PREFIX_DIR}'"
|
yann@136
|
828 |
CT_Pushd "${CT_CC_CORE_SHARED_PREFIX_DIR}"
|
yann@776
|
829 |
CT_DoExecLog DEBUG tar xv${tar_opt}f "${state_dir}/cc_core_shared_prefix_dir.tar${tar_ext}"
|
yann@136
|
830 |
CT_Popd
|
yann@136
|
831 |
|
yann@523
|
832 |
CT_DoLog DEBUG " Restoring CT_CC_CORE_STATIC_PREFIX_DIR='${CT_CC_CORE_STATIC_PREFIX_DIR}'"
|
yann@136
|
833 |
CT_Pushd "${CT_CC_CORE_STATIC_PREFIX_DIR}"
|
yann@776
|
834 |
CT_DoExecLog DEBUG tar xv${tar_opt}f "${state_dir}/cc_core_static_prefix_dir.tar${tar_ext}"
|
yann@121
|
835 |
CT_Popd
|
yann@121
|
836 |
|
yann@121
|
837 |
# Restore the environment, discarding any error message
|
yann@121
|
838 |
# (for example, read-only bash internals)
|
yann@121
|
839 |
CT_DoLog DEBUG " Restoring environment"
|
yann@121
|
840 |
. "${state_dir}/env.sh" >/dev/null 2>&1 || true
|
yann@121
|
841 |
|
yann@135
|
842 |
# Restore the new RESTART and STOP steps
|
yann@135
|
843 |
CT_RESTART="${old_RESTART}"
|
yann@135
|
844 |
CT_STOP="${old_STOP}"
|
yann@135
|
845 |
unset old_stop old_restart
|
yann@135
|
846 |
|
yann@121
|
847 |
if [ "${CT_LOG_TO_FILE}" = "y" ]; then
|
yann@121
|
848 |
CT_DoLog DEBUG " Restoring log file"
|
yann@121
|
849 |
exec >/dev/null
|
yann@121
|
850 |
case "${CT_DEBUG_CT_SAVE_STEPS_GZIP}" in
|
yann@121
|
851 |
y) zcat "${state_dir}/log.gz" >"${CT_LOG_FILE}";;
|
yann@121
|
852 |
*) cat "${state_dir}/log" >"${CT_LOG_FILE}";;
|
yann@121
|
853 |
esac
|
yann@121
|
854 |
cat "${state_dir}/tail.log" >>"${CT_LOG_FILE}"
|
yann@121
|
855 |
exec >>"${CT_LOG_FILE}"
|
yann@121
|
856 |
rm -f "${state_dir}/tail.log"
|
yann@121
|
857 |
fi
|
yann@121
|
858 |
}
|