blob: 9e6fd07ba31fa047d4e8a77f54907e4af570345c [file] [log] [blame]
Jeff Gastonaaae43c2017-04-11 16:36:46 -07001#
2# Copyright (C) 2017 The Android Open Source Project
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
17# This file sets up Java code coverage via Jacoco
18# This file is only intended to be included internally by the build system
19# (at the time of authorship, it is included by java.mk and
20# java_host_library.mk)
21
22my_include_filter :=
23my_exclude_filter :=
24
25ifeq ($(LOCAL_EMMA_INSTRUMENT),true)
Colin Crossa6bc3a82017-09-27 14:28:41 -070026 # determine Jacoco include/exclude filters
27 DEFAULT_JACOCO_EXCLUDE_FILTER := org/junit/*,org/jacoco/*,org/mockito/*
28 # copy filters from Jack but also skip some known java packages
29 my_include_filter := $(strip $(LOCAL_JACK_COVERAGE_INCLUDE_FILTER))
30 my_exclude_filter := $(strip $(DEFAULT_JACOCO_EXCLUDE_FILTER),$(LOCAL_JACK_COVERAGE_EXCLUDE_FILTER))
Jeff Gastonaaae43c2017-04-11 16:36:46 -070031
Colin Crossa6bc3a82017-09-27 14:28:41 -070032 # replace '.' with '/' and ',' with ' ', and quote each arg
33 ifneq ($(strip $(my_include_filter)),)
34 my_include_args := $(strip $(my_include_filter))
Jeff Gastonaaae43c2017-04-11 16:36:46 -070035
Colin Crossa6bc3a82017-09-27 14:28:41 -070036 my_include_args := $(subst .,/,$(my_include_args))
37 my_include_args := '$(subst $(comma),' ',$(my_include_args))'
38 else
39 my_include_args :=
40 endif
Jeff Gastonaaae43c2017-04-11 16:36:46 -070041
Jeff Gastonb64f7052017-10-20 18:24:15 -070042 # replace '.' with '/' and ',' with ' '
Colin Crossa6bc3a82017-09-27 14:28:41 -070043 ifneq ($(strip $(my_exclude_filter)),)
44 my_exclude_args := $(my_exclude_filter)
Jeff Gastonaaae43c2017-04-11 16:36:46 -070045
Colin Crossa6bc3a82017-09-27 14:28:41 -070046 my_exclude_args := $(subst .,/,$(my_exclude_args))
47 my_exclude_args := $(subst $(comma)$(comma),$(comma),$(my_exclude_args))
Jeff Gastonb64f7052017-10-20 18:24:15 -070048 my_exclude_args := $(subst $(comma), ,$(my_exclude_args))
Colin Crossa6bc3a82017-09-27 14:28:41 -070049 else
50 my_exclude_args :=
51 endif
Jeff Gastonaaae43c2017-04-11 16:36:46 -070052
53 my_files := $(intermediates.COMMON)/jacoco
54
Jeff Gaston07d39022017-10-24 16:11:42 -070055 $(call validate-paths-are-subdirs,$(my_exclude_args))
56
Jeff Gastonaaae43c2017-04-11 16:36:46 -070057 # make a task that unzips the classes that we want to instrument from the
58 # input jar
59 my_unzipped_path := $(my_files)/work/classes-to-instrument/classes
60 my_unzipped_timestamp_path := $(my_files)/work/classes-to-instrument/updated.stamp
61$(my_unzipped_timestamp_path): PRIVATE_UNZIPPED_PATH := $(my_unzipped_path)
62$(my_unzipped_timestamp_path): PRIVATE_UNZIPPED_TIMESTAMP_PATH := $(my_unzipped_timestamp_path)
63$(my_unzipped_timestamp_path): PRIVATE_INCLUDE_ARGS := $(my_include_args)
64$(my_unzipped_timestamp_path): PRIVATE_EXCLUDE_ARGS := $(my_exclude_args)
65$(my_unzipped_timestamp_path): PRIVATE_FULL_CLASSES_PRE_JACOCO_JAR := $(LOCAL_FULL_CLASSES_PRE_JACOCO_JAR)
66$(my_unzipped_timestamp_path): $(LOCAL_FULL_CLASSES_PRE_JACOCO_JAR)
67 rm -rf $(PRIVATE_UNZIPPED_PATH) $@
68 mkdir -p $(PRIVATE_UNZIPPED_PATH)
69 unzip -q $(PRIVATE_FULL_CLASSES_PRE_JACOCO_JAR) \
70 -d $(PRIVATE_UNZIPPED_PATH) \
71 $(PRIVATE_INCLUDE_ARGS)
Jeff Gaston8fcf6a42017-10-20 18:33:14 -070072 (cd $(PRIVATE_UNZIPPED_PATH) && rm -rf $(PRIVATE_EXCLUDE_ARGS))
Jeff Gaston6fdbdc82017-10-25 15:55:16 -070073 (cd $(PRIVATE_UNZIPPED_PATH) && find -not -name "*.class" -type f | xargs --no-run-if-empty rm)
Jeff Gastonaaae43c2017-04-11 16:36:46 -070074 touch $(PRIVATE_UNZIPPED_TIMESTAMP_PATH)
75# Unfortunately in the previous task above,
76# 'rm -rf $(PRIVATE_EXCLUDE_ARGS)' needs to be a separate
77# shell command after 'unzip'.
78# We can't just use the '-x' (exclude) option of 'unzip' because if both
79# inclusions and exclusions are specified and an exclusion matches no
80# inclusions, then 'unzip' exits with an error (error 11).
81# We could ignore the error, but that would make the process less reliable
82
83
84 # make a task that zips only the classes that will be instrumented
85 # (for passing in to the report generator later)
86 my_classes_to_report_on_path := $(my_files)/report-resources/jacoco-report-classes.jar
87$(my_classes_to_report_on_path): PRIVATE_UNZIPPED_PATH := $(my_unzipped_path)
88$(my_classes_to_report_on_path): $(my_unzipped_timestamp_path)
89 rm -f $@
90 zip -q $@ \
91 -r $(PRIVATE_UNZIPPED_PATH)
92
93
94
95 # make a task that invokes instrumentation
96 my_instrumented_path := $(my_files)/work/instrumented/classes
97 my_instrumented_timestamp_path := $(my_files)/work/instrumented/updated.stamp
98$(my_instrumented_timestamp_path): PRIVATE_INSTRUMENTED_PATH := $(my_instrumented_path)
99$(my_instrumented_timestamp_path): PRIVATE_INSTRUMENTED_TIMESTAMP_PATH := $(my_instrumented_timestamp_path)
100$(my_instrumented_timestamp_path): PRIVATE_UNZIPPED_PATH := $(my_unzipped_path)
101$(my_instrumented_timestamp_path): $(my_unzipped_timestamp_path) $(JACOCO_CLI_JAR)
102 rm -rf $(PRIVATE_INSTRUMENTED_PATH)
103 mkdir -p $(PRIVATE_INSTRUMENTED_PATH)
104 java -jar $(JACOCO_CLI_JAR) \
105 instrument \
106 -quiet \
107 -dest '$(PRIVATE_INSTRUMENTED_PATH)' \
108 $(PRIVATE_UNZIPPED_PATH)
109 touch $(PRIVATE_INSTRUMENTED_TIMESTAMP_PATH)
110
111
112 # make a task that zips both the instrumented classes and the uninstrumented
113 # classes (this jar is the instrumented application to execute)
114 my_temp_jar_path := $(my_files)/work/usable.jar
115 LOCAL_FULL_CLASSES_JACOCO_JAR := $(intermediates.COMMON)/classes-jacoco.jar
116$(LOCAL_FULL_CLASSES_JACOCO_JAR): PRIVATE_TEMP_JAR_PATH := $(my_temp_jar_path)
117$(LOCAL_FULL_CLASSES_JACOCO_JAR): PRIVATE_INSTRUMENTED_PATH := $(my_instrumented_path)
118$(LOCAL_FULL_CLASSES_JACOCO_JAR): PRIVATE_FULL_CLASSES_PRE_JACOCO_JAR := $(LOCAL_FULL_CLASSES_PRE_JACOCO_JAR)
Colin Cross128800f2017-08-10 15:24:10 -0700119$(LOCAL_FULL_CLASSES_JACOCO_JAR): $(JAR_ARGS)
Jeff Gastonaaae43c2017-04-11 16:36:46 -0700120$(LOCAL_FULL_CLASSES_JACOCO_JAR): $(my_instrumented_timestamp_path) $(LOCAL_FULL_CLASSES_PRE_JACOCO_JAR)
121 rm -f $@ $(PRIVATE_TEMP_JAR_PATH)
122 # copy the pre-jacoco jar (containing files excluded from instrumentation)
123 cp $(PRIVATE_FULL_CLASSES_PRE_JACOCO_JAR) $(PRIVATE_TEMP_JAR_PATH)
124 # copy instrumented files back into the resultant jar
Colin Cross128800f2017-08-10 15:24:10 -0700125 $(JAR) -uf $(PRIVATE_TEMP_JAR_PATH) $(call jar-args-sorted-files-in-directory,$(PRIVATE_INSTRUMENTED_PATH))
Jeff Gastonaaae43c2017-04-11 16:36:46 -0700126 mv $(PRIVATE_TEMP_JAR_PATH) $@
127
128 # this is used to trigger $(my_classes_to_report_on_path) to build
129 # when $(LOCAL_FULL_CLASSES_JACOCO_JAR) builds, but it isn't truly a
130 # dependency.
131$(LOCAL_FULL_CLASSES_JACOCO_JAR): $(my_classes_to_report_on_path)
132
Jeff Gastone84fdb72017-10-20 18:47:25 -0700133else # LOCAL_EMMA_INSTRUMENT != true
Jeff Gastonaaae43c2017-04-11 16:36:46 -0700134 LOCAL_FULL_CLASSES_JACOCO_JAR := $(LOCAL_FULL_CLASSES_PRE_JACOCO_JAR)
Jeff Gastone84fdb72017-10-20 18:47:25 -0700135endif # LOCAL_EMMA_INSTRUMENT == true
Jeff Gastonaaae43c2017-04-11 16:36:46 -0700136
137LOCAL_INTERMEDIATE_TARGETS += $(LOCAL_FULL_CLASSES_JACOCO_JAR)