summaryrefslogtreecommitdiff
path: root/nj
blob: 7a29e39208a86ddc496dff0e493adb318c3f4fa0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#! /bin/bash
#
# Copyright 2017 Kenneth Graunke <kenneth@whitecape.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
# _____________________________________________________________________
#
# The "nj" ninja builder
#
# From any directory in a Git worktree, 'nj' will jump to the build
# directory, build, and optionally install your project.
#
# It supports in-tree builds, a single out-of-tree build (in build/),
# or multiple out-of-tree builds (in build/*/).
#
# Usage:
# $ nj [<build>] [<targets>] # if using multiple out-of-tree builds.
# $ nj [<targets>]           # if using an in-tree or single out-of-tree build
#
# By default, <build> defaults to "debug".
#
# Setup:
#
# You can create multiple out-of-tree builds, named whatever you like.
# An example build called 'debug' would look like:
#
# - build/debug
# - build/debug/extratargets (optional)
# - build/debug/install (optional)
#
# If no targets are specified, nj defaults to 'all'.  It appends the contents
# of the 'extratargets' file, if it exists, allowing you to build additional
# targets that aren't enabled by default in your project's build system, such
# as 'check'.
#
# Additionally, if 'install' exists, it runs 'ninja install'.  This can be
# a directory which you've set as Meson/CMake's install prefix, or just a
# blank file if you want it to install elsewhere.
# _____________________________________________________________________

topdir=$(git rev-parse --show-toplevel)
if [ -f "$topdir/build.ninja" ]; then
    # In-tree build exists, use that.
    builddir="${topdir}"
elif [ -f "$topdir/build/build.ninja" ]; then
    # Single out-of-tree build, use that.
    builddir="${topdir}/build"
else
    # Select an out of tree build from the first command line argument,
    # defaulting to build-debug if no directory was given.
    builddir="${topdir}/build-${1:-debug}"
    shift

    if [ ! -f "$builddir/build.ninja" ]; then
        echo "No build directory in \"$builddir\"...aborting."
        exit
    fi
fi

cd "$builddir"

extratargets="$(cat extratargets 2>/dev/null)"
targets="${@:-all $extratargets}"

ninja $targets
[[ "$targets" == all* ]] && [ -e install ] && ninja install > /dev/null