xref: /aosp_15_r20/build/bazel/scripts/gen_build_number.sh (revision 7594170e27e0732bc44b93d1440d87a54b6ffe7c)
1*7594170eSAndroid Build Coastguard Worker#!/bin/bash -e
2*7594170eSAndroid Build Coastguard Worker# Copyright (C) 2022 The Android Open Source Project
3*7594170eSAndroid Build Coastguard Worker#
4*7594170eSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
5*7594170eSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
6*7594170eSAndroid Build Coastguard Worker# You may obtain a copy of the License at
7*7594170eSAndroid Build Coastguard Worker#
8*7594170eSAndroid Build Coastguard Worker#       http://www.apache.org/licenses/LICENSE-2.0
9*7594170eSAndroid Build Coastguard Worker#
10*7594170eSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
11*7594170eSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
12*7594170eSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*7594170eSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
14*7594170eSAndroid Build Coastguard Worker# limitations under the License.
15*7594170eSAndroid Build Coastguard Worker
16*7594170eSAndroid Build Coastguard Worker# Script used as --workspace_status_command.
17*7594170eSAndroid Build Coastguard Worker# Must execute at the root of workspace.
18*7594170eSAndroid Build Coastguard Worker# https://docs.bazel.build/versions/main/command-line-reference.html#flag--workspace_status_command
19*7594170eSAndroid Build Coastguard Worker
20*7594170eSAndroid Build Coastguard Worker# Common script utilities
21*7594170eSAndroid Build Coastguard Workersource $(cd $(dirname $BASH_SOURCE) &> /dev/null && pwd)/../../make/shell_utils.sh
22*7594170eSAndroid Build Coastguard Workerrequire_top
23*7594170eSAndroid Build Coastguard Worker
24*7594170eSAndroid Build Coastguard Worker
25*7594170eSAndroid Build Coastguard Workerif [[ ! -f "WORKSPACE" ]]; then
26*7594170eSAndroid Build Coastguard Worker    echo "ERROR: gen_build_number.sh must be executed at the root of Bazel workspace." >&2
27*7594170eSAndroid Build Coastguard Worker    exit 1
28*7594170eSAndroid Build Coastguard Workerfi
29*7594170eSAndroid Build Coastguard Worker
30*7594170eSAndroid Build Coastguard Worker
31*7594170eSAndroid Build Coastguard Worker# TODO(b/228463719): figure out how to get the path properly.
32*7594170eSAndroid Build Coastguard WorkerBUILD_NUMBER_FILE=$(getoutdir)/soong/build_number.txt
33*7594170eSAndroid Build Coastguard Workerif [[ -f ${BUILD_NUMBER_FILE} ]]; then
34*7594170eSAndroid Build Coastguard Worker    BUILD_NUMBER=$(cat ${BUILD_NUMBER_FILE})
35*7594170eSAndroid Build Coastguard Workerelse
36*7594170eSAndroid Build Coastguard Worker    BUILD_NUMBER=eng.${USER:0:6}.$(date '+%Y%m%d.%H%M%S')
37*7594170eSAndroid Build Coastguard Workerfi
38*7594170eSAndroid Build Coastguard Worker
39*7594170eSAndroid Build Coastguard Workerecho "BUILD_NUMBER ${BUILD_NUMBER}"
40