OpenWrt, as a widely used Linux distribution for embedded devices, has a cleverly designed project directory structure. Understanding the role of each directory is crucial for developers.
This article will discuss the original project directory structure of OpenWrt and the changes in each directory during the compilation process.
Original Project Directory Structure of OpenWrt
After cloning the OpenWrt source code from the official repository, you will see the following typical directory structure:
openwrt/├── config/ # Global configuration directory├── include/ # Compilation system header files├── package/ # Software package directory├── scripts/ # Various script files├── target/ # Target platform related configuration├── toolchain/ # Toolchain configuration├── tools/ # Tools required for compilation└── Makefile # Main Makefile
config/ Directory
This directory contains global configuration files and default configurations. The Config.in file defines all configurable options, while autoconf.h is generated during the configuration process.
include/ Directory
This directory includes various header files and macro definitions required by the compilation system. The most important ones include:
package.mk: Package compilation rules
kernel.mk: Kernel compilation configuration
host-build.mk: Host tool compilation configuration
package/ Directory
This is the largest directory, containing all optional software packages. Each subdirectory represents a software package and includes:
Makefile: Package compilation rules
patches/: Directory for patch files
files/: Directory for configuration files
target/ Directory
This directory contains configurations specific to the target platform, such as:
linux/: Kernel configurations for various platforms
image/: Image generation configurations
Changes in Directory During Compilation Process
1. Initial Configuration Stage
After executing make menuconfig
A .config file is generated: stores all configuration options
A tmp/ directory is created: for temporary files and configuration cache
An include/autoconf.h file is generated: configuration in C header file format
2. Toolchain Compilation Stage
After executing make toolchain/install
A staging_dir/ directory is created, containing:
toolchain-*/: Cross-compilation toolchain
target-*/: Libraries and header files for the target platform
host/: Host tools
3. Software Package Compilation Stage
After executing make package/compile
A build_dir/ directory is created: compilation working directory for each software package
A bin/ directory is created: final generated ipk packages and firmware
Within build_dir/, a target-*/ directory is created for target platform-related builds
A host/ directory is created for host tool builds
A toolchain-*/ directory is created for toolchain-related builds
4. Image Generation Stage
After executing make target/install:
The final firmware is generated in bin/targets/
A rootfs structure is created in the tmp/ directory
Various formats of image files are generated
Detailed Explanation of Key Directories
staging_dir/ Directory
This is an important directory during the compilation process, containing:
toolchain: Cross-compilation toolchain
target: Libraries and header files for the target platform
host: Host tools
build_dir/ Directory
This is the actual compilation working directory, where each software package will unpack its source code and compile.
dl/ Directory
This directory caches downloaded source packages to avoid repeated downloads.
feeds/ Directory
When using the feeds mechanism, additional software packages will be placed here.
Compilation Process Example
Pulling code and compiling
# Clone the source codegit clone https://git.openwrt.org/openwrt/openwrt.git
# Update feeds./scripts/feeds update -a./scripts/feeds install -a
# Configuremake menuconfig
# Start compilation# -j number of processing threads | V=s detailed output of the compilation process logmake -j6 V=s
During the compilation process, you will observe:
dl/
The directory gradually fills with downloaded source packages, including compressed packages of specified versions of the Linux source code.
build_dir/
The directory begins to show compilation directories for various software, such as the specified version of the Linux source code being unpacked here.
staging_dir/
The directory gradually completes the toolchain and target files.
bin/
The directory ultimately generates firmware and ipk packages.
Practical Tips
Clean compilation: Cleans compilation results while retaining configuration and download cache
make clean
Full clean: Thoroughly cleans all generated files
make dirclean
Compile a specific package separately
make package/<package_name>/compile
View dependencies: Check package dependencies
make package/<package_name>/prepare
Understanding the directory structure of OpenWrt not only helps in troubleshooting issues during the compilation process but also aids in better customizing your OpenWrt system.