Update to latest jacoco usage

Bug: 36792868
Test: EMMA_INSTRUMENT_STATIC=true m -j

Change-Id: I419b543283b52be9a72f5c6b10e4cbea68782174
This commit is contained in:
Jeff Gaston 2017-04-11 16:36:46 -07:00
parent b253e8e854
commit aaae43cd24
10 changed files with 194 additions and 90 deletions

View file

@ -2585,20 +2585,36 @@ $(APPS_ZIP): $(INSTALLED_SYSTEMIMAGE)
zip -qjX $@ $$apps_to_zip; \
fi
#------------------------------------------------------------------
# A zip of emma code coverage meta files. Generated for fully emma
# instrumented build.
#
ifeq (true,$(EMMA_INSTRUMENT))
EMMA_META_ZIP := $(PRODUCT_OUT)/emma_meta.zip
# the dependency will be set up later in build/core/main.mk.
#------------------------------------------------------------------
# An archive of classes for use in generating code-coverage reports
# These are the uninstrumented versions of any classes that were
# to be instrumented.
# Any dependencies are set up later in build/core/main.mk.
ifeq ($(ANDROID_COMPILE_WITH_JACK),false)
JACOCO_REPORT_CLASSES_ALL := $(PRODUCT_OUT)/jacoco-report-classes-all.jar
$(JACOCO_REPORT_CLASSES_ALL) :
@echo "Collecting uninstrumented classes"
$(hide) find $(TARGET_COMMON_OUT_ROOT) $(HOST_COMMON_OUT_ROOT) -name "jacoco-report-classes.jar" | \
zip -@ -0 -q -X $@
# Meaning of these options:
# -@ scan stdin for file paths to add to the zip
# -0 don't do any compression
# -q supress most output
# -X skip storing extended file attributes
else
EMMA_META_ZIP := $(PRODUCT_OUT)/emma_meta.zip
$(EMMA_META_ZIP) :
@echo "Collecting Emma coverage meta files."
$(hide) find $(TARGET_COMMON_OUT_ROOT) $(HOST_COMMON_OUT_ROOT) -name "coverage.em" | \
zip -@ -qX $@
endif
endif # EMMA_INSTRUMENT=true
#------------------------------------------------------------------
# A zip of Proguard obfuscation dictionary files.
# Only for apps_only build.

View file

@ -85,6 +85,8 @@ LOCAL_FINDBUGS_FLAGS:=
LOCAL_FORCE_STATIC_EXECUTABLE:=
LOCAL_FULL_LIBS_MANIFEST_FILES:=
LOCAL_FULL_MANIFEST_FILE:=
LOCAL_FULL_CLASSES_JACOCO_JAR:=
LOCAL_FULL_CLASSES_PRE_JACOCO_JAR:=
LOCAL_GCNO_FILES:=
LOCAL_GENERATED_SOURCES:=
# Group static libraries with "-Wl,--start-group" and "-Wl,--end-group" when linking.

View file

@ -668,7 +668,7 @@ RELOCATION_PACKER := prebuilts/misc/$(BUILD_OS)-$(HOST_PREBUILT_ARCH)/relocation
FINDBUGS_DIR := external/owasp/sanitizer/tools/findbugs/bin
FINDBUGS := $(FINDBUGS_DIR)/findbugs
EMMA_JAR := external/emma/lib/emma$(COMMON_JAVA_PACKAGE_SUFFIX)
JACOCO_CLI_JAR := $(HOST_OUT_JAVA_LIBRARIES)/jacoco-cli$(COMMON_JAVA_PACKAGE_SUFFIX)
# Tool to merge AndroidManifest.xmls
ANDROID_MANIFEST_MERGER := $(JAVA) -classpath prebuilts/devtools/tools/lib/manifest-merger.jar com.android.manifmerger.Main merge

View file

