From c503b0248be832e522b37f271ad7ba3dd13b4c7d Mon Sep 17 00:00:00 2001 From: Michael Marineau Date: Tue, 22 Oct 2013 22:19:56 -0700 Subject: [PATCH] add(build_toolchains): New command to build cross and native target toolchains. This replaces the cross-toolchain compile step in bootstrap_sdk and adds the ability to build native toolchains using the cross toolchain. This is just the first step towards actually providing the native toolchain in a container. --- build_library/catalyst_toolchains.sh | 136 +++++++++++++++++++++++++++ build_library/toolchain_util.sh | 50 ++++++++++ build_toolchains | 54 +++++++++++ 3 files changed, 240 insertions(+) create mode 100644 build_library/catalyst_toolchains.sh create mode 100644 build_library/toolchain_util.sh create mode 100755 build_toolchains diff --git a/build_library/catalyst_toolchains.sh b/build_library/catalyst_toolchains.sh new file mode 100644 index 0000000000..28f15e8079 --- /dev/null +++ b/build_library/catalyst_toolchains.sh @@ -0,0 +1,136 @@ +#!/bin/bash + +set -e +source /tmp/chroot-functions.sh +source /tmp/toolchain_util.sh + +# A note on packages: +# The default PKGDIR is /usr/portage/packages +# To make sure things are uploaded to the correct places we split things up: +# crossdev build packages use ${PKGDIR}/crossdev (uploaded to SDK location) +# build deps in crossdev's sysroot use ${PKGDIR}/cross/${CHOST} (no upload) +# native toolchains use ${PKGDIR}/native/${BOARD} (uploaded to board location) + +get_dependency_list() { + local ROOT="$1" + local IFS=$'| \t\n' + shift + + PORTAGE_CONFIGROOT="$ROOT" SYSROOT="$ROOT" \ + ROOT="$ROOT" emerge ${clst_myemergeopts} \ + --pretend --with-bdeps=y --onlydeps --quiet \ + "$@" | sed -e 's/.*\] \([^ :]*\).*/=\1/' | + egrep -v "(=$(echo "$*")-[0-9])" +} + +configure_portage() { + local pkg_path="$1" + mkdir -p "${ROOT}/etc/portage" + rm -f "${ROOT}/etc/make.profile" "${ROOT}/etc/portage/make.profile" + # eselect will report "!!! Warning: Strange path." but that's OK + eselect profile set --force coreos:coreos/amd64/generic + cat >"${ROOT}/etc/portage/make.conf" </dev/null + then + PKGDIR="${PKGDIR}" run_merge -u "${cross_pkgs[@]}" + else + PKGDIR="${PKGDIR}" crossdev \ + --ov-output "/tmp/crossdev" --stable \ + --env PKGDIR="${PORTDIR}/packages/crossdev" \ + --portage "${clst_myemergeopts}" \ + --stage4 --target "${cross_chost}" + fi + + # Setup ccache for our shiny new toolchain + ccache-config --install-links "${cross_chost}" +} + +configure_cross_root() { + local cross_chost="$1" + local ROOT="/usr/${cross_chost}" + + CHOST="${cross_chost}" ROOT="$ROOT" SYSROOT="$ROOT" \ + configure_portage "cross/${cross_chost}" + + # In order to get a dependency list we must calculate it before + # updating package.provided. Otherwise portage will no-op. + get_dependency_list "$ROOT" "${TOOLCHAIN_PKGS[@]}" \ + > "$ROOT/etc/portage/cross-${cross_chost}-depends" + + # Add toolchain to packages.provided since they are on the host system + mkdir -p "$ROOT/etc/portage/profile/package.provided" + local native_pkg cross_pkg cross_pkg_version + for native_pkg in "${TOOLCHAIN_PKGS[@]}"; do + cross_pkg="${native_pkg/*\//cross-${cross_chost}/}" + cross_pkg_version=$(portageq match / "${cross_pkg}") + echo "${native_pkg%/*}/${cross_pkg_version#*/}" + done > "$ROOT/etc/portage/profile/package.provided/cross-${cross_chost}" +} + +build_cross_libs() { + local cross_chost="$1" + local ROOT="/usr/${cross_chost}" + local cross_deps=$(<"$ROOT/etc/portage/cross-${cross_chost}-depends") + + # --root is required because run_merge overrides ROOT= + PORTAGE_CONFIGROOT="$ROOT" run_merge -u --root="$ROOT" $cross_deps +} + +configure_target_root() { + local board="$1" + local cross_chost=$(get_board_chost "$1") + + CHOST="${cross_chost}" ROOT="/build/${board}" \ + SYSROOT="/usr/${cross_chost}" configure_portage "target/${board}" +} + +build_target_toolchain() { + local board="$1" + local ROOT="/build/${board}" + + # --root is required because run_merge overrides ROOT= + PORTAGE_CONFIGROOT="$ROOT" \ + run_merge -u --root="$ROOT" "${TOOLCHAIN_PKGS[@]}" +} + +for cross_chost in $(get_chost_list); do + echo "Building cross toolchain for ${cross_chost}" + build_cross_toolchain "${cross_chost}" + configure_cross_root "${cross_chost}" + build_cross_libs "${cross_chost}" +done + +for board in $(get_board_list); do + echo "Building native toolchain for ${bard}" + configure_target_root "${board}" + build_target_toolchain "${board}" +done diff --git a/build_library/toolchain_util.sh b/build_library/toolchain_util.sh new file mode 100644 index 0000000000..e38be28b25 --- /dev/null +++ b/build_library/toolchain_util.sh @@ -0,0 +1,50 @@ +#!/bin/bash + +# Toolchain packages are treated a bit specially, since they take a +# while to build and are generally more complicated to build they are +# only built via catalyst and everyone else installs them as binpkgs. +TOOLCHAIN_PKGS=( + sys-devel/binutils + sys-devel/gcc + sys-kernel/linux-headers + sys-libs/glibc +) + +# Map board names to CHOSTs and portage profiles. This is the +# definitive list, there is assorted code new and old that either +# guesses or hard-code these. All that should migrate to this list. +declare -A BOARD_CHOST BOARD_PROFILE +BOARD_CHOST["amd64-generic"]="x86_64-cros-linux-gnu" +BOARD_PROFILE["amd64-generic"]="coreos:coreos/amd64/generic" +BOARD_NAMES=( "${!BOARD_CHOST[@]}" ) + +get_board_list() { + local IFS=$'\n\t ' + sort <<<"${BOARD_NAMES[*]}" +} + +get_chost_list() { + local IFS=$'\n\t ' + sort -u <<<"${BOARD_CHOST[*]}" +} + +get_profile_list() { + local IFS=$'\n\t ' + sort -u <<<"${BOARD_PROFILE[*]}" +} + +get_board_chost() { + if [[ ${#BOARD_CHOST["$1"]} -ne 0 ]]; then + echo "${BOARD_CHOST["$1"]}" + else + die "Unknown board '$1'" + fi +} + +get_board_profile() { + if [[ ${#BOARD_PROFILE["$1"]} -ne 0 ]]; then + echo "${BOARD_PROFILE["$1"]}" + else + die "Unknown board '$1'" + fi +} diff --git a/build_toolchains b/build_toolchains new file mode 100755 index 0000000000..c22ed0977f --- /dev/null +++ b/build_toolchains @@ -0,0 +1,54 @@ +#!/bin/bash +# +# Copyright (c) 2013 The CoreOS Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +SCRIPT_ROOT=$(dirname $(readlink -f "$0")) +. "${SCRIPT_ROOT}/common.sh" || exit 1 + +TYPE="coreos-toolchains" +ARCH=$(portageq envvar ARCH) +DEFAULT_SEED="builds/coreos-sdk/stage4-${ARCH}-latest.tar.bz2" +DEFAULT_PROFILE="coreos:default/linux/${ARCH}/10.0" +FORCE_STAGES="stage4" + +. "${BUILD_LIBRARY_DIR}/catalyst.sh" || exit 1 +. "${BUILD_LIBRARY_DIR}/toolchain_util.sh" || exit 1 + +# include upload options +. "${BUILD_LIBRARY_DIR}/release_util.sh" || exit 1 + +ROOT_OVERLAY="${TEMPDIR}/stage4-${ARCH}-$FLAGS_version-overlay" + +## Define the stage4 config template +catalyst_stage4() { +cat <