kotlinc_runner.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #!/usr/bin/env python
  2. # Copyright (c) 2023 Project CHIP Authors
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. # Copyright 2015 The Chromium Authors. All rights reserved.
  16. # Use of this source code is governed by a BSD-style license that can be
  17. # found in the LICENSE file.
  18. """Wrapper script to run kotlinc command as an action with gn."""
  19. import argparse
  20. import json
  21. import os
  22. import subprocess
  23. import sys
  24. EXIT_SUCCESS = 0
  25. EXIT_FAILURE = 1
  26. def IsExecutable(path):
  27. """Returns whether file at |path| exists and is executable.
  28. Args:
  29. path: absolute or relative path to test.
  30. Returns:
  31. True if the file at |path| exists, False otherwise.
  32. """
  33. return os.path.isfile(path) and os.access(path, os.X_OK)
  34. def FindCommand(command):
  35. """Looks up for |command| in PATH.
  36. Args:
  37. command: name of the command to lookup, if command is a relative or absolute
  38. path (i.e. contains some path separator) then only that path will be
  39. tested.
  40. Returns:
  41. Full path to command or None if the command was not found.
  42. On Windows, this respects the PATHEXT environment variable when the
  43. command name does not have an extension.
  44. """
  45. fpath, _ = os.path.split(command)
  46. if fpath:
  47. if IsExecutable(command):
  48. return command
  49. if sys.platform == 'win32':
  50. # On Windows, if the command does not have an extension, cmd.exe will
  51. # try all extensions from PATHEXT when resolving the full path.
  52. command, ext = os.path.splitext(command)
  53. if not ext:
  54. exts = os.environ['PATHEXT'].split(os.path.pathsep)
  55. else:
  56. exts = [ext]
  57. else:
  58. exts = ['']
  59. for path in os.environ['PATH'].split(os.path.pathsep):
  60. for ext in exts:
  61. path = os.path.join(path, command) + ext
  62. if IsExecutable(path):
  63. return path
  64. return None
  65. def ReadBuildConfig(build_config):
  66. with open(build_config, 'r') as file:
  67. return json.load(file)
  68. def ComputeClasspath(build_config_json):
  69. unique_jars = build_config_json['deps_info']['deps_jars']
  70. if sys.platform == 'win32':
  71. return ";".join(unique_jars)
  72. else:
  73. return ":".join(unique_jars)
  74. def main():
  75. kotlin_path = FindCommand('kotlinc')
  76. if not kotlin_path:
  77. sys.stderr.write('kotlinc: command not found\n')
  78. sys.exit(EXIT_FAILURE)
  79. parser = argparse.ArgumentParser('Kotkinc runner')
  80. parser.add_argument(
  81. '--classdir',
  82. dest='classdir',
  83. required=True,
  84. help='Directory that will contain class files')
  85. parser.add_argument(
  86. '--outfile',
  87. dest='outfile',
  88. required=True,
  89. help='Output file containing a list of classes')
  90. parser.add_argument(
  91. '--build-config',
  92. dest='build_config',
  93. required=True,
  94. help='Build config')
  95. parser.add_argument(
  96. 'rest', metavar='KOTLINC_ARGS', nargs='*', help='Argumets to pass to kotlinc')
  97. args = parser.parse_args()
  98. if not os.path.isdir(args.classdir):
  99. os.makedirs(args.classdir, exist_ok=True)
  100. build_config_json = ReadBuildConfig(args.build_config)
  101. classpath = ComputeClasspath(build_config_json)
  102. kotlin_args = [kotlin_path]
  103. if classpath:
  104. kotlin_args += ["-classpath", classpath]
  105. retcode = subprocess.check_call(kotlin_args + args.rest)
  106. if retcode != EXIT_SUCCESS:
  107. return retcode
  108. with open(args.outfile, 'wt') as f:
  109. prefixlen = len(args.classdir) + 1
  110. for root, dirnames, filenames in os.walk(args.classdir):
  111. for filename in filenames:
  112. if filename.endswith('.class'):
  113. f.write(os.path.join(root[prefixlen:], filename))
  114. f.write('\n')
  115. return EXIT_SUCCESS
  116. if __name__ == '__main__':
  117. sys.exit(main())