# Driver Makefile — nulsl-libc
#
# This is the HAND-WRITTEN top-level makefile (committed to git). It never
# runs the autotools machinery in-tree; instead it drives two out-of-tree
# builds, one per bin/ directory, exactly as the Null Linux guidelines want:
#
#   make release   -> ./configure CFLAGS='-O2'   into bin/release/
#   make debug     -> ./configure CFLAGS='-O0 -g' into bin/debug/
#
# Run `./autogen.sh` once after cloning (or just run `make release` — it
# regenerates the configure script itself when needed).
#
# Never run `./configure` at the repository root: configure.ac refuses it.

.PHONY: all release debug bench check clean distclean compile_commands

all: release

# --- bootstrapping ---------------------------------------------------------

# Regenerates configure (and friends) when autogen.sh is newer.
configure: autogen.sh
	./autogen.sh

# --- builds ----------------------------------------------------------------

# Release is ALWAYS -O2 (project guideline #4).
bin/release/Makefile: configure
	@mkdir -p bin/release
	@cd bin/release && ../../configure CFLAGS='-O2'

bin/debug/Makefile: configure
	@mkdir -p bin/debug
	@cd bin/debug && ../../configure CFLAGS='-O0 -g'

release: bin/release/Makefile
	$(MAKE) -C bin/release

debug: bin/debug/Makefile
	$(MAKE) -C bin/debug

# --- developer conveniences -------------------------------------------------

bench: release
	$(MAKE) -C bin/release bench

check: release
	$(MAKE) -C bin/release check

# LSP support: compile_commands.json for clangd (requires `bear`).
compile_commands: release
	@command -v bear >/dev/null 2>&1 || { \
	    echo "error: 'bear' is not installed (sudo pacman -S bear, apt install bear, ...)"; exit 1; }
	@cd bin/release && bear --output ../../compile_commands.json -- $(MAKE) clean all

clean:
	@test ! -f bin/release/Makefile || $(MAKE) -C bin/release clean
	@test ! -f bin/debug/Makefile || $(MAKE) -C bin/debug clean

distclean: clean
	rm -rf bin/release bin/debug
