OS Makefile

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
cbrowne
Posts: 2
Joined: Fri Nov 12, 2010 4:04 pm

OS Makefile

Post 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
User avatar
gravaera
Member
Member
Posts: 737
Joined: Tue Jun 02, 2009 4:35 pm
Location: Supporting the cause: Use \tabs to indent code. NOT \x20 spaces.

Re: OS Makefile

Post 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
17:56 < sortie> Paging is called paging because you need to draw it on pages in your notebook to succeed at it.
User avatar
carbonBased
Member
Member
Posts: 382
Joined: Sat Nov 20, 2004 12:00 am
Location: Wellesley, Ontario, Canada
Contact:

Re: OS Makefile

Post 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
User avatar
Brynet-Inc
Member
Member
Posts: 2426
Joined: Tue Oct 17, 2006 9:29 pm
Libera.chat IRC: brynet
Location: Canada
Contact:

Re: OS Makefile

Post 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.
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: OS Makefile

Post 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.
Every good solution is obvious once you've found it.
User avatar
carbonBased
Member
Member
Posts: 382
Joined: Sat Nov 20, 2004 12:00 am
Location: Wellesley, Ontario, Canada
Contact:

Re: OS Makefile

Post 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
TylerH
Member
Member
Posts: 285
Joined: Tue Apr 13, 2010 8:00 pm
Contact:

Re: OS Makefile

Post 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?
User avatar
carbonBased
Member
Member
Posts: 382
Joined: Sat Nov 20, 2004 12:00 am
Location: Wellesley, Ontario, Canada
Contact:

Re: OS Makefile

Post 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
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: OS Makefile

Post 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.
Every good solution is obvious once you've found it.
Dario
Member
Member
Posts: 117
Joined: Sun Aug 31, 2008 12:39 pm

Re: OS Makefile

Post 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.
____
Dario
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: OS Makefile

Post 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.
Every good solution is obvious once you've found it.
Dario
Member
Member
Posts: 117
Joined: Sun Aug 31, 2008 12:39 pm

Re: OS Makefile

Post by Dario »

Thanks, will read it through the weekend since my script is getting very ugly.
____
Dario
Post Reply