% 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"  : "i486-unknown-freebsdelf",
  "FreeBSD4"  : "i486-unknown-freebsdelf",
  "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--cygwin32",
  "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"
}

if equal(TARGET, "NT386")
  % we assume that we use cygwin for everything under NT386GNU
  SL = "\\"
else
  SL = "/"
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

% 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

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

% misc fixups & ship commands
pgms = "LANGUAGES=m3cg"
postcp = "cp -p gcc/cm3cg ."
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/cm3cg . && cp -p gcc/mips-tfile ."
  end
end

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

% finally, compile it
exec ("cd", build_dir, ";", M3CC_MAKE, ARG0, ARG1, pgms)
if defined("postcp")
  exec(postcp)
end



