Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / build / android / gyp / create_java_binary_script.py
blob0f07a1deb759ee50a462ae2c84531f40fe90762c
1 #!/usr/bin/env python
3 # Copyright 2014 The Chromium Authors. All rights reserved.
4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file.
7 """Creates a simple script to run a java "binary".
9 This creates a script that sets up the java command line for running a java
10 jar. This includes correctly setting the classpath and the main class.
11 """
13 import optparse
14 import os
15 import sys
17 from util import build_utils
19 # The java command must be executed in the current directory because there may
20 # be user-supplied paths in the args. The script receives the classpath relative
21 # to the directory that the script is written in and then, when run, must
22 # recalculate the paths relative to the current directory.
23 script_template = """\
24 #!/usr/bin/env python
26 # This file was generated by build/android/gyp/create_java_binary_script.py
28 import os
29 import sys
31 self_dir = os.path.dirname(__file__)
32 classpath = [{classpath}]
33 extra_args = {extra_args}
34 if os.getcwd() != self_dir:
35 offset = os.path.relpath(self_dir, os.getcwd())
36 classpath = [os.path.join(offset, p) for p in classpath]
37 java_args = [
38 "java",
39 "-classpath", ":".join(classpath),
40 "-enableassertions",
41 \"{main_class}\"] + extra_args + sys.argv[1:]
42 os.execvp("java", java_args)
43 """
45 def main(argv):
46 argv = build_utils.ExpandFileArgs(argv)
47 parser = optparse.OptionParser()
48 build_utils.AddDepfileOption(parser)
49 parser.add_option('--output', help='Output path for executable script.')
50 parser.add_option('--jar-path', help='Path to the main jar.')
51 parser.add_option('--main-class',
52 help='Name of the java class with the "main" entry point.')
53 parser.add_option('--classpath', action='append',
54 help='Classpath for running the jar.')
55 options, extra_args = parser.parse_args(argv)
57 classpath = [options.jar_path]
58 for cp_arg in options.classpath:
59 classpath += build_utils.ParseGypList(cp_arg)
61 run_dir = os.path.dirname(options.output)
62 classpath = [os.path.relpath(p, run_dir) for p in classpath]
64 with open(options.output, 'w') as script:
65 script.write(script_template.format(
66 classpath=('"%s"' % '", "'.join(classpath)),
67 main_class=options.main_class,
68 extra_args=repr(extra_args)))
70 os.chmod(options.output, 0750)
72 if options.depfile:
73 build_utils.WriteDepfile(
74 options.depfile,
75 build_utils.GetPythonDependencies())
78 if __name__ == '__main__':
79 sys.exit(main(sys.argv[1:]))