From 5e587937310b5e7fc57af3f20989cdbd73f1b437 Mon Sep 17 00:00:00 2001
From: Colin Cross
Date: Fri, 7 Apr 2017 17:46:40 -0700
Subject: [PATCH 1/2] Add support for LOCAL_ERROR_PRONE_FLAGS
This will allow individual modules to enable error prone checks
as needed so that the change to treat an error prone check as a
compiler error can be done atomically with the changes to fix
the issues that check reveals.
Tested with the following steps, cleaning up the targets in
between to ensure that it was rebuilt everytime.
Set LOCAL_ERROR_PRONE_FLAGS := -Xep:ClassCanBeStatic:WARN
compiler error in junit-params-test target. Built
javac-check-junit-params-test with RUN_ERROR_PRONE=true to make
sure that compilation failed.
Built the javac-check-junit-params-test without RUN_ERROR_PRONE
set to make sure that the flag is not included unless explicitly
requested.
Built the junit-params-test as normal to make sure it still
worked properly.
Bug: 31507496
Test: See above
Change-Id: I88b5800dacd0e30dd9dcfa5f42e89d1428957365
Merged-In: I88b5800dacd0e30dd9dcfa5f42e89d1428957365
(cherry picked from commit 3ca9298d994103549ceed4d42ae54debd922aa0b)
---
core/build-system.html | 5 +++++
core/clear_vars.mk | 1 +
core/host_java_library.mk | 5 +++++
core/java.mk | 6 ++++++
4 files changed, 17 insertions(+)
diff --git a/core/build-system.html b/core/build-system.html
index 95f35ce82..e72e1411b 100644
--- a/core/build-system.html
+++ b/core/build-system.html
@@ -707,6 +707,11 @@ example.
them here. For example:
LOCAL_JAVACFLAGS += -Xlint:deprecation
+LOCAL_ERROR_PRONE_FLAGS
+If you have additional flags to pass into the error prone compiler, add
+them here. For example:
+LOCAL_ERROR_PRONE_FLAGS += -Xep:ClassCanBeStatic:ERROR
+
LOCAL_JAVA_LIBRARIES
When linking Java apps and libraries, LOCAL_JAVA_LIBRARIES
specifies which sets of java classes to include. Currently there are
diff --git a/core/clear_vars.mk b/core/clear_vars.mk
index 85431f565..3619106fa 100644
--- a/core/clear_vars.mk
+++ b/core/clear_vars.mk
@@ -68,6 +68,7 @@ LOCAL_DROIDDOC_USE_STANDARD_DOCLET:=
LOCAL_DX_FLAGS:=
LOCAL_EMMA_COVERAGE_FILTER:=
LOCAL_EMMA_INSTRUMENT:=
+LOCAL_ERROR_PRONE_FLAGS:=
LOCAL_EXPORT_CFLAGS:=
LOCAL_EXPORT_C_INCLUDE_DEPS:=
LOCAL_EXPORT_C_INCLUDE_DIRS:=
diff --git a/core/host_java_library.mk b/core/host_java_library.mk
index 3e70eba4e..f1da55391 100644
--- a/core/host_java_library.mk
+++ b/core/host_java_library.mk
@@ -58,6 +58,11 @@ include $(BUILD_SYSTEM)/java_common.mk
# Run build/tools/java-layers.py for more details.
layers_file := $(addprefix $(LOCAL_PATH)/, $(LOCAL_JAVA_LAYERS_FILE))
+# If error prone is enabled then add LOCAL_ERROR_PRONE_FLAGS to LOCAL_JAVACFLAGS
+ifeq ($(RUN_ERROR_PRONE),true)
+LOCAL_JAVACFLAGS += $(LOCAL_ERROR_PRONE_FLAGS)
+endif
+
$(full_classes_compiled_jar): PRIVATE_JAVA_LAYERS_FILE := $(layers_file)
$(full_classes_compiled_jar): PRIVATE_JAVACFLAGS := $(GLOBAL_JAVAC_DEBUG_FLAGS) $(LOCAL_JAVACFLAGS)
$(full_classes_compiled_jar): PRIVATE_JAR_EXCLUDE_FILES :=
diff --git a/core/java.mk b/core/java.mk
index bbbec0a3d..f7f7eee41 100644
--- a/core/java.mk
+++ b/core/java.mk
@@ -418,6 +418,12 @@ $(full_classes_compiled_jar): PRIVATE_WARNINGS_ENABLE := $(LOCAL_WARNINGS_ENABLE
# This intentionally depends on java_sources, not all_java_sources.
# Deps for generated source files must be handled separately,
# via deps on the target that generates the sources.
+
+# If error prone is enabled then add LOCAL_ERROR_PRONE_FLAGS to LOCAL_JAVACFLAGS
+ifeq ($(RUN_ERROR_PRONE),true)
+LOCAL_JAVACFLAGS += $(LOCAL_ERROR_PRONE_FLAGS)
+endif
+
$(full_classes_compiled_jar): PRIVATE_JAVACFLAGS := $(GLOBAL_JAVAC_DEBUG_FLAGS) $(LOCAL_JAVACFLAGS)
$(full_classes_compiled_jar): PRIVATE_JAR_EXCLUDE_FILES := $(LOCAL_JAR_EXCLUDE_FILES)
$(full_classes_compiled_jar): PRIVATE_JAR_PACKAGES := $(LOCAL_JAR_PACKAGES)
From cde14987ba2a4a054ba4e2f711a4b714dfc54853 Mon Sep 17 00:00:00 2001
From: Colin Cross
Date: Fri, 7 Apr 2017 16:28:41 -0700
Subject: [PATCH 2/2] Support java annotation proccessors
The codebase is growing more uses of annotation processors,
through a mixture of LOCAL_ADDITIONAL_DEPENDENCIES and
LOCAL_JACK_FLAGS. Add LOCAL_ANNOTATION_PROCESSORS and handle
the rest in the build system.
For now we also need LOCAL_ANNOTATION_PROCESSOR_CLASSES to
work around bad behavior in grok (b/25860419).
Test: m -j java
Change-Id: I465b021e65d25ed3ea517333a82ec4ebd63fc038
---
core/build-system.html | 11 +++++++++++
core/clear_vars.mk | 2 ++
core/definitions.mk | 8 ++++----
core/host_dalvik_java_library.mk | 3 ++-
core/host_java_library.mk | 3 ++-
core/java.mk | 8 +++++---
core/java_common.mk | 15 +++++++++++++++
7 files changed, 41 insertions(+), 9 deletions(-)
diff --git a/core/build-system.html b/core/build-system.html
index e72e1411b..c7938ccfb 100644
--- a/core/build-system.html
+++ b/core/build-system.html
@@ -592,6 +592,17 @@ alphabetically.
+LOCAL_ANNOTATION_PROCESSORS
+Set this to a list of modules built with BUILD_HOST_JAVA_LIBRARY
+to have their jars passed to javac with -processorpath for use as annotation
+processors.
+
+LOCAL_ANNOTATION_PROCESSOR_CLASSES
+Set this to a list of classes to be passed to javac as -processor arguments.
+This list is would be unnecessary, as javac will autodetect annotation processor
+classes, except that the Grok tool that is used on the Android source code
+does not autodetect them and requires listing them manually.
+
LOCAL_ASSET_FILES
In Android.mk files that include $(BUILD_PACKAGE)
set this
to the set of files you want built into your app. Usually:
diff --git a/core/clear_vars.mk b/core/clear_vars.mk
index 3619106fa..a4751fd42 100644
--- a/core/clear_vars.mk
+++ b/core/clear_vars.mk
@@ -12,6 +12,8 @@ LOCAL_ADDITIONAL_HTML_DIR:=
LOCAL_ADDITIONAL_JAVA_DIR:=
LOCAL_AIDL_INCLUDES:=
LOCAL_ALLOW_UNDEFINED_SYMBOLS:=
+LOCAL_ANNOTATION_PROCESSORS:=
+LOCAL_ANNOTATION_PROCESSOR_CLASSES:=
LOCAL_APK_LIBRARIES:=
LOCAL_ARM_MODE:=
LOCAL_ASFLAGS:=
diff --git a/core/definitions.mk b/core/definitions.mk
index 94b043694..3e3bf7260 100644
--- a/core/definitions.mk
+++ b/core/definitions.mk
@@ -2188,9 +2188,9 @@ endef
# $(2): bootclasspath
define compile-java
$(hide) rm -f $@
-$(hide) rm -rf $(PRIVATE_CLASS_INTERMEDIATES_DIR)
+$(hide) rm -rf $(PRIVATE_CLASS_INTERMEDIATES_DIR) $(PRIVATE_ANNO_INTERMEDIATES_DIR)
$(hide) mkdir -p $(dir $@)
-$(hide) mkdir -p $(PRIVATE_CLASS_INTERMEDIATES_DIR)
+$(hide) mkdir -p $(PRIVATE_CLASS_INTERMEDIATES_DIR) $(PRIVATE_ANNO_INTERMEDIATES_DIR)
$(call unzip-jar-files,$(PRIVATE_STATIC_JAVA_LIBRARIES),$(PRIVATE_CLASS_INTERMEDIATES_DIR))
$(call dump-words-to-file,$(PRIVATE_JAVA_SOURCES),$(PRIVATE_CLASS_INTERMEDIATES_DIR)/java-source-list)
$(hide) if [ -d "$(PRIVATE_SOURCE_INTERMEDIATES_DIR)" ]; then \
@@ -2209,7 +2209,7 @@ $(hide) if [ -s $(PRIVATE_CLASS_INTERMEDIATES_DIR)/java-source-list-uniq ] ; the
$(addprefix -classpath ,$(strip \
$(call normalize-path-list,$(PRIVATE_ALL_JAVA_LIBRARIES)))) \
$(if $(findstring true,$(PRIVATE_WARNINGS_ENABLE)),$(xlint_unchecked),) \
- -extdirs "" -d $(PRIVATE_CLASS_INTERMEDIATES_DIR) \
+ -extdirs "" -d $(PRIVATE_CLASS_INTERMEDIATES_DIR) -s $(PRIVATE_ANNO_INTERMEDIATES_DIR) \
$(PRIVATE_JAVACFLAGS) \
\@$(PRIVATE_CLASS_INTERMEDIATES_DIR)/java-source-list-uniq \
|| ( rm -rf $(PRIVATE_CLASS_INTERMEDIATES_DIR) ; exit 41 ) \
@@ -3334,4 +3334,4 @@ $(foreach source,$(ENFORCE_RRO_SOURCES), \
$(eval include $(BUILD_SYSTEM)/generate_enforce_rro.mk) \
$(eval ALL_MODULES.$(enforce_rro_source_module).REQUIRED += $(enforce_rro_module)) \
)
-endef
\ No newline at end of file
+endef
diff --git a/core/host_dalvik_java_library.mk b/core/host_dalvik_java_library.mk
index 34e88cebf..a7c1ad692 100644
--- a/core/host_dalvik_java_library.mk
+++ b/core/host_dalvik_java_library.mk
@@ -86,7 +86,7 @@ $(cleantarget): PRIVATE_CLEAN_FILES += $(intermediates.COMMON)
ifndef LOCAL_JACK_ENABLED
$(full_classes_compiled_jar): PRIVATE_JAVA_LAYERS_FILE := $(layers_file)
-$(full_classes_compiled_jar): PRIVATE_JAVACFLAGS := $(GLOBAL_JAVAC_DEBUG_FLAGS) $(LOCAL_JAVACFLAGS)
+$(full_classes_compiled_jar): PRIVATE_JAVACFLAGS := $(GLOBAL_JAVAC_DEBUG_FLAGS) $(LOCAL_JAVACFLAGS) $(annotation_processor_flags)
$(full_classes_compiled_jar): PRIVATE_JAR_EXCLUDE_FILES :=
$(full_classes_compiled_jar): PRIVATE_JAR_PACKAGES :=
$(full_classes_compiled_jar): PRIVATE_JAR_EXCLUDE_PACKAGES :=
@@ -96,6 +96,7 @@ $(full_classes_compiled_jar): \
$(full_java_lib_deps) \
$(jar_manifest_file) \
$(proto_java_sources_file_stamp) \
+ $(annotation_processor_deps) \
$(LOCAL_ADDITIONAL_DEPENDENCIES)
$(transform-host-java-to-package)
diff --git a/core/host_java_library.mk b/core/host_java_library.mk
index f1da55391..9f640b078 100644
--- a/core/host_java_library.mk
+++ b/core/host_java_library.mk
@@ -64,7 +64,7 @@ LOCAL_JAVACFLAGS += $(LOCAL_ERROR_PRONE_FLAGS)
endif
$(full_classes_compiled_jar): PRIVATE_JAVA_LAYERS_FILE := $(layers_file)
-$(full_classes_compiled_jar): PRIVATE_JAVACFLAGS := $(GLOBAL_JAVAC_DEBUG_FLAGS) $(LOCAL_JAVACFLAGS)
+$(full_classes_compiled_jar): PRIVATE_JAVACFLAGS := $(GLOBAL_JAVAC_DEBUG_FLAGS) $(LOCAL_JAVACFLAGS) $(annotation_processor_flags)
$(full_classes_compiled_jar): PRIVATE_JAR_EXCLUDE_FILES :=
$(full_classes_compiled_jar): PRIVATE_JAR_PACKAGES :=
$(full_classes_compiled_jar): PRIVATE_JAR_EXCLUDE_PACKAGES :=
@@ -74,6 +74,7 @@ $(full_classes_compiled_jar): \
$(full_java_lib_deps) \
$(jar_manifest_file) \
$(proto_java_sources_file_stamp) \
+ $(annotation_processor_deps) \
$(NORMALIZE_PATH) \
$(LOCAL_ADDITIONAL_DEPENDENCIES)
$(transform-host-java-to-package)
diff --git a/core/java.mk b/core/java.mk
index f7f7eee41..b9c48c6b4 100644
--- a/core/java.mk
+++ b/core/java.mk
@@ -424,7 +424,7 @@ ifeq ($(RUN_ERROR_PRONE),true)
LOCAL_JAVACFLAGS += $(LOCAL_ERROR_PRONE_FLAGS)
endif
-$(full_classes_compiled_jar): PRIVATE_JAVACFLAGS := $(GLOBAL_JAVAC_DEBUG_FLAGS) $(LOCAL_JAVACFLAGS)
+$(full_classes_compiled_jar): PRIVATE_JAVACFLAGS := $(GLOBAL_JAVAC_DEBUG_FLAGS) $(LOCAL_JAVACFLAGS) $(annotation_processor_flags)
$(full_classes_compiled_jar): PRIVATE_JAR_EXCLUDE_FILES := $(LOCAL_JAR_EXCLUDE_FILES)
$(full_classes_compiled_jar): PRIVATE_JAR_PACKAGES := $(LOCAL_JAR_PACKAGES)
$(full_classes_compiled_jar): PRIVATE_JAR_EXCLUDE_PACKAGES := $(LOCAL_JAR_EXCLUDE_PACKAGES)
@@ -437,6 +437,7 @@ $(full_classes_compiled_jar): \
$(layers_file) \
$(RenderScript_file_stamp) \
$(proto_java_sources_file_stamp) \
+ $(annotation_processor_deps) \
$(NORMALIZE_PATH) \
$(LOCAL_ADDITIONAL_DEPENDENCIES)
$(transform-java-to-classes.jar)
@@ -730,13 +731,14 @@ else # LOCAL_PROGUARD_ENABLED not defined
$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_JACK_PROGUARD_FLAGS :=
endif # LOCAL_PROGUARD_ENABLED defined
-$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_JACK_FLAGS := $(GLOBAL_JAVAC_DEBUG_FLAGS) $(LOCAL_JACK_FLAGS)
+$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_JACK_FLAGS := $(GLOBAL_JAVAC_DEBUG_FLAGS) $(LOCAL_JACK_FLAGS) $(annotation_processor_flags)
$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_JACK_VERSION := $(LOCAL_JACK_VERSION)
jack_all_deps := $(java_sources) $(java_resource_sources) $(full_jack_deps) \
$(jar_manifest_file) $(layers_file) $(RenderScript_file_stamp) \
$(common_proguard_flag_files) $(proguard_flag_files) \
- $(proto_java_sources_file_stamp) $(LOCAL_ADDITIONAL_DEPENDENCIES) $(LOCAL_JARJAR_RULES) \
+ $(proto_java_sources_file_stamp) $(annotation_processor_deps) \
+ $(LOCAL_ADDITIONAL_DEPENDENCIES) $(LOCAL_JARJAR_RULES) \
$(NORMALIZE_PATH) $(JACK_DEFAULT_ARGS) $(JACK)
$(jack_check_timestamp): $(jack_all_deps) | setup-jack-server
diff --git a/core/java_common.mk b/core/java_common.mk
index 67943a451..600208a66 100644
--- a/core/java_common.mk
+++ b/core/java_common.mk
@@ -148,6 +148,20 @@ endif
need_compile_java := $(strip $(all_java_sources)$(all_res_assets)$(java_resource_sources))$(LOCAL_STATIC_JAVA_LIBRARIES)$(filter true,$(LOCAL_SOURCE_FILES_ALL_GENERATED))
ifdef need_compile_java
+annotation_processor_flags :=
+annotation_processor_deps :=
+
+ifdef LOCAL_ANNOTATION_PROCESSORS
+ annotation_processor_jars := $(call java-lib-deps,$(LOCAL_ANNOTATION_PROCESSORS),true)
+ annotation_processor_flags += -processorpath $(call normalize-path-list,$(annotation_processor_jars))
+ annotation_processor_deps += $(annotation_processor_jars)
+
+ # b/25860419: annotation processors must be explicitly specified for grok
+ annotation_processor_flags += $(foreach class,$(LOCAL_ANNOTATION_PROCESSOR_CLASSES),-processor $(class))
+
+ annotation_processor_jars :=
+endif
+
full_static_java_libs := \
$(foreach lib,$(LOCAL_STATIC_JAVA_LIBRARIES), \
$(call intermediates-dir-for, \
@@ -159,6 +173,7 @@ $(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_RESOURCE_DIR := $(LOCAL_RESOURCE_DIR)
$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_ASSET_DIR := $(LOCAL_ASSET_DIR)
$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_CLASS_INTERMEDIATES_DIR := $(intermediates.COMMON)/classes
+$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_ANNO_INTERMEDIATES_DIR := $(intermediates.COMMON)/anno
$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_SOURCE_INTERMEDIATES_DIR := $(intermediates.COMMON)/src
$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_HAS_PROTO_SOURCES := $(if $(proto_sources),true)
$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_PROTO_SOURCE_INTERMEDIATES_DIR := $(intermediates.COMMON)/proto