%
% This template has not been used lately in pm3 being replaced by NT386GNU.
% It would need UPDATING.
%
%-------------------------------------------------- compilation environment ---

readonly TARGET = "NT386"
readonly DEFAULT_BUILD_DIR = TARGET
readonly OS_TYPE = "POSIX"
readonly WORD_SIZE = "32BITS"
readonly GNU_PLATFORM = "i486--nt"

readonly NAMING_CONVENTIONS = "2"
%                                        object files       libraries
%  0=Unix                          =>  .o   .io    .mo       libXX.a
%  1=Unix with a grumpy C compiler =>  .o   _i.o   _m.o      libXX.a
%  2=Windows/NT                    =>  .obj .io    .mo       XX.lib
%

% naming convention of the target
% it is not necessary to define it if it is equal to NAMING_CONVENTIONS
% TARGET_NAMING_CONVENTIONS = "0"

%------------------------------------------------------------- export paths ---
% During the installation, destination directories that do not exists 
% will be created. You need the necessary permissions to do so; otherwise,
% the installation will fail, but can be restarted after you have 
% fixed the permissions.

INSTALL_ROOT = "c:\\modula-3\\"
%-- handy for installations that keep all M3 stuff together

% For Windows (NT and 95), the actual installation still gest done 
% from Unix and thus we use Unix names...
BIN_INSTALL   = INSTALL_ROOT & "bin"                % executables
LIB_INSTALL   = INSTALL_ROOT & "lib"                % libraries
PKG_INSTALL   = INSTALL_ROOT & "pkg"                % packages
DOC_INSTALL   = INSTALL_ROOT & "doc"                % documents
EMACS_INSTALL = INSTALL_ROOT & "elisp"              % emacs lisp code
MAN_INSTALL   = INSTALL_ROOT & "man"                % man pages
HTML_INSTALL  = INSTALL_ROOT & "www"                % public hypertext

% The manual pages normally go in subdirectories man{1,...8} of
% the MAN_INSTALL directory.  If you prefer to have them all in
% a single section, define MAN_SECTION to be that section's name.
% MAN_SECTION = "l"

% On some systems (e.g. AFS) you must install public files in a different
% place from where you use them.  If that is the case for your system,
% specify the "use" location here, otherwise leave them alone.
%
% For Windows (NT and 95) we use the stuff from the mounted unix
% file system.
USE_ROOT = "\\\\unix\\proj\\"

BIN_USE   = BIN_INSTALL       % executables
LIB_USE   = LIB_INSTALL       % libraries
PKG_USE   = PKG_INSTALL       % packages

readonly INSTALL_IMPLS = "TRUE"
% "TRUE"
%    => save all source files during the install
%    => makes debugging easier and browsing more fruitful
% "" (i.e. FALSE)
%    => save only the exported interfaces and templates 
%    => makes the installed system slightly smaller.

readonly NEED_OBJECTS = ""
% "TRUE"
%    => accumulate a list of derived objects in COMPILE_OBJECTS
%    => for building shared libraries in the library_hooks function below
% ""
%    => don't bother

%---------------------------------------------------------------------- X11 ---
% If you have X11 installed and would like the X11 binding interfaces
% to be built, define the procedure "import_X11" to import the libraries
% that are needed.  Otherwise, define "import_X11" to be an empty procedure.
%
% If you use the MIT server with DECnet support, you need X11 and dnet,
% otherwise X11 should be enough.
% 
% "import_X11" is called from the X11 package.  "import_Motif" is
% called from the motif package.  "import_PEX" is called from the PEX
% package.

proc import_X11() is
end

readonly proc import_Motif() is
end

readonly proc import_PEX() is
end

%-------------------------------------------------------------------- emacs ---
% If you have emacs and want to compile ".el" files to ".elc" files,
% fill in the function below.  Otherwise, comment out or delete the
% entire function.  Note, the distributed code assumes gnuemacs version 19
% or later.

readonly proc emacs_compile (el) is
  exec ("emacs -batch -f batch-byte-compile", el)
end

%-------------------------------------------------------------------- hooks ---
% here are utilities used by the following hooks

% _ifdef(a,b,c) == if defined(a) return b else return c
readonly proc _ifdef (nm, a, b) is
  if defined (nm)
    return a
  else
    return b
  end
end

VERBOSE = "@"
% "@" ==> don't echo the commands executed by the hooks
% ""  ==> echo the commands executed by the hooks

KEEP_HOOKS_OUTPUT = ""
% "TRUE" ==> keep the files generated by the tools
% ""     ==> delete those files


%--------------------------------------------------------------- C compiler ---

% C compiler with flags for compiling a single ".c" file.
% You can override this definition in your m3makefile by defining CC there.
CC = _ifdef ("CC", CC, [ "c:\\msdev\\bin\\cl", "-D_MT", "-D_DLL", "-D_X86_", "-DWIN32", "-nologo" ])

% "m3_compile_c" is called to compile C source files.  Note that this function
% is only called if your program or library explicitly includes C source
% code.
%   source: the name of the source file
%   object: the name of the object file to produces
%   includes: a list (array) of the name of the include files (no flags, only
%             the names)
%   optimize: if true, optimization is required, otherwise not.
%   debug:  if true, debug information is required, otherwise not.
%   shared: if true, the generated executable must be usable in a shared lib.
proc m3_compile_c (source, object, includes, optimize, debug, shared) is
  local args = [ "-c", source, "-I" & LIB_USE ]
  foreach i in includes
    args += "-I" & i
  end
  if object    args += [ "-o", object ] end
  if optimize  args += "-O2" end
  if debug     args += "-Od -Zi"  end
  % if shared    args += "-fPIC" end
  return try_exec (VERBOSE & CC, args)
