Tinyos Makerules解读

Makerules 文件解读 位于/opt/tinyos-2.1.2/support/make

 1 #-*-Makefile-*- vim:syntax=make
 2 #$Id: Makerules,v 1.6 2008/09/26 20:13:58 klueska Exp $
 3 
 4 # @author Cory Sharp <cssharp@eecs.berkeley.edu>
 5 
 6 ### --- This makefile requires GNU Make version 3.80 or newer.
 7 
 8 
 9 ### ---
10 ### --- Prepare variables
11 ### ---
12 
13 
14 #  Get TOSDIR from ncc if it isn't set already.
15 ifndef TOSDIR
16 TOSDIR := $(shell ncc -print-tosdir)
17 endif

15-17行为tos文件夹路径设置函数

 

 1 #  Mung MAKERULES for Cygwin; see the warning below for more details.
 2 ifneq ($(findstring \,$(MAKERULES)),)
 3 MAKERULES := $(subst \,/,$(MAKERULES))
 4 define BACKSLASH_WARNING
 5 warning, MAKERULES contains backslashes.
 6 
 7     The environment variable MAKERULES contains backslashes \'s.  This can
 8     cause shell scripts including ones in this make system to fail in
 9     strange ways.  I've changed those to forward slashes for you for this
10     build.  However, you are strongly encouraged to respecify MAKERULES as
11     either a standard unix-style path or as a mixed-style path where the
12     backslashes are replaced with forward slashes /'s.
13 
14 endef
15 $(warning $(BACKSLASH_WARNING))
16 endif

这段代码作用是在cygwin下,文件路径格式修改。将路径中的'\'替换为'/'。

 

1 #  Deduce TINYOS_MAKE_PATH, the path to this file, if it's not defined already.
2 ifndef TINYOS_MAKE_PATH
3   ifdef MAKERULES
4     TINYOS_MAKE_PATH := $(dir $(MAKERULES))
5     TINYOS_MAKE_PATH := $(TINYOS_MAKE_PATH:%/=%)
6   else
7     TINYOS_MAKE_PATH := $(TOSDIR)/../support/make
8   endif
9 endif

4-5行是windows下make路径设置,7行是linux下make路径。

 

 1 #  Use a default Makelocal if it's not defined already.
 2 TINYOS_MAKELOCAL ?= $(TINYOS_MAKE_PATH)/Makelocal
 3 
 4 #  Use a default Makedefaults if it's not defined already.
 5 TINYOS_MAKEDEFAULTS ?= $(TINYOS_MAKE_PATH)/Makedefaults
 6 
 7 #  Allow users to specify additional directories to find TOSMake files.
 8 TOSMAKE_PATH += $(TINYOS_MAKE_PATH)
 9 
10 #  Save makecmdgoals (a read only var) to goals so that we can modify it.
11 GOALS += $(MAKECMDGOALS)

11行中变量MAKECMDGOALS是make指令后的目标参数。

 

 1 ### ---
 2 ### --- Define make functions.
 3 ### --- (Lord, this is ugly. I want a real scripting language so bad.)
 4 ### ---
 5 ### --- The functions a user will generally be interested in are
 6 ### ---   TOSMake_include(file)
 7 ### ---   TOSMake_include_platform(dir)
 8 ### ---
 9 
10 #  names(words)
11 #    Produce option names, like junk from /path/to/junk.target.
12 names = $(sort $(basename $(notdir $(1))))

12行目的是先获取文件名,然后获取文件名前缀,最后按字母排序输出。

 

1 #  TOSMake_find(file_or_dir)
2 #    Search for file_or_dir within TOSMAKE_PATH.  For the special case of
3 #    initializing TOSMAKE_PATH itself, this function does not search 
4 #    TOSMAKE_PATH if file_or_dir begins with +.
5 sh_search = for a in $(TOSMAKE_PATH); do [ -e "$$a/$$n" ] && echo "$$a/$$n" && break; done
6 TOSMake_find = $(if $(filter +%,$(1)),$(1:+%=%),$(shell n="$(1)"; $(sh_search)))

TOSMAKE_find是搜索到的文件名。其中路径带有“+”符号的为获取TOSMAKE_PATH目录下的指定文件,函数定义在118行,在149行执行文件包含。

不带“+”符号返回的为make子目录中的文件,函数定义在106行,然后在指定的target文件中执行文件包含。

 

