-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMakefile
150 lines (117 loc) · 4.61 KB
/
Makefile
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
SDK_PATH = sdk
CFLAGS = -target x86_64-unknown-windows \
-ffreestanding \
-fshort-wchar \
-mno-red-zone \
-I$(SDK_PATH)/gnu-efi/inc \
-I$(SDK_PATH)/gnu-efi/inc/x86_64 \
-I$(SDK_PATH)/gnu-efi/inc/protocol \
-I$(SDK_PATH)/gnu-efi/lib
LDFLAGS = -target x86_64-unknown-windows \
-nostdlib \
-Wl,-entry:efi_main \
-Wl,-subsystem:efi_application \
-fuse-ld=lld
# C Compiler
CC = clang
# Finds all .c files in the current directory
SRC = $(wildcard *.c)
SRC += $(wildcard $(SDK_PATH)/gnu-efi/lib/*.c)
SRC += $(wildcard $(SDK_PATH)/gnu-efi/lib/runtime/*.c)
SRC += $(wildcard $(SDK_PATH)/gnu-efi/lib/x86_64/*.c)
# Finds gnu-efi asm files
ASM = $(wildcard $(SDK_PATH)/gnu-efi/lib/x86_64/*.S)
# Generate object filenames
OBJ = $(SRC:%.c=%.o)
OBJ += $(ASM:%.S=%.o)
# Generate clang assembly filenames (produced from save_temps)
CASM = $(SRC:%.c=%.s)
# Generate preprocessed c filenames (produced from save_temps)
CPRE = $(SRC:%.c=%.i)
# Generate llvm bytecode filenames (produced from save_temps)
LLBC = $(SRC:%.c=%.bc)
# Final output filenames
EFI = out.efi
BOOT_DRIVE = out.img
# Calculates the required image size, essentially max(EFI_SIZE, MIN_SIZE)
EFI_SIZE = $(shell stat -L -c %s $(EFI))
MIN_SIZE = 51200
IMG_SIZE = $(shell echo $$(($(EFI_SIZE) > $(MIN_SIZE) ? $(EFI_SIZE) : $(MIN_SIZE))))
# Finds the path of the OVMF_CODE-pure-efi.fd file in the sdk folder
EDK2_PATH = $(shell find $(SDK_PATH)/ -maxdepth 1 -type d -name "edk2.git-ovmf-x64*")
EDK2_BIOS = $(EDK2_PATH)/usr/share/edk2.git/ovmf-x64/OVMF_CODE-pure-efi.fd
# Finds the URL to download the latest version of edk2.git-ovmf-x64
EDK2_BASE_URL = https://www.kraxel.org/repos/jenkins/edk2/
EDK2_FILE_URL = $(shell wget -q $(EDK2_BASE_URL) -O - | grep -Po 'edk2.git-ovmf-x64[^"]*' | head -1)
.PHONY: clean clean_all qemu all img update_edk2 save_temps
#
# Compiling
#
# First recipe in the makefile is the default, dependency: all the object files
all: $(OBJ)
$(CC) $(LDFLAGS) -o $(EFI) $(OBJ)
$(MAKE) img
# Gets called whenever something has a dependency for a .o (object) file, it then iterates over all .c files
%.o: %.c
ifeq (, $(shell find $(SDK_PATH)/gnu-efi -type d -name inc))
$(warning Doesn't look like you've initialized the submodules, attempting now...)
git submodule update --init --recursive
endif
$(CC) $(CFLAGS) -c -o $@ $<
# Gets called whenever something has a dependency for a .o (object) file, it then iterates over all .S files
%.o: %.S
ifeq (, $(shell find $(SDK_PATH)/gnu-efi -type d -name inc))
$(warning Doesn't look like you've initialized the submodules, attempting now...)
git submodule update --init --recursive
endif
$(CC) $(CFLAGS) -c -o $@ $<
#
# Packaging
#
# Creates a FAT formatted image file and copies in the compiled UEFI application to EFI/BOOT/BOOTX64.EFI
img:
ifeq (, $(shell which mkfs.vfat))
$(error Can't find mkfs.vfat, consider doing sudo apt install dosfstools)
endif
ifeq (, $(shell which mcopy))
$(error Can't find mcopy, consider doing sudo apt install mtools)
endif
dd if=/dev/zero of=$(BOOT_DRIVE) bs=$(IMG_SIZE) count=2
mkfs.vfat $(BOOT_DRIVE)
mmd -i $(BOOT_DRIVE) ::EFI
mmd -i $(BOOT_DRIVE) ::EFI/BOOT
mcopy -i $(BOOT_DRIVE) $(EFI) ::EFI/BOOT/BOOTX64.EFI
#
# Convenience Stuff
#
# Deletes all files created from the build process
clean:
rm -f $(OBJ) $(BOOT_DRIVE) $(EFI) $(notdir $(CASM)) $(notdir $(CPRE)) $(notdir $(LLBC)) $(notdir $(ASM))
# Runs the image file in qemu
qemu:
ifeq (, $(shell which qemu-system-x86_64))
$(error Can't find qemu-system-x86_64, consider doing sudo apt install qemu-system-x86)
endif
qemu-system-x86_64 -bios $(EDK2_BIOS) -drive file=$(BOOT_DRIVE),format=raw
# Updates and extracts the latest version of edk2 from https://www.kraxel.org/repos/jenkins/edk2/
update_edk2:
ifeq ($(SDK_PATH)/$(EDK2_FILE_URL:%.rpm=%), $(EDK2_PATH))
$(info Looks like you've already got the latest version of edk2 in your sdk folder)
$(info Current: $(SDK_PATH)/$(EDK2_FILE_URL:%.rpm=%))
$(info Latest: $(EDK2_PATH))
else
wget $(EDK2_BASE_URL)$(EDK2_FILE_URL)
ifeq (, $(shell which rpm2cpio))
$(error Can't find rpm2cpio, consider doing sudo apt install rpm)
endif
rpm2cpio $(EDK2_FILE_URL) | cpio -id
mkdir $(SDK_PATH)/$(EDK2_FILE_URL:%.rpm=%)
mv usr $(SDK_PATH)/$(EDK2_FILE_URL:%.rpm=%)
ifneq (, $(EDK2_PATH))
rm -r $(EDK2_PATH)
endif
rm $(EDK2_FILE_URL)
endif
# At time of writing there's a bug in clang that prevents "-save-temps -masm=intel" from working
save_temps: CFLAGS += -save-temps
save_temps: all