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