build: Make build system more flexible to use when using the host toolchain

This commit is contained in:
mintsuki 2021-04-08 19:41:46 +02:00
parent 7e5b809a7e
commit 543edfa0b3
5 changed files with 90 additions and 42 deletions

View File

@ -1,14 +1,24 @@
CC = cc
PREFIX = /usr/local
DESTDIR =
PATH := $(shell pwd)/toolchain/bin:$(PATH)
SHELL := env PATH=$(PATH) /bin/bash
TOOLCHAIN = x86_64-elf
TOOLCHAIN_CC = $(TOOLCHAIN)-gcc
AR = $(TOOLCHAIN)-ar
OBJCOPY = $(TOOLCHAIN)-objcopy
TOOLCHAIN_AR = $(TOOLCHAIN)-ar
PATH := $(shell pwd)/toolchain/bin:$(PATH)
ifeq ($(shell which $(TOOLCHAIN_CC)), )
TOOLCHAIN_CC := gcc
endif
ifeq ($(shell which $(TOOLCHAIN_AR)), )
TOOLCHAIN_AR := ar
endif
ifneq ($(shell $(TOOLCHAIN_CC) -dumpmachine | head -c 6), x86_64)
$(error No suitable x86_64 GCC compiler found, please install an x86_64 GCC toolchain or run "make toolchain")
endif
STAGE1_FILES := $(shell find -L ./stage1 -type f -name '*.asm' | sort)
@ -100,7 +110,7 @@ toolchain:
gnu-efi:
git clone https://git.code.sf.net/p/gnu-efi/code --branch=3.0.13 --depth=1 $@
$(MAKE) -C gnu-efi/gnuefi CC="$(TOOLCHAIN_CC) -m64" AR="$(AR)"
$(MAKE) -C gnu-efi/gnuefi CC="$(TOOLCHAIN_CC) -m64" AR="$(TOOLCHAIN_AR)"
$(MAKE) -C gnu-efi/lib CC="$(TOOLCHAIN_CC) -m64" ARCH=x86_64 x86_64/efi_stub.o
ovmf:

View File

