From 2de3599acc75ce3ae9c6debdd86e31a7090804ba Mon Sep 17 00:00:00 2001
From: wagerlabs <joelr1@gmail.com>
Date: Thu, 14 Jan 2010 17:36:29 +0000
Subject: [PATCH] static libxul with review changes

---
 browser/Makefile.in                           |    1 -
 browser/app/Makefile.in                       |   79 +++++++++++++++++++++-
 browser/build.mk                              |    8 ++
 browser/components/build/Makefile.in          |   15 ++++
 browser/confvars.sh                           |    3 +-
 config/app-build.mk                           |   17 +++++
 config/config.mk                              |    6 ++
 config/rules.mk                               |   14 ++++-
 config/static-config.mk                       |    8 ++
 config/static-rules.mk                        |   19 ++++-
 configure.in                                  |   16 ++--
 db/sqlite3/src/Makefile.in                    |    4 +
 extensions/Makefile.in                        |    2 +
 js/jsd/Makefile.in                            |    2 +
 js/src/config/config.mk                       |    2 +
 modules/libpr0n/decoders/icon/Makefile.in     |    9 +++
 modules/libpr0n/decoders/icon/gtk/Makefile.in |    4 +
 modules/plugin/Makefile.in                    |    4 +
 toolkit/components/alerts/Makefile.in         |    2 +
 toolkit/components/alerts/src/mac/Makefile.in |    1 -
 toolkit/library/Makefile.in                   |   13 +++-
 toolkit/static-postxpcom-tiers.mk             |   83 ++++++++++++++++++++++
 toolkit/toolkit-tiers.mk                      |    6 +-
 toolkit/xre/Makefile.in                       |    8 +-
 xpcom/build/Makefile.in                       |    5 +-
 xpcom/stub/Makefile.in                        |   26 +++++++
 xulrunner/Makefile.in                         |    5 +-
 xulrunner/app/Makefile.in                     |   93 ++++++++++++++++++++++++-
 xulrunner/app/nsRegisterGRE.h                 |    2 +-
 xulrunner/app/nsRegisterGREUnix.cpp           |    2 +-
 xulrunner/app/nsRegisterGREWin.cpp            |    2 +-
 xulrunner/app/nsXULRunnerApp.cpp              |    3 +-
 xulrunner/build.mk                            |    4 +
 xulrunner/confvars.sh                         |    6 +-
 34 files changed, 438 insertions(+), 36 deletions(-)
 create mode 100644 config/app-build.mk
 create mode 100644 toolkit/static-postxpcom-tiers.mk

diff --git a/browser/Makefile.in b/browser/Makefile.in
index 012214b..caf9695 100644
--- a/browser/Makefile.in
+++ b/browser/Makefile.in
@@ -44,7 +44,6 @@ include $(topsrcdir)/config/config.mk
 
 PARALLEL_DIRS = \
   base \
-  components \
   fuel \
   locales \
   themes \
diff --git a/browser/app/Makefile.in b/browser/app/Makefile.in
index 8e04667..b408887 100644
--- a/browser/app/Makefile.in
+++ b/browser/app/Makefile.in
@@ -103,6 +103,9 @@ else
 STATIC_COMPONENTS_LINKER_PATH = -L$(DEPTH)/staticlib
 endif
 LIBS += $(DEPTH)/toolkit/xre/$(LIB_PREFIX)xulapp_s.$(LIB_SUFFIX)
+ifeq (cocoa,$(MOZ_WIDGET_TOOLKIT))
+OS_LIBS += -framework SystemConfiguration -framework OpenGL
+endif
 else
 ifeq (cocoa,$(MOZ_WIDGET_TOOLKIT))
 LIBS += $(DIST)/bin/XUL
@@ -115,12 +118,17 @@ ifeq (cocoa,$(MOZ_WIDGET_TOOLKIT))
 TK_LIBS := $(TK_LIBS)
 endif
 
+ifndef BUILD_STATIC_LIBS
 ifdef MOZ_ENABLE_LIBXUL
 APP_XPCOM_LIBS = $(XPCOM_GLUE_LDOPTS)
 else
 MOZILLA_INTERNAL_API = 1
 APP_XPCOM_LIBS = $(XPCOM_LIBS)
 endif
+else
+MOZILLA_INTERNAL_API = 1
+APP_XPCOM_LIBS =
+endif
 
 LIBS += \
 	$(STATIC_COMPONENTS_LINKER_PATH) \
@@ -148,6 +156,19 @@ MOZ_WINCONSOLE = 0
 endif
 endif
 
+
+ifdef BUILD_STATIC_LIBS
+
+SHARED_LIBRARY_LIBS += \
+  $(DEPTH)/toolkit/xre/$(LIB_PREFIX)xulapp_s.$(LIB_SUFFIX) \
+  $(NULL)
+
+SHARED_LIBRARY_LIBS += \
+  $(foreach component,$(COMPONENT_LIBS),$(DEPTH)/staticlib/components/$(LIB_PREFIX)$(component).$(LIB_SUFFIX)) \
+  $(foreach lib,$(STATIC_LIBS),$(DEPTH)/staticlib/$(LIB_PREFIX)$(lib).$(LIB_SUFFIX)) \
+  $(NULL)
+endif
+
 # This switches $(INSTALL) to copy mode, like $(SYSINSTALL), so things that
 # shouldn't get 755 perms need $(IFLAGS1) for either way of calling nsinstall.
 NSDISTMODE = copy
@@ -171,6 +192,58 @@ endif
 ifdef BUILD_STATIC_LIBS
 include $(topsrcdir)/config/static-config.mk
 
