build: Makefile improvements

This commit is contained in:
mintsuki 2021-12-08 17:41:19 +01:00
parent 2be3776f34
commit 477a04b630
3 changed files with 58 additions and 65 deletions

View File

@ -6,20 +6,23 @@ PREFIX ?= /usr/local
DESTDIR ?=
BUILDDIR ?= $(shell pwd)/build
BINDIR ?= $(BUILDDIR)/bin
override BINDIR := $(BUILDDIR)/bin
SPACE := $(subst ,, )
override SPACE := $(subst ,, )
MKESCAPE = $(subst $(SPACE),\ ,$(1))
SHESCAPE = $(subst ','\'',$(1))
export PATH := $(shell pwd)/toolchain/bin:$(PATH)
override PATH := $(shell pwd)/toolchain/bin:$(PATH)
export PATH
NCPUS := $(shell nproc 2>/dev/null || sysctl -n hw.ncpu 2>/dev/null || echo 1)
override NCPUS := $(shell nproc 2>/dev/null || sysctl -n hw.ncpu 2>/dev/null || echo 1)
export LIMINE_VERSION := $(shell cat version 2>/dev/null || ( git describe --exact-match --tags `git log -n1 --pretty='%h'` 2>/dev/null || git log -n1 --pretty='%h' ) )
override LIMINE_VERSION := $(shell cat version 2>/dev/null || ( git describe --exact-match --tags `git log -n1 --pretty='%h'` 2>/dev/null || git log -n1 --pretty='%h' ) )
export LIMINE_VERSION
export LIMINE_COPYRIGHT := $(shell grep Copyright LICENSE.md)
override LIMINE_COPYRIGHT := $(shell grep Copyright LICENSE.md)
export LIMINE_COPYRIGHT
TOOLCHAIN ?= limine
@ -37,7 +40,7 @@ MAKEOVERRIDES += TOOLCHAIN_CC+=--target=x86_64-elf
endif
endif
CC_MACHINE := $(shell PATH='$(call SHESCAPE,$(PATH))' $(TOOLCHAIN_CC) -dumpmachine | dd bs=6 count=1 2>/dev/null)
override CC_MACHINE := $(shell PATH='$(call SHESCAPE,$(PATH))' $(TOOLCHAIN_CC) -dumpmachine | dd bs=6 count=1 2>/dev/null)
ifneq ($(MAKECMDGOALS), toolchain)
ifneq ($(MAKECMDGOALS), distclean)
@ -51,14 +54,12 @@ endif
endif
endif
STAGE1_FILES := $(shell find -L ./stage1 -type f -name '*.asm' | sort)
override STAGE1_FILES := $(shell find -L ./stage1 -type f -name '*.asm')
.PHONY: all
all:
$(MAKE) limine-uefi
$(MAKE) limine-uefi32
$(MAKE) limine-bios
all: limine-uefi limine-uefi32 limine-bios
$(MAKE) limine-install
$(MAKE) '$(call SHESCAPE,$(BINDIR))/limine-eltorito-efi.bin'
.PHONY: limine-install
limine-install:
@ -68,7 +69,7 @@ limine-install:
.PHONY: clean
clean: limine-bios-clean limine-uefi-clean limine-uefi32-clean
$(MAKE) -C '$(call SHESCAPE,$(BINDIR))' clean || true
rm -rf '$(call SHESCAPE,$(BINDIR))' '$(call SHESCAPE,$(BUILDDIR))/stage1'
.PHONY: install
install: all
@ -109,20 +110,16 @@ $(call MKESCAPE,$(BINDIR))/limine-eltorito-efi.bin:
) || rm -f '$(call SHESCAPE,$@)'
.PHONY: limine-uefi
limine-uefi:
$(MAKE) gnu-efi
limine-uefi: gnu-efi
$(MAKE) stage23-uefi
mkdir -p '$(call SHESCAPE,$(BINDIR))'
cp '$(call SHESCAPE,$(BUILDDIR))/stage23-uefi/BOOTX64.EFI' '$(call SHESCAPE,$(BINDIR))/'
$(MAKE) '$(call SHESCAPE,$(BINDIR))/limine-eltorito-efi.bin'
.PHONY: limine-uefi32
limine-uefi32:
$(MAKE) gnu-efi
limine-uefi32: gnu-efi
$(MAKE) stage23-uefi32
mkdir -p '$(call SHESCAPE,$(BINDIR))'
cp '$(call SHESCAPE,$(BUILDDIR))/stage23-uefi32/BOOTIA32.EFI' '$(call SHESCAPE,$(BINDIR))/'
$(MAKE) '$(call SHESCAPE,$(BINDIR))/limine-eltorito-efi.bin'
.PHONY: limine-bios-clean
limine-bios-clean: stage23-bios-clean decompressor-clean