@ -32,13 +32,13 @@ such as Long Mode, 5-level paging, and SMP (multicore), to name a few.
For convenience, for point releases, binaries are distributed. These binaries are
shipped in the `-binary` branches and tags of this repository (see [branches](https://github.com/limine-bootloader/limine/branches/all) and [tags](https://github.com/limine-bootloader/limine/tags)).
For example, to clone the latest binary release of the `v2.0` branch one can do
For example, to clone the latest binary release of the `v2.x` branch one can do
```bash
git clone https://github.com/limine-bootloader/limine.git --branch=v2.0-branch-binary --depth=1
```
or, to clone a specific binary point release (for example v2.0.1)
or, to clone a specific binary point release (for example v2.1.6)
```bash
git clone https://github.com/limine-bootloader/limine.git --branch=v2.0.1-binary --depth=1
git clone https://github.com/limine-bootloader/limine.git --branch=v2.1.6-binary --depth=1
```
Additionally, the absolute latest Limine binary release can be obtained by fetching
@ -52,23 +52,39 @@ rebuild `limine-install`, simply use `make` in the binary release.
## Building the bootloader
*These steps are not necessary if cloning a binary release. if so, skip to the*
*next paragraph.*
*These steps are not necessary if cloning a binary release. if so, skip to*
*"Installing Limine binaries".*
It is necessary to first build the set of tools that the bootloader needs
in order to be built.
### Building the toolchain
This can be accomplished by running:
This step can take a long time, but it will ensure that the compiler will work with
Limine. If on an x86_64 host, with GCC installed, you can also skip to the next
paragraph.
The toolchain building process depends on the following packages: `wget`, `gcc`,
`g++`, `binutils`.
Building the toolchain can be accomplished by running:
```bash
make toolchain
```
*The above step may take a while*
After that is done, the bootloader itself can be built with:
### Building Limine
In order to build Limine, the following packages have to be installed: `git`,
`nasm`, `mtools`. Furthermore, either the toolchain must have been built in the
previous paragraph, or `gcc` and `binutils` must also be installed.
The bootloader can then be built with:
```bash
make
```
It is possible to pass `make` additional flags, most relevantly, `TOOLCHAIN=` which
allows one to specify an alternative toolchain for the build system to use
(the default is `x86_64-elf`, falling back to no-triple, or host, toolchain).
The generated bootloader files are going to be in `bin`.
## Installing Limine binaries

View File

@ -6,8 +6,15 @@ endif
TOOLCHAIN = x86_64-elf
CC = $(TOOLCHAIN)-gcc
OBJCOPY = $(TOOLCHAIN)-objcopy
TOOLCHAIN_CC = $(TOOLCHAIN)-gcc
TOOLCHAIN_OBJCOPY = $(TOOLCHAIN)-objcopy
ifeq ($(shell which $(TOOLCHAIN_CC)), )
TOOLCHAIN_CC := gcc
endif
ifeq ($(shell which $(OBJCOPY)), )
TOOLCHAIN_OBJCOPY := objcopy
endif
CFLAGS = -flto -Os -pipe -Wall -Wextra -Werror
@ -55,13 +62,13 @@ builddir:
for i in $(OBJ); do mkdir -p `dirname $$i`; done
$(BUILDDIR)/decompressor.bin: $(OBJ)
$(CC) $(OBJ) $(LDFLAGS) $(INTERNAL_LDFLAGS) -o $(BUILDDIR)/decompressor.elf
$(OBJCOPY) -O binary $(BUILDDIR)/decompressor.elf $@
$(TOOLCHAIN_CC) $(OBJ) $(LDFLAGS) $(INTERNAL_LDFLAGS) -o $(BUILDDIR)/decompressor.elf
$(TOOLCHAIN_OBJCOPY) -O binary $(BUILDDIR)/decompressor.elf $@
-include $(HEADER_DEPS)
$(BUILDDIR)/%.o: %.c
$(CC) $(CFLAGS) $(INTERNAL_CFLAGS) -c $< -o $@
$(TOOLCHAIN_CC) $(CFLAGS) $(INTERNAL_CFLAGS) -c $< -o $@
$(BUILDDIR)/%.o: %.asm
nasm $< -f elf32 -o $@

View File

@ -1,7 +1,9 @@
CC = cc
OBJCOPY = objcopy
PREFIX = /usr/local
DESTDIR =
OBJCOPY = objcopy
OBJCOPY_ARCH = default
LIMINE_HDD_BIN = limine-hdd.bin
BUILD_DIR = $(shell realpath .)

View File

@ -15,10 +15,23 @@ endif
TOOLCHAIN = x86_64-elf
CC = $(TOOLCHAIN)-gcc
OBJCOPY = $(TOOLCHAIN)-objcopy
OBJDUMP = $(TOOLCHAIN)-objdump
READELF = $(TOOLCHAIN)-readelf
TOOLCHAIN_CC = $(TOOLCHAIN)-gcc
TOOLCHAIN_OBJCOPY = $(TOOLCHAIN)-objcopy
TOOLCHAIN_OBJDUMP = $(TOOLCHAIN)-objdump
TOOLCHAIN_READELF = $(TOOLCHAIN)-readelf
ifeq ($(shell which $(TOOLCHAIN_CC)), )
TOOLCHAIN_CC := gcc
endif
ifeq ($(shell which $(TOOLCHAIN_OBJCOPY)), )
TOOLCHAIN_OBJCOPY := objcopy
endif
ifeq ($(shell which $(TOOLCHAIN_OBJDUMP)), )
TOOLCHAIN_OBJDUMP := objdump
endif
ifeq ($(shell which $(TOOLCHAIN_READELF)), )
TOOLCHAIN_READELF := readelf
endif
COM_OUTPUT = false
E9_OUTPUT = false
@ -134,11 +147,11 @@ $(BUILDDIR)/sys/smp_trampoline.bin: sys/smp_trampoline.real
$(BUILDDIR)/sys/smp_trampoline.o: $(BUILDDIR)/sys/smp_trampoline.bin
cd "`dirname $<`" && \
$(OBJCOPY) -B i8086 -I binary -O $(OBJCOPY_ARCH) "`basename $<`" $@
$(TOOLCHAIN_OBJCOPY) -B i8086 -I binary -O $(OBJCOPY_ARCH) "`basename $<`" $@
$(BUILDDIR)/font.o: font.bin
cd "`dirname $<`" && \
$(OBJCOPY) -B i8086 -I binary -O $(OBJCOPY_ARCH) "`basename $<`" $@
$(TOOLCHAIN_OBJCOPY) -B i8086 -I binary -O $(OBJCOPY_ARCH) "`basename $<`" $@
ifeq ($(TARGET), bios)
@ -146,34 +159,34 @@ $(BUILDDIR)/stage2.bin.gz: $(BUILDDIR)/stage2.bin
gzip -n -9 < $< > $@
$(BUILDDIR)/stage2.bin: $(BUILDDIR)/limine.sys
dd if=$< bs=$$(( 0x$$($(READELF) -S $(BUILDDIR)/limine.elf | grep .stage3.text | sed 's/^.*] //' | awk '{print $$3}' | sed 's/^0*//') - 0x8000 )) count=1 of=$@
dd if=$< bs=$$(( 0x$$($(TOOLCHAIN_READELF) -S $(BUILDDIR)/limine.elf | grep .stage3.text | sed 's/^.*] //' | awk '{print $$3}' | sed 's/^0*//') - 0x8000 )) count=1 of=$@
$(BUILDDIR)/stage2.map.o: $(BUILDDIR)/limine_stage2only.elf
GENSYMS="`pwd`/gensyms.sh" && \
cd "`dirname $<`" && \
"$$GENSYMS" $(OBJDUMP) $< stage2 32
"$$GENSYMS" $(TOOLCHAIN_OBJDUMP) $< stage2 32
$(BUILDDIR)/full.map.o: $(BUILDDIR)/limine_nomap.elf
GENSYMS="`pwd`/gensyms.sh" && \
cd "`dirname $<`" && \
"$$GENSYMS" $(OBJDUMP) $< full 32
"$$GENSYMS" $(TOOLCHAIN_OBJDUMP) $< full 32
$(BUILDDIR)/limine.sys: $(BUILDDIR)/limine.elf
$(OBJCOPY) -O binary $< $@
$(TOOLCHAIN_OBJCOPY) -O binary $< $@
$(BUILDDIR)/limine_stage2only.elf: $(OBJ)
$(CC) $^ $(LDFLAGS) $(INTERNAL_LDFLAGS) -Tlinker_stage2only.ld -o $@ || \
$(TOOLCHAIN_CC) $^ $(LDFLAGS) $(INTERNAL_LDFLAGS) -Tlinker_stage2only.ld -o $@ || \
( echo "This error means that stage 2 was trying to use stage 3 symbols before loading stage 3" && \
false )
$(BUILDDIR)/limine_nomap.elf: $(OBJ) $(BUILDDIR)/font.o $(BUILDDIR)/sys/smp_trampoline.o $(BUILDDIR)/stage2.map.o
$(CC) $^ $(LDFLAGS) $(INTERNAL_LDFLAGS) -Tlinker_nomap.ld -o $@
$(TOOLCHAIN_CC) $^ $(LDFLAGS) $(INTERNAL_LDFLAGS) -Tlinker_nomap.ld -o $@
$(BUILDDIR)/limine.elf: $(OBJ) $(BUILDDIR)/font.o $(BUILDDIR)/sys/smp_trampoline.o $(BUILDDIR)/stage2.map.o $(BUILDDIR)/full.map.o
$(CC) $^ $(LDFLAGS) $(INTERNAL_LDFLAGS) -Tlinker.ld -o $@
$(TOOLCHAIN_CC) $^ $(LDFLAGS) $(INTERNAL_LDFLAGS) -Tlinker.ld -o $@
$(BUILDDIR)/limine_dbg.elf: $(OBJ) $(BUILDDIR)/font.o $(BUILDDIR)/sys/smp_trampoline.o $(BUILDDIR)/stage2.map.o $(BUILDDIR)/full.map.o
$(CC) $^ $(LDFLAGS) $(INTERNAL_LDFLAGS) -Tlinker_dbg.ld -o $@
$(TOOLCHAIN_CC) $^ $(LDFLAGS) $(INTERNAL_LDFLAGS) -Tlinker_dbg.ld -o $@
endif
@ -182,13 +195,13 @@ ifeq ($(TARGET), uefi)
$(BUILDDIR)/full.map.o: $(BUILDDIR)/limine_efi_nomap.elf
GENSYMS="`pwd`/gensyms.sh" && \
cd "`dirname $<`" && \
"$$GENSYMS" $(OBJDUMP) $< full 64
"$$GENSYMS" $(TOOLCHAIN_OBJDUMP) $< full 64
$(BUILDDIR)/BOOTX64.EFI: $(BUILDDIR)/limine_efi.elf
$(OBJCOPY) -j .text -j .sdata -j .data -j .dynamic -j .dynsym -j .rel -j .rela -j .rel.* -j .rela.* -j .reloc --target efi-app-x86_64 --subsystem=10 $< $@
$(TOOLCHAIN_OBJCOPY) -j .text -j .sdata -j .data -j .dynamic -j .dynsym -j .rel -j .rela -j .rel.* -j .rela.* -j .reloc --target efi-app-x86_64 --subsystem=10 $< $@
$(BUILDDIR)/limine_efi_nomap.elf: $(OBJ) $(BUILDDIR)/font.o $(BUILDDIR)/sys/smp_trampoline.o
$(CC) \
$(TOOLCHAIN_CC) \
-Tlinker_uefi_nomap.ld \
../gnu-efi/gnuefi/crt0-efi-x86_64.o \
../gnu-efi/gnuefi/libgnuefi.a \
@ -196,7 +209,7 @@ $(BUILDDIR)/limine_efi_nomap.elf: $(OBJ) $(BUILDDIR)/font.o $(BUILDDIR)/sys/smp_
$^ $(LDFLAGS) $(INTERNAL_LDFLAGS) -o $@
$(BUILDDIR)/limine_efi.elf: $(OBJ) $(BUILDDIR)/font.o $(BUILDDIR)/sys/smp_trampoline.o $(BUILDDIR)/full.map.o
$(CC) \
$(TOOLCHAIN_CC) \
-Tlinker_uefi.ld \
../gnu-efi/gnuefi/crt0-efi-x86_64.o \
../gnu-efi/gnuefi/libgnuefi.a \
@ -208,21 +221,21 @@ endif
-include $(HEADER_DEPS)
$(BUILDDIR)/%.o: %.c
$(CC) $(CFLAGS) $(INTERNAL_CFLAGS) -c $< -o $@
$(TOOLCHAIN_CC) $(CFLAGS) $(INTERNAL_CFLAGS) -c $< -o $@
-include $(HEADER_DEPS)
ifeq ($(TARGET), bios)
$(BUILDDIR)/%.s2.o: %.s2.c
$(CC) $(S2CFLAGS) $(INTERNAL_CFLAGS) -c $< -o $@
$(TOOLCHAIN_CC) $(S2CFLAGS) $(INTERNAL_CFLAGS) -c $< -o $@
endif
-include $(HEADER_DEPS)
ifeq ($(TARGET), uefi)
$(BUILDDIR)/%.32.o: %.32.c
$(CC) $(CFLAGS) $(INTERNAL_CFLAGS32) -c $< -o $@.32
$(OBJCOPY) -I elf32-i386 -O elf64-x86-64 $@.32 $@
$(TOOLCHAIN_CC) $(CFLAGS) $(INTERNAL_CFLAGS32) -c $< -o $@.32
$(TOOLCHAIN_OBJCOPY) -I elf32-i386 -O elf64-x86-64 $@.32 $@
rm $@.32
endif