Page 1 of 2

What does your Makefile looks like?

Posted: Thu May 03, 2012 6:48 am
by bluemoon
Ok. It's What does your ___ looks like? series again.

Please share your satisified Makefile, and perhaps we may improve each other :)

Here's mine, generated by a shell script I wrote, which take the form ./configure --arch=i586|x86_64
(ps. Thanks Solar for his Makefile tutorial in the wiki)

Code: Select all

# Auto Generated Configuration
# Configuration: ./configure --arch=x86_64

# Target Option
########################
ARCH    =x86_64
TARGET  =x86_64-elf


# Path
########################
PATH_MOUNT=/mnt/bluemoon/
PATH_ROOT =../../../
PATH_INC  =$(PATH_ROOT)usr/include/
PATH_BIN  =$(PATH_ROOT)usr/bin/$(ARCH)/
PATH_LIB  =$(PATH_ROOT)usr/lib/$(ARCH)/
PATH_LIBC =$(PATH_ROOT)usr/libc/$(TARGET)/
IMAGE_FILE=$(PATH_ROOT)test/bluemoon.img


# Tools
########################
NASM    =nasm -felf64
CC      =$(TARGET)-gcc
CXX     =$(TARGET)-g++
LD      =$(TARGET)-ld
AR      =$(TARGET)-ar -rcs
OBJCOPY =$(TARGET)-objcopy


# Compiler Flags
########################
INCLUDE =-I$(PATH_INC) -I$(PATH_LIBC)include/ -I$(ARCH)/
CWARN    =-Wall -Wextra -Wattributes -Wbuiltin-macro-redefined -Wcast-align -Wconversion         \
          -Wdiv-by-zero -Wdouble-promotion -Wenum-compare -Wfloat-equal -Winit-self              \
          -Wint-to-pointer-cast -Wlogical-op -Wmissing-braces -Wmissing-field-initializers       \
          -Woverflow -Wpointer-arith -Wredundant-decls -Wreturn-type -Wshadow -Wsign-compare     \
          -Wtype-limits -Wuninitialized -Wwrite-strings                                          \
          -Wno-unused-parameter -Wno-unused-variable -Wno-multichar -Wno-unused-but-set-variable \
          -Wdeclaration-after-statement -Wimplicit-int -Wjump-misses-init -Wpointer-sign         \
          -Wpointer-to-int-cast -Wmissing-parameter-type
CXXWARN  =-Wall -Wextra -Wattributes -Wbuiltin-macro-redefined -Wcast-align -Wconversion         \
          -Wdiv-by-zero -Wdouble-promotion -Wenum-compare -Wfloat-equal -Winit-self              \
          -Wint-to-pointer-cast -Wlogical-op -Wmissing-braces -Wmissing-field-initializers       \
          -Woverflow -Wpointer-arith -Wredundant-decls -Wreturn-type -Wshadow -Wsign-compare     \
          -Wtype-limits -Wuninitialized -Wwrite-strings                                          \
          -Wno-unused-parameter -Wno-unused-variable -Wno-multichar -Wno-unused-but-set-variable \
          -Wc++0x-compat  -Wsign-promo
CFLAGS   =-ffreestanding -masm=intel -std=c99 -O2 -mcmodel=kernel -mno-red-zone \
          -mno-mmx -mno-sse -mno-sse2 -mno-sse3 -mno-3dnow -c -D_$(ARCH) $(INCLUDE) -Werror $(CWARN)
CXXFLAGS =-ffreestanding -masm=intel -O2 -mcmodel=kernel -mno-red-zone \
          -mno-mmx -mno-sse -mno-sse2 -mno-sse3 -mno-3dnow \
          -fno-exceptions -fno-rtti -fno-stack-protector -c -D_$(ARCH) $(INCLUDE) -Werror $(CXXWARN)
LDFLAGS  =-nostdinc -nostdlib 


# Phony
########################
.PHONY: all clean kernel install
all: kernel


