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
User avatar
Jeko
Member
Member
Posts: 500
Joined: Fri Mar 17, 2006 12:00 am
Location: Napoli, Italy

Makefile

Post by Jeko »

I'm rewriting my kernel from 0 because I want to do it better than before.

Now I'm making a more clean Makefile:

Code: Select all

KERNEL_NAME := jeko.img

CC := ccache gcc
AS := nasm
LD := ld

PROJDIRS := .

CFILES := $(shell find $(PROJDIRS) -mindepth 1 -maxdepth 3 -name "*.c")
ASMFILES := $(shell find $(PROJDIRS) -mindepth 1 -maxdepth 3 -name "*.asm")
OBJFILES := $(shell find $(PROJDIRS) -mindepth 1 -maxdepth 3 -name "*.o")

CFLAGS := -Wall -O -floop-optimize2 -fno-builtin -nostdlib -nostartfiles -nodefaultlibs -nostdinc -I include -ffreestanding -fno-stack-protector
LDFLAGS := -Tlink.ld -o $(KERNEL_NAME)
ASFLAGS := -felf

all: $(KERNEL_NAME)

install:
	mount /floppy/
	cp $(KERNEL_NAME) /floppy/

	umount /floppy/

clean:
	rm $(OBJFILES)

%.o: %.asm

	$(AS) $(ASFLAGS) -o $@ $<



%.o: %.c

	$(CC) $(CFLAGS) -c $< -o $@



$(KERNEL_NAME): $(OBJFILES)	

	$(LD) $(LDFLAGS) $(OBJFILES)
but it doesn't work. Why?
User avatar
Zenith
Member
Member
Posts: 224
Joined: Tue Apr 10, 2007 4:42 pm

Post by Zenith »

In what way doesn't it work? Can you show us the error message make produces?
djenkins75
Posts: 11
Joined: Sun Dec 16, 2007 11:32 am
Location: Wisconsin, USA

Post by djenkins75 »

I would suspect that "OBJFILES" is empty if they have not already been built.


Therefore, "KERNEL_NAME" has no dependencies, so none of the objects get built.

I would suggest defining OBJFILES some other way. Make supports a method for renaming the extension of the strings contained within a variable. I forget the exact syntax, but something like this:

OBJILES:= $(ASMFILES:=.o) $(CFILES:=.o)

except the above is incorrect. It will add ".o" to each file instead of replacing ".c" or ".asm" with ".o". But there is a way to do it.

Exercise left to the OP.
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post by pcmattman »

patsubst, ie:

Code: Select all

$OBJS=$(patsubst %.c,%.o,$(SRCS))
Read Solar's makefile tutorial on the wiki.
User avatar
Jeko
Member
Member
Posts: 500
Joined: Fri Mar 17, 2006 12:00 am
Location: Napoli, Italy

Post by Jeko »

Code: Select all

KERNEL_NAME := jeko.img

CC := ccache gcc
AS := nasm
LD := ld

PROJDIRS := .

CFILES := $(shell find $(PROJDIRS) -mindepth 1 -maxdepth 3 -name "*.c")
ASMFILES := $(shell find $(PROJDIRS) -mindepth 1 -maxdepth 3 -name "*.asm")
OBJFILES := $(patsubst %.c, %.o, $(CFILES))

CFLAGS := -Wall -O -floop-optimize2 -fno-builtin -nostdlib -nostartfiles -nodefaultlibs -nostdinc -I include -ffreestanding -fno-stack-protector
LDFLAGS := -Tlink.ld -o $(KERNEL_NAME)
ASFLAGS := -felf

all: $(KERNEL_NAME)

install:
	mount /floppy/
	cp $(KERNEL_NAME) /floppy/

	umount /floppy/

clean:
	rm $(OBJFILES)

%.o: %.asm

	$(AS) $(ASFLAGS) -o $@ $<



%.o: %.c

	$(CC) $(CFLAGS) -c $< -o $@



$(KERNEL_NAME): $(OBJFILES)	

	$(LD) $(LDFLAGS) $(OBJFILES)
