我是靠谱客的博主 儒雅黑裤,这篇文章主要介绍解读Makefile (2),现在分享给大家,希望可以做个参考。

二、Makefile解读: sub-make

Linux各级内核源代码的子目录下都有Makefile,大多数Makefile要嵌入主目录下的Rule.make,Rule.make将识别各个Makefile中所定义的一些变量。变量obj-y表示需要编绎到内核中的目标文件名集合,定义O_TARGET表示将obj-y连接为一个O_TARGET名称的目标文件,定义L_TARGET表示将obj-y合并为一个L_TARGET名称的库文件。同样obj-m表示需要编绎成模块的目标文件名集合。

如果还需进行子目录make,则需要定义subdir-y和subdir-m。在Makefile中,用"obj-$(CONFIG_BINFMT_ELF) += binfmt_elf.o"和"subdir-$(CONFIG_EXT2_FS) += ext2"这种形式自动为obj-y、obj-m、subdir-y、subdir-m添加文件名。有时,情况没有这么单纯,还需要使用条件语句个别对待。Makefile中还有其它一些变量,如mod-subdirs定义了subdir-m以外的所有模块子目录。

Rules.make是如何使make进入子目录的呢?

先来看subdir-y是如何处理的,在Rules.make中,先对subdir-y中的每一个文件名加上前缀"_subdir_"再进行排序生成subdir-list集合,再以它作为目标集,对其中每一个目标产生一个子make,同时将目标名的前缀去掉得到子目录名,作为子make的起始目录参数。subdir-m与subdir-y类似,但情况稍微复杂一些。

由于subdir-y中可能有模块定义,因此利用mod-subdirs变量将subdir-y中模块目录提取出来,再与subdir-m合成一个大的MOD_SUB_DIRS集合。subdir-m的目标所用的前缀是"_modsubdir_"。

一点说明,子目录中的Makefile与Rules.make都没有嵌入.config文件,它是通过主Makefile向下传递MAKEFILES变量完成的。MAKEFILES是make自已识别的一个变量,在执行新的Makefile之前,make会首先加载MAKEFILES所指的文件。在主Makefile中它即指向.config。

三、模块的版本化处理

模块的版本化是内核与模块接口之间进行严格类型匹配的一种方法。当内核配置了CONFIG_MODVERSIONS之后,make dep操作会在include/linux/modules/目录下为各级Makefile中export-objs变量所对应的源文件生成扩展名为.ver的文件。

例如对于kernel/ksyms.c,make用以下命令生成对应的ksyms.ver:

复制代码
1
2
3
4
gcc -E -D__KERNEL__ -D__GENKSYMS__ ksyms.c | /sbin/genksyms -k 2.4.1 > ksyms.ver

-D__GENKSYMS__的作用是使ksyms.c中的EXPORT_SYMBOL宏不进行扩展。genksyms命令识别EXPORT_SYMBOL()中的函数名和对应的原型,再根据其原型计算出该函数的版本号。例如ksyms.c中有一行:

复制代码
1
EXPORT_SYMBOL(kmalloc);

kmalloc原型是:

复制代码
1
void *kmalloc(size_t, int);

genksyms程序对应的输出为:

复制代码
1
2
#define __ver_kmalloc 93d4cfe6 #define kmalloc _set_ver(kmalloc)

在内核符号表和模块中,kmalloc将变成kmalloc_R93d4cfe6。在生成完所有的.ver文件后,make将重建include/linux/modversions.h文件,它包含一系列#include指令行嵌入各个.ver文件。

在编绎内核本身export-objs中的文件时,make会增加一个"-DEXPORT_SYMTAB"编绎标志,它使源文件嵌入modversions.h文件,将EXPORT_SYMBOL宏展开中的函数名字符串进行版本名扩展;同时,它也定义_set_ver()宏为一空操作,使代码中的函数名不受其影响。

在编绎模块时,make会增加"-include=linux/modversion.h -DMODVERSIONS"编绎标志,使模块中代码的函数名得到相应版本扩展。

