Skip to content

Commit

Permalink
Refactor GNUmakefile
Browse files Browse the repository at this point in the history
  • Loading branch information
yousinix committed Feb 23, 2020
1 parent 04c2a9e commit 8e2612d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 124 deletions.
148 changes: 30 additions & 118 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,86 +5,29 @@
# Recursive Make Considered Harmful
# http://aegis.sourceforge.net/auug97.pdf
#
OBJDIR := obj

ifdef LAB
SETTINGLAB := true
else
-include conf/lab.mk
endif

-include conf/env.mk

ifndef SOL
SOL := 0
endif
ifndef LABADJUST
LABADJUST := 0
endif

ifndef LABSETUP
LABSETUP := ./
endif

V := @
TOP := .
OBJDIR := obj
TOOLPREFIX := i386-elf-
QEMU := qemu-system-i386
PERL := perl
IMAGE := $(OBJDIR)/fos.img

TOP = .
CC := $(TOOLPREFIX)gcc -m32 -pipe
GCC_LIB := $(shell $(CC) -print-libgcc-file-name)
AS := $(TOOLPREFIX)as --32
AR := $(TOOLPREFIX)ar
LD := $(TOOLPREFIX)ld -m elf_i386
OBJCOPY := $(TOOLPREFIX)objcopy
OBJDUMP := $(TOOLPREFIX)objdump
NM := $(TOOLPREFIX)nm

# Cross-compiler fos toolchain
#
# This Makefile will automatically use the cross-compiler toolchain
# installed as 'i386-elf-*', if one exists. If the host tools ('gcc',
# 'objdump', and so forth) compile for a 32-bit x86 ELF target, that will
# be detected as well. If you have the right compiler toolchain installed
# using a different name, set GCCPREFIX explicitly in conf/env.mk

# try to infer the correct GCCPREFIX
ifndef GCCPREFIX
GCCPREFIX := $(shell if i386-elf-objdump -i 2>&1 | grep '^elf32-i386$$' >/dev/null 2>&1; \
then echo 'i386-elf-'; \
elif objdump -i 2>&1 | grep 'elf32-i386' >/dev/null 2>&1; \
then echo ''; \
else echo "***" 1>&2; \
echo "*** Error: Couldn't find an i386-*-elf version of GCC/binutils." 1>&2; \
echo "*** Is the directory with i386-elf-gcc in your PATH?" 1>&2; \
echo "*** If your i386-*-elf toolchain is installed with a command" 1>&2; \
echo "*** prefix other than 'i386-elf-', set your GCCPREFIX" 1>&2; \
echo "*** environment variable to that prefix and run 'make' again." 1>&2; \
echo "*** To turn off this error, run 'gmake GCCPREFIX= ...'." 1>&2; \
echo "***" 1>&2; exit 1; fi)
endif

# Note for migration to new build systems, if this doesn't work with 64 compiler try
# to add specific 32bit options like: -m32, --32 and -m elf_i386 to CC, AS, AR and LD
#CC := $(GCCPREFIX)gcc -m32 -pipe
#GCC_LIB := $(shell $(CC) -print-libgcc-file-name)
#AS := $(GCCPREFIX)as --32
#AR := $(GCCPREFIX)ar
#LD := $(GCCPREFIX)ld -m elf_i386
#OBJCOPY := $(GCCPREFIX)objcopy
#OBJDUMP := $(GCCPREFIX)objdump
#NM := $(GCCPREFIX)nm

CC := i386-elf-gcc -m32 -pipe
GCC_LIB := $(shell $(CC) -print-libgcc-file-name)
AS := i386-elf-as --32
AR := i386-elf-ar
LD := i386-elf-ld -m elf_i386
OBJCOPY := i386-elf-objcopy
OBJDUMP := i386-elf-objdump
NM := i386-elf-nm


# Note for migration to new build systems, if this doesn't work with 64 compilers, try adding -m32 to NCC flags
# Native commands
NCC := gcc $(CC_VER) -m32 -pipe
TAR := gtar
PERL := perl

# Compiler flags
# -fno-builtin is required to avoid refs to undefined functions in the kernel.
# Only optimize to -O1 to discourage inlining, which complicates backtraces.
#CFLAGS := $(CFLAGS) $(DEFS) $(LABDEFS) -O -fno-builtin -I$(TOP) -MD -Wall -Wno-format -Wno-unused -Werror -gstabs
CFLAGS := $(CFLAGS) $(DEFS) $(LABDEFS) -O0 -fno-builtin -I$(TOP) -MD -Wall -Wno-format -Wno-unused -Werror -fno-stack-protector -ggdb -g3
CFLAGS := -O0 -fno-builtin -I$(TOP) -MD -Wall -Wno-format -Wno-unused -Werror -fno-stack-protector -ggdb -g3

# Linker flags for FOS user programs
ULDFLAGS := -T user/user.ld
Expand All @@ -102,13 +45,14 @@ all:
.DELETE_ON_ERROR:

