forked from coreos/dev-util
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoolchain_utils.sh
executable file
·219 lines (190 loc) · 5.3 KB
/
toolchain_utils.sh
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/bin/bash
# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
# TODO: Convert this to python.
get_all_board_toolchains()
{
cros_setup_toolchains --show-board-cfg="$1" | sed 's:,: :g'
}
get_ctarget_from_board()
{
local all_toolchains=( $(get_all_board_toolchains "$@") )
echo "${all_toolchains[0]}"
}
get_board_arch()
{
local ctarget=$(get_ctarget_from_board "$@")
# Ask crossdev what the magical portage arch is!
local arch=$(eval $(crossdev --show-target-cfg "${ctarget}"); echo ${arch})
if [[ -z ${arch} ]] ; then
error "Unable to determine ARCH from toolchain: ${ctarget}"
return 1
fi
echo "${arch}"
return 0
}
is_number()
{
echo "$1" | grep -q "^[0-9]\+$"
}
is_config_installed()
{
gcc-config -l | cut -d" " -f3 | grep -q "$1$"
}
get_installed_atom_from_config()
{
local gcc_path="$(gcc-config -B "$1")"
equery b "$gcc_path" | head -n1
}
get_atom_from_config()
{
echo "$1" | sed -E "s|(.*)-(.*)|cross-\1/gcc-\2|g"
}
get_ctarget_from_atom()
{
local atom="$1"
echo "$atom" | sed -E 's|cross-([^/]+)/.*|\1|g'
}
copy_gcc_libs_helper()
{
local target_location="$1"
local file_path="$2"
local dir_path=$(dirname "$file_path")
info "Copying $file_path symlink and file to $target_location/$dir_path/."
sudo mkdir -p "$target_location/$dir_path"
sudo cp -a "$file_path"* "$target_location/$dir_path/"
local env_d_file="$target_location/etc/env.d/05gcc"
info "Adding $dir_path to LDPATH in file $env_d_file"
sudo mkdir -p $(dirname "$env_d_file")
local line_to_add="LDPATH=\"$dir_path\""
if ! grep -q "^$line_to_add$" "$env_d_file" &>/dev/null
then
echo "$line_to_add" | sudo_append "$env_d_file"
fi
}
copy_gcc_libs()
{
# TODO: Figure out a better way of doing this?
local target_location="$1"
local atom="$2"
local libgcc_file=$(portageq contents / $atom | \
grep /libgcc_s.so$)
local libstdcxx_file=$(portageq contents / $atom | \
grep /libstdc++.so$)
if [[ -z "$libgcc_file" || -z "$libstdcxx_file" ]]
then
error "Could not find libgcc_s.so/libstdcxx_s.so. Is\
=$atom emerged properly?"
return 1
fi
copy_gcc_libs_helper $target_location $libgcc_file
copy_gcc_libs_helper $target_location $libstdcxx_file
sudo ROOT="$target_location" env-update
return 0
}
get_current_binutils_config()
{
local ctarget="$1"
binutils-config -l | grep "$ctarget" | grep "*" | awk '{print $NF}'
}
get_bfd_config()
{
local ctarget="$1"
binutils-config -l | grep "$ctarget" | grep -v "gold" | head -n1 | \
awk '{print $NF}'
}
emerge_gcc()
{
local atom="$1"
local ctarget="$(get_ctarget_from_atom $atom)"
mask_file="/etc/portage/package.mask/cross-$ctarget"
moved_mask_file=0
# Move the package mask file elsewhere.
if [[ -f "$mask_file" ]]
then
temp_file="$(mktemp)"
sudo mv "$mask_file" "$temp_file"
moved_mask_file=1
fi
USE+=" multislot"
if echo "$atom" | grep -q "gcc-4.6.0$"
then
old_binutils_config="$(get_current_binutils_config $ctarget)"
bfd_binutils_config="$(get_bfd_config $ctarget)"
if [[ "$old_binutils_config" != "$bfd_binutils_config" ]]
then
sudo binutils-config "$bfd_binutils_config"
fi
fi
sudo ACCEPT_KEYWORDS="*" USE="$USE" emerge ="$atom"
emerge_retval=$?
# Move the package mask file back.
if [[ $moved_mask_file -eq 1 ]]
then
sudo mv "$temp_file" "$mask_file"
fi
if [[ ! -z "$old_binutils_config" &&
"$old_binutils_config" != "$(get_current_binutils_config $ctarget)" ]]
then
sudo binutils-config "$old_binutils_config"
fi
return $emerge_retval
}
# This function should only be called when testing experimental toolchain
# compilers. Please don't call this from any other script.
cros_gcc_config()
{
# Return if we're not switching profiles.
if [[ "$1" == -* ]]
then
sudo gcc-config "$1"
return $?
fi
# cros_gcc_config can be called like:
# cros_gcc_config <number> to switch config to that
# number. In that case we should just try to switch to
# that config and not try to install a missing one.
if ! is_number "$1" && ! is_config_installed "$1"
then
info "Configuration $1 not found."
info "Trying to emerge it..."
local atom="$(get_atom_from_config "$1")"
emerge_gcc "$atom" || die "Could not install $atom"
fi
sudo gcc-config "$1" || die "Could not switch to $1"
local boards=$(get_boards_from_config "$1")
local board
for board in $boards
do
cros_install_libs_for_config "$board" "$1"
emerge-"$board" --oneshot sys-devel/libtool
done
}
get_boards_from_config()
{
local atom=$(get_installed_atom_from_config "$1")
if [[ $atom != cross* ]]
then
warn "$atom is not a cross-compiler."
warn "Therefore not adding its libs to the board roots."
return 0
fi
# Now copy the lib files into all possible boards.
local ctarget="$(get_ctarget_from_atom "$atom")"
for board_root in /build/*
do
local board="$(basename $board_root)"
local board_tc="$(get_ctarget_from_board $board)"
if [[ "${board_tc}" == "${ctarget}" ]]
then
echo "$board"
fi
done
}
cros_install_libs_for_config()
{
local board="$1"
local atom=$(get_installed_atom_from_config "$2")
copy_gcc_libs /build/"$board" "$atom"
}