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 chmod -R =rwX $(PRIVATE_UNZIPPED_PATH) 54 (cd $(PRIVATE_UNZIPPED_PATH) && rm -rf $(PRIVATE_EXCLUDE_ARGS)) 55 (cd $(PRIVATE_UNZIPPED_PATH) && find -not -name "*.class" -type f -exec rm {} \;) 56 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# 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) 80 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 \ 92 --quiet \ 93 --dest '$(PRIVATE_INSTRUMENTED_PATH)' \ 94 $(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) 105$(LOCAL_FULL_CLASSES_JACOCO_JAR): $(JAR_ARGS) 106$(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 111 $(JAR) -uf $(PRIVATE_TEMP_JAR_PATH) $(call jar-args-sorted-files-in-directory,$(PRIVATE_INSTRUMENTED_PATH)) 112 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 119else # LOCAL_EMMA_INSTRUMENT != true 120 LOCAL_FULL_CLASSES_JACOCO_JAR := $(LOCAL_FULL_CLASSES_PRE_JACOCO_JAR) 121endif # LOCAL_EMMA_INSTRUMENT == true 122 123LOCAL_INTERMEDIATE_TARGETS += $(LOCAL_FULL_CLASSES_JACOCO_JAR) 124