+STATIC_EXTRA_LIBS += $(DEPTH)/dist/lib/$(LIB_PREFIX)mozz.$(LIB_SUFFIX)
+# Don't want nsStaticComponents.cpp
+STATIC_CPPSRCS =
+
+ifdef MOZ_ENABLE_DBUS
+LIBS += $(MOZ_DBUS_GLIB_LIBS)
+endif
+
+ifdef MOZ_PLATFORM_MAEMO
+LIBS += $(MOZ_PLATFORM_MAEMO_LIBS)
+endif
+
+ifdef MOZ_ENABLE_LIBCONIC
+LIBS += $(LIBCONIC_LIBS)
+endif
+
+ifdef MOZ_ENABLE_GCONF
+LIBS += $(MOZ_GCONF_LIBS)
+endif
+
+ifdef MOZ_ENABLE_GNOMEVFS
+LIBS += $(MOZ_GNOMEVFS_LIBS)
+endif
+
+ifdef MOZ_ENABLE_LIBNOTIFY
+IBS += $(MOZ_LIBNOTIFY_LIBS)
+endif
+
+ifdef MOZ_ENABLE_GIO
+LIBS += $(MOZ_GIO_LIBS)
+endif
+
+# Assume both FORCE and UNFORCE are defined
+
+ifdef MKSHLIB_FORCE_ALL
+LIBS += \
+	$(MKSHLIB_FORCE_ALL) \
+	$(DIST)/lib/$(LIB_PREFIX)xul.$(LIB_SUFFIX) \
+	$(DIST)/lib/$(LIB_PREFIX)xpcom_core.$(LIB_SUFFIX) \
+	$(MKSHLIB_UNFORCE_ALL) \
+	$(NULL)
+else
+FORCE_LOAD_LIBS += \
+	$(DIST)/lib/$(LIB_PREFIX)xul.$(LIB_SUFFIX) \
+	$(DIST)/lib/$(LIB_PREFIX)xpcom_core.$(LIB_SUFFIX) \
+	$(NULL)
+endif
+
+ifeq ($(OS_ARCH),WINNT)
+OS_LIBS += $(call EXPAND_LIBNAME,shlwapi)
+endif
+
 EXTRA_DEPS	+= \
 	$(STATIC_EXTRA_DEPS) \
 	$(NULL)
@@ -222,7 +295,11 @@ endif
 include $(topsrcdir)/config/rules.mk
 
 ifdef BUILD_STATIC_LIBS
-include $(topsrcdir)/config/static-rules.mk
+
+# Make sure shared libraries resolve using symbols in Firefox
+ifeq ($(OS_ARCH),Linux)
+LDFLAGS += -rdynamic
+endif
 
 DEFINES += -DIMPL_XREAPI
 endif
diff --git a/browser/build.mk b/browser/build.mk
index 9f2f26f..d7bc5d3 100644
--- a/browser/build.mk
+++ b/browser/build.mk
@@ -41,14 +41,22 @@ endif
 
 TIERS += app
 
+ifndef BUILD_STATIC_LIBS
 ifdef MOZ_EXTENSIONS
 tier_app_dirs += extensions
 endif
+endif
 
 tier_app_dirs += $(MOZ_BRANDING_DIRECTORY)
 
 tier_app_dirs += browser
 
+ifndef LIBXUL_SDK
+ifdef BUILD_STATIC_LIBS
+include $(topsrcdir)/toolkit/static-postxpcom-tiers.mk
+endif
+endif
+
 installer:
 	@$(MAKE) -C browser/installer installer
 
diff --git a/browser/components/build/Makefile.in b/browser/components/build/Makefile.in
index 4538754..b976f2d 100644
--- a/browser/components/build/Makefile.in
+++ b/browser/components/build/Makefile.in
@@ -86,4 +86,19 @@ EXTRA_DSO_LDOPTS += $(LIBXUL_DIST)/lib/$(LIB_PREFIX)thebes.$(IMPORT_LIB_SUFFIX)
 endif
 endif
 
+ifdef BUILD_STATIC_LIBS
+ifeq ($(OS_ARCH), Darwin)
+LIBXUL_LIBS=$(XPCOM_FROZEN_LDOPTS)
+XPCOM_LIBS=$(XPCOM_FROZEN_LDOPTS)
+endif
+endif
+
+ifdef BUILD_STATIC_LIBS
+ifeq ($(OS_ARCH),WINNT)
+EXTRA_DSO_LDOPTS += \
+	$(DEPTH)/browser/app/$(LIB_PREFIX)$(MOZ_APP_NAME).$(IMPORT_LIB_SUFFIX)
+endif
+endif
+
+
 include $(topsrcdir)/config/rules.mk
diff --git a/browser/confvars.sh b/browser/confvars.sh
index 56f2986..1e5edf2 100755
--- a/browser/confvars.sh
+++ b/browser/confvars.sh
@@ -41,7 +41,8 @@ MOZ_UPDATER=1
 MOZ_PHOENIX=1
 
 MOZ_ENABLE_LIBXUL=1
-MOZ_STATIC_BUILD_UNSUPPORTED=1
+BUILD_STATIC_LIBS=1
+
 MOZ_PLACES=1
 # always enabled for form history
 MOZ_MORKREADER=1
diff --git a/config/app-build.mk b/config/app-build.mk
new file mode 100644
index 0000000..d3e59d3
--- /dev/null
+++ b/config/app-build.mk
@@ -0,0 +1,17 @@
+ifndef BUILD_STATIC_LIBS
+LIBS += $(XPCOM_LIBS)
+endif
+
+LIBS += \
+  $(NSPR_LIBS) \
+  $(DIST)/lib/$(LIB_PREFIX)xul.$(LIB_SUFFIX) \
+	$(DIST)/lib/$(LIB_PREFIX)xpcom_core.$(LIB_SUFFIX) \
+  $(NULL)
+  
+ifdef APP_NEEDS_XRE
+LIBS += $(DEPTH)/toolkit/xre/$(LIB_PREFIX)xulapp_s.$(LIB_SUFFIX)
+LOCAL_INCLUDES += -I$(topsrcdir)/toolkit/xre
+endif
+
+
+
diff --git a/config/config.mk b/config/config.mk
index 1499cc4..2c0c1db 100644
--- a/config/config.mk
+++ b/config/config.mk
@@ -349,6 +349,7 @@ endif
 # Disable PIC if necessary
 #
 
