#!/bin/sh
# Script to build the vxWorks 5.5 compiler tools for ppc on linux.
# This builds a 32-bit binary since gcc 2.95 pre-dated x86_64,
# but it can be run on a 64-bit system with 32-bit libraries.
# It uses the gcc34 compiler on the host, newer versions of gcc
# do not produce working cross-compilers.

# The end result is a compressed tarfile of the host/x86-linux
# directory tree that can be extracted directly into the root
# of the tornado installation.

# ----------- Set the following parameters

# Scratchpad location, needs about 400MB free space
SCRATCH=/local

# Existing Tornado 2.2.x installation
TORNADO=/usr/local/vw/tornado22-68k

# For convenience
HERE=`/bin/pwd`

# Location of source tarfile from Wind River, unmodified
SOURCE=${HERE}/cum.tor2_2-ppc.tgz

# The tarfile we create
PRODUCT=${HERE}/gnu-tools.tor2_2-ppc-rhel5.tgz

# ----------- End of user parameters

# Must use the gcc 3.4 compiler and ILP32 model
GCC=`which gcc34`
CC="${GCC} -m32"

# Compiler flags, no-aliasing is essential
CFLAGS='-O2 -fno-strict-aliasing'

# Create scratch area
cd ${SCRATCH}
rm -rf wind-ppc

mkdir wind-ppc
cd wind-ppc

# Point to WRS build tree
export WIND_BASE=${SCRATCH}/wind-ppc

# Create Wind River's directory structure
mkdir -p host/x86-linux2/bin
mkdir -p host/src/gnu.ppc
mkdir -p native/x86-linux2/bin

# Make gcc actually run gcc34 (makefile prepends this dir to path)
ln -s ${GCC} native/x86-linux2/bin/gcc

# This is where it finds the vxWorks headers
ln -s ${TORNADO}/target

cd host/src/gnu.ppc

# Extract the sources
tar xfz ${SOURCE}

# Disable a couple of modules
mv intl intl-disabled
mv texinfo texinfo-disabled

# Build the WRS fake version of makeinfo
${GCC} ${CFLAGS} -o ../../x86-linux2/bin/makeinfo makeinfo.c

# Patch in a few source changes
chmod +w gcc/configure gcc/dwarf2out.c gcc/gcc.c gcc/toplev.c

patch -p0 << '__EOF__'
=== modified file 'gcc/configure'
# Fix a syntax error in the gcc/configure script that generates a warning
--- gcc/configure	2001-12-07 15:02:29.000000000 -0600
+++ gcc/configure	2012-02-07 15:02:35.000000000 -0600
@@ -8194,7 +8194,7 @@
 echo $ac_n "checking assembler alignment features""... $ac_c" 1>&6
 echo "configure:8196: checking assembler alignment features" >&5
 if test "$gcc_cv_as_alignment_features" = ""; then
-if test x$gcc_cv_as = x ]; then
+if test x$gcc_cv_as = x; then
 gcc_cv_as_gas_srcdir=`echo $srcdir | sed -e 's,/gcc$,,'`/gas
 if test -x "$DEFAULT_ASSEMBLER"; then
 	gcc_cv_as="$DEFAULT_ASSEMBLER"
=== modified file 'gcc/dwarf2out.c'
# Someone takes the address of loc_descr so it can't be register
--- gcc/dwarf2out.c	2012-01-18 15:51:04.584343657 -0600
+++ gcc/dwarf2out.c	2012-01-18 15:52:09.243344230 -0600
@@ -7043,7 +7043,7 @@
      register tree decl, for_type;
 {
   register unsigned long offset;
-  register dw_loc_descr_ref loc_descr;
+  dw_loc_descr_ref loc_descr;
   register enum dwarf_location_atom op;
 
   if (TREE_CODE (decl) == TREE_VEC)
=== modified file 'gcc/gcc.c'
# Disable WRS licensing checks
--- gcc/gcc.c      2012-01-18 16:56:35 +0000
+++ gcc/gcc.c      2012-01-18 20:31:24 +0000
@@ -2672,7 +2672,7 @@
 static int *warn_std_ptr = 0;
 
 /* WRS LOCAL true if get_feature should _not_ be invoked.  */
-static int no_license;
+static int no_license = 1;
 
 
 #if defined(HAVE_OBJECT_SUFFIX) || defined(HAVE_EXECUTABLE_SUFFIX)
=== modified file 'gcc/toplev.c'
# Static routine after non-static prototype
--- gcc/toplev.c	2009-12-03 11:06:20.000000000 -0600
+++ gcc/toplev.c	2012-01-18 15:44:10.448612460 -0600
@@ -230,7 +230,7 @@
 void print_rtl_graph_with_bb PROTO ((const char *, const char *, rtx));
 void clean_graph_dump_file PROTO ((const char *, const char *));
 void finish_graph_dump_file PROTO ((const char *, const char *));
-void oom PROTO ((unsigned));
+static void oom PROTO ((unsigned));
 /* Length of line when printing switch values.  */
 #define MAX_LINE 75
 
__EOF__

# Build it!
linux32 make -f makefile CC="${CC}" CFLAGS="${CFLAGS}" ppc

cd ../..

# Host-type adjustment needed for EPICS
mv x86-linux2 x86-linux

cd ..

# Now tar up the tree we just built
tar cfz ${PRODUCT} host/x86-linux
