limine/stage23/Makefile

146 lines
3.5 KiB
Makefile
Raw Normal View History

2021-03-02 12:23:43 +03:00
TARGET = bios
ifeq ($(TARGET), bios)
TOOLCHAIN=i386-elf
else ifeq ($(TARGET), uefi)
TOOLCHAIN=x86_64-elf
else
$(error Invalid toolchain)
endif
CC = $(TOOLCHAIN)-gcc
LD = $(TOOLCHAIN)-gcc
OBJCOPY = $(TOOLCHAIN)-objcopy
OBJDUMP = $(TOOLCHAIN)-objdump
READELF = $(TOOLCHAIN)-readelf
2021-02-26 03:30:27 +03:00
COM_OUTPUT = false
E9_OUTPUT = false
BUILD_ID := $(shell dd if=/dev/urandom count=8 bs=1 | od -An -t x8 | sed 's/^ /0x/')
2021-02-23 03:16:12 +03:00
LIMINE_VERSION := $(shell git describe --exact-match --tags `git log -n1 --pretty='%h'` || git log -n1 --pretty='%h')
2020-10-18 07:23:39 +03:00
WERROR = -Werror
CFLAGS = -Os -g -pipe -Wall -Wextra $(WERROR)
2020-09-13 15:36:18 +03:00
INTERNAL_CFLAGS := \
2020-09-17 15:37:22 +03:00
-std=gnu11 \
-fplan9-extensions \
-ffreestanding \
2020-09-13 15:36:18 +03:00
-fno-stack-protector \
-fno-pic \
2020-11-15 19:56:10 +03:00
-fno-omit-frame-pointer \
2021-03-02 12:23:43 +03:00
-fno-lto \
2020-09-21 13:46:42 +03:00
-Wno-address-of-packed-member \
-masm=intel \
-mgeneral-regs-only \
2020-09-21 13:46:42 +03:00
-MMD \
-DBUILD_ID=$(BUILD_ID) \
-DLIMINE_VERSION='"$(LIMINE_VERSION)"' \
2021-02-26 03:30:27 +03:00
-DCOM_OUTPUT=$(COM_OUTPUT) \
-DE9_OUTPUT=$(E9_OUTPUT) \
2021-03-02 12:23:43 +03:00
-D$(TARGET)=1 \
2020-04-29 17:53:05 +03:00
-I. \
2020-09-21 13:46:42 +03:00
-I..
2021-03-02 12:23:43 +03:00
ifeq ($(TARGET), uefi)
INTERNAL_CFLAGS += \
-I../gnu-efi/inc \
-I../gnu-efi/inc/x86_64 \
-fpic \
-fshort-wchar \
-mno-red-zone \
-mcmodel=small
endif
LDFLAGS = -Os -g
2020-09-13 15:36:18 +03:00
INTERNAL_LDFLAGS := \
2021-03-02 12:23:43 +03:00
-fno-lto \
-nostdlib \
2021-03-02 12:23:43 +03:00
-z max-page-size=0x1000
ifeq ($(TARGET), bios)
INTERNAL_LDFLAGS += \
-static \
-no-pie \
-lgcc \
-static-libgcc
else ifeq ($(TARGET), uefi)
INTERNAL_LDFLAGS += \
-shared \
-z nocombreloc \
-Wl,-Bsymbolic
endif
.PHONY: all clean
C_FILES := $(shell find -L ./ -type f -name '*.c' | sort)
2021-03-02 12:23:43 +03:00
ifeq ($(TARGET), bios)
ASM_FILES := $(shell find -L ./ -type f -name '*.asm' | sort)
2021-03-02 12:23:43 +03:00
endif
2020-09-13 15:36:18 +03:00
OBJ := $(ASM_FILES:.asm=.o) $(C_FILES:.c=.o)
2021-03-02 12:23:43 +03:00
ifeq ($(TARGET), uefi)
OBJ += sys/smp_trampoline.o ../gnu-efi/lib/x86_64/efi_stub.o
endif
2020-09-21 13:46:42 +03:00
HEADER_DEPS := $(C_FILES:.c=.d)
2021-03-02 12:23:43 +03:00
ifeq ($(TARGET), bios)
all: limine_dbg.elf limine.sys stage2.bin stage2.bin.gz
2021-03-02 12:23:43 +03:00
else ifeq ($(TARGET), uefi)
all: limine_dbg.elf BOOTX64.EFI
2021-03-02 12:23:43 +03:00
endif
BOOTX64.EFI: limine.elf
$(OBJCOPY) -I elf64-x86-64 -O efi-app-x86_64 limine.elf $@
2021-02-22 22:43:51 +03:00
stage2.bin.gz: stage2.bin
gzip -n -9 < stage2.bin > stage2.bin.gz
2021-02-21 01:04:06 +03:00
2021-02-25 03:24:54 +03:00
stage2.bin: limine.sys
dd if=limine.sys bs=$$(( 0x$$($(READELF) -S limine.elf | grep .stage3.text | sed 's/^.*] //' | awk '{print $$3}' | sed 's/^0*//') - 0x8000 )) count=1 of=$@
2021-02-21 01:04:06 +03:00
2021-02-25 03:24:54 +03:00
limine.map.o: limine_nomap.elf
2021-03-02 12:23:43 +03:00
ifeq ($(TARGET), bios)
2021-02-25 03:24:54 +03:00
./gensyms.sh $(OBJDUMP) limine_nomap.elf limine
2021-03-02 12:23:43 +03:00
else ifeq ($(TARGET), uefi)
./gensyms64.sh $(OBJDUMP) limine_nomap.elf limine
endif
2021-02-25 03:24:54 +03:00
limine.sys: limine.elf
2020-11-15 19:56:10 +03:00
$(OBJCOPY) -O binary $< $@
limine_nomap.elf: $(OBJ) font.o
2021-03-02 12:23:43 +03:00
$(LD) $(OBJ) font.o $(LDFLAGS) $(INTERNAL_LDFLAGS) -Tlinker_nomap_$(TARGET).ld -o $@
ifeq ($(TARGET), bios)
$(LD) $(OBJ) font.o $(LDFLAGS) $(INTERNAL_LDFLAGS) -Wl,--gc-sections -Tlinker_stage2only.ld -o limine_stage2only.elf || \
( echo "This error means that stage 2 was trying to use stage 3 symbols before loading stage 3" && \
false )
2021-03-02 12:23:43 +03:00
endif
2021-02-25 03:24:54 +03:00
limine_dbg.elf: $(OBJ) font.o
$(LD) $(OBJ) font.o $(LDFLAGS) $(INTERNAL_LDFLAGS) -Tlinker_dbg.ld -o $@
font.o:
$(OBJCOPY) -B i8086 -I binary -O default font.bin $@
limine.elf: $(OBJ) font.o limine.map.o
2021-03-02 12:23:43 +03:00
$(LD) $(OBJ) font.o limine.map.o $(LDFLAGS) $(INTERNAL_LDFLAGS) -Tlinker_$(TARGET).ld -o $@
2020-09-21 13:46:42 +03:00
-include $(HEADER_DEPS)
2020-09-13 15:36:18 +03:00
%.o: %.c
$(CC) $(CFLAGS) $(INTERNAL_CFLAGS) -c $< -o $@
2021-03-02 12:23:43 +03:00
%.o: %.S
$(CC) $(CFLAGS) $(INTERNAL_CFLAGS) -c $< -o $@
2021-03-02 12:23:43 +03:00
ifeq ($(TARGET), bios)
%.o: %.asm
nasm $< -f elf32 -o $@
2021-03-02 12:23:43 +03:00
else ifeq ($(TARGET), uefi)
%.o: %.asm
nasm $< -f elf64 -o $@
endif
clean:
2021-03-02 12:42:35 +03:00
rm -f limine.elf limine_nomap.elf limine_stage2only.elf font.o limine.map.o limine.sys stage2.bin stage2.bin.gz BOOTX64.EFI $(OBJ) $(HEADER_DEPS)