gen-kotlin-build-file.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. # Generates kotlinc module xml file to drive kotlinc
  17. import argparse
  18. import os
  19. from ninja_rsp import NinjaRspFileReader
  20. def parse_args():
  21. """Parse commandline arguments."""
  22. def convert_arg_line_to_args(arg_line):
  23. for arg in arg_line.split():
  24. if arg.startswith('#'):
  25. return
  26. if not arg.strip():
  27. continue
  28. yield arg
  29. parser = argparse.ArgumentParser(fromfile_prefix_chars='@')
  30. parser.convert_arg_line_to_args = convert_arg_line_to_args
  31. parser.add_argument('--out', dest='out',
  32. help='file to which the module.xml contents will be written.')
  33. parser.add_argument('--classpath', dest='classpath', action='append', default=[],
  34. help='classpath to pass to kotlinc.')
  35. parser.add_argument('--name', dest='name',
  36. help='name of the module.')
  37. parser.add_argument('--out_dir', dest='out_dir',
  38. help='directory to which kotlinc will write output files.')
  39. parser.add_argument('--srcs', dest='srcs', action='append', default=[],
  40. help='file containing whitespace separated list of source files.')
  41. parser.add_argument('--common_srcs', dest='common_srcs', action='append', default=[],
  42. help='file containing whitespace separated list of common multiplatform source files.')
  43. return parser.parse_args()
  44. def main():
  45. """Program entry point."""
  46. args = parse_args()
  47. if not args.out:
  48. raise RuntimeError('--out argument is required')
  49. if not args.name:
  50. raise RuntimeError('--name argument is required')
  51. with open(args.out, 'w') as f:
  52. # Print preamble
  53. f.write('<modules>\n')
  54. f.write(' <module name="%s" type="java-production" outputDir="%s">\n' % (args.name, args.out_dir or ''))
  55. # Print classpath entries
  56. for c in args.classpath:
  57. for entry in c.split(':'):
  58. path = os.path.abspath(entry)
  59. f.write(' <classpath path="%s"/>\n' % path)
  60. # For each rsp file, print source entries
  61. for rsp_file in args.srcs:
  62. for src in NinjaRspFileReader(rsp_file):
  63. path = os.path.abspath(src)
  64. if src.endswith('.java'):
  65. f.write(' <javaSourceRoots path="%s"/>\n' % path)
  66. elif src.endswith('.kt'):
  67. f.write(' <sources path="%s"/>\n' % path)
  68. else:
  69. raise RuntimeError('unknown source file type %s' % file)
  70. for rsp_file in args.common_srcs:
  71. for src in NinjaRspFileReader(rsp_file):
  72. path = os.path.abspath(src)
  73. f.write(' <sources path="%s"/>\n' % path)
  74. f.write(' <commonSources path="%s"/>\n' % path)
  75. f.write(' </module>\n')
  76. f.write('</modules>\n')
  77. if __name__ == '__main__':
  78. main()