Ahmad Sharif | 795ed17 | 2011-06-30 00:24:02 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
Mike Frysinger | df6dd08 | 2012-02-16 22:15:29 | [diff] [blame] | 3 | # Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Ahmad Sharif | 795ed17 | 2011-06-30 00:24:02 | [diff] [blame] | 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 | |
Mike Frysinger | df6dd08 | 2012-02-16 22:15:29 | [diff] [blame] | 9 | get_all_board_toolchains() |
Ahmad Sharif | 795ed17 | 2011-06-30 00:24:02 | [diff] [blame] | 10 | { |
Mike Frysinger | 5ee554f | 2012-06-01 20:36:08 | [diff] [blame] | 11 | cros_setup_toolchains --show-board-cfg="$1" | sed 's:,: :g' |
Mike Frysinger | df6dd08 | 2012-02-16 22:15:29 | [diff] [blame] | 12 | } |
| 13 | |
| 14 | get_ctarget_from_board() |
| 15 | { |
| 16 | local all_toolchains=( $(get_all_board_toolchains "$@") ) |
| 17 | echo "${all_toolchains[0]}" |
| 18 | } |
| 19 | |
| 20 | get_board_arch() |
| 21 | { |
| 22 | local ctarget=$(get_ctarget_from_board "$@") |
| 23 | |
Mike Frysinger | b4342af | 2012-02-28 17:25:01 | [diff] [blame] | 24 | # 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 Frysinger | df6dd08 | 2012-02-16 22:15:29 | [diff] [blame] | 27 | error "Unable to determine ARCH from toolchain: ${ctarget}" |
| 28 | return 1 |
Mike Frysinger | b4342af | 2012-02-28 17:25:01 | [diff] [blame] | 29 | fi |
Mike Frysinger | df6dd08 | 2012-02-16 22:15:29 | [diff] [blame] | 30 | |
Mike Frysinger | b4342af | 2012-02-28 17:25:01 | [diff] [blame] | 31 | echo "${arch}" |
Mike Frysinger | df6dd08 | 2012-02-16 22:15:29 | [diff] [blame] | 32 | return 0 |
Ahmad Sharif | 795ed17 | 2011-06-30 00:24:02 | [diff] [blame] | 33 | } |
| 34 | |
Ahmad Sharif | 30dd2d0 | 2011-06-30 23:56:24 | [diff] [blame] | 35 | is_number() |
| 36 | { |
| 37 | echo "$1" | grep -q "^[0-9]\+$" |
| 38 | } |
| 39 | |
| 40 | is_config_installed() |
| 41 | { |
| 42 | gcc-config -l | cut -d" " -f3 | grep -q "$1$" |
| 43 | } |
| 44 | |
| 45 | get_installed_atom_from_config() |
| 46 | { |
| 47 | local gcc_path="$(gcc-config -B "$1")" |
| 48 | equery b "$gcc_path" | head -n1 |
| 49 | } |
| 50 | |
Ahmad Sharif | 795ed17 | 2011-06-30 00:24:02 | [diff] [blame] | 51 | get_atom_from_config() |
| 52 | { |
Ahmad Sharif | 30dd2d0 | 2011-06-30 23:56:24 | [diff] [blame] | 53 | echo "$1" | sed -E "s|(.*)-(.*)|cross-\1/gcc-\2|g" |
Ahmad Sharif | 795ed17 | 2011-06-30 00:24:02 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | get_ctarget_from_atom() |
| 57 | { |
| 58 | local atom="$1" |
| 59 | echo "$atom" | sed -E 's|cross-([^/]+)/.*|\1|g' |
| 60 | } |
| 61 | |
Ahmad Sharif | 30dd2d0 | 2011-06-30 23:56:24 | [diff] [blame] | 62 | get_current_binutils_config() |
| 63 | { |
| 64 | local ctarget="$1" |
Ahmad Sharif | d32c1cc | 2011-09-12 18:07:24 | [diff] [blame] | 65 | binutils-config -l | grep "$ctarget" | grep "*" | awk '{print $NF}' |
| 66 | } |
| 67 | |
| 68 | get_bfd_config() |
| 69 | { |
| 70 | local ctarget="$1" |
| 71 | binutils-config -l | grep "$ctarget" | grep -v "gold" | head -n1 | \ |
| 72 | awk '{print $NF}' |
Ahmad Sharif | 30dd2d0 | 2011-06-30 23:56:24 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | emerge_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 Sharif | 30dd2d0 | 2011-06-30 23:56:24 | [diff] [blame] | 93 | old_binutils_config="$(get_current_binutils_config $ctarget)" |
Ahmad Sharif | d32c1cc | 2011-09-12 18:07:24 | [diff] [blame] | 94 | bfd_binutils_config="$(get_bfd_config $ctarget)" |
| 95 | if [[ "$old_binutils_config" != "$bfd_binutils_config" ]] |
Ahmad Sharif | 30dd2d0 | 2011-06-30 23:56:24 | [diff] [blame] | 96 | then |
Ahmad Sharif | d32c1cc | 2011-09-12 18:07:24 | [diff] [blame] | 97 | sudo binutils-config "$bfd_binutils_config" |
Ahmad Sharif | 30dd2d0 | 2011-06-30 23:56:24 | [diff] [blame] | 98 | 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 Sharif | ec4ed4e | 2011-07-12 21:31:39 | [diff] [blame] | 118 | # This function should only be called when testing experimental toolchain |
| 119 | # compilers. Please don't call this from any other script. |
Ahmad Sharif | 795ed17 | 2011-06-30 00:24:02 | [diff] [blame] | 120 | cros_gcc_config() |
| 121 | { |
Ahmad Sharif | 795ed17 | 2011-06-30 00:24:02 | [diff] [blame] | 122 | # Return if we're not switching profiles. |
| 123 | if [[ "$1" == -* ]] |
| 124 | then |
Ahmad Sharif | 30dd2d0 | 2011-06-30 23:56:24 | [diff] [blame] | 125 | sudo gcc-config "$1" |
| 126 | return $? |
Ahmad Sharif | 795ed17 | 2011-06-30 00:24:02 | [diff] [blame] | 127 | fi |
| 128 | |
Ahmad Sharif | 30dd2d0 | 2011-06-30 23:56:24 | [diff] [blame] | 129 | # 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 Sharif | ec4ed4e | 2011-07-12 21:31:39 | [diff] [blame] | 143 | local boards=$(get_boards_from_config "$1") |
| 144 | local board |
| 145 | for board in $boards |
| 146 | do |
Ahmad Sharif | ec4ed4e | 2011-07-12 21:31:39 | [diff] [blame] | 147 | emerge-"$board" --oneshot sys-devel/libtool |
| 148 | done |
Ahmad Sharif | 30dd2d0 | 2011-06-30 23:56:24 | [diff] [blame] | 149 | } |
| 150 | |
Ahmad Sharif | ec4ed4e | 2011-07-12 21:31:39 | [diff] [blame] | 151 | get_boards_from_config() |
Ahmad Sharif | 30dd2d0 | 2011-06-30 23:56:24 | [diff] [blame] | 152 | { |
| 153 | local atom=$(get_installed_atom_from_config "$1") |
Ahmad Sharif | 795ed17 | 2011-06-30 00:24:02 | [diff] [blame] | 154 | 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 Sharif | 30dd2d0 | 2011-06-30 23:56:24 | [diff] [blame] | 162 | local ctarget="$(get_ctarget_from_atom "$atom")" |
Ahmad Sharif | 795ed17 | 2011-06-30 00:24:02 | [diff] [blame] | 163 | for board_root in /build/* |
| 164 | do |
Ahmad Sharif | 30dd2d0 | 2011-06-30 23:56:24 | [diff] [blame] | 165 | local board="$(basename $board_root)" |
| 166 | local board_tc="$(get_ctarget_from_board $board)" |
Ahmad Sharif | 795ed17 | 2011-06-30 00:24:02 | [diff] [blame] | 167 | if [[ "${board_tc}" == "${ctarget}" ]] |
| 168 | then |
Ahmad Sharif | ec4ed4e | 2011-07-12 21:31:39 | [diff] [blame] | 169 | echo "$board" |
Ahmad Sharif | 795ed17 | 2011-06-30 00:24:02 | [diff] [blame] | 170 | fi |
| 171 | done |
| 172 | } |