%
% This template has not been used lately and NEEDS UPDATING
%
%-------------------------------------------------- compilation environment ---

readonly TARGET = "AIX386"
readonly DEFAULT_BUILD_DIR = TARGET
readonly OS_TYPE           = "POSIX"
readonly WORD_SIZE         = "32BITS"
readonly GNU_PLATFORM      = "i486-ibm-aix"

% naming convention of the host
readonly NAMING_CONVENTIONS = "0"
%                                        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 exist
% 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 = "/usr/local/"
%-- handy for installations that keep all M3 stuff together

BIN_INSTALL   = INSTALL_ROOT & "bin"                % executables
LIB_INSTALL   = INSTALL_ROOT & "lib/m3/" & TARGET   % libraries
DOC_INSTALL   = INSTALL_ROOT & "lib/m3/doc"         % documents
PKG_INSTALL   = INSTALL_ROOT & "lib/m3/pkg"         % packages
EMACS_INSTALL = INSTALL_ROOT & "lib/elisp"          % emacs lisp code
MAN_INSTALL   = INSTALL_ROOT & "man"                % man pages
HTML_INSTALL  = INSTALL_ROOT & "lib/m3/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.
BIN_USE   = BIN_INSTALL
LIB_USE   = LIB_INSTALL
PKG_USE   = PKG_INSTALL

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_DECPEX" is called from the PEX package.
% "import_OpenGL" is called from the opengl package.
% "import_TCP" is called from the tcp package.

readonly proc import_X11() is
  import_lib("Xaw",  "/usr/local/lib")
  import_lib("Xmu",  "/usr/local/lib")
  import_lib("Xext", "/usr/local/lib")
  import_lib("Xt",   "/usr/local/lib")
  import_lib("X11",  "/usr/local/lib")
end

readonly proc import_Motif() is
  import_lib("Xm",   "/usr/local/lib")
end

readonly proc import_DECPEX() is
  % DEC PEX differs from MIT PEX, and is only supported on Digital machines.  
end

readonly proc import_OpenGL() is
  % import_lib (GLU,  "/usr/lib")
  % import_lib (GL,   "/usr/lib")
  % import_lib (Xext, "/usr/lib")
end

readonly proc import_TCP() is
end

readonly PLATFORM_SUPPORTS_X      = "TRUE"
readonly PLATFORM_SUPPORTS_MOTIF  = "TRUE"
readonly PLATFORM_SUPPORTS_DECPEX = ""
readonly PLATFORM_SUPPORTS_OPENGL = ""

% Does your X11 server have the shared memory extension?
readonly X11_WITH_SHARED_MEM = ""

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

%--------------------------------------------------------------- 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, [ "/bin/cc", "-w", "-D_BSD", "-D_NO_PROTO" ])

% "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]
  foreach i in includes
    args += "-I" & i
  end
  if object    args += [ "-o", object ] end
  if optimize  args += "-O" end
  if debug     args += "-g"  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, [ "/bin/cc" ])

% "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 = [ "-o", prog, objects, "-lm" ]
  foreach i in imported_libs
    if not empty(i[0]) args += "-L" & i[0] end
    args += "-l" & i[1]
  end

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

  return try_exec (VERBOSE & LINK, args)
end

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

% program to build library archives
% You can override this definition in your m3makefile by defining MAKELIB 
% there.
MAKELIB = _ifdef ("MAKELIB", MAKELIB, [ "/bin/ar", "cru" ])

% 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" ])

% "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)
    if not equal(ret, 0) return ret end
    ret = try_exec (VERBOSE & RANLIB, lib_a)
  end

% if shared
%   local lib_so   = format ("lib%s.so", lib)
%   local lib_sox  = format ("lib%s.so.3", lib)
%   local lib_soxx = format ("lib%s.so.3.5", lib)
%   local args = [ "-shared", "-soname", lib_so, "-o", lib_soxx, 
%	"/usr/lib/crti.o", "/usr/lib/crtbeginS.o", 
%	objects, "/usr/lib/crtendS.o", "/usr/lib/crtn.o" ]
%
%
%   if defined ("_all")
%     ret = try_exec (VERBOSE & MAKESHLIB, args)
%     if not equal(ret, 0) return ret end
%     %exec ("gcc -shared -Wl,-soname", "-Wl," & lib_so,
%     %        "-o", lib_soxx, objects)
%     link_file(lib_soxx, lib_sox)
%     link_file(lib_soxx, lib_so)
%     install_derived (lib_soxx)
%     install_derived (lib_sox)
%     install_derived (lib_so)
%     install_link_to_derived (lib_sox, LIB_INSTALL)
%     install_link_to_derived (lib_so, LIB_INSTALL)
%   end
%   deriveds (lib_soxx, [""])
%   deriveds (lib_sox, [""])
%   deriveds (lib_so, [""])
% end
  return ret
end

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

% assembler
% You can override this definition in your m3makefile by defining ASM
% there.
ASM = _ifdef ("ASM", ASM, [ "/bin/as" ])

% "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.
BACKEND = _ifdef ("BACKEND", BACKEND, [  LIB_USE & "/m3cgc1", "-quiet" ])

% For platforms without an integrated backend, "m3_backend" is called to
% translate Modula-3 intermediate code to object code.
%  source: the name of the source file to compile
%  object: the name of the object file to create
%  optimize: if true, do optimization
%  debug: if true, generate debug information
%  shared: if true, generate an object that can be used to make a shlib
proc m3_backend (source, object, optimize, debug, shared) is
  local args = [ "-o", object, source ]
  
  if optimize args += "-O" end
  if debug args += "-g" end
  % if shared args += "-fPIC" end

  return try_exec(VERBOSE & BACKEND, args)
end

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

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

%------------------------------------------------------------- GNU variants ---
% The two large pieces of GNU software used by the Modula-3 system
% gcc(=m3cc) and gdb(=m3gdb) often require slightly different C compilers
% or flags.  They are specified here.  Note that they may be overridden
% from the m3build command line.
%
% To use the GNU defaults for CC and CFLAGS, specify "*".
%

GNU_CC     = _ifdef ("GNU_CC",     GNU_CC,     "*")
GNU_CFLAGS = _ifdef ("GNU_CFLAGS", GNU_CFLAGS, "*")
GNU_MAKE   = _ifdef ("GNU_MAKE",   GNU_MAKE,   "make")

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

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

M3_COVERAGE_LIB = LIB_USE & "/report_coverage.o"
% --- 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 = "STATIC"
% "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 = "COMPILER"
% "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 ("@cp -p", src, dest)
  % exec ("@/usr/ucb/install -c -m", mode, src, dest)

  % Note: it seems that the standard install is not compatible with our
  % makefiles, but that the install that comes with Andrews or the BSD
  % install script is.
end
