Linux to PPC Darwin Cross Compiler

From CGSecurity
Jump to navigation Jump to search

En.png English De.png Deutsch Es.png Español It.png Italiano


Linux to powerpc Darwin/OSX cross-compiler

This script is designed to be run as root. The goal is to install a cross-compiler to be able to build TestDisk and PhotoRec for MacOS X.

#!/bin/sh
# This script builds and installs a gcc cross compiler from Linux
# to darwin
# Copyright Christophe GRENIER <grenier@cgsecurity.org>
# Licence: GPL v2 or any later version
# Version 2006/02/24
root=`pwd`
name=`basename $0`
WGET=wget

die () {
        echo >&2 "$1"
        exit 1
}

exists () {
        if [ ! -r "$1" ]; then
                die "$1 does not exist"
        fi
}

usage () {
        die "usage: $name [clean]"
}

case "$#" in
1)
        case "$1" in
        clean)
                rm -rf build-binutils* build-gcc*
                exit 0
        ;;
        *)
                usage
        ;;
        esac
;;
esac

# You may want to change the installation prefix, which is where the
# script will install the cross-compiler tools.
prefix='/usr'
gccVers='4.0.2'
odcctoolsVers='20060123'
GCCSOURCE="ftp://ftp.uvsq.fr/pub/gcc/releases/gcc-$gccVers"
DARWINROOTS="http://darwinsource.opendarwin.org/Roots"
CCTOOLSSOURCE="http://www.opendarwin.org/downloads/"

target='powerpc-apple-darwin'
configureGCCFlags="--with-as=/usr/bin/$target-as  --with-ld=/usr/bin/$target-ld --with-libtool=/usr/bin/$target-libtool"
makeGCCFlags=''
gccTarBz2="gcc-core-$gccVers.tar.bz2"
gxxTarBz2="gcc-g++-$gccVers.tar.bz2"

echo 'Checking that needed files exist.'
for file in architecture-245.root.tar.gz Csu-57.root.tar.gz dyld-43.root.tar.gz gcc-4061.root.tar.gz gcc_os-1809.root.tar.gz gcc_select-46.root.tar.gz Libc-391.root.tar.gz Libc_headers-391.root.tar.gz Libc_static-391.root.tar.gz libdyld-43.root.tar.gz Libm-93.root.tar.gz Libmx-93.root.tar.gz Libsystem-88.root.tar.gz ncurses-8.1.root.tar.gz SystemStubs-5.root.tar.gz xnu-792.1.5.root.tar.gz zlib-15.root.tar.gz OpenSSL-26.root.tar.gz
do
if [ ! -e $file ];
then
  $WGET $DARWINROOTS/$file
  exists $file
fi
done

#$gxxTarBz2
for file in $gccTarBz2
do
if [ ! -e $file ];
then
  $WGET $GCCSOURCE/$file
  exists $file
fi
done

for file in odcctools-$odcctoolsVers.tar.bz2
do
if [ ! -e $file ];
then
  $WGET $CCTOOLSSOURCE/$file
  exists $file
fi
done

echo 'Creating the env'
mkdir -p $prefix/$target || die "Cannot create $prefix/$target."
ln -sf $prefix/$target $prefix/$target/usr
ln -sf $prefix/$target $prefix/$target/local
ln -sf $prefix/$target/lib/system/ /usr/lib/system
for file in architecture-245.root.tar.gz Csu-57.root.tar.gz dyld-43.root.tar.gz gcc-4061.root.tar.gz gcc_os-1809.root.tar.gz gcc_select-46.root.tar.gz Libc-391.root.tar.gz Libc_headers-391.root.tar.gz Libc_static-391.root.tar.gz libdyld-43.root.tar.gz Libm-93.root.tar.gz Libmx-93.root.tar.gz Libsystem-88.root.tar.gz ncurses-8.1.root.tar.gz SystemStubs-5.root.tar.gz xnu-792.1.5.root.tar.gz
do
  tar xzf $file -C $prefix/$target
done

echo 'Building odcctools.'
if [ ! -d odcctools-$odcctoolsVers ];
then
        tar xjf odcctools-$odcctoolsVers.tar.bz2
fi
cd odcctools-$odcctoolsVers
eval    ./configure --target=$target --prefix=/usr 2>&1 | tee $root/configure-odcctools-$target.log || die "Configure of odcctools failed."
# Remove ld64 as it doesn't compile
perl -pi -e  "s,COMPONENTS = ar as include libstuff libmacho ld ld64 man misc otool,COMPONENTS = ar as include libstuff libmacho ld man misc otool,;" Makefile
eval    make 2>&1 | tee $root/odcctools-$target.log || die "Build of odcctools failed."
eval    make install 2>&1 | tee $root/odcctools-$target-install.log
#|| die "Install of odcctools failed."

echo 'Building gcc.'
cd $root
if [ ! -d gcc-$gccVers ];
then
        tar xjf $gccTarBz2
#       tar xjf $gxxTarBz2
fi
perl -pi -e  "s,/usr/bin/libtool,/usr/bin/$target-libtool,;" gcc-$gccVers/gcc/config/darwin.h
mkdir -p build-gcc-$target
cd build-gcc-$target
#       --enable-languages=c,c++ \
eval ../gcc-$gccVers/configure -v $configureGCCFlags \
        --enable-languages=c \
        --prefix=$prefix \
        --target=$target \
        --enable-static --enable-shared  \
        2>&1 | tee $root/configure-gcc-$target.log ||
        die "Configure of gcc failed."
eval make $makeGCCFlags all install 2>&1 |tee $root/gcc-$target.log ||
        die "Build of gcc failed."
echo 'Success.'

Once the cross-compiler is installed, you should be able to build a program using

./configure --host=powerpc-apple-darwin && make