blob: 1f344b213c47e25fc42bb2934a5c743d9f297958 [file] [log] [blame]
Ahmad Sharif795ed172011-06-29 17:24:02 -07001#!/bin/bash
2
3# Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
4# 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
9get_ctarget_from_board()
10{
11 local board="$1"
Ahmad Sharif30dd2d02011-06-30 16:56:24 -070012 local base_board="$(echo $board | cut -d '_' -f 1)"
13 local board_overlay="$(cros_overlay_list --board="$base_board"\
14 --primary_only)"
Ahmad Sharif795ed172011-06-29 17:24:02 -070015 cat "$board_overlay/toolchain.conf"
16}
17
Ahmad Sharif30dd2d02011-06-30 16:56:24 -070018is_number()
19{
20 echo "$1" | grep -q "^[0-9]\+$"
21}
22
23is_config_installed()
24{
25 gcc-config -l | cut -d" " -f3 | grep -q "$1$"
26}
27
28get_installed_atom_from_config()
29{
30 local gcc_path="$(gcc-config -B "$1")"
31 equery b "$gcc_path" | head -n1
32}
33
Ahmad Sharif795ed172011-06-29 17:24:02 -070034get_atom_from_config()
35{
Ahmad Sharif30dd2d02011-06-30 16:56:24 -070036 echo "$1" | sed -E "s|(.*)-(.*)|cross-\1/gcc-\2|g"
Ahmad Sharif795ed172011-06-29 17:24:02 -070037}
38
39get_ctarget_from_atom()
40{
41 local atom="$1"
42 echo "$atom" | sed -E 's|cross-([^/]+)/.*|\1|g'
43}
44
45copy_gcc_libs_helper()
46{
47 local target_location="$1"
48 local file_path="$2"
49 local dir_path=$(dirname "$file_path")
50 info "Copying $file_path symlink and file to $target_location/$dir_path/."
51 sudo mkdir -p "$target_location/$dir_path"
Ahmad Sharif30dd2d02011-06-30 16:56:24 -070052 sudo cp -a "$file_path"* "$target_location/$dir_path/"
Ahmad Sharif795ed172011-06-29 17:24:02 -070053 local env_d_file="$target_location/etc/env.d/05gcc"
54 info "Adding $dir_path to LDPATH in file $env_d_file"
55 sudo mkdir -p $(dirname "$env_d_file")
56 local line_to_add="LDPATH=\"$dir_path\""
57 if ! grep -q "^$line_to_add$" "$env_d_file" &>/dev/null
58 then
59 echo "$line_to_add" | sudo_append "$env_d_file"
60 fi
61}
62
63copy_gcc_libs()
64{
65 # TODO: Figure out a better way of doing this?
66 local target_location="$1"
67 local atom="$2"
68 local libgcc_file=$(portageq contents / $atom | \
69 grep /libgcc_s.so$)
70 local libstdcxx_file=$(portageq contents / $atom | \
Ahmad Sharif30dd2d02011-06-30 16:56:24 -070071 grep /libstdc++.so$)
Ahmad Sharif795ed172011-06-29 17:24:02 -070072 if [[ -z "$libgcc_file" || -z "$libstdcxx_file" ]]
73 then
74 error "Could not find libgcc_s.so/libstdcxx_s.so. Is\
75 =$atom emerged properly?"
76 return 1
77 fi
78 copy_gcc_libs_helper $target_location $libgcc_file
79 copy_gcc_libs_helper $target_location $libstdcxx_file
80 return 0
81}
82
Ahmad Sharif30dd2d02011-06-30 16:56:24 -070083get_current_binutils_config()
84{
85 local ctarget="$1"
Ahmad Sharifd32c1cc2011-09-12 11:07:24 -070086 binutils-config -l | grep "$ctarget" | grep "*" | awk '{print $NF}'
87}
88
89get_bfd_config()
90{
91 local ctarget="$1"
92 binutils-config -l | grep "$ctarget" | grep -v "gold" | head -n1 | \
93 awk '{print $NF}'
Ahmad Sharif30dd2d02011-06-30 16:56:24 -070094}
95
96emerge_gcc()
97{
98 local atom="$1"
99 local ctarget="$(get_ctarget_from_atom $atom)"
100 mask_file="/etc/portage/package.mask/cross-$ctarget"
101 moved_mask_file=0
102
103 # Move the package mask file elsewhere.
104 if [[ -f "$mask_file" ]]
105 then
106 temp_file="$(mktemp)"
107 sudo mv "$mask_file" "$temp_file"
108 moved_mask_file=1
109 fi
110
111 USE+=" multislot"
112 if echo "$atom" | grep -q "gcc-4.6.0$"
113 then
Ahmad Sharif30dd2d02011-06-30 16:56:24 -0700114 old_binutils_config="$(get_current_binutils_config $ctarget)"
Ahmad Sharifd32c1cc2011-09-12 11:07:24 -0700115 bfd_binutils_config="$(get_bfd_config $ctarget)"
116 if [[ "$old_binutils_config" != "$bfd_binutils_config" ]]
Ahmad Sharif30dd2d02011-06-30 16:56:24 -0700117 then
Ahmad Sharifd32c1cc2011-09-12 11:07:24 -0700118 sudo binutils-config "$bfd_binutils_config"
Ahmad Sharif30dd2d02011-06-30 16:56:24 -0700119 fi
120 fi
121 sudo ACCEPT_KEYWORDS="*" USE="$USE" emerge ="$atom"
122 emerge_retval=$?
123
124 # Move the package mask file back.
125 if [[ $moved_mask_file -eq 1 ]]
126 then
127 sudo mv "$temp_file" "$mask_file"
128 fi
129
130 if [[ ! -z "$old_binutils_config" &&
131 "$old_binutils_config" != "$(get_current_binutils_config $ctarget)" ]]
132 then
133 sudo binutils-config "$old_binutils_config"
134 fi
135
136 return $emerge_retval
137}
138
Ahmad Sharifec4ed4e2011-07-12 14:31:39 -0700139# This function should only be called when testing experimental toolchain
140# compilers. Please don't call this from any other script.
Ahmad Sharif795ed172011-06-29 17:24:02 -0700141cros_gcc_config()
142{
Ahmad Sharif795ed172011-06-29 17:24:02 -0700143 # Return if we're not switching profiles.
144 if [[ "$1" == -* ]]
145 then
Ahmad Sharif30dd2d02011-06-30 16:56:24 -0700146 sudo gcc-config "$1"
147 return $?
Ahmad Sharif795ed172011-06-29 17:24:02 -0700148 fi
149
Ahmad Sharif30dd2d02011-06-30 16:56:24 -0700150 # cros_gcc_config can be called like:
151 # cros_gcc_config <number> to switch config to that
152 # number. In that case we should just try to switch to
153 # that config and not try to install a missing one.
154 if ! is_number "$1" && ! is_config_installed "$1"
155 then
156 info "Configuration $1 not found."
157 info "Trying to emerge it..."
158 local atom="$(get_atom_from_config "$1")"
159 emerge_gcc "$atom" || die "Could not install $atom"
160 fi
161
162 sudo gcc-config "$1" || die "Could not switch to $1"
163
Ahmad Sharifec4ed4e2011-07-12 14:31:39 -0700164 local boards=$(get_boards_from_config "$1")
165 local board
166 for board in $boards
167 do
Ahmad Shariff3dad642011-08-25 11:53:36 -0700168 cros_install_libs_for_config "$board" "$1"
Ahmad Sharifec4ed4e2011-07-12 14:31:39 -0700169 emerge-"$board" --oneshot sys-devel/libtool
170 done
Ahmad Sharif30dd2d02011-06-30 16:56:24 -0700171}
172
Ahmad Sharifec4ed4e2011-07-12 14:31:39 -0700173get_boards_from_config()
Ahmad Sharif30dd2d02011-06-30 16:56:24 -0700174{
175 local atom=$(get_installed_atom_from_config "$1")
Ahmad Sharif795ed172011-06-29 17:24:02 -0700176 if [[ $atom != cross* ]]
177 then
178 warn "$atom is not a cross-compiler."
179 warn "Therefore not adding its libs to the board roots."
180 return 0
181 fi
182
183 # Now copy the lib files into all possible boards.
Ahmad Sharif30dd2d02011-06-30 16:56:24 -0700184 local ctarget="$(get_ctarget_from_atom "$atom")"
Ahmad Sharif795ed172011-06-29 17:24:02 -0700185 for board_root in /build/*
186 do
Ahmad Sharif30dd2d02011-06-30 16:56:24 -0700187 local board="$(basename $board_root)"
188 local board_tc="$(get_ctarget_from_board $board)"
Ahmad Sharif795ed172011-06-29 17:24:02 -0700189 if [[ "${board_tc}" == "${ctarget}" ]]
190 then
Ahmad Sharifec4ed4e2011-07-12 14:31:39 -0700191 echo "$board"
Ahmad Sharif795ed172011-06-29 17:24:02 -0700192 fi
193 done
194}
Ahmad Sharifec4ed4e2011-07-12 14:31:39 -0700195
196cros_install_libs_for_config()
197{
198 local board="$1"
199 local atom=$(get_installed_atom_from_config "$2")
200 copy_gcc_libs /build/"$board" "$atom"
201}