Makefile - calling a converter

azenris
Posts: 16
Joined: Thu Nov 26, 2009 5:44 pm

Makefile - calling a converter

Post by azenris » Mon Jun 06, 2011 12:32 am

Might not be right place to post, but hoping you can help me.

I want to edit the makefile to use a program on files within in a folder, and then automatically link those files in.

Basically I have a folder called "stuff".
Inside stuff, are files *.stf.
I have a program "stfConverter".
I want to use the stfConverter to convert all the .stf within the stuff folder.
Then I want the resulting files, ".bin" linked into the project as normal.

Hope I explained it right, or at least in part you get me :(

I know I should probably spend ages researching makefiles (considering their uses), but I'm in the middle of other things and didn't want to abandon my
train of thought in learning and entire new subject.

Any help would be appreciated :)

mtheall
Posts: 210
Joined: Thu Feb 03, 2011 10:47 pm

Re: Makefile - calling a converter

Post by mtheall » Mon Jun 06, 2011 2:54 am

First, you want to define your source files:
STFFILES := $(notdir $(wildcard stuff/*.stf))
This makes a list of all your '.stf' files in the 'stuff' directory.

Then, you want to define your target files:
export STFTARGETS := $(foreach file,$(addsuffix .bin,$(STFFILES)),../bin/$(file))
This takes the list above and appends '.bin' to the filenames and points them to be in the 'bin' directory.

Next, you want to add a rule to produce the target files from the source files. I used this for trial:
$(STFTARGETS): ../bin/%.stf.bin : ../stuff/%.stf
@cp $< $@
You want to replace 'cp' with your 'stfConverter' utility. It needs to take $< as the input file, and $@ for the output file.

Finally, you need to add $(STFTARGETS) to the dependency list of $(OUTPUT).elf:
$(OUTPUT).elf : $(STFTARGETS) $(OFILES)
This will cause your $(STFTARGETS) rule to happen when you build. Hopefully, your '.stf.bin' files will be generated before the 'bin2o' rule can pick them up so that they will linked into your project. It worked for me, generating the header files and all.

You may also want to add a command to 'clean' so that they can be cleaned up. I used:
clean:
@echo clean ...
@rm -fr $(foreach file,$(notdir $(STFTARGETS)),bin/$(file))
@rm -fr $(BUILD) $(TARGET).elf $(TARGET).nds

These things all worked for me without a hitch, but your mileage may vary. If you have any issues, let me know and I'll most definitely be around to help more. You should definitely learn about makefiles, especially the libnds example makefiles. You can do some really powerful stuff.

azenris
Posts: 16
Joined: Thu Nov 26, 2009 5:44 pm

Re: Makefile - calling a converter

Post by azenris » Mon Jun 06, 2011 3:02 am

Thanks, going to try this out tomorrow when I wake up :)

Yea I definitely need to read more on makefiles. I kept saying I would and still I haven't. I was looking at some sites before making my post and it
seems quite a big topic, which is why I wasn't wanting to start it just yet.

Anyway, TY again.

azenris
Posts: 16
Joined: Thu Nov 26, 2009 5:44 pm

Re: Makefile - calling a converter

Post by azenris » Mon Jun 06, 2011 9:53 pm

I have no idea if I'm doing this right, or even understanding the concepts of it all, but anyway!
I didn't exactly get your way working, but did end up with the following makefile:

Code: Select all

#---------------------------------------------------------------------------------
.SUFFIXES:
#---------------------------------------------------------------------------------

ifeq ($(strip $(DEVKITARM)),)
$(error "Please set DEVKITARM in your environment. export DEVKITARM=<path to>devkitARM")
endif

include $(DEVKITARM)/ds_rules

#---------------------------------------------------------------------------------
# TARGET is the name of the output
# BUILD is the directory where object files & intermediate files will be placed
# SOURCES is a list of directories containing source code
# INCLUDES is a list of directories containing extra header files
# DATA is a list of directories containing binary files embedded using bin2o
# GRAPHICS is a list of directories containing image files to be converted with grit
# MODELS is a directory containing models
#---------------------------------------------------------------------------------
TARGET		:=	$(shell basename $(CURDIR))
BUILD		:=	build
SOURCES		:=	source
INCLUDES	:=	include
DATA		:=	data  
GRAPHICS	:=	gfx  
MODELS		:=	models  

#---------------------------------------------------------------------------------
# options for code generation
#---------------------------------------------------------------------------------
ARCH	:=	-mthumb -mthumb-interwork -march=armv5te -mtune=arm946e-s

CFLAGS	:=	-g -Wall -O2\
 		-fomit-frame-pointer\
		-ffast-math \
		$(ARCH)

CFLAGS	+=	$(INCLUDE) -DARM9
CXXFLAGS	:= $(CFLAGS) -fno-rtti -fno-exceptions

ASFLAGS	:=	-g $(ARCH)
LDFLAGS	=	-specs=ds_arm9.specs -g $(ARCH) -Wl,-Map,$(notdir $*.map)

#---------------------------------------------------------------------------------
# any extra libraries we wish to link with the project (order is important)
#---------------------------------------------------------------------------------
LIBS	:= 	-lnds9
 
#---------------------------------------------------------------------------------
# list of directories containing libraries, this must be the top level containing
# include and lib
#---------------------------------------------------------------------------------
LIBDIRS	:=	$(LIBNDS)
 
#---------------------------------------------------------------------------------
# no real need to edit anything past this point unless you need to add additional
# rules for different file extensions
#---------------------------------------------------------------------------------
ifneq ($(BUILD),$(notdir $(CURDIR)))
#---------------------------------------------------------------------------------

export OUTPUT	:=	$(CURDIR)/$(TARGET)

export VPATH	:=	$(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \
					$(foreach dir,$(DATA),$(CURDIR)/$(dir)) \
					$(foreach dir,$(GRAPHICS),$(CURDIR)/$(dir)) \
					$(foreach dir,$(MODELS),$(CURDIR)/$(dir))

export DEPSDIR	:=	$(CURDIR)/$(BUILD)

CFILES		:=	$(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c)))
CPPFILES	:=	$(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp)))
SFILES		:=	$(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s)))
PNGFILES	:=	$(foreach dir,$(GRAPHICS),$(notdir $(wildcard $(dir)/*.png)))
BINFILES	:=	$(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*)))
MODELFILES	:=	$(foreach dir,$(MODELS),$(notdir $(wildcard $(dir)/*.md2)))

#---------------------------------------------------------------------------------
# use CXX for linking C++ projects, CC for standard C
#---------------------------------------------------------------------------------
ifeq ($(strip $(CPPFILES)),)
#---------------------------------------------------------------------------------
	export LD	:=	$(CC)
#---------------------------------------------------------------------------------
else
#---------------------------------------------------------------------------------
	export LD	:=	$(CXX)
#---------------------------------------------------------------------------------
endif
#---------------------------------------------------------------------------------

export OMODELS :=	$(foreach file,$(addsuffix .o,$(MODELFILES)),$(file))

export OFILES	:=	$(addsuffix .o,$(BINFILES)) \
					$(PNGFILES:.png=.o) \
					$(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(SFILES:.s=.o)

export INCLUDE	:=	$(foreach dir,$(INCLUDES),-iquote $(CURDIR)/$(dir)) \
					$(foreach dir,$(LIBDIRS),-I$(dir)/include) \
					-I$(CURDIR)/$(BUILD)
 
export LIBPATHS	:=	$(foreach dir,$(LIBDIRS),-L$(dir)/lib)

icons := $(wildcard *.bmp)

ifneq (,$(findstring $(TARGET).bmp,$(icons)))
	export GAME_ICON := $(CURDIR)/$(TARGET).bmp
else
	ifneq (,$(findstring icon.bmp,$(icons)))
		export GAME_ICON := $(CURDIR)/icon.bmp
	endif
endif
 
.PHONY: $(BUILD) clean
 
#---------------------------------------------------------------------------------
$(BUILD):
	@[ -d $@ ] || mkdir -p $@
	@make --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile
 
#---------------------------------------------------------------------------------
clean:
	@echo clean ...
	@rm -fr $(BUILD) $(TARGET).elf $(TARGET).nds

#---------------------------------------------------------------------------------
else
 
#---------------------------------------------------------------------------------
# main targets
#---------------------------------------------------------------------------------
$(OUTPUT).nds	: 	$(OUTPUT).elf
$(OUTPUT).elf	:	$(OMODELS) $(OFILES)
 
#---------------------------------------------------------------------------------
%.bin.o	:	%.bin
#---------------------------------------------------------------------------------
	@echo $(notdir $<)
	$(bin2o)
	
#---------------------------------------------------------------------------------
%.md2.o : %.md2
#---------------------------------------------------------------------------------
	@dsmfConverter -l -c -t $< -o$@
	$(bin2o)

#---------------------------------------------------------------------------------
# This rule creates assembly source files using grit
# grit takes an image file and a .grit describing how the file is to be processed
# add additional rules like this for each image extension
# you use in the graphics folders
#---------------------------------------------------------------------------------
%.s %.h   : %.png %.grit
#---------------------------------------------------------------------------------
	grit $< -fts -o$*

-include $(DEPSDIR)/*.d
 
#---------------------------------------------------------------------------------------
endif
#---------------------------------------------------------------------------------------
within the models folder is "dolphin.md2"

when I link the created header from the bin2o "dolphin_md2.h"
I tried to use the variable dolphin_md2_size, but its gives the error:

Code: Select all

undefined reference to `dolphin_md2_size'

mtheall
Posts: 210
Joined: Thu Feb 03, 2011 10:47 pm

Re: Makefile - calling a converter

Post by mtheall » Mon Jun 06, 2011 11:09 pm

I'm assuming your '.md2.o' rule doesn't do what you think it does. You need an intermediate rule that does the conversion, then the final rule that makes the '.o'. In any case, I can't recreate your 'undefined reference'.

azenris
Posts: 16
Joined: Thu Nov 26, 2009 5:44 pm

Re: Makefile - calling a converter

Post by azenris » Tue Jun 07, 2011 12:37 am

This is the most up to date makefile: http://pastebin.com/AJzUuHCn

Code: Select all

#---------------------------------------------------------------------------------
.SUFFIXES:
#---------------------------------------------------------------------------------

ifeq ($(strip $(DEVKITARM)),)
$(error "Please set DEVKITARM in your environment. export DEVKITARM=<path to>devkitARM")
endif

include $(DEVKITARM)/ds_rules

#---------------------------------------------------------------------------------
# TARGET is the name of the output
# BUILD is the directory where object files & intermediate files will be placed
# SOURCES is a list of directories containing source code
# INCLUDES is a list of directories containing extra header files
# DATA is a list of directories containing binary files embedded using bin2o
# GRAPHICS is a list of directories containing image files to be converted with grit
# MODELS is a directory containing models
#---------------------------------------------------------------------------------
TARGET		:=	$(shell basename $(CURDIR))
BUILD		:=	build
SOURCES		:=	source
INCLUDES	:=	include
DATA		:=	data  
GRAPHICS	:=	gfx  
MODELS		:=	models  

#---------------------------------------------------------------------------------
# options for code generation
#---------------------------------------------------------------------------------
ARCH	:=	-mthumb -mthumb-interwork -march=armv5te -mtune=arm946e-s

CFLAGS	:=	-g -Wall -O2\
 		-fomit-frame-pointer\
		-ffast-math \
		$(ARCH)

CFLAGS	+=	$(INCLUDE) -DARM9
CXXFLAGS	:= $(CFLAGS) -fno-rtti -fno-exceptions

ASFLAGS	:=	-g $(ARCH)
LDFLAGS	=	-specs=ds_arm9.specs -g $(ARCH) -Wl,-Map,$(notdir $*.map)

#---------------------------------------------------------------------------------
# any extra libraries we wish to link with the project (order is important)
#---------------------------------------------------------------------------------
LIBS	:= 	-lnds9
 
#---------------------------------------------------------------------------------
# list of directories containing libraries, this must be the top level containing
# include and lib
#---------------------------------------------------------------------------------
LIBDIRS	:=	$(LIBNDS)
 
#---------------------------------------------------------------------------------
# no real need to edit anything past this point unless you need to add additional
# rules for different file extensions
#---------------------------------------------------------------------------------
ifneq ($(BUILD),$(notdir $(CURDIR)))
#---------------------------------------------------------------------------------

export OUTPUT	:=	$(CURDIR)/$(TARGET)

export VPATH	:=	$(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \
					$(foreach dir,$(DATA),$(CURDIR)/$(dir)) \
					$(foreach dir,$(GRAPHICS),$(CURDIR)/$(dir)) \
					$(foreach dir,$(MODELS),$(CURDIR)/$(dir))

export DEPSDIR	:=	$(CURDIR)/$(BUILD)

CFILES		:=	$(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c)))
CPPFILES	:=	$(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp)))
SFILES		:=	$(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s)))
PNGFILES	:=	$(foreach dir,$(GRAPHICS),$(notdir $(wildcard $(dir)/*.png)))
BINFILES	:=	$(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*)))
MODELFILES	:=	$(foreach dir,$(MODELS),$(notdir $(wildcard $(dir)/*.md2)))

#---------------------------------------------------------------------------------
# use CXX for linking C++ projects, CC for standard C
#---------------------------------------------------------------------------------
ifeq ($(strip $(CPPFILES)),)
#---------------------------------------------------------------------------------
	export LD	:=	$(CC)
#---------------------------------------------------------------------------------
else
#---------------------------------------------------------------------------------
	export LD	:=	$(CXX)
#---------------------------------------------------------------------------------
endif
#---------------------------------------------------------------------------------

export OMODELS :=	$(foreach file,$(addsuffix .o,$(MODELFILES)),$(file))

export OFILES	:=	$(addsuffix .o,$(BINFILES)) \
					$(PNGFILES:.png=.o) \
					$(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(SFILES:.s=.o)

export INCLUDE	:=	$(foreach dir,$(INCLUDES),-iquote $(CURDIR)/$(dir)) \
					$(foreach dir,$(LIBDIRS),-I$(dir)/include) \
					-I$(CURDIR)/$(BUILD)
 
export LIBPATHS	:=	$(foreach dir,$(LIBDIRS),-L$(dir)/lib)

icons := $(wildcard *.bmp)

ifneq (,$(findstring $(TARGET).bmp,$(icons)))
	export GAME_ICON := $(CURDIR)/$(TARGET).bmp
else
	ifneq (,$(findstring icon.bmp,$(icons)))
		export GAME_ICON := $(CURDIR)/icon.bmp
	endif
endif
 
.PHONY: $(BUILD) clean
 
#---------------------------------------------------------------------------------
$(BUILD):
	@[ -d $@ ] || mkdir -p $@
	@make --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile
 
#---------------------------------------------------------------------------------
clean:
	@echo clean ...
	@rm -fr $(BUILD) $(TARGET).elf $(TARGET).nds

#---------------------------------------------------------------------------------
else
 
#---------------------------------------------------------------------------------
# main targets
#---------------------------------------------------------------------------------
$(OUTPUT).nds	: 	$(OUTPUT).elf
$(OUTPUT).elf	:	$(OMODELS) $(OFILES)
 
#---------------------------------------------------------------------------------
# dsmfConverter
#---------------------------------------------------------------------------------
%.md2.bin : %.md2
	@dsmfConverter -l -c -t $< -o$@
%.md2.o : %.md2.bin
	$(bin2o)

#---------------------------------------------------------------------------------
%.bin.o	: %.bin
#---------------------------------------------------------------------------------
	@echo $(notdir $<)
	$(bin2o)

#---------------------------------------------------------------------------------
# This rule creates assembly source files using grit
# grit takes an image file and a .grit describing how the file is to be processed
# add additional rules like this for each image extension
# you use in the graphics folders
#---------------------------------------------------------------------------------
%.s %.h   : %.png %.grit
#---------------------------------------------------------------------------------
	grit $< -fts -o$*

-include $(DEPSDIR)/*.d
 
#---------------------------------------------------------------------------------------
endif
#---------------------------------------------------------------------------------------
testcode: http://uploading.com/files/1aa6ffb9/Pred.rar/
converter exe: http://uploading.com/files/d6mfm959/dsmfConverter.exe/

I put dsmfConverter in C:\devkitPro\devkitARM\bin
Note I don't actually know if the code fully works yet, I just started it, but It shouldn't matter at this stage.

coverter src: http://uploading.com/files/c6e2mc21/dsmfConverter.rar/

Oh I saw your message Wintermute "try arm-eabi-nm on the .md2.o files", not sure what you meant by that. Where would I add it?
I'm starting to wish I learnt building code the hard way instead of magic compilers :(
Didn't reply as I didn't notice the message till later :p

TY for the help so far, will keep trying stuff.



EDIT:
Just to note, I also tried your makefile, I copy&pasted and changed the cp to my conversion tool, it gave the same error as the makefile above.

mtheall
Posts: 210
Joined: Thu Feb 03, 2011 10:47 pm

Re: Makefile - calling a converter

Post by mtheall » Tue Jun 07, 2011 1:17 am

There's a program in devkitPro/devkitARM/arm-eabi/bin or something like that named arm-eabi-nm. Run that program with the generated .md2.o file as the argument. It should list the variables found in it. whether you see the missing ones or not, it will help us troubleshoot more.

azenris
Posts: 16
Joined: Thu Nov 26, 2009 5:44 pm

Re: Makefile - calling a converter

Post by azenris » Tue Jun 07, 2011 3:51 pm

What does the letter before each variable mean when using arm-eabi-nm
I got the output:

Code: Select all

00000000 R dolphin_md2_bin
00001000 R dolphin_md2_bin_end
00001000 R dolphin_md2_bin_size
Also I used the converter to create a .bin before hand, placed it in the data folder, and it worked as normal.
It seems the .o is not being included when the converter is called automatically.

mtheall
Posts: 210
Joined: Thu Feb 03, 2011 10:47 pm

Re: Makefile - calling a converter

Post by mtheall » Tue Jun 07, 2011 4:50 pm

"R" means it's from a read-only section of memory. As far as why it doesn't get included during the linking, I have no idea. Ideally I'd need a copy of your project to troubleshoot any further.

mtheall
Posts: 210
Joined: Thu Feb 03, 2011 10:47 pm

Re: Makefile - calling a converter

Post by mtheall » Tue Jun 07, 2011 5:16 pm

FIXED!!!

w00t that makes me happy.

I removed OMODELS, and just put the directive in OFILES. So OFILES looks like this:

Code: Select all

export OFILES   :=      $(addsuffix .o,$(MODELFILES)) \
                                        $(addsuffix .o,$(BINFILES)) \
                                        $(PNGFILES:.png=.o) \
                                        $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(SFILES:.s=.o)
Now it should get linked in.

Post Reply

Who is online

Users browsing this forum: No registered users and 15 guests