blob: ce6aa21a225ba095d2ecaa1e0fc239f78fbe3953 [file] [log] [blame]
Colin Cross014489c2020-06-02 20:09:13 -07001#!/usr/bin/env python3
2#
3# Copyright (C) 2018 The Android Open Source Project
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
18"""This file generates project.xml and lint.xml files used to drive the Android Lint CLI tool."""
19
20import argparse
Cole Faust98098602024-01-17 17:11:51 -080021import sys
Jaewoong Junga1104392021-04-19 17:38:17 -070022from xml.dom import minidom
Colin Cross014489c2020-06-02 20:09:13 -070023
Colin Cross9b1aa0c2020-06-25 17:12:28 -070024from ninja_rsp import NinjaRspFileReader
25
Colin Cross014489c2020-06-02 20:09:13 -070026
27def check_action(check_type):
28 """
29 Returns an action that appends a tuple of check_type and the argument to the dest.
30 """
31 class CheckAction(argparse.Action):
32 def __init__(self, option_strings, dest, nargs=None, **kwargs):
33 if nargs is not None:
34 raise ValueError("nargs must be None, was %s" % nargs)
35 super(CheckAction, self).__init__(option_strings, dest, **kwargs)
36 def __call__(self, parser, namespace, values, option_string=None):
37 checks = getattr(namespace, self.dest, [])
38 checks.append((check_type, values))
39 setattr(namespace, self.dest, checks)
40 return CheckAction
41
42
43def parse_args():
44 """Parse commandline arguments."""
45
46 def convert_arg_line_to_args(arg_line):
47 for arg in arg_line.split():
48 if arg.startswith('#'):
49 return
50 if not arg.strip():
51 continue
52 yield arg
53
54 parser = argparse.ArgumentParser(fromfile_prefix_chars='@')
55 parser.convert_arg_line_to_args = convert_arg_line_to_args
56 parser.add_argument('--project_out', dest='project_out',
57 help='file to which the project.xml contents will be written.')
58 parser.add_argument('--config_out', dest='config_out',
59 help='file to which the lint.xml contents will be written.')
60 parser.add_argument('--name', dest='name',
61 help='name of the module.')
62 parser.add_argument('--srcs', dest='srcs', action='append', default=[],
63 help='file containing whitespace separated list of source files.')
64 parser.add_argument('--generated_srcs', dest='generated_srcs', action='append', default=[],
65 help='file containing whitespace separated list of generated source files.')
66 parser.add_argument('--resources', dest='resources', action='append', default=[],
67 help='file containing whitespace separated list of resource files.')
68 parser.add_argument('--classes', dest='classes', action='append', default=[],
69 help='file containing the module\'s classes.')
70 parser.add_argument('--classpath', dest='classpath', action='append', default=[],
71 help='file containing classes from dependencies.')
72 parser.add_argument('--extra_checks_jar', dest='extra_checks_jars', action='append', default=[],
73 help='file containing extra lint checks.')
74 parser.add_argument('--manifest', dest='manifest',
75 help='file containing the module\'s manifest.')
76 parser.add_argument('--merged_manifest', dest='merged_manifest',
77 help='file containing merged manifest for the module and its dependencies.')
78 parser.add_argument('--library', dest='library', action='store_true',
79 help='mark the module as a library.')
80 parser.add_argument('--test', dest='test', action='store_true',
81 help='mark the module as a test.')
82 parser.add_argument('--cache_dir', dest='cache_dir',
83 help='directory to use for cached file.')
Colin Crossc31efeb2020-06-23 10:25:26 -070084 parser.add_argument('--root_dir', dest='root_dir',
85 help='directory to use for root dir.')
Colin Cross014489c2020-06-02 20:09:13 -070086 group = parser.add_argument_group('check arguments', 'later arguments override earlier ones.')
87 group.add_argument('--fatal_check', dest='checks', action=check_action('fatal'), default=[],
88 help='treat a lint issue as a fatal error.')
89 group.add_argument('--error_check', dest='checks', action=check_action('error'), default=[],
90 help='treat a lint issue as an error.')
91 group.add_argument('--warning_check', dest='checks', action=check_action('warning'), default=[],
92 help='treat a lint issue as a warning.')
93 group.add_argument('--disable_check', dest='checks', action=check_action('ignore'), default=[],
94 help='disable a lint issue.')
95 return parser.parse_args()
96
97
Colin Cross014489c2020-06-02 20:09:13 -070098def write_project_xml(f, args):
99 test_attr = "test='true' " if args.test else ""
100
101 f.write("<?xml version='1.0' encoding='utf-8'?>\n")
102 f.write("<project>\n")
Colin Crossc31efeb2020-06-23 10:25:26 -0700103 if args.root_dir:
104 f.write(" <root dir='%s' />\n" % args.root_dir)
Colin Cross014489c2020-06-02 20:09:13 -0700105 f.write(" <module name='%s' android='true' %sdesugar='full' >\n" % (args.name, "library='true' " if args.library else ""))
106 if args.manifest:
107 f.write(" <manifest file='%s' %s/>\n" % (args.manifest, test_attr))
108 if args.merged_manifest:
109 f.write(" <merged-manifest file='%s' %s/>\n" % (args.merged_manifest, test_attr))
110 for src_file in args.srcs:
111 for src in NinjaRspFileReader(src_file):
112 f.write(" <src file='%s' %s/>\n" % (src, test_attr))
113 for src_file in args.generated_srcs:
114 for src in NinjaRspFileReader(src_file):
115 f.write(" <src file='%s' generated='true' %s/>\n" % (src, test_attr))
116 for res_file in args.resources:
117 for res in NinjaRspFileReader(res_file):
118 f.write(" <resource file='%s' %s/>\n" % (res, test_attr))
119 for classes in args.classes:
120 f.write(" <classes jar='%s' />\n" % classes)
121 for classpath in args.classpath:
122 f.write(" <classpath jar='%s' />\n" % classpath)
123 for extra in args.extra_checks_jars:
124 f.write(" <lint-checks jar='%s' />\n" % extra)
125 f.write(" </module>\n")
126 if args.cache_dir:
127 f.write(" <cache dir='%s'/>\n" % args.cache_dir)
128 f.write("</project>\n")
129
130
131def write_config_xml(f, args):
132 f.write("<?xml version='1.0' encoding='utf-8'?>\n")
133 f.write("<lint>\n")
134 for check in args.checks:
135 f.write(" <issue id='%s' severity='%s' />\n" % (check[1], check[0]))
136 f.write("</lint>\n")
137
138
139def main():
140 """Program entry point."""
141 args = parse_args()
142
Colin Cross014489c2020-06-02 20:09:13 -0700143 if args.project_out:
144 with open(args.project_out, 'w') as f:
145 write_project_xml(f, args)
146
147 if args.config_out:
148 with open(args.config_out, 'w') as f:
149 write_config_xml(f, args)
150
151
152if __name__ == '__main__':
153 main()