#!/bin/sh
# Script to build the vxWorks 5.5 compiler tools for m68k 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 200MB 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-m68k.tgz

# The tarfile we create
PRODUCT=${HERE}/gnu-tools.tor2_2-m68k-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-68k
mkdir wind-68k
cd wind-68k

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

# Create Wind River's directory structure
mkdir -p host/x86-linux2/bin
mkdir -p host/src/gnu
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

# Extract the sources
tar xfz ${SOURCE}

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

# Patch in a few source changes
chmod +w gas/as.h gas/tc.h gcc/gcc.c
patch -p0 << '__EOF__'
=== modified file 'gas/as.h'
# Move struct definition back, JF broke the build
--- gas/as.h       2012-01-18 16:56:35 +0000
+++ gas/as.h       2012-01-18 17:01:42 +0000
@@ -605,7 +605,19 @@
 struct expressionS;
 struct fix;
 typedef struct symbol symbolS;
-struct relax_type;
+struct relax_type
+{
+  /* Forward reach. Signed number. > 0.  */
+  long rlx_forward;
+  /* Backward reach. Signed number. < 0.  */
+  long rlx_backward;
+
+  /* Bytes length of this address.  */
+  unsigned char rlx_length;
+
+  /* Next longer relax-state.  0 means there is no 'next' relax-state.  */
+  relax_substateT rlx_more;
+};
 typedef struct frag fragS;
 
 #ifdef BFD_ASSEMBLER
=== modified file 'gas/tc.h'
# How we know it was JF who broke it...
--- gas/tc.h       2012-01-18 16:56:35 +0000
+++ gas/tc.h       2012-01-18 17:01:22 +0000
@@ -24,23 +24,6 @@
 
 extern const pseudo_typeS md_pseudo_table[];
 
-/* JF moved this here from as.h under the theory that nobody except MACHINE.c
-   and write.c care about it anyway.  */
-
-struct relax_type
-{
-  /* Forward reach. Signed number. > 0.  */
-  long rlx_forward;
-  /* Backward reach. Signed number. < 0.  */
-  long rlx_backward;
-
-  /* Bytes length of this address.  */
-  unsigned char rlx_length;
-
-  /* Next longer relax-state.  0 means there is no 'next' relax-state.  */
-  relax_substateT rlx_more;
-};
-
 typedef struct relax_type relax_typeS;
 
 extern const int md_reloc_size;	/* Size of a relocation record */
=== 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)
__EOF__

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

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
