% Copyright 1996-2000 Critical Mass, Inc. All rights reserved.
% See file COPYRIGHT-CMASS for details.
%
% Standard configuration file for Alpha/AXP running OSF/1.
%

%-------------------------------------------------- compilation environment ---

readonly TARGET       = "ALPHA_OSF"
readonly TARGET_ARCH  = "ALPHA64"
readonly TARGET_OS    = "OSF"
readonly WORD_SIZE    = "64BITS"
readonly TARGET_ENDIAN = "LITTLE" % { "BIG" OR "LITTLE" }
readonly GNU_PLATFORM = "alpha-dec-osf5.1"

%------------------------------------------------ external system libraries ---
% SYSTEM_LIBS provides a mapping from Modula-3 names for the common
% external libraries to site-dependent information about how they
% are accessed.  If SYSTEM_LIBS{x} is defined it should be a list
% of linker arguments that are used when linking against that library.
% If SYSTEM_LIBS{x} is not defined, the Modula-3 system will assume
% that the library is not available.

SYSTEM_LIBS = {
  "LIBC"        : [ "-ldnet_stub", "-lm", "-lrt" ],
  "LEX-YACC"    : [  "-ll" ],
  %"FLEX-BISON" : [ "L/usr/local/lib", "-lfl" ],
  %"POSTGRES95" : [  "L/usr/local/postgres95/lib", "-lpq" ],
  %"ODBC"       : [ "L/usr/local/lib", "-lodbc" ],
  %"OPENGL"     : [ "-lGLU", "-lGL", "-lXext" ],
  %"DECPEX"     : [ "-ldecpex" ],
  "MOTIF"       : [ "-lXm" ],
  "X11"         : [  "-lXaw", "-lXmu", "-lXext", "-lXt", "-lX11" ],
  "TCP"         : [ ]
}

% SYSTEM_LIBORDER defines the order in which SYSTEM_LIBS should be
% scanned by the linker.

SYSTEM_LIBORDER = [ "OPENGL", "DECPEX", "MOTIF", "X11", "TCP", "ODBC",
                    "POSTGRES95", "FLEX-BISON", "LEX-YACC", "LIBC" ]

%---------------------------------------------------------- system programs ---

proc configure_c_compiler() is
  if defined("SYSTEM_CC")
    return
  end
  SYSTEM_CC = "/usr/bin/cc -O3 -g3 -pthread -readonly_strings"
            & " -ieee_with_no_inexact -error_unresolved "
  if IsTargetOSF1v4()
    SYSTEM_CC = SYSTEM_CC & "-Wl,-B,symbolic"
  else
    SYSTEM_CC = SYSTEM_CC & "-B symbolic"
  end
end
readonly SYSTEM_AR  = "/usr/bin/ar"
readonly SYSTEM_ASM = "/usr/bin/as -nocpp -g -oldas -c -O0" % same as gcc

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

%M3_USE_STACK_WALKER = TRUE

m3back_pic = "" % -fPIC isn't needed.
m3back_m64 = "" % -m64 not allowed
m3back_mieee = "-mieee"
m3back_optimize = ""

%--------------------------------------------------------------- C compiler ---
% "compile_c" is called to compile C source files.

proc compile_c (source, object, options, optimize, debug) is
  configure_c_compiler()
  return try_exec ("@" & SYSTEM_CC, options, "-c", source)
end

%---------------------------------------------------------------- assembler ---
% "assemble" is called to assemble .s or .asm files.

proc assemble (source, object) is
  local ret_code = 0
  ret_code = try_exec ("@" & SYSTEM_ASM, source, "-o", object)
  if not equal (ret_code, 0) return ret_code end
  ret_code = try_exec (m3fixup, "-o", object, source)
  return ret_code
end

%--------------------------------------------------------- library creation ---
% "make_lib" is called to combine a collection of object modules into
% a library.

proc make_lib (lib, options, objects, imported_libs, shared) is
  local ret_code = 0
  local lib_a    = format ("lib%s.a", lib)
  local lib_so   = format ("lib%s.so", lib)

  % first, build the non-shared library
  ret_code = try_exec ("@" & SYSTEM_AR, "crusl", lib_a, objects)
  if not equal (ret_code, 0) return ret_code end

  if shared
    % then, build the shared library
    configure_c_compiler()
    ret_code = try_exec ("@" & SYSTEM_CC, "-shared -o",
                         lib_so, "-all", lib_a, "-none", imported_libs)
    if not equal (ret_code, 0) return ret_code end
  end
  return skip_lib(lib, shared)
end

%-------------------------------------------------------------------
% "skip_lib" is called when the compiler decides it doesn't need to
% call "make_lib", but it wants to discover the names of the derived
% files that should be deleted or shipped.

proc skip_lib (lib, shared) is
    local lib_a	  = "lib" & lib & ".a"
    local lib_so  = "lib" & lib & ".so"

    % Tell clean what to delete.
    deriveds("", [lib_a, lib_so])

    if shared
	% make sure the shared library stuff gets installed properly
	LibdExport(lib_so)
	install_link_to_derived(lib_a, LIB_INSTALL)
	install_derived_link("../../../lib/" & lib_so, lib_so)
    else
        DeleteFiles([lib_so])
    end

    return 0
end

%------------------------------------------------------------------- linker ---
% "m3_link" is called to produce a final executable.

proc m3_link (prog, options, objects, imported_libs, shared) is
  configure_c_compiler()
  local linker = [ "@" & SYSTEM_CC ]
  local args = [ "-o", prog, options, objects, imported_libs ]  
  if equal($M3_PORTABLE_RUN_PATH, "")
    args += [ "-rpath", LIB_USE ]
  end
  return try_exec (linker, args)
end

%-------------------------------------------------------------------
% "skip_link" is called when the compiler decides it doesn't need to
% call "m3_link", but it wants to discover the names of the derived
% files that should be deleted or shipped.

proc skip_link (prog, shared) is
  return 0
end

%------------------------------------------------------------ misc. options ---
% Note, most of these options can be set from the command line.  Otherwise,
% they can be set "permanently" here in the config file or in as needed
% in user's m3makefiles.

M3_SHARED_LIB_ARG = ""

include("Unix.common")

%------------------------------------------------------------------------------

readonly m3fixup = "@" & BIN_USE & "/mips-tfile"
% The default assembler isn't smart enough to include debugging
% information from the assembler source file, so we run mips-tfile
% after assembly to reinsert the debugging info in the object file.