+ifndef BUILD_STATIC_LIBS
 ifndef _ENABLE_PIC
 DSO_CFLAGS=
 ifeq ($(OS_ARCH)_$(HAVE_GCC3_ABI),Darwin_1)
@@ -357,6 +358,7 @@ else
 DSO_PIC_CFLAGS=
 endif
 endif
+endif
 
 ifndef SHARED_LIBRARY_NAME
 ifdef LIBRARY_NAME
@@ -835,3 +837,7 @@ ifdef TIERS
 DIRS += $(foreach tier,$(TIERS),$(tier_$(tier)_dirs))
 STATIC_DIRS += $(foreach tier,$(TIERS),$(tier_$(tier)_staticdirs))
 endif
+
+ifdef BUILD_STATIC_LIBS
+include $(topsrcdir)/config/static-config.mk
+endif
diff --git a/config/rules.mk b/config/rules.mk
index c6b6d74..5baf56b 100644
--- a/config/rules.mk
+++ b/config/rules.mk
@@ -196,6 +196,10 @@ check-one:
 
 endif # XPCSHELL_TESTS
 
+ifdef BUILD_STATIC_LIBS
+CPP_UNIT_TESTS=
+endif
+
 ifdef CPP_UNIT_TESTS
 
 # Compile the tests to $(DIST)/bin.  Make lots of niceties available by default
@@ -204,7 +208,11 @@ ifdef CPP_UNIT_TESTS
 CPPSRCS += $(CPP_UNIT_TESTS)
 SIMPLE_PROGRAMS += $(CPP_UNIT_TESTS:.cpp=$(BIN_SUFFIX))
 INCLUDES += -I$(DIST)/include/testing
-LIBS += $(XPCOM_GLUE_LDOPTS) $(NSPR_LIBS)
+ifndef BUILD_STATIC_LIBS
+LIBS += $(XPCOM_GLUE_LDOPTS)
+endif
+LIBS += $(NSPR_LIBS)
+
 
 # ...and run them the usual way
 check::
