blob: 4e94ba5cf2a1a91cfa2385add53aab7fd471bbe1 [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{
Mike Frysinger5ee554f2012-06-01 16:36:08 -040011 cros_setup_toolchains --show-board-cfg="$1" | sed 's:,: :g'
Mike Frysingerdf6dd082012-02-16 17:15:29 -050012}
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 12:25:01 -050024 # 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 17:15:29 -050027 error "Unable to determine ARCH from toolchain: ${ctarget}"
28 return 1
Mike Frysingerb4342af2012-02-28 12:25:01 -050029 fi
Mike Frysingerdf6dd082012-02-16 17:15:29 -050030
Mike Frysingerb4342af2012-02-28 12:25:01 -050031 echo "${arch}"
Mike Frysingerdf6dd082012-02-16 17:15:29 -050032 return 0
Ahmad Sharif795ed172011-06-29 17:24:02 -070033}
34
Ahmad Sharif30dd2d02011-06-30 16:56:24 -070035is_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-29 17:24:02 -070051get_atom_from_config()
52{
Ahmad Sharif30dd2d02011-06-30 16:56:24 -070053 echo "$1" | sed -E "s|(.*)-(.*)|cross-\1/gcc-\2|g"
Ahmad Sharif795ed172011-06-29 17:24:02 -070054}
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 16:56:24 -070062get_current_binutils_config()
63{
64 local ctarget="$1"
Ahmad Sharifd32c1cc2011-09-12 11:07:24 -070065 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 16:56:24 -070073}
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 16:56:24 -070093 old_binutils_config="$(get_current_binutils_config $ctarget)"
Ahmad Sharifd32c1cc2011-09-12 11:07:24 -070094 bfd_binutils_config="$(get_bfd_config $ctarget)"
95 if [[ "$old_binutils_config" != "$bfd_binutils_config" ]]
Ahmad Sharif30dd2d02011-06-30 16:56:24 -070096 then
Ahmad Sharifd32c1cc2011-09-12 11:07:24 -070097 sudo binutils-config "$bfd_binutils_config"
Ahmad Sharif30dd2d02011-06-30 16:56:24 -070098 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 14:31:39 -0700118# This function should only be called when testing experimental toolchain
119# compilers. Please don't call this from any other script.
Ahmad Sharif795ed172011-06-29 17:24:02 -0700120cros_gcc_config()
121{
Ahmad Sharif795ed172011-06-29 17:24:02 -0700122 # Return if we're not switching profiles.
123 if [[ "$1" == -* ]]
124 then
Ahmad Sharif30dd2d02011-06-30 16:56:24 -0700125 sudo gcc-config "$1"
126 return $?
Ahmad Sharif795ed172011-06-29 17:24:02 -0700127 fi
128
Ahmad Sharif30dd2d02011-06-30 16:56:24 -0700129 # 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 14:31:39 -0700143 local boards=$(get_boards_from_config "$1")
144 local board
145 for board in $boards
146 do
Ahmad Sharifec4ed4e2011-07-12 14:31:39 -0700147 emerge-"$board" --oneshot sys-devel/libtool
148 done
Ahmad Sharif30dd2d02011-06-30 16:56:24 -0700149}
150
Ahmad Sharifec4ed4e2011-07-12 14:31:39 -0700151get_boards_from_config()
Ahmad Sharif30dd2d02011-06-30 16:56:24 -0700152{
153 local atom=$(get_installed_atom_from_config "$1")
Ahmad Sharif795ed172011-06-29 17:24:02 -0700154 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 16:56:24 -0700162 local ctarget="$(get_ctarget_from_atom "$atom")"
Ahmad Sharif795ed172011-06-29 17:24:02 -0700163 for board_root in /build/*
164 do
Ahmad Sharif30dd2d02011-06-30 16:56:24 -0700165 local board="$(basename $board_root)"
166 local board_tc="$(get_ctarget_from_board $board)"
Ahmad Sharif795ed172011-06-29 17:24:02 -0700167 if [[ "${board_tc}" == "${ctarget}" ]]
168 then
Ahmad Sharifec4ed4e2011-07-12 14:31:39 -0700169 echo "$board"
Ahmad Sharif795ed172011-06-29 17:24:02 -0700170 fi
171 done
172}