This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: --with-sysroot newbie troubles: "ld: cannot open crt1.o"


Is sysroot needed when building a complete cross toolchain into the same
install tree?  I'm working on a script to build powerpc64-linux cross
tools on a powerpc64-linux system, starting from Dan's crosstool.sh.
The following works with sources for GCC, binutils, and glibc from a
week ago, with a binutils patch that Alan plans to apply soon and a
small change to a ppc64-specific glibc Makefile.  This script is still
being tweaked to remove things that aren't necessary for the only
environment in which it will be used.

As Alan said, biarch builds are messy; I don't have that figured out
yet for a complete toolchain.

Janis

#!/bin/sh

abort() {
    echo "`date`  $@"
    exec /bin/false
}

#
# crosstool.sh
# Build a powerpc64-linux GNU toolchain on a powerpc64-linux system.
#
# Based on crosstool.sh from Dan Kegel and Bill Gatliff, with changes
# to support powerpc64-linux builds and with common code removed.
#
# Copyright (c) 2001 by Bill Gatliff, bgat@billgatliff.com 
# Copyright (c) 2003 by Dan Kegel, dkegel@ixiacom.com, Ixia Communications, 
# Copyright (c) 2004 by IBM for changes by Janis Johnson and Alan Modra,
# janis187@us.ibm.com and amodra@bigpond.net.au.
# All rights reserved.  This script is provided under the terms of the GPL.
#
# For general questions about cross builds for the GNU toolchain see the
# crossgcc mailing list at http://sources.redhat.com/ml/crossgcc.  For
# question specific to powerpc64-linux toolchain builds contact Janis or
# Alan, or drop into #ppc64 on irc.freenode.net.

########################################################################
# There are several things here that might just be unneeded remnants
# from Dan Kegel's script that are not needed for this powerpc64-linux
# built.  These include:
# - Is --with-sysroot needed when everything is the sysroot directory
#   is also in the prefix tree?
# - It's probably OK if $HOST==$BUILD; Dan made them different to avoid
#   executing anything just built, but that should be OK here.
# - Look carefully at all of the configure options; some of them are
#   most likely unnecessary.  In particular, we ought to enable nls to
#   allow testing that functionality.
#
# After the minor details here are worked out, the next step is a
# script for complete build of bi-arch tools.
########################################################################

#
# Meant to be invoked from another shell script.
# Usage: several environment variables must be set, namely:
test -z "${PREFIX}"  \
  && abort "Please set PREFIX to where you want the toolchain installed."
test -z "${SYSROOT}" \
  && abort "Please set SYSROOT to where you want the links to headers and" \
           " libraries."
test -z "${BINUTILS_DIR}" \
  && abort "Please set BINUTILS_DIR to the bare filename of the binutils" \
           " directory"
test -z "${GCC_DIR}" \
  && abort "Please set GCC_DIR to the bare filename of the gcc directory"
test -z "${GLIBC_DIR}" \
  && abort "Please set GLIBC_DIR to the bare filename of the glibc directory"
test -z "${LINUX_DIR}" \
  && abort "Please set LINUX_DIR to the bare filename of the kernel directory"
test -z "${TARGET}" \
  && abort "Please set TARGET to the GNU target identifier" \
     " (e.g. powerpc64-linux)"
test -z "${TARGET_CFLAGS}" \
  && abort "Please set TARGET_CFLAGS to any compiler flags needed when" \
     " building glibc (-O recommended)"

# Additional environment variables are optional, namely:
test -z "${BINUTILS_EXTRA_CONFIG}" && echo  "BINUTILS_EXTRA_CONFIG not set," \
  " so not passing any extra options to the binutils configure script"
test -z "${GCC_EXTRA_CONFIG}" && echo  "GCC_EXTRA_CONFIG not set," \
  " so not passing any extra options to GCC's configure script" 
test -z "${GLIBC_EXTRA_CONFIG}" && echo "GLIBC_EXTRA_CONFIG not set," \
  " so not passing any extra options to glibc's configure script"
test -z "${GLIBC_EXTRA_ENV}"  && echo "GLIBC_EXTRA_ENV not set," \
  " so not passing any extra environment variables to glibc's configure script"
test -z "${KERNELCONFIG}" && test -z ${LINUX_DIR}/.config  \
  && echo  "KERNELCONFIG not set," \
  " and no .config file found, so not configuring linux kernel"

