# Makefile for applications/3pp/mozilla-1.7.12


### Requires these modules

REQUIRES += applications/3pp/glib-1.2.10
REQUIRES += applications/3pp/gtk+-1.2.10
REQUIRES += applications/3pp/libpng-1.2.8


### Component specification

COMPONENT_IS_3PP  = true
COMPONENT_TARGETS = $(TARGET_NAME_VIP)
NEEDS_ABS_PATHS = true


### Tar archive

tar_archive = $(CURDIR)/mozilla-1.7.12-source.tar.bz2
src_dir     = $(CURDIR)/$(TARGET)/mozilla
obj_dir     = $(CURDIR)/$(TARGET)/build


### Patches

patches += configure                   # Cross compile
patches += nsprpub_config_Makefile.in  # Cross compile
patches += libidl                      # Fixes for libIDL on target and 64-bit.

patches += nsGlobalWindow.cpp          # Event bubbling (TOI), dump() to stderr
patches += nsGtkEventHandler.cpp       # Embedding, TOI input
patches += jsinterp.c                  # Javascript function call behaviour
# mips*el matches mipsel-kreatel
patches += config.sub_mips

# The xptcall implementation for MIPS is broken for recent versions of GCC.
#
# - The C++ vtable layout has changed, so the method invocation code
#   written in MIPS assembler language must be updated.
#
#     http://www.gnu.org/software/gcc/gcc-3.2/c++-abi.html
#
#   See also the changes in xptcinvoke_asm_sparc_linux_GCC3.s from
#   xptcinvoke_asm_sparc_linux.s where an offset of 8 is no longer added
#   during the lookup.
#   
#   Further changes/bug fixes have been made to the C ABI in GCC 3.4 to achieve
#   SGI/Irix compatibility but these should not affect Mozilla since the first
#   argument in method calls is always a pointer ('this').
#   More information can be found here (note: we use the little endian o32 C ABI):
#
#     http://www.gnu.org/software/gcc/gcc-3.4/mips-abi.html
#
# - The name mangling of C++ functions and methods is now different so
#   the assembler stubs must be changed to avoid linking errors.
#   The new implementation is modeled after the Mac OS X/Darwin port.
patches += xptcall_GCC3_mips

# The library libgklayout.so causes problems on MIPS because the global offset
# table (GOT) used by position independent code (PIC) will exceed 64 kB
# (16K entries). This can be verified by running 'objdump -h' and noting the
# size of the section labeled '.got'.
# The lookup code normally generated by GCC cannot handle entries beyond this
# limit.
# However this problem can be resolved by compiling the library with the flag
# '-Wa,-xgot' (or with GCC 3.4 and later '-mxgot') since the assembler will
# then generate a different code sequence. The downside is that three
# instructions are now used for each lookup instead of just one.
# This flag is already added both in configure and in config/rules.mk.
# However with the current devenv the resulting library will still be
# broken.
# Our workaround doesn't use xgot and instead splits the layout libraries
# into several smaller libraries since the 64K limit is per library.
# NOTE: By explicitly disabling xgot code size is reduced since single
# instruction lookups are then possible.
# NOTE: The new libraries libgklayout1.so and libgklayout2.so are now
# linked with a version script that reduces the size of the export symbol
# table. The list of symbols was compiled by running the command 
# 'nm --dynamic lib.so | grep -e " U "' on the three layout libraries and
# concatenating the results. The list was then further processed with
# 'sort --unique'. Some postprocessing is also needed to get the
# right version script syntax.
patches += no_xgot_mips
patches += layout_build_mips

# Add support for the SH4 architecture. Some new files are also copied to
# the Mozilla tree below.
patches += nspr_sh4
patches += xptcall_sh4

# Don't use fdlibm for the JavaScript math implementation. We don't need it
# and it is broken on both MIPS and SH4.
patches += no_fdlibm

# Mozilla includes it own version of zlib, libjpeg and libpng. These are used
# instead of libraries present in the system if the version numbers don't
# match the requirements. The version number check is implemented by building
# and running a simple C test program. This is obviously not possible when
# cross-compiling and in this case Mozilla plays it safe and uses its own
# libraries.
# This patch removes this undesired restriction and forces Mozilla to use our
# libraries also when cross-compiling in order to save some memory in the STB.
# NOTE: It is now up to us to check the version numbers when upgrading!
patches += system_libs

# Gamma correction in the PNG library requires HW floating point support for
# acceptable performance. The Xilleon chip with a MIPS 4kc core does not have
# a floating point co-processor and thus we choose to disable gamma correction
# when building for this architecture.
# This patch is needed to prevent Mozilla from using the unsupported functions
# in libpng.
# TODO: MIPS only?
patches += no_png_gamma

# We currently don't use the built-in implementation of SSL/TLS needed by the
# https protocol. Instead all https requests are redirected to a local proxy
# ("SSLProxy") running on port 3138. This helper process in turn uses the
# OpenSSL library to setup an encrypted connection to the server. It is not
# really clear if and how much memory this solution actually saves.
# Error handling is currently very basic, i e the end-user is not presented
# with any informative messages when things go wrong.
patches += https

# Load content in the browser even when there is a Content-Disposition
# header in the response.
patches += content_disposition

# Some navigation patches for HTML select elements.
patches += html_select

# Make scrollbars in form controls look better and remove some printed media
# stuff while we're at it.
# Note that window and frame scrollbars are deliberately disabled in
# the Kreatel app-suite. This is done in userContent.css which is a
# user-supplied file stored in the profile directory.
patches += scrollbars

# Don't load images etc when there is too little free memory.
patches += memory_limit

# Get local time zone when retrieving date/time.
patches += js_src_jsdate.c

# Fixed compile warnings
patches += nsHTMLValue.h

