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 | |
| 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 return os.path.isfile(path) and os.access(path, os.X_OK) | |
| 24 | |
| 25 def FindCommand(command): | |
|
mbjorge
2017/04/07 19:23:18
add one line doc strings for each of the methods
damargulis
2017/04/07 23:02:19
Done.
| |
| 26 fpath, _ = os.path.split(command) | |
|
mbjorge
2017/04/07 19:23:18
rename: fpath -> filepath or command_name
damargulis
2017/04/07 23:02:19
Done.
| |
| 27 if fpath: | |
| 28 if 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 if not ext: | |
| 36 exts = os.environ['PATHEXT'].split(os.path.pathsep) | |
| 37 else: | |
| 38 exts = [ext] | |
| 39 else: | |
| 40 exts = [''] | |
| 41 | |
| 42 for path in os.environ['PATH'].split(os.path.pathsep): | |
| 43 for ext in exts: | |
| 44 path = os.path.join(path, command) + ext | |
| 45 if IsExecutable(path): | |
| 46 return path | |
| 47 | |
| 48 return None | |
| 49 | |
| 50 | |
| 51 def RunCompiler(args): | |
| 52 java_path = FindCommand('java') | |
| 53 if not java_path: | |
| 54 sys.stderr.write('java: command not found\n') | |
| 55 sys.exit(EXIT_FAILURE) | |
| 56 return subprocess.check_call([java_path, '-jar'] + args) | |
| 57 | |
| 58 def postOrder(deps, sources): | |
|
mbjorge
2017/04/07 19:23:18
nit: PostOrder
damargulis
2017/04/07 23:02:19
Done.
| |
| 59 for dep in deps: | |
| 60 with file(dep, 'r') as dep_list: | |
|
mbjorge
2017/04/07 19:23:18
with open(dep, 'r') as dep_list
When opening a fi
damargulis
2017/04/07 23:02:19
Done.
| |
| 61 lines = [line.strip() for line in dep_list.readlines()] | |
| 62 split = lines.index('deps:') | |
|
mbjorge
2017/04/07 19:23:18
Maybe pull this out into a helper method?
new_sou
damargulis
2017/04/07 23:02:18
Done.
| |
| 63 sources = postOrder(lines[split+1:], sources) | |
| 64 sources = sources + [source for source in lines[1:split] | |
| 65 if source not in sources] | |
| 66 return sources | |
| 67 | |
| 68 def main(): | |
| 69 parser = ArgumentParser() | |
| 70 parser.add_argument('-c', '--compiler', required=True, | |
| 71 help='Path to compiler') | |
| 72 parser.add_argument('-s', '--sources', nargs='*', default=[], | |
| 73 help='List of js source files') | |
| 74 parser.add_argument('-o', '--output', required=True, | |
| 75 help='Compile to output') | |
| 76 parser.add_argument('-d', '--deps', nargs='*', default = [], | |
| 77 help='List of js_libarary dependencies') | |
| 78 parser.add_argument('-b', '--bootstrap', | |
| 79 help='A file to include before all others') | |
| 80 parser.add_argument('-cf', '--config', nargs='*', default = [], | |
| 81 help='A list of files to include after bootstrap and' \ | |
| 82 'before all others') | |
| 83 parser.add_argument('-df', '--defs', nargs='*', default = [], | |
| 84 help='A list of custom flags to pass to the compiler. ' \ | |
| 85 'Do not include leading dashes') | |
| 86 parser.add_argument('-e', '--externs', nargs='*', default = [], | |
| 87 help='A list of extern files to pass to the compiler') | |
| 88 | |
| 89 args = parser.parse_args() | |
| 90 sources = postOrder(args.deps, args.sources) | |
| 91 | |
| 92 defs = ['--' + flag for flag in args.defs] | |
| 93 | |
| 94 compiler_args = [ | |
| 95 args.compiler, | |
| 96 '--compilation_level', | |
| 97 'SIMPLE_OPTIMIZATIONS', | |
|
mbjorge
2017/04/07 19:23:18
does it make sense for this to be in the GN templa
damargulis
2017/04/07 23:02:19
I think it makes sense to add this in from the GN
| |
| 98 ] + defs | |
| 99 | |
| 100 if(args.externs): | |
| 101 compiler_args += ['--externs'] + args.externs | |
| 102 | |
| 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 if __name__ == '__main__': | |
| 115 sys.exit(main()) | |
| OLD | NEW |