summaryrefslogtreecommitdiff
path: root/boot/Makefile
blob: b49690cafa278f506028e4e90de73295416dbe87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# ============================================================================
# boot/Makefile - UEFI Loader Build
# Produces: $(RESULT_DIR)/BOOTX64.EFI (x86) or BOOTAA64.EFI(aarch64)
#
# Strategy: Compile with Clang targeting Windows PE32+, link with lld-link.
# This produces a native .efi (PE32+) directly - no objcopy needed.
# TODO: create PE header in the code, and get rid from clang dependency
# ============================================================================

include ../config.mk
-include ../config_generated.mk

COMMON_SRC := \
    common/efi_loader.cpp \
    common/efi_elf_parser.cpp

ARCH_SRC := \
    $(ARCH)/entry.cpp

SRC := $(COMMON_SRC) $(ARCH_SRC)
OBJ := $(patsubst %.cpp,%.o,$(SRC))

# ── UEFI-specific flags ────────────────────────────────────────────────────
# Compile as PE/COFF using Clang's Windows target with -fshort-wchar.

ifeq ($(ARCH),x86_64)
    EFI_TARGET  := x86_64-unknown-windows
    EFI_BINARY  := BOOTX64.EFI
    EFI_MFLAGS  := -mno-red-zone #https://os.phil-opp.com/red-zone/#:~:text=Disable%20the%20Red%20Zone%20%7C%20Writing%20an%20OS%20in%20Rust
else ifeq ($(ARCH),aarch64)
    EFI_TARGET  := aarch64-unknown-windows
    EFI_BINARY  := BOOTAA64.EFI
    EFI_MFLAGS  :=
endif

EFI_CXXFLAGS := \
    -target $(EFI_TARGET) \
    -std=c++23 \
    -ffreestanding \
    -fno-exceptions \
    -fno-rtti \
    -fno-stack-protector \
    -fno-threadsafe-statics \
    -fno-use-cxa-atexit \
    -fshort-wchar \
    -nostdlib \
    -Wall -Wextra \
    -Werror=return-type \
    -O2 -g \
    $(EFI_MFLAGS) \
    -I../include \
    -include $(PROJECT_ROOT)/include/kernel/config.h \
    -DEFI_ARCH_$(shell echo $(ARCH) | tr a-z A-Z)

EFI_LDFLAGS := \
    -flavor link \
    -subsystem:efi_application \
    -entry:efi_main \
    -nodefaultlib

# ── Targets ─────────────────────────────────────────────────────────────────

.PHONY: all clean

all: $(RESULT_DIR)/$(EFI_BINARY)

# Link directly to PE32+ .efi
$(RESULT_DIR)/$(EFI_BINARY): $(OBJ)
	@mkdir -p $(dir $@)
	@echo "  LINK  $@"
	@lld-link $(EFI_LDFLAGS) $(OBJ) -out:$@

%.o: %.cpp
	@echo "  CXX   $<"
	@$(CXX) $(EFI_CXXFLAGS) -c $< -o $@

clean:
	find . -name '*.o' -delete