% Copyright 1996-2000 Critical Mass, Inc. All rights reserved.
% See file COPYRIGHT-CMASS for details.
%

readonly TARGET       = "PPC_DARWIN"
readonly WORD_SIZE    = "32BITS"
readonly GNU_PLATFORM = "powerpc-apple-darwin6.3"


%
% fPIC is not usually needed here.
% It is the default for Apple gcc and left
% here in case user is using a self-built FSF gcc.
%
readonly SYSTEM_CC  = "gcc -fPIC -m32"
readonly SYSTEM_LIBTOOL = "libtool"
readonly SYSTEM_ASM = "as"


%
% fPIC really is needed here, despite man gcc saying it is the default.
% This is because man gcc is about Apple's gcc but m3cg is
% built from FSF source.
%
readonly m3back_flags = "-fPIC -gstabs+ -quiet -m32"


%------------------------------------------------ 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"       : [ ],
  "LEX-YACC"   : [ "-ll" ],
  "FLEX-BISON" : [ "-lfl" ],
% not present by default, but this is the default install location
% "POSTGRES95" : [ "-L/usr/local/pgsql/lib", "-lpq" ],
  "OPENGL"     : [ "-lGLU", "-lGL", "-lXext" ],
  "ODBC"       : [ "-liodbc", ],
% "MOTIF"      : [ "-lXm" ],
  "X11"        : [
  	"-L/usr/X11R6/lib",
    "-lXaw",
    "-lXmu",
    "-lXext",
    "-lXt",
    "-lSM",
    "-lICE",
    "-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"
]

%--------------------------------------------------------- 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 vmaj     = "5"
  local vmin     = "2"
  local version  = vmaj & "." & vmin
  local ret_code = 0
  local lib_a    = format ("lib%s.a", lib)
  local lib_so   = format ("lib%s.dylib", lib)
  local lib_sox  = format ("lib%s.%s.dylib", lib, vmaj)
  local lib_soxx = format ("lib%s.%s.dylib", lib, version)
  local lib_pn   = LIB_INSTALL & "/" & lib_soxx

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

  if shared
    % build the shared library
    ret_code = try_exec (
        "@" & SYSTEM_CC, "-dynamiclib",
        "-multiply_defined suppress",
        "-twolevel_namespace",
        "-compatibility_version", vmaj,
        "-current_version", version,
        "-install_name", lib_pn,
        "-lgcc",
        "-o", lib_soxx,
        objects,
        imported_libs
        )
    if not equal (ret_code, 0)
      delete_file (lib_a)
      return ret_code
    end

    % create the version aliases
    link_file(lib_soxx, lib_sox)
    link_file(lib_sox, lib_so)

    % finally, make sure the shared library stuff gets installed properly
    install_derived (lib_soxx)
    install_derived_link (lib_soxx, lib_sox)
    install_derived_link (lib_sox, lib_so)
    install_link_to_derived (lib_soxx, LIB_INSTALL)
    install_link_to_derived (lib_sox, LIB_INSTALL)
    install_link_to_derived (lib_so, LIB_INSTALL)
  else
    delete_file (lib_so)
    delete_file (lib_sox)
    delete_file (lib_soxx)
  end

  return 0
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 vmaj     = "5"
  local vmin     = "2"
  local version  = vmaj & "." & vmin
  local ret_code = 0
  local lib_a    = format ("lib%s.a", lib)
  local lib_so   = format ("lib%s.dylib", lib)
  local lib_sox  = format ("lib%s.%s.dylib", lib, vmaj)
  local lib_soxx = format ("lib%s.%s.dylib", lib, version)

  if shared
    % make sure the shared library stuff gets installed properly
    install_derived (lib_soxx)
    install_derived (lib_sox)
    install_derived (lib_so)
    install_link_to_derived (lib_soxx, LIB_INSTALL)
    install_link_to_derived (lib_sox, LIB_INSTALL)
    install_link_to_derived (lib_so, LIB_INSTALL)
  else
    delete_file (lib_so)
    delete_file (lib_sox)
    delete_file (lib_soxx)
  end

  return 0
end

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

proc m3_link (prog, options, objects, imported_libs, shared) is
  local args =
  [
    "-o",
    prog,
    options,
    objects,
    imported_libs,
    "-multiply_defined suppress",
    "-bind_at_load",
    "-shared-libgcc",
  ]
  if M3_PROFILING args += "-pg" end
  return try_exec ("@" & SYSTEM_CC, args)
end

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

include ("Unix.common")

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

if not equal(try_exec("@grep -F M3_SPLIT_LIBNAMES_IF_SHARED " & BIN_USE & "/cm3 > /dev/null"), 0)
%
% This is critical on MacOSX to achieve dynamic linking.
% Specifying /foo/bar.a statically links to bar.a.
% Specifying -L/foo -lbar probes first for /foo/bar.dylib
% and then for /foo/bar.a, achieving dynamic linking if possible.
% See "man ld".
%
%error("new version")
M3_SPLIT_LIBNAMES = TRUE
% --- split shared library names and pass -L/-l arguments to the linker
else
%
% On older releases, the above breaks building standalone.
%
%error("old version")
M3_SPLIT_LIBNAMES = FALSE
M3_SPLIT_LIBNAMES_IF_SHARED = TRUE  	 
% --- split library names and pass -L/-l arguments to the linker if linking 	 
%     dynamically, pass full library names else 	 
%     (assumes M3_SPLIT_LIBNAMES = FALSE)
end

M3_SHARED_LIB_ARG = ""
