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 | |
| 62 | copy_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 Sharif | 30dd2d0 | 2011-06-30 23:56:24 | [diff] [blame] | 69 | sudo cp -a "$file_path"* "$target_location/$dir_path/" |
Ahmad Sharif | 795ed17 | 2011-06-30 00:24:02 | [diff] [blame] | 70 | 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 | |
| 80 | copy_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 Sharif | 30dd2d0 | 2011-06-30 23:56:24 | [diff] [blame] | 88 | grep /libstdc++.so$) |
Ahmad Sharif | 795ed17 | 2011-06-30 00:24:02 | [diff] [blame] | 89 | 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 Khoury | e7a52e4 | 2011-11-02 18:07:37 | [diff] [blame] | 97 | sudo ROOT="$target_location" env-update |
Ahmad Sharif | 795ed17 | 2011-06-30 00:24:02 | [diff] [blame] | 98 | return 0 |
| 99 | } |
| 100 | |
Ahmad Sharif | 30dd2d0 | 2011-06-30 23:56:24 | [diff] [blame] | 101 | get_current_binutils_config() |
| 102 | { |
| 103 | local ctarget="$1" |
Ahmad Sharif | d32c1cc | 2011-09-12 18:07:24 | [diff] [blame] | 104 | binutils-config -l | grep "$ctarget" | grep "*" | awk '{print $NF}' |
| 105 | } |
| 106 | |
| 107 | get_bfd_config() |
| 108 | { |
| 109 | local ctarget="$1" |
| 110 | binutils-config -l | grep "$ctarget" | grep -v "gold" | head -n1 | \ |
| 111 | awk '{print $NF}' |
Ahmad Sharif | 30dd2d0 | 2011-06-30 23:56:24 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | emerge_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 Sharif | 30dd2d0 | 2011-06-30 23:56:24 | [diff] [blame] | 132 | old_binutils_config="$(get_current_binutils_config $ctarget)" |
Ahmad Sharif | d32c1cc | 2011-09-12 18:07:24 | [diff] [blame] | 133 | bfd_binutils_config="$(get_bfd_config $ctarget)" |
| 134 | if [[ "$old_binutils_config" != "$bfd_binutils_config" ]] |
Ahmad Sharif | 30dd2d0 | 2011-06-30 23:56:24 | [diff] [blame] | 135 | then |
Ahmad Sharif | d32c1cc | 2011-09-12 18:07:24 | [diff] [blame] | 136 | sudo binutils-config "$bfd_binutils_config" |
Ahmad Sharif | 30dd2d0 | 2011-06-30 23:56:24 | [diff] [blame] | 137 | 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 Sharif | ec4ed4e | 2011-07-12 21:31:39 | [diff] [blame] | 157 | # This function should only be called when testing experimental toolchain |
| 158 | # compilers. Please don't call this from any other script. |
Ahmad Sharif | 795ed17 | 2011-06-30 00:24:02 | [diff] [blame] | 159 | cros_gcc_config() |
| 160 | { |
Ahmad Sharif | 795ed17 | 2011-06-30 00:24:02 | [diff] [blame] | 161 | # Return if we're not switching profiles. |
| 162 | if [[ "$1" == -* ]] |
| 163 | then |
Ahmad Sharif | 30dd2d0 | 2011-06-30 23:56:24 | [diff] [blame] | 164 | sudo gcc-config "$1" |
| 165 | return $? |
Ahmad Sharif | 795ed17 | 2011-06-30 00:24:02 | [diff] [blame] | 166 | fi |
| 167 | |
Ahmad Sharif | 30dd2d0 | 2011-06-30 23:56:24 | [diff] [blame] | 168 | # 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 Sharif | ec4ed4e | 2011-07-12 21:31:39 | [diff] [blame] | 182 | local boards=$(get_boards_from_config "$1") |
| 183 | local board |
| 184 | for board in $boards |
| 185 | do |
Ahmad Sharif | f3dad64 | 2011-08-25 18:53:36 | [diff] [blame] | 186 | cros_install_libs_for_config "$board" "$1" |
Ahmad Sharif | ec4ed4e | 2011-07-12 21:31:39 | [diff] [blame] | 187 | emerge-"$board" --oneshot sys-devel/libtool |
| 188 | done |
Ahmad Sharif | 30dd2d0 | 2011-06-30 23:56:24 | [diff] [blame] | 189 | } |
| 190 | |
Ahmad Sharif | ec4ed4e | 2011-07-12 21:31:39 | [diff] [blame] | 191 | get_boards_from_config() |
Ahmad Sharif | 30dd2d0 | 2011-06-30 23:56:24 | [diff] [blame] | 192 | { |
| 193 | local atom=$(get_installed_atom_from_config "$1") |
Ahmad Sharif | 795ed17 | 2011-06-30 00:24:02 | [diff] [blame] | 194 | 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 Sharif | 30dd2d0 | 2011-06-30 23:56:24 | [diff] [blame] | 202 | local ctarget="$(get_ctarget_from_atom "$atom")" |
Ahmad Sharif | 795ed17 | 2011-06-30 00:24:02 | [diff] [blame] | 203 | for board_root in /build/* |
| 204 | do |
Ahmad Sharif | 30dd2d0 | 2011-06-30 23:56:24 | [diff] [blame] | 205 | local board="$(basename $board_root)" |
| 206 | local board_tc="$(get_ctarget_from_board $board)" |
Ahmad Sharif | 795ed17 | 2011-06-30 00:24:02 | [diff] [blame] | 207 | if [[ "${board_tc}" == "${ctarget}" ]] |
| 208 | then |
Ahmad Sharif | ec4ed4e | 2011-07-12 21:31:39 | [diff] [blame] | 209 | echo "$board" |
Ahmad Sharif | 795ed17 | 2011-06-30 00:24:02 | [diff] [blame] | 210 | fi |
| 211 | done |
| 212 | } |
Ahmad Sharif | ec4ed4e | 2011-07-12 21:31:39 | [diff] [blame] | 213 | |
| 214 | cros_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 | } |