View File

@ -4,14 +4,14 @@ endif
BUILDDIR =
SPACE := $(subst ,, )
override SPACE := $(subst ,, )
MKESCAPE = $(subst $(SPACE),\ ,$(1))
SHESCAPE = $(subst ','\'',$(1))
OBJESCAPE = $(subst .a ,.a' ',$(subst .o ,.o' ',$(call SHESCAPE,$(1))))
ifeq ($(call MKESCAPE,$(BUILDDIR)), )
$(error $(call MKESCAPE,$(BUILDDIR)) not specified)
$(error BUILDDIR not specified)
endif
TOOLCHAIN ?= limine
@ -33,7 +33,7 @@ endif
WERROR = -Werror
CFLAGS ?= -Os -pipe -Wall -Wextra $(WERROR)
INTERNAL_CFLAGS = \
override INTERNAL_CFLAGS := \
-m32 \
-march=i686 \
-mtune=generic \
@ -56,22 +56,20 @@ INTERNAL_CFLAGS = \
LDFLAGS ?=
INTERNAL_LDFLAGS = \
override INTERNAL_LDFLAGS := \
-melf_i386 \
-nostdlib \
-z max-page-size=0x1000 \
-static \
-Tlinker.ld
.PHONY: all clean
override C_FILES := $(shell find -L ./ -type f -name '*.c')
override ASM_FILES := $(shell find -L ./ -type f -name '*.asm')
override OBJ := $(addprefix $(call MKESCAPE,$(BUILDDIR))/, $(ASM_FILES:.asm=.o) $(C_FILES:.c=.o))
override HEADER_DEPS := $(addprefix $(call MKESCAPE,$(BUILDDIR))/, $(C_FILES:.c=.d))
C_FILES := $(shell find -L ./ -type f -name '*.c' | sort)
ASM_FILES := $(shell find -L ./ -type f -name '*.asm' | sort)
OBJ := $(addprefix $(call MKESCAPE,$(BUILDDIR))/, $(ASM_FILES:.asm=.o) $(C_FILES:.c=.o))
HEADER_DEPS := $(addprefix $(call MKESCAPE,$(BUILDDIR))/, $(C_FILES:.c=.d))
all:
$(MAKE) '$(call SHESCAPE,$(BUILDDIR))/decompressor.bin'
.PHONY: all
all: $(call MKESCAPE,$(BUILDDIR))/decompressor.bin
$(call MKESCAPE,$(BUILDDIR))/decompressor.bin: $(OBJ) $(call MKESCAPE,$(BUILDDIR))/tinf/tinfgzip.o $(call MKESCAPE,$(BUILDDIR))/tinf/tinflate.o
$(TOOLCHAIN_LD) '$(call OBJESCAPE,$^)' $(LDFLAGS) $(INTERNAL_LDFLAGS) -o '$(call SHESCAPE,$(BUILDDIR))/decompressor.elf'
@ -97,5 +95,6 @@ $(call MKESCAPE,$(BUILDDIR))/%.o: %.asm
dirname '$(call SHESCAPE,$@)' | xargs -d '\n' mkdir -p
nasm '$(call SHESCAPE,$<)' -f elf32 -o '$(call SHESCAPE,$@)'
.PHONY: clean
clean:
rm -rf '$(call SHESCAPE,$(BUILDDIR))'