test -z "${KERNELCONFIG}" || test -r "${KERNELCONFIG}"  \
  || abort  "Can't read file KERNELCONFIG = $KERNELCONFIG, please fix."

# And one is derived.
GLIBCTHREADS_FILENAME=`echo $GLIBC_DIR | sed 's/glibc-/glibc-linuxthreads-/'`

set -ex

# Make all paths absolute (it's so confusing otherwise)
# FIXME: this doesn't work well with some automounters
PREFIX=`cd $PREFIX; pwd`
BINUTILS_DIR=`cd $BINUTILS_DIR; pwd`
GCC_DIR=`cd $GCC_DIR; pwd`
LINUX_DIR=`cd $LINUX_DIR; pwd`
GLIBC_DIR=`cd $GLIBC_DIR; pwd`

# make sure the build product's binaries are in the search path
PATH="${PREFIX}/bin:${PATH}"
export PATH

# test that we have write permissions to the install dir
mkdir -p ${PREFIX}/${TARGET}
touch ${PREFIX}/${TARGET}/test-if-write
test -f ${PREFIX}/${TARGET}/test-if-write \
  || abort "You don't appear to have write permissions to ${PREFIX}/${TARGET}."

echo
echo Building for:
echo "    --target=$TARGET"
echo "    --prefix=$PREFIX"

# Keep track of where we're starting from.
BUILD_DIR=`pwd`

# This is used for building kernel headers.
ARCH=ppc64

# Set HOST to something almost, but not completely, identical to BUILD
# This strange operation causes gcc to always generate a cross-compiler
# even if the build machine is the same kind as the host.
BUILD=powerpc-unknown-linux-gnu
HOST=powerpc-linux

#---------------------------------------------------------
echo "`date`  Set up sysroot directory"

# The binutils and GCC builds look for headers and libraries in a
# specified sysroot directory.  Set up such a directory, with links
# into the install tree we're building.
if test '!' -d ${SYSROOT}; then
    mkdir -p ${SYSROOT}/usr
    ln -s ${PREFIX}/${TARGET}/include ${SYSROOT}/usr/include
    for z in etc lib sbin share sys-include
    do
        ln -s ${PREFIX}/${TARGET}/${z} ${SYSROOT}/${z}
    done
fi

#---------------------------------------------------------
echo "`date`  Prepare kernel headers"

cd $LINUX_DIR

if test -f "$KERNELCONFIG" ; then
    cp $KERNELCONFIG .config
fi
if test -f .config; then
    yes "" | make ARCH=$ARCH oldconfig
fi

make ARCH=$ARCH symlinks include/linux/version.h

mkdir -p ${PREFIX}/${TARGET}/include
cp -r include/linux ${PREFIX}/${TARGET}/include
cp -r include/asm-${ARCH} ${PREFIX}/${TARGET}/include/asm
cp -r include/asm-generic ${PREFIX}/${TARGET}/include/asm-generic

#---------------------------------------------------------
echo "`date`  Build binutils"

cd $BUILD_DIR
mkdir -p build-binutils; cd build-binutils

if test '!' -f Makefile; then
    ${BINUTILS_DIR}/configure \
      --prefix=$PREFIX \
      --target=$TARGET \
      --build=$BUILD \
      --host=$HOST \
      --disable-nls \
    || abort "binutils configure failed"
fi

make all || abort "binutils make failed"
make install || abort "binutils install failed"

cd ..

# test to see if this step passed
test -x ${PREFIX}/bin/${TARGET}-ld || abort Build failed during binutils 

#---------------------------------------------------------
echo "`date`  Install glibc headers needed for core compiler"