This is my new Makefile, but it doesn't work. At least for ASM files.

How can I do the makefile to work also with asmfiles?
(I think I must change OBJFILES := $(patsubst %.c, %.o, $(CFILES)), because it takes only $(CFILES). But how must I do it?
djenkins75
Posts: 11
Joined: Sun Dec 16, 2007 11:32 am
Location: Wisconsin, USA

Post by djenkins75 »

OBJFILES := $(patsubst %.c, %.o, $(CFILES)) $(patsubst %.asm, %.o, $(ASMFILES))


Just concatenate the patsubst together.
User avatar
Jeko
Member
Member
Posts: 500
Joined: Fri Mar 17, 2006 12:00 am
Location: Napoli, Italy

Post by Jeko »

Ok now it works. But I have a question. How can I use GRUB in order to boot my operating system?
I read this, but if my makefile become this:

Code: Select all

KERNEL_NAME := jeko.img

CC := ccache gcc
AS := nasm
LD := ld

PROJDIRS := .

CFILES := $(shell find $(PROJDIRS) -mindepth 1 -maxdepth 3 -name "*.c")
ASMFILES := $(shell find $(PROJDIRS) -mindepth 1 -maxdepth 3 -name "*.asm")
OBJFILES := $(patsubst %.c, %.o, $(CFILES)) $(patsubst %.asm, %.o, $(ASMFILES))

CFLAGS := -Wall -O -floop-optimize2 -fno-builtin -nostdlib -nostartfiles -nodefaultlibs -nostdinc -I include -ffreestanding -fno-stack-protector
LDFLAGS := -Tlink.ld -o $(KERNEL_NAME)
ASFLAGS := -felf

all: $(KERNEL_NAME)

install:
	mount /floppy/
	cp $(KERNEL_NAME) /floppy/

	umount /floppy/

clean:
	rm $(OBJFILES)

%.o: %.asm

	$(AS) $(ASFLAGS) -o $@ $<



%.o: %.c

	$(CC) $(CFLAGS) -c $< -o $@



$(KERNEL_NAME): $(OBJFILES) barebones bootfloppy.img menu.cfg	

	$(LD) $(LDFLAGS) $(OBJFILES)
	cp bootfloppy.img $@
	chmod +w $@
	mcopy -o -i $@ $< ::/boot
	mcopy -o -i $@ menu.cfg ::/boot
It doesn't work. This is the error message:
ccache gcc -Wall -O -floop-optimize2 -fno-builtin -nostdlib -nostartfiles -nodefaultlibs -nostdinc -I include -ffreestanding -fno-stack-protector -c kernel.c -o kernel.o
nasm -felf -o loader.o loader.asm
ld -Tlink.ld -o jeko.img ./kernel.o ./loader.o
cp bootfloppy.img jeko.img
chmod +w jeko.img
mcopy -o -i jeko.img kernel.o ::/boot
Total number of sectors not a multiple of sectors per track!
Add mtools_skip_check=1 to your .mtoolsrc file to skip this test
make: *** [jeko.img] Error 1


There isn't a better method to work with GRUB?
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Post by AJ »

Hi,

The problem is with you 'mcopy' command line options, not with GRUB or any other part of the toolchain. I've never used mtools, but it may be worth checking out the mcopy documentation - looks like you have to copy over a set number of sectors (at minimum, the bumber of sectors in a track?). Alternatively, use partcopy or another alternative.

Cheers,
Adam
User avatar
Jeko
Member
Member
Posts: 500
Joined: Fri Mar 17, 2006 12:00 am
Location: Napoli, Italy

Post by Jeko »

AJ wrote:Hi,

The problem is with you 'mcopy' command line options, not with GRUB or any other part of the toolchain. I've never used mtools, but it may be worth checking out the mcopy documentation - looks like you have to copy over a set number of sectors (at minimum, the bumber of sectors in a track?). Alternatively, use partcopy or another alternative.

Cheers,
Adam
could you explain me how to use GRUB as my bootloader?
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Post by Solar »

How about the GRUB manual?
Every good solution is obvious once you've found it.
Post Reply