Ahmad Sharif | 795ed17 | 2011-06-29 17:24:02 -0700 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
Mike Frysinger | df6dd08 | 2012-02-16 17:15:29 -0500 | [diff] [blame] | 3 | # Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Ahmad Sharif | 795ed17 | 2011-06-29 17:24:02 -0700 | [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 17:15:29 -0500 | [diff] [blame] | 9 | get_all_board_toolchains() |
Ahmad Sharif | 795ed17 | 2011-06-29 17:24:02 -0700 | [diff] [blame] | 10 | { |
| 11 | local board="$1" |
Mike Frysinger | df6dd08 | 2012-02-16 17:15:29 -0500 | [diff] [blame] | 12 | local base_board=$(echo "${board}" | cut -d '_' -f 1) |
| 13 | local board_overlay=$(cros_overlay_list --board="${base_board}" \ |
| 14 | --primary_only) |
| 15 | sed 's:#.*::' "${board_overlay}/toolchain.conf" |
| 16 | } |
| 17 | |
| 18 | get_ctarget_from_board() |
| 19 | { |
| 20 | local all_toolchains=( $(get_all_board_toolchains "$@") ) |
| 21 | echo "${all_toolchains[0]}" |
| 22 | } |
| 23 | |
| 24 | get_board_arch() |
| 25 | { |
| 26 | local ctarget=$(get_ctarget_from_board "$@") |
| 27 | |
| 28 | case ${ctarget} in |
| 29 | arm*) echo "arm" ;; |
| 30 | i?86*) echo "x86" ;; |
| 31 | x86_64*) echo "amd64" ;; |
| 32 | *) |
| 33 | error "Unable to determine ARCH from toolchain: ${ctarget}" |
| 34 | return 1 |
| 35 | ;; |
| 36 | esac |
| 37 | |
| 38 | return 0 |
Ahmad Sharif | 795ed17 | 2011-06-29 17:24:02 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Ahmad Sharif | 30dd2d0 | 2011-06-30 16:56:24 -0700 | [diff] [blame] | 41 | is_number() |
| 42 | { |
| 43 | echo "$1" | grep -q "^[0-9]\+$" |
| 44 | } |
| 45 | |
| 46 | is_config_installed() |
| 47 | { |
| 48 | gcc-config -l | cut -d" " -f3 | grep -q "$1$" |
| 49 | } |
| 50 | |
| 51 | get_installed_atom_from_config() |
| 52 | { |
| 53 | local gcc_path="$(gcc-config -B "$1")" |
| 54 | equery b "$gcc_path" | head -n1 |
| 55 | } |
| 56 | |
Ahmad Sharif | 795ed17 | 2011-06-29 17:24:02 -0700 | [diff] [blame] | 57 | get_atom_from_config() |
| 58 | { |
Ahmad Sharif | 30dd2d0 | 2011-06-30 16:56:24 -0700 | [diff] [blame] | 59 | echo "$1" | sed -E "s|(.*)-(.*)|cross-\1/gcc-\2|g" |
Ahmad Sharif | 795ed17 | 2011-06-29 17:24:02 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | get_ctarget_from_atom() |
| 63 | { |
| 64 | local atom="$1" |
| 65 | echo "$atom" | sed -E 's|cross-([^/]+)/.*|\1|g' |
| 66 | } |
| 67 | |
| 68 | copy_gcc_libs_helper() |
| 69 | { |
| 70 | local target_location="$1" |
| 71 | local file_path="$2" |
| 72 | local dir_path=$(dirname "$file_path") |
| 73 | info "Copying $file_path symlink and file to $target_location/$dir_path/." |
| 74 | sudo mkdir -p "$target_location/$dir_path" |
Ahmad Sharif | 30dd2d0 | 2011-06-30 16:56:24 -0700 | [diff] [blame] | 75 | sudo cp -a "$file_path"* "$target_location/$dir_path/" |
Ahmad Sharif | 795ed17 | 2011-06-29 17:24:02 -0700 | [diff] [blame] | 76 | local env_d_file="$target_location/etc/env.d/05gcc" |
| 77 | info "Adding $dir_path to LDPATH in file $env_d_file" |
| 78 | sudo mkdir -p $(dirname "$env_d_file") |
| 79 | local line_to_add="LDPATH=\"$dir_path\"" |
| 80 | if ! grep -q "^$line_to_add$" "$env_d_file" &>/dev/null |
| 81 | then |
| 82 | echo "$line_to_add" | sudo_append "$env_d_file" |
| 83 | fi |
| 84 | } |
| 85 | |
| 86 | copy_gcc_libs() |
| 87 | { |
| 88 | # TODO: Figure out a better way of doing this? |
| 89 | local target_location="$1" |
| 90 | local atom="$2" |
| 91 | local libgcc_file=$(portageq contents / $atom | \ |
| 92 | grep /libgcc_s.so$) |
| 93 | local libstdcxx_file=$(portageq contents / $atom | \ |
Ahmad Sharif | 30dd2d0 | 2011-06-30 16:56:24 -0700 | [diff] [blame] | 94 | grep /libstdc++.so$) |
Ahmad Sharif | 795ed17 | 2011-06-29 17:24:02 -0700 | [diff] [blame] | 95 | if [[ -z "$libgcc_file" || -z "$libstdcxx_file" ]] |
| 96 | then |
| 97 | error "Could not find libgcc_s.so/libstdcxx_s.so. Is\ |
| 98 | =$atom emerged properly?" |
| 99 | return 1 |
| 100 | fi |
| 101 | copy_gcc_libs_helper $target_location $libgcc_file |
| 102 | copy_gcc_libs_helper $target_location $libstdcxx_file |
Raymes Khoury | e7a52e4 | 2011-11-02 11:07:37 -0700 | [diff] [blame] | 103 | sudo ROOT="$target_location" env-update |
Ahmad Sharif | 795ed17 | 2011-06-29 17:24:02 -0700 | [diff] [blame] | 104 | return 0 |
| 105 | } |
| 106 | |
Ahmad Sharif | 30dd2d0 | 2011-06-30 16:56:24 -0700 | [diff] [blame] | 107 | get_current_binutils_config() |
| 108 | { |
| 109 | local ctarget="$1" |
Ahmad Sharif | d32c1cc | 2011-09-12 11:07:24 -0700 | [diff] [blame] | 110 | binutils-config -l | grep "$ctarget" | grep "*" | awk '{print $NF}' |
| 111 | } |
| 112 | |
| 113 | get_bfd_config() |
| 114 | { |
| 115 | local ctarget="$1" |
| 116 | binutils-config -l | grep "$ctarget" | grep -v "gold" | head -n1 | \ |
| 117 | awk '{print $NF}' |
Ahmad Sharif | 30dd2d0 | 2011-06-30 16:56:24 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | emerge_gcc() |
| 121 | { |
| 122 | local atom="$1" |
| 123 | local ctarget="$(get_ctarget_from_atom $atom)" |
| 124 | mask_file="/etc/portage/package.mask/cross-$ctarget" |
| 125 | moved_mask_file=0 |
| 126 | |
| 127 | # Move the package mask file elsewhere. |
| 128 | if [[ -f "$mask_file" ]] |
| 129 | then |
| 130 | temp_file="$(mktemp)" |
| 131 | sudo mv "$mask_file" "$temp_file" |
| 132 | moved_mask_file=1 |
| 133 | fi |
| 134 | |
| 135 | USE+=" multislot" |
| 136 | if echo "$atom" | grep -q "gcc-4.6.0$" |
| 137 | then |
Ahmad Sharif | 30dd2d0 | 2011-06-30 16:56:24 -0700 | [diff] [blame] | 138 | old_binutils_config="$(get_current_binutils_config $ctarget)" |
Ahmad Sharif | d32c1cc | 2011-09-12 11:07:24 -0700 | [diff] [blame] | 139 | bfd_binutils_config="$(get_bfd_config $ctarget)" |
| 140 | if [[ "$old_binutils_config" != "$bfd_binutils_config" ]] |
Ahmad Sharif | 30dd2d0 | 2011-06-30 16:56:24 -0700 | [diff] [blame] | 141 | then |
Ahmad Sharif | d32c1cc | 2011-09-12 11:07:24 -0700 | [diff] [blame] | 142 | sudo binutils-config "$bfd_binutils_config" |
Ahmad Sharif | 30dd2d0 | 2011-06-30 16:56:24 -0700 | [diff] [blame] | 143 | fi |
| 144 | fi |
| 145 | sudo ACCEPT_KEYWORDS="*" USE="$USE" emerge ="$atom" |
| 146 | emerge_retval=$? |
| 147 | |
| 148 | # Move the package mask file back. |
| 149 | if [[ $moved_mask_file -eq 1 ]] |
| 150 | then |
| 151 | sudo mv "$temp_file" "$mask_file" |
| 152 | fi |
| 153 | |
| 154 | if [[ ! -z "$old_binutils_config" && |
| 155 | "$old_binutils_config" != "$(get_current_binutils_config $ctarget)" ]] |
| 156 | then |
| 157 | sudo binutils-config "$old_binutils_config" |
| 158 | fi |
| 159 | |
| 160 | return $emerge_retval |
| 161 | } |
| 162 | |
Ahmad Sharif | ec4ed4e | 2011-07-12 14:31:39 -0700 | [diff] [blame] | 163 | # This function should only be called when testing experimental toolchain |
| 164 | # compilers. Please don't call this from any other script. |
Ahmad Sharif | 795ed17 | 2011-06-29 17:24:02 -0700 | [diff] [blame] | 165 | cros_gcc_config() |
| 166 | { |
Ahmad Sharif | 795ed17 | 2011-06-29 17:24:02 -0700 | [diff] [blame] | 167 | # Return if we're not switching profiles. |
| 168 | if [[ "$1" == -* ]] |
| 169 | then |
Ahmad Sharif | 30dd2d0 | 2011-06-30 16:56:24 -0700 | [diff] [blame] | 170 | sudo gcc-config "$1" |
| 171 | return $? |
Ahmad Sharif | 795ed17 | 2011-06-29 17:24:02 -0700 | [diff] [blame] | 172 | fi |
| 173 | |
Ahmad Sharif | 30dd2d0 | 2011-06-30 16:56:24 -0700 | [diff] [blame] | 174 | # cros_gcc_config can be called like: |
| 175 | # cros_gcc_config <number> to switch config to that |
| 176 | # number. In that case we should just try to switch to |
| 177 | # that config and not try to install a missing one. |
| 178 | if ! is_number "$1" && ! is_config_installed "$1" |
| 179 | then |
| 180 | info "Configuration $1 not found." |
| 181 | info "Trying to emerge it..." |
| 182 | local atom="$(get_atom_from_config "$1")" |
| 183 | emerge_gcc "$atom" || die "Could not install $atom" |
| 184 | fi |
| 185 | |
| 186 | sudo gcc-config "$1" || die "Could not switch to $1" |
| 187 | |
Ahmad Sharif | ec4ed4e | 2011-07-12 14:31:39 -0700 | [diff] [blame] | 188 | local boards=$(get_boards_from_config "$1") |
| 189 | local board |
| 190 | for board in $boards |
| 191 | do |
Ahmad Sharif | f3dad64 | 2011-08-25 11:53:36 -0700 | [diff] [blame] | 192 | cros_install_libs_for_config "$board" "$1" |
Ahmad Sharif | ec4ed4e | 2011-07-12 14:31:39 -0700 | [diff] [blame] | 193 | emerge-"$board" --oneshot sys-devel/libtool |
| 194 | done |
Ahmad Sharif | 30dd2d0 | 2011-06-30 16:56:24 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Ahmad Sharif | ec4ed4e | 2011-07-12 14:31:39 -0700 | [diff] [blame] | 197 | get_boards_from_config() |
Ahmad Sharif | 30dd2d0 | 2011-06-30 16:56:24 -0700 | [diff] [blame] | 198 | { |
| 199 | local atom=$(get_installed_atom_from_config "$1") |
Ahmad Sharif | 795ed17 | 2011-06-29 17:24:02 -0700 | [diff] [blame] | 200 | if [[ $atom != cross* ]] |
| 201 | then |
| 202 | warn "$atom is not a cross-compiler." |
| 203 | warn "Therefore not adding its libs to the board roots." |
| 204 | return 0 |
| 205 | fi |
| 206 | |
| 207 | # Now copy the lib files into all possible boards. |
Ahmad Sharif | 30dd2d0 | 2011-06-30 16:56:24 -0700 | [diff] [blame] | 208 | local ctarget="$(get_ctarget_from_atom "$atom")" |
Ahmad Sharif | 795ed17 | 2011-06-29 17:24:02 -0700 | [diff] [blame] | 209 | for board_root in /build/* |
| 210 | do |
Ahmad Sharif | 30dd2d0 | 2011-06-30 16:56:24 -0700 | [diff] [blame] | 211 | local board="$(basename $board_root)" |
| 212 | local board_tc="$(get_ctarget_from_board $board)" |
Ahmad Sharif | 795ed17 | 2011-06-29 17:24:02 -0700 | [diff] [blame] | 213 | if [[ "${board_tc}" == "${ctarget}" ]] |
| 214 | then |
Ahmad Sharif | ec4ed4e | 2011-07-12 14:31:39 -0700 | [diff] [blame] | 215 | echo "$board" |
Ahmad Sharif | 795ed17 | 2011-06-29 17:24:02 -0700 | [diff] [blame] | 216 | fi |
| 217 | done |
| 218 | } |
Ahmad Sharif | ec4ed4e | 2011-07-12 14:31:39 -0700 | [diff] [blame] | 219 | |
| 220 | cros_install_libs_for_config() |
| 221 | { |
| 222 | local board="$1" |
| 223 | local atom=$(get_installed_atom_from_config "$2") |
| 224 | copy_gcc_libs /build/"$board" "$atom" |
| 225 | } |