Home | History | Annotate | Download | only in core
      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
     25 my_include_filter := $(strip $(LOCAL_JACK_COVERAGE_INCLUDE_FILTER))
     26 my_exclude_filter := $(strip $(DEFAULT_JACOCO_EXCLUDE_FILTER),$(LOCAL_JACK_COVERAGE_EXCLUDE_FILTER))
     27 
     28 my_include_args := $(call jacoco-class-filter-to-file-args, $(my_include_filter))
     29 my_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
     33 my_include_args := $(foreach arg,$(my_include_args),'$(arg)')
     34 
     35 ifeq ($(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 -q $(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 
     75 
     76   # make a task that invokes instrumentation
     77   my_instrumented_path := $(my_files)/work/instrumented/classes
     78   my_instrumented_timestamp_path := $(my_files)/work/instrumented/updated.stamp
     79 $(my_instrumented_timestamp_path): PRIVATE_INSTRUMENTED_PATH := $(my_instrumented_path)
     80 $(my_instrumented_timestamp_path): PRIVATE_INSTRUMENTED_TIMESTAMP_PATH := $(my_instrumented_timestamp_path)
     81 $(my_instrumented_timestamp_path): PRIVATE_UNZIPPED_PATH := $(my_unzipped_path)
     82 $(my_instrumented_timestamp_path): $(my_unzipped_timestamp_path) $(JACOCO_CLI_JAR)
     83 	rm -rf $(PRIVATE_INSTRUMENTED_PATH)
     84 	mkdir -p $(PRIVATE_INSTRUMENTED_PATH)
     85 	java -jar $(JACOCO_CLI_JAR) \
     86 	  instrument \
     87 	  --quiet \
     88 	  --dest '$(PRIVATE_INSTRUMENTED_PATH)' \
     89 	  $(PRIVATE_UNZIPPED_PATH)
     90 	touch $(PRIVATE_INSTRUMENTED_TIMESTAMP_PATH)
     91 
     92 
     93   # make a task that zips both the instrumented classes and the uninstrumented
     94   # classes (this jar is the instrumented application to execute)
     95   my_temp_jar_path := $(my_files)/work/usable.jar
     96   LOCAL_FULL_CLASSES_JACOCO_JAR := $(intermediates.COMMON)/classes-jacoco.jar
     97 $(LOCAL_FULL_CLASSES_JACOCO_JAR): PRIVATE_TEMP_JAR_PATH := $(my_temp_jar_path)
     98 $(LOCAL_FULL_CLASSES_JACOCO_JAR): PRIVATE_INSTRUMENTED_PATH := $(my_instrumented_path)
     99 $(LOCAL_FULL_CLASSES_JACOCO_JAR): PRIVATE_FULL_CLASSES_PRE_JACOCO_JAR := $(LOCAL_FULL_CLASSES_PRE_JACOCO_JAR)
    100 $(LOCAL_FULL_CLASSES_JACOCO_JAR): $(JAR_ARGS)
    101 $(LOCAL_FULL_CLASSES_JACOCO_JAR): $(my_instrumented_timestamp_path) $(LOCAL_FULL_CLASSES_PRE_JACOCO_JAR)
    102 	rm -f $@ $(PRIVATE_TEMP_JAR_PATH)
    103 	# copy the pre-jacoco jar (containing files excluded from instrumentation)
    104 	cp $(PRIVATE_FULL_CLASSES_PRE_JACOCO_JAR) $(PRIVATE_TEMP_JAR_PATH)
    105 	# copy instrumented files back into the resultant jar
    106 	$(JAR) -uf $(PRIVATE_TEMP_JAR_PATH) $(call jar-args-sorted-files-in-directory,$(PRIVATE_INSTRUMENTED_PATH))
    107 	mv $(PRIVATE_TEMP_JAR_PATH) $@
    108 
    109   # this is used to trigger $(my_classes_to_report_on_path) to build
    110   # when $(LOCAL_FULL_CLASSES_JACOCO_JAR) builds, but it isn't truly a
    111   # dependency.
    112 $(LOCAL_FULL_CLASSES_JACOCO_JAR): $(my_classes_to_report_on_path)
    113 
    114 else # LOCAL_EMMA_INSTRUMENT != true
    115   LOCAL_FULL_CLASSES_JACOCO_JAR := $(LOCAL_FULL_CLASSES_PRE_JACOCO_JAR)
    116 endif # LOCAL_EMMA_INSTRUMENT == true
    117 
    118 LOCAL_INTERMEDIATE_TARGETS += $(LOCAL_FULL_CLASSES_JACOCO_JAR)
    119