Build System

Last Updated on : 2023-11-27 08:33:37download

TuyaOS implements a modular build system on top of the syntax of the Android NDK’s Android.mk. The build system allows you to build firmware with one click in Tuya Wind IDE. You can also add the allowed files or folders in Tuya Wind IDE. The build system automatically loads them from the specified location to compile.

Build system

In the TuyaOS development framework, the build system consists of the following files.

  • The build_app.sh file in the root directory.
  • The Makefile file in the root directory.
  • The *.mk files in the ./scripts/mk/ directory.
  • The local.mk file in the respective source code directory.
  • The build.sh file in the vendor/$(TARGET_PLATFORM)/tuyaos/tuyaos_adapter directory.

build_app.sh

build_app.sh is the entry point to compilation. This script is called when you click Build Project in Tuya Wind IDE. The following parameters are required to run build_app.sh.

sh build_app.sh $1 $2 $3 $4
  • $1: the path of the application project, for example, apps/tuyaos_demo_quickstart.
  • $2: the name of the application, for example, tuyaos_demo_quickstart.
  • $3: the version number, in the format XX.XX.XX, for example, 1.0.0.
  • $4: user command (optional), for example, clean.

The build output by build_app.sh is stored in the $1/output directory, with a name in the format $2_xx_$3.bin. xx indicates the purpose of the firmware, QIO for production firmware, UG for firmware updates, and UA for user area firmware.

Makefile

Makefile, the entry point to the build system, includes all the *.mk files and local.mk files. To compile your code, you can click Build Project in Tuya Wind IDE or run build_app.sh with parameters or make on the command line. In either of the ways, Makefile is executed to perform compiling and linking for the entire TuyaOS.

When you run the make command in Tuya Wind IDE, you are required to specify the application to build. The version number defaults to 1.0.0.

local.mk

local.mk describes how to build the specified directory in terms of the source file, header file, compilation options, and object file. The local.mk file is literally a tiny GNU makefile fragment. The build system parses local.mk and compiles the source code accordingly.

The following is an example of local.mk:

LOCAL_PATH := $(call my-dir)    # The directory where the current file resides.
#----
include $(CLEAR_VARS)    # Clear LOCAL_xxx variables.
LOCAL_MODULE := $(notdir $(LOCAL_PATH))    # The name of the current module.

# External header file, which should be specified as a directory.
# Load to CFLAGS to allow other components to access, which will be packaged into the SDK output.
LOCAL_TUYA_SDK_INC := $(LOCAL_PATH)/include

# External CFLAGS that the build system is aware of when compiling other components.
LOCAL_TUYA_SDK_CFLAGS :=

# Source code
LOCAL_SRC_FILES := $(shell find $(LOCAL_PATH)/src -name "*.c" -o -name "*.cpp" -o -name "*.cc")

# Internal CFLAGS, which applies to this component only.
LOCAL_CFLAGS :=

# Assign values to global variables
TUYA_SDK_INC += $(LOCAL_TUYA_SDK_INC)  # Do not edit this line.
TUYA_SDK_CFLAGS += $(LOCAL_TUYA_SDK_CFLAGS)  # Do not edit this line.

include $(BUILD_STATIC_LIBRARY)    # Generate static library
include $(BUILD_SHARED_LIBRARY)    # Generate dynamic library
#----

Generally, you can find local.mk from the ./apps/$(APP_NAME) directory and the ./vendor/$(TARGET_PLATFORM)/tuyaos/tuyaos_adapter/ directory in Tuya Wind IDE.

*.mk

There are multiple *.mk files stored in the ./scripts/mk/ directory. Typically, the mk files are used for two purposes:

  • Describe the targets to compile TuyaOS and applications.

    For example, ./scripts/mk/app.mk describes the targets to compile firmware. Explicit targets:

    • app_comp_static: Copy ./apps/$(APP_NAME)/libs/libtuyaapp_components.a to the ./libs/ directory.
    • app_driv_static: Copy ./apps/$(APP_NAME)/libs/libtuyaapp_drivers.a to the ./libs/ directory.
    • app_adapter_static: Copy $(OUTPUT_DIR)/lib/libtuyaos_adapter.a to the ./libs/ directory.

    local.mk also describes how to build your project:

    • ./vendor/$(TARGET_PLATFORM)/tuyaos/tuyaos_adapter/local.mk is compiled into $(OUTPUT_DIR)/lib/libtuyaos_adapter.a.
    • ./apps/$(APP_NAME)/local.mk is compiled into $(OUTPUT_DIR)/lib/lib$(APP_NAME).a.

    These targets are dependencies for app_by_name. The default target for the make command is app. This allows you to select appropriate objects to compile and call app_by_name to compile app_comp_static, app_driv_static, and app_adapter_static. Then, call build.sh in vendor/$(TARGET_PLATFORM)/tuyaos/tuyaos_adapter to perform linking.

  • Describe rules and actions: For example, ./scripts/xmake.mk provides functions for compilation. You can include xmake_static.mk or include xmake_shared.mk at the end of the file to compile a static library or a dynamic library.

