blob: f4a368d98b867b8bf20f03693561d5b583ba277a [file] [log] [blame]
Ahmad Sharif795ed172011-06-30 00:24:021#!/bin/bash
2
Mike Frysingerdf6dd082012-02-16 22:15:293# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Ahmad Sharif795ed172011-06-30 00:24:024# 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 22:15:299get_all_board_toolchains()
Ahmad Sharif795ed172011-06-30 00:24:0210{
Mike Frysinger5ee554f2012-06-01 20:36:0811 cros_setup_toolchains --show-board-cfg="$1" | sed 's:,: :g'
Mike Frysingerdf6dd082012-02-16 22:15:2912}
13
14get_ctarget_from_board()
15{
16 local all_toolchains=( $(get_all_board_toolchains "$@") )
17 echo "${all_toolchains[0]}"
18}
19
20get_board_arch()
21{
22 local ctarget=$(get_ctarget_from_board "$@")
23
Mike Frysingerb4342af2012-02-28 17:25:0124 # Ask crossdev what the magical portage arch is!
25 local arch=$(eval $(crossdev --show-target-cfg "${ctarget}"); echo ${arch})
26 if [[ -z ${arch} ]] ; then
Mike Frysingerdf6dd082012-02-16 22:15:2927 error "Unable to determine ARCH from toolchain: ${ctarget}"
28 return 1
Mike Frysingerb4342af2012-02-28 17:25:0129 fi
Mike Frysingerdf6dd082012-02-16 22:15:2930
Mike Frysingerb4342af2012-02-28 17:25:0131 echo "${arch}"
Mike Frysingerdf6dd082012-02-16 22:15:2932 return 0
Ahmad Sharif795ed172011-06-30 00:24:0233}
34
Ahmad Sharif30dd2d02011-06-30 23:56:2435is_number()
36{
37 echo "$1" | grep -q "^[0-9]\+$"
38}
39
40is_config_installed()
41{
42 gcc-config -l | cut -d" " -f3 | grep -q "$1$"
43}
44
45get_installed_atom_from_config()
46{
47 local gcc_path="$(gcc-config -B "$1")"
48 equery b "$gcc_path" | head -n1
49}
50
Ahmad Sharif795ed172011-06-30 00:24:0251get_atom_from_config()
52{
Ahmad Sharif30dd2d02011-06-30 23:56:2453 echo "$1" | sed -E "s|(.*)-(.*)|cross-\1/gcc-\2|g"
Ahmad Sharif795ed172011-06-30 00:24:0254}
55
56get_ctarget_from_atom()
57{
58 local atom="$1"
59 echo "$atom" | sed -E 's|cross-([^/]+)/.*|\1|g'
60}
61
62copy_gcc_libs_helper()
63{
64 local target_location="$1"
65 local file_path="$2"
66 local dir_path=$(dirname "$file_path")
67 info "Copying $file_path symlink and file to $target_location/$dir_path/."
68 sudo mkdir -p "$target_location/$dir_path"
Ahmad Sharif30dd2d02011-06-30 23:56:2469 sudo cp -a "$file_path"* "$target_location/$dir_path/"
Ahmad Sharif795ed172011-06-30 00:24:0270 local env_d_file="$target_location/etc/env.d/05gcc"
71 info "Adding $dir_path to LDPATH in file $env_d_file"
72 sudo mkdir -p $(dirname "$env_d_file")
73 local line_to_add="LDPATH=\"$dir_path\""
74 if ! grep -q "^$line_to_add$" "$env_d_file" &>/dev/null
75 then
76 echo "$line_to_add" | sudo_append "$env_d_file"
77 fi
78}
79
80copy_gcc_libs()
81{
82 # TODO: Figure out a better way of doing this?
83 local target_location="$1"
84 local atom="$2"
85 local libgcc_file=$(portageq contents / $atom | \
86 grep /libgcc_s.so$)
87 local libstdcxx_file=$(portageq contents / $atom | \
Ahmad Sharif30dd2d02011-06-30 23:56:2488 grep /libstdc++.so$)
Ahmad Sharif795ed172011-06-30 00:24:0289 if [[ -z "$libgcc_file" || -z "$libstdcxx_file" ]]
90 then
91 error "Could not find libgcc_s.so/libstdcxx_s.so. Is\
92 =$atom emerged properly?"
93 return 1
94 fi
95 copy_gcc_libs_helper $target_location $libgcc_file
96 copy_gcc_libs_helper $target_location $libstdcxx_file
Raymes Khourye7a52e42011-11-02 18:07:3797 sudo ROOT="$target_location" env-update
Ahmad Sharif795ed172011-06-30 00:24:0298 return 0
99}
100
Ahmad Sharif30dd2d02011-06-30 23:56:24101get_current_binutils_config()
102{
103 local ctarget="$1"
Ahmad Sharifd32c1cc2011-09-12 18:07:24104 binutils-config -l | grep "$ctarget" | grep "*" | awk '{print $NF}'
105}
106
107get_bfd_config()
108{
109 local ctarget="$1"
110 binutils-config -l | grep "$ctarget" | grep -v "gold" | head -n1 | \
111 awk '{print $NF}'
Ahmad Sharif30dd2d02011-06-30 23:56:24112}
113
114emerge_gcc()
115{
116 local atom="$1"
117 local ctarget="$(get_ctarget_from_atom $atom)"
118 mask_file="/etc/portage/package.mask/cross-$ctarget"
119 moved_mask_file=0
120
121 # Move the package mask file elsewhere.
122 if [[ -f "$mask_file" ]]
123 then
124 temp_file="$(mktemp)"
125 sudo mv "$mask_file" "$temp_file"
126 moved_mask_file=1
127 fi
128
129 USE+=" multislot"
130 if echo "$atom" | grep -q "gcc-4.6.0$"
131 then
Ahmad Sharif30dd2d02011-06-30 23:56:24132 old_binutils_config="$(get_current_binutils_config $ctarget)"
Ahmad Sharifd32c1cc2011-09-12 18:07:24133 bfd_binutils_config="$(get_bfd_config $ctarget)"
134 if [[ "$old_binutils_config" != "$bfd_binutils_config" ]]
Ahmad Sharif30dd2d02011-06-30 23:56:24135 then
Ahmad Sharifd32c1cc2011-09-12 18:07:24136 sudo binutils-config "$bfd_binutils_config"
Ahmad Sharif30dd2d02011-06-30 23:56:24137 fi
138 fi
139 sudo ACCEPT_KEYWORDS="*" USE="$USE" emerge ="$atom"
140 emerge_retval=$?
141
142 # Move the package mask file back.
143 if [[ $moved_mask_file -eq 1 ]]
144 then
145 sudo mv "$temp_file" "$mask_file"
146 fi
147
148 if [[ ! -z "$old_binutils_config" &&
149 "$old_binutils_config" != "$(get_current_binutils_config $ctarget)" ]]
150 then
151 sudo binutils-config "$old_binutils_config"
152 fi
153
154 return $emerge_retval
155}
156
Ahmad Sharifec4ed4e2011-07-12 21:31:39157# This function should only be called when testing experimental toolchain
158# compilers. Please don't call this from any other script.
Ahmad Sharif795ed172011-06-30 00:24:02159cros_gcc_config()
160{
Ahmad Sharif795ed172011-06-30 00:24:02161 # Return if we're not switching profiles.
162 if [[ "$1" == -* ]]
163 then
Ahmad Sharif30dd2d02011-06-30 23:56:24164 sudo gcc-config "$1"
165 return $?
Ahmad Sharif795ed172011-06-30 00:24:02166 fi
167
Ahmad Sharif30dd2d02011-06-30 23:56:24168 # cros_gcc_config can be called like:
169 # cros_gcc_config <number> to switch config to that
170 # number. In that case we should just try to switch to
171 # that config and not try to install a missing one.
172 if ! is_number "$1" && ! is_config_installed "$1"
173 then
174 info "Configuration $1 not found."
175 info "Trying to emerge it..."
176 local atom="$(get_atom_from_config "$1")"
177 emerge_gcc "$atom" || die "Could not install $atom"
178 fi
179
180 sudo gcc-config "$1" || die "Could not switch to $1"
181
Ahmad Sharifec4ed4e2011-07-12 21:31:39182 local boards=$(get_boards_from_config "$1")
183 local board
184 for board in $boards
185 do
Ahmad Shariff3dad642011-08-25 18:53:36186 cros_install_libs_for_config "$board" "$1"
Ahmad Sharifec4ed4e2011-07-12 21:31:39187 emerge-"$board" --oneshot sys-devel/libtool
188 done
Ahmad Sharif30dd2d02011-06-30 23:56:24189}
190
Ahmad Sharifec4ed4e2011-07-12 21:31:39191get_boards_from_config()
Ahmad Sharif30dd2d02011-06-30 23:56:24192{
193 local atom=$(get_installed_atom_from_config "$1")
Ahmad Sharif795ed172011-06-30 00:24:02194 if [[ $atom != cross* ]]
195 then
196 warn "$atom is not a cross-compiler."
197 warn "Therefore not adding its libs to the board roots."
198 return 0
199 fi
200
201 # Now copy the lib files into all possible boards.
Ahmad Sharif30dd2d02011-06-30 23:56:24202 local ctarget="$(get_ctarget_from_atom "$atom")"
Ahmad Sharif795ed172011-06-30 00:24:02203 for board_root in /build/*
204 do
Ahmad Sharif30dd2d02011-06-30 23:56:24205 local board="$(basename $board_root)"
206 local board_tc="$(get_ctarget_from_board $board)"
Ahmad Sharif795ed172011-06-30 00:24:02207 if [[ "${board_tc}" == "${ctarget}" ]]
208 then
Ahmad Sharifec4ed4e2011-07-12 21:31:39209 echo "$board"
Ahmad Sharif795ed172011-06-30 00:24:02210 fi
211 done
212}
Ahmad Sharifec4ed4e2011-07-12 21:31:39213
214cros_install_libs_for_config()
215{
216 local board="$1"
217 local atom=$(get_installed_atom_from_config "$2")
218 copy_gcc_libs /build/"$board" "$atom"
219}