#! /usr/bin/bash

# Invoke the generated Makefile.

# wri-make takes one option:
#  -[g]        If 'g' is present, build a debugging version.
#              (Adds a .PDB symbol table to the library)
# All other command-line arguments follow -g and are passed on to make.

# Unfortunately, libtool doesn't respect the compiler and linker
# flags given on the command line.  But we can force certain flags
# by using the global environment variables WRICCFLAGS and WRILDFLAGS,
# which add options to wricc and wrild.
WRICCFLAGS="-q"	# Add Microsoft extensions
WRILDFLAGS="-Gexport-all-symbols -Yl,/BASE:0x62000000 -s 's/\(.*\)lib\(.*\)-[0-9].dll\$/\1\2.dll/; s/gmp/GMP/g'"
# Set a DLL base address, ensure that library files are output as
# GMP.dll, not libgmp-3.dll, by supplying a sed transformation script,
# and make any DLLs export all their symbols.
export WRICCFLAGS
export WRILDFLAGS

# Making a debug version?  (wri-make -g).  Add -g flag to WRICCFLAGS.
case x"$1" in
    x-g ) WRICCFLAGS="$WRICCFLAGS -g" 
          shift ;;
esac

# Get the directory that this script is located in.  We require
# that the other WRI tools (wricc, wrild, ln, etc.) are in the same
# directory.
script_dir=`dirname "${0}"`
script_dir=`cd "${script_dir}"; pwd`

# Irritatingly, configure generates a libtool that uses the wrong
# kind of quotes in its definition of fix_srcfile_path.  Fix that
# if you can.
if [ -f libtool ] && ( grep '^fix_srcfile_path="' libtool >/dev/null 2>&1 )
then
    cp libtool libtool.orig
    sed "s/^\(fix_srcfile_path=\)\"\(.*\)\"\$/\1'\2'/" libtool.orig > libtool
fi

# Equally irritatingly, configure places an unjustified limit on line length
# in libtool.  If it's only four digits long (e.g. 8192), let's bump it up by
# a factor of ten (by appending a 0).
if [ -f libtool ] && ( grep '^max_cmd_len=....$' libtool >/dev/null 2>&1 )
then
    cp libtool libtool.orig
    sed "s/^\(max_cmd_len=....\)$/\10/" libtool.orig > libtool
fi



PATH="$script_dir":$PATH
make $@



# Copyright 2010 Wolfram Research, Inc.
# 
# This file is part of the build scripts for building the GNU MP Library
# for Mathematica on Windows (32-bit).
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 3 of the License, or (at your
# option) any later version.
# 
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
# License for more details.
# 
# You should have received a copy of the GNU Lesser General Public License
# along with this program.  If not, see http://www.gnu.org/licenses/.
