aboutsummaryrefslogtreecommitdiff
#!/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