@ -2515,12 +2515,6 @@ $(if $(PRIVATE_JAR_EXCLUDE_PACKAGES), $(hide) echo unsupported options JAR_EXCLU
$(if $(PRIVATE_JAR_MANIFEST), $(hide) echo unsupported options JAR_MANIFEST in $@; exit 53)
endef
define transform-classes.jar-to-emma
$(hide) $(JAVA) -classpath $(EMMA_JAR) emma instr -outmode fullcopy -outfile \
$(PRIVATE_EMMA_COVERAGE_FILE) -ip $< -d $(PRIVATE_EMMA_INTERMEDIATES_DIR) \
$(addprefix -ix , $(PRIVATE_EMMA_COVERAGE_FILTER))
endef
define desugar-classpath
$(filter-out -classpath -bootclasspath "",$(subst :,$(space),$(1)))
endef

View file

@ -33,16 +33,11 @@ endif
full_classes_compiled_jar := $(intermediates.COMMON)/classes-full-debug.jar
full_classes_jarjar_jar := $(intermediates.COMMON)/classes-jarjar.jar
emma_intermediates_dir := $(intermediates.COMMON)/emma_out
# emma is hardcoded to use the leaf name of its input for the output file --
# only the output directory can be changed
full_classes_emma_jar := $(emma_intermediates_dir)/lib/$(notdir $(full_classes_jarjar_jar))
full_classes_jar := $(intermediates.COMMON)/classes.jar
LOCAL_INTERMEDIATE_TARGETS += \
$(full_classes_compiled_jar) \
$(full_classes_jarjar_jar) \
$(full_classes_emma_jar)
#######################################
include $(BUILD_SYSTEM)/base_rules.mk
@ -95,23 +90,13 @@ else
full_classes_jarjar_jar := $(full_classes_compiled_jar)
endif
ifeq (true,$(LOCAL_EMMA_INSTRUMENT))
$(full_classes_emma_jar): PRIVATE_EMMA_COVERAGE_FILE := $(intermediates.COMMON)/coverage.em
$(full_classes_emma_jar): PRIVATE_EMMA_INTERMEDIATES_DIR := $(emma_intermediates_dir)
ifdef LOCAL_EMMA_COVERAGE_FILTER
$(full_classes_emma_jar): PRIVATE_EMMA_COVERAGE_FILTER := $(LOCAL_EMMA_COVERAGE_FILTER)
else
# by default, avoid applying emma instrumentation onto emma classes itself,
# otherwise there will be exceptions thrown
$(full_classes_emma_jar): PRIVATE_EMMA_COVERAGE_FILTER := *,-emma,-emmarun,-com.vladium.*
endif
# this rule will generate both $(PRIVATE_EMMA_COVERAGE_FILE) and
# $(full_classes_emma_jar)
$(full_classes_emma_jar) : $(full_classes_jarjar_jar) | $(EMMA_JAR)
$(transform-classes.jar-to-emma)
else # LOCAL_EMMA_INSTRUMENT
full_classes_emma_jar := $(full_classes_jarjar_jar)
endif # LOCAL_EMMA_INSTRUMENT
$(eval $(call copy-one-file,$(full_classes_emma_jar),$(LOCAL_BUILT_MODULE)))
$(eval $(call copy-one-file,$(full_classes_emma_jar),$(full_classes_jar)))
LOCAL_FULL_CLASSES_PRE_JACOCO_JAR := $(full_classes_jarjar_jar)
#######################################
include $(BUILD_SYSTEM)/jacoco.mk
#######################################
$(eval $(call copy-one-file,$(LOCAL_FULL_CLASSES_JACOCO_JAR),$(LOCAL_BUILT_MODULE)))
$(eval $(call copy-one-file,$(LOCAL_FULL_CLASSES_JACOCO_JAR),$(full_classes_jar)))

140
core/jacoco.mk Normal file
View file

@ -0,0 +1,140 @@
#
# Copyright (C) 2017 The Android Open Source Project
#
# 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.
#
# This file sets up Java code coverage via Jacoco
# This file is only intended to be included internally by the build system
# (at the time of authorship, it is included by java.mk and
# java_host_library.mk)
my_include_filter :=
my_exclude_filter :=
ifeq ($(LOCAL_EMMA_INSTRUMENT),true)
ifeq ($(ANDROID_COMPILE_WITH_JACK),false)
# determine Jacoco include/exclude filters
DEFAULT_JACOCO_EXCLUDE_FILTER := org/junit/*,org/jacoco/*,org/mockito/*
# copy filters from Jack but also skip some known java packages
my_include_filter := $(strip $(LOCAL_JACK_COVERAGE_INCLUDE_FILTER))
my_exclude_filter := $(strip $(DEFAULT_JACOCO_EXCLUDE_FILTER),$(LOCAL_JACK_COVERAGE_EXCLUDE_FILTER))
# replace '.' with '/' and ',' with ' ', and quote each arg
ifneq ($(strip $(my_include_filter)),)
my_include_args := $(strip $(my_include_filter))
my_include_args := $(subst .,/,$(my_include_args))
my_include_args := '$(subst $(comma),' ',$(my_include_args))'
else
my_include_args :=
endif
# replace '.' with '/' and ',' with ' ', and quote each arg
ifneq ($(strip $(my_exclude_filter)),)
my_exclude_args := $(my_exclude_filter)
my_exclude_args := $(subst .,/,$(my_exclude_args))
my_exclude_args := $(subst $(comma)$(comma),$(comma),$(my_exclude_args))
my_exclude_args := '$(subst $(comma),' ', $(my_exclude_args))'
else
my_exclude_args :=
endif
endif # ANDROID_COMPILE_WITH_JACK==false
endif # LOCAL_EMMA_INSTRUMENT == true
# determine whether to run the instrumenter based on whether there is any work
# for it to do
ifneq ($(my_include_filter),)
my_files := $(intermediates.COMMON)/jacoco
# make a task that unzips the classes that we want to instrument from the
# input jar
my_unzipped_path := $(my_files)/work/classes-to-instrument/classes
my_unzipped_timestamp_path := $(my_files)/work/classes-to-instrument/updated.stamp
$(my_unzipped_timestamp_path): PRIVATE_UNZIPPED_PATH := $(my_unzipped_path)
$(my_unzipped_timestamp_path): PRIVATE_UNZIPPED_TIMESTAMP_PATH := $(my_unzipped_timestamp_path)
$(my_unzipped_timestamp_path): PRIVATE_INCLUDE_ARGS := $(my_include_args)
$(my_unzipped_timestamp_path): PRIVATE_EXCLUDE_ARGS := $(my_exclude_args)
$(my_unzipped_timestamp_path): PRIVATE_FULL_CLASSES_PRE_JACOCO_JAR := $(LOCAL_FULL_CLASSES_PRE_JACOCO_JAR)
$(my_unzipped_timestamp_path): $(LOCAL_FULL_CLASSES_PRE_JACOCO_JAR)
rm -rf $(PRIVATE_UNZIPPED_PATH) $@
mkdir -p $(PRIVATE_UNZIPPED_PATH)
unzip -q $(PRIVATE_FULL_CLASSES_PRE_JACOCO_JAR) \
-d $(PRIVATE_UNZIPPED_PATH) \
$(PRIVATE_INCLUDE_ARGS)
rm -rf $(PRIVATE_EXCLUDE_ARGS)
touch $(PRIVATE_UNZIPPED_TIMESTAMP_PATH)
# Unfortunately in the previous task above,
# 'rm -rf $(PRIVATE_EXCLUDE_ARGS)' needs to be a separate
# shell command after 'unzip'.
# We can't just use the '-x' (exclude) option of 'unzip' because if both
# inclusions and exclusions are specified and an exclusion matches no
# inclusions, then 'unzip' exits with an error (error 11).
# We could ignore the error, but that would make the process less reliable
# make a task that zips only the classes that will be instrumented
# (for passing in to the report generator later)
my_classes_to_report_on_path := $(my_files)/report-resources/jacoco-report-classes.jar
$(my_classes_to_report_on_path): PRIVATE_UNZIPPED_PATH := $(my_unzipped_path)
$(my_classes_to_report_on_path): $(my_unzipped_timestamp_path)
rm -f $@
zip -q $@ \
-r $(PRIVATE_UNZIPPED_PATH)
# make a task that invokes instrumentation
my_instrumented_path := $(my_files)/work/instrumented/classes
my_instrumented_timestamp_path := $(my_files)/work/instrumented/updated.stamp
$(my_instrumented_timestamp_path): PRIVATE_INSTRUMENTED_PATH := $(my_instrumented_path)
$(my_instrumented_timestamp_path): PRIVATE_INSTRUMENTED_TIMESTAMP_PATH := $(my_instrumented_timestamp_path)
$(my_instrumented_timestamp_path): PRIVATE_UNZIPPED_PATH := $(my_unzipped_path)
$(my_instrumented_timestamp_path): $(my_unzipped_timestamp_path) $(JACOCO_CLI_JAR)
rm -rf $(PRIVATE_INSTRUMENTED_PATH)
mkdir -p $(PRIVATE_INSTRUMENTED_PATH)
java -jar $(JACOCO_CLI_JAR) \
instrument \
-quiet \
-dest '$(PRIVATE_INSTRUMENTED_PATH)' \
$(PRIVATE_UNZIPPED_PATH)
touch $(PRIVATE_INSTRUMENTED_TIMESTAMP_PATH)
# make a task that zips both the instrumented classes and the uninstrumented
# classes (this jar is the instrumented application to execute)
my_temp_jar_path := $(my_files)/work/usable.jar
LOCAL_FULL_CLASSES_JACOCO_JAR := $(intermediates.COMMON)/classes-jacoco.jar
$(LOCAL_FULL_CLASSES_JACOCO_JAR): PRIVATE_TEMP_JAR_PATH := $(my_temp_jar_path)
$(LOCAL_FULL_CLASSES_JACOCO_JAR): PRIVATE_INSTRUMENTED_PATH := $(my_instrumented_path)
$(LOCAL_FULL_CLASSES_JACOCO_JAR): PRIVATE_FULL_CLASSES_PRE_JACOCO_JAR := $(LOCAL_FULL_CLASSES_PRE_JACOCO_JAR)
$(LOCAL_FULL_CLASSES_JACOCO_JAR): $(my_instrumented_timestamp_path) $(LOCAL_FULL_CLASSES_PRE_JACOCO_JAR)
rm -f $@ $(PRIVATE_TEMP_JAR_PATH)
# copy the pre-jacoco jar (containing files excluded from instrumentation)
cp $(PRIVATE_FULL_CLASSES_PRE_JACOCO_JAR) $(PRIVATE_TEMP_JAR_PATH)
# copy instrumented files back into the resultant jar
$(JAR) -uf $(PRIVATE_TEMP_JAR_PATH) -C $(PRIVATE_INSTRUMENTED_PATH) .
mv $(PRIVATE_TEMP_JAR_PATH) $@
# this is used to trigger $(my_classes_to_report_on_path) to build
# when $(LOCAL_FULL_CLASSES_JACOCO_JAR) builds, but it isn't truly a
# dependency.
$(LOCAL_FULL_CLASSES_JACOCO_JAR): $(my_classes_to_report_on_path)
else # my_include_filter == ''
LOCAL_FULL_CLASSES_JACOCO_JAR := $(LOCAL_FULL_CLASSES_PRE_JACOCO_JAR)
endif # my_include_filter != ''
LOCAL_INTERMEDIATE_TARGETS += $(LOCAL_FULL_CLASSES_JACOCO_JAR)

View file

@ -114,10 +114,6 @@ full_classes_processed_jar := $(intermediates.COMMON)/classes-processed.jar
full_classes_desugar_jar := $(intermediates.COMMON)/classes-desugar.jar
jarjar_leaf := classes-jarjar.jar
full_classes_jarjar_jar := $(intermediates.COMMON)/$(jarjar_leaf)
emma_intermediates_dir := $(intermediates.COMMON)/emma_out
# emma is hardcoded to use the leaf name of its input for the output file --
# only the output directory can be changed
full_classes_emma_jar := $(emma_intermediates_dir)/lib/$(jarjar_leaf)
full_classes_proguard_jar := $(intermediates.COMMON)/classes-proguard.jar
built_dex_intermediate := $(intermediates.COMMON)/$(built_dex_intermediate_leaf)/classes.dex
full_classes_stubs_jar := $(intermediates.COMMON)/stubs.jar
@ -140,7 +136,6 @@ LOCAL_INTERMEDIATE_TARGETS += \
$(full_classes_compiled_jar) \
$(full_classes_desugar_jar) \
$(full_classes_jarjar_jar) \
$(full_classes_emma_jar) \
$(full_classes_jar) \
$(full_classes_proguard_jar) \
$(built_dex_intermediate) \
@ -504,29 +499,13 @@ else
full_classes_jarjar_jar := $(full_classes_desugar_jar)
endif
ifeq ($(LOCAL_EMMA_INSTRUMENT),true)
$(full_classes_emma_jar): PRIVATE_EMMA_COVERAGE_FILE := $(intermediates.COMMON)/coverage.emma.ignore
$(full_classes_emma_jar): PRIVATE_EMMA_INTERMEDIATES_DIR := $(emma_intermediates_dir)
# module level coverage filter can be defined using LOCAL_EMMA_COVERAGE_FILTER
# in Android.mk
ifdef LOCAL_EMMA_COVERAGE_FILTER
$(full_classes_emma_jar): PRIVATE_EMMA_COVERAGE_FILTER := $(LOCAL_EMMA_COVERAGE_FILTER)
else
# by default, avoid applying emma instrumentation onto emma classes itself,
# otherwise there will be exceptions thrown
$(full_classes_emma_jar): PRIVATE_EMMA_COVERAGE_FILTER := *,-emma,-emmarun,-com.vladium.*
endif
# this rule will generate both $(PRIVATE_EMMA_COVERAGE_FILE) and
# $(full_classes_emma_jar)
$(full_classes_emma_jar): $(full_classes_jarjar_jar) | $(EMMA_JAR)
$(transform-classes.jar-to-emma)
LOCAL_FULL_CLASSES_PRE_JACOCO_JAR := $(full_classes_jarjar_jar)
else
full_classes_emma_jar := $(full_classes_jarjar_jar)
endif
#######################################
include $(BUILD_SYSTEM)/jacoco.mk
#######################################
# TODO: this should depend on full_classes_emma_jar once coverage works again
full_classes_pre_proguard_jar := $(full_classes_jarjar_jar)
full_classes_pre_proguard_jar := $(LOCAL_FULL_CLASSES_JACOCO_JAR)
# Keep a copy of the jar just before proguard processing.
$(eval $(call copy-one-file,$(full_classes_pre_proguard_jar),$(intermediates.COMMON)/classes-pre-proguard.jar))

View file

@ -53,12 +53,7 @@ endif
ifeq (true,$(EMMA_INSTRUMENT))
ifeq (true,$(LOCAL_EMMA_INSTRUMENT))
ifeq (true,$(EMMA_INSTRUMENT_STATIC))
ifdef LOCAL_JACK_ENABLED
# Jack supports coverage with Jacoco
LOCAL_STATIC_JAVA_LIBRARIES += jacocoagent
else
LOCAL_STATIC_JAVA_LIBRARIES += emma
endif # LOCAL_JACK_ENABLED
endif # LOCAL_EMMA_INSTRUMENT
endif # EMMA_INSTRUMENT_STATIC
else

View file

@ -1006,9 +1006,13 @@ ifneq ($(TARGET_BUILD_APPS),)
$(call dist-for-goals,apps_only, $(apps_only_dist_built_files))
ifeq ($(EMMA_INSTRUMENT),true)
$(EMMA_META_ZIP) : $(apps_only_installed_files)
$(call dist-for-goals,apps_only, $(EMMA_META_ZIP))
ifeq ($(ANDROID_COMPILE_WITH_JACK),false)
$(JACOCO_REPORT_CLASSES_ALL) : $(apps_only_installed_files)
$(call dist-for-goals,apps_only, $(JACOCO_REPORT_CLASSES_ALL))
else
$(EMMA_META_ZIP) : $(apps_only_installed_files)
$(call dist-for-goals,apps_only, $(EMMA_META_ZIP))
endif
endif
$(PROGUARD_DICT_ZIP) : $(apps_only_installed_files)
@ -1065,9 +1069,13 @@ else # TARGET_BUILD_APPS
endif
ifeq ($(EMMA_INSTRUMENT),true)
$(EMMA_META_ZIP) : $(INSTALLED_SYSTEMIMAGE)
$(call dist-for-goals, dist_files, $(EMMA_META_ZIP))
ifeq ($(ANDROID_COMPILE_WITH_JACK),false)
$(JACOCO_REPORT_CLASSES_ALL) : $(INSTALLED_SYSTEMIMAGE)
$(call dist-for-goals, dist_files, $(JACOCO_REPORT_CLASSES_ALL))
else
$(EMMA_META_ZIP) : $(INSTALLED_SYSTEMIMAGE)
$(call dist-for-goals, dist_files, $(EMMA_META_ZIP))
endif
endif
# Building a full system-- the default is to build droidcore

View file

@ -246,7 +246,7 @@ endif # LOCAL_JACK_ENABLED
ifeq (true,$(EMMA_INSTRUMENT))
ifndef LOCAL_EMMA_INSTRUMENT
# No emma for test apks.
# No jacoco for test apks.
ifeq (,$(LOCAL_INSTRUMENTATION_FOR))
LOCAL_EMMA_INSTRUMENT := true
endif # No test apk
@ -257,21 +257,14 @@ endif # EMMA_INSTRUMENT is true
ifeq (true,$(LOCAL_EMMA_INSTRUMENT))
ifeq (true,$(EMMA_INSTRUMENT_STATIC))
ifdef LOCAL_JACK_ENABLED
# Jack supports coverage with Jacoco
ifneq ($(LOCAL_SRC_FILES)$(LOCAL_STATIC_JAVA_LIBRARIES)$(LOCAL_SOURCE_FILES_ALL_GENERATED),)
# Only add jacocoagent if the package contains some java code
LOCAL_STATIC_JAVA_LIBRARIES += jacocoagent
endif # Contains java code
else
LOCAL_STATIC_JAVA_LIBRARIES += emma
endif # LOCAL_JACK_ENABLED
else
ifdef LOCAL_SDK_VERSION
ifdef TARGET_BUILD_APPS
# In unbundled build, merge the coverage library into the apk.
ifdef LOCAL_JACK_ENABLED
# Jack supports coverage with Jacoco
ifneq ($(LOCAL_SRC_FILES)$(LOCAL_STATIC_JAVA_LIBRARIES)$(LOCAL_SOURCE_FILES_ALL_GENERATED),)
# Only add jacocoagent if the package contains some java code
LOCAL_STATIC_JAVA_LIBRARIES += jacocoagent
@ -279,9 +272,6 @@ LOCAL_STATIC_JAVA_LIBRARIES += jacocoagent
LOCAL_PROGUARD_FLAGS += -include $(BUILD_SYSTEM)/proguard.jacoco.flags
LOCAL_JACK_PROGUARD_FLAGS += -include $(BUILD_SYSTEM)/proguard.jacoco.flags
endif # Contains java code
else # ! LOCAL_JACK_ENABLED
LOCAL_STATIC_JAVA_LIBRARIES += emma
endif # ! LOCAL_JACK_ENABLED
else # ! TARGET_BUILD_APPS
ifdef LOCAL_JACK_ENABLED
# If build against the SDK in full build, core.jar is not used
@ -292,11 +282,6 @@ ifdef LOCAL_JACK_ENABLED
# Note: we have nothing to do for proguard since jacoco will be
# on the classpath only, thus not modified during the compilation.
LOCAL_JAVA_LIBRARIES += jacocoagent
else # ! LOCAL_JACK_ENABLED
# If build against the SDK in full build, core.jar is not used,
# we have to use prebiult emma.jar to make Proguard happy;
# Otherwise emma classes are included in core.jar.
LOCAL_PROGUARD_FLAGS += -libraryjars $(EMMA_JAR)
endif # ! LOCAL_JACK_ENABLED
endif # ! TARGET_BUILD_APPS
endif # LOCAL_SDK_VERSION