# Changed the directory-index-to-html stream converter so that it doesn't
# generate a xhtml header. This header is removed because it causes
# problems in navhelper, i.e. the blue navigation box doesn't work.
patches += noXHTMLDirs

# Update for the marque feature. When testing with Telus channel 1 -
# (http://channel1.mytelus.com), the marquee will stop about 1 minute
# in each cycle
patches += marquee

### Targets

target_targets = $(TARGET_INSTALL_DIR)/mozilla

host_targets  += $(HOST_INSTALL_DIR)/bin/host_xpidl
host_targets  += $(HOST_INSTALL_DIR)/bin/host_xpt_link

OTHER_TARGETS += $(target_targets) $(host_targets)

DOC_TARGETS   += $(src_dir)/LICENSE
DOC_TARGETS   += $(src_dir)/LEGAL

DOC_OMIT_SOURCE_DIR = yes
DOC_PATH = ../../doc/doc


### Extra cleanup

CLEANUP_FILES += $(addprefix rebuild_,$(TARGET_NAME_ALL))


### Common.mk

include ../../../makesystem/common.mk


### Prevent Mozilla from using target flags for host builds.

MOZ_CROSSSETUP += HOST_CFLAGS="$(HOST_LDFLAGS) $(HOST_CFLAGS) "
MOZ_CROSSSETUP += HOST_CXXFLAGS="$(HOST_LDFLAGS) $(HOST_CFLAGS) "


### Dependencies

$(src_dir): $(tar_archive)
	@echo "Expanding $(^F)"
	@mkdir -p $(TARGET)
	@cd $(TARGET) && tar xfj $^
	@cd $(src_dir) && \
	  for p in $(patches) ; do \
	    patch -b -p0 < $(CURDIR)/patch/$$p.patch ; \
	  done
	@cd $(CURDIR)/patch && \
	  cp layout.ver $(src_dir)/layout/build && \
	  cp xptcall_sh4.cpp xptcinvoke_asm_sh4.s xptcstubs_asm_sh4.s.m4 \
	    $(src_dir)/xpcom/reflect/xptcall/src/md/unix
	@touch $@

$(obj_dir)/Makefile: $(src_dir)
	@mkdir -p $(obj_dir)
	@cd $(obj_dir) && \
	  $(BSG_DEVSETUP) $(MOZ_CROSSSETUP) \
	  CROSS_COMPILE=1 \
	  GLIB_CONFIG="$(TARGET_3PP_DIST_DIR)/bin/glib-config" \
	  GTK_CONFIG="$(TARGET_3PP_DIST_DIR)/bin/gtk-config" \
	  $(src_dir)/configure \
	    --build=$(ARCH_BUILD) \
	    --host=$(TARGET_ARCH) \
	    --target=$(TARGET_ARCH) \
	    --disable-accessibility \
	    --disable-composer \
	    --disable-debug \
	    --disable-freetype2 \
	    --disable-freetypetest \
	    --disable-glibtest \
	    --disable-gtktest \
	    --disable-installer \
	    --disable-jsd \
	    --disable-jsloader \
	    --disable-ldap \
	    --disable-libIDLtest \
	    --disable-logging \
	    --disable-mailnews \
	    --disable-mathml \
	    --disable-necko-disk-cache \
	    --disable-oji \
	    --disable-postscript \
	    --disable-profilelocking \
	    --disable-profilesharing \
	    --disable-static-component-loader \
	    --disable-tests \
	    --disable-updater \
	    --disable-view-source \
	    --disable-xpfe-components \
	    --disable-xpinstall \
	    --disable-xprint \
	    --disable-xtf \
	    --disable-xul \
	    --enable-chrome-format=symlink \
	    --enable-elf-dynstr-gc \
	    --enable-extensions="cookie,xmlextras,transformiix" \
	    --enable-image-decoders="bmp,gif,jpeg,png" \
	    --enable-necko-protocols="file,http,res" \
	    --enable-necko-small-buffers \
	    --enable-optimize="$(OPTIMIZATION_FLAGS)" \
	    --enable-plaintext-editor-only \
	    --enable-reorder \
	    --enable-single-profile \
	    --with-glib-prefix=$(TARGET_3PP_DIST_DIR) \
	    --with-gtk-prefix=$(TARGET_3PP_DIST_DIR) \
	    --with-system-jpeg=$(TARGET_3PP_DIST_DIR) \
	    --with-system-png=$(TARGET_3PP_DIST_DIR) \
	    --with-system-zlib=$(TARGET_3PP_DIST_DIR) \
	    --x-includes=$(TARGET_3PP_DIST_DIR)/include \
	    --x-libraries=$(TARGET_3PP_DIST_DIR)/lib
	@touch $@

$(obj_dir)/dist: $(obj_dir)/Makefile
	@$(BSG_DEVSETUP) $(MAKE) -C $(obj_dir)
	@touch $@

# Special rule to rebuild only part of Mozilla during debugging etc.
# 1. Add a make command to the rule below, e g:
#      @$(BSG_DEVSETUP) $(MAKE) -C $(obj_dir)/path_to_module
# 2. To start rebuild type the following at the command line:
#      rm rebuild_*; make local_all
rebuild_$(TARGET):
	@touch $@

$(target_targets): $(obj_dir)/dist rebuild_$(TARGET)
	@echo "Installing Mozilla"
	@mkdir -p $@
	@cp -urL $(obj_dir)/dist/* $@
	@touch $@

$(host_targets): $(obj_dir)/dist
	@echo "Installing $(@F)"
	@mkdir -p $(@D)
	@cp -L $(obj_dir)/dist/host/bin/$(@F) $(@D)

$(DOC_TARGETS): $(src_dir)
