From 6ffb1179d6adf08edd64848b45b7415b6b6de43d Mon Sep 17 00:00:00 2001 From: raywilliams Date: Mon, 22 Jun 2015 09:17:37 -0700 Subject: [PATCH] Add the Errorprone Java Compiler These changes let the errorprone compiler find problems when building Android. A global flag disabled Errorprone by default. When enabled, code problems will be shown with suggestions on how to fix them. BUG=485599 Review URL: https://codereview.chromium.org/1136573002 Cr-Commit-Position: refs/heads/master@{#335509} --- DEPS | 3 + build/android/BUILD.gn | 26 +++ build/android/gyp/find_sun_tools_jar.py | 56 +++++ build/android/gyp/javac.py | 26 ++- build/android/setup.gyp | 29 +++ build/config/android/config.gni | 3 + build/config/android/internal_rules.gni | 17 +- build/config/android/rules.gni | 14 ++ build/host_jar.gypi | 27 ++- build/java.gypi | 19 ++ build/java_apk.gypi | 20 +- third_party/errorprone/BUILD.gn | 22 ++ third_party/errorprone/LICENSE | 202 ++++++++++++++++++ third_party/errorprone/OWNERS | 2 + third_party/errorprone/README.chromium | 16 ++ third_party/errorprone/errorprone.gyp | 58 +++++ .../ChromiumErrorProneCompiler.java | 57 +++++ 17 files changed, 587 insertions(+), 10 deletions(-) create mode 100644 build/android/BUILD.gn create mode 100755 build/android/gyp/find_sun_tools_jar.py create mode 100644 third_party/errorprone/BUILD.gn create mode 100644 third_party/errorprone/LICENSE create mode 100644 third_party/errorprone/OWNERS create mode 100644 third_party/errorprone/README.chromium create mode 100644 third_party/errorprone/errorprone.gyp create mode 100644 third_party/errorprone/src/org/chromium/errorprone/ChromiumErrorProneCompiler.java diff --git a/DEPS b/DEPS index 5abf4777b8d2fd..e8b32d0514c269 100644 --- a/DEPS +++ b/DEPS @@ -415,6 +415,9 @@ deps_os = { 'src/third_party/cardboard-java/src': Var('chromium_git') + '/external/github.com/googlesamples/cardboard-java.git' + '@' + '08ad25a04f2801bd822c3f2cd28301b68d74aef6', + 'src/third_party/errorprone/lib': + Var('chromium_git') + '/chromium/third_party/errorprone.git' + '@' + '6c66e56c0f9d750aef83190466df834f9d6af8ab', + 'src/third_party/findbugs': Var('chromium_git') + '/chromium/deps/findbugs.git' + '@' + '7f69fa78a6db6dc31866d09572a0e356e921bf12', diff --git a/build/android/BUILD.gn b/build/android/BUILD.gn new file mode 100644 index 00000000000000..90a8b518548537 --- /dev/null +++ b/build/android/BUILD.gn @@ -0,0 +1,26 @@ +# Copyright 2014 The Chromium Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +import("//build/config/android/rules.gni") + +sun_tools_jar_path = "$root_gen_dir/sun_tools_jar/tools.jar" + +action("find_sun_tools_jar") { + script = "//build/android/gyp/find_sun_tools_jar.py" + depfile = "$target_gen_dir/$target_name.d" + outputs = [ + depfile, + sun_tools_jar_path, + ] + args = [ + "--depfile", + rebase_path(depfile, root_build_dir), + "--output", + rebase_path(sun_tools_jar_path, root_build_dir), + ] +} + +java_prebuilt("sun_tools_java") { + jar_path = sun_tools_jar_path +} diff --git a/build/android/gyp/find_sun_tools_jar.py b/build/android/gyp/find_sun_tools_jar.py new file mode 100755 index 00000000000000..2f15a154abd891 --- /dev/null +++ b/build/android/gyp/find_sun_tools_jar.py @@ -0,0 +1,56 @@ +#!/usr/bin/env python +# +# Copyright 2014 The Chromium Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +"""This finds the java distribution's tools.jar and copies it somewhere. +""" + +import argparse +import os +import re +import shutil +import sys + +from util import build_utils + +RT_JAR_FINDER = re.compile(r'\[Opened (.*)/jre/lib/rt.jar\]') + +def main(): + parser = argparse.ArgumentParser(description='Find Sun Tools Jar') + parser.add_argument('--depfile', + help='Path to depfile. This must be specified as the ' + 'action\'s first output.') + parser.add_argument('--output', required=True) + args = parser.parse_args() + + sun_tools_jar_path = FindSunToolsJarPath() + + if sun_tools_jar_path is None: + raise Exception("Couldn\'t find tools.jar") + + # Using copyfile instead of copy() because copy() calls copymode() + # We don't want the locked mode because we may copy over this file again + shutil.copyfile(sun_tools_jar_path, args.output) + + if args.depfile: + build_utils.WriteDepfile( + args.depfile, + [sun_tools_jar_path] + build_utils.GetPythonDependencies()) + + +def FindSunToolsJarPath(): + # This works with at least openjdk 1.6, 1.7 and sun java 1.6, 1.7 + stdout = build_utils.CheckOutput( + ["java", "-verbose", "-version"], print_stderr=False) + for ln in stdout.splitlines(): + match = RT_JAR_FINDER.match(ln) + if match: + return os.path.join(match.group(1), 'lib', 'tools.jar') + + return None + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/build/android/gyp/javac.py b/build/android/gyp/javac.py index e36fd43bc05d35..c8f0dae9bd19e3 100755 --- a/build/android/gyp/javac.py +++ b/build/android/gyp/javac.py @@ -54,8 +54,20 @@ def ApplyColor(line): return '\n'.join(map(ApplyColor, output.split('\n'))) +ERRORPRONE_OPTIONS = [ + '-Xepdisable:' + # Something in chrome_private_java makes this check crash. + 'com.google.errorprone.bugpatterns.ClassCanBeStatic,' + # These crash on lots of targets. + 'com.google.errorprone.bugpatterns.WrongParameterPackage,' + 'com.google.errorprone.bugpatterns.GuiceOverridesGuiceInjectableMethod,' + 'com.google.errorprone.bugpatterns.GuiceOverridesJavaxInjectableMethod,' + 'com.google.errorprone.bugpatterns.ElementsCountedInLoop' +] + def DoJavac( - classpath, classes_dir, chromium_code, java_files): + classpath, classes_dir, chromium_code, + use_errorprone_path, java_files): """Runs javac. Builds |java_files| with the provided |classpath| and puts the generated @@ -88,7 +100,12 @@ def DoJavac( # trigger a compile warning or error. javac_args.extend(['-XDignore.symbol.file']) - javac_cmd = ['javac'] + javac_args + java_files + if use_errorprone_path: + javac_cmd = [use_errorprone_path] + ERRORPRONE_OPTIONS + else: + javac_cmd = ['javac'] + + javac_cmd = javac_cmd + javac_args + java_files def Compile(): build_utils.CheckOutput( @@ -183,6 +200,10 @@ def main(argv): help='Whether code being compiled should be built with stricter ' 'warnings for chromium code.') + parser.add_option( + '--use-errorprone-path', + help='Use the Errorprone compiler at this path.') + parser.add_option( '--classes-dir', help='Directory for compiled .class files.') @@ -241,6 +262,7 @@ def main(argv): classpath, classes_dir, options.chromium_code, + options.use_errorprone_path, java_files) if options.jar_path: diff --git a/build/android/setup.gyp b/build/android/setup.gyp index b3c3422163fd0b..0e1c2c4b492a7e 100644 --- a/build/android/setup.gyp +++ b/build/android/setup.gyp @@ -77,6 +77,35 @@ }, ], }, # build_output_dirs + { + 'target_name': 'sun_tools_java', + 'type': 'none', + 'variables': { + 'found_jar_path': '<(PRODUCT_DIR)/sun_tools_java/tools.jar', + 'jar_path': '<(found_jar_path)', + }, + 'includes': [ + '../../build/host_prebuilt_jar.gypi', + ], + 'actions': [ + { + 'action_name': 'find_sun_tools_jar', + 'variables' : { + }, + 'inputs' : [ + 'gyp/find_sun_tools_jar.py', + 'gyp/util/build_utils.py', + ], + 'outputs': [ + '<(found_jar_path)', + ], + 'action': [ + 'python', 'gyp/find_sun_tools_jar.py', + '--output', '<(found_jar_path)', + ], + }, + ], + }, # sun_tools_java ] } diff --git a/build/config/android/config.gni b/build/config/android/config.gni index e74e8fe5bd0303..d6a8ad5c7a9be2 100644 --- a/build/config/android/config.gni +++ b/build/config/android/config.gni @@ -42,6 +42,9 @@ if (is_android) { # Set to true to run findbugs on JAR targets. run_findbugs = false + + # Set to true to enable the Errorprone compiler + use_errorprone_java_compiler = false } # Host stuff ----------------------------------------------------------------- diff --git a/build/config/android/internal_rules.gni b/build/config/android/internal_rules.gni index 0933ceb8a49c9f..1988d8fbbdd168 100644 --- a/build/config/android/internal_rules.gni +++ b/build/config/android/internal_rules.gni @@ -909,6 +909,12 @@ template("compile_java") { if (defined(invoker.chromium_code)) { _chromium_code = invoker.chromium_code } + + _enable_errorprone = use_errorprone_java_compiler + if (defined(invoker.enable_errorprone)) { + _enable_errorprone = invoker.enable_errorprone + } + _manifest_entries = [] if (defined(invoker.manifest_entries)) { _manifest_entries = invoker.manifest_entries @@ -978,7 +984,13 @@ template("compile_java") { if (_chromium_code) { args += [ "--chromium-code=1" ] } - + if (_enable_errorprone) { + deps += [ "//third_party/errorprone:chromium_errorprone" ] + args += [ + "--use-errorprone-path", + "bin/chromium_errorprone", + ] + } args += rebase_path(_java_files, root_build_dir) } @@ -1128,6 +1140,9 @@ template("java_library_impl") { chromium_code = _chromium_code android = _requires_android + if (defined(invoker.enable_errorprone)) { + _enable_errorprone = invoker.enable_errorprone + } if (defined(invoker.jar_excluded_patterns)) { jar_excluded_patterns = invoker.jar_excluded_patterns } diff --git a/build/config/android/rules.gni b/build/config/android/rules.gni index 4c22fac76814df..b831c09d025117 100644 --- a/build/config/android/rules.gni +++ b/build/config/android/rules.gni @@ -798,6 +798,7 @@ template("java_strings_grd_prebuilt") { # android_library target, for example. # # chromium_code: If true, extra analysis warning/errors will be enabled. +# enable_errorprone: If true, enables the errorprone compiler. # # data_deps, testonly # @@ -825,6 +826,9 @@ template("java_binary") { if (defined(invoker.deps)) { deps = invoker.deps } + if (defined(invoker.enable_errorprone)) { + enable_errorprone = invoker.enable_errorprone + } if (defined(invoker.java_files)) { java_files = invoker.java_files } @@ -919,6 +923,8 @@ template("junit_binary") { # ease the gyp->gn conversion and will be removed in the future. # # chromium_code: If true, extra analysis warning/errors will be enabled. +# enable_errorprone: If true, enables the errorprone compiler. +# # jar_excluded_patterns: List of patterns of .class files to exclude from the # final jar. # @@ -967,6 +973,9 @@ template("java_library") { if (defined(invoker.deps)) { deps = invoker.deps } + if (defined(invoker.enable_errorprone)) { + enable_errorprone = invoker.enable_errorprone + } if (defined(invoker.jar_excluded_patterns)) { jar_excluded_patterns = invoker.jar_excluded_patterns } @@ -1058,6 +1067,8 @@ template("java_prebuilt") { # ease the gyp->gn conversion and will be removed in the future. # # chromium_code: If true, extra analysis warning/errors will be enabled. +# enable_errorprone: If true, enables the errorprone compiler. +# # jar_excluded_patterns: List of patterns of .class files to exclude from the # final jar. # @@ -1103,6 +1114,9 @@ template("android_library") { if (defined(invoker.deps)) { deps = invoker.deps } + if (defined(invoker.enable_errorprone)) { + enable_errorprone = invoker.enable_errorprone + } if (defined(invoker.jar_excluded_patterns)) { jar_excluded_patterns = invoker.jar_excluded_patterns } diff --git a/build/host_jar.gypi b/build/host_jar.gypi index 9c35177872ce92..a47f6bbdd99dd2 100644 --- a/build/host_jar.gypi +++ b/build/host_jar.gypi @@ -53,6 +53,8 @@ 'jar_path': '<(jar_dir)/<(jar_name)', 'main_class%': '', 'stamp': '<(intermediate_dir)/jar.stamp', + 'enable_errorprone%': '0', + 'errorprone_exe_path': '<(PRODUCT_DIR)/bin.java/chromium_errorprone', }, 'all_dependent_settings': { 'variables': { @@ -64,18 +66,25 @@ 'action_name': 'javac_<(_target_name)', 'message': 'Compiling <(_target_name) java sources', 'variables': { - 'extra_options': [], + 'extra_args': [], + 'extra_inputs': [], 'java_sources': [ '(main_class)'] - }] + 'extra_args': ['--main-class=>(main_class)'] + }], + ['enable_errorprone == 1', { + 'extra_inputs': [ + '<(errorprone_exe_path)', + ], + 'extra_args': [ '--use-errorprone-path=<(errorprone_exe_path)' ], + }], ], }, 'inputs': [ @@ -83,6 +92,7 @@ '<(DEPTH)/build/android/gyp/javac.py', '^@(java_sources)', '>@(input_jars_paths)', + '<@(extra_inputs)', ], 'outputs': [ '<(jar_path)', @@ -95,7 +105,7 @@ '--chromium-code=<(chromium_code)', '--stamp=<(stamp)', '--jar-path=<(jar_path)', - '<@(extra_options)', + '<@(extra_args)', '^@(java_sources)', ], }, @@ -125,7 +135,12 @@ ] } ] - }] + }], + ['enable_errorprone == 1', { + 'dependencies': [ + '<(DEPTH)/third_party/errorprone/errorprone.gyp:chromium_errorprone', + ], + }], ] } diff --git a/build/java.gypi b/build/java.gypi index cee70e56760050..a6a286dc1224b6 100644 --- a/build/java.gypi +++ b/build/java.gypi @@ -81,6 +81,8 @@ 'run_findbugs%': 0, 'proguard_config%': '', 'proguard_preprocess%': '0', + 'enable_errorprone%': '0', + 'errorprone_exe_path': '<(PRODUCT_DIR)/bin.java/chromium_errorprone', 'variables': { 'variables': { 'proguard_preprocess%': 0, @@ -244,13 +246,28 @@ }, ], }], + ['enable_errorprone == 1', { + 'dependencies': [ + '<(DEPTH)/third_party/errorprone/errorprone.gyp:chromium_errorprone', + ], + }], ], 'actions': [ { 'action_name': 'javac_<(_target_name)', 'message': 'Compiling <(_target_name) java sources', 'variables': { + 'extra_args': [], + 'extra_inputs': [], 'java_sources': ['>!@(find >(java_in_dir)/src >(additional_src_dirs) -name "*.java")'], + 'conditions': [ + ['enable_errorprone == 1', { + 'extra_inputs': [ + '<(errorprone_exe_path)', + ], + 'extra_args': [ '--use-errorprone-path=<(errorprone_exe_path)' ], + }], + ], }, 'inputs': [ '<(DEPTH)/build/android/gyp/util/build_utils.py', @@ -258,6 +275,7 @@ '>@(java_sources)', '>@(input_jars_paths)', '>@(additional_input_paths)', + '<@(extra_inputs)', ], 'outputs': [ '<(compile_stamp)', @@ -273,6 +291,7 @@ '--jar-excluded-classes=<(jar_excluded_classes)', '--stamp=<(compile_stamp)', '>@(java_sources)', + '<@(extra_args)', ] }, { diff --git a/build/java_apk.gypi b/build/java_apk.gypi index 6b92021bdfd9c6..64cd67c2cf9bfe 100644 --- a/build/java_apk.gypi +++ b/build/java_apk.gypi @@ -209,6 +209,8 @@ 'native_lib_placeholder_stamp': '<(apk_package_native_libs_dir)/<(android_app_abi)/native_lib_placeholder.stamp', 'native_lib_placeholders': [], 'main_apk_name': '<(apk_name)', + 'enable_errorprone%': '0', + 'errorprone_exe_path': '<(PRODUCT_DIR)/bin.java/chromium_errorprone', }, # Pass the jar path to the apk's "fake" jar target. This would be better as # direct_dependent_settings, but a variable set by a direct_dependent_settings @@ -251,6 +253,11 @@ '<(DEPTH)/base/base.gyp:chromium_android_linker', ], }], + ['enable_errorprone == 1', { + 'dependencies': [ + '<(DEPTH)/third_party/errorprone/errorprone.gyp:chromium_errorprone', + ], + }], ['native_lib_target != ""', { 'variables': { 'conditions': [ @@ -793,6 +800,8 @@ 'action_name': 'javac_<(_target_name)', 'message': 'Compiling java for <(_target_name)', 'variables': { + 'extra_args': [], + 'extra_inputs': [], 'gen_src_dirs': [ '<(intermediate_dir)/gen', '>@(generated_src_dirs)', @@ -808,7 +817,14 @@ # targets use the same java_in_dir and both use java_apk.gypi or # both use java.gypi.) 'java_sources': ['>!@(find >(java_in_dir)>(java_in_dir_suffix) >(additional_src_dirs) -name "*.java" # apk)'], - + 'conditions': [ + ['enable_errorprone == 1', { + 'extra_inputs': [ + '<(errorprone_exe_path)', + ], + 'extra_args': [ '--use-errorprone-path=<(errorprone_exe_path)' ], + }], + ], }, 'inputs': [ '<(DEPTH)/build/android/gyp/util/build_utils.py', @@ -816,6 +832,7 @@ '>@(java_sources)', '>@(input_jars_paths)', '<(codegen_stamp)', + '<@(extra_inputs)', ], 'conditions': [ ['native_lib_target != ""', { @@ -835,6 +852,7 @@ '--jar-path=<(javac_jar_path)', '--jar-excluded-classes=<(jar_excluded_classes)', '--stamp=<(compile_stamp)', + '<@(extra_args)', '>@(java_sources)', ], }, diff --git a/third_party/errorprone/BUILD.gn b/third_party/errorprone/BUILD.gn new file mode 100644 index 00000000000000..ff4c02cbb15172 --- /dev/null +++ b/third_party/errorprone/BUILD.gn @@ -0,0 +1,22 @@ +# Copyright 2014 The Chromium Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +import("//build/config/android/rules.gni") + +# GYP: //third_party/errorprone.gyp:errorprone_java +java_prebuilt("errorprone_java") { + jar_path = "lib/error_prone_core-1.1.2.jar" +} + +# GYP: //third_party/errorprone.gyp:chromium_errorprone +java_binary("chromium_errorprone") { + chromium_code = false + main_class = "org.chromium.errorprone.ChromiumErrorProneCompiler" + disable_errorprone = true + java_files = [ "src/org/chromium/errorprone/ChromiumErrorProneCompiler.java" ] + deps = [ + "//build/android:sun_tools_java", + ":errorprone_java", + ] +} diff --git a/third_party/errorprone/LICENSE b/third_party/errorprone/LICENSE new file mode 100644 index 00000000000000..d645695673349e --- /dev/null +++ b/third_party/errorprone/LICENSE @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/third_party/errorprone/OWNERS b/third_party/errorprone/OWNERS new file mode 100644 index 00000000000000..bfbaea912382b3 --- /dev/null +++ b/third_party/errorprone/OWNERS @@ -0,0 +1,2 @@ +cjhopman@chromium.org +jbudorick@chromium.org diff --git a/third_party/errorprone/README.chromium b/third_party/errorprone/README.chromium new file mode 100644 index 00000000000000..2b371510793098 --- /dev/null +++ b/third_party/errorprone/README.chromium @@ -0,0 +1,16 @@ +Name: Error Prone +Short Name: errorprone +URL: http://errorprone.info/ +Version: 1.1.2 +Date: May 05, 2015 +License: Apache 2.0 +License File: LICENSE +Security Critical: no + +Description: +Catch common Java mistakes as compile-time errors + +Local Modifications: +No changes to the original source +Added ChromiumErrorProneCompiler.java as a main entry point for Chromium +Added gn and gyp files diff --git a/third_party/errorprone/errorprone.gyp b/third_party/errorprone/errorprone.gyp new file mode 100644 index 00000000000000..6a1c5db7e73149 --- /dev/null +++ b/third_party/errorprone/errorprone.gyp @@ -0,0 +1,58 @@ +# Copyright 2014 The Chromium Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +{ + 'targets': [ + { + # GN: //third_party/errorprone:errorprone_java + 'target_name': 'errorprone_java', + 'type': 'none', + 'variables': { + 'jar_path': 'lib/error_prone_core-1.1.2.jar', + }, + 'includes': [ + '../../build/host_prebuilt_jar.gypi', + ] + }, + { + # GN: //third_party/errorprone:chromium_errorprone + 'target_name': 'chromium_errorprone', + 'type': 'none', + 'variables': { + 'src_paths': [ + 'src/org/chromium/errorprone/ChromiumErrorProneCompiler.java', + ], + 'enable_errorprone': 0, + }, + 'dependencies': [ + '../../build/android/setup.gyp:sun_tools_java', + 'errorprone_java', + ], + 'includes': [ + '../../build/host_jar.gypi', + ], + 'actions': [ + { + 'action_name': 'create_errorprone_binary_script', + 'inputs': [ + '<(DEPTH)/build/android/gyp/create_java_binary_script.py', + '<(DEPTH)/build/android/gyp/util/build_utils.py', + # Ensure that the script is touched when the jar is. + '<(jar_path)', + ], + 'outputs': [ + '<(PRODUCT_DIR)/bin.java/chromium_errorprone' + ], + 'action': [ + 'python', '<(DEPTH)/build/android/gyp/create_java_binary_script.py', + '--output', '<(PRODUCT_DIR)/bin.java/chromium_errorprone', + '--classpath=>@(input_jars_paths)', + '--jar-path=<(jar_path)', + '--main-class=org.chromium.errorprone.ChromiumErrorProneCompiler', + ], + }, + ], + }, + ], +} diff --git a/third_party/errorprone/src/org/chromium/errorprone/ChromiumErrorProneCompiler.java b/third_party/errorprone/src/org/chromium/errorprone/ChromiumErrorProneCompiler.java new file mode 100644 index 00000000000000..706c6412138c65 --- /dev/null +++ b/third_party/errorprone/src/org/chromium/errorprone/ChromiumErrorProneCompiler.java @@ -0,0 +1,57 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +package org.chromium.errorprone; + +import com.google.errorprone.BugPattern; +import com.google.errorprone.ErrorProneOptions; +import com.google.errorprone.ErrorProneScanner; +import com.google.errorprone.ErrorReportingJavaCompiler; +import com.google.errorprone.JDKCompatible; +import com.google.errorprone.Scanner; +import com.google.errorprone.bugpatterns.BugChecker; + +import com.sun.tools.javac.file.JavacFileManager; +import com.sun.tools.javac.main.Main; +import com.sun.tools.javac.util.Context; +import com.sun.tools.javac.util.List; + +import java.io.PrintWriter; +import java.util.HashSet; +import java.util.Set; + +import javax.tools.JavaFileObject; + +/** + * Configures (and compiles with) the error-prone java compiler. + */ +public class ChromiumErrorProneCompiler { + + public static void main(String[] args) { + System.exit(compile(args)); + } + + private static int compile(String[] args) { + PrintWriter printWriter = new PrintWriter(System.err, true); + Main main = new Main("javac (chromium-error-prone)", printWriter); + Context context = new Context(); + JavacFileManager.preRegister(context); + + ErrorProneOptions epOptions = ErrorProneOptions.processArgs(args); + final Set disabledChecks = new HashSet(epOptions.getDisabledChecks()); + + Scanner scannerInContext = new ErrorProneScanner(new ErrorProneScanner.EnabledPredicate() { + @Override + public boolean isEnabled(Class check, BugPattern annotation) { + return !disabledChecks.contains(check.getCanonicalName()); + } + }); + context.put(Scanner.class, scannerInContext); + + ErrorReportingJavaCompiler.preRegister(context); + return JDKCompatible.runCompile( + main, epOptions.getRemainingArgs(), context, List.nil(), null); + } + +}