使用 make 命令构建 Linux 内核涉及多个步骤。下面是整个过程的基本概述:
1. 获取内核源代码
* 从 kernel.org 或你的发行版软件仓库下载内核源代码。
* 将源代码解压缩到一个目录中。
2. 配置内核
* 切换到内核源代码目录。
* 可选择清理源代码树:make clean。
* 使用以下方法之一配置内核:
* 使用默认配置:make defconfig 或 make oldconfig。
* 自定义配置:make menuconfig(基于文本)、make xconfig(基于 Qt)或 make gconfig(基于 GTK)。
3. 构建内核
* 运行 make 来构建内核。可选择使用 -j 标志指定并行编译的线程数,后跟线程数(例如,make -j4 表示 4 个线程。)
* 此步骤可能需要一些时间,具体取决于硬件和所选配置选项。
确保在开始编译前安装了必要的开发工具和依赖项(gcc、make、用于 menuconfig/xconfig/gconfig 的 libncurses-dev 等)。
Building the Linux kernel using the make command involves several steps. Here’s a basic overview of the process:
-
Get the Kernel Source:
-
Download the kernel source code from kernel.org or your distribution’s repositories.
-
Extract the source code to a directory.
-
-
Configure the Kernel:
-
Change to the kernel source directory.
-
Optionally, clean the source tree: make clean.
-
Configure the kernel using one of the following methods:
-
Use the default configuration: make defconfig or make oldconfig.
-
Customize the configuration: make menuconfig (text-based), make xconfig (Qt-based), or make gconfig (GTK-based).
-
-
-
Build the Kernel:
-
Run make to build the kernel. Optionally, you can specify the number of threads for parallel compilation using -j flag followed by the number of threads (e.g., make -j4 for 4 threads. j means job).
-
This step might take some time depending on your hardware and the configuration options chosen.
-
Ensure you have the necessary development tools and dependencies installed (gcc, make, libncurses-dev for menuconfig/xconfig/gconfig, etc.) before starting the build process.
补充说明:
1,执行make defconfig,会将当前默认的配置写入.config文件,如果没有就创建。
2,make menuconfig,退出时提示是否保存,会保存到.config文件,如果没有就创建。
3,make时使用的就是.config配置文件,如果此文件发生变化,旧内容保存到.config.old,新内容保存到.config
4, make oldconfig会载入.config.old的内容到.config文件。
在Kernel源码根目录下运行make help:
$ make helpCleaning targets:clean - Remove most generated files but keep the config andenough build support to build external modulesmrproper - Remove all generated files + config + various backup filesdistclean - mrproper + remove editor backup and patch filesConfiguration targets:config - Update current config utilising a line-oriented programnconfig - Update current config utilising a ncurses menu based programmenuconfig - Update current config utilising a menu based programxconfig - Update current config utilising a Qt based front-endgconfig - Update current config utilising a GTK+ based front-endoldconfig - Update current config utilising a provided .config as baselocalmodconfig - Update current config disabling modules not loadedexcept those preserved by LMC_KEEP environment variablelocalyesconfig - Update current config converting local mods to coreexcept those preserved by LMC_KEEP environment variabledefconfig - New config with default from ARCH supplied defconfigsavedefconfig - Save current config as ./defconfig (minimal config)allnoconfig - New config where all options are answered with noallyesconfig - New config where all options are accepted with yesallmodconfig - New config selecting modules when possiblealldefconfig - New config with all symbols set to defaultrandconfig - New config with random answer to all optionsyes2modconfig - Change answers from yes to mod if possiblemod2yesconfig - Change answers from mod to yes if possiblemod2noconfig - Change answers from mod to no if possiblelistnewconfig - List new optionshelpnewconfig - List new options and help textolddefconfig - Same as oldconfig but sets new symbols to theirdefault value without promptingtinyconfig - Configure the tiniest possible kerneltestconfig - Run Kconfig unit tests (requires python3 and pytest)Other generic targets:all - Build all targets marked with [*]* vmlinux - Build the bare kernel* modules - Build all modulesmodules_install - Install all modules to INSTALL_MOD_PATH (default: /)dir/ - Build all files in dir and belowdir/file.[ois] - Build specified target onlydir/file.ll - Build the LLVM assembly file(requires compiler support for LLVM assembly generation)dir/file.lst - Build specified mixed source/assembly target only(requires a recent binutils and recent build (System.map))dir/file.ko - Build module including final linkmodules_prepare - Set up for building external modulestags/TAGS - Generate tags file for editorscscope - Generate cscope indexgtags - Generate GNU GLOBAL indexkernelrelease - Output the release version string (use with make -s)kernelversion - Output the version stored in Makefile (use with make -s)image_name - Output the image name (use with make -s)headers_install - Install sanitised kernel headers to INSTALL_HDR_PATH(default: ./usr)Static analysers:checkstack - Generate a list of stack hogsversioncheck - Sanity check on version.h usageincludecheck - Check for duplicate included header filesexport_report - List the usages of all exported symbolsheaderdep - Detect inclusion cycles in headerscoccicheck - Check with Coccinelleclang-analyzer - Check with clang static analyzerclang-tidy - Check with clang-tidyTools:nsdeps - Generate missing symbol namespace dependenciesKernel selftest:kselftest - Build and run kernel selftestBuild, install, and boot kernel beforerunning kselftest on itRun as root for full coveragekselftest-all - Build kernel selftestkselftest-install - Build and install kernel selftestkselftest-clean - Remove all generated kselftest fileskselftest-merge - Merge all the config dependencies ofkselftest to existing .config.Rust targets:rustavailable - Checks whether the Rust toolchain isavailable and, if not, explains why.rustfmt - Reformat all the Rust code in the kernelrustfmtcheck - Checks if all the Rust code in the kernelis formatted, printing a diff otherwise.rustdoc - Generate Rust documentation(requires kernel .config)rusttest - Runs the Rust tests(requires kernel .config; downloads external repos)rust-analyzer - Generate rust-project.json rust-analyzer support file(requires kernel .config)dir/file.[os] - Build specified target onlydir/file.rsi - Build macro expanded source, similar to C preprocessing.Run with RUSTFMT=n to skip reformatting if needed.The output is not intended to be compilable.dir/file.ll - Build the LLVM assembly fileUserspace tools targets:use "make tools/help"or "cd tools; make help"Kernel packaging:rpm-pkg - Build both source and binary RPM kernel packagesbinrpm-pkg - Build only the binary kernel RPM packagedeb-pkg - Build both source and binary deb kernel packagesbindeb-pkg - Build only the binary kernel deb packagesnap-pkg - Build only the binary kernel snap package(will connect to external hosts)dir-pkg - Build the kernel as a plain directory structuretar-pkg - Build the kernel as an uncompressed tarballtargz-pkg - Build the kernel as a gzip compressed tarballtarbz2-pkg - Build the kernel as a bzip2 compressed tarballtarxz-pkg - Build the kernel as a xz compressed tarballtarzst-pkg - Build the kernel as a zstd compressed tarballperf-tar-src-pkg - Build perf-6.1.55.tar source tarballperf-targz-src-pkg - Build perf-6.1.55.tar.gz source tarballperf-tarbz2-src-pkg - Build perf-6.1.55.tar.bz2 source tarballperf-tarxz-src-pkg - Build perf-6.1.55.tar.xz source tarballperf-tarzst-src-pkg - Build perf-6.1.55.tar.zst source tarballDocumentation targets:Linux kernel internal documentation in different formats from ReST:htmldocs - HTMLlatexdocs - LaTeXpdfdocs - PDFepubdocs - EPUBxmldocs - XMLlinkcheckdocs - check for broken external links(will connect to external hosts)refcheckdocs - check for references to non-existing files underDocumentationcleandocs - clean all generated filesmake SPHINXDIRS="s1 s2" [target] Generate only docs of folder s1, s2valid values for SPHINXDIRS are: PCI RCU accounting admin-guide arc arm arm64 block bpf cdrom core-api cpu-freq crypto dev-tools devicetree doc-guide driver-api fault-injection fb filesystems firmware-guide fpga gpu hid hwmon i2c ia64 iio infiniband input isdn kbuild kernel-hacking leds livepatch locking loongarch m68k maintainer mhi mips misc-devices mm netlabel networking nios2 openrisc parisc pcmcia peci power powerpc process riscv rust s390 scheduler scsi security sh sound sparc spi staging target timers tools trace translations usb userspace-api virt w1 watchdog x86 xtensamake SPHINX_CONF={conf-file} [target] use *additional* sphinx-buildconfiguration. This is e.g. useful to build with nit-picking config.make DOCS_THEME={sphinx-theme} selects a different Sphinx theme.make DOCS_CSS={a .css file} adds a DOCS_CSS override file for html/epub output.Default location for the generated documents is Documentation/outputArchitecture specific targets (x86):* bzImage - Compressed kernel image (arch/x86/boot/bzImage)install - Install kernel using (your) ~/bin/installkernel or(distribution) /sbin/installkernel or install to$(INSTALL_PATH) and run lilofdimage - Create 1.4MB boot floppy image (arch/x86/boot/fdimage)fdimage144 - Create 1.4MB boot floppy image (arch/x86/boot/fdimage)fdimage288 - Create 2.8MB boot floppy image (arch/x86/boot/fdimage)hdimage - Create a BIOS/EFI hard disk image (arch/x86/boot/hdimage)isoimage - Create a boot CD-ROM image (arch/x86/boot/image.iso)bzdisk/fdimage*/hdimage/isoimage also accept:FDARGS="..." arguments for the booted kernelFDINITRD=file initrd for the booted kernelkvm_guest.config - Enable Kconfig items for running this kernel as a KVM guestxen.config - Enable Kconfig items for running this kernel as a Xen guestx86_debug.config - Enable tip tree debugging options for testingi386_defconfig - Build for i386x86_64_defconfig - Build for x86_64make V=0|1 [targets] 0 => quiet build (default), 1 => verbose buildmake V=2 [targets] 2 => give reason for rebuild of targetmake O=dir [targets] Locate all output files in "dir", including .configmake C=1 [targets] Check re-compiled c source with $CHECK(sparse by default)make C=2 [targets] Force check of all c source with $CHECKmake RECORDMCOUNT_WARN=1 [targets] Warn about ignored mcount sectionsmake W=n [targets] Enable extra build checks, n=1,2,3 where1: warnings which may be relevant and do not occur too often2: warnings which occur quite often but may still be relevant3: more obscure warnings, can most likely be ignorede: warnings are being treated as errorsMultiple levels can be combined with W=12 or W=123Execute "make" or "make all" to build all targets marked with [*]For further info see the ./README file