xref: /aosp_15_r20/external/gsc-utils/util/opentitantool.sh (revision 4f2df630800bdcf1d4f0decf95d8a1cb87344f5f)
1*4f2df630SAndroid Build Coastguard Worker#!/bin/bash
2*4f2df630SAndroid Build Coastguard Worker# Copyright 2023 The ChromiumOS Authors
3*4f2df630SAndroid Build Coastguard Worker# Use of this source code is governed by a BSD-style license that can be
4*4f2df630SAndroid Build Coastguard Worker# found in the LICENSE file.
5*4f2df630SAndroid Build Coastguard Worker# Wrapper script that build opentitan tool with bazel then passes the
6*4f2df630SAndroid Build Coastguard Worker# command line parameters to it
7*4f2df630SAndroid Build Coastguard Worker# Use FORCE_REBUILD=<anything> to rebuild opentitantool even if binary exists
8*4f2df630SAndroid Build Coastguard Worker
9*4f2df630SAndroid Build Coastguard Workerset -euo pipefail
10*4f2df630SAndroid Build Coastguard Worker
11*4f2df630SAndroid Build Coastguard WorkerFORCE_REBUILD="${FORCE_REBUILD:+1}"
12*4f2df630SAndroid Build Coastguard Worker
13*4f2df630SAndroid Build Coastguard Workermain() {
14*4f2df630SAndroid Build Coastguard Worker    local script_path
15*4f2df630SAndroid Build Coastguard Worker    local opentitan_root
16*4f2df630SAndroid Build Coastguard Worker    local bin
17*4f2df630SAndroid Build Coastguard Worker
18*4f2df630SAndroid Build Coastguard Worker    script_path="$(cd "$(dirname \
19*4f2df630SAndroid Build Coastguard Worker       "$(test -L "$0" && readlink "$0" || echo "$0")")" >/dev/null 2>&1 ; \
20*4f2df630SAndroid Build Coastguard Worker       pwd -P)"
21*4f2df630SAndroid Build Coastguard Worker    opentitan_root="${script_path}/../../../third_party/lowrisc/opentitan"
22*4f2df630SAndroid Build Coastguard Worker    bin="${opentitan_root}/bazel-bin/sw/host/opentitantool/opentitantool"
23*4f2df630SAndroid Build Coastguard Worker
24*4f2df630SAndroid Build Coastguard Worker    # If we force rebuild or the binary isn't present, build it now
25*4f2df630SAndroid Build Coastguard Worker    if [[ -n "${FORCE_REBUILD}" || ! -f "${bin}" ]]; then
26*4f2df630SAndroid Build Coastguard Worker        # Execute in sub shell so we don't change working directories
27*4f2df630SAndroid Build Coastguard Worker        ( "${opentitan_root}/bazelisk.sh" build //sw/host/opentitantool \
28*4f2df630SAndroid Build Coastguard Worker            >/dev/null 2>&1 )
29*4f2df630SAndroid Build Coastguard Worker    fi
30*4f2df630SAndroid Build Coastguard Worker
31*4f2df630SAndroid Build Coastguard Worker    # Call opentitantool from original working directory
32*4f2df630SAndroid Build Coastguard Worker    "${bin}" "$@"
33*4f2df630SAndroid Build Coastguard Worker}
34*4f2df630SAndroid Build Coastguard Worker
35*4f2df630SAndroid Build Coastguard Workermain "$@"
36