• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#
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
22# determine Jacoco include/exclude filters even when coverage is not enabled
23# to get syntax checking on LOCAL_JACK_COVERAGE_(INCLUDE|EXCLUDE)_FILTER
24# 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)')
34
35ifeq ($(LOCAL_EMMA_INSTRUMENT),true)
36  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)
50	unzip -qDD $(PRIVATE_FULL_CLASSES_PRE_JACOCO_JAR) \
51	  -d $(PRIVATE_UNZIPPED_PATH) \
52	  $(PRIVATE_INCLUDE_ARGS)
53	(cd $(PRIVATE_UNZIPPED_PATH) && rm -rf $(PRIVATE_EXCLUDE_ARGS))
54	(cd $(PRIVATE_UNZIPPED_PATH) && find -not -name "*.class" -type f -exec rm {} \;)
55	touch $(PRIVATE_UNZIPPED_TIMESTAMP_PATH)
56# Unfortunately in the previous task above,
57# 'rm -rf $(PRIVATE_EXCLUDE_ARGS)' needs to be a separate
58# shell command after 'unzip'.
59# We can't just use the '-x' (exclude) option of 'unzip' because if both
60# inclusions and exclusions are specified and an exclusion matches no
61# inclusions, then 'unzip' exits with an error (error 11).
62# We could ignore the error, but that would make the process less reliable
63
64
65  # make a task that zips only the classes that will be instrumented
66  # (for passing in to the report generator later)
67  my_classes_to_report_on_path := $(my_files)/report-resources/jacoco-report-classes.jar
68$(my_classes_to_report_on_path): PRIVATE_UNZIPPED_PATH := $(my_unzipped_path)
69$(my_classes_to_report_on_path): $(my_unzipped_timestamp_path)
70	rm -f $@
71	zip -q $@ \
72	  -r $(PRIVATE_UNZIPPED_PATH)
73
74# Make a rule to copy the jacoco-report-classes.jar to a packaging directory.
75$(eval $(call copy-one-file,$(my_classes_to_report_on_path),\
76  $(call local-packaging-dir,jacoco)/jacoco-report-classes.jar))
77$(call add-dependency,$(LOCAL_BUILT_MODULE),\
78  $(call local-packaging-dir,jacoco)/jacoco-report-classes.jar)
79
80  # make a task that invokes instrumentation
81  my_instrumented_path := $(my_files)/work/instrumented/classes
82  my_instrumented_timestamp_path := $(my_files)/work/instrumented/updated.stamp
83$(my_instrumented_timestamp_path): PRIVATE_INSTRUMENTED_PATH := $(my_instrumented_path)
84$(my_instrumented_timestamp_path): PRIVATE_INSTRUMENTED_TIMESTAMP_PATH := $(my_instrumented_timestamp_path)
85$(my_instrumented_timestamp_path): PRIVATE_UNZIPPED_PATH := $(my_unzipped_path)
86$(my_instrumented_timestamp_path): $(my_unzipped_timestamp_path) $(JACOCO_CLI_JAR)
87	rm -rf $(PRIVATE_INSTRUMENTED_PATH)
88	mkdir -p $(PRIVATE_INSTRUMENTED_PATH)
89	java -jar $(JACOCO_CLI_JAR) \
90	  instrument \
91	  --quiet \
92	  --dest '$(PRIVATE_INSTRUMENTED_PATH)' \
93	  $(PRIVATE_UNZIPPED_PATH)
94	touch $(PRIVATE_INSTRUMENTED_TIMESTAMP_PATH)
95
96
97  # make a task that zips both the instrumented classes and the uninstrumented
98  # classes (this jar is the instrumented application to execute)
99  my_temp_jar_path := $(my_files)/work/usable.jar
100  LOCAL_FULL_CLASSES_JACOCO_JAR := $(intermediates.COMMON)/classes-jacoco.jar
101$(LOCAL_FULL_CLASSES_JACOCO_JAR): PRIVATE_TEMP_JAR_PATH := $(my_temp_jar_path)
102$(LOCAL_FULL_CLASSES_JACOCO_JAR): PRIVATE_INSTRUMENTED_PATH := $(my_instrumented_path)
103$(LOCAL_FULL_CLASSES_JACOCO_JAR): PRIVATE_FULL_CLASSES_PRE_JACOCO_JAR := $(LOCAL_FULL_CLASSES_PRE_JACOCO_JAR)
104$(LOCAL_FULL_CLASSES_JACOCO_JAR): $(JAR_ARGS)
105$(LOCAL_FULL_CLASSES_JACOCO_JAR): $(my_instrumented_timestamp_path) $(LOCAL_FULL_CLASSES_PRE_JACOCO_JAR)
106	rm -f $@ $(PRIVATE_TEMP_JAR_PATH)
107	# copy the pre-jacoco jar (containing files excluded from instrumentation)
108	cp $(PRIVATE_FULL_CLASSES_PRE_JACOCO_JAR) $(PRIVATE_TEMP_JAR_PATH)
109	# copy instrumented files back into the resultant jar
110	$(JAR) -uf $(PRIVATE_TEMP_JAR_PATH) $(call jar-args-sorted-files-in-directory,$(PRIVATE_INSTRUMENTED_PATH))
111	mv $(PRIVATE_TEMP_JAR_PATH) $@
112
113  # this is used to trigger $(my_classes_to_report_on_path) to build
114  # when $(LOCAL_FULL_CLASSES_JACOCO_JAR) builds, but it isn't truly a
115  # dependency.
116$(LOCAL_FULL_CLASSES_JACOCO_JAR): $(my_classes_to_report_on_path)
117
118else # LOCAL_EMMA_INSTRUMENT != true
119  LOCAL_FULL_CLASSES_JACOCO_JAR := $(LOCAL_FULL_CLASSES_PRE_JACOCO_JAR)
120endif # LOCAL_EMMA_INSTRUMENT == true
121
122LOCAL_INTERMEDIATE_TARGETS += $(LOCAL_FULL_CLASSES_JACOCO_JAR)
123