@@ -2286,3 +2294,7 @@ CHECK_FROZEN_VARIABLES = $(foreach var,$(FREEZE_VARIABLES), \
 
 libs export libs::
 	$(CHECK_FROZEN_VARIABLES)
+
+ifdef BUILD_STATIC_LIBS
+include $(topsrcdir)/config/static-rules.mk
+endif
diff --git a/config/static-config.mk b/config/static-config.mk
index eba4edf..b9cb602 100644
--- a/config/static-config.mk
+++ b/config/static-config.mk
@@ -117,3 +117,11 @@ STATIC_EXTRA_LIBS += $(call EXPAND_LIBNAME,odm cfg)
 endif
 
 LOCAL_INCLUDES += -I$(topsrcdir)/config
+
+# 
+# Force whole loading of static libraries
+#
+
+FORCE_LOAD_CONTENTS = $(addsuffix .contents, $(FORCE_LOAD_LIBS))
+GARBAGE_DIRS += $(FORCE_LOAD_CONTENTS)
+EXTRA_DEPS += $(FORCE_LOAD_CONTENTS)
diff --git a/config/static-rules.mk b/config/static-rules.mk
index f4842db..9953523 100644
--- a/config/static-rules.mk
+++ b/config/static-rules.mk
@@ -5,9 +5,6 @@ else
 _TARGET = $@
 endif
 
-$(warning FINAL_LINK_COMP_NAMES = $(FINAL_LINK_COMP_NAMES))
-$(warning FINAL_LINK_COMPS = $(FINAL_LINK_COMPS))
-
 nsStaticComponents.cpp: $(topsrcdir)/config/nsStaticComponents.cpp.in $(GLOBAL_DEPS) $(FINAL_LINK_COMP_NAMES)
 	rm -f $@
 	cat $< | \
@@ -21,5 +18,19 @@ LDFLAGS	+= -Wl,-LD_LAYOUT:lgot_buffer=80
 endif
 
 ifeq (cocoa,$(MOZ_WIDGET_TOOLKIT))
-LIBS	+= -framework QuickTime -framework IOKit -lcrypto
+OS_LIBS += -framework QuickTime -framework IOKit -lcrypto
 endif
+
+# 
+# Force whole loading of static libraries
+#
+
+$(DIST)/lib/$(LIB_PREFIX)%.$(LIB_SUFFIX).contents: $(DIST)/lib/$(LIB_PREFIX)%.$(LIB_SUFFIX)
+	rm -fr $@
+	mkdir $@
+	cd $@ && $(AR_EXTRACT) ../$<
+
+FORCE_LOAD_OBJS := $(foreach lib, $(FORCE_LOAD_LIBS), $(addprefix $(lib).contents/, $(shell $(AR_LIST) $(lib) $(CLEANUP1))))
+GARBAGE += $(FORCE_LOAD_OBJS)
+LIBS += $(FORCE_LOAD_OBJS)
+FORCE_LOAD_LIBS = 
diff --git a/configure.in b/configure.in
index 18cfcce..2680e91 100644
--- a/configure.in
+++ b/configure.in
@@ -2104,8 +2104,8 @@ case "$target" in
     LIB_SUFFIX=lib 
     MKCSHLIB='$(LD) -NOLOGO -DLL -OUT:$@ $(DSO_LDOPTS)'
     MKSHLIB='$(LD) -NOLOGO -DLL -OUT:$@ $(DSO_LDOPTS)'
-    MKSHLIB_FORCE_ALL=
-    MKSHLIB_UNFORCE_ALL=
+    MKSHLIB_FORCE_ALL='-OPT:NOREF'
+    MKSHLIB_UNFORCE_ALL='-OPT:REF'
     MOZ_COMPONENT_NSPR_LIBS='$(NSPR_LIBS)'
     MOZ_COMPONENT_NSPR_LIBS='$(NSPR_LIBS)'
     MOZ_DEBUG_FLAGS='-Zi'
@@ -2206,8 +2206,8 @@ case "$target" in
         IMPORT_LIB_SUFFIX=lib
         MKSHLIB='$(LD) -NOLOGO -DLL -OUT:$@ -PDB:$(LINK_PDBFILE) $(DSO_LDOPTS)'
         MKCSHLIB='$(LD) -NOLOGO -DLL -OUT:$@ -PDB:$(LINK_PDBFILE) $(DSO_LDOPTS)'
-        MKSHLIB_FORCE_ALL=
-        MKSHLIB_UNFORCE_ALL=
+    	MKSHLIB_FORCE_ALL='-OPT:NOREF'
+    	MKSHLIB_UNFORCE_ALL='-OPT:REF'
         DSO_LDOPTS=-SUBSYSTEM:WINDOWS
         _USE_CPP_INCLUDE_FLAG=1
         _DEFINES_CFLAGS='-FI $(DEPTH)/dist/include/mozilla-config.h -DMOZILLA_CLIENT'
@@ -7510,14 +7510,14 @@ if test -n "$MOZ_STATIC_BUILD_UNSUPPORTED" -a -n "$BUILD_STATIC_LIBS"; then
 	AC_MSG_ERROR([--enable-static is not supported for building $MOZ_APP_NAME. You probably want --enable-libxul.])
 fi
 
-if test -n "$MOZ_ENABLE_LIBXUL" -a -n "$BUILD_STATIC_LIBS"; then
-	AC_MSG_ERROR([--enable-libxul is not compatible with --enable-static])
-fi
-
 if test -n "$MOZ_IPC" -a -z "$MOZ_ENABLE_LIBXUL"; then
     AC_MSG_ERROR([--enable-ipc requires --enable-libxul])
 fi
 
+if test -n "$MOZ_IPC" -a -n "$BUILD_STATIC_LIBS"; then
+	AC_MSG_ERROR([--enable-ipc is not compatible with --enable-static.])
+fi
+
 AC_SUBST(LIBXUL_LIBS)
 
 if test -n "$MOZ_ENABLE_LIBXUL"; then
diff --git a/db/sqlite3/src/Makefile.in b/db/sqlite3/src/Makefile.in
index 08e6f0f..4db8518 100644
--- a/db/sqlite3/src/Makefile.in
+++ b/db/sqlite3/src/Makefile.in
@@ -48,7 +48,11 @@ include $(DEPTH)/config/autoconf.mk
 MODULE           = sqlite3
 LIBRARY_NAME     = mozsqlite3
 SHORT_LIBNAME    = mozsqlt3
+ifndef BUILD_STATIC_LIBS
 FORCE_SHARED_LIB = 1
+else
+DIST_INSTALL = 1
+endif
 VISIBILITY_FLAGS =
 LIB_IS_C_ONLY    = 1
 
diff --git a/extensions/Makefile.in b/extensions/Makefile.in
index 64a39b3..09f75d7 100644
--- a/extensions/Makefile.in
+++ b/extensions/Makefile.in
@@ -42,6 +42,8 @@ VPATH     = @srcdir@
 
 include $(DEPTH)/config/autoconf.mk
 
+ifndef BUILD_STATIC_LIBS
 PARALLEL_DIRS = $(MOZ_EXTENSIONS)
+endif
 
 include $(topsrcdir)/config/rules.mk
diff --git a/js/jsd/Makefile.in b/js/jsd/Makefile.in
index fb2500a..d72daa7 100644
--- a/js/jsd/Makefile.in
+++ b/js/jsd/Makefile.in
@@ -47,7 +47,9 @@ include $(DEPTH)/config/autoconf.mk
 
 MODULE		= jsdebug
 LIBRARY_NAME	= jsd
+ifndef BUILD_STATIC_LIBS
 FORCE_SHARED_LIB= 1
+endif
 ifeq ($(OS_ARCH)$(MOZ_ENABLE_LIBXUL),WINNT)
 LIBRARY_NAME	= jsd32$(VERSION_NUMBER)
 endif
diff --git a/js/src/config/config.mk b/js/src/config/config.mk
index 1499cc4..1a9351f 100644
--- a/js/src/config/config.mk
+++ b/js/src/config/config.mk
@@ -349,6 +349,7 @@ endif
 # Disable PIC if necessary
 #
 
+#ifndef BUILD_STATIC_LIBS
 ifndef _ENABLE_PIC
 DSO_CFLAGS=
 ifeq ($(OS_ARCH)_$(HAVE_GCC3_ABI),Darwin_1)
@@ -357,6 +358,7 @@ else
 DSO_PIC_CFLAGS=
 endif
 endif
+#endif
 
 ifndef SHARED_LIBRARY_NAME
 ifdef LIBRARY_NAME
diff --git a/modules/libpr0n/decoders/icon/Makefile.in b/modules/libpr0n/decoders/icon/Makefile.in
index 2cb4a48..586c495 100644
--- a/modules/libpr0n/decoders/icon/Makefile.in
+++ b/modules/libpr0n/decoders/icon/Makefile.in
@@ -50,12 +50,21 @@ MODULE_NAME	= nsIconDecoderModule
 ifneq (,$(filter gtk2,$(MOZ_WIDGET_TOOLKIT)))
 EXTRA_DSO_LDOPTS = $(MOZ_GTK2_LIBS)
 PLATFORM = gtk
+ifndef BUILD_STATIC_LIBS
 FORCE_SHARED_LIB = 1
+endif
 else
 LIBXUL_LIBRARY = 1
 EXPORT_LIBRARY = 1
 endif
 
+ifdef BUILD_STATIC_LIBS
+USE_STATIC_LIBS = 1
+FORCE_STATIC_LIB = 1
+EXPORT_LIBRARY = 1
+MOZILLA_INTERNAL_API = 1
+endif
+
 ifneq (,$(filter qt,$(MOZ_WIDGET_TOOLKIT)))
 EXTRA_DSO_LDOPTS = $(MOZ_QT_LIBS)
 PLATFORM = qt
diff --git a/modules/libpr0n/decoders/icon/gtk/Makefile.in b/modules/libpr0n/decoders/icon/gtk/Makefile.in
index e6a0448..5638f6e 100644
--- a/modules/libpr0n/decoders/icon/gtk/Makefile.in
+++ b/modules/libpr0n/decoders/icon/gtk/Makefile.in
@@ -59,5 +59,9 @@ FORCE_STATIC_LIB = 1
 # This library is used by other shared libs in a static build
 FORCE_USE_PIC = 1
 
+ifdef BUILD_STATIC_LIBS
+MOZILLA_INTERNAL_API = 1
+endif
+
 include $(topsrcdir)/config/rules.mk
 
diff --git a/modules/plugin/Makefile.in b/modules/plugin/Makefile.in
index 8a80364..650fa58 100644
--- a/modules/plugin/Makefile.in
+++ b/modules/plugin/Makefile.in
@@ -49,6 +49,9 @@ DIRS		= base/public
 ifdef MOZ_PLUGINS
 DIRS		+= base/src 
 
+# Picked up by the post-xpcom tier of a static build
+
+ifndef BUILD_STATIC_LIBS
 ifeq ($(MOZ_WIDGET_TOOLKIT),gtk2)
 TOOL_DIRS += sdk/samples/unixprinting
 endif
@@ -59,6 +62,7 @@ endif
 ifdef MOZ_ENABLE_GTK2
 TOOL_DIRS += default/unix
 endif
+endif
 
 ifeq ($(OS_ARCH),WINNT)
 TOOL_DIRS += default/windows
diff --git a/toolkit/components/alerts/Makefile.in b/toolkit/components/alerts/Makefile.in
index e7d651b..8f07e7e 100644
--- a/toolkit/components/alerts/Makefile.in
+++ b/toolkit/components/alerts/Makefile.in
@@ -50,12 +50,14 @@ DIRS += src
 endif
 
 ifneq (,$(filter cocoa, $(MOZ_WIDGET_TOOLKIT)))
+ifndef BUILD_STATIC_LIBS
 # src/mac/growl needs to be first for linking to work!
 DIRS += \
   src/mac/growl \
   src/mac \
   $(NULL)
 endif
+endif
 
 ifdef ENABLE_TESTS
 DIRS  += test
diff --git a/toolkit/components/alerts/src/mac/Makefile.in b/toolkit/components/alerts/src/mac/Makefile.in
index fb524dc..91bffd0 100644
--- a/toolkit/components/alerts/src/mac/Makefile.in
+++ b/toolkit/components/alerts/src/mac/Makefile.in
@@ -46,7 +46,6 @@ LIBRARY_NAME = alerts_s
 IS_COMPONENT = 1
 FORCE_SHARED_LIB = 1
 
-
 CMMSRCS = \
   nsAlertsService.mm \
   mozGrowlDelegate.mm \
diff --git a/toolkit/library/Makefile.in b/toolkit/library/Makefile.in
index 07f08ac..49c4dd3 100644
--- a/toolkit/library/Makefile.in
+++ b/toolkit/library/Makefile.in
@@ -50,9 +50,14 @@ include $(topsrcdir)/intl/unicharutil/util/objs.mk
 MODULE = libxul
 LIBRARY_NAME = xul
 FORCE_USE_PIC = 1
+ifndef BUILD_STATIC_LIBS
 FORCE_SHARED_LIB = 1
+else
+DIST_INSTALL = 1
+endif
 MOZILLA_INTERNAL_API = 1
 
+ifndef BUILD_STATIC_LIBS
 ifeq ($(MOZ_WIDGET_TOOLKIT),cocoa)
 # This is going to be a framework named "XUL", not an ordinary library named
 # "libxul.dylib"
@@ -62,7 +67,7 @@ LIBRARY_NAME=XUL
 # as-is so that dependencies of the form -ltracemalloc still work.
 MAKE_FRAMEWORK=1
 endif
-
+endif
 
 ifeq ($(OS_ARCH)_$(GNU_CC),WINNT_)
 CPPSRCS += dlldeps-xul.cpp
@@ -76,6 +81,7 @@ ifeq ($(OS_ARCH),OS2)
 CPPSRCS += dlldeps-xul.cpp
 endif
 
+ifndef BUILD_STATIC_LIBS
 # dependent libraries
 ifneq (,$(MOZ_ENABLE_GTK2))
 SHARED_LIBRARY_LIBS += \
@@ -91,6 +97,11 @@ SHARED_LIBRARY_LIBS += \
 	$(foreach component,$(COMPONENT_LIBS),$(DEPTH)/staticlib/components/$(LIB_PREFIX)$(component).$(LIB_SUFFIX)) \
 	$(foreach lib,$(STATIC_LIBS),$(DEPTH)/staticlib/$(LIB_PREFIX)$(lib).$(LIB_SUFFIX)) \
 	$(NULL)
+else
+ifneq (,$(MOZ_ENABLE_GTK2))
+DEFINES += -DMOZ_ENABLE_GTK2
+endif
+endif
 
 ifdef MOZ_JAVAXPCOM
 LOCAL_INCLUDES += \
diff --git a/toolkit/static-postxpcom-tiers.mk b/toolkit/static-postxpcom-tiers.mk
new file mode 100644
index 0000000..d5cc658
--- /dev/null
+++ b/toolkit/static-postxpcom-tiers.mk
@@ -0,0 +1,83 @@
+# ***** 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 the Mozilla build system.
+#
+# The Initial Developer of the Original Code is
+# the Mozilla Foundation <http://www.mozilla.org/>.
+# Portions created by the Initial Developer are Copyright (C) 2006
+# the Initial Developer. All Rights Reserved.
+#
+# Contributor(s):
+#   Benjamin Smedberg <benjamin@smedbergs.us> (Initial Code)
+#
+# 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 *****
+
+TIERS += postxpcom 
+
+# tier "postxpcom" - everything that depends on the xpcom shared lib
+
+ifdef BUILD_STATIC_LIBS
+tier_postxpcom_dirs = xpcom/stub 
+
+ifneq ($(MOZ_BUILD_APP), xulrunner)
+tier_postxpcom_dirs += $(MOZ_BUILD_APP)/components
+endif
+ 
+ifdef MOZ_ENABLE_GNOME_COMPONENT
+tier_postxpcom_dirs += toolkit/system/gnome
+endif
+
+ifndef MOZ_ENABLE_LIBCONIC
+ifdef MOZ_ENABLE_DBUS
+tier_postxpcom_dirs += toolkit/system/dbus
+endif
+endif
+
+ifdef MOZ_EXTENSIONS
+tier_postxpcom_dirs += extensions
+endif
+
+ifeq ($(MOZ_WIDGET_TOOLKIT),gtk2)
+tier_postxpcom_dirs += 	modules/plugin/sdk/samples/unixprinting
+endif
+
+ifdef MOZ_ENABLE_GTK2
+tier_postxpcom_dirs += modules/plugin/default/unix
+endif
+
+ifeq ($(MOZ_BUILD_APP), xulrunner)
+tier_postxpcom_dirs += $(MOZ_BUILD_APP)/examples
+endif
+
+ifneq (,$(filter cocoa, $(MOZ_WIDGET_TOOLKIT)))
+# src/mac/growl needs to be first for linking to work!
+tier_postxpcom_dirs += \
+	toolkit/components/alerts/src/mac/growl \
+	toolkit/components/alerts/src/mac \
+	$(NULL)
+endif
+
+endif # BUILD_STATIC_LIBS
diff --git a/toolkit/toolkit-tiers.mk b/toolkit/toolkit-tiers.mk
index 012f915..34b59ed 100644
--- a/toolkit/toolkit-tiers.mk
+++ b/toolkit/toolkit-tiers.mk
@@ -228,11 +228,9 @@ tier_toolkit_dirs += embedding/browser/gtk
 endif
 endif
 
-ifndef BUILD_STATIC_LIBS
 tier_toolkit_dirs += toolkit/library
-endif
 
-ifdef MOZ_ENABLE_LIBXUL
+ifndef BUILD_STATIC_LIBS
 tier_toolkit_dirs += xpcom/stub
 endif
 
@@ -240,6 +238,7 @@ ifdef NS_TRACE_MALLOC
 tier_toolkit_dirs += tools/trace-malloc
 endif
 
+ifndef BUILD_STATIC_LIBS
 ifdef MOZ_ENABLE_GNOME_COMPONENT
 tier_toolkit_dirs    += toolkit/system/gnome
 endif
@@ -250,6 +249,7 @@ ifdef MOZ_ENABLE_DBUS
 tier_toolkit_dirs    += toolkit/system/dbus
 endif
 endif
+endif
 
 ifdef MOZ_LEAKY
 tier_toolkit_dirs        += tools/leaky
diff --git a/toolkit/xre/Makefile.in b/toolkit/xre/Makefile.in
index 2255af8..824e550 100644
--- a/toolkit/xre/Makefile.in
+++ b/toolkit/xre/Makefile.in
@@ -47,12 +47,12 @@ MODULE = xulapp
 LIBRARY_NAME = xulapp_s
 LIBXUL_LIBRARY = 1
 
-
-
-
-
 FORCE_STATIC_LIB = 1
 
+ifdef BUILD_STATIC_LIBS
+MOZILLA_INTERNAL_API = 1
+endif
+
 XPIDLSRCS = \
 	nsINativeAppSupport.idl \
 	$(NULL)
diff --git a/xpcom/build/Makefile.in b/xpcom/build/Makefile.in
index e4438ba..00df425 100644
--- a/xpcom/build/Makefile.in
+++ b/xpcom/build/Makefile.in
@@ -51,9 +51,8 @@ LIBRARY_NAME	= xpcom_core
 SHORT_LIBNAME	= xpcomcor
 LIBXUL_LIBRARY = 1
 
-# This is only a static library in libxul builds
-ifdef MOZ_ENABLE_LIBXUL
-EXPORT_LIBRARY = 1
+ifdef BUILD_STATIC_LIBS
+DIST_INSTALL = 1
 endif
 
 GRE_MODULE	= 1
diff --git a/xpcom/stub/Makefile.in b/xpcom/stub/Makefile.in
index cea7973..d81aa12 100644
--- a/xpcom/stub/Makefile.in
+++ b/xpcom/stub/Makefile.in
@@ -74,12 +74,24 @@ FORCE_SHARED_LIB = 1
 
 EXTRA_DSO_LDOPTS = $(LIBS_DIR)
 
+# Don't want -Wl,-z,defs
+
+ifdef BUILD_STATIC_LIBS
+ifeq ($(OS_TARGET),Linux)
+DSO_LDOPTS = -shared
+endif
+endif
+
 DEPENDENT_LIBS_LIST += \
 	$(LIB_PREFIX)nspr4$(DLL_SUFFIX) \
 	$(LIB_PREFIX)plc4$(DLL_SUFFIX) \
 	$(LIB_PREFIX)plds4$(DLL_SUFFIX) \
 	$(NULL)
 
+ifdef BUILD_STATIC_LIBS
+USE_STATIC_LIBS = 1
+endif
+
 ifdef MOZ_ENABLE_LIBXUL
 
 DEPENDENT_LIBS_LIST += \
@@ -97,6 +109,9 @@ else
 DEPENDENT_LIBS_LIST += $(LIB_PREFIX)mozsqlite3$(DLL_SUFFIX)
 endif
 
+# We want libxul.a when building static
+
+ifndef BUILD_STATIC_LIBS
 ifeq (bundle,$(MOZ_FS_LAYOUT))
 EXTRA_DSO_LDOPTS += $(DIST)/bin/XUL
 DEPENDENT_LIBS_LIST += XUL
@@ -104,6 +119,7 @@ else
 EXTRA_DSO_LIBS = xul
 DEPENDENT_LIBS_LIST += $(LIB_PREFIX)xul$(DLL_SUFFIX)
 endif
+endif
 
 else #!MOZ_ENABLE_LIBXUL
 
@@ -116,6 +132,16 @@ endif
 
 endif
 
+ifdef BUILD_STATIC_LIBS
+ifeq ($(OS_TARGET),Darwin)
+EXTRA_DSO_LDOPTS += -undefined dynamic_lookup
+endif
+ifneq (, $(filter WINNT WINCE, $(OS_ARCH)))
+EXTRA_DSO_LIBS += \
+	$(DEPTH)/$(MOZ_BUILD_APP)/app/$(LIB_PREFIX)$(MOZ_APP_NAME)
+endif
+endif
+
 EXTRA_DSO_LDOPTS += $(EXTRA_DSO_LIBS) $(NSPR_LIBS)
 
 include $(topsrcdir)/config/rules.mk
diff --git a/xulrunner/Makefile.in b/xulrunner/Makefile.in
index 46406ba..e040f5c 100644
--- a/xulrunner/Makefile.in
+++ b/xulrunner/Makefile.in
@@ -47,9 +47,12 @@ PARALLEL_DIRS = \
   app \
   setup \
   stub \
-  examples \
   $(NULL)
 
+ifndef BUILD_STATIC_LIBS
+PARALLEL_DIRS += examples
+endif
+
 ifeq ($(OS_ARCH),WINNT)
 ifdef MOZ_INSTALLER
 DIRS += installer/windows
diff --git a/xulrunner/app/Makefile.in b/xulrunner/app/Makefile.in
index d166ed6..e3ec0fd 100644
--- a/xulrunner/app/Makefile.in
+++ b/xulrunner/app/Makefile.in
@@ -96,6 +96,7 @@ ifeq (cocoa,$(MOZ_WIDGET_TOOLKIT))
 TK_LIBS := -framework Cocoa $(TK_LIBS)
 endif
 
+ifndef BUILD_STATIC_LIBS
 ifndef MOZ_ENABLE_LIBXUL
 ifeq (bundle,$(MOZ_FS_LAYOUT))
 LIBS += $(DIST)/bin/XUL
@@ -103,11 +104,23 @@ else
 EXTRA_DSO_LIBS += xul
 endif
 endif
+else
+MOZILLA_INTERNAL_API = 1
+XPCOM_LIBS = 
+endif
 
 LIBS += \
 	$(EXTRA_DSO_LIBS) \
 	$(MOZ_JS_LIBS) \
-	$(DIST)/lib/$(LIB_PREFIX)xpcomglue_s.$(LIB_SUFFIX) \
+	$(NULL)
+
+ifndef BUILD_STATIC_LIBS
+LIBS += $(DIST)/lib/$(LIB_PREFIX)xpcomglue_s.$(LIB_SUFFIX)
+else
+LIBS += $(DEPTH)/toolkit/xre/$(LIB_PREFIX)xulapp_s.$(LIB_SUFFIX)
+endif
+
+LIBS += \
 	$(XPCOM_LIBS) \
 	$(NSPR_LIBS) \
 	$(TK_LIBS) \
@@ -147,6 +160,76 @@ WIN32_EXE_LDFLAGS += -ENTRY:wmainCRTStartup
 endif
 endif
 
+ifdef BUILD_STATIC_LIBS
+include $(topsrcdir)/config/static-config.mk
+
+STATIC_EXTRA_LIBS += $(MOZ_ZLIB_LIBS) 
+STATIC_CPPSRCS =
+
+ifeq ($(OS_ARCH), Darwin)
+MOZ_OPTIMIZE_LDFLAGS =
+endif
+
+ifdef MOZ_ENABLE_DBUS
+LIBS += $(MOZ_DBUS_GLIB_LIBS)
+endif
+
+ifdef MOZ_PLATFORM_MAEMO
+LIBS += $(MOZ_PLATFORM_MAEMO_LIBS)
+endif
+
+ifdef MOZ_ENABLE_LIBCONIC
+LIBS += $(LIBCONIC_LIBS)
+endif
+
+ifdef MOZ_ENABLE_GCONF
+LIBS += $(MOZ_GCONF_LIBS)
+endif
+
+ifdef MOZ_ENABLE_GNOMEVFS
+LIBS += $(MOZ_GNOMEVFS_LIBS)
+endif
+
+ifdef MOZ_ENABLE_LIBNOTIFY
+LIBS += $(MOZ_LIBNOTIFY_LIBS)
+endif
+
+ifdef MOZ_ENABLE_GIO
+LIBS += $(MOZ_GIO_LIBS)
+endif
+
+# Assume both FORCE and UNFORCE are defined
+
+ifdef MKSHLIB_FORCE_ALL
+LIBS += \
+        $(MKSHLIB_FORCE_ALL) \
+	$(DIST)/lib/$(LIB_PREFIX)xul.$(LIB_SUFFIX) \
+	$(DIST)/lib/$(LIB_PREFIX)xpcom_core.$(LIB_SUFFIX) \
+        $(MKSHLIB_UNFORCE_ALL) \
+	$(NULL)
+else
+FORCE_LOAD_LIBS += \
+	$(DIST)/lib/$(LIB_PREFIX)xul.$(LIB_SUFFIX) \
+	$(DIST)/lib/$(LIB_PREFIX)xpcom_core.$(LIB_SUFFIX) \
+	$(NULL)
+endif
+
+EXTRA_DEPS	+= \
+	$(STATIC_EXTRA_DEPS) \
+	$(NULL)
+DEFINES		+= $(STATIC_DEFINES)
+CPPSRCS		+= $(STATIC_CPPSRCS)
+EXTRA_DSO_LIBS	+= $(STATIC_EXTRA_DSO_LIBS)
+EXTRA_LIBS	+= $(STATIC_EXTRA_LIBS)
+endif
+
+ifdef BUILD_STATIC_LIBS
+ifeq ($(OS_ARCH),WINCE)
+EXTRA_LIBS += $(FT2_LIBS)
+OS_LIBS += $(call EXPAND_LIBNAME, aygshell cellcore ole32 ws2 ddraw)
+endif
+endif
+
 ifeq ($(OS_ARCH),WINNT)
 OS_LIBS += $(call EXPAND_LIBNAME,comctl32 comdlg32 uuid shell32 ole32 oleaut32 version winspool)
 RCINCLUDE = splash.rc
@@ -177,6 +260,14 @@ LIBS += $(JEMALLOC_LIBS)
 
 include $(topsrcdir)/config/rules.mk
 
+ifdef BUILD_STATIC_LIBS
+include $(topsrcdir)/config/static-rules.mk
+
+ifeq ($(OS_ARCH),Linux)
+LDFLAGS += -rdynamic
+endif
+endif
+
 DEFINES += -DXULRUNNER_ICO=\"$(DIST)/branding/xulrunner.ico\" -DDOCUMENT_ICO=\"$(DIST)/branding/document.ico\"
 
 ifeq ($(MOZ_WIDGET_TOOLKIT),photon)
diff --git a/xulrunner/app/nsRegisterGRE.h b/xulrunner/app/nsRegisterGRE.h
index e4bfd8e..ff0ae1b 100755
--- a/xulrunner/app/nsRegisterGRE.h
+++ b/xulrunner/app/nsRegisterGRE.h
@@ -39,7 +39,7 @@
 #define nsRegisterGRE_h__
 
 #include "nscore.h"
-#include "nsStringAPI.h"
+#include "nsStringGlue.h"
 class nsIFile;
 struct GREProperty;
 
diff --git a/xulrunner/app/nsRegisterGREUnix.cpp b/xulrunner/app/nsRegisterGREUnix.cpp
index 298e5da..932cf3f 100755
--- a/xulrunner/app/nsRegisterGREUnix.cpp
+++ b/xulrunner/app/nsRegisterGREUnix.cpp
@@ -43,7 +43,7 @@
 #include "nsILocalFile.h"
 
 #include "nsAppRunner.h" // for MAXPATHLEN
-#include "nsStringAPI.h"
+#include "nsStringGlue.h"
 #include "nsINIParser.h"
 #include "nsCOMPtr.h"
 
diff --git a/xulrunner/app/nsRegisterGREWin.cpp b/xulrunner/app/nsRegisterGREWin.cpp
index 46e1ef2..5ea1184 100644
--- a/xulrunner/app/nsRegisterGREWin.cpp
+++ b/xulrunner/app/nsRegisterGREWin.cpp
@@ -42,7 +42,7 @@
 #include "nsILocalFile.h"
 
 #include "nsAppRunner.h" // for MAXPATHLEN
-#include "nsStringAPI.h"
+#include "nsStringGlue.h"
 #include "nsXPCOMGlue.h"
 #include "nsCOMPtr.h"
 
diff --git a/xulrunner/app/nsXULRunnerApp.cpp b/xulrunner/app/nsXULRunnerApp.cpp
index 21670d9..c3f02d6 100644
--- a/xulrunner/app/nsXULRunnerApp.cpp
+++ b/xulrunner/app/nsXULRunnerApp.cpp
@@ -50,7 +50,8 @@
 #include "nsCOMPtr.h"
 #include "nsMemory.h"
 #include "nsCRTGlue.h"
-#include "nsStringAPI.h"
+#include "nsStringGlue.h"
+#include "nsXPCOMStrings.h"
 #include "nsServiceManagerUtils.h"
 #include "plstr.h"
 #include "prprf.h"
diff --git a/xulrunner/build.mk b/xulrunner/build.mk
index c50bf1e..867a64e 100644
--- a/xulrunner/build.mk
+++ b/xulrunner/build.mk
@@ -68,6 +68,10 @@ endif
 
 tier_app_dirs += xulrunner
 
+ifdef BUILD_STATIC_LIBS
+include $(topsrcdir)/toolkit/static-postxpcom-tiers.mk
+endif
+
 installer:
 	@echo "XULRunner doesn't have an installer yet."
 
diff --git a/xulrunner/confvars.sh b/xulrunner/confvars.sh
index 90bcb5b..59f776d 100755
--- a/xulrunner/confvars.sh
+++ b/xulrunner/confvars.sh
@@ -41,9 +41,11 @@ MOZ_APP_DISPLAYNAME=XULRunner
 MOZ_UPDATER=1
 MOZ_XULRUNNER=1
 MOZ_ENABLE_LIBXUL=1
-MOZ_STATIC_BUILD_UNSUPPORTED=1
+MOZ_STATIC_BUILD_UNSUPPORTED=
+BUILD_STATIC_LIBS=1
 MOZ_APP_VERSION=$MOZILLA_VERSION
-MOZ_JAVAXPCOM=1
+MOZ_JAVAXPCOM=
+
 if test "$MOZ_STORAGE"; then
   MOZ_PLACES=1
 fi
-- 
1.6.4.1

