blob: 74d84f44d2c2c7f91d074547e352916ce2538944 [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
Colin Cross3c8d30c2017-11-21 15:01:26 -080022# determine Jacoco include/exclude filters even when coverage is not enabled
23# to get syntax checking on LOCAL_JACK_COVERAGE_(INCLUDE|EXCLUDE)_FILTER
24DEFAULT_JACOCO_EXCLUDE_FILTER := org/junit/*,org/jacoco/*,org/mockito/*
25# copy filters from Jack but also skip some known java packages
26my_include_filter := $(strip $(LOCAL_JACK_COVERAGE_INCLUDE_FILTER))
27my_exclude_filter := $(strip $(DEFAULT_JACOCO_EXCLUDE_FILTER),$(LOCAL_JACK_COVERAGE_EXCLUDE_FILTER))
28
29my_include_args := $(call jacoco-class-filter-to-file-args, $(my_include_filter))
30my_exclude_args := $(call jacoco-class-filter-to-file-args, $(my_exclude_filter))
31
32# single-quote each arg of the include args so the '*' gets evaluated by zip
33# don't quote the exclude args they need to be evaluated by bash for rm -rf
34my_include_args := $(foreach arg,$(my_include_args),'$(arg)')
Jeff Gastonaaae43c2017-04-11 16:36:46 -070035
36ifeq ($(LOCAL_EMMA_INSTRUMENT),true)
Jeff Gastonaaae43c2017-04-11 16:36:46 -070037 my_files := $(intermediates.COMMON)/jacoco
38
39 # make a task that unzips the classes that we want to instrument from the
40 # input jar
41 my_unzipped_path := $(my_files)/work/classes-to-instrument/classes
42 my_unzipped_timestamp_path := $(my_files)/work/classes-to-instrument/updated.stamp
43$(my_unzipped_timestamp_path): PRIVATE_UNZIPPED_PATH := $(my_unzipped_path)
44$(my_unzipped_timestamp_path): PRIVATE_UNZIPPED_TIMESTAMP_PATH := $(my_unzipped_timestamp_path)
45$(my_unzipped_timestamp_path): PRIVATE_INCLUDE_ARGS := $(my_include_args)
46$(my_unzipped_timestamp_path): PRIVATE_EXCLUDE_ARGS := $(my_exclude_args)
47$(my_unzipped_timestamp_path): PRIVATE_FULL_CLASSES_PRE_JACOCO_JAR := $(LOCAL_FULL_CLASSES_PRE_JACOCO_JAR)
48$(my_unzipped_timestamp_path): $(LOCAL_FULL_CLASSES_PRE_JACOCO_JAR)
49 rm -rf $(PRIVATE_UNZIPPED_PATH) $@
50 mkdir -p $(PRIVATE_UNZIPPED_PATH)
51 unzip -q $(PRIVATE_FULL_CLASSES_PRE_JACOCO_JAR) \
52 -d $(PRIVATE_UNZIPPED_PATH) \
53 $(PRIVATE_INCLUDE_ARGS)
Jeff Gaston8fcf6a42017-10-20 18:33:14 -070054 (cd $(PRIVATE_UNZIPPED_PATH) && rm -rf $(PRIVATE_EXCLUDE_ARGS))
Jeff Gaston6fdbdc82017-10-25 15:55:16 -070055 (cd $(PRIVATE_UNZIPPED_PATH) && find -not -name "*.class" -type f | xargs --no-run-if-empty rm)
Jeff Gastonaaae43c2017-04-11 16:36:46 -070056 touch $(PRIVATE_UNZIPPED_TIMESTAMP_PATH)
57# Unfortunately in the previous task above,
58# 'rm -rf $(PRIVATE_EXCLUDE_ARGS)' needs to be a separate
59# shell command after 'unzip'.
60# We can't just use the '-x' (exclude) option of 'unzip' because if both
61# inclusions and exclusions are specified and an exclusion matches no
62# inclusions, then 'unzip' exits with an error (error 11).
63# We could ignore the error, but that would make the process less reliable
64
65
66 # make a task that zips only the classes that will be instrumented
67 # (for passing in to the report generator later)
68 my_classes_to_report_on_path := $(my_files)/report-resources/jacoco-report-classes.jar
69$(my_classes_to_report_on_path): PRIVATE_UNZIPPED_PATH := $(my_unzipped_path)
70$(my_classes_to_report_on_path): $(my_unzipped_timestamp_path)
71 rm -f $@
72 zip -q $@ \
73 -r $(PRIVATE_UNZIPPED_PATH)
74
75
76
77 # make a task that invokes instrumentation
78 my_instrumented_path := $(my_files)/work/instrumented/classes
79 my_instrumented_timestamp_path := $(my_files)/work/instrumented/updated.stamp
80$(my_instrumented_timestamp_path): PRIVATE_INSTRUMENTED_PATH := $(my_instrumented_path)
81$(my_instrumented_timestamp_path): PRIVATE_INSTRUMENTED_TIMESTAMP_PATH := $(my_instrumented_timestamp_path)
82$(my_instrumented_timestamp_path): PRIVATE_UNZIPPED_PATH := $(my_unzipped_path)
83$(my_instrumented_timestamp_path): $(my_unzipped_timestamp_path) $(JACOCO_CLI_JAR)
84 rm -rf $(PRIVATE_INSTRUMENTED_PATH)
85 mkdir -p $(PRIVATE_INSTRUMENTED_PATH)
86 java -jar $(JACOCO_CLI_JAR) \
87 instrument \
88 -quiet \
89 -dest '$(PRIVATE_INSTRUMENTED_PATH)' \
90 $(PRIVATE_UNZIPPED_PATH)
91 touch $(PRIVATE_INSTRUMENTED_TIMESTAMP_PATH)
92
93
94 # make a task that zips both the instrumented classes and the uninstrumented
95 # classes (this jar is the instrumented application to execute)
96 my_temp_jar_path := $(my_files)/work/usable.jar
97 LOCAL_FULL_CLASSES_JACOCO_JAR := $(intermediates.COMMON)/classes-jacoco.jar
98$(LOCAL_FULL_CLASSES_JACOCO_JAR): PRIVATE_TEMP_JAR_PATH := $(my_temp_jar_path)
99$(LOCAL_FULL_CLASSES_JACOCO_JAR): PRIVATE_INSTRUMENTED_PATH := $(my_instrumented_path)
100$(LOCAL_FULL_CLASSES_JACOCO_JAR): PRIVATE_FULL_CLASSES_PRE_JACOCO_JAR := $(LOCAL_FULL_CLASSES_PRE_JACOCO_JAR)
Colin Cross128800f2017-08-10 15:24:10 -0700101$(LOCAL_FULL_CLASSES_JACOCO_JAR): $(JAR_ARGS)
Jeff Gastonaaae43c2017-04-11 16:36:46 -0700102$(LOCAL_FULL_CLASSES_JACOCO_JAR): $(my_instrumented_timestamp_path) $(LOCAL_FULL_CLASSES_PRE_JACOCO_JAR)
103 rm -f $@ $(PRIVATE_TEMP_JAR_PATH)
104 # copy the pre-jacoco jar (containing files excluded from instrumentation)
105 cp $(PRIVATE_FULL_CLASSES_PRE_JACOCO_JAR) $(PRIVATE_TEMP_JAR_PATH)
106 # copy instrumented files back into the resultant jar
Colin Cross128800f2017-08-10 15:24:10 -0700107 $(JAR) -uf $(PRIVATE_TEMP_JAR_PATH) $(call jar-args-sorted-files-in-directory,$(PRIVATE_INSTRUMENTED_PATH))
Jeff Gastonaaae43c2017-04-11 16:36:46 -0700108 mv $(PRIVATE_TEMP_JAR_PATH) $@
109
110 # this is used to trigger $(my_classes_to_report_on_path) to build
111 # when $(LOCAL_FULL_CLASSES_JACOCO_JAR) builds, but it isn't truly a
112 # dependency.
113$(LOCAL_FULL_CLASSES_JACOCO_JAR): $(my_classes_to_report_on_path)
114
Jeff Gastone84fdb72017-10-20 18:47:25 -0700115else # LOCAL_EMMA_INSTRUMENT != true
Jeff Gastonaaae43c2017-04-11 16:36:46 -0700116 LOCAL_FULL_CLASSES_JACOCO_JAR := $(LOCAL_FULL_CLASSES_PRE_JACOCO_JAR)
Jeff Gastone84fdb72017-10-20 18:47:25 -0700117endif # LOCAL_EMMA_INSTRUMENT == true
Jeff Gastonaaae43c2017-04-11 16:36:46 -0700118
119LOCAL_INTERMEDIATE_TARGETS += $(LOCAL_FULL_CLASSES_JACOCO_JAR)