BREW Makefile生成工具功能改进——把中间文件放入独立目录

    技术2022-05-20  31

      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)

     

      下面附上参考资料:

       1.让 GNU Make 把中间文件放到独立目录

       2.GNU Make 下创建目录的问题

    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)

     


    最新回复(0)