blob: d259b42ea4a1a38f9a5d7ad3cb2505d5af108db3 [file] [log] [blame]
Cody Schroederfe728dc2016-01-22 21:48:08 +00001#!/bin/bash
2#
3# Copyright 2016 The Bazel Authors. All rights reserved.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17USAGE='bazel-run.sh [<bazel option>...] <target> [ -- [<target option>]... ]'
18DESCRIPTION='
19 Builds and runs the command generated by "bazel run" in the calling
20 terminal. The command is run as a grandchild of the current shell,
21 not from the Bazel server. Therefore, the program will have a controlling terminal, and
22 the Bazel lock is released before running the command.'
23
24function usage() {
25 echo "$USAGE" "$DESCRIPTION" >&2
26}
27
28function die() {
29 echo "$1"
30 exit 1
31}
32
33function cleanup() {
34 rm "$runcmd"
35}
36
37[ $# -gt 0 ] || { usage; exit 1; }
38
39runcmd="$(mktemp /tmp/bazel-run.XXXXXX)" || die "Could not create tmp file"
40trap "cleanup" EXIT
41
42bazel run --script_path="$runcmd" "$@" || exit $?
43[ -x "$runcmd" ] || die "File $runcmd not executable"
44
45"$runcmd"
46