1 #  TOSMake_makelist(dir,extension)
2 #    Get a list of files with the given extension from a directory which MUST
3 #    be a subdir under TOSMAKE_PATH.
4 TOSMake_makelist = $(wildcard $(call TOSMake_find,$(1))/*.$(2))

此段代码是获取make文件列表,带入参数是 “$(1))/*.$(2)”,其中$(1)是文件路径参数,$(2)是文件后缀名参数。

 

1 #  TOSMake_include(file)
2 #    Include a makefile which MUST be in a dir or subdir under TOSMAKE_PATH.
3 TOSMake_include = $(eval include $(call TOSMake_find,$(1)))

执行包含在TOSMAKE_PATH目录下的文件$(1)。

 

1 #  TOSMake_extra_targets(name)
2 #    Create a default make targets for a TOSMake extra full with its possible
3 #    options afterward.
4 define TOSMake_extra_targets
5 $(subst :,%,$(1)): FORCE
6     @:
7 endef

当传入make目标无效时调用此函数,执行空操作。

第五行的FORCE是定义的为目标,常用在没有命令和依赖的规则。(如果一个规则只有目标,没有依赖,也没有命令,而且该规则的目标也不存在,则make认为只要该规则运行,其目标就已被更新。这意味着所有以这种规则的目标为依赖的规则,它们的命令将总被执行。)

 

 1 #  TOSMake_include_dir(dir)
 2 #    Pull in .extras and .targets from a directory which MUST be a subdir
 3 #    under TOSMAKE_PATH.  Create default extra rules as necessary, etc.
 4 TOSMake_include_dir = $(eval $(call TOSMake_include_dir_define,$(1)))
 5 define TOSMake_include_dir_define
 6 $(eval NEW_EXTRAS := $(call TOSMake_makelist,$(1),extra))
 7 $(eval NEW_TARGETS := $(call TOSMake_makelist,$(1),target))
 8 $(eval VALID_EXTRAS += $(NEW_EXTRAS))
 9 $(eval VALID_TARGETS += $(NEW_TARGETS))
10 $(eval EXTRAS = $(filter $(call names,$(VALID_EXTRAS)),$(GOALS)))
11 $(eval TARGETS = $(filter $(call names,$(VALID_TARGETS)),$(GOALS)))
12 $(eval OTHERS = $(filter-out $(EXTRAS) $(TARGETS),$(GOALS)))
13 $(foreach file,$(NEW_EXTRAS) $(NEW_TARGETS),$(if $(filter $(call names,$(file)),$(GOALS)),$(eval include $(file))))
14 endef

这段代码目的是查找有效的EXTRA和TARGET,并包含这些有效文件。参数$(1)是文件查找路径。

 

1 TOSMake_accum_dir = $(eval $(call TOSMake_accum_dir_define,$(1)))
2 define TOSMake_accum_dir_define
3 $(eval NEW_EXTRAS := $(call TOSMake_makelist,$(1),extra))
4 $(eval NEW_TARGETS := $(call TOSMake_makelist,$(1),target))
5 $(eval VALID_EXTRAS += $(NEW_EXTRAS))
6 $(eval VALID_TARGETS += $(NEW_TARGETS))
7 $(eval TARGETS = $(filter $(call names,$(VALID_TARGETS)),$(GOALS)))
8 endef

此代码上方的类似,不同的地方是查找的路径不同,本代码并不执行文件包含操作。

 

1 #  TOSMake_include_platform(dir)
2 #    Pull in a directory as a new TOSMake platform, which MUST be a subdir of
3 #    TOSMAKE_PATH.  A platform directory must also have a .rules file, which
4 #    is automatically evaluated.
5 TOSMake_include_platform=$(eval $(call TOSMake_include_platform_define,$(1)))
6 define TOSMake_include_platform_define
7 $(call TOSMake_include_dir,$(1))
8 $(call TOSMake_include,$(1)/$(1).rules)
9 endef

执行包含target目标指定的文件。例如telosb.target中参数$(1)是msp。

 

1 #  Makelocal comes first to allow overriding Makedefaults.
2 -include $(TINYOS_MAKELOCAL)
3 -include $(TINYOS_MAKEDEFAULTS)
4 
5 PLATFORMDIR ?= $(TOSDIR)/platforms/$(PLATFORM)

第五行中的$(PLATFORM)在目标对应的target文件中被定义,如telosb平台在telosb.target中。

 

1 #  Mark TOSMAKE_PATH with a + so that they're not searched for by TOSMake_find.
2 $(foreach incdir,$(addprefix +,$(TOSMAKE_PATH)),$(call TOSMake_accum_dir,$(incdir)))
3 
4 $(foreach file,$(VALID_EXTRAS),$(if $(filter $(call names,$(file)),$(GOALS)),$(eval include $(file))))
5 $(foreach file,$(VALID_TARGETS),$(if $(filter $(call names,$(file)),$(GOALS)),$(eval include $(file))))
6 
7 #  Make default rules for each extra with full argument
8 $(foreach goal,$(MAKECMDGOALS),$(if $(filter-out $(TARGETS) help,$(goal)),$(eval $(call TOSMake_extra_targets,$(goal)))))

此段代码都是文件包含操作,调用了上面的函数生成。

第2行搜索的make目录下的对应的target和extra文件,和其他目录的文件以“+”区分。

4,5行包含有行2搜索到的有效目标文件。

若目标无效,行8则会调用“TOSMake_extra_targets”函数执行空操作。

 

 1 ### ---
 2 ### --- Define USAGE, print help if necessary or requested, etc.
 3 ### ---
 4 
 5 #  USAGE is printed out when help is requested.  Files other than this should
 6 #  add text to HELP, not USAGE.
 7 define USAGE
 8 
 9 
10 Usage:  make <target> <extras>
11         make <target> help
12 
13         Valid targets: $(call names,$(VALID_TARGETS))
14         Valid extras: $(call names,$(VALID_EXTRAS))
15 $(HELP)
16 
17 endef

此段代码定义的是语法规则显示函数,其中参数$(HELP)在TINYOS_MAKEDEFAULTS中定义。

 

1 #  If no target or an invalid target is specified, print usage.
2 ifeq ($(TARGETS),)
3   ifeq ($(GOALS),)
4     $(error $(USAGE)Please specify a valid target)
5   else
6     $(error $(USAGE)ERROR, "$(GOALS)" does not specify a valid target)
7   endif
8 endif

代码2,3行判断目标是否有效,若无效或不存在则会生成对应的错误信息。

 

 1 #  If the user specifically had help on the command line, don't build any
 2 #  targets, instead display help information and exit with a nice error.
 3 ifeq ($(filter help,$(GOALS)),help)
 4 define USAGE
 5 
 6 
 7 Usage:  make $(TARGETS) <extras>
 8 
 9     Valid targets: $(call names,$(VALID_TARGETS))
10         Valid extras: $(call names,$(VALID_EXTRAS))
11 $(HELP)
12 
13 endef
14 $(error $(USAGE)Thank you)
15 endif

此段代码判断目标是否为help,若成立则输出内部定义的USAGE函数。

 

1 $(COMPONENT).nc:
2     @echo "ERROR: You need to create a top level file called $(COMPONENT).nc, or modify your local Makefile to point to the real name of your top level component."
3     @false
4 
5 .PHONY: FORCE

第1-3行语法规则有待研究。。。。

第五行定义了FORCE伪目标。

 

完整Makerules如下:

  1 #-*-Makefile-*- vim:syntax=make
  2 #$Id: Makerules,v 1.6 2008/09/26 20:13:58 klueska Exp $
  3 
  4 # @author Cory Sharp <cssharp@eecs.berkeley.edu>
  5 
  6 ### --- This makefile requires GNU Make version 3.80 or newer.
  7 
  8 
  9 ### ---
 10 ### --- Prepare variables
 11 ### ---
 12 
 13 
 14 #  Get TOSDIR from ncc if it isn't set already.
 15 ifndef TOSDIR
 16 TOSDIR := $(shell ncc -print-tosdir)
 17 endif
 18 
 19 #  Mung MAKERULES for Cygwin; see the warning below for more details.
 20 ifneq ($(findstring \,$(MAKERULES)),)
 21 MAKERULES := $(subst \,/,$(MAKERULES))
 22 define BACKSLASH_WARNING
 23 warning, MAKERULES contains backslashes.
 24 
 25     The environment variable MAKERULES contains backslashes \'s.  This can
 26     cause shell scripts including ones in this make system to fail in
 27     strange ways.  I've changed those to forward slashes for you for this
 28     build.  However, you are strongly encouraged to respecify MAKERULES as
 29     either a standard unix-style path or as a mixed-style path where the
 30     backslashes are replaced with forward slashes /'s.
 31 
 32 endef
 33 $(warning $(BACKSLASH_WARNING))
 34 endif
 35 
 36 #  Deduce TINYOS_MAKE_PATH, the path to this file, if it's not defined already.
 37 ifndef TINYOS_MAKE_PATH
 38   ifdef MAKERULES
 39     TINYOS_MAKE_PATH := $(dir $(MAKERULES))
 40     TINYOS_MAKE_PATH := $(TINYOS_MAKE_PATH:%/=%)
 41   else
 42     TINYOS_MAKE_PATH := $(TOSDIR)/../support/make
 43   endif
 44 endif
 45 
 46 #  Use a default Makelocal if it's not defined already.
 47 TINYOS_MAKELOCAL ?= $(TINYOS_MAKE_PATH)/Makelocal
 48 
 49 #  Use a default Makedefaults if it's not defined already.
 50 TINYOS_MAKEDEFAULTS ?= $(TINYOS_MAKE_PATH)/Makedefaults
 51 
 52 #  Allow users to specify additional directories to find TOSMake files.
 53 TOSMAKE_PATH += $(TINYOS_MAKE_PATH)
 54 
 55 #  Save makecmdgoals (a read only var) to goals so that we can modify it.
 56 GOALS += $(MAKECMDGOALS)
 57 
 58 #  Extract user options from goals of the form opt,arg, transform to opt=arg,
 59 #  and evaluate.  Then, reduce GOALS to have the args removed.
 60 OptRE := [,.]
 61 GoalOpts := $(shell perl -e 'print join " ", map {s{^(.*?)$(OptRE)}{\U$$1=};$$_} grep /$(OptRE)/, split /\s+/, "$(GOALS)";')
 62 GOALS := $(shell perl -e '$$_="$(GOALS)"; s{$(OptRE)\S*}{}g; print;')
 63 $(foreach opt,$(GoalOpts),$(eval $(opt)))
 64 
 65 
 66 ### ---
 67 ### --- Define make functions.
 68 ### --- (Lord, this is ugly. I want a real scripting language so bad.)
 69 ### ---
 70 ### --- The functions a user will generally be interested in are
 71 ### ---   TOSMake_include(file)
 72 ### ---   TOSMake_include_platform(dir)
 73 ### ---
 74 
 75 #  names(words)
 76 #    Produce option names, like junk from /path/to/junk.target.
 77 names = $(sort $(basename $(notdir $(1))))
 78 
 79 #  TOSMake_find(file_or_dir)
 80 #    Search for file_or_dir within TOSMAKE_PATH.  For the special case of
 81 #    initializing TOSMAKE_PATH itself, this function does not search 
 82 #    TOSMAKE_PATH if file_or_dir begins with +.
 83 sh_search = for a in $(TOSMAKE_PATH); do [ -e "$$a/$$n" ] && echo "$$a/$$n" && break; done
 84 TOSMake_find = $(if $(filter +%,$(1)),$(1:+%=%),$(shell n="$(1)"; $(sh_search)))
 85 
 86 #  TOSMake_makelist(dir,extension)
 87 #    Get a list of files with the given extension from a directory which MUST
 88 #    be a subdir under TOSMAKE_PATH.
 89 TOSMake_makelist = $(wildcard $(call TOSMake_find,$(1))/*.$(2))
 90 
 91 #  TOSMake_include(file)
 92 #    Include a makefile which MUST be in a dir or subdir under TOSMAKE_PATH.
 93 TOSMake_include = $(eval include $(call TOSMake_find,$(1)))
 94 
 95 #  TOSMake_extra_targets(name)
 96 #    Create a default make targets for a TOSMake extra full with its possible
 97 #    options afterward.
 98 define TOSMake_extra_targets
 99 $(subst :,%,$(1)): FORCE
100     @:
101 endef
102 
103 #  TOSMake_include_dir(dir)
104 #    Pull in .extras and .targets from a directory which MUST be a subdir
105 #    under TOSMAKE_PATH.  Create default extra rules as necessary, etc.
106 TOSMake_include_dir = $(eval $(call TOSMake_include_dir_define,$(1)))
107 define TOSMake_include_dir_define
108 $(eval NEW_EXTRAS := $(call TOSMake_makelist,$(1),extra))
109 $(eval NEW_TARGETS := $(call TOSMake_makelist,$(1),target))
110 $(eval VALID_EXTRAS += $(NEW_EXTRAS))
111 $(eval VALID_TARGETS += $(NEW_TARGETS))
112 $(eval EXTRAS = $(filter $(call names,$(VALID_EXTRAS)),$(GOALS)))
113 $(eval TARGETS = $(filter $(call names,$(VALID_TARGETS)),$(GOALS)))
114 $(eval OTHERS = $(filter-out $(EXTRAS) $(TARGETS),$(GOALS)))
115 $(foreach file,$(NEW_EXTRAS) $(NEW_TARGETS),$(if $(filter $(call names,$(file)),$(GOALS)),$(eval include $(file))))
116 endef
117 
118 TOSMake_accum_dir = $(eval $(call TOSMake_accum_dir_define,$(1)))
119 define TOSMake_accum_dir_define
120 $(eval NEW_EXTRAS := $(call TOSMake_makelist,$(1),extra))
121 $(eval NEW_TARGETS := $(call TOSMake_makelist,$(1),target))
122 $(eval VALID_EXTRAS += $(NEW_EXTRAS))
123 $(eval VALID_TARGETS += $(NEW_TARGETS))
124 $(eval TARGETS = $(filter $(call names,$(VALID_TARGETS)),$(GOALS)))
125 endef
126 
127 #  TOSMake_include_platform(dir)
128 #    Pull in a directory as a new TOSMake platform, which MUST be a subdir of
129 #    TOSMAKE_PATH.  A platform directory must also have a .rules file, which
130 #    is automatically evaluated.
131 TOSMake_include_platform=$(eval $(call TOSMake_include_platform_define,$(1)))
132 define TOSMake_include_platform_define
133 $(call TOSMake_include_dir,$(1))
134 $(call TOSMake_include,$(1)/$(1).rules)
135 endef
136 
137 
138 ### ---
139 ### --- Include Makelocal and Makedefaults
140 ### ---
141 
142 #  Makelocal comes first to allow overriding Makedefaults.
143 -include $(TINYOS_MAKELOCAL)
144 -include $(TINYOS_MAKEDEFAULTS)
145 
146 PLATFORMDIR ?= $(TOSDIR)/platforms/$(PLATFORM)
147 
148 #  Mark TOSMAKE_PATH with a + so that they're not searched for by TOSMake_find.
149 $(foreach incdir,$(addprefix +,$(TOSMAKE_PATH)),$(call TOSMake_accum_dir,$(incdir)))
150 
151 $(foreach file,$(VALID_EXTRAS),$(if $(filter $(call names,$(file)),$(GOALS)),$(eval include $(file))))
152 $(foreach file,$(VALID_TARGETS),$(if $(filter $(call names,$(file)),$(GOALS)),$(eval include $(file))))
153 
154 #  Make default rules for each extra with full argument
155 $(foreach goal,$(MAKECMDGOALS),$(if $(filter-out $(TARGETS) help,$(goal)),$(eval $(call TOSMake_extra_targets,$(goal)))))
156 
157 
158 ### ---
159 ### --- Define USAGE, print help if necessary or requested, etc.
160 ### ---
161 
162 #  USAGE is printed out when help is requested.  Files other than this should
163 #  add text to HELP, not USAGE.
164 define USAGE
165 
166 
167 Usage:  make <target> <extras>
168         make <target> help
169 
170         Valid targets: $(call names,$(VALID_TARGETS))
171         Valid extras: $(call names,$(VALID_EXTRAS))
172 $(HELP)
173 
174 endef
175 
176 #  If no target or an invalid target is specified, print usage.
177 ifeq ($(TARGETS),)
178   ifeq ($(GOALS),)
179     $(error $(USAGE)Please specify a valid target)
180   else
181     $(error $(USAGE)ERROR, "$(GOALS)" does not specify a valid target)
182   endif
183 endif
184 
185 #  If the user specifically had help on the command line, don't build any
186 #  targets, instead display help information and exit with a nice error.
187 ifeq ($(filter help,$(GOALS)),help)
188 define USAGE
189 
190 
191 Usage:  make $(TARGETS) <extras>
192 
193     Valid targets: $(call names,$(VALID_TARGETS))
194         Valid extras: $(call names,$(VALID_EXTRAS))
195 $(HELP)
196 
197 endef
198 $(error $(USAGE)Thank you)
199 endif
200 
201 $(COMPONENT).nc:
202     @echo "ERROR: You need to create a top level file called $(COMPONENT).nc, or modify your local Makefile to point to the real name of your top level component."
203     @false
204 
205 .PHONY: FORCE
View Code

 

posted @ 2016-10-05 12:16  ZYVV  阅读(449)  评论(0编辑  收藏  举报