View File

@ -5,26 +5,26 @@ endif
TARGET =
BUILDDIR =
SRCDIR := $(shell pwd)
override SRCDIR := $(shell pwd)
SPACE := $(subst ,, )
override SPACE := $(subst ,, )
MKESCAPE = $(subst $(SPACE),\ ,$(1))
SHESCAPE = $(subst ','\'',$(1))
OBJESCAPE = $(subst .a ,.a' ',$(subst .o ,.o' ',$(call SHESCAPE,$(1))))
ifeq ($(call MKESCAPE,$(BUILDDIR)), )
$(error $(call MKESCAPE,$(BUILDDIR)) not specified)
$(error BUILDDIR not specified)
endif
ifeq ($(TARGET), bios)
OBJCOPY_ARCH := elf32-i386
override OBJCOPY_ARCH := elf32-i386
else ifeq ($(TARGET), uefi)
OBJCOPY_ARCH := elf64-x86-64
override OBJCOPY_ARCH := elf64-x86-64
else ifeq ($(TARGET), uefi32)
OBJCOPY_ARCH := elf32-i386
override OBJCOPY_ARCH := elf32-i386
else
$(error Invalid target)
$(error Invalid target)
endif
TOOLCHAIN ?= limine
@ -61,9 +61,9 @@ E9_OUTPUT = false
WERROR = -Werror
CFLAGS ?= -O3 -g -pipe -Wall -Wextra $(WERROR)
S2CFLAGS := $(CFLAGS) -Os
override S2CFLAGS := $(CFLAGS) -Os
INTERNAL_CFLAGS := \
override INTERNAL_CFLAGS := \
-std=gnu11 \
-ffreestanding \
-fno-stack-protector \
@ -86,7 +86,7 @@ INTERNAL_CFLAGS := \
-I'$(call SHESCAPE,$(BUILDDIR))/tinf'
ifeq ($(TARGET), bios)
INTERNAL_CFLAGS += \
override INTERNAL_CFLAGS += \
-Dbios=1 \
-Duefi=0 \
-m32 \
@ -98,7 +98,7 @@ ifeq ($(TARGET), bios)
endif
ifeq ($(TARGET), uefi)
INTERNAL_CFLAGS32 := \
override INTERNAL_CFLAGS32 := \
$(INTERNAL_CFLAGS) \
-Dbios=0 \
-Duefi=1 \
@ -109,7 +109,7 @@ ifeq ($(TARGET), uefi)
-DGNU_EFI_USE_MS_ABI \
-fpie
INTERNAL_CFLAGS += \
override INTERNAL_CFLAGS += \
-Dbios=0 \
-Duefi=1 \
-m64 \
@ -124,7 +124,7 @@ ifeq ($(TARGET), uefi)
endif
ifeq ($(TARGET), uefi32)
INTERNAL_CFLAGS += \
override INTERNAL_CFLAGS += \
-Dbios=0 \
-Duefi=1 \
-m32 \
@ -139,19 +139,19 @@ endif
LDFLAGS ?=
INTERNAL_LDFLAGS := \
override INTERNAL_LDFLAGS := \
-nostdlib \
-z max-page-size=0x1000
ifeq ($(TARGET), bios)
INTERNAL_LDFLAGS += \
override INTERNAL_LDFLAGS += \
-melf_i386 \
-static \
--build-id=sha1
endif
ifeq ($(TARGET), uefi)
INTERNAL_LDFLAGS += \
override INTERNAL_LDFLAGS += \
-melf_x86_64 \
-static \
-pie \
@ -160,7 +160,7 @@ ifeq ($(TARGET), uefi)
endif
ifeq ($(TARGET), uefi32)
INTERNAL_LDFLAGS += \
override INTERNAL_LDFLAGS += \
-melf_i386 \
-static \
-pie \
@ -170,36 +170,33 @@ endif
.PHONY: all clean
C_FILES := $(shell find -L ./ -type f -name '*.c' | sort)
override C_FILES := $(shell find -L ./ -type f -name '*.c')
ifeq ($(TARGET), bios)
ASM32_FILES := $(shell find -L ./ -type f -name '*.asm32' | sort)
ASMB_FILES := $(shell find -L ./ -type f -name '*.asmb' | sort)
override ASM32_FILES := $(shell find -L ./ -type f -name '*.asm32')
override ASMB_FILES := $(shell find -L ./ -type f -name '*.asmb')
OBJ := $(addprefix $(call MKESCAPE,$(BUILDDIR))/, $(ASM32_FILES:.asm32=.o) $(ASMB_FILES:.asmb=.o) $(C_FILES:.c=.o))
override OBJ := $(addprefix $(call MKESCAPE,$(BUILDDIR))/, $(ASM32_FILES:.asm32=.o) $(ASMB_FILES:.asmb=.o) $(C_FILES:.c=.o))
endif
ifeq ($(TARGET), uefi)
ASM64_FILES := $(shell find -L ./ -type f -name '*.asm64' | sort)
ASM64U_FILES := $(shell find -L ./ -type f -name '*.asm64u' | sort)
override ASM64_FILES := $(shell find -L ./ -type f -name '*.asm64')
override ASM64U_FILES := $(shell find -L ./ -type f -name '*.asm64u')
OBJ := $(addprefix $(call MKESCAPE,$(BUILDDIR))/, $(ASM64_FILES:.asm64=.o) $(ASM64U_FILES:.asm64u=.o) $(C_FILES:.c=.o))
override OBJ := $(addprefix $(call MKESCAPE,$(BUILDDIR))/, $(ASM64_FILES:.asm64=.o) $(ASM64U_FILES:.asm64u=.o) $(C_FILES:.c=.o))
endif
ifeq ($(TARGET), uefi32)
ASM32_FILES := $(shell find -L ./ -type f -name '*.asm32' | sort)
ASM32U_FILES := $(shell find -L ./ -type f -name '*.asm32u' | sort)
override ASM32_FILES := $(shell find -L ./ -type f -name '*.asm32')
override ASM32U_FILES := $(shell find -L ./ -type f -name '*.asm32u')
OBJ := $(addprefix $(call MKESCAPE,$(BUILDDIR))/, $(ASM32_FILES:.asm32=.o) $(ASM32U_FILES:.asm32u=.o) $(C_FILES:.c=.o))
override OBJ := $(addprefix $(call MKESCAPE,$(BUILDDIR))/, $(ASM32_FILES:.asm32=.o) $(ASM32U_FILES:.asm32u=.o) $(C_FILES:.c=.o))
endif
HEADER_DEPS := $(addprefix $(call MKESCAPE,$(BUILDDIR))/, $(C_FILES:.c=.d))
override HEADER_DEPS := $(addprefix $(call MKESCAPE,$(BUILDDIR))/, $(C_FILES:.c=.d))
ifeq ($(TARGET), bios)
all:
$(MAKE) '$(call SHESCAPE,$(BUILDDIR))/limine_dbg.elf' '$(call SHESCAPE,$(BUILDDIR))/limine.sys' '$(call SHESCAPE,$(BUILDDIR))/stage2.bin' '$(call SHESCAPE,$(BUILDDIR))/stage2.bin.gz'
all: $(call MKESCAPE,$(BUILDDIR))/limine_dbg.elf $(call MKESCAPE,$(BUILDDIR))/limine.sys $(call MKESCAPE,$(BUILDDIR))/stage2.bin.gz
else ifeq ($(TARGET), uefi)
all:
$(MAKE) '$(call SHESCAPE,$(BUILDDIR))/BOOTX64.EFI'
all: $(call MKESCAPE,$(BUILDDIR))/BOOTX64.EFI
else ifeq ($(TARGET), uefi32)
all:
$(MAKE) '$(call SHESCAPE,$(BUILDDIR))/BOOTIA32.EFI'
all: $(call MKESCAPE,$(BUILDDIR))/BOOTIA32.EFI
endif
$(call MKESCAPE,$(BUILDDIR))/sys/smp_trampoline.bin: sys/smp_trampoline.real