blob: d0ecd2803e51dd0210d1a312af0e47c7fca024d4 [file] [log] [blame]
Ahmad Sharif795ed172011-06-29 17:24:02 -07001#!/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
9get_ctarget_from_board()
10{
11 local board="$1"
12 local board_overlay=$(cros_overlay_list --board="$board" --primary_only)
13 cat "$board_overlay/toolchain.conf"
14}
15
16get_atom_from_config()
17{
18 local gcc_path="$(gcc-config -B "$1")"
19 equery b ${gcc_path} | head -n1
20}
21
22get_ctarget_from_atom()
23{
24 local atom="$1"
25 echo "$atom" | sed -E 's|cross-([^/]+)/.*|\1|g'
26}
27
28copy_gcc_libs_helper()
29{
30 local target_location="$1"
31 local file_path="$2"
32 local dir_path=$(dirname "$file_path")
33 info "Copying $file_path symlink and file to $target_location/$dir_path/."
34 sudo mkdir -p "$target_location/$dir_path"
35 sudo cp -a "$file_path" "$target_location/$dir_path/"
36 sudo cp -a "$(readlink -f $file_path)" "$target_location/$dir_path/"
37 local env_d_file="$target_location/etc/env.d/05gcc"
38 info "Adding $dir_path to LDPATH in file $env_d_file"
39 sudo mkdir -p $(dirname "$env_d_file")
40 local line_to_add="LDPATH=\"$dir_path\""
41 if ! grep -q "^$line_to_add$" "$env_d_file" &>/dev/null
42 then
43 echo "$line_to_add" | sudo_append "$env_d_file"
44 fi
45}
46
47copy_gcc_libs()
48{
49 # TODO: Figure out a better way of doing this?
50 local target_location="$1"
51 local atom="$2"
52 local libgcc_file=$(portageq contents / $atom | \
53 grep /libgcc_s.so$)
54 local libstdcxx_file=$(portageq contents / $atom | \
55 grep /libstdc++.so)
56 if [[ -z "$libgcc_file" || -z "$libstdcxx_file" ]]
57 then
58 error "Could not find libgcc_s.so/libstdcxx_s.so. Is\
59 =$atom emerged properly?"
60 return 1
61 fi
62 copy_gcc_libs_helper $target_location $libgcc_file
63 copy_gcc_libs_helper $target_location $libstdcxx_file
64 return 0
65}
66
67cros_gcc_config()
68{
69 sudo gcc-config "$1" || return $?
70
71 # Return if we're not switching profiles.
72 if [[ "$1" == -* ]]
73 then
74 return 0
75 fi
76
77 local atom=$(get_atom_from_config "$1")
78 if [[ $atom != cross* ]]
79 then
80 warn "$atom is not a cross-compiler."
81 warn "Therefore not adding its libs to the board roots."
82 return 0
83 fi
84
85 # Now copy the lib files into all possible boards.
86 local ctarget=$(get_ctarget_from_atom "$atom")
87 for board_root in /build/*
88 do
89 local board=$(basename ${board_root} | cut -d '_' -f 1)
90 local board_tc=$(get_ctarget_from_board $board)
91 if [[ "${board_tc}" == "${ctarget}" ]]
92 then
93 copy_gcc_libs "/build/$board" $atom
94 fi
95 done
96}