2024-10-15 17:07:18 +02:00
|
|
|
# Compiler to use
|
2024-10-19 13:06:27 +02:00
|
|
|
CC ?= gcc
|
|
|
|
|
|
|
|
BASE_CFLAGS := -Wall -Wextra -std=gnu23 -I include
|
2024-10-15 17:07:18 +02:00
|
|
|
|
|
|
|
# Directory for build outputs
|
2024-10-14 19:48:56 +02:00
|
|
|
BUILD_DIR := build
|
2024-10-15 17:07:18 +02:00
|
|
|
|
|
|
|
# Name of the output program
|
2024-10-19 13:06:27 +02:00
|
|
|
PROGRAM_NAME ?= criscv
|
2024-10-14 19:48:56 +02:00
|
|
|
|
2024-10-15 17:07:18 +02:00
|
|
|
# Find all .c files in the source directory
|
|
|
|
SRCS := $(wildcard src/*.c)
|
|
|
|
|
|
|
|
# Generate corresponding .o file names in the build/obj directory
|
|
|
|
OBJS := $(patsubst src/%.c,$(BUILD_DIR)/obj/%.o,$(SRCS))
|
|
|
|
|
2024-10-14 19:48:56 +02:00
|
|
|
# Name of the final executable
|
|
|
|
TARGET := $(BUILD_DIR)/$(PROGRAM_NAME)
|
|
|
|
|
2024-10-15 17:07:18 +02:00
|
|
|
|
2024-10-19 13:06:27 +02:00
|
|
|
# Default target: build the executable
|
|
|
|
all: CFLAGS := $(BASE_CFLAGS) -O3 -lelf
|
|
|
|
all: $(TARGET)
|
|
|
|
|
|
|
|
# Debug target
|
|
|
|
debug: CFLAGS := $(BASE_CFLAGS) -O0 -g -lelf
|
|
|
|
debug: $(TARGET)
|
2024-10-15 17:07:18 +02:00
|
|
|
|
|
|
|
# Declare 'all' and 'clean' as phony targets (not files)
|
2024-10-19 13:06:27 +02:00
|
|
|
.PHONY: all debug static clean
|
2024-10-14 19:48:56 +02:00
|
|
|
|
|
|
|
|
2024-10-15 17:07:18 +02:00
|
|
|
# Rule to link object files into the final executable
|
2024-10-14 19:48:56 +02:00
|
|
|
$(TARGET): $(OBJS) | $(BUILD_DIR)
|
|
|
|
$(CC) $(CFLAGS) $^ -o $@
|
2024-10-15 17:07:18 +02:00
|
|
|
du -b $(TARGET) # Size of the executable in bytes
|
2024-10-14 19:48:56 +02:00
|
|
|
|
2024-10-15 17:07:18 +02:00
|
|
|
# Rule to compile source files into object files
|
|
|
|
$(BUILD_DIR)/obj/%.o: src/%.c | $(BUILD_DIR)/obj
|
2024-10-14 19:48:56 +02:00
|
|
|
$(CC) $(CFLAGS) -c $< -o $@
|
|
|
|
|
2024-10-15 17:07:18 +02:00
|
|
|
# Create build directories if they don't exist
|
2024-10-14 19:48:56 +02:00
|
|
|
$(BUILD_DIR) $(BUILD_DIR)/obj:
|
|
|
|
mkdir -p $@
|
|
|
|
|
2024-10-15 17:07:18 +02:00
|
|
|
# Clean target: remove the build directory
|
2024-10-14 19:48:56 +02:00
|
|
|
clean:
|
|
|
|
rm -rf $(BUILD_DIR)
|