##############################################################
#
# KERNEL
#
##############################################################
KERNEL_TMP      :=.build/$(ARCH)/
KERNEL_C        :=$(wildcard *.c)
KERNEL_CPP      :=$(wildcard *.cpp)
KERNEL_ARCH_ASM :=$(wildcard $(ARCH)/*.asm)
KERNEL_ARCH_C   :=$(wildcard $(ARCH)/*.c)
KERNEL_ARCH_CPP :=$(wildcard $(ARCH)/*.cpp)
KERNEL_OBJ      :=$(patsubst %.c,    $(KERNEL_TMP)%_c.o,  $(KERNEL_C))     \
                  $(patsubst %.cpp,  $(KERNEL_TMP)%_cpp.o,$(KERNEL_CPP))   \
                  $(patsubst $(ARCH)/%.asm,  $(KERNEL_TMP)$(ARCH)-%_a.o,  $(KERNEL_ARCH_ASM))  \
                  $(patsubst $(ARCH)/%.c,    $(KERNEL_TMP)$(ARCH)-%_c.o,  $(KERNEL_ARCH_C))    \
                  $(patsubst $(ARCH)/%.cpp,  $(KERNEL_TMP)$(ARCH)-%_cpp.o,$(KERNEL_ARCH_CPP))
KERNEL_DEP    :=$(patsubst %.o,%.d,$(KERNEL_OBJ))
KERNEL_BIN    :=$(PATH_BIN)kernel64.bin
KERNEL_SYM    :=$(PATH_BIN)kernel64.sym
KERNEL_USELIB :=$(PATH_LIBC)lib/libc.a $(PATH_LIB)libelf.a

-include $(KERNEL_DEP)

kernel: $(KERNEL_BIN)
$(KERNEL_BIN): $(KERNEL_TMP) $(KERNEL_OBJ) $(KERNEL_USELIB) Makefile
        @echo [LINK] $@
        @$(LD) $(LDFLAGS) -T$(ARCH)/kernel.ld -o $@ $(KERNEL_OBJ) $(KERNEL_USELIB)
        @$(OBJCOPY) --only-keep-debug $@ $(KERNEL_SYM)
        @$(OBJCOPY) --strip-debug --strip-unneeded $@

$(KERNEL_TMP):
        @mkdir -p $@
$(KERNEL_TMP)$(ARCH)-%_a.o: $(ARCH)/%.asm Makefile
        @echo [NASM] $<
        @$(NASM) -I$(ARCH)/ -MD $(KERNEL_TMP)$(ARCH)-$*_a.d -MP $< -o $@
$(KERNEL_TMP)$(ARCH)-%_c.o: $(ARCH)/%.c Makefile
        @echo [GCC ] $<
        @$(CC) $(CFLAGS) -I./ -MD -MP $< -o $@
$(KERNEL_TMP)$(ARCH)-%_cpp.o: $(ARCH)/%.cpp Makefile
        @echo [CXX ] $<
        @$(CXX) $(CXXFLAGS) -I./ -MD -MP $< -o $@
$(KERNEL_TMP)%_a.o: %.asm Makefile
        @echo [NASM] $<
        @$(NASM) -MD $(KERNEL_TMP)$*_$(TARGET)_a.d -MP $< -o $@
$(KERNEL_TMP)%_c.o: %.c Makefile
        @echo [GCC ] $<
        @$(CC) $(CFLAGS) -MD -MP $< -o $@
$(KERNEL_TMP)%_cpp.o: %.cpp Makefile
        @echo [CXX ] $<
        @$(CXX) $(CXXFLAGS) -MD -MP $< -o $@


##############################################################
install: kernel
        @echo [COPY] $(KERNEL_BIN) TO $(PATH_MOUNT)
        @cp $(KERNEL_BIN) $(PATH_MOUNT)


##############################################################
clean:
        @rm $(KERNEL_OBJ) $(KERNEL_DEP)

Re: What does your Makefile looks like?

Posted: Thu May 03, 2012 10:10 am
by bubach
omg.
fasm kernel.asm kernel.sys
fasm boot.asm boot.bin
partcopy boot.bin 0 200 -f0
copy kernel.sys a:
c:\.....\bochs.exe
something like that. just the right amount of complicated for me, thank you very much. ;)

Re: What does your Makefile looks like?

Posted: Thu May 03, 2012 7:25 pm
by neon
Dont have a single makefile; rather the design is split into "environment" makefiles and "component" makefiles. I decided this method over scripts to simplify later support for self hosting. The environment makefiles abstracts toolchain independence and targets so component makefiles only need to define specific variables for the component and call the environment makefiles. For example, here is the makefile used by our boot loader. This example illustrates a component makefile and what they look like.

Code: Select all

DBGMSG = DBGMSG_YES
SYSTEM = SYSTEM_NEPTUNE
SUBSYSTEM = SUBSYSTEM_EFI_APP
TARGETNAME = bootldr.efi
SOURCES = boot.c config.c install.c mulboot.c relocate.c chainload.c entry.c menu.c part.c
LIBS = ..\lib\bootlib.lib
ENTRYPOINT = osloader_entry
INCLUDES = ../inc/

!include $(NEPTUNE_ENV)\makefile.def
The environment makefiles are a bit more complicated though and consist of several makefiles that define targets and toolchain specific options. Kind of like what bluemoon posted but (i) across multiple files and (ii) more !if ... !endif cases to watch and set variables for configuration.

Re: What does your Makefile looks like?

Posted: Thu May 03, 2012 8:14 pm
by Rudster816
Not particularly good, but it works.

Main:

Code: Select all

ARCH = amd64
HOSTPLATFORM = cygwin-pc-i686
TARGETPLATFORM = x86_64-elf


SRCDIR = .
INCDIR = $(SRCDIR)/include
OUTDIR = ./../output
OBJDIR = $(OUTDIR)/obj
BINDIR = $(OUTDIR)/bin
ASMDIR = $(OUTDIR)/asm
DEPDIR = $(OUTDIR)/dep

OUTPUTDIRS = $(OUTDIR) $(OBJDIR) $(BINDIR) $(ASMDIR) $(DEPDIR)

STDLIBINCDIR = $(SRCDIR)/lib/include -I$(SRCDIR)/lib/internals
ARCHINCDIR = $(SRCDIR)/arch/$(ARCH)/include
DEFAULTINCDIRS = -I$(STDLIBINCDIR) -I$(ARCHINCDIR) -I$(INCDIR)

TOOLBINDIR = ./../toolchain/$(HOSTPLATFORM)/bin
TOOLPREFIX = $(TOOLBINDIR)/$(TARGETPLATFORM)
CC = $(TOOLPREFIX)-gcc
LD = $(TOOLPREFIX)-ld
AR = $(TOOLPREFIX)-ar
DD = dd
AS = $(TOOLBINDIR)/nasm
RM = rm
CP = cp
MCOPY = $(TOOLBINDIR)/mcopy
RANLIB = $(TOOLPREFIX)-ranlib

include boot/Makefile
include kernel/Makefile
include lib/Makefile

.PHONY : image clean-image

image : $(BINDIR)/wnos-hdd.img
clean-image : 
	$(RM) -f $(BINDIR)/wnos-hdd.img

$(BINDIR)/wnos-hdd.img : $(BINDIR)/kernel.elf64 $(BINDIR)/fat32mbr.bin $(BINDIR)/ezloader.bin
	@$(CP) $(SRCDIR)/template.img $(BINDIR)/wnos-hdd.img
	@echo CP $(BINDIR)/wnos-hdd.img
	@$(MCOPY) -i $(BINDIR)/wnos-hdd.img $(BINDIR)/ezloader.bin ::ezloader.bin
	@echo MCOPY $(BINDIR)/ezloader.bin
	@$(MCOPY) -i $(BINDIR)/wnos-hdd.img $(BINDIR)/kernel.elf64 ::kernel.elf64
	@echo MCOPY $(BINDIR)/kernel.elf64
	@$(DD) if=$(BINDIR)/wnos-hdd.img ibs=8192k of=$(BINDIR)/wnos-hdd-padded.img conv=sync 2> /dev/null
	@echo DD $(BINDIR)/wnos-hdd-padded.img


.PHONY : all clean
all : kernel boot lib image

clean : clean-boot clean-kernel clean-lib clean-image

$(OUTPUTDIRS) : % :
	mkdir $@
Example of a 'child' Makefile (kernel/Makefile)

Code: Select all

.PHONY : kernel clean-kernel

kernel : $(BINDIR)/kernel.elf64
clean-kernel :
	rm -f $(BINDIR)/kernel.elf64
	rm -f $(OBJDIR)/kernel/*.o
	rm -f $(OBJDIR)/kernel/*/*.o
	rm -f $(ASMDIR)/kernel/*.s
	rm -f $(ASMDIR)/kernel/*/*.s
	rm -f $(DEPDIR)/kernel/*.d
	rm -f $(DEPDIR)/kernel/*/*.d

KERNEL_WARNINGS = -Wall -Wextra
KERNEL_CCFLAGS = -O2 -g $(KERNEL_WARNINGS) -m64 -std=c99 $(DEFAULTINCDIRS) -nostdlib -ffreestanding -mcmodel=large -mno-red-zone -mno-mmx -mno-sse -mno-sse2 -mno-sse3 -mno-3dnow

KERNEL_SRCROOT = $(SRCDIR)/kernel
KERNEL_SRCSUBS = mm apic acpi
KERNEL_SRCDIRS = $(KERNEL_SRCROOT) $(foreach SUBDIR,$(KERNEL_SRCSUBS),$(KERNEL_SRCROOT)/$(SUBDIR))
KERNEL_OUTDIRS = $(ASMDIR)/kernel $(DEPDIR)/kernel $(OBJDIR)/kernel
KERNEL_OUTDIRS += $(foreach SUBDIR,$(KERNEL_SRCSUBS),$(ASMDIR)/kernel/$(SUBDIR) $(DEPDIR)/kernel/$(SUBDIR) $(OBJDIR)/kernel/$(SUBDIR))

KERNEL_CC_OBJS = $(patsubst %.o, $(OBJDIR)/kernel/%.o, $(patsubst %.c, %.o, $(notdir $(wildcard $(SRCDIR)/kernel/*.c))))
KERNEL_CC_OBJS += $(foreach SUBDIR,$(KERNEL_SRCSUBS),$(patsubst %.o, $(OBJDIR)/kernel/$(SUBDIR)/%.o, $(patsubst %.c, %.o, $(notdir $(wildcard $(SRCDIR)/kernel/$(SUBDIR)/*.c)))))

KERNEL_AS_OBJS = $(patsubst %.o, $(OBJDIR)/kernel/%.o, $(patsubst %.asm, %.o, $(notdir $(wildcard $(SRCDIR)/kernel/*.asm))))
KERNEL_AS_OBJS += $(foreach SUBDIR,$(KERNEL_SRCSUBS),$(patsubst %.o, $(OBJDIR)/kernel/$(SUBDIR)/%.o, $(patsubst %.asm, %.o, $(notdir $(wildcard $(SRCDIR)/kernel/$(SUBDIR)/*.asm)))))

KERNEL_DEPS = $(patsubst %.d, $(DEPDIR)/kernel/%.d, $(patsubst %.c, %.d, $(notdir $(wildcard $(SRCDIR)/kernel/*.c))))
KERNEL_DEPS += $(foreach SUBDIR,$(KERNEL_SRCSUBS),$(patsubst %.d, $(DEPDIR)/kernel/$(SUBDIR)/%.d, $(patsubst %.c, %.d, $(notdir $(wildcard $(SRCDIR)/kernel/$(SUBDIR)/*.c)))))

$(KERNEL_OUTDIRS) : % :
	mkdir $@

$(KERNEL_DEPS) : $(DEPDIR)/%.d : $(SRCDIR)/%.c | $(OUTPUTDIRS) $(KERNEL_OUTDIRS)
	@$(CC) $(KERNEL_CCFLAGS) -c $< -MM -MT $(OBJDIR)/$(patsubst %.c,%.o, $<) -o $@
	@echo CC $@

-include $(KERNEL_DEPS)

$(OBJDIR)/kernel/bootstrap.o : $(BINDIR)/trampoline.bin

$(KERNEL_CC_OBJS): $(OBJDIR)/%.o: %.c | $(OUTPUTDIRS) $(KERNEL_OUTDIRS)
	@$(CC) $(KERNEL_CCFLAGS) -c $< -o $@ -Wa,-aln=$(ASMDIR)/$(patsubst %.c,%.s, $<)
	@echo CC $@

$(KERNEL_AS_OBJS): $(OBJDIR)/%.o: $(SRCDIR)/%.asm
	@$(AS) -f elf64 -o $@ $<
	@echo NASM $@

$(BINDIR)/kernel.elf64 : $(KERNEL_AS_OBJS) $(KERNEL_CC_OBJS) $(OBJDIR)/pdclib.a $(SRCDIR)/kernel/linker.ld
	@$(LD) -T $(SRCDIR)/kernel/linker.ld -z max-page-size=0x1000 -o $(BINDIR)/kernel.elf64 $(KERNEL_AS_OBJS) $(KERNEL_CC_OBJS) $(OBJDIR)/pdclib.a
	@echo LD $@

Re: What does your Makefile looks like?

Posted: Thu May 03, 2012 8:45 pm
by NickJohnson
The makefile for Rhombus is kind of meh, but here's something I put together for a new project. It can download/build/install the whole cross-toolchain, rebuild the guest OS directory hierarchy, uses separate build and source directories, and is completely non-recursive.

Code: Select all

.PHONY: make-system clean tools clean-tools

CC := ./tools/bin/i586-elf-gcc
LD := ./tools/bin/i586-elf-ld
AS := ./tools/bin/nasm

CFLAGS  := -std=c99 -pedantic -Wall -Wextra -MMD -Werror
CFLAGS  += -O3 -fomit-frame-pointer
CFLAGS	+= -mno-sse -mno-mmx -mno-sse2 -mno-sse3
CFLAGS  += -I src/include
ASFLAGS := -felf32

all: make-all

##############################################################################
#
# Set up cross-compiler and build environment
#
##############################################################################

tools: clean-tools
	@ echo ""
	@ echo "Starting automatic cross-toolchain build. This may take a while depending on"
	@ echo "your Internet connection and processor speed. I would take this opportunity to"
	@ echo "mix up a batch of muffins if I were you."
	@ echo ""
	@ echo "The cross-toolchain will be completely contained within the tools/ directory. No"
	@ echo "superuser access is required."
	@ echo ""
	@ mkdir -p tools
	@ echo " WGET	tools/binutils-2.22.tar.bz2"
	@ wget -P tools http://ftp.gnu.org/gnu/binutils/binutils-2.22.tar.bz2
	@ echo " WGET	tools/gcc-4.7.0.tar.bz2"
	@ wget -P tools http://ftp.gnu.org/gnu/gcc/gcc-4.7.0/gcc-4.7.0.tar.bz2
	@ echo " WGET	tools/nasm-2.09.10.tar.bz2"
	@ wget -P tools http://www.nasm.us/pub/nasm/releasebuilds/2.09.10/nasm-2.09.10.tar.bz2
	@ echo " UNTAR	tools/binutils-2.22.tar.bz2"
	@ tar -xf tools/binutils-2.22.tar.bz2 -C tools
	@ rm tools/binutils-2.22.tar.bz2
	@ echo " UNTAR	tools/gcc-4.7.0.tar.bz2"
	@ tar -xf tools/gcc-4.7.0.tar.bz2 -C tools
	@ rm tools/gcc-4.7.0.tar.bz2
	@ echo " UNTAR	tools/nasm-2.09.10.tar.bz2"
	@ tar -xf tools/nasm-2.09.10.tar.bz2 -C tools
	@ rm tools/nasm-2.09.10.tar.bz2
	@ mkdir -p tools/build-binutils
	@ echo ""
	@ echo " CONFIGURING BINUTILS"
	@ echo ""
	@ cd tools/build-binutils && ../binutils-2.22/configure \
		--target=i586-elf --prefix=$(PWD)/tools --disable-nls
	@ echo ""
	@ echo " COMPILING BINUTILS"
	@ echo ""
	@ make -C tools/build-binutils all
	@ echo ""
	@ echo " INSTALLING BINUTILS"
	@ echo ""
	@ make -C tools/build-binutils install
	@ echo ""
	@ echo " CLEAN	tools/build-binutils tools/binutils-2.22"
	@ rm -rf tools/build-binutils tools/binutils-2.22
	@ mkdir -p tools/build-gcc
	@ echo ""
	@ echo " CONFIGURING GCC"
	@ echo ""
	@ export PATH=$PATH:$(PWD)/tools/bin
	@ cd tools/build-gcc && ../gcc-4.7.0/configure \
		--target=i586-elf --prefix=$(PWD)/tools --disable-nls \
		--enable-languages=c --without-headers
	@ echo ""
	@ echo " COMPILING GCC"
	@ echo ""
	@ make -C tools/build-gcc all-gcc
	@ echo ""
	@ echo " INSTALLING GCC"
	@ echo ""
	@ make -C tools/build-gcc install-gcc
	@ echo ""
	@ echo " CLEAN	tools/build-gcc tools/gcc-4.7.0"
	@ rm -rf tools/build-gcc tools/gcc-4.7.0
	@ echo ""
	@ echo " CONFIGURING NASM"
	@ echo ""
	@ cd tools/nasm-2.09.10 && ./configure --prefix=$(PWD)/tools
	@ echo ""
	@ echo " COMPILING NASM"
	@ echo ""
	@ make -C tools/nasm-2.09.10
	@ echo ""
	@ echo " INSTALLING NASM"
	@ echo ""
	@ make -C tools/nasm-2.09.10 install
	@ echo ""
	@ echo " CLEAN	tools/nasm-2.09.10"
	@ rm -rf tools/nasm-2.09.10
	
clean-tools:
	@ echo " CLEAN	tools/"
	@ - rm -r tools

##############################################################################
#
# Build components and install them to the system root
#
##############################################################################

# build and install all packages into root/
make-all: skeleton make-kernel make-system

.PHONY: skeleton make-kernel

# re-initialize root/ with a blank skeleton structure
skeleton:
	@ echo " CLEAN	root/"
	@ - rm -r root
	@ echo " CLONE	skel/ -> root/"
	@ cp -r skel root

######################################
#
# Kernel
#
######################################

make-kernel: build-kernel install-kernel

.PHONY: build-kernel install-kernel

build-kernel: build/kernel/kernel

kernel_OBJECTS := $(patsubst src/%.s,build/%.o,$(shell find src/kernel -name "*.s"))
kernel_OBJECTS += $(patsubst src/%.c,build/%.o,$(shell find src/kernel -name "*.c"))

build/kernel/kernel: $(kernel_OBJECTS) src/kernel/kernel.ld
	@ echo " LD	"$@ $(kernel_OBJECTS)
	@ $(LD) -o $@ $(kernel_OBJECTS) -Tsrc/kernel/kernel.ld

build/kernel/%.o: src/kernel/%.s
	@ mkdir -p `dirname $@`
	@ echo " AS	"$<
	@ $(AS) $(ASFLAGS) $< -o $@

build/kernel/%.o: src/kernel/%.c
	@ mkdir -p `dirname $@`
	@ echo " CC	"$<
	@ $(CC) $(CFLAGS) -ffreestanding -c $< -o $@

install-kernel: build-kernel
	@ echo " CP	"build/kernel/kernel -> root/boot/kernel
	@ cp build/kernel/kernel root/boot/kernel

-include $(patsubst %.o,%.d,$(kernel_OBJECTS))

######################################
#
# System / Driver Layer
#
######################################

make-system: build-system install-system

.PHONY: build-system install-system

build-system: build/system/system

system_OBJECTS := $(patsubst src/%.s,build/%.o,$(shell find src/system -name "*.s"))
system_OBJECTS += $(patsubst src/%.c,build/%.o,$(shell find src/system -name "*.c"))

build/system/system: $(system_OBJECTS) src/system/system.ld
	@ echo " LD	"$@ $(system_OBJECTS)
	@ $(LD) -o $@ $(system_OBJECTS) -Tsrc/system/system.ld

build/system/%.o: src/system/%.s
	@ mkdir -p `dirname $@`
	@ echo " AS	"$<
	@ $(AS) $(ASFLAGS) $< -o $@

build/system/%.o: src/system/%.c
	@ mkdir -p `dirname $@`
	@ echo " CC	"$<
	@ $(CC) $(CFLAGS) -Isrc/system/include -ffreestanding -c $< -o $@

install-system: build-system
	@ echo " CP	"build/system/system -> root/boot/system
	@ cp build/system/system root/boot/system

-include $(patsubst %.o,%.d,$(system_OBJECTS))

##############################################################################
#
# Create system images
#
##############################################################################

# create a new CD image in images/ from the contents of root/
cd-image: make-all
	@ mkdir -p images/
	@ echo " IMAGE	images/pinion.iso"
	@ genisoimage -R -b boot/grub/stage2_eltorito -no-emul-boot \
		-quiet -boot-load-size 4 -boot-info-table \
		-o images/pinion.iso root

# run the emulator to test the system
test: cd-image
	@ echo " TEST	"
	@ env test/run.sh

##############################################################################
#
# Clean up object files
#
##############################################################################

clean:
	@ echo " CLEAN	build/"
	@ - rm -r build

Re: What does your Makefile looks like?

Posted: Mon May 07, 2012 1:57 am
by Solar
Makefile tutorial.

Makefile tutorial, extended / unfinished.

PDCLib's Makefile (somewhat trivial).

And, to show that you can do very different things with a Makefile too, the one from my current (LaTeX related) project. Also quite trivial, I think... except that it uses a neat little trick in latexmk to automatically create LaTeX source from data files with the help of a couple of Perl scripts to satisfy \include statements in the LaTeX documents...

Code: Select all

.PHONY: all clean gm show showgm todolist outdir/Directors_Cut.pdf

TEXFILES:=$(shell find . -name \*.tex)

all: outdir/Directors_Cut.pdf outdir/Qabbals.pdf

clean:
	latexmk -outdir=outdir -CA
	@$(RM) outdir/*

outdir/%.tex: rawfiles/%.raw
	@rawfiles/raw2tex $< > $@

outdir/%.tex: rawfiles/%.rawc
	@rawfiles/rawc2tex $< > $@

outdir/%.tex: rawfiles/%.rawt
	@rawfiles/rawt2tex $< > $@

outdir/Directors_Cut.pdf:
	@latexmk -outdir=outdir -pdf -pdflatex="pdflatex -interaction=nonstopmode" -use-make Directors_Cut.tex 2>&1 | tee outdir/Directors_Cut.mklog

outdir/Qabbals.pdf:
	@latexmk -outdir=outdir -pdf -pdflatex="pdflatex -interaction=nonstopmode" Qabbals.tex 2>&1 | tee outdir/Qabbals.mklog

gm:
	cd Gamemaster_Law && $(MAKE) -f ../Makefile Gamemaster_Law.pdf

show: outdir/Directors_Cut.pdf
	@evince $< &

showq: outdir/Qabals.pdf
	@evince $< &

showgm: gmlaw
	@evince Gamemaster_Law/Gamemaster_Law.pdf &

todolist:
	@grep TODO *.tex

ref.tex: SL_Ch1[234]_[123]_*.tex
	@cat $^ > $@

Re: What does your Makefile looks like?

Posted: Mon May 07, 2012 7:57 am
by darkinsanity
Well, my Makefile is pretty simple and works, but I'm sure it could be done better ;)

Code: Select all

SRCS = $(shell find src -name *.bas)
SRCS += $(shell find src -name *.asm)
OBJS = $(addprefix obj/, $(addsuffix .o,$(basename $(notdir $(SRCS)))))

COMPILER = fbc
ASSEMBLER = nasm
LINKER = ld

CFLAGS = -c -nodeflibs -lang fb -arch 486 -i ./include/
AFLAGS = -f elf32
LFLAGS = -melf_i386 -Tkernel.ld

frost.krn: $(OBJS)
	$(LINKER) $(LFLAGS) -o $@ $^

obj/%.o: src/%.bas
	$(COMPILER) $(CFLAGS) $^ -o $@

obj/%.o: src/%.asm
	$(ASSEMBLER) $(AFLAGS) $^ -o $@

clean:
	rm $(OBJS) frost.krn

.PHONY: clean

Re: What does your Makefile looks like?

Posted: Tue May 08, 2012 1:34 pm
by rdos
I don't use makefiles. I have a number of OpenWatcom IDE-files that builds the device-drivers, classlibrary interface and applications. The only thing that is built with makefiles is clib, but this is integrated with OpenWatcom's build system.

I find it far easier to maintain IDE project files than traditional makefiles.

BTW, I can even build the bootloader and multiboot stub with the IDE, so those don't require makefiles either.

Re: What does your Makefile looks like?

Posted: Tue May 08, 2012 1:52 pm
by bluemoon
rdos wrote:I find it far easier to maintain IDE project files than traditional makefiles.
I agree. My kernel's Makefile alone even has more code than my boot sector. :shock:

But then, I think it worth the effort as the make system is really powerful for many automations. Once I have written the Makefile I don't even need to touch it even I add more source files into the project - make can scan directories and take care of dependency according to my rule - my Makefile even create per architecture temp/.obj directory if it does not exists, so gcc won't complain with write error.

Re: What does your Makefile looks like?

Posted: Wed May 09, 2012 5:56 am
by rdos
I created specific targets in the IDE for boot sector, GRUB stub and 2:d stage bootloader. That way I didn't need to run those with makefiles. Besides, the IDE creates makefiles automatically when building a project. It's just that I don't need to deal with making them myself. I also created special executable formats in the linker. :wink:

Re: What does your Makefile looks like?

Posted: Wed May 09, 2012 7:11 am
by Solar
rdos wrote:Besides, the IDE creates makefiles automatically when building a project. It's just that I don't need to deal with making them myself.
All auto-generated Makefiles I've seen so far suck royally, and usually don't even get dependencies quite right. That is especially true for Makefiles generated by automake. I hope those generated by Watcom are better...

Re: What does your Makefile looks like?

Posted: Wed May 09, 2012 8:47 am
by rdos
Solar wrote:
rdos wrote:Besides, the IDE creates makefiles automatically when building a project. It's just that I don't need to deal with making them myself.
All auto-generated Makefiles I've seen so far suck royally, and usually don't even get dependencies quite right. That is especially true for Makefiles generated by automake. I hope those generated by Watcom are better...
Apart from non-obvious dependencies like a recompiled clib, I've had no issues with dependencies. The automatic dependeny handling seems to work well.

Re: What does your Makefile looks like?

Posted: Wed May 09, 2012 8:53 am
by bluemoon
Solar wrote:All auto-generated Makefiles I've seen so far suck royally, and usually don't even get dependencies quite right.
This remind me of unpleasant experience of using visual studio. From time to time a weird bug will pop up, and you spent a day on it and the bug just don't make sense, then realize VC has messed up the precompiled header/dependency checking, and you need to "clean rebuild" the project.

Re: What does your Makefile looks like?

Posted: Wed May 09, 2012 11:10 am
by klange

Code: Select all

# ToAruOS Primary Build Script
# This script will pull either clang (with -fcolor-diagnostics), gcc (with no extra options), or cc
CC = `util/compiler`
# Sometimes we just have to use GCC
GCC = gcc
# CFLAGS for core components
CFLAGS = -Wall -Wextra -pedantic -m32 -O0 -std=c99 -finline-functions -fno-stack-protector -nostdinc -ffreestanding -Wno-unused-function -Wno-unused-parameter -g
# CFLAGS for native utils
NATIVEFLAGS = -std=c99 -g -pedantic -Wall -Wextra -Wno-unused-parameter
# Linker for core
LD = ld -m elf_i386
YASM = yasm
# Feel free to be specific, but I'd rather you not be.
FILESYSTEMS  = $(patsubst %.c,%.o,$(wildcard kernel/fs/*.c))
VIDEODRIVERS = $(patsubst %.c,%.o,$(wildcard kernel/video/*.c))
DEVICES      = $(patsubst %.c,%.o,$(wildcard kernel/devices/*.c))
VIRTUALMEM   = $(patsubst %.c,%.o,$(wildcard kernel/mem/*.c))
MISCMODS     = $(patsubst %.c,%.o,$(wildcard kernel/misc/*.c))
SYSTEM       = $(patsubst %.c,%.o,$(wildcard kernel/sys/*.c))
DATASTRUCTS  = $(patsubst %.c,%.o,$(wildcard kernel/ds/*.c))
CPUBITS      = $(patsubst %.c,%.o,$(wildcard kernel/cpu/*.c))
REALEMU      = $(patsubst %.c,%.o,$(wildcard kernel/v8086/*.c))

SUBMODULES = ${MODULES} ${FILESYSTEMS} ${VIDEODRIVERS} ${DEVICES} ${VIRTUALMEM} ${MISCMODS} ${SYSTEM} ${DATASTRUCTS} ${CPUBITS} ${REALEMU}

UTILITIES = util/bin/readelf util/bin/typewriter
EMU = qemu
GENEXT = genext2fs
DD = dd conv=notrunc
BEG = util/mk-beg
END = util/mk-end
INFO = util/mk-info
ERRORS = 2>>/tmp/.`whoami`-build-errors || util/mk-error
ERRORSS = >>/tmp/.`whoami`-build-errors || util/mk-error

BEGRM = util/mk-beg-rm
ENDRM = util/mk-end-rm

EMUARGS     = -kernel toaruos-kernel -m 256 -serial stdio -vga std -hda toaruos-disk.img -k en-us -no-frame
EMUKVM      = -enable-kvm

H=hdd/bin
INITRDBIN = $H/reboot $H/nyancat $H/clear $H/cat $H/terminal $H/esh $H/echo $H/init $H/login $H/ls $H/uname $H/fire $H/donut $H/whoami $H/yes

.PHONY: all system clean clean-once clean-hard clean-soft clean-docs clean-bin clean-aux clean-core update-version install run docs utils
.SECONDARY: 

all: .passed system docs utils tags
system: .passed toaruos-disk.img toaruos-kernel

install: system
	@${BEG} "CP" "Installing to /boot..."
	@cp toaruos-kernel /boot/toaruos-kernel
	@cp toaruos-initrd /boot/toaruos-initrd
	@${END} "CP" "Installed to /boot"

# Various different quick options
run: system
	${EMU} ${EMUARGS} -append "vid=qemu hdd"
kvm: system
	${EMU} ${EMUARGS} ${EMUKVM} -append "vid=qemu hdd"
vga: system
	${EMU} ${EMUARGS} -append "vgaterm hdd"
vga-kvm: system
	${EMU} ${EMUARGS} ${EMUKVM} -append "vgaterm hdd"
term: system
	${EMU} ${EMUARGS} -append "vid=qemu single hdd"
term-kvm: system
	${EMU} ${EMUARGS} ${EMUKVM} -append "vid=qemu single hdd"

utils: ${UTILITIES}

.passed:
	@util/check-reqs > /dev/null
	@touch .passed

#################
# Documentation #
#################
docs: docs/core.pdf

docs/core.pdf: docs/*.tex
	@${BEG} "docs" "Generating documentation..."
	@pdflatex -draftmode -halt-on-error -output-directory docs/ docs/core.tex > /dev/null ${ERRORS}
	@makeindex -q docs/*.idx ${ERRORS}
	@pdflatex -halt-on-error -output-directory docs/ docs/core.tex > /dev/null ${ERRORS}
	@${END} "docs" "Generated documentation"

################
#    Kernel    #
################
toaruos-kernel: kernel/start.o kernel/link.ld kernel/main.o ${SUBMODULES} .passed
	@${BEG} "LD" "$<"
	@${LD} -T kernel/link.ld -o toaruos-kernel kernel/*.o ${SUBMODULES} ${ERRORS}
	@${END} "LD" "$<"
	@${INFO} "--" "Kernel is ready!"

kernel/start.o: kernel/start.s
	@${BEG} "yasm" "$<"
	@${YASM} -f elf -o kernel/start.o kernel/start.s ${ERRORS}
	@${END} "yasm" "$<"

kernel/sys/version.o: kernel/*/*.c kernel/*.c

%.o: %.c
	@${BEG} "CC" "$<"
	@${CC} ${CFLAGS} -I./kernel/include -c -o $@ $< ${ERRORS}
	@${END} "CC" "$<"

hdd/bin/%:
	@cd userspace; make ../$@

################
#   Ram disk   #
################
toaruos-initrd: .passed ${INITRDBIN}
	@${BEG} "initrd" "Generating initial RAM disk"
	@# Get rid of the old one
	@-rm -f toaruos-initrd
	@-rm -f initrd/bin/*
	@cp ${INITRDBIN} initrd/bin/
	@${GENEXT} -d initrd -q -b 4096 toaruos-initrd ${ERRORS}
	@${END} "initrd" "Generated initial RAM disk"
	@${INFO} "--" "Ramdisk image is ready!"

####################
# Hard Disk Images #
####################

# TODO: Install Grub to one of these by pulling newest grub builds
#       from the Grub2 website.

hdd:
	@mkdir hdd

toaruos-disk.img: hdd userspace/*.c
	@${BEG} "hdd" "Generating a Hard Disk image..."
	@-rm -f toaruos-disk.img
	@${GENEXT} -d hdd -q -b 131072 -N 4096 toaruos-disk.img ${ERRORS}
	@${END} "hdd" "Generated Hard Disk image"
	@${INFO} "--" "Hard disk image is ready!"

################
#   Utilities  #
################

util/bin/%: util/%.c
	@${BEG} "CC" "$<"
	@${CC} ${NATIVEFLAGS} -o $@ $< ${ERRORS}
	@${END} "CC" "$<"

################
#   Userspace  #
################
loader/crtbegin.o: loader/crtbegin.s
	@${BEG} "yasm" "$<"
	@${YASM} -f elf32 -o $@ $< ${ERRORS}
	@${END} "yasm" "$<"

hdd/bin/%: loader/%.o loader/crtbegin.o loader/syscall.o
	@${BEG} "LD" "$<"
	@${LD} -T loader/link.ld -s -S -o $@ $< ${ERRORS}
	@${END} "LD" "$<"

##############
#    ctags   #
##############
tags: kernel/*/*.c kernel/*.c userspace/*.c
	@${BEG} "ctag" "Generating CTags..."
	@ctags -R --c++-kinds=+p --fields=+iaS --extra=+q
	@${END} "ctag" "Generated CTags."

###############
#    clean    #
###############

clean-soft:
	@${BEGRM} "RM" "Cleaning modules..."
	@-rm -f kernel/*.o
	@-rm -f ${SUBMODULES}
	@${ENDRM} "RM" "Cleaned modules."

clean-docs:
	@${BEGRM} "RM" "Cleaning documentation..."
	@-rm -f docs/*.pdf docs/*.aux docs/*.log docs/*.out
	@-rm -f docs/*.idx docs/*.ind docs/*.toc docs/*.ilg
	@${ENDRM} "RM" "Cleaned documentation"

clean-bin:
	@${BEGRM} "RM" "Cleaning native binaries..."
	@-rm -f hdd/bin/*
	@${ENDRM} "RM" "Cleaned native binaries"

clean-aux:
	@${BEGRM} "RM" "Cleaning auxillary files..."
	@-rm -f loader/*.o
	@-rm -f util/bin/*
	@${ENDRM} "RM" "Cleaned auxillary files"

clean-core:
	@${BEGRM} "RM" "Cleaning final output..."
	@-rm -f toaruos-kernel
	@-rm -f toaruos-initrd
	@${ENDRM} "RM" "Cleaned final output"

clean: clean-soft clean-core
	@${INFO} "--" "Finished soft cleaning"

clean-hard: clean clean-bin clean-aux clean-docs
	@${INFO} "--" "Finished hard cleaning"

clean-disk:
	@${BEGRM} "RM" "Deleting hard disk image..."
	@-rm -f toaruos-disk.img
	@${ENDRM} "RM" "Deleted hard disk image"

clean-once:
	@${BEGRM} "RM" "Cleaning one-time files..."
	@-rm -f .passed
	@${ENDRM} "RM" "Cleaned one-time files"

# vim:noexpandtab
# vim:tabstop=4
# vim:shiftwidth=4

Re: What does your Makefile looks like?

Posted: Wed May 09, 2012 1:54 pm
by darkinsanity
Hm, maybe I'd use an IDE if there was one that offers me the flexibility needed. But so far I've not seen a good FreeBASIC IDE for Linux that allows me to compile my kernel in a comfortable way.
My other projects (written in C++) are being compiled by an IDE: Code::Blocks, my favorite IDE for Windows and Linux. I really like the combination of CB with gcc/MinGW and I've never had any problems with it.