# Only need to install bootstrap glibc headers for gcc-3.0 and above?
# Or maybe just gcc-3.3 and above?
# This will change for gcc-3.5, I think.
# See also http://gcc.gnu.org/PR8180, which complains about the need for
# this step.
# Don't install them if they're already there (it's really slow)
cd $BUILD_DIR
if grep -q gcc-3 ${GCC_DIR}/ChangeLog \
      && test '!' -f ${PREFIX}/${TARGET}/include/features.h; then
    mkdir -p build-glibc-headers; cd build-glibc-headers

    if test '!' -f Makefile; then
        # The following three things have to be done to build glibc-2.3.x,
        # but they don't hurt older versions.
        # 1. override CC to keep glibc's configure from using $TARGET-gcc. 
        # 2. disable linuxthreads, which needs a real cross-compiler to
        # generate tcb-offsets.h properly
        # 3. build with gcc 3.2 or later
        # Compare these options with the ones used when building glibc for
        # real below - they're different.
        # so when configure checks to make sure gcc has access to the
        # assembler you just built...
        # Alternately, we could put ${PREFIX}/${TARGET}/bin on the path.

        CC=gcc \
        ${GLIBC_DIR}/configure \
          --prefix=${PREFIX}/${TARGET} \
          --target=$TARGET \
          --host=$TARGET \
          --build=$BUILD \
          --with-headers=${PREFIX}/${TARGET}/include \
          --without-cvs \
          --disable-sanity-checks \
        || abort "glibc configure failed"
    fi

    if grep -q GLIBC_2.3 ${GLIBC_DIR}/ChangeLog; then
        # glibc-2.3.x passes cross options to $(CC) when generating
        # errlist-compat.c, which fails without a real cross-compiler.
        # Fortunately, we don't need errlist-compat.c, since we just need
        # .h files, 
        # so work around this by creating a fake errlist-compat.c and
        # satisfying its dependencies.
        # Another workaround might be to tell configure to not use any
        # cross options to $(CC).
        # The real fix would be to get install-headers to not generate
        # errlist-compat.c.
        make sysdeps/gnu/errlist.c
        mkdir -p stdio-common
        touch stdio-common/errlist-compat.c
    fi
    make install-headers \
    || abort "glibc make install-headers failed"

    # Two headers -- stubs.h and features.h -- aren't installed by
    # install-headers,
    # so do them by hand.  We can tolerate an empty stubs.h for the moment.
    # See e.g. http://gcc.gnu.org/ml/gcc/2002-01/msg00900.html

    mkdir -p ${PREFIX}/${TARGET}/include/gnu
    missing_file=${PREFIX}/${TARGET}/include/gnu/stubs.h
    if test '!' -f $missing_file; then
        touch $missing_file
    else
        echo "$missing_file already exists"
    fi

    missing_file=${PREFIX}/${TARGET}/include/features.h
    if test '!' -f $missing_file; then
        cp ${GLIBC_DIR}/include/features.h $missing_file
    else
        echo "$missing_file already exists"
    fi
fi

#---------------------------------------------------------
echo "`date`  Build gcc-core (just enough to build glibc)"

cd $BUILD_DIR
mkdir -p build-gcc-core; cd build-gcc-core

if test '!' -f Makefile; then
    ${GCC_DIR}/configure \
      --target=$TARGET \
      --build=$BUILD \
      --host=$HOST \
      --prefix=$PREFIX \
      --disable-multilib \
      --disable-nls \
      --disable-shared \
      --disable-threads \
      --enable-__cxa_atexit \
      --enable-languages=c \
    || abort "GCC configure failed"
fi

make all-gcc || abort "GCC make failed"
make install-gcc || abort "GCC install failed"


test -x ${PREFIX}/bin/${TARGET}-gcc || abort Build failed during gcc-core 

# The glibc build uses -lgcc_eh but doesn't really need that library.
# Link it to a different library to fake it out for now.

