From 291d45d9fb0480ab134bc72af84575047848541a Mon Sep 17 00:00:00 2001 From: mintsuki Date: Sun, 2 Jan 2022 12:45:15 +0100 Subject: [PATCH] build: Makefile improvements --- Makefile | 52 ++++++++++++++++++++++------------------- README.md | 6 ++--- decompressor/Makefile | 11 ++++----- limine-install/Makefile | 13 ++++++----- stage23/Makefile | 31 +++++++++++------------- 5 files changed, 56 insertions(+), 57 deletions(-) diff --git a/Makefile b/Makefile index 611655dc..15398289 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,3 @@ -ifneq (,) -This makefile requires GNU Make. -endif - PREFIX ?= /usr/local DESTDIR ?= @@ -34,7 +30,11 @@ ifeq ($(shell PATH='$(call SHESCAPE,$(PATH))' command -v $(TOOLCHAIN_CC) ; ), ) override TOOLCHAIN_CC := cc endif -ifeq ($(TOOLCHAIN_CC), clang) +override USING_CLANG := $(shell $(TOOLCHAIN_CC) --version | grep clang >/dev/null && echo 1) +export USING_CLANG + +ifeq ($(USING_CLANG), 1) +override ORIG_TOOLCHAIN_CC := $(TOOLCHAIN_CC) override TOOLCHAIN_CC += --target=x86_64-elf endif @@ -52,15 +52,14 @@ endif endif endif -ifeq ($(TOOLCHAIN_CC), clang --target=x86_64-elf) -override TOOLCHAIN_CC := clang +ifeq ($(USING_CLANG), 1) +override TOOLCHAIN_CC := $(ORIG_TOOLCHAIN_CC) endif override STAGE1_FILES := $(shell find -L ./stage1 -type f -name '*.asm') .PHONY: all -all: limine-uefi limine-uefi32 limine-bios - $(MAKE) limine-install limine-eltorito-efi +all: limine-uefi limine-bios .PHONY: limine-install limine-install: @@ -69,7 +68,7 @@ limine-install: $(MAKE) -C '$(call SHESCAPE,$(BINDIR))' .PHONY: clean -clean: limine-bios-clean limine-uefi-clean limine-uefi32-clean +clean: limine-bios-clean limine-uefi32-clean limine-uefi64-clean rm -rf '$(call SHESCAPE,$(BINDIR))' '$(call SHESCAPE,$(BUILDDIR))/stage1' .PHONY: install @@ -96,6 +95,7 @@ $(call MKESCAPE,$(BUILDDIR))/stage1: $(STAGE1_FILES) $(call MKESCAPE,$(BUILDDIR) .PHONY: limine-bios limine-bios: stage23-bios decompressor $(MAKE) '$(call SHESCAPE,$(BUILDDIR))/stage1' + $(MAKE) limine-install .PHONY: limine-eltorito-efi limine-eltorito-efi: @@ -104,17 +104,21 @@ limine-eltorito-efi: ( mformat -i '$(call SHESCAPE,$(BINDIR))/limine-eltorito-efi.bin' -f 1440 :: && \ mmd -D s -i '$(call SHESCAPE,$(BINDIR))/limine-eltorito-efi.bin' ::/EFI && \ mmd -D s -i '$(call SHESCAPE,$(BINDIR))/limine-eltorito-efi.bin' ::/EFI/BOOT && \ - ( ( [ -f '$(call SHESCAPE,$(BUILDDIR))/stage23-uefi/BOOTX64.EFI' ] && \ - mcopy -D o -i '$(call SHESCAPE,$(BINDIR))/limine-eltorito-efi.bin' '$(call SHESCAPE,$(BUILDDIR))/stage23-uefi/BOOTX64.EFI' ::/EFI/BOOT ) || true ) && \ + ( ( [ -f '$(call SHESCAPE,$(BUILDDIR))/stage23-uefi64/BOOTX64.EFI' ] && \ + mcopy -D o -i '$(call SHESCAPE,$(BINDIR))/limine-eltorito-efi.bin' '$(call SHESCAPE,$(BUILDDIR))/stage23-uefi64/BOOTX64.EFI' ::/EFI/BOOT ) || true ) && \ ( ( [ -f '$(call SHESCAPE,$(BUILDDIR))/stage23-uefi32/BOOTIA32.EFI' ] && \ mcopy -D o -i '$(call SHESCAPE,$(BINDIR))/limine-eltorito-efi.bin' '$(call SHESCAPE,$(BUILDDIR))/stage23-uefi32/BOOTIA32.EFI' ::/EFI/BOOT ) || true ) \ ) || rm -f '$(call SHESCAPE,$(BINDIR))/limine-eltorito-efi.bin' .PHONY: limine-uefi -limine-uefi: reduced-gnu-efi - $(MAKE) stage23-uefi +limine-uefi: limine-uefi32 limine-uefi64 + $(MAKE) limine-eltorito-efi + +.PHONY: limine-uefi64 +limine-uefi64: reduced-gnu-efi + $(MAKE) stage23-uefi64 mkdir -p '$(call SHESCAPE,$(BINDIR))' - cp '$(call SHESCAPE,$(BUILDDIR))/stage23-uefi/BOOTX64.EFI' '$(call SHESCAPE,$(BINDIR))/' + cp '$(call SHESCAPE,$(BUILDDIR))/stage23-uefi64/BOOTX64.EFI' '$(call SHESCAPE,$(BINDIR))/' .PHONY: limine-uefi32 limine-uefi32: reduced-gnu-efi @@ -125,8 +129,8 @@ limine-uefi32: reduced-gnu-efi .PHONY: limine-bios-clean limine-bios-clean: stage23-bios-clean decompressor-clean -.PHONY: limine-uefi-clean -limine-uefi-clean: stage23-uefi-clean +.PHONY: limine-uefi64-clean +limine-uefi64-clean: stage23-uefi64-clean .PHONY: limine-uefi32-clean limine-uefi32-clean: stage23-uefi32-clean @@ -162,13 +166,13 @@ stivale: reduced-gnu-efi: git clone https://github.com/limine-bootloader/reduced-gnu-efi.git -.PHONY: stage23-uefi -stage23-uefi: stivale - $(MAKE) -C stage23 all TARGET=uefi BUILDDIR='$(call SHESCAPE,$(BUILDDIR))/stage23-uefi' +.PHONY: stage23-uefi64 +stage23-uefi64: stivale + $(MAKE) -C stage23 all TARGET=uefi64 BUILDDIR='$(call SHESCAPE,$(BUILDDIR))/stage23-uefi64' -.PHONY: stage23-uefi-clean -stage23-uefi-clean: - $(MAKE) -C stage23 clean TARGET=uefi BUILDDIR='$(call SHESCAPE,$(BUILDDIR))/stage23-uefi' +.PHONY: stage23-uefi64-clean +stage23-uefi64-clean: + $(MAKE) -C stage23 clean TARGET=uefi64 BUILDDIR='$(call SHESCAPE,$(BUILDDIR))/stage23-uefi64' .PHONY: stage23-uefi32 stage23-uefi32: stivale @@ -430,7 +434,7 @@ uefi-test: $(MAKE) ovmf-x64 $(MAKE) test-clean $(MAKE) test.hdd - $(MAKE) limine-uefi + $(MAKE) limine-uefi64 $(MAKE) -C test rm -rf test_image/ mkdir test_image diff --git a/README.md b/README.md index 77c3b45e..158fffb3 100644 --- a/README.md +++ b/README.md @@ -111,17 +111,17 @@ is `./build/bin`). In order to build the BIOS port fully using clang/LLVM, run `make` as such: ```bash # (or gmake where applicable) -make limine-bios limine-install CC="clang" TOOLCHAIN="llvm" TOOLCHAIN_CC="clang" TOOLCHAIN_LD="ld.lld" +make limine-bios CC="clang" TOOLCHAIN="llvm" TOOLCHAIN_CC="clang" TOOLCHAIN_LD="ld.lld" ``` And in order to build the UEFI port using clang/LLVM + `GNU binutils`, run `make` as such: ```bash # (or gmake where applicable) -make limine-uefi TOOLCHAIN="llvm" TOOLCHAIN_CC="clang" TOOLCHAIN_LD="ld" TOOLCHAIN_OBJCOPY="objcopy" +make limine-uefi TOOLCHAIN="llvm" TOOLCHAIN_CC="clang" TOOLCHAIN_LD="ld.bfd" TOOLCHAIN_OBJCOPY="objcopy" ``` -Where `ld` and `objcopy` refer to GNU binutils versions of them. Specify their +Where `ld.bfd` and `objcopy` refer to GNU binutils versions of them. Specify their full path if necessary. ## Installing Limine binaries diff --git a/decompressor/Makefile b/decompressor/Makefile index 5b94283d..ef193ffe 100644 --- a/decompressor/Makefile +++ b/decompressor/Makefile @@ -1,8 +1,5 @@ -ifneq (,) -This makefile requires GNU Make. -endif - -BUILDDIR = +BUILDDIR ?= +USING_CLANG ?= 0 override SPACE := $(subst ,, ) @@ -30,11 +27,11 @@ ifeq ($(shell command -v $(TOOLCHAIN_OBJCOPY) ; ), ) override TOOLCHAIN_OBJCOPY := objcopy endif -ifeq ($(TOOLCHAIN_CC), clang) +ifeq ($(USING_CLANG), 1) override TOOLCHAIN_CC += --target=i686-elf endif -WERROR = -Werror +WERROR ?= -Werror CFLAGS ?= -Os -pipe -Wall -Wextra $(WERROR) override INTERNAL_CFLAGS := \ diff --git a/limine-install/Makefile b/limine-install/Makefile index 0f789d75..0443ba1a 100644 --- a/limine-install/Makefile +++ b/limine-install/Makefile @@ -1,14 +1,14 @@ -CC = cc +CC ?= cc -PREFIX = /usr/local -DESTDIR = +PREFIX ?= /usr/local +DESTDIR ?= -CFLAGS = -O2 -pipe -Wall -Wextra - -.PHONY: all install clean +CFLAGS ?= -O2 -pipe -Wall -Wextra +.PHONY: all all: limine-install +.PHONY: install install: all install -d '$(DESTDIR)$(PREFIX)/bin' install -s limine-install '$(DESTDIR)$(PREFIX)/bin/' @@ -21,6 +21,7 @@ install: all install -m 644 BOOTX64.EFI '$(DESTDIR)$(PREFIX)/share/limine/' install -m 644 BOOTIA32.EFI '$(DESTDIR)$(PREFIX)/share/limine/' +.PHONY: clean clean: rm -f limine-install limine-install.exe diff --git a/stage23/Makefile b/stage23/Makefile index b0f43d64..c77d62aa 100644 --- a/stage23/Makefile +++ b/stage23/Makefile @@ -1,9 +1,6 @@ -ifneq (,) -This makefile requires GNU Make. -endif - -TARGET = -BUILDDIR = +TARGET ?= +BUILDDIR ?= +USING_CLANG ?= 0 override SRCDIR := $(shell pwd) @@ -19,7 +16,7 @@ endif ifeq ($(TARGET), bios) override OBJCOPY_ARCH := elf32-i386 -else ifeq ($(TARGET), uefi) +else ifeq ($(TARGET), uefi64) override OBJCOPY_ARCH := elf64-x86-64 else ifeq ($(TARGET), uefi32) override OBJCOPY_ARCH := elf32-i386 @@ -55,11 +52,11 @@ ifeq ($(shell command -v $(TOOLCHAIN_READELF) ; ), ) override TOOLCHAIN_READELF := readelf endif -ifeq ($(TOOLCHAIN_CC), clang) +ifeq ($(USING_CLANG), 1) ifeq ($(TARGET), bios) override TOOLCHAIN_CC += --target=i686-elf endif -ifeq ($(TARGET), uefi) +ifeq ($(TARGET), uefi64) override TOOLCHAIN_CC += --target=x86_64-elf endif ifeq ($(TARGET), uefi32) @@ -109,7 +106,7 @@ override INTERNAL_CFLAGS += \ -fno-pie endif -ifeq ($(TARGET), uefi) +ifeq ($(TARGET), uefi64) override INTERNAL_CFLAGS32 := \ $(INTERNAL_CFLAGS) \ -Dbios=0 \ @@ -162,7 +159,7 @@ override INTERNAL_LDFLAGS += \ --build-id=sha1 endif -ifeq ($(TARGET), uefi) +ifeq ($(TARGET), uefi64) override INTERNAL_LDFLAGS += \ -melf_x86_64 \ -static \ @@ -189,7 +186,7 @@ override ASMB_FILES := $(shell find -L ./ -type f -name '*.asmb') override OBJ := $(addprefix $(call MKESCAPE,$(BUILDDIR))/, $(ASM32_FILES:.asm32=.o) $(ASMB_FILES:.asmb=.o) $(C_FILES:.c=.o)) endif -ifeq ($(TARGET), uefi) +ifeq ($(TARGET), uefi64) override ASM64_FILES := $(shell find -L ./ -type f -name '*.asm64') override ASM64U_FILES := $(shell find -L ./ -type f -name '*.asm64u') @@ -205,7 +202,7 @@ override HEADER_DEPS := $(addprefix $(call MKESCAPE,$(BUILDDIR))/, $(C_FILES:.c= ifeq ($(TARGET), bios) all: $(call MKESCAPE,$(BUILDDIR))/limine_dbg.elf $(call MKESCAPE,$(BUILDDIR))/limine.sys $(call MKESCAPE,$(BUILDDIR))/stage2.bin.gz -else ifeq ($(TARGET), uefi) +else ifeq ($(TARGET), uefi64) all: $(call MKESCAPE,$(BUILDDIR))/BOOTX64.EFI else ifeq ($(TARGET), uefi32) all: $(call MKESCAPE,$(BUILDDIR))/BOOTIA32.EFI @@ -280,7 +277,7 @@ $(call MKESCAPE,$(BUILDDIR))/reduced-gnu-efi: ../reduced-gnu-efi/* mkdir -p '$(call SHESCAPE,$(BUILDDIR))/' cp -r ../reduced-gnu-efi '$(call SHESCAPE,$(BUILDDIR))/' -ifeq ($(TARGET), uefi) +ifeq ($(TARGET), uefi64) $(call MKESCAPE,$(BUILDDIR))/full.map.o: $(call MKESCAPE,$(BUILDDIR))/limine_efi_nomap.elf cd '$(call SHESCAPE,$(BUILDDIR))' && \ @@ -330,7 +327,7 @@ endif -include $(HEADER_DEPS) -ifeq ($(TARGET), uefi) +ifeq ($(TARGET), uefi64) $(call MKESCAPE,$(BUILDDIR))/%.o: %.c $(call MKESCAPE,$(BUILDDIR))/tinf-copied $(call MKESCAPE,$(BUILDDIR))/reduced-gnu-efi mkdir -p "$$(dirname '$(call SHESCAPE,$@)')" $(TOOLCHAIN_CC) $(CFLAGS) $(INTERNAL_CFLAGS) -c '$(call SHESCAPE,$<)' -o '$(call SHESCAPE,$@)' @@ -358,7 +355,7 @@ endif -include $(HEADER_DEPS) -ifeq ($(TARGET), uefi) +ifeq ($(TARGET), uefi64) $(call MKESCAPE,$(BUILDDIR))/%.32.o: %.32.c $(call MKESCAPE,$(BUILDDIR))/reduced-gnu-efi mkdir -p "$$(dirname '$(call SHESCAPE,$@)')" $(TOOLCHAIN_CC) $(CFLAGS) $(INTERNAL_CFLAGS32) -c '$(call SHESCAPE,$<)' -o '$(call SHESCAPE,$@).32' @@ -376,7 +373,7 @@ $(call MKESCAPE,$(BUILDDIR))/%.o: %.asmb nasm '$(call SHESCAPE,$<)' -F dwarf -g -Werror -f elf32 -o '$(call SHESCAPE,$@)' endif -ifeq ($(TARGET), uefi) +ifeq ($(TARGET), uefi64) $(call MKESCAPE,$(BUILDDIR))/%.o: %.asm64 mkdir -p "$$(dirname '$(call SHESCAPE,$@)')" nasm '$(call SHESCAPE,$<)' -F dwarf -g -Werror -f elf64 -o '$(call SHESCAPE,$@)'