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 def IsExecutable(path): | |
| 22 return os.path.isfile(path) and os.access(path, os.X_OK) | |
| 23 | |
| 24 | |
| 25 def FindCommand(command): | |
| 26 filepath, _ = os.path.split(command) | |
| 27 if filepath and IsExecutable(command): | |
| 28 return command | |
| 29 | |
| 30 if sys.platform == 'win32': | |
| 31 # On Windows, if the command does not have an extension, cmd.exe will | |
| 32 # try all extensions from PATHEXT when resolving the full path. | |
| 33 command, ext = os.path.splitext(command) | |
| 34 exts = [ext] if ext else os.environ['PATHEXT'].split(os.path.pathsep) | |
| 35 else: | |
| 36 exts = [''] | |
| 37 | |
| 38 for path in os.environ['PATH'].split(os.path.pathsep): | |
| 39 for ext in exts: | |
| 40 path = os.path.join(path, command) + ext | |
| 41 if IsExecutable(path): | |
| 42 return path | |
| 43 | |
| 44 return None | |
| 45 | |
| 46 | |
| 47 def RunCompiler(args): | |
| 48 java_path = FindCommand('java') | |
| 49 if not java_path: | |
| 50 sys.stderr.write('java: command not found\n') | |
| 51 sys.exit(EXIT_FAILURE) | |
| 52 return subprocess.check_call([java_path, '-jar'] + args) | |
| 53 | |
| 54 | |
| 55 def ParseDepList(dep): | |
| 56 """Parses a depenency list, returns |sources, deps|.""" | |
| 57 assert os.path.isfile(dep), dep[:-11] + ' is not a js_library target' | |
|
mbjorge
2017/04/19 01:15:42
I think dpream@ was getting at -11 looks random fo
damargulis
2017/04/19 01:36:19
Done.
| |
| 58 with open(dep, 'r') as dep_list: | |
| 59 lines = dep_list.read().splitlines() | |
| 60 assert 'deps:' in lines, dep + ' is not formated correctly' | |
|
mbjorge
2017/04/19 01:15:42
nit: formatted
add something a bit more specific
damargulis
2017/04/19 01:36:19
Done.
| |
| 61 split = lines.index('deps:') | |
| 62 return lines[1:split], lines[split+1:] | |
| 63 | |
| 64 | |
| 65 def CrawlDepsTree(deps, sources): | |
| 66 """Parses the dependency tree creating a post-order listing of sources.""" | |
| 67 for dep in deps: | |
| 68 new_sources, new_deps = ParseDepList(dep) | |
| 69 | |
| 70 sources = CrawlDepsTree(new_deps, sources) | |
| 71 sources = sources + [source for source in new_sources | |
| 72 if source not in sources] | |
| 73 return sources | |
| 74 | |
| 75 | |
| 76 def main(): | |
| 77 parser = ArgumentParser() | |
| 78 parser.add_argument('-c', '--compiler', required=True, | |
| 79 help='Path to compiler') | |
| 80 parser.add_argument('-s', '--sources', nargs='*', default=[], | |
| 81 help='List of js source files') | |
| 82 parser.add_argument('-o', '--output', required=True, | |
| 83 help='Compile to output') | |
| 84 parser.add_argument('-d', '--deps', nargs='*', default = [], | |
| 85 help='List of js_libarary dependencies') | |
| 86 parser.add_argument('-b', '--bootstrap', | |
| 87 help='A file to include before all others') | |
| 88 parser.add_argument('-cf', '--config', nargs='*', default = [], | |
| 89 help='A list of files to include after bootstrap and' \ | |
| 90 'before all others') | |
| 91 parser.add_argument('-f', '--flags', nargs='*', default = [], | |
| 92 help='A list of custom flags to pass to the compiler. ' \ | |
| 93 'Do not include leading dashes') | |
| 94 parser.add_argument('-e', '--externs', nargs='*', default = [], | |
| 95 help='A list of extern files to pass to the compiler') | |
| 96 | |
| 97 args = parser.parse_args() | |
| 98 sources = CrawlDepsTree(args.deps, []) + args.sources | |
| 99 | |
| 100 flags = ['--' + flag for flag in args.flags] | |
|
mbjorge
2017/04/19 01:15:42
nit: match the style below '--%s' % flag
damargulis
2017/04/19 01:36:19
Done.
| |
| 101 compiler_args = [args.compiler] + flags | |
| 102 compiler_args += ['--externs=%s' % e for e in args.externs] | |
| 103 compiler_args += [ | |
| 104 '--js_output_file', | |
| 105 args.output, | |
| 106 '--js', | |
| 107 ] | |
| 108 if args.bootstrap: | |
| 109 compiler_args += [args.bootstrap] | |
| 110 compiler_args += args.config | |
| 111 compiler_args += sources | |
| 112 RunCompiler(compiler_args) | |
| 113 | |
| 114 | |
| 115 if __name__ == '__main__': | |
| 116 main() | |
| OLD | NEW |