Application compilation methods, compilation systems, and electronic devices
By generating platform configuration information and static library files from configuration files that share the same configuration facts, the problem of configuration drift caused by inconsistencies in compilation configurations between C and Rust languages is solved, thus achieving efficient application compilation.
Patent Information
- Application Number
- CN202611424050.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-09-14
- Publication Date
- 2026-11-17
AI Technical Summary
In existing technologies, compilation configuration information for C and Rust languages needs to be maintained separately for the same application, which leads to configuration drift and consequently causes compilation errors in the Rust application.
An application compilation method is provided, which generates platform configuration information for a first language and static library files for a second language by obtaining a first configuration file and a second configuration file that represent the same configuration facts, and generates target compilation units based on these files, thereby avoiding compilation errors caused by inconsistent configuration information.
By converging multiple scattered manual configurations into a build process driven by a single configuration source, configuration drift is avoided and compilation efficiency is improved.
Smart Images

Figure CN123114477A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of software compilation technology, and more specifically, to an application compilation method, compilation system, and electronic device. Background Technology
[0002] An embedded system platform may support both C and Rust applications. Therefore, the compilation process for embedded applications may involve compiling both C and Rust programs.
[0003] In existing technologies, for a single application, separate sets of C language source code and Rust language source code are maintained. Correspondingly, separate sets of compilation configuration information and rules for C and Rust are also required. For example, when the C language version of the application needs to be installed on a certain platform, compilation is performed based on the application's C language compilation configuration information and rules.
[0004] However, in the existing technology, C language compilation configuration information and Rust language compilation configuration information need to be maintained separately for the same application, which may lead to configuration drift and cause the Rust application to compile incorrectly. Summary of the Invention
[0005] The purpose of this application is to address the shortcomings of the prior art by providing an application compilation method, compilation system, and electronic device to solve the problem of configuration drift during compilation in the prior art.
[0006] To achieve the above objectives, the technical solutions adopted in the embodiments of this application are as follows: Firstly, this application provides an application compilation method, including: Obtain the first configuration file and the second configuration file corresponding to the target application. The configuration facts represented by the first configuration file and the second configuration file are the same. The first configuration file is in key-value text format and the second configuration file is in second language macro definition format. Based on the first configuration file, platform configuration information suitable for the first language is generated, and multiple system function compilation units are compiled based on the platform configuration information. The second configuration file generates a static library file for the second language, and the second configuration file generates an external function interface declaration for the first language. Based on the static library file and the external function interface declaration, generate the target compilation unit; The target application in the first language is generated based on the multiple system function compilation units, the target compilation unit, and the static library files.
[0007] Secondly, this application provides a compilation system for performing the steps of the application compilation method described in the first aspect.
[0008] Thirdly, this application provides an electronic device in which the compilation system described in the first aspect is deployed; the electronic device includes a processor and a memory, the memory storing machine-readable instructions executable by the processor, and when the electronic device is running, the processor executes the machine-readable instructions to perform the steps of the application compilation method described in the first aspect.
[0009] The application compilation method, compilation system, and electronic device provided in this application first obtain a first configuration file and a second configuration file representing the same configuration fact. Then, based on the first configuration file, platform configuration information suitable for a first language is generated, and multiple system function compilation units are generated based on the platform information. Next, based on the second configuration file, static library files for a second language and external function interface declarations for the first language are generated. Target compilation units are generated based on the static library files and external function interface declarations. Finally, a target application in the first language is generated based on the system function compilation units, target compilation units, and static library files. Since the process of compiling the target application in the first language is based solely on the same first and second configuration files, without requiring independent configuration for each stage, the previously scattered multiple manual configurations are converged into a first language-side build process driven by a single configuration source. This avoids configuration drift and first language compilation errors caused by inconsistencies in the first and second language-side configuration information. Furthermore, generating system function compilation units, target compilation units, and static library files based on the same first and second configuration files significantly improves compilation efficiency. Attached Figure Description
[0010] To more clearly illustrate the technical solutions of the embodiments of this application, the accompanying drawings used in the embodiments will be briefly introduced below. It should be understood that the following drawings only show some embodiments of this application and should not be regarded as a limitation of the scope. For those skilled in the art, other related drawings can be obtained based on these drawings without creative effort.
[0011] Figure 1 A schematic diagram of the architecture of a compiler system provided in this application; Figure 2 A flowchart illustrating the application compilation method provided in this application; Figure 3 A flowchart illustrating the generation platform configuration information for the application compilation method provided in this application; Figure 4 A flowchart illustrating the compilation unit of the system for generating the application compilation method provided in this application; Figure 5 A schematic diagram illustrating the process of generating static library files for the application compilation method provided in this application; Figure 6 A schematic diagram illustrating the process of generating external function interface declarations for the application compilation method provided in this application; Figure 7 A flowchart illustrating the process of generating target compilation units for the application compilation method provided in this application; Figure 8 A schematic diagram illustrating the process of generating the target application using the application compilation method provided in this application; Figure 9 This is a schematic diagram of the structure of an electronic device provided in an embodiment of this application. Detailed Implementation
[0012] To make the objectives, technical solutions, and advantages of the embodiments of this application clearer, the technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. It should be understood that the accompanying drawings in this application are for illustrative and descriptive purposes only and are not intended to limit the scope of protection of this application. Furthermore, it should be understood that the schematic drawings are not drawn to scale. The flowcharts used in this application illustrate operations implemented according to some embodiments of this application. It should be understood that the operations in the flowcharts may not be implemented in sequence, and steps without logical contextual relationships may be reversed or implemented simultaneously. In addition, those skilled in the art, guided by the content of this application, may add one or more other operations to the flowcharts, or remove one or more operations from the flowcharts.
[0013] Furthermore, the described embodiments are merely some, not all, of the embodiments of this application. The components of the embodiments of this application described and illustrated herein can typically be arranged and designed in various different configurations. Therefore, the following detailed description of the embodiments of this application provided in the accompanying drawings is not intended to limit the scope of the claimed application, but merely to illustrate selected embodiments of the application. All other embodiments obtained by those skilled in the art based on the embodiments of this application without inventive effort are within the scope of protection of this application.
[0014] It should be noted that the term "comprising" will be used in the embodiments of this application to indicate the presence of the features declared thereafter, but does not exclude the addition of other features.
[0015] Currently, separate compilation configuration information for C and Rust languages needs to be maintained for the same application. Specifically, the Rust side's Tom's Obvious Minimal Language (TOML), Cargo feature compilation control, bindgen entry header file, include path, and C SDK static library linking rules are typically configured and maintained separately. Here, SDK stands for Software Development Kit. When the C SDK configuration file experiences platform switching, driver startup / shutdown, or address parameter changes, the Rust side cannot automatically obtain consistent updates. This leads to configuration drift issues such as inconsistent platform constants, incorrect Foreign Function Interface (FFI) binding scopes, linker object mismatches, and conditional compilation errors, ultimately causing compilation errors in the Rust application.
[0016] Based on the above problems, this application proposes an application compilation method. This method is a cross-language synchronous compilation and build method based on a single C SDK configuration source. By providing two C SDK configuration files that represent the same configuration facts, and performing Rust platform configuration information generation, compile-time constant generation, FFI declaration generation, and compilation and build sequentially based on the two C SDK configuration files, the originally scattered multiple manual configurations are converged into a Rust-side build process driven by a single configuration source. This avoids configuration drift and Rust compilation errors caused by inconsistencies between C language and Rust-side configuration information.
[0017] As an example, the technical solution of this application can be applied to the following various scenarios: 1. Rust bare-metal application development on various ARM64 processors.
[0018] 2. Embedded projects that need to reuse existing C BSPs, C drivers, and C static libraries in the Rust no_std / no_main environment.
[0019] 3. A processor SDK that simultaneously supports multiple boards, multiple System on Chip (SoC) combinations, and multiple peripheral combinations is required.
[0020] 4. Projects that need to gradually migrate from the C bare-metal SDK to the Rust bare-metal SDK, but still need to reuse C-driven assets for a long time.
[0021] The technical solution of this application can be applied to compilation systems deployed in electronic devices. Figure 1 A schematic diagram of the architecture of a compiler system provided in this application is shown below. Figure 1As shown, the compilation system includes a hardware layer, a C SDK configuration layer, and a build coordination layer. These three layers will be described in detail below.
[0022] 1. Hardware layer The hardware layer includes multiple SoC platforms or boards based on the ARM64 processor. Each platform involves system-level hardware resources such as physical memory address space, Memory Management Unit (MMU) address mapping, Universal Asynchronous Receiver / Transmitter (UART) serial controller, and Generic Interrupt Controller (GIC). The base address, interrupt number, and calling mode of these hardware resources differ between different SoC platforms and boards. These parameters are the core parameters that the platform configuration generation module needs to extract from the configuration file and map to TOML, and they are also the data source for the compile-time constants generated by the configuration parsing module.
[0023] 2. C SDK Configuration Layer Within the C SDK configuration layer, the Kconfig tool can be deployed. Kconfig is a configuration description language for the operating system kernel, and the Kconfig tool is based on the Kconfig mechanism. Users select target platforms, drivers, and features through the menu interface provided by the Kconfig system. The Kconfig system automatically generates the first and second configuration files for the C SDK based on dependencies.
[0024] 3. Construct a coordination layer The coordination layer comprises a platform configuration generation module, a configuration parsing module, an FFI binding generation module, a C SDK compilation module, a C SDK linking module, a system function module, and an application module. The platform configuration generation module reads platform parameters from the CSDK configuration file and converts them into platform configuration information used by the Rust bare-metal system. This allows Rust's system function modules to automatically obtain consistent platform configuration information from the C SDK configuration system without manually writing Rust configuration files. The configuration parsing module parses the platform configuration information and converts it into compile-time constants recognizable by Rust compilation and runtime for use by runtime components, eliminating the need for manually hard-coded platform addresses in the Rust bare-metal system. The FFI binding generation module automatically generates C header file search paths and FFI declarations based on the driver enabling status in the C SDK configuration file and the build rules defined in the build script. By using conditional C header file entry points, it prevents symbols without enabled drivers from entering the Rust FFI binding results from the source. The C SDK compilation module generates C static libraries. The C SDK linking module automatically links C static libraries and FFI declarations during the Rust build phase to generate crates. In Rust, a crate is the smallest unit of compilation and distribution, and can be referenced and depended upon by other Rust projects. The system module compiles multiple components that support system functionality. Each compiled component generates its own crate. The application module compiles the application based on the content generated by the aforementioned modules. This application can be installed on the target platform specified in the C SDK configuration file.
[0025] Figure 2 This is a flowchart illustrating the application compilation method provided in this application, which can be applied to the aforementioned compilation system. For example... Figure 2 As shown, the method may include: S201. Obtain the first configuration file and the second configuration file corresponding to the target application. The configuration facts represented by the first configuration file and the second configuration file are the same. The first configuration file is in key-value text format, and the second configuration file is in second language macro definition format.
[0026] Optionally, both the first configuration file and the second configuration file mentioned above are C SDK configuration files. The first and second configuration files represent the same configuration facts, meaning they belong to the same source configuration file. However, the first and second configuration files have different formats to suit different stages in the subsequent process. Specifically, the first configuration file is in key-value text format, while the second configuration file is in a second language macro definition format. The second language macro definition format can be a C language macro definition format. For example, the first configuration file can be represented as `sdkconfig`, and the second configuration file can be represented as `sdkconfig.h`. In one example, for the same configuration parameter, it can be represented as `CONFIG_SMP=1` in the first configuration file and as `#define CONFIG_SMP 1` in the second configuration file.
[0027] Optionally, the first and second configuration files can be pre-generated in the C SDK configuration layer described above. For example, when installing a target application on a target platform, the user can pre-configure configuration parameters adapted to the target platform and used to compile and build the target application using the Kconfig tool deployed in the C SDK configuration layer. The Kconfig tool then generates the first and second configuration files based on the user-configured parameters. As an example, the Kconfig tool could be the menuconfig tool. The menuconfig tool is a graphical configuration tool based on the Kconfig mechanism. Users can select the target SoC model, board type, memory layout, UART controller address and interrupt number, GIC interrupt controller address, and peripheral drivers to be enabled through the interactive interface of the menuconfig tool. The menuconfig tool automatically generates the first and second configuration files based on the user's configuration information.
[0028] Optionally, based on the generated first and second configuration files, compilation and building can be completed in the build coordination layer to obtain a target application that can be installed on the target platform. Optionally, the build coordination layer uses a two-level coordination mechanism of Makefile and Cargo, and the compilation and building are completed based on the cooperation of various modules within the build coordination layer. Makefile is a configuration file for an automated build tool used in C language. Makefile defines the dependencies and execution rules between compilation steps in a software project in text form. Cargo is a package manager and build system for the Rust language, responsible for downloading and compiling dependencies, managing project configuration, calling build scripts, and generating the final executable file. The make tool can be deployed in the build coordination layer. The make tool is an automated build tool. Users can execute the make command in the application directory to trigger the make tool to start. After the make tool starts, it triggers the cooperation of each module according to the execution rules defined in the Makefile. Specifically, in the Makefile's execution rules, this step is executed first, i.e., obtaining the first and second configuration files. Then, based on the first and second configuration files, each module is called to execute the subsequent steps of this embodiment. The configuration parsing module, FFI binding generation module, C SDK linking module, system function module, and application module are deployed in Cargo. Following the Makefile execution rules, after the platform configuration generation module generates the platform configuration information, it passes the file name and path of the platform configuration information, along with the environment variables defined in the Makefile, to Cargo via commands in the Makefile. Cargo then calls the configuration parsing module, FFI binding generation module, C SDK linking module, system function module, and application module to complete the compilation and build. The aforementioned environment variables may include: the application execution directory (PROJECT_DIR), the C SDK code path (SDK_DIR), and the C static library path (SDK_LIB_DIR), etc.
[0029] S202. Generate platform configuration information suitable for the first language based on the first configuration file mentioned above, and compile and generate multiple system function compilation units based on the platform configuration information mentioned above.
[0030] Optionally, the first language mentioned above can be Rust.
[0031] Optionally, the platform configuration generation module described above can generate platform configuration information suitable for the first language based on the first configuration file. This platform configuration information can be TOML information. After generating the platform configuration information, the platform configuration generation module stores it as a TOML file. Then, according to the Makefile execution rules, the filename and path of the TOML file are passed to Cargo for use by the configuration parsing module. Optionally, the platform configuration generation module parses and transforms the parameters in the first configuration file to obtain platform configuration information for use by the Rust bare-metal system, enabling Rust's system functional modules to automatically obtain consistent platform configuration information from the C SDK configuration system.
[0032] Optionally, after the filename and path of the TOML file are passed to Cargo, the TOML file is parsed by the configuration parsing module deployed in Cargo, and then compiled by the system function modules deployed in Cargo, thus obtaining the aforementioned multiple system function compilation units. These system function compilation units support system functions. Each system function compilation unit serves as a crate on the Rust side.
[0033] S203. Generate a static library file for the second language based on the second configuration file mentioned above, and generate an external function interface declaration for the first language based on the second configuration file mentioned above.
[0034] Optionally, the second language mentioned above can be C.
[0035] Optionally, the C SDK compilation module can generate a static C library file based on the second configuration file. The C SDK compilation module identifies the required source code by recognizing the configuration parameters in the second configuration file and compiles it to obtain the static library file. After obtaining the static library file, the C SDK compilation module stores it in the C static library path (SDK_LIB_DIR), so that the subsequent C SDK linking module can obtain the C static library path from the environment variables and retrieve the C static library file from that path.
[0036] Optionally, the FFI binding generation module described above can generate the external function interface declarations for the first language based on the second configuration file. The FFI binding generation module identifies the configuration parameters in the second configuration file, obtains the required C header files, and generates the corresponding Rust external function interface declarations for those C header files. Using these Rust external function interface declarations, Rust code can directly call functions written in C and access data structures defined in C.
[0037] S204. Based on the above static library files and the above external function interface declarations, generate the target compilation unit.
[0038] Optionally, the C SDK linking module described above can generate the target compilation unit based on the static library files and external function interface declarations. The C SDK linking module can automatically link the static library files and external function interface declarations, where the external function interface declarations are FFI declarations. After automatically linking the static library files and external function interface declarations, the C SDK linking module obtains the target compilation unit. This target compilation unit serves as a crate on the Rust side.
[0039] S205. Generate a target application in the first language based on the above-mentioned multiple system function compilation units, the above-mentioned target compilation unit, and the above-mentioned static library files.
[0040] Optionally, the application module described above can generate a target application in the first language based on the aforementioned multiple system function compilation units, target compilation units, and static library files. The application module combines the aforementioned multiple system function compilation units, target compilation units, and static library files to compile the target source code of the target application, and then links the compiled target file with each compilation unit and static library file to obtain the target application. Specifically, the target application can be an ELF image file, which the application module can store in the application execution directory (PROJECT_DIR). The target application is a Rust application and can be installed on the target platform so that the target platform can run Rust applications.
[0041] In this embodiment, a first configuration file and a second configuration file representing the same configuration fact are first obtained. Then, platform configuration information suitable for the first language is generated based on the first configuration file, and multiple system function compilation units are generated based on the platform information. Static library files for the second language and external function interface declarations for the first language are generated based on the second configuration file. Target compilation units are generated based on the static library files and external function interface declarations. Finally, a target application in the first language is generated based on the system function compilation units, target compilation units, and static library files. Since the process of compiling the target application in the first language is based solely on the first and second configuration files from the same source, without requiring independent configuration for each stage, the originally scattered multiple manual configurations are converged into a first language-side build process driven by a single configuration source. This avoids configuration drift and first language compilation errors caused by inconsistencies in the configuration information between the first and second language sides. Furthermore, generating system function compilation units, target compilation units, and static library files based on the same first and second configuration files significantly improves compilation efficiency.
[0042] Figure 3 A flowchart illustrating the generation platform configuration information for the application compilation method provided in this application is shown below. Figure 3 As shown, the process of generating platform configuration information in step S202 above may include: S301. Parse and map the first configuration file to obtain multiple configuration items in the first language. Each configuration item includes a field name and the field value corresponding to the field name.
[0043] Optionally, the first configuration file is in key-value text format, which is a format in the second language. For example, the keys in the key-value text are defined in the syntax of the second language. Therefore, after obtaining the first configuration file, the platform configuration generation module first parses and maps the first configuration file to obtain multiple configuration items in the first language. Each of these configuration items includes a field name and the corresponding field value. The field name conforms to the syntax of the second language, so that platform configuration information in the first language can be generated based on these field names.
[0044] S302. Generate the platform configuration information based on the above configuration items and the preset configuration template.
[0045] Optionally, the aforementioned preset configuration template records the complete structure of the platform, including architecture identifiers, memory layout, and MMU address arrays. The configuration template conforms to the structural requirements of the first language. Based on the configuration template, the configuration template is overridden according to the configuration items parsed and mapped from the first configuration file, thereby quickly obtaining platform configuration information that is adapted to the target platform and conforms to the requirements of the first language.
[0046] In this embodiment, by parsing and mapping the first configuration file, multiple configuration items conforming to the syntax of the first language can be obtained. Based on these multiple configuration items and a preset configuration template that records the complete structure of the platform, platform configuration information is generated. Since the configuration template records the complete structure of the platform, the platform configuration information in the first language can be obtained simply by overwriting the configuration template according to the configuration items, thereby greatly improving processing efficiency.
[0047] As an optional implementation, step S301 above may include: Extract each configuration line that begins with a preset identifier from the first configuration file; parse the keys and values from each configuration line in turn to obtain multiple key-value pairs; determine the target field name corresponding to each key-value pair according to the preset mapping strategy, and determine the target field value corresponding to the target field name based on the value of the key-value pair, and treat the target field and the target field value as a configuration item.
[0048] Optionally, the first configuration file can record various types of data. When generating platform configuration information, it is not necessary to use all the data; only the data starting with the aforementioned preset identifier can be used. Optionally, the data in the first configuration file is arranged in units of configuration lines. Therefore, each configuration line starting with the preset identifier can be extracted from the first configuration file. For example, the aforementioned preset identifier can be CONFIG_.
[0049] Optionally, for each extracted configuration line, the key and value are parsed from each configuration line in turn, and the key and value parsed from a configuration line are combined into a key-value pair.
[0050] Optionally, the aforementioned preset mapping strategy guides the mapping from keys in the first configuration file to fields in the first language. As an example, the preset mapping strategy is represented in entry form, where each entry represents the correspondence between a key in the first configuration file and a field in the first language. The mapping strategy also indicates the transformation type. The transformation type can include identity transformation or hexadecimal format transformation. Identity transformation means directly writing the value corresponding to the key in the first configuration file into the configuration item as is. Hexadecimal transformation means obtaining the macro-defined variable in the first configuration file as a string, finding that the field corresponding to the macro-defined variable is hexadecimal in the key-value pair, converting the string to hexadecimal, and then writing it into the configuration item. Through hexadecimal transformation, the field values in the configuration item are formatted as required by the first language, facilitating direct use as literals in the first language code.
[0051] Optionally, after obtaining the key-value pairs, the target field name corresponding to each key-value pair is first determined according to the mapping entries in the preset mapping strategy. For example, if key A and its corresponding value A are parsed from the first configuration file, and the mapping strategy contains a mapping entry stating that key A corresponds to field A1, then according to this mapping entry, the target field name corresponding to key A can be determined to be A1. Furthermore, the target field value corresponding to the target field name is determined according to the transformation type in the preset mapping strategy. For example, if the transformation type is the aforementioned identity transformation, then the value A corresponding to key A is taken as the target field value corresponding to the target field name A1.
[0052] In this embodiment, configuration lines are extracted from the first configuration file and parsed to obtain multiple key-value pairs. These pairs are then mapped and transformed according to a preset mapping strategy, thereby obtaining configuration items that are accurately applicable to the first language.
[0053] As an optional implementation, step S302 above may include: Create a temporary copy of the configuration template; iterate through multiple configuration items, and for the current configuration item, find the target item corresponding to the current configuration item in the temporary copy, and replace the field value of the target item with the field value of the current configuration item; after iterating through multiple configuration items, validate the temporary copy. If the validation passes, rename the temporary copy to the target name to obtain the platform configuration information.
[0054] Optionally, a temporary copy is created, and subsequent field value replacements are performed on this temporary copy. After the field value replacements are complete, the temporary copy is validated. Only if the validation passes is the temporary copy renamed to the target name, which is the filename of the platform configuration information. This operation ensures the reliability of writing the file corresponding to the platform configuration information. Since file renaming is an atomic operation at the operating system level, even if abnormal situations such as power outages or process crashes occur during file writing, incomplete platform configuration information will not be generated, thus ensuring the repeatability of the compilation and build process.
[0055] Optionally, the temporary copy is overwritten by traversing multiple configuration items. For the current configuration item, the corresponding target item is searched in the temporary copy, and the field values of the target item are replaced with the field values in the current configuration item. Specifically, the target item refers to an item in the temporary copy with the same field name as the current configuration item. If the target item is found, its field value is replaced with the field value in the current configuration item, i.e., the field value of the current configuration item overwrites the field value of the target item. The overwriting of the temporary copy is complete after all configuration items have been traversed. Alternatively, if no corresponding target item is found in the temporary copy for the current configuration item, the current configuration item is skipped, and the traversal continues to the next configuration item.
[0056] In this embodiment, the field values of multiple configuration items are overwritten to a temporary copy through a traversal method, thereby obtaining platform configuration information that can be used by the first language side. Simultaneously, by creating a temporary copy and renaming it after verification, the reliability of file writing can be ensured.
[0057] Figure 4 A flowchart illustrating the compilation unit of the system for generating the application compilation method provided in this application is shown below. Figure 4 As shown, the process of compiling and generating multiple system function compilation units based on the platform configuration information in step S202 above may include: S401. Generate compile-time constants for the first language based on the platform configuration information.
[0058] Optionally, the configuration parsing module described above generates compile-time constants for the first language based on the platform configuration information. The platform configuration information is stored in the form of a TOML file. After obtaining the filename and path of the TOML file, the configuration parsing module reads the TOML file from the path according to the filename, and parses the TOML file to obtain the compile-time constants for the first language.
[0059] Optionally, the parsing module is configured to parse the TOML file and output the parsed values as code in the first language, thereby obtaining the aforementioned compile-time constants. As an example, the compile-time constants in the first language may include const compile-time constants or static variables, etc.
[0060] S402. Generate multiple system function compilation units based on the above compile-time constants.
[0061] Optionally, the system includes multiple components for supporting system functions. These components can be compiled by the aforementioned system function modules based on the aforementioned compile-time constants to obtain the aforementioned multiple system function compilation units.
[0062] As an optional implementation, the components supporting system functions may include: a platform hardware adaptation component supporting hardware functions such as interrupts, serial ports, and an MMU; a logging component for logging; and a runtime scheduling component for providing basic runtime functions. Accordingly, the platform hardware adaptation component, the logging component, and the runtime scheduling component can be compiled based on the aforementioned compile-time constants to obtain multiple system function compilation units. For example, the system function module reads the compile-time constants and uses them to compile the platform hardware adaptation component, obtaining the system function compilation unit corresponding to the platform hardware adaptation component. Each of the multiple system function compilation units serves as a crate for candidate linking.
[0063] In this embodiment, a compile-time constant for the first language is generated based on the platform configuration information. Accordingly, during the compilation process of the components supporting the system functions, the compile-time constant can be used to efficiently compile multiple system function compilation units.
[0064] Figure 5 A flowchart illustrating the process of generating static library files for the application compilation method provided in this application is shown below. Figure 5 As shown, the process of generating a static library file for the second language based on the second configuration file in step S203 above may include: S501, Retrieve the configuration macros from the second configuration file.
[0065] Optionally, the aforementioned configuration macro can be a macro containing the identifier "CONFIG_" in the second configuration file. The C SDK compilation module can iterate through the lines of the second configuration file. When it encounters a line, it identifies whether the identifier "CONFIG_" exists in that line. If so, the C SDK compilation module determines that the content of that line is a configuration macro and extracts the content of the configuration macro.
[0066] S502. Based on the configuration macros in the second configuration file mentioned above, determine the driver source code to be compiled.
[0067] Optionally, the configuration macro records the identifier of the driver source code, which may be, for example, the filename of the driver source code. By reading the identifier of the driver source code in the configuration macro, the C SDK compilation module can determine the driver source code to be compiled. For example, if the C SDK compilation module reads the filename of driver source code 1 from a configuration macro, it can determine that driver source code 1 is the driver source code to be compiled.
[0068] S503: Use a cross-compiler to compile the above driver source code to obtain the first target file.
[0069] Optionally, the aforementioned cross-compiler can be an ARM64 cross-compiler. An ARM64 cross-compiler can compile the driver source code to obtain a first object file that can run on the ARM64 architecture.
[0070] Optionally, all driver source code files in the system are pre-stored in the aforementioned C SDK code path (SDK_DIR). After the C SDK compilation module determines the driver source code that needs to be compiled this time, it reads these driver source codes from the C SDK code path according to their file names and calls the cross compiler to compile these driver source codes to obtain the aforementioned first target file.
[0071] S504. Use the archiving tool to package the first target file to obtain a static library file.
[0072] Optionally, after obtaining the first target file, the C SDK compilation module calls the aforementioned archiving tool to package the first target file into a static library file. Optionally, the static library file can be a .a file. The C SDK compilation module stores the static library file in the aforementioned C static library path (SDK_LIB_DIR) so that the subsequent C SDK linking module can obtain the C static library path from the environment variables and retrieve the C static library file from the C static library path.
[0073] In this embodiment, the C SDK compilation module, based on the configuration macros in the second configuration file, can accurately determine the driver source code that needs to be compiled, and accordingly obtain these driver source codes, compile and package them to obtain the first static library file. Since the above process is based on the configuration macros in the second configuration file, the conditional compilation scope of the C SDK driver source code is controlled according to the configuration macros, thereby ensuring the integrity and accuracy of the obtained static library file.
[0074] Figure 6 A flowchart illustrating the process of generating external function interface declarations for the application compilation method provided in this application is shown below. Figure 6As shown, the process of generating the external function interface declaration of the first language based on the second configuration file in step S203 above may include: S601. Obtain configuration information, which indicates the mapping relationship between configuration macros and header file search paths.
[0075] Optionally, the configuration information can be pre-configured by the user. The configuration information defines one or more logic rules to indicate the mapping relationship between configuration macros and header file paths. For example, the configuration information defines the following logic: when the CONFIG_A macro is enabled, the header file path corresponding to A will be added to the header file search path; when the CONFIG_B macro is enabled, the header file path corresponding to B will also be added to the header file search path.
[0076] S602. Obtain the enabled configuration macros in the second configuration file mentioned above.
[0077] Optionally, the aforementioned configuration macro can be a macro containing the identifier "CONFIG_" in the second configuration file. The lines of the second configuration file can be traversed, and upon reaching a line, it can be checked whether the identifier "CONFIG_" exists. If so, the content of that line is determined to be a configuration macro. For each configuration macro, it is determined whether it carries an enable flag; if so, the configuration is considered an enabled configuration macro.
[0078] S603. Based on the above configuration information and the configuration macros enabled in the second configuration file, determine the target header file path.
[0079] Optionally, the target header file path can be determined by combining the logic defined in the above configuration information and the enabled configuration macros. Specifically, if an enabled configuration macro exists, it is determined whether the configuration macro appears in the logic defined in the above configuration information. If so, the header file path corresponding to the configuration macro is used as the target header file path. If multiple enabled configuration macros exist, each enabled configuration macro is checked separately. If a configuration macro appears in the logic defined in the above configuration information, the header file path corresponding to the configuration macro is used as the target header file path. For example, if CONFIG_A is an enabled configuration macro, the configuration information is searched for logic containing CONFIG_A. If found, the header file path corresponding to CONFIG_A is used as a target header file path. For example, if the configuration information contains the following logic: when the CONFIG_A macro is enabled, the header file path corresponding to A will be added to the header file search path, indicating that logic containing CONFIG_A has been found.
[0080] S604. Based on the target header file path mentioned above, generate the external function interface declaration for the first language.
[0081] Optionally, the target header file path indicates the header files that need to be used. Based on the target header file path, the external function interface declaration of the first language, i.e., the FFI declaration, can be generated.
[0082] In this embodiment, the target header file path can be determined based on configuration information containing one or more logics and the enabled configuration macros in the second configuration file. Then, the FFI declaration of the first language can be generated based on the target header file path. That is, the FFI declaration is controlled by the enabled configuration macros and predefined logic, so that the generation scope of the FFI declaration is consistent with the driver actually compiled on the second language side.
[0083] As an optional implementation, step S604 above may include: The target header file path is written into the header file search path, and the code generation tool is invoked. When the code generation tool runs, it obtains the target header file path from the header file search path and parses multiple target header files from the target header file path. The code generation tool parses each target header file to obtain the function types, function declarations, and macro definitions of each target header file, and generates the external function interface declaration of the first language based on the function types, function declarations, and macro definitions of each target header file.
[0084] For example, the code generation tool mentioned above could be the bindgen tool.
[0085] Optionally, during operation, the code generation tool first obtains the target header file path through a header file search path, and then reads multiple target header files from that path. The tool uses each target header file as an entry point, ensuring that only function interfaces included in those target header files generate corresponding FFI bindings. For each target header file, the tool parses it to obtain the function types, function declarations, and macro definitions. Then, it generates FFI declarations corresponding to the function types, function declarations, and macro definitions on the first language side, thereby converting the second language function interface into a callable function interface in the first language. For example, for a function declaration, the tool generates an extern "C" FFI function declaration.
[0086] In this embodiment, the code generation tool obtains the target header file based on the header file search path, and parses the function type, function declaration and macro definition from the target header file, and then generates the FFI declaration on the first language side. Through the above process, the generation scope of the code generation tool is limited. The code generation tool only processes and generates the FFI declaration of the driver interface that the target application actually needs to enable, so that the generation scope of the FFI declaration is consistent with the driver actually compiled on the second language side.
[0087] Figure 7 A flowchart illustrating the process of generating the target compilation unit for the application compilation method provided in this application is shown below. Figure 7 As shown, step S204 above may include: S701. The directory search instruction indicates the storage path of the static library file, and the link library instruction declares the names of the static libraries in the static library file in sequence, and the above directory search instruction and the above link library instruction are used as link parameters.
[0088] Optionally, the C SDK linking module obtains the storage path of the static library file from the aforementioned environment variables and generates the directory search instruction using the static library file's storage path as a parameter. Simultaneously, the C SDK linking module sequentially obtains the static library name from the static library file and generates the link library instruction using the static library name as a parameter. Then, the directory search instruction and the link library instruction are used as linking parameters.
[0089] S702. Link the above linking parameters, the above static library files, and the above external function interface declarations to obtain the above target compilation unit.
[0090] Optionally, the above linking parameters are used as linking instruction metadata. This linking instruction metadata is linked with the static library file and the FFI declaration to obtain a crate, which is the target compilation unit mentioned above.
[0091] In this embodiment, the C SDK linking module generates linking parameters through directory search instructions and linking library instructions, and automatically links static library files, FFI declarations, and linking parameters, thereby achieving fast and efficient generation of target compilation units.
[0092] Figure 8 A flowchart illustrating the process of generating the target application using the application compilation method provided in this application is shown below. Figure 8 As shown, step S205 above may include: S801. Compile the target source code of the target application to obtain a second target file. The target source code is written in the first language.
[0093] Optionally, the target source code mentioned above is source code written by the user in a first language beforehand. Before compiling the target source code, the application module confirms whether the target compilation unit and multiple system function compilation units have been successfully generated. If so, it starts compilation. During the compilation of the target source code, the application module may depend on one or more files from the target compilation unit and multiple system function compilation units. The compiled second target file may, for example, be a .o target file.
[0094] S802, link the second target file, multiple system function compilation units, target compilation units, and static library files to obtain the target application in the first language.
[0095] Optionally, the application module links the second target file and the aforementioned multiple system function compilation units, target compilation units, and static library files to obtain the target application. The target application can be an executable ELF image file.
[0096] In this embodiment, the application module sequentially compiles and links the target source code to obtain a target application that can run on the target platform and is suitable for a bare-metal system using the first language.
[0097] As an optional implementation, step S801 above may include: Conditional compilation of the target application's source code is performed based on procedural macros.
[0098] The aforementioned process macros include attribute macros and function macros.
[0099] The aforementioned attribute macros are marked before the code items in the first language in a preset macro format. The attribute macros take the configuration key name in the second configuration file as a parameter and determine whether the code item participates in the compilation based on the configuration key name.
[0100] For example, the preset macro format mentioned above could be #[macro name]. Whether a code item participates in compilation can be determined in two ways. The first way is by using the direct form. Specifically, for a configuration key, the annotated code item participates in compilation only if the configuration key exists in the second configuration file. The second way is by using the negative form. Specifically, for a configuration key, the annotated code item participates in compilation only if the configuration key does not exist in the second configuration file. During the macro expansion phase, the parsing result of the second configuration file is read, and the configuration key name after removing the CONFIG_ prefix is searched. If the condition is met (e.g., the configuration key exists and the parameter is in direct form, or the configuration key does not exist and the parameter is in negative form), the source code of the annotated code item is output as is, allowing it to enter the subsequent compilation process normally; if the condition is not met, empty code is output, causing the code item to be removed during the compilation phase.
[0101] The aforementioned functional macro is used in the form of a function call. The functional macro receives parameters in key type format, which include a key and a type. The key is the configuration key name in the second configuration file, and the type is the expected first language constant type. The functional macro retrieves the value corresponding to the key from the second configuration file based on the key in the key type format parameter, and converts the value corresponding to the key into the value under the type in the key type format parameter.
[0102] For example, the syntax of the above functional macro can be macro name!(parameter). The functional macro retrieves the value corresponding to the key in the key-type format parameter from the second configuration file and converts it to a value of the type specified in the key-type format parameter. For instance, if there is a configuration macro named CONFIG_SMP in the second configuration file, then using sdkval!(SMP:usize) in the target source code will retrieve the value of CONFIG_SMP and define it as type usize. Here, sdkval is the name of the functional macro, and usize is the type specified in the key-type format parameter.
[0103] In this embodiment, the C SDK configuration state is directly reused in the conditional compilation logic of the target source code in the first language. The decision-making basis is completely consistent with the driver pruning on the second language side. At the same time, attribute macros solve the problem of inconsistent compilation scope caused by the separate maintenance of control switches and configuration macros in the first language in traditional solutions. Functional macros directly consume configuration values such as platform address, configuration size, device name, and function switches in the second configuration file in a type-safe manner, thus eliminating the inconsistencies caused by manually copying configuration constants.
[0104] As an optional implementation, the method of this application further includes: The system monitors whether the first configuration file and / or the second configuration file have changed based on the file change monitoring command; if so, it regenerates the target application in the first language based on the changed first configuration file and / or the second configuration file.
[0105] Optionally, a file change monitoring command can be used to set either the first configuration file or the second configuration file as a dependency file that triggers a rebuild. Accordingly, when the user modifies the first configuration file and regenerates the second configuration file, the compilation system automatically detects the change and regenerates the target application in the first language based on the modified file. Specifically, the method steps in the foregoing embodiments are re-executed to obtain a new target application in the first language.
[0106] In this embodiment, by monitoring changes to the first configuration file and the second configuration file, the target application can be regenerated in a timely manner, thereby avoiding the use of outdated configuration information.
[0107] As an optional implementation, when switching target platforms is required, users only need to modify the target platform and board-related configuration items in the first and second configuration files. The compilation system will then update the configuration based on the CONFIG_FAMILY and CONFIG_TARGET_ information in the updated configuration files. The system automatically determines the new platform name. The platform configuration generation module selects the corresponding TOML template based on the platform name and automatically writes parameters such as the new platform's memory base address, UART address, and GIC address into the corresponding platform TOML fields. The configuration parsing module then generates new compile-time constants based on this. The entire platform switching process does not require manual modification of the first language's TOML file or constant definition file, avoiding the three simultaneous modifications required in traditional solutions: modifying the C SDK configuration, the Rust platform TOML, and Rust constants.
[0108] As an optional implementation, when an application needs to enable or disable specific C SDK peripheral drivers, such as enabling the GPIO driver while disabling the SPI driver, the user only needs to modify the corresponding driver enabling configuration item in the configuration file. The FFI binding generation module uses the updated CONFIG_USE_ and CONFIG_ENABLE_ The macro automatically adjusts the header file search path set, adjusts the inclusion scope of driver header files based on the same macro conditions, and regenerates FFI declarations that only include the currently enabled driver interfaces. The C SDK linking module only links the corresponding driver's static library. The entire driver combination change process does not require manual modification of the header file search list, manual editing of the FFI declaration file, or adjustment of the linked library list; these three synchronizations are automatically completed by the configuration source.
[0109] As an optional implementation, when developing the driver wrapper code in the first language, users can directly synchronize with the C SDK configuration using the `sdkcfg` attribute macro and the `sdkval` functional macro. Typical usage patterns include: annotating the GPIO driver wrapper module definition with `sdkcfg(USE_GPIO)` so that the module only participates in compilation when the GPIO driver is enabled; and using `sdkval!(UART_PADDR:usize)` in code that needs to reference UART physical addresses to obtain compile-time constants, whose values are automatically determined by the corresponding macro definitions in the second configuration file. When the configurations in the first and second configuration files are changed and the system is rebuilt, the `sdkcfg` macro automatically adjusts the compilation scope of the code items, and the `sdkval` macro automatically updates the constant values, without requiring any modification to the first language source code.
[0110] This application also provides an electronic device 90, such as... Figure 9The diagram shows the structure of an electronic device 90 provided in this embodiment of the application, including a processor 91, a memory 92, and optionally, a bus 93. The aforementioned compilation system is deployed in the electronic device. The memory 92 stores machine-readable instructions executable by the processor 91. When the electronic device 90 is running, the processor 91 communicates with the memory 92 via the bus 93, and the processor 91 executes the machine-readable instructions to perform the steps of the aforementioned application compilation method.
[0111] This application also provides a computer-readable storage medium storing a computer program, which, when run by a processor, executes the steps of the above-described application compilation method.
[0112] Those skilled in the art will clearly understand that, for the sake of convenience and brevity, the specific working processes of the systems and devices described above can be referred to the corresponding processes in the method embodiments, and will not be repeated here. In the several embodiments provided in this application, it should be understood that the disclosed systems, devices, and methods can be implemented in other ways. The device embodiments described above are merely illustrative. For example, the division of modules is only a logical functional division, and in actual implementation, there may be other division methods. Furthermore, multiple modules or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the displayed or discussed mutual coupling or direct coupling or communication connection can be through some communication interfaces; the indirect coupling or communication connection of devices or modules can be electrical, mechanical, or other forms.
[0113] Furthermore, the functional units in the various embodiments of this application can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit. If the functions are implemented as software functional units and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this invention, or the part that contributes to the prior art, or a part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of this invention. The aforementioned storage medium includes: USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, optical disks, and other media capable of storing program code.
[0114] The above are merely specific embodiments of this application, but the scope of protection of this application is not limited thereto. Any changes or substitutions that can be easily conceived by those skilled in the art within the scope of the technology disclosed in this application should be included within the scope of protection of this application.
Claims
1. An application compilation method, characterized in that, include: Obtain the first configuration file and the second configuration file corresponding to the target application. The configuration facts represented by the first configuration file and the second configuration file are the same. The first configuration file is in key-value text format and the second configuration file is in second language macro definition format. Based on the first configuration file, platform configuration information suitable for the first language is generated, and multiple system function compilation units are compiled based on the platform configuration information. The second configuration file generates a static library file for the second language, and the second configuration file generates an external function interface declaration for the first language. Based on the static library file and the external function interface declaration, generate the target compilation unit; The target application in the first language is generated based on the multiple system function compilation units, the target compilation unit, and the static library files.
2. The method according to claim 1, characterized in that, The step of generating platform configuration information suitable for the first language based on the first configuration file includes: The first configuration file is parsed and mapped to obtain multiple configuration items in the first language. Each configuration item includes a field name and the field value corresponding to the field name. The platform configuration information is generated based on the multiple configuration items and the preset configuration template.
3. The method according to claim 2, characterized in that, The parsing and mapping of the first configuration file yields multiple configuration items in the first language, including: Extract each configuration line that begins with a preset identifier from the first configuration file; The keys and values are parsed sequentially from each of the configuration lines to obtain multiple key-value pairs; According to the preset mapping strategy, the target field name corresponding to each key-value pair is determined, and the target field value corresponding to the target field name is determined according to the value of the key-value pair. The target field and the target field value are used as a configuration item.
4. The method according to claim 2, characterized in that, The step of generating the platform configuration information based on the multiple configuration items and the preset configuration template includes: Create a temporary copy of the configuration template; Iterate through the multiple configuration items, and for the current configuration item that has been traversed, find the target item corresponding to the current configuration item in the temporary copy, and replace the field value of the target item with the field value in the current configuration item; After traversing the multiple configuration items, the temporary copy is verified. If the verification passes, the temporary copy is renamed to the target name to obtain the platform configuration information.
5. The method according to claim 1, characterized in that, The process of compiling and generating multiple system function compilation units based on the platform configuration information includes: Generate compile-time constants for the first language based on the platform configuration information; The multiple system function compilation units are generated based on the aforementioned compile-time constants.
6. The method according to claim 5, characterized in that, The generation of the plurality of system function compilation units based on the compile-time constants includes: Based on the compiler constant compilation platform hardware adaptation component, logging component, and runtime scheduling component, the multiple system function compilation units are obtained.
7. The method according to claim 1, characterized in that, The step of generating a static library file for the second language based on the second configuration file includes: Retrieve the configuration macros from the second configuration file; The driver source code to be compiled is determined based on the configuration macros in the second configuration file; The driver source code is compiled using a cross-compiler to obtain the first object file; The first target file is packaged using an archiving tool to obtain the static library file.
8. The method according to claim 1, characterized in that, The step of generating the external function interface declaration for the first language based on the second configuration file includes: Obtain configuration information, which indicates the mapping relationship between configuration macros and header file search paths; Retrieve the enabled configuration macros in the second configuration file; Based on the configuration information and the enabled configuration macros in the second configuration file, determine the target header file path; Based on the target header file path, generate the external function interface declaration for the first language.
9. The method according to claim 8, characterized in that, The step of generating the external function interface declaration of the first language based on the target header file path includes: Write the target header file path into the header file search path and call the code generation tool; When the code generation tool is running, it obtains the target header file path from the header file search path and parses multiple target header files from the target header file path; The code generation tool parses each of the target header files to obtain the function types, function declarations, and macro definitions of each target header file, and generates the external function interface declaration of the first language based on the function types, function declarations, and macro definitions of each target header file.
10. The method according to claim 1, characterized in that, The step of generating a target compilation unit based on the static library file and the external function interface declaration includes: The directory search command indicates the storage path of the static library file, and the link library command declares the static library name in the static library file in sequence; Use the directory search command and the link library command as link parameters; The linking parameters, the static library file, and the external function interface declaration are linked to obtain the target compilation unit.
11. The method according to claim 1, characterized in that, The step of generating the target application in the first language based on the plurality of system function compilation units, the target compilation unit, and the static library file includes: The target source code of the target application is compiled to obtain a second target file, wherein the target source code is written in the first language; The second target file, the plurality of system function compilation units, the target compilation unit, and the static library file are linked to obtain the target application in the first language.
12. The method according to claim 11, characterized in that, The compilation of the target source code of the target application includes: Conditional compilation of the target source code of the target application is performed based on procedural macros; The process macros include: attribute macros and functional macros; The attribute macro is marked before the code item in the first language in a preset macro format. The attribute macro takes the configuration key name in the second configuration file as a parameter and determines whether the code item participates in compilation based on the configuration key name. The functional macro is used in the form of a function call. The functional macro receives parameters in key type format, which include a key and a type. The key is the configuration key name in the second configuration file, and the type is the expected first language constant type. The functional macro obtains the value corresponding to the key from the second configuration file according to the key in the key type format parameter, and converts the value corresponding to the key into the value under the type in the key type format parameter.
13. The method according to any one of claims 1-12, characterized in that, The method further includes: Monitor whether the first configuration file and / or the second configuration file have been changed based on the file change monitoring command; If so, the target application in the first language is regenerated based on the modified first configuration file and / or the second configuration file.
14. A compilation system, characterized in that, The compilation system is used to perform the steps of the application compilation method according to any one of claims 1-13.
15. An electronic device, characterized in that, The electronic device is equipped with the compilation system of claim 14; the electronic device includes a processor and a memory, the memory storing machine-readable instructions executable by the processor, wherein when the electronic device is running, the processor executes the machine-readable instructions to perform the steps of the application compilation method of any one of claims 1 to 13.
