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