Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Makefile to allow building 32-bit targets on Linux using GCC #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 42 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,14 +1,39 @@
CC=i386-elf-gcc
ASM=i386-elf-as
LD=i386-elf-ld
.PHONY: all dirs clean proper iso

UNAME=$(shell uname | tr a-z A-Z)
ifneq ($(findstring LINUX, $(UNAME)),)
BUILD_OS=LINUX
endif
ifneq ($(findstring DARWIN, $(UNAME)),)
BUILD_OS=MACOSX
endif
ifeq ($(BUILD_OS),)
$(error $(UNAME) support has not yet been coded into this Makefile)
endif


ifeq ($(BUILD_OS),MACOSX)
CC=i386-elf-gcc
ASM=i386-elf-as
LD=i386-elf-ld
ASFLAGS=
LDFLAGS=
BSCTFLAGS=
endif
ifeq ($(BUILD_OS),LINUX)
CC=gcc
ASM=as
LD=ld
ASFLAGS=--32
LDFLAGS=-m elf_i386 -s
BSCTFLAGS:=$(LDFLAGS)
endif

GFLAGS=
CCFLAGS=-m32 -std=c11 -O2 -g -Wall -Wextra -Wpedantic -Wstrict-aliasing
CCFLAGS+=-Wno-pointer-arith -Wno-unused-parameter
CCFLAGS+=-nostdlib -nostdinc -ffreestanding -fno-pie -fno-stack-protector
CCFLAGS+=-fno-builtin-function -fno-builtin
ASFLAGS=
LDFLAGS=

BOOTSECT_SRCS=\
src/stage0.S
Expand All @@ -23,30 +48,33 @@ BOOTSECT=bootsect.bin
KERNEL=kernel.bin
ISO=boot.iso

all: dirs bootsect kernel
all: iso
iso: dirs $(ISO)
proper: clean all

dirs:
mkdir -p bin

clean:
rm ./**/*.o
rm ./*.iso
rm ./**/*.elf
rm ./**/*.bin
rm -f ./**/*.o
rm -f ./*.iso
rm -f ./**/*.elf
rm -f ./**/*.bin
rm -f $(BOOTSECT) $(KERNEL)

%.o: %.c
$(CC) -o $@ -c $< $(GFLAGS) $(CCFLAGS)

%.o: %.S
$(ASM) -o $@ -c $< $(GFLAGS) $(ASFLAGS)

bootsect: $(BOOTSECT_OBJS)
$(LD) -o ./bin/$(BOOTSECT) $^ -Ttext 0x7C00 --oformat=binary
$(BOOTSECT): $(BOOTSECT_OBJS)
$(LD) -o ./bin/$(BOOTSECT) $^ $(BSCTFLAGS) -Ttext 0x7C00 --oformat=binary

kernel: $(KERNEL_OBJS)
$(KERNEL): $(KERNEL_OBJS)
$(LD) -o ./bin/$(KERNEL) $^ $(LDFLAGS) -Tsrc/link.ld

iso: bootsect kernel
$(ISO): $(BOOTSECT) $(KERNEL)
dd if=/dev/zero of=boot.iso bs=512 count=2880
dd if=./bin/$(BOOTSECT) of=boot.iso conv=notrunc bs=512 seek=0 count=1
dd if=./bin/$(KERNEL) of=boot.iso conv=notrunc bs=512 seek=1 count=2048
Binary file removed bin/bootsect.bin
Binary file not shown.
Binary file removed bin/kernel.bin
Binary file not shown.