blob: 4e94ba5cf2a1a91cfa2385add53aab7fd471bbe1 [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
Ahmad Sharif30dd2d02011-06-30 23:56:2462get_current_binutils_config()
63{
64 local ctarget="$1"
Ahmad Sharifd32c1cc2011-09-12 18:07:2465 binutils-config -l | grep "$ctarget" | grep "*" | awk '{print $NF}'
66}
67
68get_bfd_config()
69{
70 local ctarget="$1"
71 binutils-config -l | grep "$ctarget" | grep -v "gold" | head -n1 | \
72 awk '{print $NF}'
Ahmad Sharif30dd2d02011-06-30 23:56:2473}
74
75emerge_gcc()
76{
77 local atom="$1"
78 local ctarget="$(get_ctarget_from_atom $atom)"
79 mask_file="/etc/portage/package.mask/cross-$ctarget"
80 moved_mask_file=0
81
82 # Move the package mask file elsewhere.
83 if [[ -f "$mask_file" ]]
84 then
85 temp_file="$(mktemp)"
86 sudo mv "$mask_file" "$temp_file"
87 moved_mask_file=1
88 fi
89
90 USE+=" multislot"
91 if echo "$atom" | grep -q "gcc-4.6.0$"
92 then
Ahmad Sharif30dd2d02011-06-30 23:56:2493 old_binutils_config="$(get_current_binutils_config $ctarget)"
Ahmad Sharifd32c1cc2011-09-12 18:07:2494 bfd_binutils_config="$(get_bfd_config $ctarget)"
95 if [[ "$old_binutils_config" != "$bfd_binutils_config" ]]
Ahmad Sharif30dd2d02011-06-30 23:56:2496 then
Ahmad Sharifd32c1cc2011-09-12 18:07:2497 sudo binutils-config "$bfd_binutils_config"
Ahmad Sharif30dd2d02011-06-30 23:56:2498 fi
99 fi
100 sudo ACCEPT_KEYWORDS="*" USE="$USE" emerge ="$atom"
101 emerge_retval=$?
102
103 # Move the package mask file back.
104 if [[ $moved_mask_file -eq 1 ]]
105 then
106 sudo mv "$temp_file" "$mask_file"
107 fi
108
109 if [[ ! -z "$old_binutils_config" &&
110 "$old_binutils_config" != "$(get_current_binutils_config $ctarget)" ]]
111 then
112 sudo binutils-config "$old_binutils_config"
113 fi
114
115 return $emerge_retval
116}
117
Ahmad Sharifec4ed4e2011-07-12 21:31:39118# This function should only be called when testing experimental toolchain
119# compilers. Please don't call this from any other script.
Ahmad Sharif795ed172011-06-30 00:24:02120cros_gcc_config()
121{
Ahmad Sharif795ed172011-06-30 00:24:02122 # Return if we're not switching profiles.
123 if [[ "$1" == -* ]]
124 then
Ahmad Sharif30dd2d02011-06-30 23:56:24125 sudo gcc-config "$1"
126 return $?
Ahmad Sharif795ed172011-06-30 00:24:02127 fi
128
Ahmad Sharif30dd2d02011-06-30 23:56:24129 # cros_gcc_config can be called like:
130 # cros_gcc_config <number> to switch config to that
131 # number. In that case we should just try to switch to
132 # that config and not try to install a missing one.
133 if ! is_number "$1" && ! is_config_installed "$1"
134 then
135 info "Configuration $1 not found."
136 info "Trying to emerge it..."
137 local atom="$(get_atom_from_config "$1")"
138 emerge_gcc "$atom" || die "Could not install $atom"
139 fi
140
141 sudo gcc-config "$1" || die "Could not switch to $1"
142
Ahmad Sharifec4ed4e2011-07-12 21:31:39143 local boards=$(get_boards_from_config "$1")
144 local board
145 for board in $boards
146 do
Ahmad Sharifec4ed4e2011-07-12 21:31:39147 emerge-"$board" --oneshot sys-devel/libtool
148 done
Ahmad Sharif30dd2d02011-06-30 23:56:24149}
150
Ahmad Sharifec4ed4e2011-07-12 21:31:39151get_boards_from_config()
Ahmad Sharif30dd2d02011-06-30 23:56:24152{
153 local atom=$(get_installed_atom_from_config "$1")
Ahmad Sharif795ed172011-06-30 00:24:02154 if [[ $atom != cross* ]]
155 then
156 warn "$atom is not a cross-compiler."
157 warn "Therefore not adding its libs to the board roots."
158 return 0
159 fi
160
161 # Now copy the lib files into all possible boards.
Ahmad Sharif30dd2d02011-06-30 23:56:24162 local ctarget="$(get_ctarget_from_atom "$atom")"
Ahmad Sharif795ed172011-06-30 00:24:02163 for board_root in /build/*
164 do
Ahmad Sharif30dd2d02011-06-30 23:56:24165 local board="$(basename $board_root)"
166 local board_tc="$(get_ctarget_from_board $board)"
Ahmad Sharif795ed172011-06-30 00:24:02167 if [[ "${board_tc}" == "${ctarget}" ]]
168 then
Ahmad Sharifec4ed4e2011-07-12 21:31:39169 echo "$board"
Ahmad Sharif795ed172011-06-30 00:24:02170 fi
171 done
172}