1#!/usr/bin/env python3
2#
3# Copyright 2018 Google Inc. All rights reserved.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17# Generates kotlinc module xml file to drive kotlinc
18
19import argparse
20import os
21
22from ninja_rsp import NinjaRspFileReader
23
24def parse_args():
25  """Parse commandline arguments."""
26
27  def convert_arg_line_to_args(arg_line):
28    for arg in arg_line.split():
29      if arg.startswith('#'):
30        return
31      if not arg.strip():
32        continue
33      yield arg
34
35  parser = argparse.ArgumentParser(fromfile_prefix_chars='@')
36  parser.convert_arg_line_to_args = convert_arg_line_to_args
37  parser.add_argument('--out', dest='out',
38                      help='file to which the module.xml contents will be written.')
39  parser.add_argument('--classpath', dest='classpath', action='append', default=[],
40                      help='classpath to pass to kotlinc.')
41  parser.add_argument('--name', dest='name',
42                      help='name of the module.')
43  parser.add_argument('--out_dir', dest='out_dir',
44                      help='directory to which kotlinc will write output files.')
45  parser.add_argument('--srcs', dest='srcs', action='append', default=[],
46                      help='file containing whitespace separated list of source files.')
47  parser.add_argument('--common_srcs', dest='common_srcs', action='append', default=[],
48                      help='file containing whitespace separated list of common multiplatform source files.')
49
50  return parser.parse_args()
51
52def main():
53  """Program entry point."""
54  args = parse_args()
55
56  if not args.out:
57    raise RuntimeError('--out argument is required')
58
59  if not args.name:
60    raise RuntimeError('--name argument is required')
61
62  with open(args.out, 'w') as f:
63    # Print preamble
64    f.write('<modules>\n')
65    f.write('  <module name="%s" type="java-production" outputDir="%s">\n' % (args.name, args.out_dir or ''))
66
67    # Print classpath entries
68    for c in args.classpath:
69      for entry in c.split(':'):
70        path = os.path.abspath(entry)
71        f.write('    <classpath path="%s"/>\n' % path)
72
73    # For each rsp file, print source entries
74    for rsp_file in args.srcs:
75      for src in NinjaRspFileReader(rsp_file):
76        path = os.path.abspath(src)
77        if src.endswith('.java'):
78          f.write('    <javaSourceRoots path="%s"/>\n' % path)
79        elif src.endswith('.kt'):
80          f.write('    <sources path="%s"/>\n' % path)
81        else:
82          raise RuntimeError('unknown source file type %s' % file)
83
84    for rsp_file in args.common_srcs:
85      for src in NinjaRspFileReader(rsp_file):
86        path = os.path.abspath(src)
87        f.write('    <sources path="%s"/>\n' % path)
88        f.write('    <commonSources path="%s"/>\n' % path)
89
90    f.write('  </module>\n')
91    f.write('</modules>\n')
92
93if __name__ == '__main__':
94  main()
95