xref: /aosp_15_r20/build/soong/bin/overrideflags (revision 333d2b3687b3a337dbcca9d65000bca186795e39)
1#!/bin/bash -e
2# Copyright (C) 2023 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
17source $(cd $(dirname $BASH_SOURCE) &> /dev/null && pwd)/../../make/shell_utils.sh
18
19require_top
20
21function print_help() {
22    echo -e "overrideflags is used to set default value for local build."
23    echo -e "\nOptions:"
24    echo -e "\t--release-config  \tPath to release configuration directory. Required"
25    echo -e "\t--no-edit         \tIf present, skip editing flag value file."
26    echo -e "\t-h/--help         \tShow this help."
27}
28
29function main() {
30    while (($# > 0)); do
31        case $1 in
32        --release-config)
33            if [[ $# -le 1 ]]; then
34                echo "--release-config requires a path"
35                return 1
36            fi
37            local release_config_dir="$2"
38            shift 2
39            ;;
40        --no-edit)
41            local no_edit="true"
42            shift 1
43            ;;
44        -h|--help)
45            print_help
46            return
47            ;;
48        *)
49            echo "$1 is unrecognized"
50            print_help
51            return 1
52            ;;
53        esac
54    done
55
56
57
58    case $(uname -s) in
59        Darwin)
60            local host_arch=darwin-x86
61            ;;
62        Linux)
63            local host_arch=linux-x86
64            ;;
65        *)
66            >&2 echo Unknown host $(uname -s)
67            return
68            ;;
69    esac
70
71    if [[ -z "${release_config_dir}" ]]; then
72        echo "Please provide release configuration path by --release-config"
73        exit 1
74    elif [ ! -d "${release_config_dir}" ]; then
75        echo "${release_config_dir} is an invalid directory"
76        exit 1
77    fi
78    local T="$(gettop)"
79    local aconfig_dir="${T}"/build/make/tools/aconfig/
80    local overrideflag_py="${aconfig_dir}"/overrideflags/overrideflags.py
81    local overridefile="${release_config_dir}/aconfig/override_values.textproto"
82
83    # Edit override file
84    if [[ -z "${no_edit}" ]]; then
85        editor="${EDITOR:-$(which vim)}"
86
87        eval "${editor} ${overridefile}"
88        if [ $? -ne 0 ]; then
89            echo "Fail to set override values"
90            return 1
91        fi
92    fi
93
94    ${T}/prebuilts/build-tools/${host_arch}/bin/py3-cmd -u "${overrideflag_py}" \
95        --overrides "${overridefile}" \
96        --out "${release_config_dir}/aconfig"
97}
98
99
100main "$@"
101