KOS Makefiles

If you have any questions on programming, this is the place to ask them, whether you're a newbie or an experienced programmer. Discussion on programming in general is also welcome. We will help you with programming homework, but we will not do your work for you! Any porting requests must be made in Developmental Ideas.
Post Reply
Dreamcast alive
DCEmu Junior
DCEmu Junior
Posts: 45
https://www.artistsworkshop.eu/meble-kuchenne-na-wymiar-warszawa-gdzie-zamowic/
Joined: Tue Apr 11, 2006 3:29 am
Has thanked: 0
Been thanked: 2 times

KOS Makefiles

Post by Dreamcast alive »

Hi guys,

I've learned "make" a little bit in the last days and I also studied KOS make files a little bit. I have one little question now regarding the make files of KOS. It seems to me there are no include file dependencies and also no auto generated dependencies. If someone changes a header file the build process might not work because foo.o depends only on foo.c and not foo.h according to the make file. Have I overlooked something or is my understanding wrong? What is the explanation for this?

This is a rule from Makefile.rules to build objects files from C files:

Code: Select all

%.o: %.c
	kos-cc $(CFLAGS) -c $< -o $@
Thanks in advance.
User avatar
BlueCrab
The Crabby Overlord
The Crabby Overlord
Posts: 5663
Joined: Mon May 27, 2002 11:31 am
Location: Sailing the Skies of Arcadia
Has thanked: 9 times
Been thanked: 69 times
Contact:

Re: KOS Makefiles

Post by BlueCrab »

Well, it is impossible to know in the generic rules that are provided in the KOS Makefile.rules what header files a given .c file will depend on, so if you don't do anything else, then yes you could potentially run into dependency issues.

Of course, you can always do something like this in your Makefile to fix that problem:

Code: Select all

foo.o: foo.c foo.h bar.h
That would, of course, make foo.o depend not only on foo.c, but also on foo.h and bar.h. You could also, as you seem to realize in your post, use automatic dependency generation, like what is described here.
Dreamcast alive
DCEmu Junior
DCEmu Junior
Posts: 45
Joined: Tue Apr 11, 2006 3:29 am
Has thanked: 0
Been thanked: 2 times

Re: KOS Makefiles

Post by Dreamcast alive »

Okay, thank you for clearing this. So if a header has been changed the user has to do a "touch foo.c" on all C files which inclde that header file. Then he can call make. Or you can do a make mrproper if the project is not too big. It's a pity that there is no comfortable built in funtion to auto generate depencies. The official solution from the GNU Software Foundation seems a bit complicated to me. For each source file there will be created an extra file only for the dependencies but it's better than nothing I think :)
User avatar
BlueCrab
The Crabby Overlord
The Crabby Overlord
Posts: 5663
Joined: Mon May 27, 2002 11:31 am
Location: Sailing the Skies of Arcadia
Has thanked: 9 times
Been thanked: 69 times
Contact:

Re: KOS Makefiles

Post by BlueCrab »

You don't have to bother with the "touch foo.c" if you just add blank rules for all your .c files like the one I gave as an example in the previous post.

Also, automatic dependency tracking is a bit ugly, but once you set it up, it should work for all your files, pretty much. Perhaps at some point I'll add that to the KOS Makefile rules...

EDIT: Added in commit 441060. Basically, if you add a KOS_DEPDIR variable to your Makefile, it'll automatically generate and include the appropriate dependency tracking information. As an example, here's a Makefile that includes everything you'd need (specifically, from CrabEmu):

Code: Select all

#	Makefile
#	CrabEmu Dreamcast Makefile
#	Copyright (C) 2007, 2008, 2009, 2012, 2013 Lawrence Sebald
#
#	This program is free software; you can redistribute it and/or modify
#	it under the terms of the GNU General Public License as published by
#	the Free Software Foundation; either version 2 of the License, or
#	(at your option) any later version.
#
#	This program is distributed in the hope that it will be useful,
#	but WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#	GNU General Public License for more details.
#
#	You should have received a copy of the GNU General Public License
#	along with this program; if not, write to the Free Software
#	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

KOS_DEPDIR = .dep
TARGET = crabemu.bin

