|
| 1 | +# This makefile is quite similar to the one used by the kernel, with |
| 2 | +# the exception of some kernel-only flags that have been omitted here |
| 3 | + |
| 4 | +override MAKEFLAGS += -rR |
| 5 | +override TARGET := hello |
| 6 | + |
| 7 | +define DEFAULT_VAR = |
| 8 | + ifeq ($(origin $1),default) |
| 9 | + override $(1) := $(2) |
| 10 | + endif |
| 11 | + ifeq ($(origin $1),undefined) |
| 12 | + override $(1) := $(2) |
| 13 | + endif |
| 14 | +endef |
| 15 | + |
| 16 | +override DEFAULT_CC := x86_64-elf-gcc |
| 17 | +$(eval $(call DEFAULT_VAR,CC,$(DEFAULT_CC))) |
| 18 | + |
| 19 | +override DEFAULT_LD := x86_64-elf-ld |
| 20 | +$(eval $(call DEFAULT_VAR,LD,$(DEFAULT_LD))) |
| 21 | + |
| 22 | +override DEFAULT_CFLAGS := -g -O2 -pipe |
| 23 | +$(eval $(call DEFAULT_VAR,CFLAGS,$(DEFAULT_CFLAGS))) |
| 24 | + |
| 25 | +# User controllable C preprocessor flags. We set none by default. |
| 26 | +override DEFAULT_CPPFLAGS := |
| 27 | +$(eval $(call DEFAULT_VAR,CPPFLAGS,$(DEFAULT_CPPFLAGS))) |
| 28 | + |
| 29 | +# User controllable linker flags. We set none by default. |
| 30 | +override DEFAULT_LDFLAGS := |
| 31 | +$(eval $(call DEFAULT_VAR,LDFLAGS,$(DEFAULT_LDFLAGS))) |
| 32 | + |
| 33 | +# Internal C flags that should not be changed by the user. |
| 34 | +override CFLAGS += \ |
| 35 | + -Wall \ |
| 36 | + -Wextra \ |
| 37 | + -std=gnu11 \ |
| 38 | + -ffreestanding \ |
| 39 | + -fno-stack-protector \ |
| 40 | + -fno-stack-check \ |
| 41 | + -fno-lto \ |
| 42 | + -fno-PIE \ |
| 43 | + -fno-PIC \ |
| 44 | + -m64 \ |
| 45 | + -march=x86-64 \ |
| 46 | + -mabi=sysv |
| 47 | + |
| 48 | +# Internal C preprocessor flags that should not be changed by the user. |
| 49 | +override CPPFLAGS := \ |
| 50 | + -I include \ |
| 51 | + -I. \ |
| 52 | + $(CPPFLAGS) \ |
| 53 | + -MMD \ |
| 54 | + -MP |
| 55 | + |
| 56 | +# Internal linker flags that should not be changed by the user. |
| 57 | +override LDFLAGS += \ |
| 58 | + -nostdlib \ |
| 59 | + -static \ |
| 60 | + -m elf_x86_64 \ |
| 61 | + -z max-page-size=0x1000 |
| 62 | + |
| 63 | +ifeq ($(shell $(LD) --help 2>&1 | grep 'no-pie' >/dev/null 2>&1; echo $$?),0) |
| 64 | + override LDFLAGS += -no-pie |
| 65 | +endif |
| 66 | + |
| 67 | +override CFILES := $(shell find -L . -type f -name '*.c') |
| 68 | +override ASFILES := $(shell find -L . -type f -name '*.s') |
| 69 | + |
| 70 | +override BUILD_DIR := ../../build/user/bin |
| 71 | +override INITRAMFS_DIR := ../../build/initramfs/bin |
| 72 | + |
| 73 | +override OBJ := $(patsubst %.c,$(BUILD_DIR)/%.c.o,$(CFILES)) $(patsubst %.s,$(BUILD_DIR)/%.s.o,$(ASFILES)) |
| 74 | +override HEADER_DEPS := $(patsubst %.c,$(BUILD_DIR)/%.c.d,$(CFILES)) $(patsubst %.s,$(BUILD_DIR)/%.s.d,$(ASFILES)) |
| 75 | + |
| 76 | +.PHONY: all |
| 77 | +all: $(BUILD_DIR)/$(TARGET) |
| 78 | + |
| 79 | +$(BUILD_DIR)/$(TARGET): $(OBJ) |
| 80 | + $(LD) $(OBJ) $(LDFLAGS) -o $@ |
| 81 | + @mkdir -p $(INITRAMFS_DIR) |
| 82 | + @cp $(BUILD_DIR)/$(TARGET) $(INITRAMFS_DIR) |
| 83 | + |
| 84 | +-include $(HEADER_DEPS) |
| 85 | + |
| 86 | +$(BUILD_DIR)/%.c.o: %.c |
| 87 | + @mkdir -p $(@D) |
| 88 | + $(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $@ |
| 89 | + |
| 90 | +$(BUILD_DIR)/%.s.o: %.s |
| 91 | + @mkdir -p $(@D) |
| 92 | + $(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $@ |
| 93 | + |
| 94 | +.PHONY: clean |
| 95 | +clean: |
| 96 | + rm -rf $(BUILD_DIR) |
| 97 | + |
| 98 | +.PHONY: format |
| 99 | +format: |
| 100 | + find -type f \( -name '*.c' -o -name '*.h' \) ! -name 'limine.h' -exec clang-format -i {} + |
0 commit comments