%
% $Id: m3makefile,v 1.12 2008-04-23 12:31:24 jkrell Exp $
%
% Copyright (C) 1994, Digital Equipment Corporation
% All rights reserved.
% See the file COPYRIGHT for a full description.
%
% Last modified on Fri Feb 10 13:17:23 PST 1995 by kalsow
%

% Define quick to avoid doing a configure.  It should only need to be done
%  once on a given platform. 
%quick = 1

readonly Platform_info = {
  "AIX386"    : "i686-ibm-aix",
  "ALPHA_OSF" : "alpha-dec-osf1",
  "AMD64_DARWIN" : "amd64-apple-darwin8.7.1",
  "AMD64_LINUX" : "amd64-pc-linux-gnu",
  "AP3000"    : "apollo68-bsd",
  "ARM"       : "arm--riscos",
  "BSDI4"     : "i686-unknown-freebsdelf",
  "DS3100"    : "decstation",
  "FBSD_ALPHA": "alpha-unknown-freebsd",
  "FreeBSD"   : "i686-unknown-bsd",
  "FreeBSD2"  : "i686-unknown-freebsd",
  "FreeBSD3"  : "i686-unknown-freebsd3",
  "FreeBSD4"  : "i686-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"     : "i686-pc-linux-gnuaout",
  "LINUXELF"  : "i686-pc-linux-gnu",
  "LINUXLIBC6": "i686-pc-linux-gnu",
  "NEXT"      : "next-bsd",
  "NT386"     : "i686-pc-mingw32",
  "NT386GNU"  : "i686-pc-cygwin",
  "OKI"       : "i860--sysv4.0",
  "SEQUENT"   : "i686-sequent-bsd",
  "SOLgnu"    : "sparc-sun-solaris2",
  "SOLsun"    : "sparc-sun-solaris2",
  "SPARC"     : "sparc-sun-sunos4.1",
  "SUN3"      : "m68k-sun-sunos4.1",
  "SUN386"    : "i686-sun-sunos4.1",
  "Tru64v5"   : "alpha-dec-osf1",
  "UMAX"      : "encore-bsd",
  "VAX"       : "vax-dec-ultrix",
  "I386_DARWIN" : "i686-apple-darwin8.7.1",
  "PPC_DARWIN"  : "powerpc-apple-darwin6.3",
  "PPC_LINUX"   : "powerpc-apple-linuxelf",
  "NetBSD2_i386" : "i686-unknown-netbsdelf"
}

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 m3gdb_config = {
  % misc. gdb configuration options  (e.g.  "DS3100" : "--with-stabs")
} % m3gdb_config

readonly proc get_config (target) is
  if m3gdb_config contains target
    return m3gdb_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

readonly proc Platform_ExeExtension (Platform) is
  if {"NT386":1,"NT386GNU":1,"AMD64_NT":1,"IA64_NT":1,"ARM_CE":1} contains Platform
    return ".exe"
  else
    return ""
  end
end

readonly proc HostIsNT () is
  return equal ($OS, "Windows_NT")
end

m3gdb_ResponseFileCounter = "0"

readonly proc m3gdb_Run (x) is
    >> "_m3.log" in
      write(x & CR)
    end
    if HostIsNT ()
        local readonly Temp = "_m3sh" & m3gdb_ResponseFileCounter & ".sh"
        > Temp in
            write(x)
        end
        %
        % Quake can't do math, or generate uniquely named temp files.
        % Uniqueness is not critical here, it is merely useful
        % while debugging to go back and see the temp files.
        %
        m3gdb_ResponseFileCounter =
            { "0":"1","1":"2","2":"3","3":"4","4":"5",
              "5":"6","6":"7","7":"8","8":"9","9":"0", }{m3gdb_ResponseFileCounter}

        write("sh ./" & Temp & " (" & x & ")" & CR)
        exec("sh ./" & Temp)
    else
        exec(x)
    end
end


%
% star means default
% These are meant to come from the config file, but it can omit them
%
if not defined("GNU_CC")     GNU_CC     = "*"    end
if not defined("GNU_CFLAGS") GNU_CFLAGS = "*"    end
if not defined("GNU_MAKE")   GNU_MAKE   = "make" end


M3GDB_CFLAGS="-g"

% check for overrides, otherwise use the defaults from the configuration file
if not defined ("M3GDB_HOST")    M3GDB_HOST   = TARGET     end
if not defined ("M3GDB_CC")      M3GDB_CC     = GNU_CC     end
if not defined ("M3GDB_CFLAGS")  M3GDB_CFLAGS = GNU_CFLAGS end
if not defined ("M3GDB_MAKE")    M3GDB_MAKE   = GNU_MAKE   end
if not defined ("M3GDB_CONFIG")  M3GDB_CONFIG = get_config (M3GDB_HOST) end

% check for non-default flags
local env = get_overrides ("CC", M3GDB_CC) & " " & get_overrides ("CFLAGS", M3GDB_CFLAGS)
env = env & " MAKEINFO=echo"
M3GDB_CONFIG = M3GDB_CONFIG & " " & env
M3GDB_MAKE = M3GDB_MAKE & " " & env

if not defined ("quick")
  % configure the sources
  CONFIG_PLATFORM = GNU_platform (M3GDB_HOST)
  m3gdb_Run([M3GDB_CONFIG, "../gdb/configure", CONFIG_PLATFORM])
end

% compile
if (equal(M3GDB_HOST, "SOLgnu"))
  M3GDB_MAKE = M3GDB_MAKE & " LDFLAGS=-lintl"
end
m3gdb_Run([M3GDB_MAKE])

EXE = Platform_ExeExtension (M3GDB_HOST)

% build the exportable link and man page and export them
link_file ("gdb/gdb" & EXE, "m3gdb" & EXE)
BindExport ("m3gdb" & EXE)
ManPage ("m3gdb","1")

%import("m3doc")
%HtmlFile("index")

%OtherPackage("m3gdb")
