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