Page 1 of 1

OS Makefile

Posted: Fri Nov 12, 2010 4:20 pm
by cbrowne
New to OS development but thought I'd share my Makefile as it has already proven itself invaluable to the process:

Code: Select all

CC=gcc

OSNAME=YourNameHere

default: test

# bootfloppy.img is a testing image with JUST the bootloader
bootfloppy.img: obj/boot.bin
	dd if=obj/boot.bin bs=512 of=bootfloppy.img

obj:
	mkdir obj

iso:
	mkdir iso

$(OSNAME).iso: iso/$(OSNAME).bin
	mkisofs -o $(OSNAME).iso -b $(OSNAME).bin -boot-load-size 4 -no-emul-boot ./iso/

obj/kernel.bin: src/kernel.c obj
	$(CC) -c src/kernel.c -o obj/kernel.o
	ld obj/kernel.o -o obj/kernel.bin --oformat=binary -Ttext=0x100000

obj/boot.bin: src/boot.s obj
	nasm src/boot.s -f bin -o obj/boot.bin

iso/$(OSNAME).bin: obj/boot.bin obj/kernel.bin iso
	cat obj/boot.bin obj/kernel.bin > iso/$(OSNAME).bin

libc.o:
	make -C src/libc libc.o

clean:
	@echo -n removing obj directory...
	@rm -rf obj
	@echo done

bin-clean:
	@make clean # remove everything clean removes
	@echo -n removing boot images...
	@rm -f $(OSNAME).iso # delete boot CD
	@rm -f bootfloppy.img # delete bootable floppy
	@echo done

test: bootfloppy.img
	qemu -fda bootfloppy.img -boot order=a
testcd: $(OSNAME).iso
	qemu -cdrom $(OSNAME).iso -boot order=d
Sure, it's not the prettiest thing in the world, and I've used some unconventional names, and there are a thousand other holes that The Internet will love to pick in it, but if it is useful to just one person other than me then sharing it (and all the flames I get whenever I share anything on the world wide web) was worthwhile.

Usage:
"make" on its own does the same as "make test"
"make test" will create and run a bootable floppy disk
"make bootfloppy.img" will create a bootable floppy disk
"make testcd" will create and run a bootable ISO
"make YourNameHere.iso" will create a bootable ISO

It's fairly obvious from a simple dissection what the source naming convention is, and it wouldn't be too much effort for somebody to abstract this naming convention into Makefile variables (eg SRCDIR=src/, etc.).

Oh yeah and it should also be fairly obvious that it's aimed at a Linux environment with qemu installed. Again, it shouldn't take -too- much effort to abstract the linux-specific stuff nor the qemu stuff. I just tend to err on the side of brevity rather than concision/portability.

Have fun hacking it, anyway.

License: Public Domain

Re: OS Makefile

Posted: Fri Nov 12, 2010 4:50 pm
by gravaera
Hi:

While the effort to be useful and edifying is very nice, the problem is that writing a Makefile is not something specific to kernel development. Make is a very old Unix tool, used in software development projects of all kinds. Any old timer, or anyone familiar with GNU development, or anyone who just sticks with the "common denominator" for build system maintenance will know Make syntax.

Makefiles are also not something you can copy and paste since different projects have different build requirements including possible conditional compilation of objects. In other words, this is impractical since anyone doing a kernel development project (or any software development project at all, of any kind) should know how to use some kind of build system. Being able to use a build system is no big deal and the skill in itself has nothing inherently to do with kernel development.

--All the best,
gravaera

Re: OS Makefile

Posted: Mon Nov 29, 2010 12:29 pm
by carbonBased
This actually brings up an interesting question (at least to me)...

What type of build environments are devs typically using out there?

I switched from a Makefile based system, to a more extensible ant based system a while back. It's worked out fairly nicely for me, so far.

--Jeff

Re: OS Makefile

Posted: Mon Nov 29, 2010 2:14 pm
by Brynet-Inc
carbonBased wrote:This actually brings up an interesting question (at least to me)...

What type of build environments are devs typically using out there?

I switched from a Makefile based system, to a more extensible ant based system a while back. It's worked out fairly nicely for me, so far.

--Jeff
You switched to a Java build system? quite the dependency.

Re: OS Makefile

Posted: Tue Nov 30, 2010 2:28 am
by Solar
I'm quite the traditionalist: bash, vim, make, unless it becomes obvious something "heavier" is necessary.

Since I am frequently using different systems, it has the additional benefit that the above "usual suspects" are literally available everywhere.

Re: OS Makefile

Posted: Tue Nov 30, 2010 8:56 am
by carbonBased
Brynet-Inc wrote:
carbonBased wrote:This actually brings up an interesting question (at least to me)...

What type of build environments are devs typically using out there?

I switched from a Makefile based system, to a more extensible ant based system a while back. It's worked out fairly nicely for me, so far.

--Jeff
You switched to a Java build system? quite the dependency.
Java's available for pretty much every system on the planet... I don't consider it much of a dependency.

Also, Ant is java based, but not java specific. It works pretty well with my C based project.

Additionally, the link between xml and tasks is nice; keeps the intent separate from the logic that actually does it. For example, through a single line of xml, I can iterate through all my device driver definition files, and auto generate the glue/support code.

--Jeff

Re: OS Makefile

Posted: Tue Nov 30, 2010 3:58 pm
by TylerH
It's irrelevant how ubiquitous Java may or may not be, if you ever want your OS to be self hosting. Is Java available for your system?

Re: OS Makefile

Posted: Wed Dec 01, 2010 7:55 am
by carbonBased
TylerH wrote:It's irrelevant how ubiquitous Java may or may not be, if you ever want your OS to be self hosting. Is Java available for your system?
At the moment, no, but that hardly makes it irrelevant. Porting Java to a decently mature OS should not be difficult. My previous job entailed running a custom VM on several platforms, so I'm familiar with what is required.

Really; it terms of all the things involved with OS development, I don't think the choice of your build environment should be limited by the effort involved in porting it. There are thousands of more complicated things that deserve greater attention, imo.

Cheers,
--Jeff

Re: OS Makefile

Posted: Thu Dec 02, 2010 12:54 am
by Solar
Quite the statement. Keep us informed when you've got a JVM running on your OS... I'd certainly want to have my OS self-hosting at a much earlier point, but that's choices.

Re: OS Makefile

Posted: Thu Dec 02, 2010 1:32 am
by Dario
I'm still lazy to learn Makefile well, plus I'm always short on time...so instead I use bash script for build and micro emacs for sources.

Re: OS Makefile

Posted: Thu Dec 02, 2010 2:47 am
by Solar
Done right, a Makefile can save you tons of time, as it will find your source files, detect their dependencies and re-compile only those files that actually need to.

We even have a Makefile tutorial in our Wiki, written by yours truly.

Re: OS Makefile

Posted: Thu Dec 02, 2010 5:53 am
by Dario
Thanks, will read it through the weekend since my script is getting very ugly.