Bug 1220731 - Refactor embedjs script for use from moz.build rather than makefiles r=shu r=glandium
authorJon Coppeard <jcoppeard@mozilla.com>
Fri, 06 Nov 2015 13:09:01 +0000
changeset 307554 6d7d90a28e057220e59988c6fca3ed5f20bacea3
parent 307553 23f47084749fb6bba5723996c7f2fd11aa65b352
child 307555 64d766f1523741587f7a2c17ceea4fa86599de99
push id1040
push userraliiev@mozilla.com
push dateMon, 29 Feb 2016 17:11:22 +0000
treeherdermozilla-release@8c3167321162 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersshu, glandium
bugs1220731
milestone45.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1220731 - Refactor embedjs script for use from moz.build rather than makefiles r=shu r=glandium
config/rules.mk
configure.in
js/src/Makefile.in
js/src/builtin/embedjs.py
js/src/configure.in
js/src/moz.build
--- a/config/rules.mk
+++ b/config/rules.mk
@@ -499,20 +499,18 @@ IFLAGS1 = -rb
 IFLAGS2 = -rb
 else
 IFLAGS1 = -m 644
 IFLAGS2 = -m 755
 endif
 
 ifeq (_WINNT,$(GNU_CC)_$(OS_ARCH))
 OUTOPTION = -Fo# eol
-PREPROCESS_OPTION = -P -Fi# eol
 else
 OUTOPTION = -o # eol
-PREPROCESS_OPTION = -E -o #eol
 endif # WINNT && !GNU_CC
 
 ifneq (,$(filter ml%,$(AS)))
 ASOUTOPTION = -Fo# eol
 else
 ASOUTOPTION = -o # eol
 endif
 
--- a/configure.in
+++ b/configure.in
@@ -9140,16 +9140,28 @@ HAVE_STATFS64
 HAVE_STATFS
 HAVE_SYS_STATVFS_H
 HAVE_SYS_STATFS_H
 HAVE_SYS_VFS_H
 HAVE_SYS_MOUNT_H
 "
 
 dnl ========================================================
+dnl Determine options to use for running the preprocessor.
+dnl ========================================================
+
+if test -z "$GNU_CC" -a "$OS_ARCH" = "WINNT"; then
+    PREPROCESS_OPTION="-P -Fi"
+else
+    PREPROCESS_OPTION="-E -o "
+fi
+
+AC_SUBST(PREPROCESS_OPTION)
+
+dnl ========================================================
 dnl ICU Support
 dnl ========================================================
 
 # Internationalization is not built or exposed on Fennec.
 # See Bug 1215256
 
 if test "$MOZ_BUILD_APP" = "mobile/android"; then
     _INTL_API=no
--- a/js/src/Makefile.in
+++ b/js/src/Makefile.in
@@ -272,57 +272,16 @@ ifdef HAVE_DTRACE
 	    -e '/const/!s/char \*/const char */g' \
 	    javascript-trace.h.in > javascript-trace.h
 
 # We can't automatically generate dependencies on auto-generated headers;
 # we have to list them explicitly.
 $(addsuffix .$(OBJ_SUFFIX),Probes jsinterp jsobj): $(CURDIR)/javascript-trace.h
 endif
 
-# Prepare self-hosted JS code for embedding
-export:: selfhosting
-selfhosting:: selfhosted.out.h
-
-selfhosting_srcs := \
-  $(srcdir)/builtin/Utilities.js \
-  $(srcdir)/builtin/Array.js \
-  $(srcdir)/builtin/Date.js \
-  $(srcdir)/builtin/Error.js \
-  $(srcdir)/builtin/Generator.js \
-  $(srcdir)/builtin/Intl.js \
-  $(srcdir)/builtin/IntlData.js \
-  $(srcdir)/builtin/Iterator.js \
-  $(srcdir)/builtin/Map.js \
-  $(srcdir)/builtin/Module.js \
-  $(srcdir)/builtin/Number.js \
-  $(srcdir)/builtin/Object.js \
-  $(srcdir)/builtin/Reflect.js \
-  $(srcdir)/builtin/RegExp.js \
-  $(srcdir)/builtin/String.js \
-  $(srcdir)/builtin/Set.js \
-  $(srcdir)/builtin/TypedArray.js \
-  $(srcdir)/builtin/TypedObject.js \
-  $(srcdir)/builtin/WeakSet.js \
-  $(NULL)
-
-selfhosted_out_h_deps := \
-  $(selfhosting_srcs) \
-  $(srcdir)/js.msg \
-  $(srcdir)/builtin/embedjs.py \
-  $(srcdir)/builtin/TypedObjectConstants.h \
-  $(srcdir)/builtin/SelfHostingDefines.h \
-  $(NULL)
-
-SELFHOSTED_DEFINES += $(DEFINES) $(ACDEFINES) $(MOZ_DEBUG_DEFINES)
-
-selfhosted.out.h: $(selfhosted_out_h_deps)
-	$(PYTHON) $(srcdir)/builtin/embedjs.py $(SELFHOSTED_DEFINES) \
-	  -c '$(CCC)' -p '$(PREPROCESS_OPTION)' -m $(srcdir)/js.msg \
-	  -o $@ $(selfhosting_srcs)
-
 ###############################################
 # Generating source package tarballs
 # (only possible when tar is found)
 ifneq (,$(TAR))
 
 source-package:
 	SRCDIR=$(srcdir) \
 	DIST=$(DIST) \
