#
# $Id$
#
# Part of Equinox Desktop Environment (EDE).
# Copyright (c) 2000-2007 EDE Authors.
#
# This program is licensed under terms of the 
# GNU General Public License version 2 or newer.
# See COPYING for details.

SUFOBJ_SHARED = ".lo" ;
SUFLIB_SHARED = ".so" ;
SUFLA_SHARED  = ".la" ;

# StaticLibrary [lib-name] : [source-files] ;
rule StaticLibrary
{
	Library $(<) : $(>) ;
}

rule SharedLibraryFromObjects
{
	local _i _l _l_so _l_la _s ;

	# Add grist to file names
	_s = [ FGristFiles $(3) ] ;
	_l = $(1:S=$(SUFLIB_SHARED).$(2)) ;

	# for sonames; it accept only libname.so
	_l_so = $(1:S=$(SUFLIB_SHARED)) ;
	_l_so = $(_l_so:BSR=) ;

	# libname.la file
	_l_la = $(_l_so:S=$(SUFLA_SHARED)) ;

    # library depends on its member objects
	if $(KEEPOBJS) {
		LocalDepends obj : $(_s) ;
	} else {
		LocalDepends lib : $(_l) ;
	}

	# Set LOCATE for the library and its contents.  The bound
	# value shows up as $(NEEDLIBS) on the Link actions.
	# For compatibility, we only do this if the library doesn't
	# already have a path.

	if ! $(_l:D) {
		MakeLocate $(_l) : $(LOCATE_TARGET) ;
	}

	# we never scan shared libraries for member objects
	LocalDepends $(_l) : $(_s) ;

	# clean library
	LocalClean clean : $(_l) ;

	if $(OS) = "SOLARIS" {
		# solaris have other options for soname creation
		LINKFLAGS on $(_l) = -h $(_l_so) -G [ on $(_l) return $(LINKFLAGS) ] ;
	} else {
		# let linker knows we are making shared library by adding -shared and -Wl,-soname libname.so flags
		LINKFLAGS on $(_l) = -Wl,-soname,$(_l_so) -shared [ on $(_l) return $(LINKFLAGS) ] ;
	}

	# make it
	Link $(_l) : $(_s) ;

	# make a link
	SymLink $(_l_so) : $(_l) ;

	# let link be created in directory where libname.so.X.X.X exists
	MakeLocate $(_l_so) : $(_l:D) ;
	LocalDepends lib : $(_l_so) ;

	# generate libname.la file
	DLNAME1 on $(_l_la) = $(_l_so) ;
	DLNAME2 on $(_l_la) = $(_l:BSR=) ;
	GenLa $(_l_la) ;
	LocalClean clean : $(_l_la) ;
	LocalClean clean : $(_l_so) ;

	# let libname.la be created in directory where libname.so.X.X.X exists
	MakeLocate $(_l_la) : $(_l:D) ;
	LocalDepends lib : $(_l_la) ;
}

rule SharedObjects
{
	# temporary replace SUFOBJ since Objects rule use it
	local SUFOBJ = $(SUFOBJ_SHARED) ;

	ObjectCcFlags $(<) : -fPIC ;
	ObjectC++Flags $(<) : -fPIC ;

	# call the normal Objects rule
	Objects $(<) ;
}

# SharedLibrary [library] : [version] : [source] ;
# Creates shared library ala libtool
rule SharedLibrary
{
	if ! $(UNIX)  {
		Exit "Don't know how to build shared libraries on this platform" ;
	}

	SharedLibraryFromObjects $(1) : $(2) : $(3:S=$(SUFOBJ_SHARED)) ;
	SharedObjects $(3) ;
}

# .la files are generated by libtool and we emulate that too
# NOTE: jam will discard comments that should be written to the file
# so instead plain '#' is used '@@#' and later is replaced with sed
actions GenLa
{
	today=`date`

	cat > "$(<).tmp" <<EOF
@@# $(<:D=) - a libtool library file
@@# Generated by 'EDE jam build' on: $today

@@# The name that we can dlopen(3).
dlname='$(DLNAME1)'

@@# Names of this library.
library_names='$(DLNAME2) $(DLNAME1)'

@@# The name of the static archive.
old_library=''

@@# Libraries that this one depends upon.
dependency_libs=''

@@# Is this already installed library.
installed=yes

@@# Files to dlopen/dlpreopen.
dlopen=''
dlpreopen=''

@@# Directory that this library needs to be installed in:
libdir='$(libdir)'
EOF

	cat "$(<).tmp" | $(SED) 's/^@@#/#/g' > "$(<)"
	$(RM) "$(<).tmp"
}