Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / build / chip / java / javac_runner.py
1 #!/usr/bin/env python
2 # Copyright (c) 2020 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 javac command as an action with gn."""
20
21 import argparse
22 import os
23 import subprocess
24 import sys
25
26 EXIT_SUCCESS = 0
27 EXIT_FAILURE = 1
28
29
30 def IsExecutable(path):
31   """Returns whether file at |path| exists and is executable.
32
33   Args:
34     path: absolute or relative path to test.
35
36   Returns:
37     True if the file at |path| exists, False otherwise.
38   """
39   return os.path.isfile(path) and os.access(path, os.X_OK)
40
41
42 def FindCommand(command):
43   """Looks up for |command| in PATH.
44
45   Args:
46     command: name of the command to lookup, if command is a relative or absolute
47       path (i.e. contains some path separator) then only that path will be
48       tested.
49
50   Returns:
51     Full path to command or None if the command was not found.
52
53     On Windows, this respects the PATHEXT environment variable when the
54     command name does not have an extension.
55   """
56   fpath, _ = os.path.split(command)
57   if fpath:
58     if IsExecutable(command):
59       return command
60
61   if sys.platform == 'win32':
62     # On Windows, if the command does not have an extension, cmd.exe will
63     # try all extensions from PATHEXT when resolving the full path.
64     command, ext = os.path.splitext(command)
65     if not ext:
66       exts = os.environ['PATHEXT'].split(os.path.pathsep)
67     else:
68       exts = [ext]
69   else:
70     exts = ['']
71
72   for path in os.environ['PATH'].split(os.path.pathsep):
73     for ext in exts:
74       path = os.path.join(path, command) + ext
75       if IsExecutable(path):
76         return path
77
78   return None
79
80
81 def main():
82   java_path = FindCommand('javac')
83   if not java_path:
84     sys.stderr.write('javac: command not found\n')
85     sys.exit(EXIT_FAILURE)
86
87   parser = argparse.ArgumentParser('Javac runner')
88   parser.add_argument(
89       '--classdir',
90       dest='classdir',
91       required=True,
92       help='Directory that will contain class files')
93   parser.add_argument(
94       '--outfile',
95       dest='outfile',
96       required=True,
97       help='Output file containing a list of classes')
98   parser.add_argument(
99       'rest', metavar='JAVAC_ARGS', nargs='*', help='Argumets to pass to javac')
100
101   args = parser.parse_args()
102   if not os.path.isdir(args.classdir):
103     os.makedirs(args.classdir)
104   retcode = subprocess.check_call([java_path] + args.rest)
105   if retcode != EXIT_SUCCESS:
106     return retcode
107
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
116   return EXIT_SUCCESS
117
118
119 if __name__ == '__main__':
120   sys.exit(main())