由于生成.ver文件比较费时,make还为每个.ver创建了一个后缀为.stamp时戳文件。在make dep时,如果其.stamp文件比源文件旧才重新生成.ver文件,否则只是更新.stamp文件时戳。另外,在生成.ver和modversions.h文件时,make都会比较新文件和旧文件的内容,保持它们修改时间为最旧。

四、Rules.make的注释

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
[code:1:974578564b] # # This file contains rules which are shared between multiple Makefiles. # # # False targets. # # .PHONY: dummy # # Special variables which should not be exported # # 取消这些变量通过环境向make子进程传递。 unexport EXTRA_AFLAGS # as 的开关 unexport EXTRA_CFLAGS # cc 的开关 unexport EXTRA_LDFLAGS # ld 的开关 unexport EXTRA_ARFLAGS # ar 的开关 unexport SUBDIRS # unexport SUB_DIRS # 编绎内核需进入的子目录, 等于subdir-y unexport ALL_SUB_DIRS # 所有的子目录 unexport MOD_SUB_DIRS # 编绎模块需进入的子目录 unexport O_TARGET # ld合并的输出对象 unexport ALL_MOBJS # 所有的模块名 unexport obj-y # 编绎成内核的文件集 unexport obj-m # 编绎成模块的文件集 unexport obj-n # unexport obj- # unexport export-objs # 需进行版本处理的文件集 unexport subdir-y # 编绎内核所需进入的子目录 unexport subdir-m # 编绎模块所需进入的子目录 unexport subdir-n unexport subdir- # # Get things started. # first_rule: sub_dirs $(MAKE) all_targets # 在内核编绎子目录中过滤出 可以作为模块的子目录。 both-m := $(filter $(mod-subdirs), $(subdir-y)) SUB_DIRS := $(subdir-y) # 求出总模块子目录 MOD_SUB_DIRS := $(sort $(subdir-m) $(both-m)) # 求出总子目录 ALL_SUB_DIRS := $(sort $(subdir-y) $(subdir-m) $(subdir-n) $(subdir-)) # # Common rules # # 将c文件编绎成汇编文件的规则, $@为目标对象。 %.s: %.c $(CC) $(CFLAGS) $(EXTRA_CFLAGS) $(CFLAGS_$@) -S $< -o $@ # 将c文件生成预处理文件的规则。 %.i: %.c $(CPP) $(CFLAGS) $(EXTRA_CFLAGS) $(CFLAGS_$@) $< > $@ # 将c文件编绎成目标文件的规则, $<为第一个所依赖的对象; # 在目标文件的目录下生成flags文件, strip删除多余的空格, subst将逗号替换成冒号 。 %.o: %.c $(CC) $(CFLAGS) $(EXTRA_CFLAGS) $(CFLAGS_$@) -c -o $@ $< @ ( echo 'ifeq ($(strip $(subst $(comma),:, $(CFLAGS) $(EXTRA_CFLAGS) $(CFLAGS_$@))), $$(strip $$(subst $$(comma),:,$$(CFLAGS) $$(EXTRA_CFLAGS) $$(CFLAGS_$@))))' ; echo ' FILES_FLAGS_UP_TO_DATE += $@' ; echo ' endif' ) > $(dir $@)/.$(notdir $@).flags # 汇编文件生成目标文件的规则。 %.o: %.s $(AS) $(AFLAGS) $(EXTRA_CFLAGS) -o $@ $< # Old makefiles define their own rules for compiling .S files, # but these standard rules are available for any Makefile that # wants to use them. Our plan is to incrementally convert all # the Makefiles to these standard rules. -- rmk, mec ifdef USE_STANDARD_AS_RULE # 汇编文件生成预处理文件的标准规则。 %.s: %.S $(CPP) $(AFLAGS) $(EXTRA_AFLAGS) $(AFLAGS_$@) $< > $@ # 汇编文件生成目标文件的标准规则。 %.o: %.S $(CC) $(AFLAGS) $(EXTRA_AFLAGS) $(AFLAGS_$@) -c -o $@ $< endif # c文件生成调试列表文件的规则, $*扩展为目标的主文件名。 %.lst: %.c $(CC) $(CFLAGS) $(EXTRA_CFLAGS) $(CFLAGS_$@) -g -c -o $*.o $< $(TOPDIR)/scripts/makelst $* $(TOPDIR) $(OBJDUMP) # # # all_targets: $(O_TARGET) $(L_TARGET) # # Rule to compile a set of .o files into one .o file # ifdef O_TARGET $(O_TARGET): $(obj-y) rm -f $@ # $^扩展为全部依赖对象, 如果obj-y为空就生成一个同名空的库文件。 ifneq "$(strip $(obj-y))" "" $(LD) $(EXTRA_LDFLAGS) -r -o $@ $(filter $(obj-y), $^) else $(AR) rcs $@ endif # 生成flags文件的shell语句。 @ ( echo 'ifeq ($(strip $(subst $(comma),:, $(EXTRA_LDFLAGS) $(obj-y))),$$(strip $$(subst $$(comma),:,$$(EXTRA_LDFLAGS) $$(obj-y))))' ; echo 'FILES_FLAGS_UP_TO_DATE += $@' ; echo 'endif' ) > $(dir $@)/.$(notdir $@).flags endif # O_TARGET # # Rule to compile a set of .o files into one .a file # # 将obj-y组合成库 L_TARGET的方法。 ifdef L_TARGET $(L_TARGET): $(obj-y) rm -f $@ $(AR) $(EXTRA_ARFLAGS) rcs $@ $(obj-y) @ ( echo 'ifeq ($(strip $(subst $(comma),:,$(EXTRA_ARFLAGS) $(obj-y))),$$(strip $$(subst $$(comma), :,$$(EXTRA_ARFLAGS) $$(obj-y))))' ; echo 'FILES_FLAGS_UP_TO_DATE += $@' ; echo 'endif' ) > $(dir $@)/.$(notdir $@).flags endif # # This make dependencies quickly # # wildcard为查找目录中的文件名的宏。 fastdep: dummy $(TOPDIR)/scripts/mkdep $(wildcard *.[chS] local.h.master) > .depend ifdef ALL_SUB_DIRS # 将ALL_SUB_DIRS中的目录名 加上前缀_sfdep_作为目标运行子make, 并将ALL_SUB_DIRS 通过 # 变量_FASTDEP_ALL_SUB_DIRS传递给子make。 $(MAKE) $(patsubst %,_sfdep_%,$(ALL_SUB_DIRS)) _FASTDEP_ALL_SUB_DIRS="$(ALL_SUB_DIRS)" endif ifdef _FASTDEP_ALL_SUB_DIRS # 与上一段相对应, 定义子目录目标, 并将目标名还原为目录名,进入该子目录make。 $(patsubst %,_sfdep_%,$(_FASTDEP_ALL_SUB_DIRS)): $(MAKE) -C $(patsubst _sfdep_%,%,$@) fastdep endif # # A rule to make subdirectories # # 下面2段完成内核编绎子目录中的make。 subdir-list = $(sort $(patsubst %,_subdir_%,$(SUB_DIRS))) sub_dirs: dummy $(subdir-list) ifdef SUB_DIRS $(subdir-list) : dummy $(MAKE) -C $(patsubst _subdir_%,%,$@) endif # # A rule to make modules # # 求出有效的模块文件表。 ALL_MOBJS = $(filter-out $(obj-y), $(obj-m)) ifneq "$(strip $(ALL_MOBJS))" "" # 取主目录TOPDIR到当前目录的路径。 PDWN=$(shell $(CONFIG_SHELL) $(TOPDIR)/scripts/pathdown.sh) endif unexport MOD_DIRS MOD_DIRS := $(MOD_SUB_DIRS) $(MOD_IN_SUB_DIRS) # 编绎模块时,进入模块子目录的方法。 ifneq "$(strip $(MOD_DIRS))" "" .PHONY: $(patsubst %,_modsubdir_%,$(MOD_DIRS)) $(patsubst %,_modsubdir_%, $(MOD_DIRS)) : dummy $(MAKE) -C $(patsubst _modsubdir_%,%,$@) modules # 安装模块时,进入模块子目录的方法。 .PHONY: $(patsubst %,_modinst_%,$(MOD_DIRS)) $(patsubst %,_modinst_%,$(MOD_DIRS)) : dummy $(MAKE) -C $(patsubst _modinst_%,%,$@) modules_install endif # make modules 的入口。 .PHONY: modules modules: $(ALL_MOBJS) dummy $(patsubst %,_modsubdir_%,$(MOD_DIRS)) .PHONY: _modinst__ # 拷贝模块的过程。 _modinst__: dummy ifneq "$(strip $(ALL_MOBJS))" "" mkdir -p $(MODLIB)/kernel/$(PDWN) cp $(ALL_MOBJS) $(MODLIB)/kernel/$(PDWN) endif # make modules_install 的入口, 进入子目录安装。 .PHONY: modules_install modules_install: _modinst__ $(patsubst %,_modinst_%,$(MOD_DIRS)) # # A rule to do nothing # dummy: # # This is useful for testing # script: $(SCRIPT) # # This sets version suffixes on exported symbols # Separate the object into "normal" objects and "exporting" objects # Exporting objects are: all objects that define symbol tables # ifdef CONFIG_MODULES # list-multi列出那些由 多个文件复合而成的模块; # 从编绎文件表和模块 文件表中过滤出复合模块名。 multi-used := $(filter $(list-multi), $(obj-y) $(obj-m)) # 取复合模块的构成表。 multi-objs := $(foreach m, $(multi-used), $($(basename $(m))-objs)) # 求出需进行编译的总模块表。 active-objs := $(sort $(multi-objs) $(obj-y) $(obj-m)) ifdef CONFIG_MODVERSIONS ifneq "$(strip $(export-objs))" "" # 如果有需要进行版本化的文件。 MODINCL = $(TOPDIR)/include/linux/modules # The -w option (enable warnings) for genksyms will return here in 2.1 # So where has it gone? # # Added the SMP separator to stop module accidents between uniprocessor # and SMP Intel boxes - AC - from bits by Michael Chastain # ifdef CONFIG_SMP genksyms_smp_prefix := -p smp_ else genksyms_smp_prefix := endif # 从源文件计算版本文件的规则。 $(MODINCL)/%.ver: %.c @if [ ! -r $ (MODINCL)/$*.stamp -o $(MODINCL) /$*.stamp -ot $< ]; then echo '$(CC) $(CFLAGS) -E -D__GENKSYMS__ $<'; echo '| $(GENKSYMS) $(genksyms_smp_prefix) -k $(VERSION).$(PATCHLEVEL).$(SUBLEVEL) > $@.tmp'; $(CC) $(CFLAGS) -E -D__GENKSYMS__ $< | $(GENKSYMS) $(genksyms_smp_prefix) -k $(VERSION).$(PATCHLEVEL).$(SUBLEVEL) > $@.tmp; if [ -r $@ ] && cmp -s $@ $@.tmp; then echo $@ is unchanged; rm -f $@.tmp; else echo mv $@.tmp $@; mv -f $@.tmp $@; fi; fi; touch $(MODINCL)/$*.stamp # 将版本处理源文件的扩展名改为.ver, 并加上完整的路径名, 它们依赖于autoconf.h?br>?br>$(addprefix $(MODINCL)/,$(export-objs:.o=.ver)): $(TOPDIR)/include/linux/autoconf.h # updates .ver files but not modversions.h # 通过fastdep, 逐个生成export-objs对应的版本文件。 fastdep: $(addprefix $(MODINCL)/, $(export-objs:.o=.ver)) # updates .ver files and modversions.h like before (is this needed?) # make dep过程的入口 dep: fastdep update-modverfile endif # export-objs # update modversions.h, but only if it would change # 刷新版本文件的过程。 update-modverfile: @(echo "#ifndef _LINUX_MODVERSIONS_H"; echo "#define _LINUX_MODVERSIONS_H"; echo "#include <linux/modsetver.h>"; cd $(TOPDIR)/include/linux/modules; for f in *.ver; do if [ -f $$f ]; then echo "#include <linux/modules/$${f}>"; fi; done; echo "#endif"; ) > $(TOPDIR)/include /linux/modversions.h.tmp @if [ -r $(TOPDIR)/include /linux/modversions.h ] && cmp -s $(TOPDIR)/include/linux /modversions.h $(TOPDIR)/include/linux /modversions.h.tmp; then echo $(TOPDIR)/include/linux /modversions.h was not updated; rm -f $(TOPDIR)/include /linux/modversions.h.tmp; else echo $(TOPDIR)/include /linux/modversions.h was updated; mv -f $(T OPDIR)/include/linux /modversions.h.tmp $(TOPDIR)/include /linux/modversions.h; fi $(active-objs): $(TOPDIR)/include/linux/modversions.h else # 如果没有配置版本化,modversions.h的内容。 $(TOPDIR)/include/linux/modversions.h: @echo "#include <linux/modsetver.h>" > $@ endif # CONFIG_MODVERSIONS ifneq "$(strip $(export-objs))" "" # 版本化目标文件的编绎方法。 $(export-objs): $(export-objs:.o=.c) $(TOPDIR)/include/linux/modversions.h $(CC) $(CFLAGS) $(EXTRA_CFLAGS) $(CFLAGS_$@) -DEXPORT_SYMTAB -c $(@:.o=.c) @ ( echo 'ifeq ($(strip $(subst $(comma),:, $(CFLAGS) $(EXTRA_CFLAGS) $(CFLAGS_$@) -DEXPORT_SYMTAB)), $$(strip $$(subst $$(comma),:, $$(CFLAGS) $$(EXTRA_CFLAGS) $$(CFLAGS_$@) -DEXPORT_SYMTAB)))' ; echo 'FILES_FLAGS_UP_TO_DATE += $@' ; echo 'endif' ) > $(dir $@)/.$(notdir $@).flags endif endif # CONFIG_MODULES # # include dependency files if they exist # # 嵌入源文件之间的依赖关系。 ifneq ($(wildcard .depend),) include .depend endif # 嵌入头文件之间的依赖关系。 ifneq ($(wildcard $(TOPDIR) /.hdepend),) include $(TOPDIR)/.hdepend endif # # Find files whose flags have changed and force recompilation. # For safety, this works in the converse direction: # every file is forced, except those whose flags are positively up-to-date. # # 已经更新过的文件列表。 FILES_FLAGS_UP_TO_DATE := # For use in expunging commas from flags, which mung our checking. comma = , # 将当前目录下所有flags文件嵌入。 FILES_FLAGS_EXIST := $(wildcard .*.flags) ifneq ($(FILES_FLAGS_EXIST),) include $(FILES_FLAGS_EXIST) endif # 将无需更新的文件从总的对象中删除。 FILES_FLAGS_CHANGED := $(strip $(filter-out $(FILES_FLAGS_UP_TO_DATE), $(O_TARGET) $(L_TARGET) $(active-objs) )) # A kludge: .S files don't get flag dependencies (yet), # because that will involve changing a lot of Makefiles. Also # suppress object files explicitly listed in $(IGNORE_FLAGS_OBJS). # This allows handling of assembly files that get translated into # multiple object files (see arch/ia64/lib/idiv.S, for example). # # 将由汇编文件生成的目件文件从 FILES_FLAGS_CHANGED删除。 FILES_FLAGS_CHANGED := $(strip $(filter-out $(patsubst %.S, %.o, $(wildcard *.S) $(IGNORE_FLAGS_OBJS)), $(FILES_FLAGS_CHANGED))) # 将FILES_FLAGS_CHANGED设为目标。 ifneq ($(FILES_FLAGS_CHANGED),) $(FILES_FLAGS_CHANGED): dummy endif </pre>

(T117)

最后

以上就是儒雅黑裤最近收集整理的关于解读Makefile (2)的全部内容,更多相关解读Makefile内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(61)

评论列表共有 0 条评论

立即
投稿
返回
顶部