blob: c5647dfd48050271abfb901a9eb0f80bfd8754bb [file] [log] [blame] [edit]
#!/bin/bash
# Copyright 2024 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
#### CATEGORY=Build
### add a GN label to args.gn and regen
## usage: fx add-test [--target-list TARGET_LIST] //foo/bar //foo/baz
##
## Adds the labels to the given TARGET_LIST and regenerates. If --target-list is not
## specified, the target is developer_test_labels.
##
## This is useful when `fx test` tells you to add a target to the build,
## but you have some custom args set in `args.gn`, so do not want to run
## `fx set` to overwrite those args.
readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
source "$SCRIPT_DIR/../lib/vars.sh" || exit $?
fx-config-read
set -e
DESTINATION_LABEL="developer_test_labels"
function usage() {
echo "fx add-test [--target-list TARGET_LIST] LABELS"
echo ""
echo "Adds the labels to the given TARGET_LIST and regenerates. If --target-list is not"
echo "specified, the target is developer_test_labels."
echo ""
}
if [[ $1 == "--help" ]]; then
usage
exit 0
fi
if [[ $# -lt 1 ]]; then
usage
echo "ERROR: Too few arguments. At least one label is required."
exit 1
fi
if [[ "$1" == "--target-list" ]]; then
if [[ $# -lt 2 ]]; then
usage
echo "ERROR: Missing argument for --target-list"
exit 1
fi
if [[ $# -lt 3 ]]; then
usage
echo "ERROR: Too few arguments. At least one label is required."
exit 1
fi
DESTINATION_LABEL="$2"
shift # past argument
shift # past value
fi
# Cache args in case of typos, bad paths, etc.
args_gn="${FUCHSIA_BUILD_DIR}/args.gn"
backup_args_gn="$(mktemp --suffix=.args.gn)"
cp "${args_gn}" "${backup_args_gn}"
echo "Adding test labels to ${args_gn}"
for arg in "$@"; do
if [ "$arg" == "--with" ]; then
# Skip the '--with' argument, which is included in some error messages
continue
fi
# Append the formatted string to args.gn
echo "${DESTINATION_LABEL} += [\"$arg\"]" >> "$args_gn"
done
echo "Generating Ninja outputs file"
if ! fx-gen; then
echo "fx gen failed, restoring original args.gn and re-running 'fx gen'"
cp "${backup_args_gn}" "${args_gn}"
fx-gen
fi
rm -f "${backup_args_gn}"