summaryrefslogtreecommitdiff
path: root/utils/ipc/mojo/public/tools/bindings/concatenate_and_replace_closure_exports.py
diff options
context:
space:
mode:
authorPaul Elder <paul.elder@ideasonboard.com>2020-09-08 20:47:19 +0900
committerPaul Elder <paul.elder@ideasonboard.com>2020-11-11 19:22:37 +0900
commit82ba73535c0966e8ae8fb50db1ea23534d827717 (patch)
treef75b0d7f3933369872157105f1e467b71430e8cf /utils/ipc/mojo/public/tools/bindings/concatenate_and_replace_closure_exports.py
parent3d624b745b31383dbcd94d96246fab865820085f (diff)
utils: ipc: import mojo
Import mojo from the Chromium repository, so that we can use it for generating code for the IPC mechanism. The commit from which this was taken is: a079161ec8c6907b883f9cb84fc8c4e7896cb1d0 "Add PPAPI constructs for sending focus object to PdfAccessibilityTree" This tree has been pruned to remove directories that didn't have any necessary code: - mojo/* except for mojo/public - mojo core, docs, and misc files - mojo/public/* except for mojo/public/{tools,LICENSE} - language bindings for IPC, tests, and some mojo internals - mojo/public/tools/{fuzzers,chrome_ipc} - mojo/public/tools/bindings/generators - code generation for other languages No files were modified. Signed-off-by: Paul Elder <paul.elder@ideasonboard.com> Acked-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Acked-by: Niklas Söderlund <niklas.soderlund@ragnatech.se> Acked-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
Diffstat (limited to 'utils/ipc/mojo/public/tools/bindings/concatenate_and_replace_closure_exports.py')
-rwxr-xr-xutils/ipc/mojo/public/tools/bindings/concatenate_and_replace_closure_exports.py73
1 files changed, 73 insertions, 0 deletions
diff --git a/utils/ipc/mojo/public/tools/bindings/concatenate_and_replace_closure_exports.py b/utils/ipc/mojo/public/tools/bindings/concatenate_and_replace_closure_exports.py
new file mode 100755
index 00000000..be8985ce
--- /dev/null
+++ b/utils/ipc/mojo/public/tools/bindings/concatenate_and_replace_closure_exports.py
@@ -0,0 +1,73 @@
+#!/usr/bin/env python
+# Copyright 2018 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+"""Simple utility which concatenates a set of files into a single output file
+while also stripping any goog.provide or goog.require lines. This allows us to
+provide a very primitive sort of "compilation" without any extra toolchain
+support and without having to modify otherwise compilable sources in the tree
+which use these directives.
+
+goog.provide lines are replaced with an equivalent invocation of
+mojo.internal.exportModule, which accomplishes essentially the same thing in an
+uncompiled context. A singular exception is made for the 'mojo.internal' export,
+which is instead replaced with an inlined assignment to initialize the
+namespace.
+"""
+
+from __future__ import print_function
+
+import optparse
+import re
+
+
+_MOJO_INTERNAL_MODULE_NAME = "mojo.internal"
+_MOJO_EXPORT_MODULE_SYMBOL = "mojo.internal.exportModule"
+
+
+def FilterLine(filename, line, output):
+ if line.startswith("goog.require"):
+ return
+
+ if line.startswith("goog.provide"):
+ match = re.match("goog.provide\('([^']+)'\);", line)
+ if not match:
+ print("Invalid goog.provide line in %s:\n%s" % (filename, line))
+ exit(1)
+
+ module_name = match.group(1)
+ if module_name == _MOJO_INTERNAL_MODULE_NAME:
+ output.write("self.mojo = { internal: {} };")
+ else:
+ output.write("%s('%s');\n" % (_MOJO_EXPORT_MODULE_SYMBOL, module_name))
+ return
+
+ output.write(line)
+
+def ConcatenateAndReplaceExports(filenames):
+ if (len(filenames) < 2):
+ print("At least two filenames (one input and the output) are required.")
+ return False
+
+ try:
+ with open(filenames[-1], "w") as target:
+ for filename in filenames[:-1]:
+ with open(filename, "r") as current:
+ for line in current.readlines():
+ FilterLine(filename, line, target)
+ return True
+ except IOError as e:
+ print("Error generating %s\n: %s" % (filenames[-1], e))
+ return False
+
+def main():
+ parser = optparse.OptionParser()
+ parser.set_usage("""file1 [file2...] outfile
+ Concatenate several files into one, stripping Closure provide and
+ require directives along the way.""")
+ (_, args) = parser.parse_args()
+ exit(0 if ConcatenateAndReplaceExports(args) else 1)
+
+if __name__ == "__main__":
+ main()