build.sh

After TuyaOS is compiled and the generated libraries are saved in the ./libs/ directory, the build system executes build.sh to compile the chip vendor components, link the generated libraries, and package firmware.

  • Compiling chip vendor components: The open source components from chip vendors, such as drivers, can be edited to your needs. They are compiled after TuyaOS compilation.

  • Linking: The compiled output, including TuyaOS libraries and open or closed source chip vendor libraries, is merged into a binary file.

  • Packaging: The bootloader, binary files, and configuration files are packaged into different types of firmware and saved to the corresponding directory.

    In the linking phase, the TuyaOS-based *.a file in the ./libs directory can be added to the LDFLAGS in this order: -l$(APP_BIN_NAME) -ltuyaapp_components -ltuyaapp_drivers -ltuyaos -ltuyaos_adapter.

Build actions

There are three options to build a project with Tuya Wind IDE:

  • Option 1: In the sidebar, right-click on the target subdirectory of software/TuyaOS/apps/ and choose Build Project.

  • Option 2: On the command line, run sh build_app.sh $1 $2 $3 $4. For more information about the required parameters, see build_app.sh.

  • Option 3: On the command line, run make and specify the application to compile. The version number defaults to 1.0.0.

    The following figure shows the build process:

    Build System

Build extensions

The build system automatically loads local.mk in the default locations and compiles the source code in these locations according to the description in local.mk. The following table lists these locations:

Folder Purpose Output
apps/$(APP_NAME) The directory of executable programs, with the program entry point included generally. lib$(APP_NAME).a
vendor/$(TARGET_PLATFORM)/tuyaos/tuyaos_adapter TuyaOS kernel Adapter implementation libtuyaos_adapter.a

Add file (folder)

Add the source code file to the apps/$(APP_NAME)/src directory that the build system looks up for the source code to compile.

In the apps/$(APP_NAME)/ directory, local.mk automatically looks up apps/$(APP_NAME)/src/ using a shell script for all the source code files and adds them to the variable LOCAL_SRC_FILES for compiling to generate lib$(APP_NAME).a library.

Use chip vendor interfaces

To use the interfaces provided by the chip vendor, you need to include the header file, call functions, and add the path of the header file that contains function declarations to the local.mk in the respective directory. Example:

# Source code
LOCAL_SRC_FILES := $(shell find $(LOCAL_PATH)/src -name "*.c" -o -name "*.cpp" -o -name "*.cc")

# Internal CFLAGS, which applies to this component only.
LOCAL_CFLAGS += -I$(ROOT_DIR)/vendor/bk7231n/bk7231n_os/beken378/func/user_driver
LOCAL_CFLAGS += -I$(ROOT_DIR)/vendor/bk7231n/bk7231n_os/beken378/driver/include
LOCAL_CFLAGS += -I$(ROOT_DIR)/vendor/bk7231n/bk7231n_os/beken378/common
LOCAL_CFLAGS += -I$(ROOT_DIR)/vendor/bk7231n/bk7231n_os/beken378/app/config
LOCAL_CFLAGS += -I$(ROOT_DIR)/vendor/bk7231n/bk7231n_os/beken378/driver/common
LOCAL_CFLAGS += -I$(ROOT_DIR)/vendor/bk7231n/bk7231n_os/beken378/os/include
LOCAL_CFLAGS += -I$(ROOT_DIR)/vendor/bk7231n/bk7231n_os/beken378/os/FreeRTOSv9.0.0

Support and help

If you have any problems with TuyaOS development, you can post your questions in the Tuya Developer Forum.