<commit_before>#!/bin/bash

# Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# TODO: Convert this to python.

get_ctarget_from_board()
{
  local board="$1"
  local base_board="$(echo $board | cut -d '_' -f 1)"
  local board_overlay="$(cros_overlay_list --board="$base_board"\
    --primary_only)"
  cat "$board_overlay/toolchain.conf"
}

is_number()
{
  echo "$1" | grep -q "^[0-9]\+$"
}

is_config_installed()
{
  gcc-config -l | cut -d" " -f3 | grep -q "$1$"
}

get_installed_atom_from_config()
{
  local gcc_path="$(gcc-config -B "$1")"
  equery b "$gcc_path" | head -n1
}

get_atom_from_config()
{
  echo "$1" | sed -E "s|(.*)-(.*)|cross-\1/gcc-\2|g"
}

get_ctarget_from_atom()
{
  local atom="$1"
  echo "$atom" | sed -E 's|cross-([^/]+)/.*|\1|g'
}

copy_gcc_libs_helper()
{
  local target_location="$1"
  local file_path="$2"
  local dir_path=$(dirname "$file_path")
  info "Copying $file_path symlink and file to $target_location/$dir_path/."
  sudo mkdir -p "$target_location/$dir_path"
  sudo cp -a "$file_path"* "$target_location/$dir_path/"
  local env_d_file="$target_location/etc/env.d/05gcc"
  info "Adding $dir_path to LDPATH in file $env_d_file"
  sudo mkdir -p $(dirname "$env_d_file")
  local line_to_add="LDPATH=\"$dir_path\""
  if ! grep -q "^$line_to_add$" "$env_d_file" &>/dev/null
  then
    echo "$line_to_add" | sudo_append "$env_d_file"
  fi
}

copy_gcc_libs()
{
  # TODO: Figure out a better way of doing this?
  local target_location="$1"
  local atom="$2"
  local libgcc_file=$(portageq contents / $atom | \
                      grep /libgcc_s.so$)
  local libstdcxx_file=$(portageq contents / $atom | \
                         grep /libstdc++.so$)
  if [[ -z "$libgcc_file" || -z "$libstdcxx_file" ]]
  then
    error "Could not find libgcc_s.so/libstdcxx_s.so. Is\
          =$atom emerged properly?"
    return 1
  fi
  copy_gcc_libs_helper $target_location $libgcc_file
  copy_gcc_libs_helper $target_location $libstdcxx_file
  return 0
}

get_current_binutils_config()
{
  local ctarget="$1"
  binutils-config -l | grep "$ctarget" | grep "*" | cut -d" " -f3
}

emerge_gcc()
{
  local atom="$1"
  local ctarget="$(get_ctarget_from_atom $atom)"
  mask_file="/etc/portage/package.mask/cross-$ctarget"
  moved_mask_file=0

  # Move the package mask file elsewhere.
  if [[ -f "$mask_file" ]]
  then
    temp_file="$(mktemp)"
    sudo mv "$mask_file" "$temp_file"
    moved_mask_file=1
  fi

  USE+=" multislot"
  if echo "$atom" | grep -q "gcc-4.6.0$"
  then
    USE+=" svn_sources"
    old_binutils_config="$(get_current_binutils_config $ctarget)"
    if [[ "$old_binutils_config" != "$ctarget-2.20.1" ]]
    then
      sudo binutils-config "$ctarget-2.20.1"
    fi
  fi
  sudo ACCEPT_KEYWORDS="*" USE="$USE" emerge ="$atom"
  emerge_retval=$?

  # Move the package mask file back.
  if [[ $moved_mask_file -eq 1 ]]
  then
    sudo mv "$temp_file" "$mask_file"
  fi

  if [[ ! -z "$old_binutils_config" &&
        "$old_binutils_config" != "$(get_current_binutils_config $ctarget)" ]]
  then
    sudo binutils-config "$old_binutils_config"
  fi

  return $emerge_retval
}

