% 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
%

local ret = 0

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_BUILD")   M3CC_BUILD  = TARGET     end
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

  if stale (build_dir, build_dir)
    ret = exec (["mkdir", build_dir])
  end
end

if not defined ("no_config")
  % configure the sources
  local cmd = ["../gcc/configure", M3CC_CONFIG, "--srcdir=../gcc",
           "--host=" & gnu_platform (M3CC_HOST),
           "--build=" & gnu_platform (M3CC_BUILD),
           "--target=" & gnu_platform (M3CC_TARGET)]
  if equal(OS_TYPE,"WIN32")
    > "tmpsh" in
      write("#! /bin/sh",CR)
      write(cmd,CR)
    end
    ret = exec(["sh","tmpsh"],[],build_dir)
  else
    ret = exec(cmd,[],build_dir)
  end
  if not equal(ret,0) error("Failed to configure m3cc") end
end

% misc fixups & ship commands
pgms = "m3cgc1"
if equal (M3CC_HOST, M3CC_TARGET)
  LibdExport (program_name("m3cgc1"))
  if equal (M3CC_HOST, "DS3100") or equal (M3CC_HOST, "ALPHA_OSF")
  or equal (M3CC_HOST, "Tru64v5")
    pgms = ["m3cgc1", "mips-tfile"]
    LibdExport ("mips-tfile")
  end
  if equal (M3CC_HOST, "ALPHA_OSF") or equal (M3CC_HOST, "Tru64v5")
    > "as" in
      write("#!/bin/sh",CR)
      write("/bin/as $*",CR)
    end
    > "fixobj" in
      write("#!/bin/sh",CR)
      write(LIB_USE & "/mips-tfile $*",CR)
    end
    LibdExport ("as")
    LibdExport ("fixobj")
  end
end

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

% finally, compile it
ret = exec ([M3CC_MAKE, ARG0, ARG1, pgms],[], build_dir)
if not equal(ret,0) error("Failed to build m3cc") end


