1#!/bin/bash -eu
2
3# Copyright 2018 The Chromium OS Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7# This script extracts command line options to build bad item.
8# The generated script will be used by pass level bisection.
9#
10
11source android/common.sh
12
13abs_path=$1
14
15# The item will be `-o relative-path-to-object `, which will be used
16# for seeking command in populate log.
17# We care about the `-o` at the beginning and ` ` at the end are necessary,
18# so that we can get build command for exact this object file.
19# Example: prebuilt/../clang++ -O3 -MF obj1.o.d -o obj.o obj.cpp
20# We should count this command as one to build obj.o, not obj1.o.d.
21real_path=$(realpath --relative-to="${BISECT_WORK_BUILD}" "${abs_path}")
22item="-o $real_path "
23
24populate_log=${BISECT_BAD_BUILD}/_POPULATE_LOG
25
26output='#!/bin/bash -u\n'
27output+='source android/common.sh\n'
28
29result=$(egrep -m 1 -- "${item}" ${populate_log})
30
31# Re-generate bad item to tmp directory location
32tmp_ir='/tmp/bisection_bad_item.o'
33result=$(sed "s|$item|-o $tmp_ir |g" <<< ${result})
34
35# Remove `:` after cd command
36result=$(sed 's|cd:|cd|g' <<< ${result})
37
38# Add environment variable which helps pass level bisection
39result=$(sed 's| -o | $LIMIT_FLAGS -o |g' <<< ${result})
40
41output+=${result}
42
43# Symbolic link generated bad item to original object
44output+="\nln -f $tmp_ir $abs_path"
45output+="\ntouch $abs_path"
46
47echo -e "${output}" > android/cmd_script.sh
48
49chmod u+x android/cmd_script.sh
50
51echo 'Script created as android/cmd_script.sh'
52
53# Check if compiler is LLVM.
54if grep -q "clang" android/cmd_script.sh
55then
56    exit 0
57else
58    echo 'Pass/transformation level bisection only works for LLVM compiler.'
59    exit 1
60fi