BREW自帶的Makefile生成工具產生的Makefile,會將中間文件(.o)生成到源代碼目錄下,這樣源代碼目錄看起來很混亂。但是如果在編譯完後刪除這些文件,又無法享受到make的只編譯改動過的源文件的速度優勢。在第二版自己編寫的BREW Makefile生成工具裏(BMG,BREW Makefile 生成工具的編寫),花了些時間解決了這個問題。
目前採用的方式:
.o目標定義爲"中間目錄/文件名"的形式,例如"objs/foo.c"
模式規則定義爲"objs/%.o : %.c"形式。經試驗不能只寫成"%.o : %.c",會提示找不到生成目標的規則,可能make的模式匹配規則只是匹配文件名。
使用GCC生成每個源文件的依賴關係到一個.d文件,再使用sed處理這個文件,給裏面的源文件名加上“objs/”前綴
使用“-include $(OBJS:.o=.d)”命令包含生成的.d文件
也有其它的更簡潔處的處理方式,可參見下面列出的參考文檔1
原來使用的是微軟的NMake程序,但是它不支持VPATH,編寫起來比較麻煩。最後改用GNU Make,但是在Windows上又遇到了路徑中斜槓和反斜槓的問題。有的Windows版GNU環境和GNU Make不支持Windows的反斜槓路徑表示法,將反斜槓解釋成轉義反斜槓,導致各種生成失敗。現在採用的GNU環境是mingw的msys,GNUWin32的make 3.8.1(Mingw版和cygwin版make目標文件涉及盤符的路徑,引號還會解析失敗,盤符路徑還要寫成unix形式)版。除上面的目標文件形式外"objs/foo.c",均可以使用Windows反斜槓路徑。
對於BREW項目,還有一個易錯點,elf2mod.x這個GCC鏈接腳本。當時爲了啓用編譯器優化選項時AEEMod_Load仍能放到mod 0地址處,在鏈接腳本里寫下了這樣的語句
"AEEMod*.o"(.text.AEEMod_Load)
現在.o放到了獨立目錄,這裏也該做修改。例如
"objs/AEEMod*.o"(.text.AEEMod_Load)
下面附上參考資料:
3.一個makefile實例
TARGET=frame
OBJS=objs/EasyText.o objs/extracter.o objs/graphics.o objs/map.o objs/mapX.o objs/object.o objs/script.o objs/simple_thread.o objs/sprite.o objs/state.o objs/ui.o objs/util.o objs/frame.o objs/AEEAppGen.o objs/AEEModGen.o objs/brew_main.o objs/GCCResolver.o
VPATH=./engine ./game c:/PROGRA~1/BREW31~1.5/sdk/src ./BREW_F~1 $(BMG)
APP_INCLUDES= -I../debug/frame -I./simple_stl -I./engine -I./inc -I$(BREWDIR_3)/inc
DEP_INCLUDES= -I../debug/frame -I./simple_stl -I./engine -I./inc
PRE_DEF=-Dsdf
BREW_HOME = $(BREWDIR)
BREW_ADDINS = $(BREWADDINS)
GCC_HOME = $(GCCHOME)
GCC_LIBPATH = $(GCCLIBPATH)
#-------------------------------------------------------------------------------
# type definitions
#-------------------------------------------------------------------------------
EXETYPE = elf
BINARY = bin
MODULE = mod
#-----------------------------------------------------------------------
# Target compile time symbol definitions
#
# Tells the SDK source stuffs that we're building a dynamic app.
#-----------------------------------------------------------------------
DYNAPP = -DDYNAMIC_APP
#-----------------------------------------------------------------------
# Software tool and environment definitions
# (PREFIX needs to be set to the location of your gcc cross-compiler
# and binutils. STUBTOOL needs to be the path and filename of your
# copy of makestub)
#-----------------------------------------------------------------------
AEESRCPATH = $(BREW_HOME)/src
AEEINCPATH = $(BREW_HOME)/inc
GCC = $(GCC_HOME)/bin/arm-elf-gcc
GPP = $(GCC_HOME)/bin/arm-elf-g++
LD = $(GCC_HOME)/bin/arm-elf-ld
DUMPTOOL = $(GCC_HOME)/bin/arm-elf-objdump
ELF2MODTOOLPATH = $(BREW_ADDINS)/common/bin
ELF2MODTOOL = $(ELF2MODTOOLPATH)/elf2mod.exe
GCCRESOLVEPATH = $(BMG)
#-----------------------------------------------------------------------
# Compiler optimization options
# -O0 disables compiler optimizations. Other options probably work as
# well. Set to taste.
#-----------------------------------------------------------------------
OPT = -Os
#-----------------------------------------------------------------------
# Compiler code generation options
# Add $(TARG) to the CODE line if you're building a Thumb binary (at
# the moment, this doesn't work).
#-----------------------------------------------------------------------
END = -mlittle-endian
TARG = -mthumb
CODE = $(END) -fshort-enums -fno-builtin -ffunction-sections -fno-exceptions -fno-unwind-tables -fno-rtti -fshort-wchar
#-----------------------------------------------------------------------
# Include file search path options
# (change this to point to where the BREW SDK headers are located)
#-----------------------------------------------------------------------
INC = -I$(AEEINCPATH) -I$(GCC_LIBPATH)/include -I$(GCC_HOME)/arm-elf/include $(APP_INCLUDES)
#-----------------------------------------------------------------------
# Library search path options
# (You can change this to point to where your interworking version of
# libgcc.a resides)
#-----------------------------------------------------------------------
LIBDIRS = -L$(GCC_LIBPATH)
#-----------------------------------------------------------------------
# Nothing below here (except for the dependencies at the bottom of the
# file) should need to be changed for a reasonably normal compilation.
#-----------------------------------------------------------------------
#-----------------------------------------------------------------------
# Processor architecture options
# Sets the designated target processor for this applet.
# Currently, all BREW phones use the ARM 7t chip
#-----------------------------------------------------------------------
CPU = -mcpu=arm7tdmi
#-----------------------------------------------------------------------
# ARM Procedure Call Standard (APCS) options
# -fPIC sets posititon independent code. Other option: -fpic
# -mthumb-interwork enables switching between ARM and Thumb code
# -mapcs-frame runs on systems with the frame ptr. specified in the
# APCS
#-----------------------------------------------------------------------
ROPI =
INTRWK = -mthumb-interwork
APCS = -mapcs-frame $(ROPI) $(INTRWK)
#-----------------------------------------------------------------------
# Compiler output options
# -c sets object file output only
#-----------------------------------------------------------------------
OUT = -c
#-----------------------------------------------------------------------
# Compiler/assembler debug Options
# -g is the standard flag to leave debugging information in the
# object files.
#-----------------------------------------------------------------------
DBG = -g
#-----------------------------------------------------------------------
# Linker Options
# -o sets the output filename
#-----------------------------------------------------------------------
LINK_CMD = -Ttext 0 --emit-relocs -entry AEEMod_Load -o
LIBS =-lc -lgcc
#-----------------------------------------------------------------------
# HEXTOOL Options
#-----------------------------------------------------------------------
BINFORMAT = -O binary
#-----------------------------------------------------------------------
# Compiler flag definitions
#-----------------------------------------------------------------------
CFLAGS0 = $(OUT) $(DYNAPP) $(CPU) $(APCS) $(CODE) $(DBG)
CFLAGS = $(CFLAGS0) $(OPT) $(PRE_DEF)
#-----------------------------------------------------------------------
# Linker flag definitions
#-----------------------------------------------------------------------
LDFLAGS = $(LIBDIRS) --script $(GCCRESOLVEPATH)/elf2mod_objs.x
#-----------------------------------------------------------------------
# Default target
#-----------------------------------------------------------------------
default: $(TARGET).$(MODULE)
#-----------------------------------------------------------------------
# All target
#-----------------------------------------------------------------------
.DELETE_ON_ERROR:
all: $(TARGET).$(MODULE)
-include $(OBJS:.o=.d)
objs/%.o : %.c | objs
@echo ---------------------------------------------------------------
@echo 編譯C文件 $<
$(GCC) $(CFLAGS) $(INC) -c $< -o $@
@$(GCC) -MM -isystem$(AEEINCPATH) $(CFLAGS) $(DEP_INCLUDES) $< -o objs/$*.d.tmp
@sed -e 's|.*/.o:|$@:|' objs/$*.d.tmp > objs/$*.d
@rm objs/$*.d.tmp
@echo ---------------------------------------------------------------
objs/%.o : %.cpp | objs
@echo ---------------------------------------------------------------
@echo 編譯C++文件 $<
$(GPP) $(CFLAGS) $(INC) -c $< -o $@
@$(GPP) -MM -isystem$(AEEINCPATH) $(CFLAGS) $(DEP_INCLUDES) $< -o objs/$*.d.tmp
@sed -e 's|.*/.o:|$@:|' objs/$*.d.tmp > objs/$*.d
@rm objs/$*.d.tmp
@echo ---------------------------------------------------------------
objs:
@echo ---------------------------------------------------------------
@echo 建立目標文件目錄
@mkdir $@
@echo ---------------------------------------------------------------
#-----------------------------------------------------------------------
# Targets for making the actual binary
#-----------------------------------------------------------------------
APP_OBJS = $(OBJS)
$(TARGET).$(EXETYPE) : $(APP_OBJS)
$(LD) $(LINK_CMD) $(TARGET).$(EXETYPE) $(LDFLAGS) /
$(APP_OBJS) $(LIBS) $(LINK_ORDER)
$(TARGET).$(MODULE) : $(TARGET).$(EXETYPE)
$(ELF2MODTOOL) $(TARGET).$(EXETYPE) $(TARGET).$(MODULE)
#-----------------------------------------------------------------------
# Cleanup
#-----------------------------------------------------------------------
APP_DEPS = $(OBJS:.o=.d)
clean:
del -f $(APP_OBJS) $(APP_DEPS) $(TARGET).$(EXETYPE) $(TARGET).$(MODULE)