Bug 1019955 - Write preprocessing results for self-hosted code into file instead of stdout. r=glandium

--HG--
extra : rebase_source : 7dda05b622e766d6d04fe54e8d9bc5e8c30519a0
This commit is contained in:
Till Schneidereit 2014-06-07 14:26:54 +02:00
parent 2fb58dff88
commit 7917278dfc
2 changed files with 43 additions and 33 deletions

View File

@ -401,7 +401,8 @@ endif
selfhosted.out.h: $(selfhosted_out_h_deps)
$(PYTHON) $(srcdir)/builtin/embedjs.py $(SELFHOSTED_DEFINES) \
-p '$(CPP)' -m $(srcdir)/js.msg -o $@ $(selfhosting_srcs)
-c '$(CCC)' -p '$(PREPROCESS_OPTION)' -m $(srcdir)/js.msg \
-o $@ $(selfhosting_srcs)
###############################################
# Generating source package tarballs

View File

@ -39,6 +39,7 @@
from __future__ import with_statement
import re, sys, os, fileinput, subprocess
import shlex
import which
from optparse import OptionParser
def ToCAsciiArray(lines):
@ -77,19 +78,21 @@ namespace selfhosted {
} // js
"""
def embed(cpp, msgs, sources, c_out, js_out, env):
# Clang seems to complain and not output anything if the extension of the
# input is not something it recognizes, so just fake a .h here.
tmp = 'selfhosted.js.h'
with open(tmp, 'wb') as output:
output.write('\n'.join([msgs] + ['#include "%(s)s"' % { 's': source } for source in sources]))
cmdline = cpp + ['-D%(k)s=%(v)s' % { 'k': k, 'v': env[k] } for k in env] + [tmp]
p = subprocess.Popen(cmdline, stdout=subprocess.PIPE)
processed = ''
for line in p.stdout:
if not line.startswith('#'):
processed += line
os.remove(tmp)
MSGS_TEMPLATE = """\
#define hash #
#define id(x) x
#define hashify(x) id(hash)x
#define MSG_DEF(name, id, argc, ex, msg) hashify(define) name id
#include "%(msgs)s"
"""
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:
@ -114,21 +117,25 @@ def embed(cpp, msgs, sources, c_out, js_out, env):
'raw_total_length': len(processed)
})
def process_msgs(cpp, msgs):
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 .h here.
tmp = 'selfhosted.msg.h'
with open(tmp, 'wb') as output:
output.write("""\
#define hash #
#define id(x) x
#define hashify(x) id(hash)x
#define MSG_DEF(name, id, argc, ex, msg) hashify(define) name id
#include "%(msgs)s"
""" % { 'msgs': msgs })
p = subprocess.Popen(cpp + [tmp], stdout=subprocess.PIPE)
processed = p.communicate()[0]
os.remove(tmp)
# input is not something it recognizes, so just fake a .cpp here.
tmpIn = 'self-hosting-cpp-input.cpp';
tmpOut = 'self-hosting-preprocessed.pp';
outputArg = shlex.split(preprocessorOption + tmpOut)
with open(tmpIn, 'wb') as input:
input.write(source)
print(' '.join(cxx + outputArg + args + [tmpIn]))
result = subprocess.Popen(cxx + outputArg + args + [tmpIn]).wait()
if (result != 0):
sys.exit(result);
with open(tmpOut, 'r') as output:
processed = output.read();
os.remove(tmpIn)
os.remove(tmpOut)
return processed
def main():
@ -143,7 +150,9 @@ def main():
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('-p', type='string', metavar='cpp', help='Path to C preprocessor')
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',
@ -151,10 +160,10 @@ def main():
(options, sources) = p.parse_args()
if not (options.p and sources):
p.print_help()
exit(1)
cpp = shlex.split(options.p)
embed(cpp, process_msgs(cpp, options.m),
sources, options.o, options.s, env)
sys.exit(1)
cxx = shlex.split(options.c)
msgs = preprocess(cxx, options.p, MSGS_TEMPLATE % { 'msgs': options.m })
embed(cxx, options.p, msgs, sources, options.o, options.s, env)
if __name__ == "__main__":
main()