aboutsummaryrefslogtreecommitdiff
path: root/bin/ootb
blob: bf3fb55d5a1322e791adeef4f9321b3e5821b0bd (plain) (blame)
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/bin/sh
set -eu

usage() {
	cat <<-'EOF'
		Usage:
		  ootb BUILD_DIRECTORY < FILE...
		  ootb -h
	EOF
}

help() {
	cat <<-'EOF'


		Options:
		  -h, --help          show this message

		  BUILD_DIRECTORY    the path of the build directory
		  FILE               the files to be linked


		Create a directory out of symlinks of the given files.

		The goal is to enable parallel build directories to coexist,
		so that one do *O*ut *O*f *T*ree *B*uilds without requiring the
		build system or the project to explicitly support it.

		If a repository contains the files:

		  .git/
		  Makefile
		  README.md
		  src/
		    file1.ext
		    file2.ext

		Running `git ls-files | ootb build-1/` would create the
		'build-1/' directory with:

		  build-1/
		    Makefile -> ../Makefile
		    README.md -> ../README.md
		    src/
		      file1.ext -> ../../src/file1.ext
		      file2.ext -> ../../src/file2.ext

		With that one can `cd build-1/` and run builds there, without
		the build artifacts littering the source tree.  Also, one could
		create a build-2/ directory, where different compiler flags or
		build options are given, such as debug/release, while sharing the
		underlying source code.


		Examples:

		  Create a 'build/' directory with the files from the Git repository:

		    $ git ls-files | ootb build/
	EOF
}


for flag in "$@"; do
	case "$flag" in
		(--)
			break
			;;
		(--help)
			usage
			help
			exit
			;;
		(*)
			;;
	esac
done

while getopts 'h' flag; do
	case "$flag" in
		(h)
			usage
			help
			exit
			;;
		(*)
			usage >&2
			exit 2
			;;
	esac
done
shift $((OPTIND - 1))


BUILD_DIRECTORY="${1:-}"
eval "$(assert-arg -- "$BUILD_DIRECTORY" 'BUILD_DIRECTORY')"
mkdir -p "$BUILD_DIRECTORY"


while read -r f; do
	mkdir -p "$BUILD_DIRECTORY"/"$(dirname "$f")"
	ln -rfs "$PWD"/"$f" "$BUILD_DIRECTORY"/"$f"
done