Help cleaning up my 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
astrocrep
Member
Member
Posts: 127
Joined: Sat Apr 21, 2007 7:21 pm

Help cleaning up my makefile

Post by astrocrep »

Below is the contents of my current makefile. It needs to change in a BAD way... its becoming very combersome to maintain, and on top of that, my source code folder is being overloaded with files.

What I would like to do, is start moving code around so in my source folder, I would have sub folders:

mm/ -- This would contain the code for the allocator, pager, and kmalloc
tasks/ -- This would contain the code for tasks, and schedulers
drivers/ -- This would contain the code for the drivers, keyboard etc...
lowlevel/ -- This would contain all low-level init stuff, like gdt,idt,isr, and the assembler files...

I am not looking for someone to re-write my make file, but reading the faqs on the net isn't helping me much.

What I would like to do is:
go to the source folder, and run make all... gcc (nasm) creates objects for all the files in all the folders and moves all of the objects to a build/ folder. Then the linker links it all together, and copies to resulting file onto my floppy image... (thats currently what make install does) and currently make all, does include the make install...

Thanks in advance,
Rich

Code: Select all

CC=gcc
CFLAGS=-Wall -O -fstrength-reduce -fomit-frame-pointer -fleading-underscore -fno-stack-protector -finline-functions -nostdinc -fno-builtin -I./include -c -o
LD=ld
LDFLAGS=-Tlink.ld -o
ASM=nasm
ASMFLAGS=-f elf -o
SOURCES=start.asm pmutil.asm isr_handlers.asm irq_handlers.asm main.c gdt.c string.c iotech.c conio.c idt.c isrs.c irq.c xprinf.c kbdrv.c timer.c allocator.c pageman.c kmalloc.c reboot.c tasks.c
OBJS=start.o pmutil.o isr_handlers.o irq_handlers.o main.o gdt.o string.o iotech.o conio.o idt.o isrs.o irq.o xprintf.o kbdrv.o timer.o allocator.o pageman.o kmalloc.o reboot.o tasks.o
EXECUTABLE=cnex.sys

all: $(EXECUTABLE) install clean

$(EXECUTABLE) : $(OBJS)
	$(LD) $(LDFLAGS) $(EXECUTABLE) $(OBJS)
start.o : start.asm
	$(ASM) $(ASMFLAGS) start.o start.asm
pmutil.o : pmutil.asm
	$(ASM) $(ASMFLAGS) pmutil.o pmutil.asm
isr_handlers.o : isr_handlers.asm
	$(ASM) $(ASMFLAGS) isr_handlers.o isr_handlers.asm
irq_handlers.o : irq_handlers.asm
	$(ASM) $(ASMFLAGS) irq_handlers.o irq_handlers.asm
main.o : main.c
	$(CC) $(CFLAGS) main.o main.c
string.o   : string.c
	$(CC) $(CFLAGS) string.o string.c
iotech.o : iotech.c
	$(CC) $(CFLAGS) iotech.o iotech.c
conio.o : conio.c
	$(CC) $(CFLAGS) conio.o conio.c
idt.o : idt.c
	$(CC) $(CFLAGS) idt.o idt.c
gdt.o : gdt.c
	$(CC) $(CFLAGS) gdt.o gdt.c
irq.o : irq.c
	$(CC) $(CFLAGS) irq.o irq.c
isrs.o : isrs.c
	$(CC) $(CFLAGS) isrs.o isrs.c
xprintf.o : xprintf.c
	$(CC) $(CFLAGS) xprintf.o xprintf.c
kbdrv.o : kbdrv.c
	$(CC) $(CFLAGS) kbdrv.o kbdrv.c
timer.o : timer.c
	$(CC) $(CFLAGS) timer.o timer.c
allocator.o : allocator.c
	$(CC) $(CFLAGS) allocator.o allocator.c
pageman.o : pageman.c
	$(CC) $(CFLAGS) pageman.o pageman.c
kmalloc.o : kmalloc.c
	$(CC) $(CFLAGS) kmalloc.o kmalloc.c