# make it so that no intermediate .o files are ever deleted
.PRECIOUS: %.o $(OBJDIR)/boot/%.o $(OBJDIR)/kern/%.o \
$(OBJDIR)/lib/%.o $(OBJDIR)/fs/%.o $(OBJDIR)/user/%.o
.PRECIOUS: %.o \
$(OBJDIR)/boot/%.o \
$(OBJDIR)/kern/%.o \
$(OBJDIR)/lib/%.o \
$(OBJDIR)/user/%.o

KERN_CFLAGS := $(CFLAGS) -DFOS_KERNEL -gstabs
USER_CFLAGS := $(CFLAGS) -DJOS_USER -gstabs


USER_CFLAGS := $(CFLAGS) -DFOS_USER -gstabs


# Include Makefrags for subdirectories
Expand All @@ -118,41 +62,18 @@ include lib/Makefrag
include user/Makefrag


IMAGE = $(OBJDIR)/kern/fos.img

# For deleting the build
clean:
rm -rf $(OBJDIR)

realclean: clean
rm -rf lab$(LAB).tar.gz

distclean: realclean
rm -rf conf/gcc.mk
# Emulators

grade: $(LABSETUP)grade.sh
$(V)$(MAKE) clean >/dev/null 2>/dev/null
$(MAKE) all
sh $(LABSETUP)grade.sh

handin: tarball
@echo Please visit http://pdos.csail.mit.edu/cgi-bin/828handin
@echo and upload lab$(LAB)-handin.tar.gz. Thanks!
QEMUOPTS = -drive file=$(IMAGE),media=disk,format=raw -smp 2 -m 512 $(QEMUEXTRAS)

tarball: realclean
tar cf - `find . -type f | grep -v '^\.*$$' | grep -v '/CVS/' | grep -v '/\.svn/' | grep -v 'lab[0-9].*\.tar\.gz'` | gzip > lab$(LAB)-handin.tar.gz
qemu:
$(V)$(QEMU) -serial mon:stdio $(QEMUOPTS)

# For test runs

#run-%:
# $(V)rm -f $(OBJDIR)/kern/init.o $(IMAGES)
# $(V)$(MAKE) "DEFS=-DTEST=_binary_obj_user_$*_start -DTESTSIZE=_binary_obj_user_$*_size" $(IMAGES)
# bochs -q 'display_library: nogui'
# For deleting the build
clean:
rm -rf $(OBJDIR)

#xrun-%:
# $(V)rm -f $(OBJDIR)/kern/init.o $(IMAGES)
# $(V)$(MAKE) "DEFS=-DTEST=_binary_obj_user_$*_start -DTESTSIZE=_binary_obj_user_$*_size" $(IMAGES)
# bochs -q

# This magic automatically generates makefile dependencies
# for header files included from C source files we compile,
Expand All @@ -164,14 +85,5 @@ $(OBJDIR)/.deps: $(foreach dir, $(OBJDIRS), $(wildcard $(OBJDIR)/$(dir)/*.d))

-include $(OBJDIR)/.deps

always:
@:

QEMU := qemu-system-i386
QEMUOPTS = -drive file=$(IMAGE),media=disk,format=raw -smp 2 -m 512 $(QEMUEXTRAS)

qemu:
$(V)$(QEMU) -serial mon:stdio $(QEMUOPTS)

.PHONY: all always qemu \
handin tarball clean realclean clean-labsetup distclean grade labsetup
.PHONY: all clean qemu
12 changes: 6 additions & 6 deletions kern/Makefrag
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,14 @@ $(OBJDIR)/kern/kernel: $(KERN_OBJFILES) $(KERN_BINFILES) kern/kernel.ld
$(V)$(NM) -n $@ > [email protected]

# How to build the FOS disk image
$(OBJDIR)/kern/fos.img: $(OBJDIR)/kern/kernel $(OBJDIR)/boot/boot
$(IMAGE): $(OBJDIR)/kern/kernel $(OBJDIR)/boot/boot
@echo + mk $@
$(V)dd if=/dev/zero of=$(OBJDIR)/kern/fos.img~ count=10000 2>/dev/null
$(V)dd if=$(OBJDIR)/boot/boot of=$(OBJDIR)/kern/fos.img~ conv=notrunc 2>/dev/null
$(V)dd if=$(OBJDIR)/kern/kernel of=$(OBJDIR)/kern/fos.img~ seek=1 conv=notrunc 2>/dev/null
$(V)mv $(OBJDIR)/kern/fos.img~ $(OBJDIR)/kern/fos.img
$(V)dd if=/dev/zero of=$(IMAGE)~ count=10000 2>/dev/null
$(V)dd if=$(OBJDIR)/boot/boot of=$(IMAGE)~ conv=notrunc 2>/dev/null
$(V)dd if=$(OBJDIR)/kern/kernel of=$(IMAGE)~ seek=1 conv=notrunc 2>/dev/null
$(V)mv $(IMAGE)~ $(IMAGE)

all: $(OBJDIR)/kern/fos.img
all: $(IMAGE)

grub: $(OBJDIR)/fos-grub

Expand Down

0 comments on commit 8e2612d

Please sign in to comment.