OBJS = main.o sound.o palmenu.o ../consoles/sms/sms.o ../consoles/sms/smsmem.o \
 ../consoles/sms/smsvdp.o ../consoles/sms/smsz80.o \
 menu.o font.o pvrutils.o ../consoles/sms/93c46.o ../consoles/sms/mappers.o \
 ../cpu/CrabZ80/CrabZ80.o ../sound/sn76489.o \
 ../consoles/sms/terebi.o ../consoles/sms/tms9918a.o \
 ../consoles/sms/smsmem-gg.o ../consoles/sms/mapper-93c46.o \
 ../consoles/sms/mapper-korean.o ../consoles/sms/mapper-codemasters.o \
 ../consoles/sms/mapper-sega.o ../consoles/sms/mapper-sg1000.o \
 ../consoles/sms/cheats.o ../consoles/sms/sdscterminal.o ../utils/list.o \
 ../utils/minizip/ioapi.o ../utils/minizip/unzip.o ../sound/ym2413.o \
 ../consoles/sms/mapper-koreanmsx.o ../consoles/sms/mapper-4PAA.o \
 ../consoles/colecovision/colecovision.o ../consoles/colecovision/colecomem.o \
 ../consoles/sms/mapper-none.o ../consoles/sms/mapper-janggun.o ../rom.o \
 ../cpu/Crab6502/Crab6502.o ../consoles/nes/nes.o ../consoles/nes/nesmem.o \
 ../consoles/nes/nesppu.o ../consoles/nes/mappers/nesmapper0.o \
 ../consoles/nes/mappers/nesmapper1.o ../sound/nesapu-nosefart.o \
 ../sound/nes_apu/fds_snd.o ../sound/nes_apu/mmc5_snd.o \
 ../sound/nes_apu/nes_apu.o ../sound/nes_apu/vrcvisnd.o \
 ../consoles/nes/mappers/nesmapper2.o ../consoles/nes/mappers/nesmapper3.o \
 ../consoles/nes/mappers/nesmapper7.o ../consoles/nes/mappers/nesmapper66.o \
 ../consoles/nes/mappers/nesmapper9.o
 
 KOS_CFLAGS += -I ../consoles/sms -I ../cpu/CrabZ80 -I ../sound -I .. -W \
 -I . -DIN_CRABEMU -DCRABZ80_NO_READMAP_FALLBACK \
 -I $(KOS_BASE)/kernel/arch/dreamcast/sound -I ../utils -I ../utils/minizip \
 -I ../consoles/colecovision -Wno-char-subscripts -std=gnu99 \
 -I ../consoles/nes -I ../cpu/Crab6502 -DCRAB6502_NO_READMAP_FALLBACK \
 -DCRAB6502_CPU_TYPE_2A03 -I ../sound/nes_apu -DCRAB6502_ZPG_USE_READMAP \
 -DCRAB6502_STACK_USE_READMAP

#KOS_CFLAGS += -DDEBUG -DNO_GUI
#KOS_CFLAGS += -DFPS_COUNTER

# Uncomment below to enable YM2413 emulation in the SMS emulation. Be warned,
# that this will probably break things with the frameskip code.
#KOS_CFLAGS += -DCRABEMU_DC_ENABLE_YM2413

all: mkdep rm-elf $(TARGET)

mkdep:
	mkdir -p $(KOS_DEPDIR)

clean:
	-rm -f $(TARGET) 1ST_READ.BIN crabemu.bin crabemu.elf $(OBJS)
	-rm -rf $(KOS_DEPDIR)

rm-elf:
	-rm -f $(TARGET) crabemu.elf

crabemu.elf: $(OBJS)
	kos-cc -o $@ $^ -lkosext2fs -lpng -lz -lbz2 -lm

crabemu.bin: crabemu.elf
	kos-objcopy -O binary -R .stack $^ $@

1ST_READ.BIN: crabemu.bin
	scramble $^ $@

include $(KOS_BASE)/Makefile.rules
The relevant stuff in here is the KOS_DEPDIR line (of course), the mkdep target (and the fact that it is in the all target), and the last line of the clean target. The mkdep target simply creates the directory to store the dependency information in (so that you don't have to keep the directory around) and the last line of the clean target gets rid of the directory. Of course, you could just as easily keep around a directory and eliminate the need for that stuff at all.
Dreamcast alive
DCEmu Junior
DCEmu Junior
Posts: 45
Joined: Tue Apr 11, 2006 3:29 am
Has thanked: 0
Been thanked: 2 times

Re: KOS Makefiles

Post by Dreamcast alive »

Hey good work :) Very nice.

I think this solution is only suitable for small projects:

Code: Select all

foo.o: foo.c foo.h bar.h
Everytime you include or remove an include file in a C file you have to update the makefile which is annoying and error-prone. Then I prefer the solution or the generic rules with or without the auto generation of dependencies.
Dreamcast alive
DCEmu Junior
DCEmu Junior
Posts: 45
Joined: Tue Apr 11, 2006 3:29 am
Has thanked: 0
Been thanked: 2 times

Re: KOS Makefiles

Post by Dreamcast alive »

Hey BlueCrab,

sorry to be annoying but I have another question. Would it be a good idea to set the dependencies directory on a tmpfs (/run on my system) so the files are located on RAM? My Computer has 2 GB RAM. What do you think? Thanks.
User avatar
BlueCrab
The Crabby Overlord
The Crabby Overlord
Posts: 5663
Joined: Mon May 27, 2002 11:31 am
Location: Sailing the Skies of Arcadia
Has thanked: 9 times
Been thanked: 69 times
Contact:

Re: KOS Makefiles

Post by BlueCrab »

I wouldn't do that, since you'll lose them on a reboot. Kinda defeats the purpose a lot of the time. :wink:

That, and I doubt there'd be THAT much of an improvement in compile time unless you're doing something huge... By huge, I mean something with more source code than the Linux kernel itself (which I REALLY doubt you're doing).
Post Reply