diff --git a/b2g_product.mk b/b2g_product.mk
index f17e35f18dd2d27cd314dc3859484988f0761fbd..3fa8e23d620176cf3e0d123bad0bf651ee2defd7 100644
--- a/b2g_product.mk
+++ b/b2g_product.mk
@@ -65,6 +65,37 @@ CLEAR_XPCOM_VARS:=$(dir $(firstword $(MAKEFILE_LIST)))clear_xpcom_vars.mk
 BUILD_XPCOM:=$(dir $(firstword $(MAKEFILE_LIST)))xpcom.mk
 
 
+# Define JS_BINARY to enable minification of Gecko chrome javscript files
+#
+# JS_BINARY is used by the Gecko build system to verify that minified chrome
+# Javscript code is still correct.  Unfortunately the normal "js binary" is not
+# a part of the source tree and it would be undesirable to add it as a
+# dependency.  This block instead adapts the xpcshell Javscript runtime, used by
+# build Gaia, for this purpose.
+#
+export JS_BINARY=$(abspath out/js_binary.sh)
+ifeq ($(wildcard $(JS_BINARY)),)
+
+  XPCSHELL=$(shell $(MAKE) -s -C gaia xpcshell_sdk)
+  ifeq ($(wildcard $(XPCSHELL)),)
+    $(error Unabled to find xpcshell.  Best guess: $(XPCSHELL))
+  endif
+  RUN_MOZILLA=$(dir $(XPCSHELL))run-mozilla.sh
+  ifeq ($(wildcard $(RUN_MOZILLA)),)
+    $(error Unabled to find run-mozilla.sh.  Best guess: $(RUN_MOZILLA))
+  endif
+
+  # This specialized JS_BINARY is expected to receive three arguments:
+  #   1 - Validation script
+  #   2 - Javacript file 1
+  #   3 - Javacript file 2
+  $(shell mkdir -p $(dir $(JS_BINARY)))
+  $(shell echo '#!/bin/bash' >> $(JS_BINARY))
+  $(shell echo 'exec $(RUN_MOZILLA) $(XPCSHELL) -e "scriptArgs=[$$2, $$3];" -f $$1' >> $(JS_BINARY))
+  $(shell chmod +x $(JS_BINARY))
+endif
+
+
 # GAIA_DISTRIBUTION_DIR enables overrides for Gaia settings
 ifndef GAIA_DISTRIBUTION_DIR
 GAIA_DISTRIBUTION_DIR:=$(ANDROID_BUILD_TOP)/out/target/product/$(TARGET_PRODUCT)/gaia_distribution
diff --git a/vendorsetup.sh b/vendorsetup.sh
index 624889bb78bf57859c92d70141f13f8618e8cf54..1f998cddb6fc84307679d2359d12c9d85eb1059b 100644
--- a/vendorsetup.sh
+++ b/vendorsetup.sh
@@ -312,6 +312,19 @@ rungdb()
    ( cd $(gettop)/device/qcom/b2g_common && ./run-gdb.sh $@ )
 }
 
+
+# Use a local B2G SDK if possible instead of downloading it
+if [[ -d prebuilts/b2g_sdk/$(uname)/.git ]]; then
+   export USE_LOCAL_XULRUNNER_SDK=1
+   export XULRUNNER_DIRECTORY="$(gettop)/prebuilts/b2g_sdk/$(uname)"
+else
+   echo =================================================================
+   echo Notice: Local B2G SDK was not found, fetching it now if necessary
+   echo =================================================================
+   make -s -C gaia b2g_sdk
+fi
+
+
 if [[ -z $1 ]]; then
    __patch_tree
 else
@@ -323,12 +336,6 @@ else
    esac
 fi
 
-# Use a local B2G SDK if present instead of downloading a new copy
-if [[ -d prebuilts/b2g_sdk/$(uname)/.git ]]; then
-   export USE_LOCAL_XULRUNNER_SDK=1
-   export XULRUNNER_DIRECTORY="$(gettop)/prebuilts/b2g_sdk/$(uname)"
-fi
-
 # Don't build sources.xml
 export DISABLE_SOURCES_XML=true
 export LEGACY_USE_JAVA6=true