blob: 33d45d5e7718349195141fab9f04a8a0444df32d [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
55 # make a task that unzips the classes that we want to instrument from the
56 # input jar
57 my_unzipped_path := $(my_files)/work/classes-to-instrument/classes
58 my_unzipped_timestamp_path := $(my_files)/work/classes-to-instrument/updated.stamp
59$(my_unzipped_timestamp_path): PRIVATE_UNZIPPED_PATH := $(my_unzipped_path)
60$(my_unzipped_timestamp_path): PRIVATE_UNZIPPED_TIMESTAMP_PATH := $(my_unzipped_timestamp_path)
61$(my_unzipped_timestamp_path): PRIVATE_INCLUDE_ARGS := $(my_include_args)
62$(my_unzipped_timestamp_path): PRIVATE_EXCLUDE_ARGS := $(my_exclude_args)
63$(my_unzipped_timestamp_path): PRIVATE_FULL_CLASSES_PRE_JACOCO_JAR := $(LOCAL_FULL_CLASSES_PRE_JACOCO_JAR)
64$(my_unzipped_timestamp_path): $(LOCAL_FULL_CLASSES_PRE_JACOCO_JAR)
65 rm -rf $(PRIVATE_UNZIPPED_PATH) $@
66 mkdir -p $(PRIVATE_UNZIPPED_PATH)
67 unzip -q $(PRIVATE_FULL_CLASSES_PRE_JACOCO_JAR) \
68 -d $(PRIVATE_UNZIPPED_PATH) \
69 $(PRIVATE_INCLUDE_ARGS)
Jeff Gaston8fcf6a42017-10-20 18:33:14 -070070 (cd $(PRIVATE_UNZIPPED_PATH) && rm -rf $(PRIVATE_EXCLUDE_ARGS))
Jeff Gaston6fdbdc82017-10-25 15:55:16 -070071 (cd $(PRIVATE_UNZIPPED_PATH) && find -not -name "*.class" -type f | xargs --no-run-if-empty rm)
Jeff Gastonaaae43c2017-04-11 16:36:46 -070072 touch $(PRIVATE_UNZIPPED_TIMESTAMP_PATH)
73# Unfortunately in the previous task above,
74# 'rm -rf $(PRIVATE_EXCLUDE_ARGS)' needs to be a separate
75# shell command after 'unzip'.
76# We can't just use the '-x' (exclude) option of 'unzip' because if both
77# inclusions and exclusions are specified and an exclusion matches no
78# inclusions, then 'unzip' exits with an error (error 11).
79# We could ignore the error, but that would make the process less reliable
80
81
82 # make a task that zips only the classes that will be instrumented
83 # (for passing in to the report generator later)
84 my_classes_to_report_on_path := $(my_files)/report-resources/jacoco-report-classes.jar
85$(my_classes_to_report_on_path): PRIVATE_UNZIPPED_PATH := $(my_unzipped_path)
86$(my_classes_to_report_on_path): $(my_unzipped_timestamp_path)
87 rm -f $@
88 zip -q $@ \
89 -r $(PRIVATE_UNZIPPED_PATH)
90
91
92
93 # make a task that invokes instrumentation
94 my_instrumented_path := $(my_files)/work/instrumented/classes
95 my_instrumented_timestamp_path := $(my_files)/work/instrumented/updated.stamp
96$(my_instrumented_timestamp_path): PRIVATE_INSTRUMENTED_PATH := $(my_instrumented_path)
97$(my_instrumented_timestamp_path): PRIVATE_INSTRUMENTED_TIMESTAMP_PATH := $(my_instrumented_timestamp_path)
98$(my_instrumented_timestamp_path): PRIVATE_UNZIPPED_PATH := $(my_unzipped_path)
99$(my_instrumented_timestamp_path): $(my_unzipped_timestamp_path) $(JACOCO_CLI_JAR)
100 rm -rf $(PRIVATE_INSTRUMENTED_PATH)
101 mkdir -p $(PRIVATE_INSTRUMENTED_PATH)
102 java -jar $(JACOCO_CLI_JAR) \
103 instrument \
104 -quiet \
105 -dest '$(PRIVATE_INSTRUMENTED_PATH)' \
106 $(PRIVATE_UNZIPPED_PATH)
107 touch $(PRIVATE_INSTRUMENTED_TIMESTAMP_PATH)
108
109
110 # make a task that zips both the instrumented classes and the uninstrumented
111 # classes (this jar is the instrumented application to execute)
112 my_temp_jar_path := $(my_files)/work/usable.jar
113 LOCAL_FULL_CLASSES_JACOCO_JAR := $(intermediates.COMMON)/classes-jacoco.jar
114$(LOCAL_FULL_CLASSES_JACOCO_JAR): PRIVATE_TEMP_JAR_PATH := $(my_temp_jar_path)
115$(LOCAL_FULL_CLASSES_JACOCO_JAR): PRIVATE_INSTRUMENTED_PATH := $(my_instrumented_path)
116$(LOCAL_FULL_CLASSES_JACOCO_JAR): PRIVATE_FULL_CLASSES_PRE_JACOCO_JAR := $(LOCAL_FULL_CLASSES_PRE_JACOCO_JAR)
Colin Cross128800f2017-08-10 15:24:10 -0700117$(LOCAL_FULL_CLASSES_JACOCO_JAR): $(JAR_ARGS)
Jeff Gastonaaae43c2017-04-11 16:36:46 -0700118$(LOCAL_FULL_CLASSES_JACOCO_JAR): $(my_instrumented_timestamp_path) $(LOCAL_FULL_CLASSES_PRE_JACOCO_JAR)
119 rm -f $@ $(PRIVATE_TEMP_JAR_PATH)
120 # copy the pre-jacoco jar (containing files excluded from instrumentation)
121 cp $(PRIVATE_FULL_CLASSES_PRE_JACOCO_JAR) $(PRIVATE_TEMP_JAR_PATH)
122 # copy instrumented files back into the resultant jar
Colin Cross128800f2017-08-10 15:24:10 -0700123 $(JAR) -uf $(PRIVATE_TEMP_JAR_PATH) $(call jar-args-sorted-files-in-directory,$(PRIVATE_INSTRUMENTED_PATH))
Jeff Gastonaaae43c2017-04-11 16:36:46 -0700124 mv $(PRIVATE_TEMP_JAR_PATH) $@
125
126 # this is used to trigger $(my_classes_to_report_on_path) to build
127 # when $(LOCAL_FULL_CLASSES_JACOCO_JAR) builds, but it isn't truly a
128 # dependency.
129$(LOCAL_FULL_CLASSES_JACOCO_JAR): $(my_classes_to_report_on_path)
130
Jeff Gastone84fdb72017-10-20 18:47:25 -0700131else # LOCAL_EMMA_INSTRUMENT != true
Jeff Gastonaaae43c2017-04-11 16:36:46 -0700132 LOCAL_FULL_CLASSES_JACOCO_JAR := $(LOCAL_FULL_CLASSES_PRE_JACOCO_JAR)
Jeff Gastone84fdb72017-10-20 18:47:25 -0700133endif # LOCAL_EMMA_INSTRUMENT == true
Jeff Gastonaaae43c2017-04-11 16:36:46 -0700134
135LOCAL_INTERMEDIATE_TARGETS += $(LOCAL_FULL_CLASSES_JACOCO_JAR)