gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > Outputting a dependency-list using GCC..

#24672 - booger - Mon Aug 09, 2004 10:39 am

I'm using the GCC-option -M to generate a list of rules for my project.
This works fine, except that it refuses to build dependencies for my assembler-sourcefiles. A snippet from my makefile:

Code:

$(DEPENDFILE):
   @arm-elf-gcc -mthumb-interwork $(CFLAGS) -I$(AGBINC) -M $(CFILES) $(SFILES) > $@


Anyone know what's wrong?

#24929 - DKL - Fri Aug 13, 2004 5:57 pm

For asm dependencies, use the --MD option from GAS. I can post one of my makefiles if you really want an example.

DKL

#24993 - booger - Sat Aug 14, 2004 7:00 pm

Quote:

For asm dependencies, use the --MD option from GAS. I can post one of my makefiles if you really want an example.

Yes, please post an example.

#25016 - DKL - Sun Aug 15, 2004 10:11 am

Code:

NAME = Name_of_your_bin

vpath %.raw data/
vpath %.lz  data/

AS = @arm-elf-as
CC = @arm-elf-gcc
LD = @arm-elf-gcc
OC = @arm-elf-objcopy
RM = @rm -f
SD = sed

DEFINES =
CFLAGS  = -mthumb -mthumb-interwork -mcpu=arm7tdmi -save-temps -fverbose-asm -Wall -O3 $(DEFINES)
AFLAGS  = -mthumb-interwork -mcpu=arm7tdmi
LFLAGS  = -nostartfiles -Tlnkscript -mthumb-interwork

#
# keep the order for the .S files
.SFILES     = crt0.S your_S_files_01.S your_S_files_02.S
.CFILES     = main.c your_c_files_01.c your_c_files_02.c
.OFILES   = $(.SFILES:.S=.o) $(.CFILES:.c=.o)
.DEPFILES = $(.SFILES:.S=.d) $(.CFILES:.c=.d)
.OVRFILES = data/your_overlay_data.raw

TARGET_ELF = $(NAME).elf
TARGET_BIN = $(NAME).gba

.PHONY: all clean
all: $(TARGET_BIN)

clean:
   $(RM) $(TARGET_BIN) $(TARGET_ELF) $(.CFILES:.c=.s) $(.CFILES:.c=.i) $(.OFILES) $(.DEPFILES)

$(TARGET_BIN) : $(TARGET_ELF)
   $(OC) -O binary $< $(<:.elf=.tmp)
   cat $(<:.elf=.tmp) $(.OVRFILES) > $@
   $(RM) $(<:.elf=.tmp)

$(TARGET_ELF): $(.OFILES)
   $(LD) $(LFLAGS) $(.OFILES) -o $@

%.o: %.c
   $(CC) $(CFLAGS) -c $< -o $@

%.o: %.S
   $(AS) $(AFLAGS) $< -o $@

%.d: %.c
   $(CC) $(CFLAGS) -MM $< | $(SD) "s/$*.o/& $@/g" > $@

%.d: %.S
   $(AS) $(AFLAGS) $< -o $(@:.d=.o) --MD $(@:.d=.dtmp)
   $(SD) -e "s/$*.o/& $@/g" $(@:.d=.dtmp) >$@
   $(RM) $(@:.d=.dtmp)
       
include $(.DEPFILES)


Dependencies are created in different files (.d files) and are then included in the makefile. I'm using a custom crt0.S & linker script, maybe you won't need '-nostartfiles -Tlnkscript' options.

As you can see, for dependencies, Sed is used to fix the strings (add .d files to dep. list). So it will build .d & .o each time a file on which they depend is modified.

An asm file is assembled the same time it's checked for dep. I found no other way to do that. It works so I'm happy. :)

Also, you'll have warnings the first time you'll make your project, since .d files don't exist yet. Don't care about that, they will by the time the end of the makefile is reached.

Hope it will help.

Happy coding,
DKL

#25231 - col - Thu Aug 19, 2004 3:57 am

Instead of
Code:
 include $(.DEPFILES) 

use
Code:
 -include $(.DEPFILES) 

and you shouldn't get those pesky warnings.

Also, is there any reason you are using -MM instead of -MD for generating dependency files ?
from the gcc manual (v3.2.3)
'-MD can be used to generate a dependency output file as a side-effect of the compilation process'
looky here:
http://gcc.gnu.org/onlinedocs/gcc-3.2.3/gcc/Preprocessor-Options.html#Preprocessor%20Options
here's an example from my makefile:
Code:

%.o: %.cpp
   $(CC) $(INCLUDES) $(DEFAULT_SWITCHES) -MD -MP -o $@ $<
...
...
-include $(SRC:.o=.d)


no regex required :)

cheers

Col

#25241 - DKL - Thu Aug 19, 2004 9:20 am

col wrote:
Instead of
Code:
 include $(.DEPFILES) 

use
Code:
 -include $(.DEPFILES) 

and you shouldn't get those pesky warnings.


Ah yes sure, I didn't even think to use "-" at this place. How dumb...
Thank you. :)

Quote:
Also, is there any reason you are using -MM instead of -MD for generating dependency files ?


Yep, there is. -MM won't take account of system header files (like <stdio.h>). I don't think you are modifying these files. Anyway, I don't even use standard libraries for my own gba dev, so that's the best choice. :)

Regards,
DKL

#25244 - col - Thu Aug 19, 2004 12:00 pm

DKL wrote:
...
Yep, there is. -MM won't take account of system header files (like <stdio.h>). I don't think you are modifying these files. Anyway, I don't even use standard libraries for my own gba dev, so that's the best choice. :)

Regards,
DKL


If you want to ignore system headers, use -MMD.
It's all at the link I posted :)

cheers

Col

#25245 - DKL - Thu Aug 19, 2004 12:31 pm

col wrote:

If you want to ignore system headers, use -MMD.
It's all at the link I posted :)


Oooh, sorry, I didn't see what you mean the first time (just woke up :) ).
Sure, it's far easier that way ! I didn't even see this option. Thanks a lot. :)

Regards,
DKL