Makefile interpretation http://www.chinaunix.net Author: snow lone Posted: 2003-02-09 12:27:09
From the original: http: //www.linuxforum.net Author: jkl =================================== ======= Makefile is primarily ========================================= = Linux kernel profile has two, one is an implicit .config file, embedded in the main Makefile; the other is include / Linux / AutoConf.h, embedded in each C source file, which is made by Make Config, Make MenuConfig, Make Xconfig These processes are created. Almost all source files are embedded in AutoConf.h through Linux / Config.h, and if the file-dependent relationship (.depend) is established in accordance with the usual method, you will cause all source code to re-add. To optimize the MAKE process, reduce unnecessary re-dictation, Linux has developed a dedicated MKDEP tool, with it to replace GCC to generate .depend files. MKDEP ignores the header files such as Linux / config.h when processing the source file, identifies the row with the "config_" feature in the source file macro. For example, if there is a row such as "#ifdef config_smp", it will output $ (Wildcard /usR/src/linux/include/config/smp.h) in the .depend file. Include / config / down file is another tool split-include generated from AutoConf.h, which uses the config_ tag in AutoConf.h to generate files corresponding to MKDEP. For example, if there is "#undef config_smp" in AutoConf.h, it generates the include / config / Smp.h file, the content is "#undef config_smp". These file names appear in the .depend file, the kernel source file is not embedded. Run split-incrude once each time the kernel is configured. Split-incrude will check the contents of the old subfiles, determine if they want to update them. In this way, regardless of the autoconf.h modification date, as long as it is configured, Make will not re-reconfigure the kernel. If the system's finite options have changed, Linux can also add increment. In order to do this, Make generates a FLAGS file every time you have done a source file. For example, when adding Sched.c, it will generate an implicit .sched.O.Flags file in the same directory. It is a piece of Makefile. When Make enters a subdirectory, the Flags files are searched, and they are embedded in the Makefile.
These FLAGS code test The current documented option is not the same. If the same is the same, add the corresponding target file to the files_flags_up_to_date list, then remove them from the dialogue table, get the Files_Flags_Changed list, finally, Set to the target update. The next step is prepared to gradually analyze the Makefile code. =================================================== makefile interpretation 2: SUB-MAKE ===============================================================================================================================================================】 Directory has Makefile, most makefiles are embedded in the rule.make under the main directory, and Rule.make will identify some variables defined in each Makefile. Variable OBJ-Y indicates that the target file name collection needs to be reformed, defined the O_Target represents the target file that connects the OBJ-Y to a O_Target name, defines the L_Target represents the library file that combines the OBJ-Y into a L_Target name. The same OBJ-M indicates a collection of target file names that need to be edited into a module. If you need subdirectory Make, you need to define subDir-y and subdir-m. In Makefile, "OBJ-$ (config_binfmt_elf) = binfmt_elf.o" and "SubDir - $ (config_ext2_fs) = ext2" are automatically OBJ-Y, OBJ-M, SUBDIR-Y, SUBDIR-M Add a file name. Sometimes, the situation is not so simple, and you need to use the conditional statements. There are other variables in Makefile, such as MOD-SUBDIRS defining all module subdirectories other than SubDIR-M. How does Rules.make enters the subdirectory? Let's first look at how the subdir-y is handled. In Rules.make, first add prefix "_subdir_" to each file name in SubDIR-Y. Generate a subDir-list collection, then use it as a target set, generate a sub-Make in each of the targets, while the prefix of the target name removes the sub-directory name as the start directory parameters of the subkey. Subdir-m is similar to subdir-y, but the situation is slightly more complicated. Since SubDIR-Y may have a module definition, extract the SubDIR-Y module directory using the mod-subdirs variable, and then synthesize a large MOD_SUB_DIRS collection with SUBDIR-M. The prefix used by the target of SubDIR-M is "_ModsubDir_". One point, Makefile in the subdirectory is not embedded in .config files, it is done by the master makefile to complete the MakeFiles variable.
Makefiles is a variable from Make's own identified, and Make will first load the files referred to in Makefiles before performing new makefile. It is pointing to .config in the primary makefile. ==================================================================3: Module version of the module Treatment =================================================== 版 版 版 版 版 内 内 内A method of strict type matching between module interfaces. When the kernel is configured by config_modversions, the Make DEP operation generates an extension called .ver file for the source file corresponding to the Export-Objs variable in the include / linux / modules / directory. For example, for kernel / ksyms.c, make use the following command to generate the corresponding Ksyms.ver: GCC -E -D__kernel__ -d__genksyms__ ksyms.c | / sbin / genksyms -k 2.4.1> ksyms.ver -d__genksyms__ role is to make The export_symbol macro in Ksyms.c is not expanded. The genksyms command identifies the function name and the corresponding prototype in Export_Symbol (), and then calculates the version number of the function according to its prototype. For example, Ksyms.c has a row: export_symbol (kmalloc); kmalloc prototype is: void * kmalloc (size_t, int); Genksyms program corresponding to: #define __ver_kmalloc 93d4cfe6 #define kmalloc _Set_VER (kmalloc) in kernel symbol table and module In the middle, Kmalloc will become kmalloc_r93d4cfe6. After generating all the .ver files, make will rebuild the include / Linux / modversions.h file, which contains a series of #include instruction lines embedded in each .ver file. When the file in the EXPORT-OBJS in the kernel itself, Make adds a "-dexport_symtab", which enters the source file into the modversions.h file, and the function name string in the Export_Symbol macro is expanded; At the same time, it also defines the _SET_VER () macro to an empty operation, so that the function name in the code is not affected. When the module is editing, make adds "-include = Linux / modversion.h -dmodversions" to remember, enables the function name of the code in the module to be expanded. Because the generated .ver file is time consuming, Make also creates a suffix for each .stamp time stamp file. When Make Dep, if it .stamp file is re-generated .ver file, it is just an update .stamp file timestamp.
In addition, when generating .ver and modversions.h files, Make compares the content of new files and old files, keeping them the oldest. =================================================== makefile interpretation 4: Rules.make Note =================================================== [Code: 1: 974578564B ] # # This file contains rules which area. # # False targets. # # .Phony: Dummy # # Special Variables Which Should Not Be Exported # # Cancel These variables pass through the environment to the Make child process. unexport switch unexport EXTRA_ARFLAGS # ar switching switch unexport EXTRA_LDFLAGS # ld switch unexport EXTRA_CFLAGS # cc of EXTRA_AFLAGS # as the unexport SUBDIRS # unexport SUB_DIRS # unravel the core into the required subdirectory, equal subdir-y unexport ALL_SUB_DIRS # all subdirectories UNEXPORT MOD_SUB_DIRS # NEXPORT O_TARGET # LD Merged Output O_Mobjs # All Module Name UNEXPORT OBJD Y # All Module Name UNEXPORT OBJ-Y # File Sets UNEXPORT OBJ-M # Add into Module File Set UNEXPORT OBJ-N # UNEXPORT OBJ- # UNEXPORT EXPORT-OBJS # Requires version processing Unexport Subdir-y # Remove subdirectory UNEXPORT SUBDIR-M # Remembered subdirectory UNEXPORT Subdir-n Unexport Subdir- # Get Things Started. # first_rule: Sub_dirs $ (Make) All_targets # Filters the subdirectory that can be used as a module in the kernel. Both-M: = $ (Filter $ (MOD-SUBDIRS), $ (SUBDIR-Y)) Sub_DIRS: = $ (SUBDIR-Y) # 总 总 模 子 子 m (Sort $ (Sort $ (SUBDIR-M) $ (Both-M)) # 总子 目 目 all_sub_dirs: = $ (Sort $ (Subdir-Y) $ (SUBDIR-N) $ (Subdir-)) # # Common Rules # # The C file is completed into the rules of the assembly file, $ @ is the target object.
% .S:% .C $ (cc) $ (cflags) $ (cflags _ $ @) $ <-o $ @ # generates a rule of a pre-processed file. % .i:% .C $ (CFLAGS) $ (extra_cflags) $ (cflags _ $ @) $ @ @ # 文件 文件 文件 目 目 规 Rule, $ < Objects; # Generate a Flags file in the directory of the target file, Strip deletes extra spaces, substers replace the comma to colon. % .O:% .C $ (cflags) $ (extra_cflags) $ (cflags _ $ @) -c-@ @ @ (/ echo 'iFeq ($ (Since $ (Subst $ (COMMA) ,:, $ (CFLAGS) $ (CFLAGS _ $ @))), $$ (Strip $$ (Subst $$ (CommA), $$ (CFLA_CFLAGS) $$ (CFLAGS_ $ @)))) '; / Echo' files_flags_up_to_date = $ @ '; / echo' endif '/)> $ (dir $ @) /. (notdir $ @). Flags # assembly file generates the rule of the target file . % .O:%. S $ (AFLAGS) $ (extra_cflags) -o $ @ $ <# old makefiles define their @ o o o Compiling .s files, # Buttedard Rules Are Available for Any Makefile That # Wants to use the the. Our plan. - RMK, MEC ifdef use_standard_as_rule # assembled files Generate standard rules for preprocessing files. % .S:%. S $ (cpp) $ (extra_aflags) $ (AFLAGS _ $ @) $ <> $ @ # assembled file generated standard rules for target files. % .O:%. S $ ((Aflags) $ (extra_aflags) $ (AFLAGS _ $ @) -c-@ @ $ IFNEQ "$ (Strip $ (OBJ-Y)" "" "$ (ld) $ (extra_ldflags) -r -O $ @ $ (Filter $ (OBJ-Y), $ ^) ELSE $ (ar) RCS $ @ Endif # Generates the shell statement of the Flags file. @ (/ Echo 'IFEQ ($ Strip $ (Subst $ (Subst $ (Comma) $ (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 inte .a File # # combines the OBJ-Y to a library L_Target. IFDEF L_TARGET $ (L_Target): $ (OBJ-Y) RM -F $ @ $ (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 dependenndencies Quickly # # Wildcard is in the lookup directory The macro of the file name. FastDep: Dummy $ (TOPDIR) / Scripts / MKDep $ (Wildcard *. [CHS] local.h.master)> .depend iFdef all_sub_dirs # Plus all the directory names in all_sub_dirs Add a prefix _sfdep_ as the target operations Make, and Pass all_sub_dirs pass the # variable _fastDep_all_sub_dirs to subkey. $ (Make) $ (PatSubst%, _ SFDEP_A, $ (all_sub_dirs)) _fastDep_all_sub_dirs = "$ (all_sub_dirs)" Endif ifdef _fastDep_all_sub_dirs) corresponds to the previous paragraph, the subdirectory target, and the target name is restored as a directory name, enter The subdirectory make. $ (PATSUBST%, _ sfDep_%, $ (_ fastdep_all_sub_dirs): $ (MAKE) -C $ (PatSubst _sfDep_%,%, $ @) fastdep endif # # a rule to make subdirector # # 下 2 paragress complete kernel series Make in the catalog. 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 "" "" # 取 目 目 目 TOPDIR to the current directory path. PDWN = $ (SHELL $ (Config_Shell) $ (TOPDIR) /SRIPTS/PathDown.sh) Endif Unexport MOD_DIRS MOD_DIRS: = $ (MOD_SUB_DIRS) $ (MOD_IN_SUB_DIRS) # When the module is entered into the module subdirectory. IFNEQ "$ (Strip $ (MOD_DIRS))" "" ".phony: $ (PatSubst%, _ MODSUBDIR _%, $ (MOD_DIRS)) $ (PatSubst%, _ MODSUBDIR _%, $ (MOD_DIRS)): Dummy $ (Make) -c $ (PatSubst _Modsubdir_%,%, $ @) Modules # When the module is installed, the method of entering the module subdirectory. .Phony: $ (PatSubst%, _ Modinst_%, $ (MOD_DIRS)) $ (PatSubst%, _ MODINST _%, $ (MOD_DIRS)): Dummy $ (Make) -C $ (PatSubst _Modinst_%,%, $ @) modules_install Endif # Make Modules entrance. .Phony: modules modules: $ (all_mobjs) Dummy / $ (PatSubst%, _ MODSUBDIR _%, $ (MOD_DIRS)). Phony: _modinst__ # copy module process. _Modinst__: Dummy IFNEQ "$ (Strip $ (all_mobjs))" "" MKDIR -P $ (MODLIB) / KERNEL / $ (PDWN) CP $ (ALLIB) / kernel / $ (pdwn) Endif # Make MODULES_INSTALL The entrance, enter the subdirectory installation. .Phony: modules_install modules_install: _Modinst__ / $ (Patsubst%, _ MODINST_MOD_D_DIRS)) # # a rule to do not 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 those listed composite module formed by a plurality of files; # The composite module name is filtered from the finished file table and module file table. Multi-used: = $ (Filter $ (List-Multi), $ (OBJ-Y) $ (OBJ-M)) #s the component table of the composite module. Multi-Objs: = $ (Foreach M, $ (Multi-use), $ ($ (BaseName $ (m)) - OBJS) # 总 总 总 模 模 模 模Active-Objs: = $ (Sort $ (Multi-Objs) $ (OBJ-M)) IFDEF Config_Modversions IFDEF Config_Modversions IFNEQ "" "" "" "" "" "" Document. 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 # From the source file calculates the rule of the version file. $ (MODINCL) /%. Ver:% .c @if [! -R $ (model) / $ *. Stamp -o $ (model) / $ *. Stamp -ot $ <]; life / echo '$ CC) $ (cflags) -e -d__genksyms__ $ <'; / echo' | $ (genksyms_smp_prefix) -k $ (version). (Patchlevel). $ (SUBLEVEL)> $ @. Tmp '; / $ (Cc) $ (cflags) -e -d__genksyms__ $ | $ (GenksYMS) $ (Genksyms_Smp_prefix) - K $ (Version). $ (SUBLEVEL)> $ @. TMP; / IF [ R $ @】 && cmp - S $ @. tmp; the echo $ @ @. tmp; / else echo mv $ @. TMP $ @; mv -f $ @. TMP $ @ @ @; @; Fi; / fi; Touch $ (MODINCL) / $ *. Stamp # transform the extension of the version processing source file to .ver and add a full path name, depending on AutoConf.h? Br>? Br> $ (AddPRefix $ (Modincl) /, $ (export-objs: .o = .ver): $ (topdir) /include/linux/autoconf.h # Updates.h # By FastDep, one by one Generate the version file corresponding to the Export-Objs. FastDep: $ (AddPRefix $ (Modincl) /, $ (Export-Objs: .o = .VER) # Updates .ver files and modvers.h like before (is this needed?) # Make DEP process portfolio DEP: fastDep Update-ModverFile Endif # Export-Objs # Update Modversions.h, But Only IT Would Change # Refresh the version of the file. Update-modverfile: @ (echo "#ifndef _linux_modversions_h"; / echo "#define _linux_modversions_h"; / echo "#include