export

SHELL = /usr/bin/env bash

ROOT := $(realpath $(dir $(lastword $(MAKEFILE_LIST))))

#
# User settings
#

BUILD_DIR = build

SOURCE_DIR = src

OUTPUT = brainfuck

CC = gcc
CFLAGS = --std=c17 -Os
LD_FLAGS =

ifneq ($(DEBUG),)
CFLAGS += -O0 -g -DDEBUG=$(DEBUG) -Wextra -Wall --pedantic -Wno-missing-braces \
	-Wno-missing-field-initializers -Wformat=2 -Wswitch-default -Wswitch \
	-Wcast-align -Wpointer-arith -Wbad-function-cast -Wstrict-overflow=5 \
    -Wstrict-prototypes -Winline -Wundef -Wnested-externs -Wcast-qual \
	-Wshadow -Wunreachable-code -Wlogical-op -Wfloat-equal -Wstrict-aliasing=2 \
	-Wredundant-decls -Wold-style-definition
endif

#
# End
#

# Relocate vars
BUILD_DIR := $(ROOT)/$(BUILD_DIR)

SOURCE_DIR := $(ROOT)/$(SOURCE_DIR)

OUTPUT := $(BUILD_DIR)/$(OUTPUT)

# Verbosity
export Q := @
ifeq ($(VERBOSE), 1)
Q :=
endif

# Colors
GREEN  := \033[32m
YELLOW := \033[33m
RESET  := \033[m

# Default targets
all: $(OUTPUT)

$(OUTPUT): FORCE
	$(Q)cd $(SOURCE_DIR) && $(MAKE) $@

FORCE:

clean:
	$(Q)rm -rf $(BUILD_DIR)

run: all
	$(Q)cd $(BUILD_DIR) && ./$(notdir $(OUTPUT))

print-var-% :
	@echo $($*)

.PHONY: clean run print-var-%