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

%
% user can say cm3 -DM3GDB_HOST=amd64-freebsd or
% cm3 -DM3GDB_HOST=i686-pc-linux-gnu, for example.
%

if not ({"AMD64_DARWIN", "ARM_DARWIN", "I386_DARWIN", "PPC_DARWIN",
         "PPC64_DARWIN"} contains TARGET or equal(OS_TYPE, "WIN32"))

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

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

m3gdb_ResponseFileCounter = 0

include ("../../m3cc/src/Math.quake")

readonly proc m3gdb_Run (x) is
    if not HostIsNT()
        write(x & "\n")
        exec(x)
        return
    end
    InitMath()
    m3gdb_ResponseFileCounter = Add(m3gdb_ResponseFileCounter, 1)
    local ShLeaf = "_m3" & m3gdb_ResponseFileCounter & ".sh"
    local Sh = ShLeaf
    write(ShLeaf & ":" & x & "\n")
    > Sh in
        write(x & "\n")
    end
    local a = try_exec("@chmod +x " & Sh)
    exec("@sh -exc " & Sh)
end

local env = ""

include ("../../m3cc/src/gnumake.common")
include ("../../m3cc/src/gnucc.common")

ConfigureGNUMake()
write("GNU_MAKE is " & GNU_MAKE & CR)
env = env & " MAKE=" & GNU_MAKE & " "

M3GDB_CFLAGS="-g"

% check for overrides, otherwise use the defaults from the configuration file
if not defined ("M3GDB_MAKE")    M3GDB_MAKE   = GNU_MAKE   end

% check for non-default flags
env = env & " MAKEINFO=:"

M3GDB_CONFIG = env
M3GDB_MAKE = M3GDB_MAKE & " " & env

% configure the sources

if not FileExists ("Makefile")
  if defined("M3GDB_HOST")
    m3gdb_Run([M3GDB_CONFIG, "../gdb/configure", M3GDB_HOST])
  else
    m3gdb_Run([M3GDB_CONFIG, "../gdb/configure"])
  end
end

m3gdb_Run([M3GDB_MAKE])

EXE = Platform_ExeExtension (HOST)

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

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

%OtherPackage("m3gdb")

end
