From: Mike Hommey <glandium@debian.org>
Date: Thu, 17 Sep 2009 20:23:40 +0200
Subject: Properly build twice in a row

Picked from upstream:
http://hg.mozilla.org/mozilla-central/rev/dc688b5346a6
http://hg.mozilla.org/mozilla-central/rev/ea23ed5288e6
---
 js/src/Makefile.in |   31 +++++++++++++++++--------------
 1 files changed, 17 insertions(+), 14 deletions(-)

diff --git a/js/src/Makefile.in b/js/src/Makefile.in
index 1ef703d..95a59fb 100644
--- a/js/src/Makefile.in
+++ b/js/src/Makefile.in
@@ -645,23 +645,26 @@ $(CPPSRCS:%.cpp=%.$(OBJ_SUFFIX)): $(CURDIR)/javascript-trace.h
 endif
 
 ifdef GNU_CC
+ifndef CROSS_COMPILE
+# If we don't have run-mozilla.sh *and* we're linking against NSPR, we don't
+# know how to run the JS binary. Oh well
+
 imacro_asm.js: imacro_asm.js.in jsopcode.tbl
 	$(CC) -c -x c -E -P -I$(srcdir) $< > $@
 
-GARBAGE += imacros.c.out imacro_asm.js
+GARBAGE += imacros.c.tmp imacro_asm.js
 
-ifndef CROSS_COMPILE
-# Obsolete comment: if you replace update-imacros with libs, this would apply
-# but then parallel gmake would sometimes try to run $(DIST)/bin/js before it
-# had been built and installed. Want a fix for this, de-automating for now.
-
-# Build imacros.c.out after descending into DIRS and building the js shell.
-# This may result in an updated imacros.c.out file that requires a re-build
-# to stabilize.
-update-imacros:: imacros.c.out
-	@cmp -s imacros.c.out $(srcdir)/$< || cp imacros.c.out $(srcdir)
-
-%.c.out: %.jsasm imacro_asm.js
-	$(DIST)/bin/js imacro_asm.js $< > $@
+ifneq (,$(wildcard $(RUN_TEST_PROGRAM))$(if $(NSPR_LIBS),,1))
+libs:: imacro_asm.js $(srcdir)/imacros.jsasm
+	$(wildcard $(RUN_TEST_PROGRAM)) $(DIST)/bin/js$(BIN_SUFFIX) $< $(srcdir)/imacros.jsasm > imacros.c.tmp
+	@cmp -s imacros.c.tmp $(srcdir)/imacros.c.out || \
+	  (echo "imacros.c.out is out of date. Run 'make update-imacros' to copy it to your source tree."; diff -U 4 $(srcdir)/imacros.c.out imacros.c.out; exit 1)
+	@echo "imacros.c.out is up to date"
+
+update-imacros: imacros.c.tmp
+	cp $< $(srcdir)/imacros.c.out
+
+.PHONY: update-imacros
+endif
 endif
 endif
