ÖÚËùÖÜÖª£¬Linux ÄÚºËÊÇÓÉ·Ö²¼ÔÚÈ«ÇòµÄ Linux °®ºÃÕß¹²Í¬¿ª·¢µÄ£¬Linux ÄÚºËÿÌ춼ÃæÁÙ×ÅÐí¶àеı仯¡£µ«ÊÇ£¬Linux Äں˵Ä×éÖ¯²¢Ã»ÓгöÏÖ»ìÂÒµÄÏÖÏ󣬷´¶øÏԵ÷dz£µÄ¼ò½à£¬¶øÇÒ¾ßÓкܺõÄÀ©Õ¹ÐÔ£¬¿ª·¢ÈËÔ±¿ÉÒԺܷ½±ãµÄÏò Linux ÄÚºËÖÐÔö¼ÓеÄÄÚÈÝ¡£ÔÒòÖ®Ò»¾ÍÊÇ Linux ²ÉÓÃÁËÄ£¿é»¯µÄÄÚºËÅäÖÃϵͳ£¬´Ó¶ø±£Ö¤ÁËÄں˵ÄÀ©Õ¹ÐÔ¡£
±¾ÎÄÊ×ÏÈ·ÖÎöÁË Linux ÄÚºËÖеÄÅäÖÃϵͳ½á¹¹£¬È»ºó£¬½âÊÍÁË Makefile ºÍÅäÖÃÎļþµÄ¸ñʽÒÔ¼°ÅäÖÃÓï¾äµÄº¬Ò壬×îºó£¬Í¨¹ýÒ»¸ö¼òµ¥µÄÀý×Ó--TEST Driver£¬¾ßÌå˵Ã÷ÈçºÎ½«×ÔÐпª·¢µÄ´úÂë¼ÓÈëµ½ Linux ÄÚºËÖС£ÔÚÏÂÃæµÄÎÄÕÂÖУ¬²»¿ÉÄܽâÊÍËùÓеŦÄܺÍÃüÁֻ¶ÔÄÇЩ³£ÓõĽøÐнâÊÍ£¬ÖÁÓÚÄÇЩûÓÐÌÖÂÛµ½µÄ£¬Çë¶ÁÕ߲ο¼ºóÃæµÄ²Î¿¼ÎÄÏס£
1£® ÅäÖÃϵͳµÄ»ù±¾½á¹¹
LinuxÄں˵ÄÅäÖÃϵͳÓÉÈý¸ö²¿·Ö×é³É£¬·Ö±ðÊÇ£º
Makefile£º·Ö²¼ÔÚ Linux ÄÚºËÔ´´úÂëÖÐµÄ Makefile£¬¶¨Òå Linux Äں˵ıàÒë¹æÔò£»
ÅäÖÃÎļþ£¨config.in£©£º¸øÓû§ÌṩÅäÖÃÑ¡ÔñµÄ¹¦ÄÜ£»
ÅäÖù¤¾ß£º°üÀ¨ÅäÖÃÃüÁî½âÊÍÆ÷£¨¶ÔÅäÖýű¾ÖÐʹÓõÄÅäÖÃÃüÁî½øÐнâÊÍ£©ºÍÅäÖÃÓû§½çÃ棨Ìṩ»ùÓÚ×Ö·û½çÃæ¡¢»ùÓÚ Ncurses ͼÐνçÃæÒÔ¼°»ùÓÚ Xwindows ͼÐνçÃæµÄÓû§ÅäÖýçÃ棬¸÷×Ô¶ÔÓ¦ÓÚ Make config¡¢Make menuconfig ºÍ make xconfig£©¡£
ÕâЩÅäÖù¤¾ß¶¼ÊÇʹÓýű¾ÓïÑÔ£¬Èç Tcl/TK¡¢Perl ±àдµÄ£¨Ò²°üº¬Ò»Ð©Óà C ±àдµÄ´úÂ룩¡£±¾ÎIJ¢²»ÊǶÔÅäÖÃϵͳ±¾Éí½øÐзÖÎö£¬¶øÊǽéÉÜÈçºÎʹÓÃÅäÖÃϵͳ¡£ËùÒÔ£¬³ý·ÇÊÇÅäÖÃϵͳµÄά»¤Õߣ¬Ò»°ãµÄÄں˿ª·¢ÕßÎÞÐëÁ˽âËüÃǵÄÔÀí£¬Ö»ÐèÒªÖªµÀÈçºÎ±àд Makefile ºÍÅäÖÃÎļþ¾Í¿ÉÒÔ¡£ËùÒÔ£¬ÔÚ±¾ÎÄÖУ¬ÎÒÃÇÖ»¶Ô Makefile ºÍÅäÖÃÎļþ½øÐÐÌÖÂÛ¡£ÁíÍ⣬·²ÊÇÉæ¼°µ½Óë¾ßÌå CPU Ìåϵ½á¹¹Ïà¹ØµÄÄÚÈÝ£¬ÎÒÃǶ¼ÒÔ ARM ΪÀý£¬ÕâÑù²»½ö¿ÉÒÔ½«ÌÖÂÛµÄÎÊÌâÃ÷È·»¯£¬¶øÇÒ¶ÔÄÚÈݱ¾Éí²»²úÉúÓ°Ïì¡£
2£® Makefile
2.1 Makefile ¸ÅÊö
Makefile µÄ×÷ÓÃÊǸù¾ÝÅäÖõÄÇé¿ö£¬¹¹Ôì³öÐèÒª±àÒëµÄÔ´ÎļþÁÐ±í£¬È»ºó·Ö±ð±àÒ룬²¢°ÑÄ¿±ê´úÂëÁ´½Óµ½Ò»Æð£¬×îÖÕÐÎ³É Linux Äں˶þ½øÖÆÎļþ¡£
ÓÉÓÚ Linux ÄÚºËÔ´´úÂëÊÇ°´ÕÕÊ÷Ðνṹ×éÖ¯µÄ£¬ËùÒÔ Makefile Ò²±»·Ö²¼ÔÚĿ¼Ê÷ÖС£Linux ÄÚºËÖÐµÄ Makefile ÒÔ¼°Óë Makefile Ö±½ÓÏà¹ØµÄÎļþÓУº
Makefile£º¶¥²ã Makefile£¬ÊÇÕû¸öÄÚºËÅäÖᢱàÒëµÄ×ÜÌå¿ØÖÆÎļþ¡£
.config£ºÄÚºËÅäÖÃÎļþ£¬°üº¬ÓÉÓû§Ñ¡ÔñµÄÅäÖÃÑ¡ÏÓÃÀ´´æ·ÅÄÚºËÅäÖúóµÄ½á¹û£¨Èç make config£©¡£
arch/*/Makefile£ºÎ»ÓÚ¸÷ÖÖ CPU ÌåϵĿ¼Ï嵀 Makefile£¬Èç arch/arm/Makefile£¬ÊÇÕë¶ÔÌض¨Æ½Ì¨µÄ Makefile¡£
¸÷¸ö×ÓĿ¼Ï嵀 Makefile£º±ÈÈç drivers/Makefile£¬¸ºÔðËùÔÚ×ÓĿ¼ÏÂÔ´´úÂëµÄ¹ÜÀí¡£
Rules.make£º¹æÔòÎļþ£¬±»ËùÓÐµÄ Makefile ʹÓá£
Óû§Í¨¹ý make config ÅäÖú󣬲úÉúÁË .config¡£¶¥²ã Makefile ¶ÁÈë .config ÖеÄÅäÖÃÑ¡Ôñ¡£¶¥²ã Makefile ÓÐÁ½¸öÖ÷ÒªµÄÈÎÎñ£º²úÉú vmlinux ÎļþºÍÄÚºËÄ£¿é£¨module£©¡£ÎªÁË´ïµ½´ËÄ¿µÄ£¬¶¥²ã Makefile µÝ¹éµÄ½øÈëµ½Äں˵ĸ÷¸ö×ÓĿ¼ÖУ¬·Ö±ðµ÷ÓÃλÓÚÕâЩ×ÓĿ¼ÖÐµÄ Makefile¡£ÖÁÓÚµ½µ×½øÈëÄÄЩ×ÓĿ¼£¬È¡¾öÓÚÄں˵ÄÅäÖá£ÔÚ¶¥²ã Makefile ÖУ¬ÓÐÒ»¾ä£ºinclude arch/$(ARCH)/Makefile£¬°üº¬ÁËÌض¨ CPU Ìåϵ½á¹¹Ï嵀 Makefile£¬Õâ¸ö Makefile Öаüº¬ÁËƽ̨Ïà¹ØµÄÐÅÏ¢¡£
λÓÚ¸÷¸ö×ÓĿ¼Ï嵀 Makefile ͬÑùÒ²¸ù¾Ý .config ¸ø³öµÄÅäÖÃÐÅÏ¢£¬¹¹Ôì³öµ±Ç°ÅäÖÃÏÂÐèÒªµÄÔ´ÎļþÁÐ±í£¬²¢ÔÚÎļþµÄ×îºóÓÐ include $(TOPDIR)/Rules.make¡£
Rules.make ÎļþÆð×ŷdz£ÖØÒªµÄ×÷Óã¬Ëü¶¨ÒåÁËËùÓÐ Makefile ¹²ÓõıàÒë¹æÔò¡£±ÈÈ磬Èç¹ûÐèÒª½«±¾Ä¿Â¼ÏÂËùÓÐµÄ c ³ÌÐò±àÒë³É»ã±à´úÂ룬ÐèÒªÔÚ Makefile ÖÐÓÐÒÔϵıàÒë¹æÔò£º
%.s: %.c
$(CC) $(CFLAGS) -S $< -o $@
Óкܶà×ÓĿ¼Ï¶¼ÓÐͬÑùµÄÒªÇ󣬾ÍÐèÒªÔÚ¸÷×﵀ Makefile Öаüº¬´Ë±àÒë¹æÔò£¬Õâ»á±È½ÏÂé·³¡£¶ø Linux ÄÚºËÖÐÔò°Ñ´ËÀàµÄ±àÒë¹æÔòͳһ·ÅÖõ½ Rules.make ÖУ¬²¢ÔÚ¸÷×﵀ Makefile Öаüº¬½øÁË Rules.make£¨include Rules.make£©£¬ÕâÑù¾Í±ÜÃâÁËÔÚ¶à¸ö Makefile ÖÐÖظ´Í¬ÑùµÄ¹æÔò¡£¶ÔÓÚÉÏÃæµÄÀý×Ó£¬ÔÚ Rules.make ÖжÔÓ¦µÄ¹æÔòΪ£º
%.s: %.c
$(CC) $(CFLAGS) $(EXTRA_CFLAGS) $(CFLAGS_$(*F)) $(CFLAGS_$@) -S $< -o $@
2.2 Makefile ÖеıäÁ¿
¶¥²ã Makefile ¶¨Òå²¢Ïò»·¾³ÖÐÊä³öÁËÐí¶à±äÁ¿£¬Îª¸÷¸ö×ÓĿ¼Ï嵀 Makefile ´«µÝһЩÐÅÏ¢¡£ÓÐЩ±äÁ¿£¬±ÈÈç SUBDIRS£¬²»½öÔÚ¶¥²ã Makefile Öж¨Òå²¢ÇÒ¸³³õÖµ£¬¶øÇÒÔÚ arch/*/Makefile »¹×÷ÁËÀ©³ä¡£
³£ÓõıäÁ¿ÓÐÒÔϼ¸Àࣺ
1£© °æ±¾ÐÅÏ¢
°æ±¾ÐÅÏ¢ÓУºVERSION£¬PATCHLEVEL, SUBLEVEL, EXTRAVERSION£¬KERNELRELEASE¡£°æ±¾ÐÅÏ¢¶¨ÒåÁ˵±Ç°Äں˵İ汾£¬±ÈÈç VERSION=2£¬PATCHLEVEL=4£¬SUBLEVEL=18£¬EXATAVERSION=-rmk7£¬ËüÃǹ²Í¬¹¹³ÉÄں˵ķ¢Ðа汾KERNELRELEASE£º2.4.18-rmk7
2£© CPU Ìåϵ½á¹¹£ºARCH
ÔÚ¶¥²ã Makefile µÄ¿ªÍ·£¬Óà ARCH ¶¨ÒåÄ¿±ê CPU µÄÌåϵ½á¹¹£¬±ÈÈç ARCH:=arm µÈ¡£Ðí¶à×ÓĿ¼µÄ Makefile ÖУ¬Òª¸ù¾Ý ARCH µÄ¶¨ÒåÑ¡Ôñ±àÒëÔ´ÎļþµÄÁÐ±í¡£
3£© ·¾¶ÐÅÏ¢£ºTOPDIR, SUBDIRS
TOPDIR ¶¨ÒåÁË Linux ÄÚºËÔ´´úÂëËùÔڵĸùĿ¼¡£ÀýÈ磬¸÷¸ö×ÓĿ¼Ï嵀 Makefile ͨ¹ý $(TOPDIR)/Rules.make ¾Í¿ÉÒÔÕÒµ½ Rules.make µÄλÖá£
SUBDIRS ¶¨ÒåÁËÒ»¸öĿ¼ÁÐ±í£¬ÔÚ±àÒëÄں˻òÄ£¿éʱ£¬¶¥²ã Makefile ¾ÍÊǸù¾Ý SUBDIRS À´¾ö¶¨½øÈëÄÄЩ×ÓĿ¼¡£SUBDIRS µÄֵȡ¾öÓÚÄں˵ÄÅäÖã¬ÔÚ¶¥²ã Makefile ÖÐ SUBDIRS ¸³ÖµÎª kernel drivers mm fs net ipc lib£»¸ù¾ÝÄں˵ÄÅäÖÃÇé¿ö£¬ÔÚ arch/*/Makefile ÖÐÀ©³äÁË SUBDIRS µÄÖµ£¬²Î¼û4£©ÖеÄÀý×Ó¡£
4£© ÄÚºË×é³ÉÐÅÏ¢£ºHEAD, CORE_FILES, NETWORKS, DRIVERS, LIBS
Linux ÄÚºËÎļþ vmlinux ÊÇÓÉÒÔϹæÔò²úÉúµÄ£º
vmlinux: $(CONFIGURATION) init/main.o init/version.o linuxsubdirs
$(LD) $(LINKFLAGS) $(HEAD) init/main.o init/version.o \
--start-group \
$(CORE_FILES) \
$(DRIVERS) \
$(NETWORKS) \
$(LIBS) \
--end-group \
-o vmlinux
¿ÉÒÔ¿´³ö£¬vmlinux ÊÇÓÉ HEAD¡¢main.o¡¢version.o¡¢CORE_FILES¡¢DRIVERS¡¢NETWORKS ºÍ LIBS ×é³ÉµÄ¡£ÕâЩ±äÁ¿£¨Èç HEAD£©¶¼ÊÇÓÃÀ´¶¨ÒåÁ¬½ÓÉú³É vmlinux µÄÄ¿±êÎļþºÍ¿âÎļþÁÐ±í¡£ÆäÖУ¬HEADÔÚarch/*/Makefile Öж¨Ò壬ÓÃÀ´È·¶¨±»×îÏÈÁ´½Ó½ø vmlinux µÄÎļþÁÐ±í¡£±ÈÈ磬¶ÔÓÚ ARM ϵÁÐµÄ CPU£¬HEAD ¶¨ÒåΪ£º
HEAD := arch/arm/kernel/head-$(PROCESSOR).o \
arch/arm/kernel/init_task.o
±íÃ÷ head-$(PROCESSOR).o ºÍ init_task.o ÐèÒª×îÏȱ»Á´½Óµ½ vmlinux ÖС£PROCESSOR Ϊ armv »ò armo£¬È¡¾öÓÚÄ¿±ê CPU¡£ CORE_FILES£¬NETWORK£¬DRIVERS ºÍ LIBS ÔÚ¶¥²ã Makefile Öж¨Ò壬²¢ÇÒÓÉ arch/*/Makefile ¸ù¾ÝÐèÒª½øÐÐÀ©³ä¡£ CORE_FILES ¶ÔÓ¦×ÅÄں˵ĺËÐÄÎļþ£¬ÓÐ kernel/kernel.o£¬mm/mm.o£¬fs/fs.o£¬ipc/ipc.o£¬¿ÉÒÔ¿´³ö£¬ÕâЩÊÇ×é³ÉÄÚºË×îΪÖØÒªµÄÎļþ¡£Í¬Ê±£¬arch/arm/Makefile ¶Ô CORE_FILES ½øÐÐÁËÀ©³ä£º
# arch/arm/Makefile
# If we have a machine-specific directory, then include it in the build.
MACHDIR := arch/arm/mach-$(MACHINE)
ifeq ($(MACHDIR),$(wildcard $(MACHDIR)))
SUBDIRS += $(MACHDIR)
CORE_FILES := $(MACHDIR)/$(MACHINE).o $(CORE_FILES)
endif
HEAD := arch/arm/kernel/head-$(PROCESSOR).o \
arch/arm/kernel/init_task.o
SUBDIRS += arch/arm/kernel arch/arm/mm arch/arm/lib arch/arm/nwfpe
CORE_FILES := arch/arm/kernel/kernel.o arch/arm/mm/mm.o $(CORE_FILES)
LIBS := arch/arm/lib/lib.a $(LIBS)
5£© ±àÒëÐÅÏ¢£ºCPP, CC, AS, LD, AR£¬CFLAGS£¬LINKFLAGS
ÔÚ Rules.make Öж¨ÒåµÄÊDZàÒëµÄͨÓùæÔò£¬¾ßÌåµ½Ìض¨µÄ³¡ºÏ£¬ÐèÒªÃ÷È·¸ø³ö±àÒë»·¾³£¬±àÒë»·¾³¾ÍÊÇÔÚÒÔÉϵıäÁ¿Öж¨ÒåµÄ¡£Õë¶Ô½»²æ±àÒëµÄÒªÇ󣬶¨ÒåÁË CROSS_COMPILE¡£±ÈÈ磺
CROSS_COMPILE = arm-linux-
CC = $(CROSS_COMPILE)gcc
LD = $(CROSS_COMPILE)ld
......
CROSS_COMPILE ¶¨ÒåÁ˽»²æ±àÒëÆ÷ǰ׺ arm-linux-£¬±íÃ÷ËùÓеĽ»²æ±àÒ빤¾ß¶¼ÊÇÒÔ arm-linux- ¿ªÍ·µÄ£¬ËùÒÔÔÚ¸÷¸ö½»²æ±àÒëÆ÷¹¤¾ß֮ǰ£¬¶¼¼ÓÈëÁË $(CROSS_COMPILE)£¬ÒÔ×é³ÉÒ»¸öÍêÕûµÄ½»²æ±àÒ빤¾ßÎļþÃû£¬±ÈÈç arm-linux-gcc¡£
CFLAGS ¶¨ÒåÁË´«µÝ¸ø C ±àÒëÆ÷µÄ²ÎÊý¡£
LINKFLAGS ÊÇÁ´½ÓÉú³É vmlinux ʱ£¬ÓÉÁ´½ÓÆ÷ʹÓõIJÎÊý¡£LINKFLAGS ÔÚ arm/*/Makefile Öж¨Ò壬±ÈÈ磺
# arch/arm/Makefile
LINKFLAGS :=-p -X -T arch/arm/vmlinux.lds
6£© ÅäÖñäÁ¿CONFIG_*
.config ÎļþÖÐÓÐÐí¶àµÄÅäÖñäÁ¿µÈʽ£¬ÓÃÀ´ËµÃ÷Óû§ÅäÖõĽá¹û¡£ÀýÈç CONFIG_MODULES=y ±íÃ÷Óû§Ñ¡ÔñÁË Linux Äں˵ÄÄ£¿é¹¦ÄÜ¡£
.config ±»¶¥²ã Makefile °üº¬ºó£¬¾ÍÐγÉÐí¶àµÄÅäÖñäÁ¿£¬Ã¿¸öÅäÖñäÁ¿¾ßÓÐÈ·¶¨µÄÖµ£ºy ±íʾ±¾±àÒëÑ¡Ïî¶ÔÓ¦µÄÄں˴úÂë±»¾²Ì¬±àÒë½ø Linux Äںˣ»m ±íʾ±¾±àÒëÑ¡Ïî¶ÔÓ¦µÄÄں˴úÂë±»±àÒë³ÉÄ£¿é£»n ±íʾ²»Ñ¡Ôñ´Ë±àÒëÑ¡ÏÈç¹û¸ù±¾¾ÍûÓÐÑ¡Ôñ£¬ÄÇôÅäÖñäÁ¿µÄֵΪ¿Õ¡£
2.3 Rules.make ±äÁ¿
Ç°Ãæ½²¹ý£¬Rules.make ÊDZàÒë¹æÔòÎļþ£¬ËùÓÐµÄ Makefile Öж¼»á°üÀ¨ Rules.make¡£Rules.make Îļþ¶¨ÒåÁËÐí¶à±äÁ¿£¬×îΪÖØÒªÊÇÄÇЩ±àÒë¡¢Á´½ÓÁбí±äÁ¿¡£
O_OBJS£¬L_OBJS£¬OX_OBJS£¬LX_OBJS£º±¾Ä¿Â¼ÏÂÐèÒª±àÒë½ø Linux ÄÚºË vmlinux µÄÄ¿±êÎļþÁÐ±í£¬ÆäÖÐ OX_OBJS ºÍ LX_OBJS ÖÐµÄ "X" ±íÃ÷Ä¿±êÎļþʹÓÃÁË EXPORT_SYMBOL Êä³ö·ûºÅ¡£
M_OBJS£¬MX_OBJS£º±¾Ä¿Â¼ÏÂÐèÒª±»±àÒë³É¿É×°ÔØÄ£¿éµÄÄ¿±êÎļþÁÐ±í¡£Í¬Ñù£¬MX_OBJS ÖÐµÄ "X" ±íÃ÷Ä¿±êÎļþʹÓÃÁË EXPORT_SYMBOL Êä³ö·ûºÅ¡£
O_TARGET£¬L_TARGET£ºÃ¿¸ö×ÓĿ¼Ï¶¼ÓÐÒ»¸ö O_TARGET »ò L_TARGET£¬Rules.make Ê×ÏÈ´ÓÔ´´úÂë±àÒëÉú³É O_OBJS ºÍ OX_OBJS ÖÐËùÓеÄÄ¿±êÎļþ£¬È»ºóʹÓà $(LD) -r °ÑËüÃÇÁ´½Ó³ÉÒ»¸ö O_TARGET »ò L_TARGET¡£O_TARGET ÒÔ .o ½á⣬¶ø L_TARGET ÒÔ .a ½áβ¡£
2.4 ×ÓĿ¼ Makefile
×ÓĿ¼ Makefile ÓÃÀ´¿ØÖƱ¾¼¶Ä¿Â¼ÒÔÏÂÔ´´úÂëµÄ±àÒë¹æÔò¡£ÎÒÃÇͨ¹ýÒ»¸öÀý×ÓÀ´½²½â×ÓĿ¼ Makefile µÄ×é³É£º
#
# Makefile for the linux kernel.
#
# All of the (potential) objects that export symbols.
# This list comes from 'grep -l EXPORT_SYMBOL *.[hc]'.
export-objs := tc.o
# Object file lists.
obj-y :=
obj-m :=
obj-n :=
obj- :=
obj-$(CONFIG_TC) += tc.o
obj-$(CONFIG_ZS) += zs.o
obj-$(CONFIG_VT) += lk201.o lk201-map.o lk201-remap.o
# Files that are both resident and modular: remove from modular.
obj-m := $(filter-out $(obj-y), $(obj-m))
# Translate to Rules.make lists.
L_TARGET := tc.a
L_OBJS := $(sort $(filter-out $(export-objs), $(obj-y)))
LX_OBJS := $(sort $(filter $(export-objs), $(obj-y)))
M_OBJS := $(sort $(filter-out $(export-objs), $(obj-m)))
MX_OBJS := $(sort $(filter $(export-objs), $(obj-m)))
include $(TOPDIR)/Rules.make
a) ×¢ÊÍ
¶Ô Makefile µÄ˵Ã÷ºÍ½âÊÍ£¬ÓÉ#¿ªÊ¼¡£
b) ±àÒëÄ¿±ê¶¨Òå
ÀàËÆÓÚ obj-$(CONFIG_TC) += tc.o µÄÓï¾äÊÇÓÃÀ´¶¨Òå±àÒëµÄÄ¿±ê£¬ÊÇ×ÓĿ¼ Makefile ÖÐ×îÖØÒªµÄ²¿·Ö¡£±àÒëÄ¿±ê¶¨ÒåÄÇЩÔÚ±¾×ÓĿ¼Ï£¬ÐèÒª±àÒëµ½ Linux ÄÚºËÖеÄÄ¿±êÎļþÁÐ±í¡£ÎªÁËÖ»ÔÚÓû§Ñ¡ÔñÁ˴˹¦Äܺó²Å±àÒ룬ËùÓеÄÄ¿±ê¶¨Ò嶼ÈÚºÏÁ˶ÔÅäÖñäÁ¿µÄÅжϡ£
Ç°Ãæ˵¹ý£¬Ã¿¸öÅäÖñäÁ¿È¡Öµ·¶Î§ÊÇ£ºy£¬n£¬m ºÍ¿Õ£¬obj-$(CONFIG_TC) ·Ö±ð¶ÔÓ¦×Å obj-y£¬obj-n£¬obj-m£¬obj-¡£Èç¹û CONFIG_TC ÅäÖÃΪ y£¬ÄÇô tc.o ¾Í½øÈëÁË obj-y ÁÐ±í¡£obj-y Ϊ°üº¬µ½ Linux ÄÚºË vmlinux ÖеÄÄ¿±êÎļþÁÐ±í£»obj-m Ϊ±àÒë³ÉÄ£¿éµÄÄ¿±êÎļþÁÐ±í£»obj-n ºÍ obj- ÖеÄÎļþÁÐ±í±»ºöÂÔ¡£ÅäÖÃϵͳ¾Í¸ù¾ÝÕâЩÁбíµÄÊôÐÔ½øÐбàÒëºÍÁ´½Ó¡£
export-objs ÖеÄÄ¿±êÎļþ¶¼Ê¹ÓÃÁË EXPORT_SYMBOL() ¶¨ÒåÁ˹«¹²µÄ·ûºÅ£¬ÒÔ±ã¿É×°ÔØÄ£¿éʹÓá£ÔÚ tc.c ÎļþµÄ×îºó²¿·Ö£¬ÓÐ "EXPORT_SYMBOL(search_tc_card);"£¬±íÃ÷ tc.o ÓзûºÅÊä³ö¡£
ÕâÀïÐèÒªÖ¸³öµÄÊÇ£¬¶ÔÓÚ±àÒëÄ¿±êµÄ¶¨Ò壬´æÔÚ×ÅÁ½ÖÖ¸ñʽ£¬·Ö±ðÊÇÀÏʽ¶¨ÒåºÍÐÂʽ¶¨Òå¡£ÀÏʽ¶¨Òå¾ÍÊÇÇ°Ãæ Rules.make ʹÓõÄÄÇЩ±äÁ¿£¬ÐÂʽ¶¨Òå¾ÍÊÇ obj-y£¬obj-m£¬obj-n ºÍ obj-¡£Linux ÄÚºËÍƼöʹÓÃÐÂʽ¶¨Ò壬²»¹ýÓÉÓÚ Rules.make ²»Àí½âÐÂʽ¶¨Ò壬ÐèÒªÔÚ Makefile ÖеÄÊÊÅä¶Î½«Æäת»»³ÉÀÏʽ¶¨Òå¡£
c) ÊÊÅä¶Î
ÊÊÅä¶ÎµÄ×÷ÓÃÊǽ«ÐÂʽ¶¨Òåת»»³ÉÀÏʽ¶¨Òå¡£ÔÚÉÏÃæµÄÀý×ÓÖУ¬ÊÊÅä¶Î¾ÍÊǽ« obj-y ºÍ obj-m ת»»³É Rules.make Äܹ»Àí½âµÄ L_TARGET£¬L_OBJS£¬LX_OBJS£¬M_OBJS£¬MX_OBJS¡£
L_OBJS := $(sort $(filter-out $(export-objs), $(obj-y))) ¶¨ÒåÁË L_OBJS µÄÉú³É·½Ê½£ºÔÚ obj-y µÄÁбíÖйýÂ˵ô export-objs£¨tc.o£©£¬È»ºóÅÅÐò²¢È¥³ýÖظ´µÄÎļþÃû¡£ÕâÀïʹÓõ½ÁË GNU Make µÄһЩÌØÊ⹦ÄÜ£¬¾ßÌåµÄº¬Òå¿É²Î¿¼ Make µÄÎĵµ£¨info make£©¡£
d) include $(TOPDIR)/Rules.make
3£® ÅäÖÃÎļþ
3.1 ÅäÖù¦ÄܸÅÊö
³ýÁË Makefile µÄ±àд£¬ÁíÍâÒ»¸öÖØÒªµÄ¹¤×÷¾ÍÊÇ°Ñй¦ÄܼÓÈëµ½ Linux µÄÅäÖÃÑ¡ÏîÖУ¬Ìṩ´ËÏÄܵÄ˵Ã÷£¬ÈÃÓû§Óлú»áÑ¡Ôñ´ËÏÄÜ¡£ËùÓеÄÕâЩ¶¼ÐèÒªÔÚ config.in ÎļþÖÐÓÃÅäÖÃÓïÑÔÀ´±àдÅäÖýű¾£¬
ÔÚ Linux ÄÚºËÖУ¬ÅäÖÃÃüÁîÓжàÖÖ·½Ê½£º
ÅäÖÃÃüÁî ½âÊͽű¾
Make config, make oldconfig scripts/Configure
Make menuconfig scripts/Menuconfig
Make xconfig scripts/tkparse
ÒÔ×Ö
·û½çÃæÅäÖã¨make config£©ÎªÀý£¬¶¥²ã Makefile µ÷Óà scripts/Configure£¬ °´ÕÕ arch/arm/config.in À´½øÐÐÅäÖá£ÃüÁîÖ´ÐÐÍêºó²úÉúÎļþ .config£¬ÆäÖб£´æ×ÅÅäÖÃÐÅÏ¢¡£ÏÂÒ»´ÎÔÙ×ö make config ½«²úÉúÐ嵀 .config Îļþ£¬Ô .config ±»¸ÄÃûΪ .config.old
3.2 ÅäÖÃÓïÑÔ
1£© ¶¥²ã²Ëµ¥
mainmenu_name /prompt/ /prompt/ ÊÇÓÃ'»ò"°üΧµÄ×Ö·û´®£¬'Óë"µÄÇø±ðÊÇ'¡'ÖпÉʹÓÃ$ÒýÓñäÁ¿µÄÖµ¡£mainmenu_name ÉèÖÃ×î¸ß²ã²Ëµ¥µÄÃû×Ö£¬ËüÖ»ÔÚ make xconfig ʱ²Å»áÏÔʾ¡£
2£© ѯÎÊÓï¾ä
bool /prompt/ /symbol/
hex /prompt/ /symbol/ /word/
int /prompt/ /symbol/ /word/
string /prompt/ /symbol/ /word/
tristate /prompt/ /symbol/
ѯÎÊÓï¾äÊ×ÏÈÏÔʾһ´®Ìáʾ·û /prompt/£¬µÈ´ýÓû§ÊäÈ룬²¢°ÑÊäÈëµÄ½á¹û¸³¸ø /symbol/ Ëù´ú±íµÄÅäÖñäÁ¿¡£²»Í¬µÄѯÎÊÓï¾äµÄÇø±ðÔÚÓÚËüÃǽÓÊܵÄÊäÈëÊý¾ÝÀàÐͲ»Í¬£¬±ÈÈç bool ½ÓÊܲ¼¶ûÀàÐÍ£¨ y »ò n £©£¬hex ½ÓÊÜ 16 ½øÖÆÊý¾Ý¡£ÓÐЩѯÎÊÓï¾ä»¹ÓеÚÈý¸ö²ÎÊý /word/£¬ÓÃÀ´¸ø³öȱʡֵ¡£
3£© ¶¨ÒåÓï¾ä
define_bool /symbol/ /word/
define_hex /symbol/ /word/
define_int /symbol/ /word/
define_string /symbol/ /word/
define_tristate /symbol/ /word/
²»Í¬ÓÚѯÎÊÓï¾äµÈ´ýÓû§ÊäÈ룬¶¨ÒåÓï¾äÏÔʽµÄ¸øÅäÖñäÁ¿ /symbol/ ¸³Öµ /word/¡£
4£© ÒÀÀµÓï¾ä
dep_bool /prompt/ /symbol/ /dep/ ...
dep_mbool /prompt/ /symbol/ /dep/ ...
dep_hex /prompt/ /symbol/ /word/ /dep/ ...
dep_int /prompt/ /symbol/ /word/ /dep/ ...
dep_string /prompt/ /symbol/ /word/ /dep/ ...
dep_tristate /prompt/ /symbol/ /dep/ ...
ÓëѯÎÊÓï¾äÀàËÆ£¬ÒÀÀµÓï¾äÒ²ÊǶ¨ÒåеÄÅäÖñäÁ¿¡£²»Í¬µÄÊÇ£¬ÅäÖñäÁ¿/symbol/µÄÈ¡Öµ·¶Î§½«ÒÀÀµÓÚÅäÖñäÁ¿Áбí/dep/ ¡¡£Õâ¾ÍÒâζ×Å£º±»¶¨ÒåµÄÅäÖñäÁ¿Ëù¶ÔÓ¦¹¦ÄܵÄÈ¡ÉáÈ¡¾öÓÚÒÀÀµÁбíËù¶ÔÓ¦¹¦ÄܵÄÑ¡Ôñ¡£ÒÔdep_boolΪÀý£¬Èç¹û/dep/ ¡ÁбíµÄËùÓÐÅäÖñäÁ¿¶¼È¡Öµy£¬ÔòÏÔʾ/prompt/£¬Óû§¿ÉÊäÈëÈÎÒâµÄÖµ¸øÅäÖñäÁ¿/symbol/£¬µ«ÊÇÖ»ÒªÓÐÒ»¸öÅäÖñäÁ¿µÄȡֵΪn£¬Ôò/symbol/±»Ç¿ÖƳÉn¡£
²»Í¬ÒÀÀµÓï¾äµÄÇø±ðÔÚÓÚËüÃÇÓÉÒÀÀµÌõ¼þËù²úÉúµÄÈ¡Öµ·¶Î§²»Í¬¡£
5£© Ñ¡ÔñÓï¾ä
choice /prompt/ /word/ /word/
choice Óï¾äÊ×Ïȸø³öÒ»´®Ñ¡ÔñÁÐ±í£¬¹©Óû§Ñ¡ÔñÆäÖÐÒ»ÖÖ¡£±ÈÈç Linux for ARM Ö§³Ö¶àÖÖ»ùÓÚ ARM core µÄ CPU£¬Linux ʹÓà choice Óï¾äÌṩһ¸ö CPU ÁÐ±í£¬¹©Óû§Ñ¡Ôñ£º
choice 'ARM system type' \
"Anakin CONFIG_ARCH_ANAKIN \
Archimedes/A5000 CONFIG_ARCH_ARCA5K \
Cirrus-CL-PS7500FE CONFIG_ARCH_CLPS7500 \
¡¡
SA1100-based CONFIG_ARCH_SA1100 \
Shark CONFIG_ARCH_SHARK" RiscPC
Choice Ê×ÏÈÏÔʾ /prompt/£¬È»ºó½« /word/ ·Ö½â³ÉÇ°ºóÁ½¸ö²¿·Ö£¬Ç°²¿·ÖΪ¶ÔӦѡÔñµÄÌáʾ·û£¬ºó²¿·ÖÊǶÔӦѡÔñµÄÅäÖñäÁ¿¡£Óû§Ñ¡ÔñµÄÅäÖñäÁ¿Îª y£¬ÆäÓàµÄ¶¼Îª n¡£
6£© ifÓï¾ä
if [ /expr/ ] ; then
/statement/
...
fi
if [ /expr/ ] ; then
/statement/
...
else
/statement/
...
fi
if Óï¾ä¶ÔÅäÖñäÁ¿£¨»òÅäÖñäÁ¿µÄ×éºÏ£©½øÐÐÅжϣ¬²¢×÷³ö²»Í¬µÄ´¦Àí¡£ÅжÏÌõ¼þ /expr/ ¿ÉÒÔÊǵ¥¸öÅäÖñäÁ¿»ò×Ö·û´®£¬Ò²¿ÉÒÔÊÇ´ø²Ù×÷·ûµÄ±í´ïʽ¡£²Ù×÷·ûÓУº=£¬!=£¬-o£¬-a µÈ¡£
7£© ²Ëµ¥¿é£¨menu block£©Óï¾ä
mainmenu_option next_comment
comment '¡..'
¡
endmenu
ÒýÈëеIJ˵¥¡£ÔÚÏòÄÚºËÔö¼ÓÐµĹ¦Äܺó£¬ÐèÒªÏàÓ¦µÄÔö¼ÓеIJ˵¥£¬²¢ÔÚв˵¥Ï¸ø³ö´ËÏÄܵÄÅäÖÃÑ¡Ïî¡£Comment ºó´øµÄ×¢Ê;ÍÊÇв˵¥µÄÃû³Æ¡£ËùÓйéÊôÓڴ˲˵¥µÄÅäÖÃÑ¡ÏîÓï¾ä¶¼Ð´ÔÚ comment ºÍ endmenu Ö®¼ä¡£
8£© Source Óï¾ä
source /word/
/word/ ÊÇÎļþÃû£¬source µÄ×÷ÓÃÊǵ÷ÈëеÄÎļþ¡£
3.3 ȱʡÅäÖÃ
Linux ÄÚºËÖ§³Ö·Ç³£¶àµÄÓ²¼þƽ̨£¬¶ÔÓÚ¾ßÌåµÄÓ²¼þƽ̨¶øÑÔ£¬ÓÐЩÅäÖþÍÊDZØÐèµÄ£¬ÓÐЩÅäÖþͲ»ÊDZØÐèµÄ¡£ÁíÍ⣬ÐÂÔö¼Ó¹¦ÄܵÄÕý³£ÔËÐÐÍùÍùÒ²ÐèÒªÒ»¶¨µÄÏȾöÌõ¼þ£¬Õë¶Ôй¦ÄÜ£¬±ØÐë×÷ÏàÓ¦µÄÅäÖá£Òò´Ë£¬Ìض¨Ó²¼þƽ̨Äܹ»Õý³£ÔËÐжÔÓ¦×ÅÒ»¸ö×îСµÄ»ù±¾ÅäÖã¬Õâ¾ÍÊÇȱʡÅäÖá£
Linux ÄÚºËÖÐÕë¶Ôÿ¸ö ARCH ¶¼»áÓÐÒ»¸öȱʡÅäÖá£ÔÚÏòÄں˴úÂëÔö¼ÓÁËÐµĹ¦Äܺó£¬Èç¹ûй¦ÄܶÔÓÚÕâ¸ö ARCH ÊDZØÐèµÄ£¬¾ÍÒªÐÞ¸Ä´Ë ARCH µÄȱʡÅäÖá£Ð޸ķ½·¨ÈçÏ£¨ÔÚ Linux Äں˸ùĿ¼Ï£©£º
±¸·Ý .config Îļþ
cp arch/arm/deconfig .config
ÐÞ¸Ä .config
cp .config arch/arm/deconfig
»Ö¸´ .config
Èç¹ûÐÂÔöµÄ¹¦ÄÜÊÊÓÃÓÚÐí¶àµÄ ARCH£¬Ö»ÒªÕë¶Ô¾ßÌåµÄ ARCH£¬Öظ´ÉÏÃæµÄ²½Öè¾Í¿ÉÒÔÁË¡£
3.4 help file
´ó¼Ò¶¼ÓÐÕâÑùµÄ¾Ñ飬ÔÚÅäÖà Linux ÄÚºËʱ£¬Óöµ½²»¶®º¬ÒåµÄÅäÖÃÑ¡Ï¿ÉÒԲ鿴ËüµÄ°ïÖú£¬´ÓÖпɵõ½Ñ¡ÔñµÄ½¨Òé¡£ÏÂÃæÎÒÃǾͿ´¿´ÈçºÎ¸ø¸øÒ»¸öÅäÖÃÑ¡ÏîÔö¼Ó°ïÖúÐÅÏ¢¡£
ËùÓÐÅäÖÃÑ¡ÏîµÄ°ïÖúÐÅÏ¢¶¼ÔÚ Documentation/Configure.help ÖУ¬ËüµÄ¸ñʽΪ£º
4£® ʵÀý
¶ÔÓÚÒ»¸ö¿ª·¢ÕßÀ´Ëµ£¬½«×Ô¼º¿ª·¢µÄÄں˴úÂë¼ÓÈëµ½ Linux ÄÚºËÖУ¬ÐèÒªÓÐÈý¸ö²½Öè¡£Ê×ÏÈÈ·¶¨°Ñ×Ô¼º¿ª·¢´úÂë·ÅÈëµ½Äں˵ÄλÖã»Æä´Î£¬°Ñ×Ô¼º¿ª·¢µÄ¹¦ÄÜÔö¼Óµ½ Linux Äں˵ÄÅäÖÃÑ¡ÏîÖУ¬Ê¹Óû§Äܹ»Ñ¡Ôñ´Ë¹¦ÄÜ£»×îºó£¬¹¹½¨×ÓĿ¼ Makefile£¬¸ù¾ÝÓû§µÄÑ¡Ôñ£¬½«ÏàÓ¦µÄ´úÂë±àÒëµ½×îÖÕÉú³ÉµÄ Linux ÄÚºËÖÐÈ¥¡£ÏÂÃ棬ÎÒÃǾÍͨ¹ýÒ»¸ö¼òµ¥µÄÀý×Ó--test driver£¬½áºÏÇ°Ãæѧµ½µÄ֪ʶ£¬À´ËµÃ÷ÈçºÎÏò Linux ÄÚºËÖÐÔö¼ÓÐµĹ¦ÄÜ¡£
4.1 Ŀ¼½á¹¹
test driver ·ÅÖÃÔÚ drivers/test/ Ŀ¼Ï£º
$cd drivers/test
$tree
.
|-- Config.in
|-- Makefile
|-- cpu
| |-- Makefile
| `-- cpu.c
|-- test.c
|-- test_client.c
|-- test_ioctl.c
|-- test_proc.c
|-- test_queue.c
`-- test
|-- Makefile
`-- test.c
4.2 ÅäÖÃÎļþ
1£© drivers/test/Config.in
#
# TEST driver configuration
#
mainmenu_option next_comment
comment 'TEST Driver'
bool 'TEST support' CONFIG_TEST
if [ "$CONFIG_TEST" = "y" ]; then
tristate 'TEST user-space interface' CONFIG_TEST_USER
bool 'TEST CPU ' CONFIG_TEST_CPU
fi
endmenu
ÓÉÓÚ test driver ¶ÔÓÚÄÚºËÀ´ËµÊÇÐµĹ¦ÄÜ£¬ËùÒÔÊ×ÏÈ´´½¨Ò»¸ö²Ëµ¥ TEST Driver¡£È»ºó£¬ÏÔʾ "TEST support"£¬µÈ´ýÓû§Ñ¡Ôñ£»½ÓÏÂÀ´ÅжÏÓû§ÊÇ·ñÑ¡ÔñÁË TEST Driver£¬Èç¹ûÊÇ£¨CONFIG_TEST=y£©£¬Ôò½øÒ»²½ÏÔʾ×Ó¹¦ÄÜ£ºÓû§½Ó¿ÚÓë CPU ¹¦ÄÜÖ§³Ö£»ÓÉÓÚÓû§½Ó¿Ú¹¦ÄÜ¿ÉÒÔ±»±àÒë³ÉÄÚºËÄ£¿é£¬ËùÒÔÕâÀïµÄѯÎÊÓï¾äʹÓÃÁË tristate£¨ÒòΪ tristate µÄÈ¡Öµ·¶Î§°üÀ¨ y¡¢n ºÍ m£¬m ¾ÍÊǶÔÓ¦×ÅÄ£¿é£©¡£
2£© arch/arm/config.in
ÔÚÎļþµÄ×îºó¼ÓÈ룺source drivers/test/Config.in£¬½« TEST Driver ×Ó¹¦ÄܵÄÅäÖÃÄÉÈëµ½ Linux Äں˵ÄÅäÖÃÖС£
4.3 Makefile
1£©drivers/test/Makefile
# drivers/test/Makefile
#
# Makefile for the TEST.
#
SUB_DIRS :=
MOD_SUB_DIRS := $(SUB_DIRS)
ALL_SUB_DIRS := $(SUB_DIRS) cpu
L_TARGET := test.a
export-objs := test.o test_client.o
obj-$(CONFIG_TEST) += test.o test_queue.o test_client.o
obj-$(CONFIG_TEST_USER) += test_ioctl.o
obj-$(CONFIG_PROC_FS) += test_proc.o
subdir-$(CONFIG_TEST_CPU) += cpu
include $(TOPDIR)/Rules.make
clean:
for dir in $(ALL_SUB_DIRS); do make -C $$dir clean; done
rm -f *.[oa] .*.flags
drivers/test Ŀ¼ÏÂ×îÖÕÉú³ÉµÄÄ¿±êÎļþÊÇ test.a¡£ÔÚ test.c ºÍ test-client.c ÖÐʹÓÃÁË EXPORT_SYMBOL Êä³ö·ûºÅ£¬ËùÒÔ test.o ºÍ test-client.o λÓÚ export-objs ÁбíÖС£È»ºó£¬¸ù¾ÝÓû§µÄÑ¡Ôñ£¨¾ßÌåÀ´Ëµ£¬¾ÍÊÇÅäÖñäÁ¿µÄÈ¡Öµ£©£¬¹¹½¨¸÷×Ô¶ÔÓ¦µÄ obj-* ÁÐ±í¡£ÓÉÓÚ TEST Driver ÖаüÒ»¸ö×ÓĿ¼ cpu£¬µ± CONFIG_TEST_CPU=y£¨¼´Óû§Ñ¡ÔñÁ˴˹¦ÄÜ£©Ê±£¬ÐèÒª½« cpu Ŀ¼¼ÓÈëµ½ subdir-y ÁбíÖС£
2£©drivers/test/cpu/Makefile
# drivers/test/test/Makefile
#
# Makefile for the TEST CPU
#
SUB_DIRS :=
MOD_SUB_DIRS := $(SUB_DIRS)
ALL_SUB_DIRS := $(SUB_DIRS)
L_TARGET := test_cpu.a
obj-$(CONFIG_test_CPU) += cpu.o
include $(TOPDIR)/Rules.make
clean:
rm -f *.[oa] .*.flags
3£©drivers/Makefile
¡¡
subdir-$(CONFIG_TEST) += test
¡¡
include $(TOPDIR)/Rules.make
ÔÚ drivers/Makefile ÖмÓÈë subdir-$(CONFIG_TEST)+= test£¬Ê¹µÃÔÚÓû§Ñ¡Ôñ TEST Driver ¹¦Äܺó£¬Äں˱àÒëʱÄܹ»½øÈë test Ŀ¼¡£
4£©Makefile
¡¡
DRIVERS-$(CONFIG_PLD) += drivers/pld/pld.o
DRIVERS-$(CONFIG_TEST) += drivers/test/test.a
DRIVERS-$(CONFIG_TEST_CPU) += drivers/test/cpu/test_cpu.a
DRIVERS := $(DRIVERS-y)
¡¡
ÔÚ¶¥²ã Makefile ÖмÓÈë DRIVERS-$(CONFIG_TEST) += drivers/test/test.a ºÍ DRIVERS-$(CONFIG_TEST_CPU) += drivers/test/cpu/test_cpu.a¡£ÈçºÎÓû§Ñ¡ÔñÁË TEST Driver£¬ÄÇô CONFIG_TEST ºÍ CONFIG_TEST_CPU ¶¼ÊÇ y£¬test.a ºÍ test_cpu.a ¾Í¶¼Î»ÓÚ DRIVERS-y ÁбíÖУ¬È»ºóÓÖ±»·ÅÖÃÔÚ DRIVERS ÁбíÖС£ÔÚÇ°ÃæÔø¾Ìáµ½¹ý£¬Linux ÄÚºËÎļþ vmlinux µÄ×é³ÉÖаüÀ¨ DRIVERS£¬ËùÒÔ test.a ºÍ test_cpu.a ×îÖտɱ»Á´½Óµ½ vmlinux ÖС£
5£® ²Î¿¼
Document/kbuild/makefiles.txt£¬Linux Kernel Source code
Document/kbuild/config-language.txt£¬Linux Kernel Source code
Contributing to the Linux Kernel--The Linux Configuration System£¬Linux Journal£¬http://www.linuxjournal.com/