# This function should only be called when testing experimental toolchain
# compilers. Please don't call this from any other script.
cros_gcc_config()
{
  # Return if we're not switching profiles.<commit_msg>Removed USE="svn_sources" when installing gcc-4.6.0.<commit_after>#!/bin/bash

# Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# TODO: Convert this to python.

get_ctarget_from_board()
{
  local board="$1"
  local base_board="$(echo $board | cut -d '_' -f 1)"
  local board_overlay="$(cros_overlay_list --board="$base_board"\
    --primary_only)"
  cat "$board_overlay/toolchain.conf"
}

is_number()
{
  echo "$1" | grep -q "^[0-9]\+$"
}

is_config_installed()
{
  gcc-config -l | cut -d" " -f3 | grep -q "$1$"
}

get_installed_atom_from_config()
{
  local gcc_path="$(gcc-config -B "$1")"
  equery b "$gcc_path" | head -n1
}

get_atom_from_config()
{
  echo "$1" | sed -E "s|(.*)-(.*)|cross-\1/gcc-\2|g"
}

get_ctarget_from_atom()
{
  local atom="$1"
  echo "$atom" | sed -E 's|cross-([^/]+)/.*|\1|g'
}

copy_gcc_libs_helper()
{
  local target_location="$1"
  local file_path="$2"
  local dir_path=$(dirname "$file_path")
  info "Copying $file_path symlink and file to $target_location/$dir_path/."
  sudo mkdir -p "$target_location/$dir_path"
  sudo cp -a "$file_path"* "$target_location/$dir_path/"
  local env_d_file="$target_location/etc/env.d/05gcc"
  info "Adding $dir_path to LDPATH in file $env_d_file"
  sudo mkdir -p $(dirname "$env_d_file")
  local line_to_add="LDPATH=\"$dir_path\""
  if ! grep -q "^$line_to_add$" "$env_d_file" &>/dev/null
  then
    echo "$line_to_add" | sudo_append "$env_d_file"
  fi
}

copy_gcc_libs()
{
  # TODO: Figure out a better way of doing this?
  local target_location="$1"
  local atom="$2"
  local libgcc_file=$(portageq contents / $atom | \
                      grep /libgcc_s.so$)
  local libstdcxx_file=$(portageq contents / $atom | \
                         grep /libstdc++.so$)
  if [[ -z "$libgcc_file" || -z "$libstdcxx_file" ]]
  then
    error "Could not find libgcc_s.so/libstdcxx_s.so. Is\
          =$atom emerged properly?"
    return 1
  fi
  copy_gcc_libs_helper $target_location $libgcc_file
  copy_gcc_libs_helper $target_location $libstdcxx_file
  return 0
}

get_current_binutils_config()
{
  local ctarget="$1"
  binutils-config -l | grep "$ctarget" | grep "*" | cut -d" " -f3
}

emerge_gcc()
{
  local atom="$1"
  local ctarget="$(get_ctarget_from_atom $atom)"
  mask_file="/etc/portage/package.mask/cross-$ctarget"
  moved_mask_file=0

  # Move the package mask file elsewhere.
  if [[ -f "$mask_file" ]]
  then
    temp_file="$(mktemp)"
    sudo mv "$mask_file" "$temp_file"
    moved_mask_file=1
  fi

  USE+=" multislot"
  if echo "$atom" | grep -q "gcc-4.6.0$"
  then
    old_binutils_config="$(get_current_binutils_config $ctarget)"
    if [[ "$old_binutils_config" != "$ctarget-2.20.1" ]]
    then
      sudo binutils-config "$ctarget-2.20.1"
    fi
  fi
  sudo ACCEPT_KEYWORDS="*" USE="$USE" emerge ="$atom"
  emerge_retval=$?

  # Move the package mask file back.
  if [[ $moved_mask_file -eq 1 ]]
  then
    sudo mv "$temp_file" "$mask_file"
  fi

  if [[ ! -z "$old_binutils_config" &&
        "$old_binutils_config" != "$(get_current_binutils_config $ctarget)" ]]
  then
    sudo binutils-config "$old_binutils_config"
  fi

  return $emerge_retval
}

# This function should only be called when testing experimental toolchain
# compilers. Please don't call this from any other script.
cros_gcc_config()
{
  # Return if we're not switching profiles.
{'fraction_non_alphanumeric': 0.15119825708061002, 'fraction_numerical': 0.012926652142338417, 'mean_word_length': 3.5632869449966864, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3469487', 'n_tokens_mistral': 2743, 'n_tokens_neox': 2594, 'n_words': 631}