blob: 8915443e9f23a8e4c3e152b4c0ef9ee42dba21a8 [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
Colin Crossa6bc3a82017-09-27 14:28:41 -070042 # replace '.' with '/' and ',' with ' ', and quote each arg
43 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))
48 my_exclude_args := '$(subst $(comma),' ', $(my_exclude_args))'
49 else
50 my_exclude_args :=
51 endif
Jeff Gastonaaae43c2017-04-11 16:36:46 -070052endif # LOCAL_EMMA_INSTRUMENT == true
53
54# determine whether to run the instrumenter based on whether there is any work
55# for it to do
56ifneq ($(my_include_filter),)
57
58 my_files := $(intermediates.COMMON)/jacoco
59
60 # make a task that unzips the classes that we want to instrument from the
61 # input jar
62 my_unzipped_path := $(my_files)/work/classes-to-instrument/classes
63 my_unzipped_timestamp_path := $(my_files)/work/classes-to-instrument/updated.stamp
64$(my_unzipped_timestamp_path): PRIVATE_UNZIPPED_PATH := $(my_unzipped_path)
65$(my_unzipped_timestamp_path): PRIVATE_UNZIPPED_TIMESTAMP_PATH := $(my_unzipped_timestamp_path)
66$(my_unzipped_timestamp_path): PRIVATE_INCLUDE_ARGS := $(my_include_args)
67$(my_unzipped_timestamp_path): PRIVATE_EXCLUDE_ARGS := $(my_exclude_args)
68$(my_unzipped_timestamp_path): PRIVATE_FULL_CLASSES_PRE_JACOCO_JAR := $(LOCAL_FULL_CLASSES_PRE_JACOCO_JAR)
69$(my_unzipped_timestamp_path): $(LOCAL_FULL_CLASSES_PRE_JACOCO_JAR)
70 rm -rf $(PRIVATE_UNZIPPED_PATH) $@
71 mkdir -p $(PRIVATE_UNZIPPED_PATH)
72 unzip -q $(PRIVATE_FULL_CLASSES_PRE_JACOCO_JAR) \
73 -d $(PRIVATE_UNZIPPED_PATH) \
74 $(PRIVATE_INCLUDE_ARGS)
75 rm -rf $(PRIVATE_EXCLUDE_ARGS)
76 touch $(PRIVATE_UNZIPPED_TIMESTAMP_PATH)
77# Unfortunately in the previous task above,
78# 'rm -rf $(PRIVATE_EXCLUDE_ARGS)' needs to be a separate
79# shell command after 'unzip'.
80# We can't just use the '-x' (exclude) option of 'unzip' because if both
81# inclusions and exclusions are specified and an exclusion matches no
82# inclusions, then 'unzip' exits with an error (error 11).
83# We could ignore the error, but that would make the process less reliable
84
85
86 # make a task that zips only the classes that will be instrumented
87 # (for passing in to the report generator later)
88 my_classes_to_report_on_path := $(my_files)/report-resources/jacoco-report-classes.jar
89$(my_classes_to_report_on_path): PRIVATE_UNZIPPED_PATH := $(my_unzipped_path)
90$(my_classes_to_report_on_path): $(my_unzipped_timestamp_path)
91 rm -f $@
92 zip -q $@ \
93 -r $(PRIVATE_UNZIPPED_PATH)
94
95
96
97 # make a task that invokes instrumentation
98 my_instrumented_path := $(my_files)/work/instrumented/classes
99 my_instrumented_timestamp_path := $(my_files)/work/instrumented/updated.stamp
100$(my_instrumented_timestamp_path): PRIVATE_INSTRUMENTED_PATH := $(my_instrumented_path)
101$(my_instrumented_timestamp_path): PRIVATE_INSTRUMENTED_TIMESTAMP_PATH := $(my_instrumented_timestamp_path)
102$(my_instrumented_timestamp_path): PRIVATE_UNZIPPED_PATH := $(my_unzipped_path)
103$(my_instrumented_timestamp_path): $(my_unzipped_timestamp_path) $(JACOCO_CLI_JAR)
104 rm -rf $(PRIVATE_INSTRUMENTED_PATH)
105 mkdir -p $(PRIVATE_INSTRUMENTED_PATH)
106 java -jar $(JACOCO_CLI_JAR) \
107 instrument \
108 -quiet \
109 -dest '$(PRIVATE_INSTRUMENTED_PATH)' \
110 $(PRIVATE_UNZIPPED_PATH)
111 touch $(PRIVATE_INSTRUMENTED_TIMESTAMP_PATH)
112
113
114 # make a task that zips both the instrumented classes and the uninstrumented
115 # classes (this jar is the instrumented application to execute)
116 my_temp_jar_path := $(my_files)/work/usable.jar
117 LOCAL_FULL_CLASSES_JACOCO_JAR := $(intermediates.COMMON)/classes-jacoco.jar
118$(LOCAL_FULL_CLASSES_JACOCO_JAR): PRIVATE_TEMP_JAR_PATH := $(my_temp_jar_path)
119$(LOCAL_FULL_CLASSES_JACOCO_JAR): PRIVATE_INSTRUMENTED_PATH := $(my_instrumented_path)
120$(LOCAL_FULL_CLASSES_JACOCO_JAR): PRIVATE_FULL_CLASSES_PRE_JACOCO_JAR := $(LOCAL_FULL_CLASSES_PRE_JACOCO_JAR)
Colin Cross128800f2017-08-10 15:24:10 -0700121$(LOCAL_FULL_CLASSES_JACOCO_JAR): $(JAR_ARGS)
Jeff Gastonaaae43c2017-04-11 16:36:46 -0700122$(LOCAL_FULL_CLASSES_JACOCO_JAR): $(my_instrumented_timestamp_path) $(LOCAL_FULL_CLASSES_PRE_JACOCO_JAR)
123 rm -f $@ $(PRIVATE_TEMP_JAR_PATH)
124 # copy the pre-jacoco jar (containing files excluded from instrumentation)
125 cp $(PRIVATE_FULL_CLASSES_PRE_JACOCO_JAR) $(PRIVATE_TEMP_JAR_PATH)
126 # copy instrumented files back into the resultant jar
Colin Cross128800f2017-08-10 15:24:10 -0700127 $(JAR) -uf $(PRIVATE_TEMP_JAR_PATH) $(call jar-args-sorted-files-in-directory,$(PRIVATE_INSTRUMENTED_PATH))
Jeff Gastonaaae43c2017-04-11 16:36:46 -0700128 mv $(PRIVATE_TEMP_JAR_PATH) $@
129
130 # this is used to trigger $(my_classes_to_report_on_path) to build
131 # when $(LOCAL_FULL_CLASSES_JACOCO_JAR) builds, but it isn't truly a
132 # dependency.
133$(LOCAL_FULL_CLASSES_JACOCO_JAR): $(my_classes_to_report_on_path)
134
135else # my_include_filter == ''
136 LOCAL_FULL_CLASSES_JACOCO_JAR := $(LOCAL_FULL_CLASSES_PRE_JACOCO_JAR)
137endif # my_include_filter != ''
138
139LOCAL_INTERMEDIATE_TARGETS += $(LOCAL_FULL_CLASSES_JACOCO_JAR)