#============================================================================
# Jam configuration and actions for Unix (GNU/Linux, BSD, Darwin, etc.)
# Copyright (C)2003 by Matze Braun <matzebraun@users.sourceforge.net>
#
#    This library is free software; you can redistribute it and/or modify it
#    under the terms of the GNU Library General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or (at your
#    option) any later version.
#
#    This library is distributed in the hope that it will be useful, but
#    WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
#    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
#    License for more details.
#
#    You should have received a copy of the GNU Library General Public License
#    along with this library; if not, write to the Free Software Foundation,
#    Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
#============================================================================
SHELL ?= "/bin/sh" ;

# Only use nasm on x86 for now...
NASM.FLAGS += -f elf ;

PLUGIN.CFLAGS += $(COMPILER.C++FLAGS.PIC) ;
LIBRARY.CFLAGS += $(COMPILER.C++FLAGS.PIC) ;

# The BFD tag name under which to embed meta-information into a plugin module.
# This much match the name expected by csGetPluginMetadata() (bfdplugins.cpp).
SECTION_TAG_NAME ?= .crystalspace ;

#----------------------------------------------------------------------------
# platform specific rules

##  ConstructApplicationTarget target : options
##    Constructs the application target name (ie. foo.exe for foo)
rule ConstructApplicationTarget
{
  return $(<) ;
}
rule ConstructStaticLibraryTarget
{
  return lib$(<)$(SUFLIB) ;
}
rule ConstructSharedLibraryTarget
{
  return lib$(<).so.$(PACKAGE_VERSION) ; 
}
rule ConstructSharedLibraryLinkLib
{
  return lib$(<).so.$(PACKAGE_VERSION) ; 
}
rule ConstructPluginTarget
{
  return $(<).so ;
}

# SystemLinkApplication target : objects : options
#  do system specific actions needed for linking the application and construct
#  correct clean targets.
rule SystemLinkApplication
{
  local target = $($(<)_TARGET) ;
  
  Depends $(target) : $(>) ;
  LinkApplication $(target) : $(>) ;
  # setup clean rules
  Clean clean : $(target) ;
  Clean $(<)clean : $(target) ;
}

rule SystemInstallApplication
{
  Depends install_bin :
    [ DoInstall $(<) : $(bindir) $(2) : $(INSTALL_PROGRAM) ] ;
}

rule SystemInstallPlugin
{
  Depends install_plugin : [ DoInstall $(<) : $(plugindir) $(2) :
      $(INSTALL_PROGRAM) ] ;
}

# Put the meta data into the object file headers
rule ObjTagMetaData
{
  Depends $(<) : $(>) ;
}
actions ObjTagMetaData
{
  $(CMD.OBJCOPY) --add-section $(SECTION_TAG_NAME)=$(>) $(<)
}

# SystemLinkPlugin target : objects : options
#  do system specific actions needed for linking the plugin and construct
#  correct clean targets.
rule SystemLinkPlugin
{
  local target = $($(<)_TARGET) ;
  
  Depends $(target) : $(>) ;
  LinkPlugin $(target) : $(>) ;
  if $(EMBED_META) = "yes" && $(OBJCOPY.AVAILABLE) = "yes"
  {
    ObjTagMetaData $(target) : $($(<)_METAFILE) ;
  }
  else
  {
    PluginMetaData $(<) : $($(<)_METAFILE) : $(3) ;
  }
  # setup clean rules
  Clean clean : $(target) ;
  Clean $(<)clean : $(target) ;
}

rule SystemLinkSharedLibrary
{
  LFlags $(<) : $(LINKLIBS) ;
  Depends $(<) : $(>) ;
  if $(LINKER.RESPONSEFILES) = "yes"
  {
    local response = $(<).resp ;
    MakeLocate $(response) : $(LOCATE.OBJECTS)/libs ;
    ResponseFile $(response) : $(>) ;
    Depends $(response) : $(>) ;
    Depends $(<) : $(response) ;
    LinkSharedLibraryRsp $(<) : $(response) ;
  }
  else
  {
    LinkSharedLibrary $(<) : $(>) ;
  }

  Clean clean : $(<) ;
  Clean $(<)clean : $(<) ;
}

actions LinkApplication bind NEEDLIBS bind EXTRAOBJECTS
{
  $(CMD.LINK) -o $(<) $(>) $(EXTRAOBJECTS) $(NEEDLIBS) $(LINKLIBS)
}

if $(PLUGIN.LFLAGS.USE_SONAME) = "yes"
{
  actions LinkPlugin bind NEEDLIBS bind EXTRAOBJECTS
  {
    $(CMD.LINK) -o $(<) $(>) $(EXTRAOBJECTS) $(NEEDLIBS) $(LINKLIBS) \
      -Wl,-soname,$(<:BS)
  }
}
else
{
  actions LinkPlugin bind NEEDLIBS bind EXTRAOBJECTS
  {
    $(CMD.LINK) -o $(<) $(>) $(EXTRAOBJECTS) $(NEEDLIBS) $(LINKLIBS)
  }
}

actions LinkSharedLibrary bind NEEDLIBS bind EXTRAOBJECTS
{
  $(CMD.LINK) -shared -o $(<) $(>) $(EXTRAOBJECTS) $(NEEDLIBS) $(LINKLIBS) \
    -Wl,-soname,$(<:BS)
}

actions LinkSharedLibraryRsp bind NEEDLIBS bind EXTRAOBJECTS
{
  $(CMD.LINK) -shared -o $(<) -Wl,@$(>) $(EXTRAOBJECTS) $(NEEDLIBS) $(LINKLIBS) \
    -Wl,-soname,$(<:BS)
}
