Page 1 of 1

Makefile

Posted: Wed Jan 30, 2008 2:54 pm
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?

Posted: Wed Jan 30, 2008 5:19 pm
by Zenith
In what way doesn't it work? Can you show us the error message make produces?

Posted: Wed Jan 30, 2008 7:25 pm
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.

Posted: Thu Jan 31, 2008 12:06 am
by pcmattman
patsubst, ie:

Code: Select all

$OBJS=$(patsubst %.c,%.o,$(SRCS))
Read Solar's makefile tutorial on the wiki.

Posted: Mon Feb 04, 2008 4:30 pm
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?

Posted: Mon Feb 04, 2008 5:08 pm
by djenkins75
OBJFILES := $(patsubst %.c, %.o, $(CFILES)) $(patsubst %.asm, %.o, $(ASMFILES))


Just concatenate the patsubst together.

Posted: Tue Feb 05, 2008 1:00 pm
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?

Posted: Wed Feb 06, 2008 2:33 am
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

Posted: Wed Feb 06, 2008 8:14 am
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?

Posted: Wed Feb 06, 2008 8:26 am
by Solar
How about the GRUB manual?