.POSIX: .SUFFIXES: # Source code location and files to watch for changes. SRC_DIR := src BUILD_DIR := build SRC_MAIN := $(SRC_DIR)/main.c WATCH_SRC := $(shell find $(SRC_DIR) -name "*.c" -or -name "*.s" -or -name "*.h") INC_DIRS := $(shell find $(SRC_DIR) -type d) INC_FLAGS := $(addprefix -I,$(INC_DIRS)) # Output executable. TARGET := bdl BIN := $(BUILD_DIR)/$(TARGET) # Compiler and linker configuration. CC := cc CFLAGS := -Wall -Wextra -pedantic -DBIN_NAME=\"$(TARGET)\" CFLAGS += $(INC_FLAGS) NASM_FLAGS ?= -felf64 LDFLAGS := LDLIBS := RELEASE_CFLAGS := -DNDEBUG -O2 -static DEBUG_CFLAGS := -DDEBUG -O0 -g .PHONY: build tests clean # Setup debug/release builds. # make clean && make DEBUG=0 # make clean && make DEBUG=1 DEBUG ?= 0 ifeq ($(DEBUG), 1) CFLAGS += $(DEBUG_CFLAGS) NASM_FLAGS += -g -F dwarf else ifeq ($(DEBUG), 2) CFLAGS += $(DEBUG_CFLAGS) -fsanitize=address NASM_FLAGS += -g -F dwarf else CFLAGS += $(RELEASE_CFLAGS) endif main: $(BIN) $(BIN): $(SRC_MAIN) $(WATCH_SRC) $(BUILD_DIR) $(CC) $(CFLAGS) $(LDFLAGS) -o $(BIN) $(SRC_MAIN) $(LDLIBS) # Create build directory if needed. $(BUILD_DIR): mkdir -p $(BUILD_DIR) tests: $(BIN) # ./$(BIN) examples/arithmetic.bdl | diff tests/arithmetic_expected.txt - # ./$(BIN) examples/booleans.bdl | diff tests/booleans_expected.txt - # ./$(BIN) examples/lists.bdl | diff tests/lists_expected.txt - # ./$(BIN) examples/types.bdl | diff tests/types_expected.txt - # ./$(BIN) examples/variables.bdl | diff tests/variables_expected.txt - run: $(BIN) $(BIN) example.bdl > build/example.asm nasm $(NASM_FLAGS) build/example.asm -o build/example.o ld build/example.o -o build/example @./build/example # Remove build directory. clean: rm -rf $(BUILD_DIR)