Add make toolchain script and reorganise source tree

This commit is contained in:
mintsuki 2020-01-21 23:45:13 +01:00
parent d624a35427
commit f7b0f240d3
26 changed files with 109 additions and 40 deletions

View File

@ -1,43 +1,7 @@
CC = gcc
LD = ld
CFLAGS = -O2 -pipe -Wall -Wextra
INTERNAL_CFLAGS = \
-m32 \
-ffreestanding \
-nostdlib \
-masm=intel \
-fno-pic \
-mno-sse \
-mno-sse2 \
-ffreestanding \
-fno-stack-protector \
-I.
LDFLAGS =
INTERNAL_LDFLAGS = \
-m elf_i386 \
-nostdlib \
-Tlinker.ld
.PHONY: all clean
C_FILES := $(shell find ./ -type f -name '*.c')
OBJ := $(C_FILES:.c=.o)
all: qloader2.bin
qloader2.bin: bootsect/bootsect.bin $(OBJ)
$(LD) $(LDFLAGS) $(INTERNAL_LDFLAGS) $(OBJ) -o stage2.bin
cat bootsect/bootsect.bin stage2.bin > $@
bootsect/bootsect.bin: bootsect/bootsect.asm
cd bootsect && nasm bootsect.asm -fbin -o bootsect.bin
%.o: %.c
$(CC) $(CFLAGS) $(INTERNAL_CFLAGS) -c $< -o $@
all:
$(MAKE) -C src all
clean:
rm -f $(OBJ) bootsect/bootsect.bin
$(MAKE) -C src clean

View File

@ -11,7 +11,7 @@ fi
# Variables.
DEVICE="$1"
MBR="$(mktemp)"
QLOADER2="qloader2.bin"
QLOADER2="src/qloader2.bin"
# Copy the loader to the device.
dd if="$DEVICE" of="$MBR" bs=1 count=64 skip=446

44
src/Makefile Normal file
View File

@ -0,0 +1,44 @@
CC = ../toolchain/bin/i386-elf-gcc
CFLAGS = -O2 -pipe -Wall -Wextra
INTERNAL_CFLAGS = \
-ffreestanding \
-masm=intel \
-fno-pic \
-mno-sse \
-mno-sse2 \
-mno-mmx \
-mno-80387 \
-ffreestanding \
-fno-stack-protector \
-I.
LDFLAGS = -O2
INTERNAL_LDFLAGS = \
-nostdlib \
-no-pie \
-lgcc \
-static-libgcc \
-Tlinker.ld
.PHONY: all clean
C_FILES := $(shell find ./ -type f -name '*.c')
OBJ := $(C_FILES:.c=.o)
all: qloader2.bin
qloader2.bin: bootsect/bootsect.bin $(OBJ)
$(CC) $(LDFLAGS) $(INTERNAL_LDFLAGS) $(OBJ) -o stage2.bin
cat bootsect/bootsect.bin stage2.bin > $@
bootsect/bootsect.bin: bootsect/bootsect.asm
cd bootsect && nasm bootsect.asm -fbin -o bootsect.bin
%.o: %.c
$(CC) $(CFLAGS) $(INTERNAL_CFLAGS) -c $< -o $@
clean:
rm -f $(OBJ) bootsect/bootsect.bin

View File

3
toolchain/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
*
!.gitignore
!make_toolchain.sh

58
toolchain/make_toolchain.sh Executable file
View File

@ -0,0 +1,58 @@
#!/usr/bin/env bash
set -e
set -x
PREFIX="$(pwd)"
TARGET=i386-elf
BINUTILSVERSION=2.33.1
GCCVERSION=9.2.0
if [ -z "$MAKEFLAGS" ]; then
MAKEFLAGS="$1"
fi
export MAKEFLAGS
export PATH="$PREFIX/bin:$PATH"
if [ -x "$(command -v gmake)" ]; then
mkdir -p "$PREFIX/bin"
cat <<EOF >"$PREFIX/bin/make"
#!/usr/bin/env sh
gmake "\$@"
EOF
chmod +x "$PREFIX/bin/make"
fi
mkdir -p build
cd build
if [ ! -f binutils-$BINUTILSVERSION.tar.gz ]; then
wget https://ftp.gnu.org/gnu/binutils/binutils-$BINUTILSVERSION.tar.gz
fi
if [ ! -f gcc-$GCCVERSION.tar.gz ]; then
wget https://ftp.gnu.org/gnu/gcc/gcc-$GCCVERSION/gcc-$GCCVERSION.tar.gz
fi
tar -xf binutils-$BINUTILSVERSION.tar.gz
tar -xf gcc-$GCCVERSION.tar.gz
rm -rf build-gcc build-binutils
mkdir build-binutils
cd build-binutils
../binutils-$BINUTILSVERSION/configure --target=$TARGET --prefix="$PREFIX" --with-sysroot --disable-nls --disable-werror
make
make install
cd ..
cd gcc-$GCCVERSION
contrib/download_prerequisites
cd ..
mkdir build-gcc
cd build-gcc
../gcc-$GCCVERSION/configure --target=$TARGET --prefix="$PREFIX" --disable-nls --enable-languages=c --without-headers
make all-gcc
make all-target-libgcc
make install-gcc
make install-target-libgcc