--- a/js/src/builtin/embedjs.py
+++ b/js/src/builtin/embedjs.py
@@ -32,20 +32,20 @@
 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
 # This utility converts JS files containing self-hosted builtins into a C
 # header file that can be embedded into SpiderMonkey.
 #
 # It uses the C preprocessor to process its inputs.
 
 from __future__ import with_statement
-import re, sys, os, fileinput, subprocess
+import re, sys, os, subprocess
 import shlex
 import which
-from optparse import OptionParser
+import buildconfig
 
 def ToCAsciiArray(lines):
   result = []
   for chr in lines:
     value = ord(chr)
     assert value < 128
     result.append(str(value))
   return ", ".join(result)
@@ -80,29 +80,27 @@ namespace selfhosted {
 
 def embed(cxx, preprocessorOption, msgs, sources, c_out, js_out, env):
   combinedSources = '\n'.join([msgs] + ['#include "%(s)s"' % { 's': source } for source in sources])
   args = ['-D%(k)s=%(v)s' % { 'k': k, 'v': env[k] } for k in env]
   preprocessed = preprocess(cxx, preprocessorOption, combinedSources, args)
   processed = '\n'.join([line for line in preprocessed.splitlines() if \
                          (line.strip() and not line.startswith('#'))])
 
-  with open(js_out, 'w') as output:
-    output.write(processed)
-  with open(c_out, 'w') as output:
-    import zlib
-    compressed = zlib.compress(processed)
-    data = ToCArray(compressed)
-    output.write(HEADER_TEMPLATE % {
-        'sources_type': 'unsigned char',
-        'sources_data': data,
-        'sources_name': 'compressedSources',
-        'compressed_total_length': len(compressed),
-        'raw_total_length': len(processed)
-    })
+  js_out.write(processed)
+  import zlib
+  compressed = zlib.compress(processed)
+  data = ToCArray(compressed)
+  c_out.write(HEADER_TEMPLATE % {
+    'sources_type': 'unsigned char',
+    'sources_data': data,
+    'sources_name': 'compressedSources',
+    'compressed_total_length': len(compressed),
+    'raw_total_length': len(processed)
+  })
 
 def preprocess(cxx, preprocessorOption, source, args = []):
   if (not os.path.exists(cxx[0])):
     cxx[0] = which.which(cxx[0])
   # Clang seems to complain and not output anything if the extension of the
   # input is not something it recognizes, so just fake a .cpp here.
   tmpIn = 'self-hosting-cpp-input.cpp';
   tmpOut = 'self-hosting-preprocessed.pp';
@@ -126,37 +124,32 @@ def messages(jsmsg):
     match = re.match("MSG_DEF\((JSMSG_(\w+))", line)
     if match:
       defines.append("#define %s %i" % (match.group(1), len(defines)))
     else:
       # Make sure that MSG_DEF isn't preceded by whitespace
       assert not line.strip().startswith("MSG_DEF")
   return '\n'.join(defines)
 
-def main():
-  env = {}
-  def define_env(option, opt, value, parser):
-    pair = value.split('=', 1)
+def get_config_defines(buildconfig):
+  # Collect defines equivalent to ACDEFINES and add MOZ_DEBUG_DEFINES.
+  env = {key: value for key, value in buildconfig.defines.iteritems()
+         if key not in buildconfig.non_global_defines}
+  for value in buildconfig.substs['MOZ_DEBUG_DEFINES'].split():
+    assert value[:2] == "-D"
+    pair = value[2:].split('=', 1)
     if len(pair) == 1:
       pair.append(1)
     env[pair[0]] = pair[1]
-  p = OptionParser(usage="%prog [options] file")
-  p.add_option('-D', action='callback', callback=define_env, type="string",
-               metavar='var=[val]', help='Define a variable')
-  p.add_option('-m', type='string', metavar='jsmsg', default='../js.msg',
-               help='js.msg file')
-  p.add_option('-c', type='string', metavar='cxx', help='Path to C++ compiler')
-  p.add_option('-p', type='string', dest='p', metavar='cxxoption',
-               help='Argument to compiler for preprocessing into an output file')
-  p.add_option('-o', type='string', metavar='filename', default='selfhosted.out.h',
-               help='C array header file')
-  p.add_option('-s', type='string', metavar='jsfilename', default='selfhosted.js',
-               help='Combined postprocessed JS file')
-  (options, sources) = p.parse_args()
-  if not (options.p and sources):
-    p.print_help()
-    sys.exit(1)
-  cxx = shlex.split(options.c)
-  msgs = messages(options.m)
-  embed(cxx, options.p, msgs, sources, options.o, options.s, env)
+  return env
 
-if __name__ == "__main__":
-  main()
+def generate_selfhosted(c_out, msg_file, *inputs):
+  # Called from moz.build to embed selfhosted JS.
+  deps = [path for path in inputs if path.endswith(".h")]
+  sources = [path for path in inputs if path.endswith(".js")]
+  assert len(deps) + len(sources) == len(inputs)
+  cxx = shlex.split(buildconfig.substs['CXX'])
+  cxx_option = buildconfig.substs['PREPROCESS_OPTION']
+  env = get_config_defines(buildconfig)
+  js_path = re.sub(r"\.out\.h$", "", c_out.name) + ".js"
+  msgs = messages(msg_file)
+  with open(js_path, 'w') as js_out:
+    embed(cxx, cxx_option, msgs, sources, c_out, js_out, env)
--- a/js/src/configure.in
+++ b/js/src/configure.in
@@ -3805,16 +3805,27 @@ if test "$ACCESSIBILITY" -a "$MOZ_ENABLE
     ATK_MAJOR_VERSION=`echo ${ATK_FULL_VERSION} | $AWK -F\. '{ print $1 }'`
     ATK_MINOR_VERSION=`echo ${ATK_FULL_VERSION} | $AWK -F\. '{ print $2 }'`
     ATK_REV_VERSION=`echo ${ATK_FULL_VERSION} | $AWK -F\. '{ print $3 }'`
     AC_DEFINE_UNQUOTED(ATK_MAJOR_VERSION, $ATK_MAJOR_VERSION)
     AC_DEFINE_UNQUOTED(ATK_MINOR_VERSION, $ATK_MINOR_VERSION)
     AC_DEFINE_UNQUOTED(ATK_REV_VERSION, $ATK_REV_VERSION)
 fi
 
+dnl ========================================================
+dnl Determine options to use for running the preprocessor.
+dnl ========================================================
+
+if test -z "$GNU_CC" -a "$OS_ARCH" = "WINNT"; then
+    PREPROCESS_OPTION="-P -Fi"
+else
+    PREPROCESS_OPTION="-E -o "
+fi
+
+AC_SUBST(PREPROCESS_OPTION)
 
 dnl ========================================================
 dnl ECMAScript Internationalization API Support (uses ICU)
 dnl ========================================================
 
 dnl top-level configure may override this with --without-intl-api
 _INTL_API=yes
 
--- a/js/src/moz.build
+++ b/js/src/moz.build
@@ -664,8 +664,37 @@ CFLAGS += CONFIG['MOZ_ICU_CFLAGS']
 CXXFLAGS += CONFIG['MOZ_ICU_CFLAGS']
 
 if not CONFIG['GNU_CXX']:
     ALLOW_COMPILER_WARNINGS = True
 
 NO_EXPAND_LIBS = True
 
 DIST_INSTALL = True
+
+# Prepare self-hosted JS code for embedding
+GENERATED_FILES += ['selfhosted.out.h']
+selfhosted = GENERATED_FILES['selfhosted.out.h']
+selfhosted.script = 'builtin/embedjs.py:generate_selfhosted'
+selfhosted.inputs = [
+    'js.msg',
+    'builtin/TypedObjectConstants.h',
+    'builtin/SelfHostingDefines.h',
+    'builtin/Utilities.js',
+    'builtin/Array.js',
+    'builtin/Date.js',
+    'builtin/Error.js',
+    'builtin/Generator.js',
+    'builtin/Intl.js',
+    'builtin/IntlData.js',
+    'builtin/Iterator.js',
+    'builtin/Map.js',
+    'builtin/Module.js',
+    'builtin/Number.js',
+    'builtin/Object.js',
+    'builtin/Reflect.js',
+    'builtin/RegExp.js',
+    'builtin/String.js',
+    'builtin/Set.js',
+    'builtin/TypedArray.js',
+    'builtin/TypedObject.js',
+    'builtin/WeakSet.js'
+]