#
# ***** BEGIN LICENSE BLOCK *****
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
#
# The contents of this file are subject to the Mozilla Public License Version
# 1.1 (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
# for the specific language governing rights and limitations under the
# License.
#
# The Original Code is mozilla.org code.
#
# The Initial Developer of the Original Code is
# Netscape Communications Corporation.
# Portions created by the Initial Developer are Copyright (C) 1998
# the Initial Developer. All Rights Reserved.
#
# Contributor(s):
#
# Alternatively, the contents of this file may be used under the terms of
# either the GNU General Public License Version 2 or later (the "GPL"), or
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
# in which case the provisions of the GPL or the LGPL are applicable instead
# of those above. If you wish to allow use of your version of this file only
# under the terms of either the GPL or the LGPL, and not to allow others to
# use your version of this file under the terms of the MPL, indicate your
# decision by deleting the provisions above and replace them with the notice
# and other provisions required by the GPL or the LGPL. If you do not delete
# the provisions above, a recipient may use your version of this file under
# the terms of any one of the MPL, the GPL or the LGPL.
#
# ***** END LICENSE BLOCK *****

DEPTH		= ../..
topsrcdir	= @top_srcdir@
srcdir		= @srcdir@
VPATH		= @srcdir@

include $(DEPTH)/config/autoconf.mk

REQUIRES	= xpcom \
		  $(NULL)

LOCAL_INCLUDES	= -I$(srcdir)

include $(topsrcdir)/config/config.mk

_FINAL_COMPONENT_NAMES := $(shell cat $(FINAL_LINK_COMP_NAMES))
_FINAL_COMPONENT_LIBS := $(shell cat $(FINAL_LINK_COMPS))
_FINAL_LINK_LIBS := $(shell cat $(FINAL_LINK_LIBS))
_META_COMPONENT_NAMES := _FINAL_COMPONENT_NAMES
_META_COMPONENT_LIBS := _FINAL_COMPONENT_LIBS
_META_DEP_LIBS := _FINAL_LINK_LIBS

LIBRARY_NAME = mozcomps
CPPSRCS = nsMetaModule_mozcomps.cpp

include $(topsrcdir)/config/static-config.mk

EXTRA_DSO_LIBS	= $(STATIC_EXTRA_DSO_LIBS)

# See explanation below.
ifeq ($(OS_ARCH),Darwin)
EXTRA_DEPS = force_data_segment.o exported_symbols
endif

FORCE_SHARED_LIB = 1

include $(topsrcdir)/config/rules.mk
include $(topsrcdir)/config/static-rules.mk

# Force XPCOM_TRANSLATE_NSGM_ENTRY_POINT to be defined so the macros
# in nsIGenericFactory.h will come out in the wash.
DEFINES += -DMETA_MODULE=\"$(LIBRARY_NAME)\" -DXPCOM_TRANSLATE_NSGM_ENTRY_POINT

# We don't want to link mozcomps against NSS if not necessary
ifneq (,$(findstring crypto,$(MOZ_META_COMPONENTS)))
STATIC_EXTRA_LIBS := $(filter-out $(NSS_LIBS),$(STATIC_EXTRA_LIBS))
endif

EXTRA_DSO_LDOPTS += \
	$(LIBS_DIR) \
	$(EXTRA_DSO_LIBS) \
	$(STATIC_EXTRA_LIBS) \
	$(MOZ_JS_LIBS) \
	$(MOZ_UNICHARUTIL_LIBS) \
	$(MOZ_COMPONENT_LIBS) \
	$(TK_LIBS) \
	$(NULL)

ifeq ($(MOZ_WIDGET_TOOLKIT),cocoa)
OS_LIBS	+= -framework QuickTime
endif

ifeq ($(OS_ARCH),Darwin)

# A limitation exists in the Mach-O format which causes "scattered" relocation
# entries in shared libraries to be limited to a 24-bit address space (16 MB).
# The high 8 bits of the address are dropped.  This can cause problems in
# a mozcomps build, particularly non-optimized builds.  To avoid the problem,
# we move the data segment before the code segment in the libraray.  The data
# segment is relatively small, and all of the relocations in question
# (which are pointers to construction vtables) reside in the
# (__DATA,__const) section.
#
# See also the 5th item in the TODO section of:
#   http://www.opensource.apple.com/darwinsource/10.2.6/cctools/ld/notes
#  (free APSL registration required)


# Generate a .o file with only a data segment, which we'll link as the first
# object file in the library.  That will cause the data segment to be first.

force_data_segment.o:
	ld -sectcreate __DATA __const /dev/null -noseglinkedit -arch ppc -o $@

exported_symbols:
	echo _nsMetaModule_nsGetModule > $@

# We have to invoke ld directly so that force_data_segment.o can be linked
# before dylib1.o
MKSHLIB = $(LD) $(DSO_LDOPTS) -o $@

# force it to be first
SHLIB_LDSTARTFILE = force_data_segment.o -ldylib1.o

# translate gcc link options to corresponding ld options
EXTRA_DSO_LDOPTS := $(subst -dynamiclib,-dylib,$(EXTRA_DSO_LDOPTS))
EXTRA_DSO_LDOPTS := $(subst install_name,dylib_install_name,$(EXTRA_DSO_LDOPTS))
EXTRA_DSO_LDOPTS := $(subst compatibility_version,dylib_compatibility_version,$(EXTRA_DSO_LDOPTS))
EXTRA_DSO_LDOPTS := $(subst current_version,dylib_current_version,$(EXTRA_DSO_LDOPTS))

# It would be nice to limit the exported symbols here, but doing so exposes a
# bug in the linker that causes some C++ static initializers not to run after
# prebinding is updated at runtime (reported as Radar bug 3268595).
#EXTRA_DSO_LDOPTS += -exported_symbols_list exported_symbols

OS_LIBS := -lstdc++ $(OS_LIBS)
SHLIB_LDENDFILE = -lgcc

endif  # darwin

ifeq ($(OS_ARCH),WINNT)
OS_LIBS	+= ole32.lib shell32.lib
endif

ifeq ($(OS_ARCH),BeOS)
OS_LIBS += -ltracker
endif

GARBAGE += $(CPPSRCS)

ifdef _NO_AUTO_VARS
_TARGET = $(srcdir)/$(@F)
GARBAGE	+= $(addprefix $(srcdir)/,$(CPPSRCS))
else
_TARGET = $@
endif

nsMetaModule_%.cpp: nsMetaModule.cpp.in $(GLOBAL_DEPS) $(topsrcdir)/config/config.mk
	rm -f $@
	cat $< | \
	sed -e "s|%DECLARE_SUBMODULE_INFOS%|$(foreach m, $($(_META_COMPONENT_NAMES)), extern nsModuleInfo NSMODULEINFO($(m));)|" | \
	sed -e "s|%SUBMODULE_INFOS%|$(foreach m, $($(_META_COMPONENT_NAMES)), \\& NSMODULEINFO($(m)),)|" \
	> $(_TARGET)

