https://github.com/zytc2009/BigTeam_learning
分类: LINUX
2009-02-10 11:04:21
edit : main.o command.o
cc –o edit main.o command.o
main.o : main.c defs.h
cc –c main.c -o main.o
command.o : command.c defs.h
cc –c command.c –o command.o
clean :
rm main.o \
command.o |
makefile – 一个简单却齐全的Make File实例 |
SHELL = sh
#=====================================================================
# Name:
# makefile
#
# Description:
# Makefile to build the $(TARGET) module.
#
# The following nmake targets are available in this makefile:
#
# all - make .elf and .mod image files (default)
# clean - delete object directory and image files
# filename.o - make object file
# filename.mix - make C and ASM mix file
#
# The above targets can be made with the following command:
#
# make [target]
#
# Assumptions:
# 1. The ARM ADS 1.0.1 or higher tools are installed
# 2. The run environment of make toos is exist in this OS
#
#----------------------------------------------------------------------------
#=====================================================================
TARGET = Test4#
SCLFILE = system.scl#
C_OBJS = mainapp.o raminit.o
A_OBJS = bootblock.o
OBJS = $(C_OBJS) $(A_OBJS)
APP_PATH = .#
# set the search path
vpath %.c $(APP_PATH)
vpath %.s $(APP_PATH)
#-------------------------------------------------------------------------------
# Target file name and type definitions
#-------------------------------------------------------------------------------
EXETYPE = elf# # Target image file format
MODULE = bin# # Binary module extension
#-------------------------------------------------------------------------------
# Target compile time symbol definitions
#-------------------------------------------------------------------------------
ARMASM = -D_ARM_ASM_#
#-------------------------------------------------------------------------------
# Software tool and environment definitions
#-------------------------------------------------------------------------------
ARMBIN = $(ARMHOME)/Bin#
ARMBIN := $(subst \,/,$(ARMBIN))#
ARMCC = $(ARMBIN)/armcc# # ARM ADS ARM 32-bit inst. set ANSI C compiler
ASM = $(ARMBIN)/armasm# # ARM ADS assembler
LD = $(ARMBIN)/armlink# # ARM ADS linker
HEXTOOL = $(ARMBIN)/fromelf# # ARM ADS utility to create hex file from image
OBJ_CMD = -o# # Command line option to specify output filename
#-------------------------------------------------------------------------------
# Processor architecture options
#-------------------------------------------------------------------------------
CPU = -cpu ARM7TDMI# # ARM7TDMI target processor
#-------------------------------------------------------------------------------
# ARM Procedure Call Standard (APCS) options
#-------------------------------------------------------------------------------
APCS = -apcs /noswst/interwork
#-------------------------------------------------------------------------------
# Additional compile time error checking options
#-------------------------------------------------------------------------------
CHK = -fa# # Check for data flow anomolies
#-------------------------------------------------------------------------------
# Compiler output options
#-------------------------------------------------------------------------------
OUT = -c# # Object file output only
#-------------------------------------------------------------------------------
# Compiler/assembler debug options
#-------------------------------------------------------------------------------
DBG = -g# # Enable debug
#-------------------------------------------------------------------------------
# Compiler optimization options
#-------------------------------------------------------------------------------
OPT = -Ospace -O2# # Full compiler optimization for space
#-------------------------------------------------------------------------------
# Compiler code generation options
#-------------------------------------------------------------------------------
END = -littleend# # Compile for little endian memory architecture
ZA = -zo# # LDR may only access 32-bit aligned addresses
CODE = $(END) $(ZA)#
#-------------------------------------------------------------------------------
# Include file search path options
#-------------------------------------------------------------------------------
INC = -I.#
#-------------------------------------------------------------------------------
# Linker options
#-------------------------------------------------------------------------------
LINK_CMD = -o# # Command line option to specify output file
# on linking
LIST = -list $(TARGET).map# # Direct map and info output to file
INFO = -elf -map -info sizes,totals,veneers,unused
LINK_OPT = -scatter $(SCLFILE)# # Use scatter load description file
#-------------------------------------------------------------------------------
# HEXTOOL options
#-------------------------------------------------------------------------------
BINFORMAT = -bin#
OUTPUT = -output#
#-------------------------------------------------------------------------------
# Compiler flag definitions
#-------------------------------------------------------------------------------
CFLAGS = $(OUT) $(INC) $(CPU) $(APCS) $(CODE) $(CHK) $(DBG) $(OPT)
AFLAGS = $(CPU) $(APCS) $(INC)
#----------------------------------------------------------------------------
# Default target
#----------------------------------------------------------------------------
.PHONY : all
all : startup $(TARGET).$(MODULE) complete
.PHONY : startup
startup:
@echo ---------------------------------------------------------------
@echo Compile startup
@echo ---------------------------------------------------------------
.PHONY : complete
complete:
@echo ---------------------------------------------------------------
@echo All have been done
@echo ---------------------------------------------------------------
#----------------------------------------------------------------------------
# Clean target
#----------------------------------------------------------------------------
# The object subdirectory, target image file, and target hex file are deleted.
.PHONY : clean
clean :
@echo ---------------------------------------------------------------
@echo CLEAN
-rm -f *.o
-rm -f *.i
-rm -f *.dep
-rm -f $(TARGET).$(EXETYPE)
-rm $(TARGET).$(MODULE)
-rm $(TARGET).map
@echo ---------------------------------------------------------------
#=====================================================================
# DEFAULT SUFFIX RULES
#=====================================================================
SRC_FILE = $(@F:.o=.c)# # Input source file specification
OBJ_FILE = $(OBJ_CMD) $(@F)# # Output object file specification
.SUFFIXES :
.SUFFIXES : .o .c .s .mix .dep
#--------------------------------------------------------------------------
# C code inference rules
#----------------------------------------------------------------------------
%.o:%.c
@echo ---------------------------------------------------------------
@echo OBJECT $(@F)
$(ARMCC) $(CFLAGS) $(OBJ_FILE) $(SRC_FILE)
@echo ---------------------------------------------------------------
%.mix:%.c
@echo ---------------------------------------------------------------
@echo OBJECT $(@F)
$(ARMCC) -S -fs $(CFLAGS) $(INC) $(OBJ_FILE) $<
@echo ---------------------------------------------------------------
#-------------------------------------------------------------------------------
# Assembly code inference rules
#-------------------------------------------------------------------------------
%.o:%.s
@echo ---------------------------------------------------------------
@echo OBJECT $(@F)
$(ARMCC) -ansic -E $(AFLAGS) $(ARMASM) $< > $*.i
$(ASM) $(AFLAGS) $(OBJ_FILE) $*.i
@echo ---------------------------------------------------------------
#-------------------------------------------------------------------------------
# Depend file inference rules
#-------------------------------------------------------------------------------
%.dep:%.c
@echo ---------------------------------------------------------------
$(ARMCC) -ansic -M $< > $*.dep
@echo ---------------------------------------------------------------
#=====================================================================
# MODULE SPECIFIC RULES
#=====================================================================
#----------------------------------------------------------------------------
# Lib file targets
#----------------------------------------------------------------------------
$(TARGET).$(MODULE) : $(TARGET).$(EXETYPE)
@echo ---------------------------------------------------------------
@echo TARGET $@
$(HEXTOOL) $(TARGET).$(EXETYPE) $(BINFORMAT) $(OUTPUT) $(TARGET).$(MODULE)
$(TARGET).$(EXETYPE) : $(OBJS)
@echo ---------------------------------------------------------------
@echo TARGET $@
$(LD) $(INFO) $(LINK_OPT) $(LIST) $(LINK_CMD) $(TARGET).$(EXETYPE) $(OBJS)
# --------------------------------------------
# C file dependency list
# --------------------------------------------
ifeq ($(MAKECMDGOALS),)
-include $(C_OBJS:.o=.dep)
else
ifeq ($(filter all,$(MAKECMDGOALS)),all)
-include $(C_OBJS:.o=.dep)
endif
endif
# --------------------------------------------
# ASM file dependency list
# --------------------------------------------
bootblock.o : bootblock.s
bootblock.o : system.h |
TARGET = Test4#
SCLFILE = system.scl#
C_OBJS = mainapp.o raminit.o
A_OBJS = bootblock.o
APP_PATH = .# |
# set the search path
vpath %.c $(APP_PATH)
vpath %.s $(APP_PATH) |
#-------------------------------------------------------------------------------
# Target file name and type definitions
#-------------------------------------------------------------------------------
EXETYPE = elf# # Target image file format
MODULE = bin# # Binary module extension
#-------------------------------------------------------------------------------
# Target compile time symbol definitions
#-------------------------------------------------------------------------------
ARMASM = -D_ARM_ASM_#
#-------------------------------------------------------------------------------
# Software tool and environment definitions
#-------------------------------------------------------------------------------
ARMBIN = $(ARMHOME)/Bin#
ARMBIN := $(subst \,/,$(ARMBIN))#
ARMCC = $(ARMBIN)/armcc# # ARM ADS ARM 32-bit inst. set ANSI C compiler
ASM = $(ARMBIN)/armasm# # ARM ADS assembler
LD = $(ARMBIN)/armlink# # ARM ADS linker
HEXTOOL = $(ARMBIN)/fromelf# # ARM ADS utility to create hex file from image
OBJ_CMD = -o# # Command line option to specify output filename |
#----------------------------------------------------------------------------
# Default target
#----------------------------------------------------------------------------
.PHONY : all
all : startup $(TARGET).$(MODULE) complete
.PHONY : startup
startup:
@echo ---------------------------------------------------------------
@echo Compile startup
@echo ---------------------------------------------------------------
.PHONY : complete
complete:
@echo ---------------------------------------------------------------
@echo All have been done
@echo --------------------------------------------------------------- |
#----------------------------------------------------------------------------
# Lib file targets
#----------------------------------------------------------------------------
$(TARGET).$(MODULE) : $(TARGET).$(EXETYPE)
@echo ---------------------------------------------------------------
@echo TARGET $@
$(HEXTOOL) $(TARGET).$(EXETYPE) $(BINFORMAT) $(OUTPUT) $(TARGET).$(MODULE)
$(TARGET).$(EXETYPE) : $(OBJS)
@echo ---------------------------------------------------------------
@echo TARGET $@
$(LD) $(INFO) $(LINK_OPT) $(LIST) $(LINK_CMD) $(TARGET).$(EXETYPE) $(OBJS) |
#=====================================================================
# DEFAULT SUFFIX RULES
#=====================================================================
SRC_FILE = $(@F:.o=.c)# # Input source file specification
OBJ_FILE = $(OBJ_CMD) $(@F)# # Output object file specification
.SUFFIXES :
.SUFFIXES : .o .c .s .mix .dep |
#--------------------------------------------------------------------------
# C code inference rules
#----------------------------------------------------------------------------
%.o:%.c
@echo ---------------------------------------------------------------
@echo OBJECT $(@F)
$(ARMCC) $(CFLAGS) $(OBJ_FILE) $(SRC_FILE)
@echo ---------------------------------------------------------------
%.mix:%.c
@echo ---------------------------------------------------------------
@echo OBJECT $(@F)
$(ARMCC) -S -fs $(CFLAGS) $(INC) $(OBJ_FILE) $<
@echo --------------------------------------------------------------- |
#-------------------------------------------------------------------------------
# Assembly code inference rules
#-------------------------------------------------------------------------------
%.o:%.s
@echo ---------------------------------------------------------------
@echo OBJECT $(@F)
$(ARMCC) -ansic -E $(AFLAGS) $(ARMASM) $< > $*.i
$(ASM) $(AFLAGS) $(OBJ_FILE) $*.i
@echo --------------------------------------------------------------- |
#-------------------------------------------------------------------------------
# Depend file inference rules
#-------------------------------------------------------------------------------
%.dep:%.c
@echo ---------------------------------------------------------------
$(ARMCC) -ansic -M $< > $*.dep
@echo --------------------------------------------------------------- |
mainapp.o: mainapp.c
mainapp.o: system.h |
# --------------------------------------------
# C file dependency list
# --------------------------------------------
ifeq ($(MAKECMDGOALS),)
-include $(C_OBJS:.o=.dep)
else
ifeq ($(filter all,$(MAKECMDGOALS)),all)
-include $(C_OBJS:.o=.dep)
endif
endif |
#----------------------------------------------------------------------------
# Clean target
#----------------------------------------------------------------------------
# The object subdirectory, target image file, and target hex file are deleted.
.PHONY : clean
clean :
@echo ---------------------------------------------------------------
@echo CLEAN
-rm -f *.o
-rm -f *.i
-rm -f *.dep
-rm -f $(TARGET).$(EXETYPE)
-rm $(TARGET).$(MODULE)
-rm $(TARGET).map
@echo ---------------------------------------------------------------
|
关键词 |
用途 |
define |
定义一个“数据包”,是用enddef做结尾,可以包含多行的命令。 |
ifeq/ifneq |
条件判断,可以搭配else使用,endif结尾。原型:ifeq(Arg1,Arg2)。 |
ifdef/ifndef |
变量是否定义的条件判断,可以搭配else使用,endif结尾。原型:ifdef Var。 |
= |
变量赋值语句。如果右值包含另一个变量,则可以在后面定义这个变量。 |
:= |
变量赋值语句。如果右值包含另一个变量,则只能引用已定义的变量。 |
?= |
条件赋值语句。如果此变量未定义才重新赋值。 |
+= |
为当前变量追加内容。 |
% |
通配符 |
vpath |
设置搜索路径,原型vpath %.x |
\ |
换行符 |
@ |
放在命令前面隐藏命令输出 |
- |
放在命令前面忽略命令错误 |
: |
依赖规则定义符,使用方式:目标:依赖 |
override |
用来指示即便此变量是由make的命令行参数设置的,也使用新的赋值。因为默认情况下Makefile中对这个变量的赋值会被忽略。 |
.PHONY |
显式声明伪目标 |
.SUFFIXES |
声明扩展名 |
函数原型 |
描述 |
$(subst |
把字串 |
$(patsubst |
查找 |
$(strip |
去掉 |
$(findstring |
在字串 |
$(filter |
以 |
$(filter-out |
以 |
$(sort
|
给字符串
|
$(word |
取字符串 |
$(wordlist |
从字符串 |
$(words |
统计 |
$(firstword |
取字符串 |
$(dir |
从文件名序列 |
$(notdir |
从文件名序列 |
$(suffix |
从文件名序列 |
$(basename |
从文件名序列 |
$(addsuffix |
把后缀 |
$(addprefix |
把前缀 |
$(join |
把 |
$(foreach ,
|
把参数
|
$(if |
$(if |
$(call |
call函数是唯一一个可以用来创建新的参数化的函数。我们可以写一个非常复杂的表达式,这个表达式中,我们可以定义许多参数,然后我们可以用call函数来向这个表达式传递参数。当make执行这个函数时, |
$(origin |
origin函数不像其它的函数,他并不操作变量的值,他只是告诉我们这个变量是哪里来的。
“undefined”
如果
default”
如果
“environment”
如果
file”
如果
“command line”
如果
“override”
如果
“automatic”
如果 |
$(error |
产生一个致命的错误, |
$(warning |
输出一段警告信息,而make继续执行。 |
$(shell |
使用Shell执行 |
$@ |
表示规则中的目标文件集Target。在模式(即"%")规则中,如果有多个目标,那么,"$@"就是匹配于目标中模式定义的集合。 |
$% |
仅当目标是函数库文件中,表示规则中的目标成员名。例如,如果一个目标是"foo.a(bar.o)",那么,"$%"就是"bar.o","$@"就是"foo.a"。如果目标不是函数库文件(Unix下是[.a],Windows下是[.lib]),其值为空。 |
$< |
依赖目标中的第一个目标名字。如果依赖目标是以模式(即"%")定义的,那么"$<"将是符合模式的一系列的文件集。注意,其是一个一个取出来的。 |
$? |
所有同目标相比更新的依赖目标的集合。以空格分隔。 |
$^ |
所有的依赖目标的集合。以空格分隔。如果在依赖目标中有多个重复的,那个这个变量会去除重复的依赖目标,只保留一份。 |
$+ |
这个变量很像"$^",也是所有依赖目标的集合。只是它不去除重复的依赖目标。 |
$* |
这个变量表示目标模式中"%"及其之前的部分。如果目标是"dir/a.foo.b",并且目标的模式是"a.%.b",那么,"$*"的值就是"dir/a.foo"。这个变量对于构造有关联的文件名是比较有效。如果目标中没有模式的定义,那么"$*"也就不能被推导出,但是,如果目标文件的后缀是make所识别的,那么"$*"就是除了后缀的那一部分。例如:如果目标是"foo.c",因为".c"是make所能识别的后缀名,所以,"$*"的值就是"foo"。这个特性是GNU make的,很有可能不兼容于其它版本的make,所以,我们应该尽量避免使用"$*",除非是在隐含规则或是静态模式中。如果目标中的后缀是make所不能识别的,那么"$*"就是空值。 |