blob: 4f02bb57c8128243c2d46559b4c0024c9168d577 [file] [log] [blame]
Ahmad Sharif795ed172011-06-29 17:24:02 -07001#!/bin/bash
2
Mike Frysingerdf6dd082012-02-16 17:15:29 -05003# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Ahmad Sharif795ed172011-06-29 17:24:02 -07004# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7# TODO: Convert this to python.
8
Mike Frysingerdf6dd082012-02-16 17:15:29 -05009get_all_board_toolchains()
Ahmad Sharif795ed172011-06-29 17:24:02 -070010{
11 local board="$1"
Mike Frysingerdf6dd082012-02-16 17:15:29 -050012 local base_board=$(echo "${board}" | cut -d '_' -f 1)
13 local board_overlay=$(cros_overlay_list --board="${base_board}" \
14 --primary_only)
15 sed 's:#.*::' "${board_overlay}/toolchain.conf"
16}
17
18get_ctarget_from_board()
19{
20 local all_toolchains=( $(get_all_board_toolchains "$@") )
21 echo "${all_toolchains[0]}"
22}
23
24get_board_arch()
25{
26 local ctarget=$(get_ctarget_from_board "$@")
27
28 case ${ctarget} in
29 arm*) echo "arm" ;;
30 i?86*) echo "x86" ;;
31 x86_64*) echo "amd64" ;;
32 *)
33 error "Unable to determine ARCH from toolchain: ${ctarget}"
34 return 1
35 ;;
36 esac
37
38 return 0
Ahmad Sharif795ed172011-06-29 17:24:02 -070039}
40
Ahmad Sharif30dd2d02011-06-30 16:56:24 -070041is_number()
42{
43 echo "$1" | grep -q "^[0-9]\+$"
44}
45
46is_config_installed()
47{
48 gcc-config -l | cut -d" " -f3 | grep -q "$1$"
49}
50
51get_installed_atom_from_config()
52{
53 local gcc_path="$(gcc-config -B "$1")"
54 equery b "$gcc_path" | head -n1
55}
56
Ahmad Sharif795ed172011-06-29 17:24:02 -070057get_atom_from_config()
58{
Ahmad Sharif30dd2d02011-06-30 16:56:24 -070059 echo "$1" | sed -E "s|(.*)-(.*)|cross-\1/gcc-\2|g"
Ahmad Sharif795ed172011-06-29 17:24:02 -070060}
61
62get_ctarget_from_atom()
63{
64 local atom="$1"
65 echo "$atom" | sed -E 's|cross-([^/]+)/.*|\1|g'
66}
67
68copy_gcc_libs_helper()
69{
70 local target_location="$1"
71 local file_path="$2"
72 local dir_path=$(dirname "$file_path")
73 info "Copying $file_path symlink and file to $target_location/$dir_path/."
74 sudo mkdir -p "$target_location/$dir_path"
Ahmad Sharif30dd2d02011-06-30 16:56:24 -070075 sudo cp -a "$file_path"* "$target_location/$dir_path/"
Ahmad Sharif795ed172011-06-29 17:24:02 -070076 local env_d_file="$target_location/etc/env.d/05gcc"
77 info "Adding $dir_path to LDPATH in file $env_d_file"
78 sudo mkdir -p $(dirname "$env_d_file")
79 local line_to_add="LDPATH=\"$dir_path\""
80 if ! grep -q "^$line_to_add$" "$env_d_file" &>/dev/null
81 then
82 echo "$line_to_add" | sudo_append "$env_d_file"
83 fi
84}
85
86copy_gcc_libs()
87{
88 # TODO: Figure out a better way of doing this?
89 local target_location="$1"
90 local atom="$2"
91 local libgcc_file=$(portageq contents / $atom | \
92 grep /libgcc_s.so$)
93 local libstdcxx_file=$(portageq contents / $atom | \
Ahmad Sharif30dd2d02011-06-30 16:56:24 -070094 grep /libstdc++.so$)
Ahmad Sharif795ed172011-06-29 17:24:02 -070095 if [[ -z "$libgcc_file" || -z "$libstdcxx_file" ]]
96 then
97 error "Could not find libgcc_s.so/libstdcxx_s.so. Is\
98 =$atom emerged properly?"
99 return 1
100 fi
101 copy_gcc_libs_helper $target_location $libgcc_file
102 copy_gcc_libs_helper $target_location $libstdcxx_file
Raymes Khourye7a52e42011-11-02 11:07:37 -0700103 sudo ROOT="$target_location" env-update
Ahmad Sharif795ed172011-06-29 17:24:02 -0700104 return 0
105}
106
Ahmad Sharif30dd2d02011-06-30 16:56:24 -0700107get_current_binutils_config()
108{
109 local ctarget="$1"
Ahmad Sharifd32c1cc2011-09-12 11:07:24 -0700110 binutils-config -l | grep "$ctarget" | grep "*" | awk '{print $NF}'
111}
112
113get_bfd_config()
114{
115 local ctarget="$1"
116 binutils-config -l | grep "$ctarget" | grep -v "gold" | head -n1 | \
117 awk '{print $NF}'
Ahmad Sharif30dd2d02011-06-30 16:56:24 -0700118}
119
120emerge_gcc()
121{
122 local atom="$1"
123 local ctarget="$(get_ctarget_from_atom $atom)"
124 mask_file="/etc/portage/package.mask/cross-$ctarget"
125 moved_mask_file=0
126
127 # Move the package mask file elsewhere.
128 if [[ -f "$mask_file" ]]
129 then
130 temp_file="$(mktemp)"
131 sudo mv "$mask_file" "$temp_file"
132 moved_mask_file=1
133 fi
134
135 USE+=" multislot"
136 if echo "$atom" | grep -q "gcc-4.6.0$"
137 then
Ahmad Sharif30dd2d02011-06-30 16:56:24 -0700138 old_binutils_config="$(get_current_binutils_config $ctarget)"
Ahmad Sharifd32c1cc2011-09-12 11:07:24 -0700139 bfd_binutils_config="$(get_bfd_config $ctarget)"
140 if [[ "$old_binutils_config" != "$bfd_binutils_config" ]]
Ahmad Sharif30dd2d02011-06-30 16:56:24 -0700141 then
Ahmad Sharifd32c1cc2011-09-12 11:07:24 -0700142 sudo binutils-config "$bfd_binutils_config"
Ahmad Sharif30dd2d02011-06-30 16:56:24 -0700143 fi
144 fi
145 sudo ACCEPT_KEYWORDS="*" USE="$USE" emerge ="$atom"
146 emerge_retval=$?
147
148 # Move the package mask file back.
149 if [[ $moved_mask_file -eq 1 ]]
150 then
151 sudo mv "$temp_file" "$mask_file"
152 fi
153
154 if [[ ! -z "$old_binutils_config" &&
155 "$old_binutils_config" != "$(get_current_binutils_config $ctarget)" ]]
156 then
157 sudo binutils-config "$old_binutils_config"
158 fi
159
160 return $emerge_retval
161}
162
Ahmad Sharifec4ed4e2011-07-12 14:31:39 -0700163# This function should only be called when testing experimental toolchain
164# compilers. Please don't call this from any other script.
Ahmad Sharif795ed172011-06-29 17:24:02 -0700165cros_gcc_config()
166{
Ahmad Sharif795ed172011-06-29 17:24:02 -0700167 # Return if we're not switching profiles.
168 if [[ "$1" == -* ]]
169 then
Ahmad Sharif30dd2d02011-06-30 16:56:24 -0700170 sudo gcc-config "$1"
171 return $?
Ahmad Sharif795ed172011-06-29 17:24:02 -0700172 fi
173
Ahmad Sharif30dd2d02011-06-30 16:56:24 -0700174 # cros_gcc_config can be called like:
175 # cros_gcc_config <number> to switch config to that
176 # number. In that case we should just try to switch to
177 # that config and not try to install a missing one.
178 if ! is_number "$1" && ! is_config_installed "$1"
179 then
180 info "Configuration $1 not found."
181 info "Trying to emerge it..."
182 local atom="$(get_atom_from_config "$1")"
183 emerge_gcc "$atom" || die "Could not install $atom"
184 fi
185
186 sudo gcc-config "$1" || die "Could not switch to $1"
187
Ahmad Sharifec4ed4e2011-07-12 14:31:39 -0700188 local boards=$(get_boards_from_config "$1")
189 local board
190 for board in $boards
191 do
Ahmad Shariff3dad642011-08-25 11:53:36 -0700192 cros_install_libs_for_config "$board" "$1"
Ahmad Sharifec4ed4e2011-07-12 14:31:39 -0700193 emerge-"$board" --oneshot sys-devel/libtool
194 done
Ahmad Sharif30dd2d02011-06-30 16:56:24 -0700195}
196
Ahmad Sharifec4ed4e2011-07-12 14:31:39 -0700197get_boards_from_config()
Ahmad Sharif30dd2d02011-06-30 16:56:24 -0700198{
199 local atom=$(get_installed_atom_from_config "$1")
Ahmad Sharif795ed172011-06-29 17:24:02 -0700200 if [[ $atom != cross* ]]
201 then
202 warn "$atom is not a cross-compiler."
203 warn "Therefore not adding its libs to the board roots."
204 return 0
205 fi
206
207 # Now copy the lib files into all possible boards.
Ahmad Sharif30dd2d02011-06-30 16:56:24 -0700208 local ctarget="$(get_ctarget_from_atom "$atom")"
Ahmad Sharif795ed172011-06-29 17:24:02 -0700209 for board_root in /build/*
210 do
Ahmad Sharif30dd2d02011-06-30 16:56:24 -0700211 local board="$(basename $board_root)"
212 local board_tc="$(get_ctarget_from_board $board)"
Ahmad Sharif795ed172011-06-29 17:24:02 -0700213 if [[ "${board_tc}" == "${ctarget}" ]]
214 then
Ahmad Sharifec4ed4e2011-07-12 14:31:39 -0700215 echo "$board"
Ahmad Sharif795ed172011-06-29 17:24:02 -0700216 fi
217 done
218}
Ahmad Sharifec4ed4e2011-07-12 14:31:39 -0700219
220cros_install_libs_for_config()
221{
222 local board="$1"
223 local atom=$(get_installed_atom_from_config "$2")
224 copy_gcc_libs /build/"$board" "$atom"
225}