% Copyright (C) 1994, Digital Equipment Corporation
% All rights reserved.
% See the file COPYRIGHT for a full description.
%
% Last modified on Fri Feb 10 13:16:06 PST 1995 by kalsow
%

readonly Platform_info = {
  "AIX386"    : "i486-ibm-aix",
  "ALPHA_OSF" : "alpha-dec-osf1",
  "AP3000"    : "apollo68-bsd",
  "ARM"       : "arm--riscos",
  "BSDI4"     : "i386-unknown-freebsdelf",
  "DS3100"    : "decstation",
  "FBSD_ALPHA": "alpha-unknown-freebsd",
  "FreeBSD"   : "i486-unknown-bsd",
  "FreeBSD2"  : "i486-unknown-freebsd",
  "FreeBSD3"  : "i386-unknown-freebsd3",
  "FreeBSD4"  : "i386-unknown-freebsd4",
  "HP300"     : "m68k-hp-hpux",
  "HPPA"      : "hppa1.1-hp-hpux",
  "IBMR2"     : "rs6000-ibm-aix3.2",
  "IBMRT"     : "romp-ibm-aos",
  "IRIX5"     : "mips-sgi-irix5",
  "LINUX"     : "i486--linux",
  "LINUXELF"  : "i486--linuxelf",
  "LINUXLIBC6": "i486--linuxelf",
  "NEXT"      : "next-bsd",
  "NT386"     : "i486--nt",
  "NT386GNU"  : "i486-pc-cygwin",
  "OKI"       : "i860--sysv4.0",
  "SEQUENT"   : "i386-sequent-bsd",
  "SOLgnu"    : "sparc-sun-solaris2",
  "SOLsun"    : "sparc-sun-solaris2",
  "SPARC"     : "sparc-sun-sunos4.1",
  "SUN3"      : "m68k-sun-sunos4.1",
  "SUN386"    : "i386-sun-sunos4.1",
  "Tru64v5"   : "alpha-dec-osf1",
  "UMAX"      : "encore-bsd",
  "VAX"       : "vax-dec-ultrix",
  "I386_DARWIN" : "i486-unknown-bsd",
  "PPC_DARWIN"  : "powerpc-apple-darwin6.3",
  "PPC_LINUX"   : "powerpc-apple-linuxelf"
}

if not defined("SL")
  if equal(TARGET, "NT386")
    % we assume that we use cygwin for everything under NT386GNU
    SL = "\\"
  else
    SL = "/"
  end
end

if equal(TARGET, "PPC_DARWIN")
  write(EOL)
  write("  Please note that you need a bootstrapped gcc 3.2.1 in order to" &
        EOL)
  write("  compile this package. All attempts to build cm3cg with Apple's" &
        EOL)
  write("  native version of gcc 3.1 have failed so far." & EOL)
  write(EOL)
end

readonly proc GNU_platform (x) is
  if Platform_info contains x
    return Platform_info{x}
  else
    error ("GNU platform is not known for \"" & x & "\"")
    return "unknown-unknown-unknown"
  end
end

readonly m3cc_config = {

  "HPPA"  : "--with-gnu-as",
  % The HP assembler doesn't understand inline debugger info.

  "IRIX5" : "--with-stabs --with-gnu-as"
  % mips-sgi-irix5 does not support debugging using the native
  % assembler.  If you don't have gas, delete the config options above.
  % You will need the latest version of gas (binutils-2.5 or better).
  % --with-stabs is necessary because Modula-3 v3.3 generates funny
  % symbol names that can't be parsed by the ECOFF debugging
  % directives.  [Modula-3 v3.4 and later don't generate funny
  % symbol names. -- Bill Kalsow 1/13/94]

} % m3cc_config

readonly proc get_config (target) is
  if m3cc_config contains target
    return m3cc_config {target}
  else
    return ""
  end
end

readonly proc get_overrides (nm, ov) is
  if equal (ov, "*")
    return ""
  else
    return format ("%s=\"%s\" ", nm, ov)
  end
end

% check for overrides, otherwise use the defaults from the configuration file
if not defined ("M3CC_HOST")    M3CC_HOST   = TARGET     end
if not defined ("M3CC_TARGET")  M3CC_TARGET = TARGET     end
if not defined ("M3CC_CC")      M3CC_CC     = GNU_CC     end
if not defined ("M3CC_CFLAGS")  M3CC_CFLAGS = GNU_CFLAGS end
if not defined ("M3CC_MAKE")    M3CC_MAKE   = GNU_MAKE   end
if not defined ("M3CC_CONFIG")  M3CC_CONFIG = get_config (M3CC_TARGET) end

% HACK: avoid the use of DW_FORM_strp debugging information for the time being
M3CC_CFLAGS = M3CC_CFLAGS & " -DAVOID_DW_FORM_STRP"

% figure out where we're going to build the beast
local build_dir = "."  % let m3build set the build directory
if not equal (M3CC_HOST, M3CC_TARGET)
  build_dir = ".." & SL & BUILD_DIR & "-" & M3CC_TARGET
end

% make sure the derived directory exists
if stale (build_dir, build_dir)
  exec ("mkdir", build_dir)
end

done  = ".configure-done"
donep = build_dir & SL & done
if defined ("FORCE")
  exec("-cd " & build_dir & " && test -f Makefile && gmake distclean")
  exec("-cd " & build_dir & " && rm -f cm3cg")
  exec("-rm -f " & donep)
end

if not defined ("no_config")
  % configure the sources
  if stale(donep, donep)
    exec ("cd", build_dir, "; .." & SL & "gcc" & SL & "configure", M3CC_CONFIG,
             "--srcdir=.." & SL & "gcc",
             "--host=" & GNU_platform (M3CC_HOST),
             "--build=" & GNU_platform (M3CC_HOST),
             "--target=" & GNU_platform (M3CC_TARGET),
             "&& echo \"done\" > " & done)
  end
end

% check for non-default flags
ARG0 = get_overrides ("CC", M3CC_CC)
ARG1 = get_overrides ("CFLAGS", M3CC_CFLAGS)

% build commands
cmds = "cd " & build_dir & "; "
cmds = cmds & "cd libiberty; " & M3CC_MAKE & "; "
cmds = cmds & "cd .." & SL & "gcc; " & M3CC_MAKE & " " & 
       ARG0 & " " & ARG1 & " m3cg"

readonly proc postcp() is
  pSrc  = build_dir & SL & "gcc" & SL & "m3cgc1"
  pDest = build_dir & SL & "cm3cg"
  if stale(pDest, pSrc)
    cp_if(pSrc, pDest)
  end
end


if equal (M3CC_HOST, M3CC_TARGET)
  BindExport ("cm3cg")
  if equal (M3CC_HOST, "DS3100") or equal (M3CC_HOST, "ALPHA_OSF")
    pgms = "LANGUAGES='m3cg mips-tfile'"
    BindExport ("mips-tfile")
    postcp = "cp -p gcc" & SL & "m3cgc1 cm3cg && cp -p gcc" & SL & "mips-tfile ."
  end
end

% finally, compile it
exec (cmds)
%write (cmds & EOL)
if defined("postcp")
  postcp()
end