reboot.o : reboot.c
	$(CC) $(CFLAGS) reboot.o reboot.c
tasks.o : tasks.c
	$(CC) $(CFLAGS) tasks.o tasks.c
clean  :
	rm -rf *.o
install :
	mdel a:cnex.sys 
	mcopy cnex.sys a:
Franchie
Posts: 11
Joined: Tue Apr 17, 2007 5:34 am

Post by Franchie »

Hi!
It needs to change in a BAD way
I hope you meant "It badly needs to change"! ;-)

I did a similar transition a while back. I now have a main Makefile, that calls the Makefiles in the subfolders by a simple cd [folder] && make. That works ok. I also use wildcards, so that I don't have to create a new rule for each file I add.

So for example, an extract of the Main Makefile is:

Code: Select all

output: ./asm/*.asm.o	./kernel/*.c.o	...
	ld ${LDFLAGS} -o $@ $^

./asm/%.asm.o: ./asm/%.asm
	cd ./asm && make

./kernel/%.c.o: ./kernel/%.c
	cd ./kernel && make
and in each of the subdirectory Makefiles I have

Code: Select all

#!/usr/bin/make
# Makefile for /asm

all: cr.asm.o	irq.asm.o	isr.asm.o	main.asm.o
	
%.asm.o: %.asm
	nasm -f aout $< -o $@

%.c.o: %.c
	${CC} ${CFLAGS} -o $@ -c $<

clean:
	rm -f *.c.o *.asm.o

This is by far not the best way of writing a Makefile, nor is it the most flexible/beautiful, but hey! it works!

You would only need to adapt the syntax to output to your ./build folder...

Hope this helps,
Franchie
JJeronimo
Member
Member
Posts: 202
Joined: Wed Oct 18, 2006 3:29 pm

Post by JJeronimo »

Franchie wrote:I did a similar transition a while back. I now have a main Makefile, that calls the Makefiles in the subfolders by a simple cd [folder] && make. That works ok. I also use wildcards, so that I don't have to create a new rule for each file I add.
Recursive use of make is not very recommended...
Take a look at the following... This does as:
1. Object filenames in LIBCOMPS are converted to C source filenames and compiled accordingly
2. Creates an archive of the objects obtained in the previous pass (name is lib$(OUTLIB).a)...
3. Creates executables listed in TOOLS, based on the corresponding source code files and on the previously compiled lib... It expects to find the headers in "include/" so that the programs can use functions exported by the lib...

It doesn't check for header files modifications... Prints human-readable output and does no chdir...

Note: test it in a sandbox before executing make clean in real life...


Code: Select all

TOOLS=
LIBCOMPS=



LIBPATH=lib
LIBOBJECTS=${LIBCOMPS:%=$(LIBPATH)/%}
OUTLIB=
LIBFILE=lib$(OUTLIB).a

TOOLSPATH=tools
TOOLSEXECS=${TOOLS:%=$(TOOLSPATH)/%}



INCLUDEPATH=include
CFLAGS=-Wall -I$(INCLUDEPATH)
LDFLAGS=-L$(LIBPATH) -l$(OUTLIB)




all:	$(TOOLSEXECS)

clean:
	rm -f $(TOOLSEXECS)
	rm -f $(LIBPATH)/$(LIBFILE)
	rm -f $(LIBOBJECTS)

$(TOOLSEXECS):		%	: %.c $(LIBPATH)/$(LIBFILE)
			@echo COMPILE $< TO $@
			@$(CC) $(CFLAGS) -o $@ $< $(LDFLAGS)

$(LIBPATH)/$(LIBFILE):	$(LIBOBJECTS)
			@echo ARCHIVE $(LIBOBJECTS) TO $@
			@ar rcs $@ $(LIBOBJECTS)

$(LIBOBJECTS):		%.o	: %.c
			@echo COMPILE $< TO $@
			@$(CC) $(CFLAGS) -c -o $@ $<
Post Reply