xref: /aosp_15_r20/external/bazelbuild-kotlin-rules/tools/coverage/bazel_jar_instrumenter.sh (revision 3a22c0a33dd99bcca39a024d43e6fbcc55c2806e)
1*3a22c0a3SAlix#!/bin/bash
2*3a22c0a3SAlix# Copyright 2022 Google LLC. All rights reserved.
3*3a22c0a3SAlix#
4*3a22c0a3SAlix# Licensed under the Apache License, Version 2.0 (the License);
5*3a22c0a3SAlix# you may not use this file except in compliance with the License.
6*3a22c0a3SAlix# You may obtain a copy of the License at
7*3a22c0a3SAlix#
8*3a22c0a3SAlix#     http://www.apache.org/licenses/LICENSE-2.0
9*3a22c0a3SAlix#
10*3a22c0a3SAlix# Unless required by applicable law or agreed to in writing, software
11*3a22c0a3SAlix# distributed under the License is distributed on an "AS IS" BASIS,
12*3a22c0a3SAlix# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*3a22c0a3SAlix# See the License for the specific language governing permissions and
14*3a22c0a3SAlix# limitations under the License.
15*3a22c0a3SAlix
16*3a22c0a3SAlix#
17*3a22c0a3SAlixset -eu
18*3a22c0a3SAlix
19*3a22c0a3SAlixreadonly current_dir="$0.runfiles"
20*3a22c0a3SAlixreadonly in="$1"
21*3a22c0a3SAlixreadonly out="$2"
22*3a22c0a3SAlixshift 2
23*3a22c0a3SAlix
24*3a22c0a3SAlixreadonly tmp_dir="$(mktemp -d)"
25*3a22c0a3SAlixtrap "rm -rf ${tmp_dir}" EXIT
26*3a22c0a3SAlix
27*3a22c0a3SAlixreadonly jacoco="$(ls ${current_dir}/rules_kotlin/bazel/jacoco_cli)"
28*3a22c0a3SAlixreadonly jar="$(which jar)"
29*3a22c0a3SAlix
30*3a22c0a3SAlix# Unzip input Jar and run JaCoCo over it
31*3a22c0a3SAlixmkdir "${tmp_dir}/classes"
32*3a22c0a3SAlixmkdir "${tmp_dir}/instrumented"
33*3a22c0a3SAlixunzip -qq -d "${tmp_dir}/classes" "${in}"
34*3a22c0a3SAlix"${jacoco}" instrument "${tmp_dir}/classes" --dest "${tmp_dir}/instrumented" >/dev/null
35*3a22c0a3SAlix
36*3a22c0a3SAlix# Rename input .class files to .class.uninstrumented
37*3a22c0a3SAlixfind "${tmp_dir}/classes" -name '*.class' -exec mv {} {}.uninstrumented \;
38*3a22c0a3SAlix
39*3a22c0a3SAlix# Zip all the files together
40*3a22c0a3SAlix"${jar}" cf "${out}" -C "${tmp_dir}/instrumented" .
41*3a22c0a3SAlix"${jar}" uf "${out}" -C "${tmp_dir}/classes" .
42*3a22c0a3SAlix"${jar}" uf "${out}" "$@"
43