TuyaOpen Compilation Process Detailed Guide
Overviewâ
This document provides a detailed introduction to the compilation process of the TuyaOpen
project, from executing the tos.py build
command to the complete process of generating binary files.
TuyaOpen
uses Python scripts to coordinate CMake/Ninja build tools, supporting multi-platform and multi-configuration compilation.
Compilation Process Architectureâ
tos.py build
â
âââ Environment Check
âââ Configuration Initialization
âââ Platform Download
âââ Platform Preparation
âââ Build Setup
âââ CMake Configuration
âââ Ninja Build
âââ Output Validation
Process Descriptionâ
1. Config Command Execution (cli_config.py)â
When the user executes the tos.py config choice
command:
-
File Replacement
Replace the default
app_default.config
file with the user-selected configuration file. -
Generate CatalogKconfig
Generate the
CatalogKconfig
file in the.build/cache/
directory, containing all Kconfig configuration files.Mainly includes
<project>/Kconfig
,src/Kconfig
, andboards/Kconfig
. -
Generate using.config File
Based on the configuration options provided by
CatalogKconfig
and the configuration results provided by theapp_default.config
file.Generate the
.build/cache/using.config
file.This file contains not only all user-selected code-related configuration information but also some compilation-related data, such as project name, version, platform, development board, etc.
2. Build Command Execution (cli_build.py)â
Related logic can be found in the file: tools/cli_command/cli_build.py
2.1 Environment Checkâ
def env_check():
...
Equivalent command:
git submodule update --init
Ensure all dependent submodules are properly updated.
2.2 Configuration Initializationâ
Generate the .build/cache/using.config
file
init_using_config(force=False)
Main configuration items required for compilation include:
CONFIG_PROJECT_NAME
: Project nameCONFIG_PLATFORM_CHOICE
: Platform selectionCONFIG_CHIP_CHOICE
: Chip modelCONFIG_BOARD_CHOICE
: Development board modelCONFIG_FRAMEWORK_CHOICE
: Framework type
2.3 Platform Downloadâ
def download_platform(platform):
...
Based on the project configuration information and git information provided by the platform/platform_config.yaml
file, download the corresponding hardware platform code.
Platform code is stored in the platform/
directory.
Equivalent command:
git clone <repository_url> <tuyaopen_root>/platform/<platform_name>
cd <tuyaopen_root>/platform/<platform_name>
git checkout <commit_hash>
2.4 Platform Preparationâ
If there is a platform_prepare.py
script in the platform code, execute this script for platform-specific preparation work:
def prepare_platform(platform, chip=""):
...
Please complete the required compilation tool downloads in this script, and it is recommended to download the toolchain in the platform/tools
directory.
Preparation work needed before compilation, such as configuration file updates, parameter settings, etc., can also be implemented in this script.
Equivalent command:
python <tuyaopen_root>/platform/<platform_name>/platform_prepare.py $CHIP
2.5 Build Setupâ
If there is a build_setup.py
script in the platform code, execute this script for build setup:
def build_setup(platform, project_name, framework, chip=""):
...
The purpose of this script is the same as the platform_prepare.py
mentioned in "2.4 Platform Preparation", and the execution timing is also the same.
Therefore, only one of them needs to exist.
Note: Due to some historical reasons in TuyaOpen's development, both scripts may take effect simultaneously
Equivalent command:
python <tuyaopen_root>/platform/<platform_name>/build_setup.py $PROJ_NAME $PLATFORM $FRAMEWORK $CHIP
2.6 CMake Configurationâ
Generate CMake build files:
def cmake_configure(using_data, verbose=False):
...
TuyaOpen
uses the CMake build system, with configuration files located in the <tuyaopen_root>/CMakeLists.txt
file.
Main build steps are described later.
Equivalent command:
mkdir <project_root>/.build
cd <project_root>/.build
cmake -G Ninja $CMAKE_VERBOSE $OPEN_SDK_ROOT -DTOS_PROJECT_NAME=$PROJ -DTOS_PROJECT_ROOT=$PROJECT_ROOT -DTOS_PROJECT_PLATFORM=$PROJECT_PLATFORM -DTOS_FRAMEWORK=$PROJECT_FRAMEWORK -DTOS_PROJECT_CHIP=$PROJECT_CHIP -DTOS_PROJECT_BOARD=$PROJECT_BOARD
2.7 Ninja Buildâ
Execute the actual compilation process:
def ninja_build(build_path, verbose=False):
...
Equivalent command:
cd <project_root>/.build
ninja example
2.8 Output Validationâ
After compilation, verify that the generated binary files meet expectations:
def check_bin_file(using_data):
...
3. CMake Build System (CMakeLists.txt)â
The main CMakeLists.txt file is responsible for the build configuration of the entire project:
<tuyaopen_root>/CMakeLists.txt
3.1 Kconfig Configuration Systemâ
- Use Kconfig to manage configuration options
- Include
using.cmake
configuration - Generate
tuya_kconfig.h
header file
3.2 Toolchain Configurationâ
include("${PLATFORM_PATH}/toolchain_file.cmake")
Configure the specific location of the cross-compilation toolchain and common compilation options.
include("${PLATFORM_PATH}/platform_config.cmake")
Configure header file paths used by the application layer. According to TuyaOpen
's cross-platform design, it should only include header file paths related to tuyaos_adapter
.
3.3 Component Compilationâ
# Compile all components in the src/ directory
list_components(COMPONENT_LIST "${TOP_SOURCE_DIR}/src")
foreach(comp ${COMPONENT_LIST})
add_subdirectory("${TOP_SOURCE_DIR}/src/${comp}")
endforeach(comp)
# Compile board-level code
if(EXISTS "${TOP_SOURCE_DIR}/boards/${TOS_PROJECT_PLATFORM}/${TOS_PROJECT_BOARD}/CMakeLists.txt")
add_subdirectory("${TOP_SOURCE_DIR}/boards/${TOS_PROJECT_PLATFORM}/${TOS_PROJECT_BOARD}")
endif()
Each component can be configured for compilation through the src/<component_name>/CMakeLists.txt
file.
Board-level code is configured for compilation through the boards/<platform>/<board>/CMakeLists.txt
file.
Output: lib<component_name>.a
3.4 Library File Generationâ
# Generate tuyaos static library containing all components
add_library(${COMPONENTS_ALL_LIB} STATIC ${all_need})
Output: libtuyaos.a
, a static library containing all components.
3.5 Application Compilationâ
# Compile application code
set(EXAMPLE_LIB "tuyaapp")
include(${TOS_PROJECT_ROOT}/CMakeLists.txt)
target_link_libraries(${EXAMPLE_LIB} ${COMPONENTS_ALL_LIB})
Application code is configured for compilation through the <project_root>/CMakeLists.txt
file.
Output: libtuyaapp.a
3.6 Platform Buildâ
# Call platform-specific build script
if(EXISTS "${TOP_SOURCE_DIR}/platform/${TOS_PROJECT_PLATFORM}/build_example.py")
set(BUILD_COMMAND python -u ./build_example.py)
Since TuyaOpen
supports multi-platform compilation, all platform-specific compilation logic can be implemented through the platform/<platform_name>/build_example.py
file.
For the main purpose of the script, refer to: build_example.py
4. Key Files and Directory Structureâ
TuyaOpen/
âââ tos.py # Main entry script
âââ tools/
â âââ cli_command/ # CLI command implementation
â â âââ cli_build.py # build command implementation
â â âââ cli_config.py # config command implementation
â â âââ util.py # Utility functions
â â âââ ...
â âââ cmake/ # CMake related files
â âââ kconfiglib/ # Kconfig tools
âââ platform/ # Platform code directory
â âââ platform_config.yaml # Platform configuration
â âââ [platform_name]/ # Specific platforms
âââ src/ # Source code components
âââ boards/ # Board support packages
âââ [project]/ # User project directory
âââ CMakeLists.txt # Project CMake file
âââ app_default.config # Default configuration
âââ .build/ # Build output directory
âââ bin/ # Binary files
âââ lib/ # Library files
âââ cache/ # Configuration cache
5. Compilation Outputâ
After successful compilation, files will be generated in the following locations:
- Binary files:
.build/bin/{app_name}_QIO_{version}.bin
- Library files:
.build/lib/
- Build parameters:
.build/build/build_param
Output information example:
====================[ BUILD SUCCESS ]===================
Target : example_QIO_1.0.0.bin
Output : /path/to/project/.build/bin
Platform : T2
Chip : T2-U
Board : t2_evb
Framework : base
========================================================
Summaryâ
TuyaOpen's build system coordinates CMake/Ninja build tools through Python scripts, achieving:
- Multi-platform support: Support for different hardware platforms through platform abstraction layer
- Configuration management: Use Kconfig system to manage configuration options
- Modular design: Component-based source code structure
- Automated build: One-click compilation, automatic dependency handling
The entire process is clearly designed, easy to extend and maintain, suitable for cross-platform development of IoT devices.