#!/bin/sh

# makeconfigure deprecated

# Copyright (C) 2000-2021 Free Software Foundation, Inc.
# Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
#
# GNU Modula-2 is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
#
# GNU Modula-2 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GNU Modula-2; see the file COPYING.  If not, write to the
# Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
#

#
# a simple shell script which builds the configuration definition
# file:
#
# gm2s/386-gas/M2Configure.def
#

#
# create configuration constant
#

EmitConst () {
   echo "  " $1 "=" $2 ";  (* automatically generated *)" >> $DESTINATION
   echo "" >> $DESTINATION
}

#
# remove the old configuration file and create new empty ones
#

Initialize () {
   /bin/rm -f $DESTINATION
   touch $DESTINATION
}


#
# ModulePrologue - write the start of the definition module.
#

ModulePrologue () {
   cat >> $DESTINATION << EOF
DEFINITION MODULE M2Configure ;

(*
   Author     : Gaius Mulley
   Title      : M2Configure
   Date       : Thu May 17 17:45:37 BST 1990
   Description: Exports configuration constants.
                Options that should not be overridden by users,
                therefore these are compiler compile time options.

EOF
   echo "                DO NOT EDIT AS THIS IS AUTOMATICALLY GENERATED BY $ExecName" >> $DESTINATION
   echo "" >> $DESTINATION
   echo "   Generated  :" `date` >> $DESTINATION
   cat >> $DESTINATION << EOF
*)

EXPORT QUALIFIED PushParametersLeftToRight,
                 ReturnArgumentOnStack,
                 SystemIsXenix,
                 UseUnderscoreForC,
                 UseDotForGDBLabels,
                 AlignmentSize,
                 ActivationRecordOffset,
                 UseDotForJumpLabels,
                 UseShortStabLineNumbers,
                 DefaultLibraryPath,
                 UsingGCCBackend ;


CONST

(*
   PushParametersLeftToRight - if TRUE indicates that the parameters
                               are pushed in order from left to right.
                               if FALSE indicates that the parameters
                               are pushed in order from right to left.
*)

   PushParametersLeftToRight = TRUE ;


(*
   ReturnArgumentOnStack - if TRUE indicates that the return argument
                           from a function is on the stack. The
                           return argument in this case is thought of
                           as parameter 0 and is treated as such.
                           if FALSE indicates that the return argument
                           from a function is held in a register.
*)

   ReturnArgumentOnStack = FALSE ;

EOF
}


#
# ModuleEpilogue - terminates the definition module.
#

ModuleEpilogue () {
   echo "" >> $DESTINATION
   echo "END M2Configure." >> $DESTINATION
}


#
# work out whether we are compiling on a xenix system
#

IsSystemXenix () {
   echo -n "are we compiling under xenix..."
   cat >> $DESTINATION << EOF3
(*
   SystemIsXenix - if true then we are compiling the compiler for Xenix.
*)

EOF3
   if grep -i xenix /etc/motd
   then
      EmitConst SystemIsXenix TRUE
      echo "yes"
   else
      EmitConst SystemIsXenix FALSE
      echo "no"
   fi
}


UseUnderscoreForC () {
   cat >> $DESTINATION << EOF
(*
   UseUnderscoreForC - if true then the C compiler uses _ in front of
                       all declarations.
*)

EOF
   echo -n "does cc use an underscore prefix..."
   /bin/rm -f testus.c
   cat > testus.c << EOF
   main () {}
EOF
   cc -c testus.c
   if nm --extern-only testus.o | grep _main > /dev/null
   then
      EmitConst UseUnderscoreForC TRUE
      echo "yes"
   else
      EmitConst UseUnderscoreForC FALSE
      echo "no"
   fi
   /bin/rm -f testus.[co]
}


