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 --defs argument which will add custom flags to the comiler. Any extern | |
|
Dirk Pranke
2017/04/14 02:01:44
s/comiler/compiler/
damargulis
2017/04/14 17:46:33
Done.
| |
| 11 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_SUCCESS = 0 | |
| 20 EXIT_FAILURE = 1 | |
| 21 | |
| 22 def IsExecutable(path): | |
| 23 """Returns whether file at |path| exists and is executable.""" | |
|
Dirk Pranke
2017/04/14 02:01:44
Generally I discourage docstrings that tend to be
damargulis
2017/04/14 17:46:33
Done.
| |
| 24 return os.path.isfile(path) and os.access(path, os.X_OK) | |
| 25 | |
| 26 def FindCommand(command): | |
| 27 """Looks up for |commmand| in PATH.""" | |
| 28 filepath, _ = os.path.split(command) | |
| 29 if filepath and IsExecutable(command): | |
| 30 return command | |
| 31 | |
| 32 if sys.platform == 'win32': | |
| 33 # On Windows, if the command does not have an extension, cmd.exe will | |
| 34 # try all extensions from PATHEXT when resolving the full path. | |
| 35 command, ext = os.path.splitext(command) | |
| 36 exts = [ext] if ext else os.environ['PATHEXT'].split(os.path.pathsep) | |
| 37 else: | |
| 38 exts = [''] | |
| 39 | |
| 40 for path in os.environ['PATH'].split(os.path.pathsep): | |
| 41 for ext in exts: | |
| 42 path = os.path.join(path, command) + ext | |
| 43 if IsExecutable(path): | |
| 44 return path | |
| 45 | |
| 46 return None | |
| 47 | |
| 48 | |
| 49 def RunCompiler(args): | |
| 50 """Runs the closure compiler with |args|.""" | |
| 51 java_path = FindCommand('java') | |
| 52 if not java_path: | |
| 53 sys.stderr.write('java: command not found\n') | |
| 54 sys.exit(EXIT_FAILURE) | |
| 55 return subprocess.check_call([java_path, '-jar'] + args) | |
| 56 | |
| 57 def ParseDepList(dep): | |
| 58 """Parses a depenency list, returns |sources, deps|.""" | |
| 59 assert os.path.isfile(dep), dep[:-11] + ' is not a js_library target' | |
| 60 with open(dep, 'r') as dep_list: | |
| 61 lines = [line.strip() for line in dep_list.readlines()] | |
| 62 split = lines.index('deps:') | |
| 63 return lines[1:split], lines[split+1:] | |
| 64 | |
| 65 def PostOrder(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 = PostOrder(new_deps, sources) | |
| 71 sources = sources + [source for source in new_sources | |
| 72 if source not in sources] | |
| 73 return sources | |
| 74 | |
| 75 def main(): | |
| 76 parser = ArgumentParser() | |
| 77 parser.add_argument('-c', '--compiler', required=True, | |
| 78 help='Path to compiler') | |
| 79 parser.add_argument('-s', '--sources', nargs='*', default=[], | |
| 80 help='List of js source files') | |
| 81 parser.add_argument('-o', '--output', required=True, | |
| 82 help='Compile to output') | |
| 83 parser.add_argument('-d', '--deps', nargs='*', default = [], | |
| 84 help='List of js_libarary dependencies') | |
| 85 parser.add_argument('-b', '--bootstrap', | |
| 86 help='A file to include before all others') | |
| 87 parser.add_argument('-cf', '--config', nargs='*', default = [], | |
| 88 help='A list of files to include after bootstrap and' \ | |
| 89 'before all others') | |
| 90 parser.add_argument('-df', '--defs', nargs='*', default = [], | |
| 91 help='A list of custom flags to pass to the compiler. ' \ | |
| 92 'Do not include leading dashes') | |
| 93 parser.add_argument('-e', '--externs', nargs='*', default = [], | |
| 94 help='A list of extern files to pass to the compiler') | |
| 95 | |
| 96 args = parser.parse_args() | |
| 97 sources = PostOrder(args.deps, []) + args.sources | |
| 98 | |
| 99 defs = ['--' + flag for flag in args.defs] | |
| 100 | |
| 101 compiler_args = [ | |
| 102 args.compiler, | |
| 103 ] + defs | |
| 104 | |
| 105 for extern in args.externs: | |
| 106 compiler_args += ['--externs=' + extern] | |
| 107 | |
| 108 compiler_args += [ | |
| 109 '--js_output_file', | |
| 110 args.output, | |
| 111 '--js', | |
| 112 ] | |
| 113 if(args.bootstrap): | |
| 114 compiler_args += [args.bootstrap] | |
| 115 compiler_args += args.config | |
| 116 compiler_args += sources | |
| 117 RunCompiler(compiler_args) | |
| 118 | |
| 119 if __name__ == '__main__': | |
| 120 sys.exit(main()) | |
| OLD | NEW |