blob: 7099526455e13fbeea8d33c4e5220c69848ee940 [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
Colin Cross3c8d30c2017-11-21 15:01:26 -080024# copy filters from Jack but also skip some known java packages
25my_include_filter := $(strip $(LOCAL_JACK_COVERAGE_INCLUDE_FILTER))
26my_exclude_filter := $(strip $(DEFAULT_JACOCO_EXCLUDE_FILTER),$(LOCAL_JACK_COVERAGE_EXCLUDE_FILTER))
27
28my_include_args := $(call jacoco-class-filter-to-file-args, $(my_include_filter))
29my_exclude_args := $(call jacoco-class-filter-to-file-args, $(my_exclude_filter))
30
31# single-quote each arg of the include args so the '*' gets evaluated by zip
32# don't quote the exclude args they need to be evaluated by bash for rm -rf
33my_include_args := $(foreach arg,$(my_include_args),'$(arg)')
Jeff Gastonaaae43c2017-04-11 16:36:46 -070034
35ifeq ($(LOCAL_EMMA_INSTRUMENT),true)
Jeff Gastonaaae43c2017-04-11 16:36:46 -070036 my_files := $(intermediates.COMMON)/jacoco
37
38 # make a task that unzips the classes that we want to instrument from the
39 # input jar
40 my_unzipped_path := $(my_files)/work/classes-to-instrument/classes
41 my_unzipped_timestamp_path := $(my_files)/work/classes-to-instrument/updated.stamp
42$(my_unzipped_timestamp_path): PRIVATE_UNZIPPED_PATH := $(my_unzipped_path)
43$(my_unzipped_timestamp_path): PRIVATE_UNZIPPED_TIMESTAMP_PATH := $(my_unzipped_timestamp_path)
44$(my_unzipped_timestamp_path): PRIVATE_INCLUDE_ARGS := $(my_include_args)
45$(my_unzipped_timestamp_path): PRIVATE_EXCLUDE_ARGS := $(my_exclude_args)
46$(my_unzipped_timestamp_path): PRIVATE_FULL_CLASSES_PRE_JACOCO_JAR := $(LOCAL_FULL_CLASSES_PRE_JACOCO_JAR)
47$(my_unzipped_timestamp_path): $(LOCAL_FULL_CLASSES_PRE_JACOCO_JAR)
48 rm -rf $(PRIVATE_UNZIPPED_PATH) $@
49 mkdir -p $(PRIVATE_UNZIPPED_PATH)
Colin Crossc27d7952020-07-11 22:33:30 -070050 unzip -qDD $(PRIVATE_FULL_CLASSES_PRE_JACOCO_JAR) \
Jeff Gastonaaae43c2017-04-11 16:36:46 -070051 -d $(PRIVATE_UNZIPPED_PATH) \
52 $(PRIVATE_INCLUDE_ARGS)
Colin Crossa8ac2822022-03-30 20:04:06 -070053 chmod -R =rwX $(PRIVATE_UNZIPPED_PATH)
Jeff Gaston8fcf6a42017-10-20 18:33:14 -070054 (cd $(PRIVATE_UNZIPPED_PATH) && rm -rf $(PRIVATE_EXCLUDE_ARGS))
Elliott Hughes37ab4e22019-01-14 12:56:07 -080055 (cd $(PRIVATE_UNZIPPED_PATH) && find -not -name "*.class" -type f -exec 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
Colin Cross330b1fe2021-04-06 18:04:06 -070075# Make a rule to copy the jacoco-report-classes.jar to a packaging directory.
76$(eval $(call copy-one-file,$(my_classes_to_report_on_path),\
77 $(call local-packaging-dir,jacoco)/jacoco-report-classes.jar))
78$(call add-dependency,$(LOCAL_BUILT_MODULE),\
79 $(call local-packaging-dir,jacoco)/jacoco-report-classes.jar)
Jeff Gastonaaae43c2017-04-11 16:36:46 -070080
81 # make a task that invokes instrumentation
82 my_instrumented_path := $(my_files)/work/instrumented/classes
83 my_instrumented_timestamp_path := $(my_files)/work/instrumented/updated.stamp
84$(my_instrumented_timestamp_path): PRIVATE_INSTRUMENTED_PATH := $(my_instrumented_path)
85$(my_instrumented_timestamp_path): PRIVATE_INSTRUMENTED_TIMESTAMP_PATH := $(my_instrumented_timestamp_path)
86$(my_instrumented_timestamp_path): PRIVATE_UNZIPPED_PATH := $(my_unzipped_path)
87$(my_instrumented_timestamp_path): $(my_unzipped_timestamp_path) $(JACOCO_CLI_JAR)
88 rm -rf $(PRIVATE_INSTRUMENTED_PATH)
89 mkdir -p $(PRIVATE_INSTRUMENTED_PATH)
90 java -jar $(JACOCO_CLI_JAR) \
91 instrument \
Colin Crossf8b06a42017-12-21 14:09:51 -080092 --quiet \
93 --dest '$(PRIVATE_INSTRUMENTED_PATH)' \
Jeff Gastonaaae43c2017-04-11 16:36:46 -070094 $(PRIVATE_UNZIPPED_PATH)
95 touch $(PRIVATE_INSTRUMENTED_TIMESTAMP_PATH)
96
97
98 # make a task that zips both the instrumented classes and the uninstrumented
99 # classes (this jar is the instrumented application to execute)
100 my_temp_jar_path := $(my_files)/work/usable.jar
101 LOCAL_FULL_CLASSES_JACOCO_JAR := $(intermediates.COMMON)/classes-jacoco.jar
102$(LOCAL_FULL_CLASSES_JACOCO_JAR): PRIVATE_TEMP_JAR_PATH := $(my_temp_jar_path)
103$(LOCAL_FULL_CLASSES_JACOCO_JAR): PRIVATE_INSTRUMENTED_PATH := $(my_instrumented_path)
104$(LOCAL_FULL_CLASSES_JACOCO_JAR): PRIVATE_FULL_CLASSES_PRE_JACOCO_JAR := $(LOCAL_FULL_CLASSES_PRE_JACOCO_JAR)
Colin Cross128800f2017-08-10 15:24:10 -0700105$(LOCAL_FULL_CLASSES_JACOCO_JAR): $(JAR_ARGS)
Jeff Gastonaaae43c2017-04-11 16:36:46 -0700106$(LOCAL_FULL_CLASSES_JACOCO_JAR): $(my_instrumented_timestamp_path) $(LOCAL_FULL_CLASSES_PRE_JACOCO_JAR)
107 rm -f $@ $(PRIVATE_TEMP_JAR_PATH)
108 # copy the pre-jacoco jar (containing files excluded from instrumentation)
109 cp $(PRIVATE_FULL_CLASSES_PRE_JACOCO_JAR) $(PRIVATE_TEMP_JAR_PATH)
110 # copy instrumented files back into the resultant jar
Colin Cross128800f2017-08-10 15:24:10 -0700111 $(JAR) -uf $(PRIVATE_TEMP_JAR_PATH) $(call jar-args-sorted-files-in-directory,$(PRIVATE_INSTRUMENTED_PATH))
Jeff Gastonaaae43c2017-04-11 16:36:46 -0700112 mv $(PRIVATE_TEMP_JAR_PATH) $@
113
114 # this is used to trigger $(my_classes_to_report_on_path) to build
115 # when $(LOCAL_FULL_CLASSES_JACOCO_JAR) builds, but it isn't truly a
116 # dependency.
117$(LOCAL_FULL_CLASSES_JACOCO_JAR): $(my_classes_to_report_on_path)
118
Jeff Gastone84fdb72017-10-20 18:47:25 -0700119else # LOCAL_EMMA_INSTRUMENT != true
Jeff Gastonaaae43c2017-04-11 16:36:46 -0700120 LOCAL_FULL_CLASSES_JACOCO_JAR := $(LOCAL_FULL_CLASSES_PRE_JACOCO_JAR)
Jeff Gastone84fdb72017-10-20 18:47:25 -0700121endif # LOCAL_EMMA_INSTRUMENT == true
Jeff Gastonaaae43c2017-04-11 16:36:46 -0700122
123LOCAL_INTERMEDIATE_TARGETS += $(LOCAL_FULL_CLASSES_JACOCO_JAR)