end


%------------------------------------------------------------------- linker ---

% C compiler with flags for linking
% You can override this definition in your m3makefile by defining LINK there.
LINK = _ifdef ("LINK", LINK, [ "c:\\msdev\\bin\\link", "-debug:mapped,partial", "-debugtype:cv", "-pdb:none", "-align:0x1000", "-ignore:505", "-machine:i386" ])
%---  "-pdb:none" enables the use of windbg with the resulting executable.

% "m3_link" is called to produce a final executable.
%   prog: the name of the executable.
%   objects: a list (array) of objects to link into the executable.
%   imported_libs: a list (array) of 2-element array: the first element 
%     is the path of the library to import and the second is the name of 
%     the library.  The first element may be empty.
%   debug: if true, the executable must contain debugging information.
%   shared: if true, the final executable must use shared libs
proc m3_link (prog, objects, imported_libs, debug, shared) is
  local args = [ "msvcrt.lib", "kernel32.lib", "user32.lib", "gdi32.lib", 
	"advapi32.lib", "winspool.lib", "comdlg32.lib", "netapi32.lib" ]

  args += [ "-o", prog, objects, "-lm" ]
  foreach i in imported_libs
    args += i[0] & SL & i[1]
  end

  % if not shared args = [ "-static", args ] end
  % if debug args += "-g" end

  return try_exec (VERBOSE & LINK, args, "> link.out")
  if not KEEP_HOOKS_OUTPUT delete_file("link.out") end
end

%--------------------------------------------------------- library creation ---

% program to build library archives
% You can override this definition in your m3makefile by defining MAKELIB 
% there.
MAKELIB = _ifdef ("MAKELIB", MAKELIB, [ "c:\\msdev\\bin\\lib", "-debugtype:cv", "-machine:i386" ])

% program to build shared libraries
% You can override this definition in your m3makefile by defining MAKESHLIB
% there.
MAKESHLIB = _ifdef ("MAKESHLIB", MAKESHLIB, "ld")

% program to index libraries
% You can override this definition in your m3makefile by defining RANLIB
% there.
% RANLIB = _ifdef ("RANLIB", RANLIB, [ "touch" ])
% unused on Win32

% "m3_make_lib" is called to combine a collection of object modules into
% a library.
%   lib: the name of the library
%   objects: a list (array) of the object files to include in the library
%   imported_libs: a list (array) of the imported libraries
%   static: if true, build a static library
%   shared: if true, build a shared library
proc m3_make_lib (lib, objects, imported_libs, static, shared) is
  local lib_a    = format ("lib%s.a", lib)
  local args = [ lib_a, objects, imported_libs ]
  local ret = 0

  if static
    ret = try_exec (VERBOSE & MAKELIB, args, "> makelib.out")
    if not KEEP_HOOKS_OUTPUT delete_file("makelib.out") end
    if not equal(ret, 0) return ret end
    % ret = try_exec (VERBOSE & RANLIB, lib_a)
  end

  if shared
    write("Cannot make shared libs.")
  end
  return ret
end

%---------------------------------------------------------------- assembler ---

% assembler
% You can override this definition in your m3makefile by defining ASM
% there.
ASM = _ifdef ("ASM", ASM, [ "c:\\msdev\\bin\\masm386", "/Ml", "/t", "/z" ])

% "m3_assemble" is called to assemble files.  Note that this function
% is only called if your program or library explicitly includes assembly source
% code.
%  source: the name of the source file
%  object: the name of the object file to produce
%  optimize: if true, optimization is required, otherwise not.
%  debug: if true, the object file must contain debugging information.
%  shared: if true, the object file must be suitable for being incorporated
%    into a shared library. 
proc m3_assemble (source, object, optimize, debug, shared) is
  local args = [ "-o", object, source ]

  return try_exec (VERBOSE & ASM, args)
end

%--------------------------------------------------------- Modula-3 backend ---

% the Modula-3 IL to assembly language pass
% You can override this definition in your m3makefile by defining BACKEND
% there.

M3_BACKEND_EXTERNAL = ""
% "TRUE" => call m3_backend
% ""     => use an internal backend (default)

M3_BACKEND_OUTPUT = "OBJ"
% "ASM" => the backend produces assembly code (default)
% "OBJ" => it produces object code

%-------------------------------------------------------- Modula-3 compiler ---

% Default options
m3_option("-w1")
m3_option("-why")
m3_option("-g")

m3front_option("-unfold_nested_procs")

M3_COVERAGE_LIB = LIB_USE & SL & "report_coverage.obj"
% --- library linked in programs compiled with "-Z" coverage option

% M3_STANDALONE = TRUE
% --- build a standalone executable using static libraries, otherwise use
% --- shared libraries

M3_GENERATE_LIB = "BOTH"
% "BOTH"   => generate both static and shared libraries
% "STATIC" => generate only a static library
% "SHARED" => generate only a shared library

M3_HAS_LOADER = TRUE
% --- generate a loader info file with objects, libraries and timestamps

M3_M3MAIN = "BACKEND"
% "COMPILER" => generates _m3main.o using a C compiler
% "BACKEND"  => generates _m3main.o using the internal backend

proc build_standalone() is
   % --- reset the linker to avoid shared libraries.
   M3_STANDALONE = "TRUE"
end

proc build_shared() is
   % --- reset the linker to use shared libraries.
   M3_STANDALONE = ""
end

%------------------------------------------------------------- installation ---
% "install_file" is called during an "m3build install" for
% each file that neededs to be externally exported.

readonly proc install_file (src, dest, mode) is
  Note_install (src, dest)
  exec ("@COPY ", src, dest)
  %  exec("@install -c -m", mode, src, dest)
end