cd ${PREFIX}/lib/gcc/${TARGET}/*
if test '!' -f libgcc_eh.a; then
    ln -s libgcc.a libgcc_eh.a
else
    echo "file `pwd`/libgcc_eh.a already exists"
fi

#---------------------------------------------------------
echo "`date`  Build glibc and linuxthreads (shared, no TLS)"

cd $BUILD_DIR
mkdir -p build-glibc-first; cd build-glibc-first

if test '!' -f Makefile; then
    # Compare these options with the ones used when installing the glibc
    # headers above - they're different.
    # Adding "--without-gd" option to avoid error "memusagestat.c:36:16:
    # gd.h: No such file or directory" 
    # See also http://sources.redhat.com/ml/libc-alpha/2000-07/msg00024.html. 
    # The --enable-clocale=gnu is recomended by LFS;
    # see http://bugs.linuxfromscratch.org/show_bug.cgi?id=411
    # Set BUILD_CC, or you won't be able to build datafiles
    # Set --build, else glibc-2.3.2 will think you're not cross-compiling,
    # and try to run the test programs

    BUILD_CC=gcc \
    CFLAGS="$TARGET_CFLAGS" \
    CC=${TARGET}-gcc \
    AR=${TARGET}-ar \
    RANLIB=${TARGET}-ranlib \
    ${GLIBC_DIR}/configure \
      --prefix=${PREFIX}/${TARGET} \
      --target=$TARGET \
      --host=$TARGET \
      --build=$BUILD \
      --without-cvs \
      --disable-profile \
      --disable-debug \
      --without-gd \
      --with-tls \
      --with-__thread \
      --enable-clocale=gnu \
      --enable-add-ons=linuxthreads \
      --with-headers=${PREFIX}/${TARGET}/include \
    || abort "glibc configure failed"
fi

make || abort "glibc make failed"
make install || abort "glibc install failed"

# Fix problems in linker scripts.
# 
# 1. Remove absolute paths
# Any file in a list of known suspects that isn't a symlink is assumed
# to be a linker script.
# FIXME: test -h is not portable
# FIXME: probably need to check more files than just these three...
# Need to use sed instead of just assuming we know what's in libc.so
# because otherwise alpha breaks
# But won't need to do this at all once we use --with-sysroot
# (available in gcc-3.3.3 and up)
#
# To make "strip *.so.*" not fail (ptxdist does this), rename to
# .so_orig rather than .so.orig
for file in libc.so libpthread.so libgcc_s.so; do
    if test -f ${PREFIX}/${TARGET}/lib/$file \
          && test ! -h ${PREFIX}/${TARGET}/lib/$file; then
        mv ${PREFIX}/${TARGET}/lib/$file ${PREFIX}/${TARGET}/lib/${file}_orig
    fi
done

test -f ${PREFIX}/${TARGET}/lib/libc.a || abort Building libc failed

#---------------------------------------------------------
echo "`date`  Build final gcc"

cd $BUILD_DIR
mkdir -p build-gcc; cd build-gcc

if test '!' -f Makefile; then
    # --disable-nls to work around crash bug on ppc405, but also because
    # embedded systems don't really need message catalogs...
    ${GCC_DIR}/configure \
        --target=$TARGET \
        --build=$BUILD \
        --host=$HOST \
        --prefix=$PREFIX \
        --with-sysroot=${SYSROOT} \
        --disable-nls \
        --disable-multilib \
        --enable-threads=posix \
        --enable-__cxa_atexit \
        --enable-languages=c,c++ \
        --enable-shared \
    || abort "GCC configure failed"
fi

make all || abort "GCC make failed"

echo "Fix the GCC specs file to use the new dynamic linker"
cd gcc
mv specs specs.orig
sed "s,/lib64/ld64\.so\.1,${PREFIX}/${TARGET}/lib/ld64.so.1," \
  < specs.orig > specs
cd ..

make install || abort "GCC install failed"

echo "If the chip does not have a floating point unit, and there are"
echo "shared libraries in /lib/nof, copy them to /lib"
echo "We check GLIBC_EXTRA_CONFIG ($GLIBC_EXTRA_CONFIG) to see if it"
echo "contains --without-fp to decide."
case "$GLIBC_EXTRA_CONFIG" in
   *--without-fp*)
      cp -a ${PREFIX}/${TARGET}/lib/nof/*.so* ${PREFIX}/${TARGET}/lib
      ;;
esac

test -x ${PREFIX}/bin/${TARGET}-gcc || abort "Build failed during final gcc"

#---------------------------------------------------------
echo "`date`  Build final glibc and linuxthreads"

# glibc needs shared GCC libraries in order to be fully functional, so
# build it again.  This time enable TLS support.

cd $BUILD_DIR
mkdir -p build-glibc; cd build-glibc

if test '!' -f Makefile; then
    BUILD_CC=gcc \
    CFLAGS="$TARGET_CFLAGS" \
    CC=${TARGET}-gcc \
    AR=${TARGET}-ar \
    RANLIB=${TARGET}-ranlib \
    ${GLIBC_DIR}/configure \
      --prefix=${PREFIX}/${TARGET} \
      --target=$TARGET \
      --host=$TARGET \
      --build=$BUILD \
      --without-cvs \
      --disable-profile \
      --disable-debug \
      --without-gd \
      --with-tls \
      --with-__thread \
      --enable-clocale=gnu \
      --enable-add-ons=linuxthreads \
      --with-headers=${PREFIX}/${TARGET}/include \
    || abort "glibc configure failed"
fi

make || abort "glibc make failed"
make install || abort "glibc install failed"

#---------------------------------------------------------
echo "`date`  Cross-toolchain build complete."
exit 0


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]