|
| 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 | + |
| 16 | +# Copyright 2015 The Chromium Authors. All rights reserved. |
| 17 | +# Use of this source code is governed by a BSD-style license that can be |
| 18 | +# found in the LICENSE file. |
| 19 | +"""Wrapper script to run kotlinc command as an action with gn.""" |
| 20 | + |
| 21 | +import argparse |
| 22 | +import json |
| 23 | +import os |
| 24 | +import subprocess |
| 25 | +import sys |
| 26 | + |
| 27 | +EXIT_SUCCESS = 0 |
| 28 | +EXIT_FAILURE = 1 |
| 29 | + |
| 30 | + |
| 31 | +def IsExecutable(path): |
| 32 | + """Returns whether file at |path| exists and is executable. |
| 33 | +
|
| 34 | + Args: |
| 35 | + path: absolute or relative path to test. |
| 36 | +
|
| 37 | + Returns: |
| 38 | + True if the file at |path| exists, False otherwise. |
| 39 | + """ |
| 40 | + return os.path.isfile(path) and os.access(path, os.X_OK) |
| 41 | + |
| 42 | + |
| 43 | +def FindCommand(command): |
| 44 | + """Looks up for |command| in PATH. |
| 45 | +
|
| 46 | + Args: |
| 47 | + command: name of the command to lookup, if command is a relative or absolute |
| 48 | + path (i.e. contains some path separator) then only that path will be |
| 49 | + tested. |
| 50 | +
|
| 51 | + Returns: |
| 52 | + Full path to command or None if the command was not found. |
| 53 | +
|
| 54 | + On Windows, this respects the PATHEXT environment variable when the |
| 55 | + command name does not have an extension. |
| 56 | + """ |
| 57 | + fpath, _ = os.path.split(command) |
| 58 | + if fpath: |
| 59 | + if IsExecutable(command): |
| 60 | + return command |
| 61 | + |
| 62 | + if sys.platform == 'win32': |
| 63 | + # On Windows, if the command does not have an extension, cmd.exe will |
| 64 | + # try all extensions from PATHEXT when resolving the full path. |
| 65 | + command, ext = os.path.splitext(command) |
| 66 | + if not ext: |
| 67 | + exts = os.environ['PATHEXT'].split(os.path.pathsep) |
| 68 | + else: |
| 69 | + exts = [ext] |
| 70 | + else: |
| 71 | + exts = [''] |
| 72 | + |
| 73 | + for path in os.environ['PATH'].split(os.path.pathsep): |
| 74 | + for ext in exts: |
| 75 | + path = os.path.join(path, command) + ext |
| 76 | + if IsExecutable(path): |
| 77 | + return path |
| 78 | + |
| 79 | + return None |
| 80 | + |
| 81 | + |
| 82 | +def ReadBuildConfig(build_config): |
| 83 | + with open(build_config, 'r') as file: |
| 84 | + return json.load(file) |
| 85 | + |
| 86 | + |
| 87 | +def ComputeClasspath(build_config_json): |
| 88 | + unique_jars = build_config_json['deps_info']['deps_jars'] |
| 89 | + if sys.platform == 'win32': |
| 90 | + return ";".join(unique_jars) |
| 91 | + else: |
| 92 | + return ":".join(unique_jars) |
| 93 | + |
| 94 | + |
| 95 | +def main(): |
| 96 | + kotlin_path = FindCommand('kotlinc') |
| 97 | + if not kotlin_path: |
| 98 | + sys.stderr.write('kotlinc: command not found\n') |
| 99 | + sys.exit(EXIT_FAILURE) |
| 100 | + |
| 101 | + parser = argparse.ArgumentParser('Kotkinc runner') |
| 102 | + parser.add_argument( |
| 103 | + '--classdir', |
| 104 | + dest='classdir', |
| 105 | + required=True, |
| 106 | + help='Directory that will contain class files') |
| 107 | + parser.add_argument( |
| 108 | + '--outfile', |
| 109 | + dest='outfile', |
| 110 | + required=True, |
| 111 | + help='Output file containing a list of classes') |
| 112 | + parser.add_argument( |
| 113 | + '--build-config', |
| 114 | + dest='build_config', |
| 115 | + required=True, |
| 116 | + help='Build config') |
| 117 | + parser.add_argument( |
| 118 | + 'rest', metavar='KOTLINC_ARGS', nargs='*', help='Argumets to pass to kotlinc') |
| 119 | + |
| 120 | + args = parser.parse_args() |
| 121 | + if not os.path.isdir(args.classdir): |
| 122 | + os.makedirs(args.classdir, exist_ok=True) |
| 123 | + |
| 124 | + build_config_json = ReadBuildConfig(args.build_config) |
| 125 | + classpath = ComputeClasspath(build_config_json) |
| 126 | + kotlin_args = [kotlin_path] |
| 127 | + if classpath: |
| 128 | + kotlin_args += ["-classpath", classpath] |
| 129 | + |
| 130 | + retcode = subprocess.check_call(kotlin_args + args.rest) |
| 131 | + if retcode != EXIT_SUCCESS: |
| 132 | + return retcode |
| 133 | + |
| 134 | + with open(args.outfile, 'wt') as f: |
| 135 | + prefixlen = len(args.classdir) + 1 |
| 136 | + for root, dirnames, filenames in os.walk(args.classdir): |
| 137 | + for filename in filenames: |
| 138 | + if filename.endswith('.class'): |
| 139 | + f.write(os.path.join(root[prefixlen:], filename)) |
| 140 | + f.write('\n') |
| 141 | + |
| 142 | + return EXIT_SUCCESS |
| 143 | + |
| 144 | + |
| 145 | +if __name__ == '__main__': |
| 146 | + sys.exit(main()) |
0 commit comments