SCons and Kconfig Quick Start
SifliSDK provides an Env configuration tool, which already includes SCons and Python. Therefore, there is no need to install SCons on the Windows platform. SifliSDK adds some script libraries on top of SCons to simplify the project configuration for users.
1. Project File Structure
SCons uses SConscript and SConstruct files to organize the source code structure. Typically, a project has only one SConstruct file located in the root directory, but it may have multiple SConscript files. Generally, each subdirectory containing source code will have a corresponding SConscript file. Users can refer to example\uart\project\ec-lbxxx to see the project file structure.
SifliSDK creates a file called rtconfig.py for each project. Therefore, each project directory will contain the following three files: rtconfig.py, SConstruct, and SConscript, which control the project compilation. A project has only one SConstruct file but may have multiple SConscript files. The SConscript file is the main organizer of the source code.
SifliSDK uses menuconfig to configure the project. Each project has a Kconfig file in the root directory, which serves as the starting point for configuration options. After configuration, .config and rtconfig.h files are generated in the project root directory, which are used by SCons scripts and C files respectively.
Most source code folders in SifliSDK also have SConscript files. These files are “found” by the SConscript file in the project root directory, which adds the corresponding source code defined by the macros in .config/rtconfig.h to the compiler.
The first file executed in SCons compilation is SConstruct. The SConstruct in the user’s project can configure some project options. Together with the project configuration generated by the menuconfig tool (.config), the SifliEnv()
function sets the default compilation options. If users need to override the compilation/linking options, they can modify the corresponding parameters in the Environment()
function.
3. Basic Functions of SCons
You can compile the project directly by entering the scons
command in the Env tool. By default, it uses the ARM CLANG compiler. The SifliSDK root directory contains a set_env.bat file, which sets the compiler directory.
If you want to specify your own compiler, you can modify the commands in set_env.bat:
set RTT_CC = keil
set RTT_EXEC = C:/Keilv5
Scons command This command not only compiles the project but also generates MDK/IAR/VS projects. Adding different parameters will result in different effects.
The
-s
parameter will suppress printing of internal commands.The
-c
parameter will clean the build targets, temporary files, and object files.The
--target=XXX
parameter is used when using MDK/IAR for project development. If you modify rtconfig.h to enable or disable certain components, you need to use this parameter to regenerate the corresponding customized project and then compile it in MDK/IAR.
Warning
Note: To generate MDK or IAR project files, the prerequisite is that the project directory contains a project template file. Only then will scons
generate the relevant source code, header file search paths, compiler parameters, and linker parameters based on the template. The template file typically specifies which chip the project is for. Therefore, in most cases, the template file is an empty project file, used to assist SCons in generating project.uvprojx or project.eww. To make it easier for customers, if no project.uvprojx is found, SiFliSDK will use a default template to generate project.uvprojx. If you don’t want to use the default template (for example, if you want to add your own compilation options), you can copy the template.uvprojx file from SifliSDK\tools\build\template into your project directory as your own template and make the necessary changes.
The
-jN
parameter enables multi-threaded compilation. On a multi-core machine, you can use this command to speed up the compilation. Generally, one CPU core can support two threads. On a dual-core machine, usescons -j4
.The
--verbose
parameter is used to display the compilation parameters. By default, the output of thescons
command does not show compilation parameters, but with this parameter, the compilation parameters will be displayed.
4. Built-in Functions of SCons
If you want to add some source code to the SCons compilation environment, you can generally create or modify an existing SConscript file.
The SConscript file controls the inclusion of source code files and allows you to specify the file’s group (similar to the concept of groups in MDK/IAR IDEs).
SCons provides many built-in functions to help us quickly add source code. By using these functions in combination with simple Python statements, we can freely add or remove source code from the project. Below are some commonly used functions. In fact, each SConscript file is a Python file.
GetCurrentDir()
function: Gets the current directory.Glob('*.c')
function: Gets all C files in the current directory. You can modify the suffix in the parameter to match files of a specific type.GetDepend(macro)
function: Defined in the script file in the tools directory, this function reads configuration information from the rtconfig.h file. Its parameter is the macro name from rtconfig.h. If a macro is enabled in rtconfig.h, this function will return true; otherwise, it returns false.Split(str)
function: Splits the stringstr
into a list.DefineGroup(name, src, depend, **parameters)
function: This is a method (function) extended from SCons.DefineGroup
is used to define a component. A component can be a directory (including files or subdirectories) and also a group or folder in subsequent IDE project files. The parameters can include:LIBS=<some.lib>
: Links a specific library into the project.LIBPATH=<some path>
: Adds a library path for searching.LIBRARY=<some.lib>
: Compiles and links all source code in the group to generate a .lib file.INSTALL_PATH=<some path>
: Installs the generated .lib file to a specific directory.CCFLAGS=<more CC flags>
: Adds compiler options.CPPPATH=<some folder>
: Adds a C header file search directory.CPPDEFINES=<more macro>
: Adds C macro definitions.LINKFLAGS=<more link flags>
: Adds linker options.ASFLAGS=<more assembly flags>
: Adds assembly options.