Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 # Copyright 2017 The Chromium Authors. All rights reserved. | |
| 2 # Use of this source code is governed by a BSD-style license that can be | |
| 3 # found in the LICENSE file. | |
| 4 """Used by a js_binary action to compile javascript files. | |
| 5 | |
| 6 This script takes in a list of sources and dependencies and compiles them all | |
| 7 together into a single compiled .js file. The dependencies are ordered in a | |
| 8 post-order, left-to-right traversal order. If multiple instances of the same | |
| 9 source file are read, only the first is kept. The script can also take in | |
| 10 optional --flags argument which will add custom flags to the compiler. Any | |
| 11 extern files can also be passed in using the --extern flag. | |
| 12 """ | |
| 13 | |
| 14 from argparse import ArgumentParser | |
| 15 import os | |
| 16 import subprocess | |
| 17 import sys | |
| 18 | |
| 19 EXIT_FAILURE = 1 | |
| 20 | |
| 21 | |
| 22 def IsExecutable(path): | |
| 23 return os.path.isfile(path) and os.access(path, os.X_OK) | |
| 24 | |
| 25 | |
| 26 def FindCommand(command): | |
| 27 filepath, _ = os.path.split(command) | |
| 28 if filepath and IsExecutable(command): | |
| 29 return command | |
| 30 | |
| 31 if sys.platform == 'win32': | |
| 32 # On Windows, if the command does not have an extension, cmd.exe will | |
| 33 # try all extensions from PATHEXT when resolving the full path. | |
| 34 command, ext = os.path.splitext(command) | |
| 35 exts = [ext] if ext else os.environ['PATHEXT'].split(os.path.pathsep) | |
| 36 else: | |
| 37 exts = [''] | |
| 38 | |
| 39 for path in os.environ['PATH'].split(os.path.pathsep): | |
| 40 for ext in exts: | |
| 41 path = os.path.join(path, command) + ext | |
| 42 if IsExecutable(path): | |
| 43 return path | |
| 44 | |
| 45 return None | |
| 46 | |
| 47 | |
| 48 def RunCompiler(args): | |
| 49 java_path = FindCommand('java') | |
| 50 if not java_path: | |
| 51 sys.stderr.write('java: command not found\n') | |
| 52 sys.exit(EXIT_FAILURE) | |
| 53 return subprocess.check_call([java_path, '-jar'] + args) | |
| 54 | |
| 55 | |
| 56 def ParseDepList(dep): | |
| 57 """Parses a depenency list, returns |sources, deps|.""" | |
| 58 assert os.path.isfile(dep), (os.path.splitext(dep) + | |
| 59 ' is not a js_library target') | |
| 60 with open(dep, 'r') as dep_list: | |
| 61 lines = dep_list.read().splitlines() | |
| 62 assert 'deps:' in lines, dep + ' is not formated correctly, missing "deps:"' | |
| 63 split = lines.index('deps:') | |
| 64 return lines[1:split], lines[split+1:] | |
| 65 | |
| 66 | |
| 67 def CrawlDepsTree(deps, sources): | |
| 68 """Parses the dependency tree creating a post-order listing of sources.""" | |
| 69 for dep in deps: | |
| 70 new_sources, new_deps = ParseDepList(dep) | |
| 71 | |
| 72 sources = CrawlDepsTree(new_deps, sources) | |
| 73 sources += [source for source in new_sources if source not in sources] | |
|
Dan Beam
2017/04/19 17:59:45
why is sources not simply a set()?
damargulis
2017/04/19 22:31:42
A set won't maintain proper ordering of the source
| |
| 74 return sources | |
| 75 | |
| 76 | |
| 77 def main(): | |
| 78 parser = ArgumentParser() | |
| 79 parser.add_argument('-c', '--compiler', required=True, | |
| 80 help='Path to compiler') | |
| 81 parser.add_argument('-s', '--sources', nargs='*', default=[], | |
| 82 help='List of js source files') | |
| 83 parser.add_argument('-o', '--output', required=True, | |
| 84 help='Compile to output') | |
| 85 parser.add_argument('-d', '--deps', nargs='*', default=[], | |
| 86 help='List of js_libarary dependencies') | |
| 87 parser.add_argument('-b', '--bootstrap', | |
| 88 help='A file to include before all others') | |
| 89 parser.add_argument('-cf', '--config', nargs='*', default=[], | |
| 90 help='A list of files to include after bootstrap and ' | |
| 91 'before all others') | |
| 92 parser.add_argument('-f', '--flags', nargs='*', default=[], | |
| 93 help='A list of custom flags to pass to the compiler. ' | |
| 94 'Do not include leading dashes') | |
| 95 parser.add_argument('-e', '--externs', nargs='*', default=[], | |
| 96 help='A list of extern files to pass to the compiler') | |
| 97 | |
| 98 args = parser.parse_args() | |
| 99 sources = CrawlDepsTree(args.deps, []) + args.sources | |
| 100 | |
| 101 flags = ['--%s' % flag for flag in args.flags] | |
| 102 compiler_args = [args.compiler] + flags | |
| 103 compiler_args += ['--externs=%s' % e for e in args.externs] | |
| 104 compiler_args += [ | |
| 105 '--js_output_file', | |
| 106 args.output, | |
| 107 '--js', | |
| 108 ] | |
| 109 if args.bootstrap: | |
| 110 compiler_args += [args.bootstrap] | |
| 111 compiler_args += args.config | |
| 112 compiler_args += sources | |
| 113 RunCompiler(compiler_args) | |
| 114 | |
| 115 | |
| 116 if __name__ == '__main__': | |
| 117 main() | |
| OLD | NEW |