blob: a4ff8d31be6d88003329fdba30d4ea6f927fda38 [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
Mike Frysingerb4342af2012-02-28 12:25:01 -050028 # Ask crossdev what the magical portage arch is!
29 local arch=$(eval $(crossdev --show-target-cfg "${ctarget}"); echo ${arch})
30 if [[ -z ${arch} ]] ; then
Mike Frysingerdf6dd082012-02-16 17:15:29 -050031 error "Unable to determine ARCH from toolchain: ${ctarget}"
32 return 1
Mike Frysingerb4342af2012-02-28 12:25:01 -050033 fi
Mike Frysingerdf6dd082012-02-16 17:15:29 -050034
Mike Frysingerb4342af2012-02-28 12:25:01 -050035 echo "${arch}"
Mike Frysingerdf6dd082012-02-16 17:15:29 -050036 return 0
Ahmad Sharif795ed172011-06-29 17:24:02 -070037}
38
Ahmad Sharif30dd2d02011-06-30 16:56:24 -070039is_number()
40{
41 echo "$1" | grep -q "^[0-9]\+$"
42}
43
44is_config_installed()
45{
46 gcc-config -l | cut -d" " -f3 | grep -q "$1$"
47}
48
49get_installed_atom_from_config()
50{
51 local gcc_path="$(gcc-config -B "$1")"
52 equery b "$gcc_path" | head -n1
53}
54
Ahmad Sharif795ed172011-06-29 17:24:02 -070055get_atom_from_config()
56{
Ahmad Sharif30dd2d02011-06-30 16:56:24 -070057 echo "$1" | sed -E "s|(.*)-(.*)|cross-\1/gcc-\2|g"
Ahmad Sharif795ed172011-06-29 17:24:02 -070058}
59
60get_ctarget_from_atom()
61{
62 local atom="$1"
63 echo "$atom" | sed -E 's|cross-([^/]+)/.*|\1|g'
64}
65
66copy_gcc_libs_helper()
67{
68 local target_location="$1"
69 local file_path="$2"
70 local dir_path=$(dirname "$file_path")
71 info "Copying $file_path symlink and file to $target_location/$dir_path/."
72 sudo mkdir -p "$target_location/$dir_path"
Ahmad Sharif30dd2d02011-06-30 16:56:24 -070073 sudo cp -a "$file_path"* "$target_location/$dir_path/"
Ahmad Sharif795ed172011-06-29 17:24:02 -070074 local env_d_file="$target_location/etc/env.d/05gcc"
75 info "Adding $dir_path to LDPATH in file $env_d_file"
76 sudo mkdir -p $(dirname "$env_d_file")
77 local line_to_add="LDPATH=\"$dir_path\""
78 if ! grep -q "^$line_to_add$" "$env_d_file" &>/dev/null
79 then
80 echo "$line_to_add" | sudo_append "$env_d_file"
81 fi
82}
83
84copy_gcc_libs()
85{
86 # TODO: Figure out a better way of doing this?
87 local target_location="$1"
88 local atom="$2"
89 local libgcc_file=$(portageq contents / $atom | \
90 grep /libgcc_s.so$)
91 local libstdcxx_file=$(portageq contents / $atom | \
Ahmad Sharif30dd2d02011-06-30 16:56:24 -070092 grep /libstdc++.so$)
Ahmad Sharif795ed172011-06-29 17:24:02 -070093 if [[ -z "$libgcc_file" || -z "$libstdcxx_file" ]]
94 then
95 error "Could not find libgcc_s.so/libstdcxx_s.so. Is\
96 =$atom emerged properly?"
97 return 1
98 fi
99 copy_gcc_libs_helper $target_location $libgcc_file
100 copy_gcc_libs_helper $target_location $libstdcxx_file
Raymes Khourye7a52e42011-11-02 11:07:37 -0700101 sudo ROOT="$target_location" env-update
Ahmad Sharif795ed172011-06-29 17:24:02 -0700102 return 0
103}
104
Ahmad Sharif30dd2d02011-06-30 16:56:24 -0700105get_current_binutils_config()
106{
107 local ctarget="$1"
Ahmad Sharifd32c1cc2011-09-12 11:07:24 -0700108 binutils-config -l | grep "$ctarget" | grep "*" | awk '{print $NF}'
109}
110
111get_bfd_config()
112{
113 local ctarget="$1"
114 binutils-config -l | grep "$ctarget" | grep -v "gold" | head -n1 | \
115 awk '{print $NF}'
Ahmad Sharif30dd2d02011-06-30 16:56:24 -0700116}
117
118emerge_gcc()
119{
120 local atom="$1"
121 local ctarget="$(get_ctarget_from_atom $atom)"
122 mask_file="/etc/portage/package.mask/cross-$ctarget"
123 moved_mask_file=0
124
125 # Move the package mask file elsewhere.
126 if [[ -f "$mask_file" ]]
127 then
128 temp_file="$(mktemp)"
129 sudo mv "$mask_file" "$temp_file"
130 moved_mask_file=1
131 fi
132
133 USE+=" multislot"
134 if echo "$atom" | grep -q "gcc-4.6.0$"
135 then
Ahmad Sharif30dd2d02011-06-30 16:56:24 -0700136 old_binutils_config="$(get_current_binutils_config $ctarget)"
Ahmad Sharifd32c1cc2011-09-12 11:07:24 -0700137 bfd_binutils_config="$(get_bfd_config $ctarget)"
138 if [[ "$old_binutils_config" != "$bfd_binutils_config" ]]
Ahmad Sharif30dd2d02011-06-30 16:56:24 -0700139 then
Ahmad Sharifd32c1cc2011-09-12 11:07:24 -0700140 sudo binutils-config "$bfd_binutils_config"
Ahmad Sharif30dd2d02011-06-30 16:56:24 -0700141 fi
142 fi
143 sudo ACCEPT_KEYWORDS="*" USE="$USE" emerge ="$atom"
144 emerge_retval=$?
145
146 # Move the package mask file back.
147 if [[ $moved_mask_file -eq 1 ]]
148 then
149 sudo mv "$temp_file" "$mask_file"
150 fi
151
152 if [[ ! -z "$old_binutils_config" &&
153 "$old_binutils_config" != "$(get_current_binutils_config $ctarget)" ]]
154 then
155 sudo binutils-config "$old_binutils_config"
156 fi
157
158 return $emerge_retval
159}
160
Ahmad Sharifec4ed4e2011-07-12 14:31:39 -0700161# This function should only be called when testing experimental toolchain
162# compilers. Please don't call this from any other script.
Ahmad Sharif795ed172011-06-29 17:24:02 -0700163cros_gcc_config()
164{
Ahmad Sharif795ed172011-06-29 17:24:02 -0700165 # Return if we're not switching profiles.
166 if [[ "$1" == -* ]]
167 then
Ahmad Sharif30dd2d02011-06-30 16:56:24 -0700168 sudo gcc-config "$1"
169 return $?
Ahmad Sharif795ed172011-06-29 17:24:02 -0700170 fi
171
Ahmad Sharif30dd2d02011-06-30 16:56:24 -0700172 # cros_gcc_config can be called like:
173 # cros_gcc_config <number> to switch config to that
174 # number. In that case we should just try to switch to
175 # that config and not try to install a missing one.
176 if ! is_number "$1" && ! is_config_installed "$1"
177 then
178 info "Configuration $1 not found."
179 info "Trying to emerge it..."
180 local atom="$(get_atom_from_config "$1")"
181 emerge_gcc "$atom" || die "Could not install $atom"
182 fi
183
184 sudo gcc-config "$1" || die "Could not switch to $1"
185
Ahmad Sharifec4ed4e2011-07-12 14:31:39 -0700186 local boards=$(get_boards_from_config "$1")
187 local board
188 for board in $boards
189 do
Ahmad Shariff3dad642011-08-25 11:53:36 -0700190 cros_install_libs_for_config "$board" "$1"
Ahmad Sharifec4ed4e2011-07-12 14:31:39 -0700191 emerge-"$board" --oneshot sys-devel/libtool
192 done
Ahmad Sharif30dd2d02011-06-30 16:56:24 -0700193}
194
Ahmad Sharifec4ed4e2011-07-12 14:31:39 -0700195get_boards_from_config()
Ahmad Sharif30dd2d02011-06-30 16:56:24 -0700196{
197 local atom=$(get_installed_atom_from_config "$1")
Ahmad Sharif795ed172011-06-29 17:24:02 -0700198 if [[ $atom != cross* ]]
199 then
200 warn "$atom is not a cross-compiler."
201 warn "Therefore not adding its libs to the board roots."
202 return 0
203 fi
204
205 # Now copy the lib files into all possible boards.
Ahmad Sharif30dd2d02011-06-30 16:56:24 -0700206 local ctarget="$(get_ctarget_from_atom "$atom")"
Ahmad Sharif795ed172011-06-29 17:24:02 -0700207 for board_root in /build/*
208 do
Ahmad Sharif30dd2d02011-06-30 16:56:24 -0700209 local board="$(basename $board_root)"
210 local board_tc="$(get_ctarget_from_board $board)"
Ahmad Sharif795ed172011-06-29 17:24:02 -0700211 if [[ "${board_tc}" == "${ctarget}" ]]
212 then
Ahmad Sharifec4ed4e2011-07-12 14:31:39 -0700213 echo "$board"
Ahmad Sharif795ed172011-06-29 17:24:02 -0700214 fi
215 done
216}
Ahmad Sharifec4ed4e2011-07-12 14:31:39 -0700217
218cros_install_libs_for_config()
219{
220 local board="$1"
221 local atom=$(get_installed_atom_from_config "$2")
222 copy_gcc_libs /build/"$board" "$atom"
223}