UseDotForGDBLabels () {
   cat >> $DESTINATION << EOF
(*
   UseDotForGDBLabels - if true then the C compiler produced .LBB2: for the
                        beginning of procedures and functions. If false then
                        the C compiler produced LBB2
*)

EOF
   echo -n "does cc generate a dot before GDB labels..."
   /bin/rm -f testus.c
   cat > testus.c << EOF
   main () {}
EOF
   cc -S -g testus.c
   if grep '\.L' testus.s > /dev/null
   then
      EmitConst UseDotForGDBLabels TRUE
      echo "yes"
   else
      EmitConst UseDotForGDBLabels FALSE
      echo "no"
   fi
   /bin/rm -f testus.[cs]
}


UseDotForJumpLabels () {
   cat >> $DESTINATION << EOF
(*
   UseDotForJumpLabels - if true then the C compiler produced .L2: for jump
                         labels. If false then the C compiler produced L2
*)

EOF
   echo -n "does cc generate a dot before jump labels..."
   /bin/rm -f testus.c
   cat > testus.c << EOF
   int x, y;
   main () { if (x>y) main(); }
EOF
   cc -S testus.c
   if grep '^\.L' testus.s > /dev/null
   then
      EmitConst UseDotForJumpLabels TRUE
      echo "yes"
   else
      EmitConst UseDotForJumpLabels FALSE
      echo "no"
   fi
   /bin/rm -f testus.[cs]
}


secondword () {
   echo $2
}


AlignmentSize () {
   cat >> $DESTINATION << EOF
(*
   AlignmentSize - returns the default alignment size used.
*)

EOF
   echo -n "alignment size..."
   /bin/rm -f testus.c
   cat > testus.c << EOF
   main () {}
EOF
   cc -g -S testus.c
   size=`secondword \`grep align testus.s | head -1\``
   EmitConst AlignmentSize $size
   echo $size
   /bin/rm -f testus.[cos]
}


ActivationRecordOffset () {
   cat >> $DESTINATION << EOF
(*
   ActivationRecordOffset - the number of words relative to frame pointer
                            which points to the statically declared parental
                            scope.
*)

EOF
   EmitConst ActivationRecordOffset -1
}


UseShortStabLineNumbers () {
   cat >> $DESTINATION << EOF
(*
   UseShortStabLineNumbers - if true then the C compiler produced .stabd 68,0,2
                             for line number information, rather than the more
                             elaborate relative to function description.
*)

EOF
   echo -n "does cc use short stab form for line numbers..."
   /bin/rm -f testus.c
   cat > testus.c << EOF
   int x, y;
   main () { if (x>y) main(); }
EOF
   cc -S -g testus.c
   if grep '68,0,2$' testus.s > /dev/null
   then
      EmitConst UseShortStabLineNumbers TRUE
      echo "yes"
   else
      EmitConst UseShortStabLineNumbers FALSE
      echo "no"
   fi
   /bin/rm -f testus.[cs]
}


DefaultLibraryPath () {
   cat >> $DESTINATION << EOF
(*
   DefaultLibraryPath - defermines the default library path and creates a
                        constant string.
*)

EOF
   echo -n "modula-2 library files in..."
   EmitConst DefaultLibraryPath "\"`cat librarydirectory` .\""
   echo \"`cat librarydirectory` .\"
}


UsingGCCBackend () {
   cat >> $DESTINATION << EOF
(*
   UsingGCCBackend - is the compiler being built with the GCC code generator?
*)
EOF
   echo -n "building modula-2 using gcc codegenerator..."
   EmitConst UsingGCCBackend $1
   echo $1
}


#
# initialize and run this script
#

if [ "$1" = "" ] ; then
   echo "makeconfigure needs a parameter TRUE or FALSE to determine whether to use gcc backend"
   exit 1
fi

ExecName=$0
DESTINATION=M2Configure.def
Initialize
ModulePrologue
IsSystemXenix
UseUnderscoreForC
UseDotForGDBLabels
AlignmentSize
ActivationRecordOffset
UseDotForJumpLabels
UseShortStabLineNumbers
DefaultLibraryPath
UsingGCCBackend $1
ModuleEpilogue
exit 0
