Code generation and compilation configuration method based on configuration description file
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-06-12
- Publication Date
- 2026-08-11
AI Technical Summary
[0003]1.可读性差:宏定义形式不直观,不利于工程人员理解整体配置含义;
[0017] 1) This invention combines structured configuration description files with the compile-time configuration process, realizing automated processing of configuration building, parsing, mapping, generation, and compilation integration. This decouples system configuration from source code, significantly improving the standardization, flexibility, and maintainability of configuration management while ensuring the determinism of compilation results. Moreover, the responsibilities of configuration personnel and code developers are clear and do not interfere with each other, which can effectively improve the efficiency of engineering collaboration. Thus, it has achieved significant technical effects and application benefits in terms of system building efficiency, configuration consistency, and engineering implementation costs.
Smart Images

Figure CN122547401A_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of software technology, and more specifically, relates to a method for code generation and compilation configuration based on configuration description files. Background Technology
[0002] In complex software systems, especially embedded systems, security systems, or multi-module systems, a large number of configuration parameters are typically required during compilation to control the start and stop of functional modules, resource limits, communication methods, and runtime environment characteristics. Traditionally, these configurations are usually written directly into header files or source code as macro definitions, which presents the following problems:
[0003] 1. Poor readability: The macro definition format is not intuitive, making it difficult for engineers to understand the overall configuration meaning;
[0004] 2. High maintenance costs: Configuration changes require direct modification of code files, which can easily introduce human error;
[0005] 3. Tight coupling between configuration and code: The same codebase is difficult to adapt to different deployment scenarios;
[0006] 4. Lack of a unified configuration source: runtime configuration and compile-time configuration are managed separately, which can easily lead to inconsistencies.
[0007] As system size increases and the number of configuration items grows, there is an urgent need for a technology to effectively decouple high-level, semantic, and structured configuration descriptions from configuration formats recognizable by the underlying compiler. Summary of the Invention
[0008] The purpose of this invention is to overcome the shortcomings of the prior art and provide a code generation and compilation configuration method based on configuration description files. By introducing a unified structured configuration description file as the configuration source, a configuration file adapted to the compiler is automatically generated before compilation, thereby achieving a clear separation between configuration and source code and improving the maintainability, portability and configurability of the software system.
[0009] To achieve the above-mentioned objectives, the present invention provides a code generation and compilation configuration method based on a configuration description file, comprising the following steps:
[0010] S1: Construct a unified structured configuration description file as the sole input source for system configuration. This configuration description file is organized according to a preset configuration hierarchy and uses a text format that can be directly read by humans. The configuration content includes: basic system information, including version number and startup parameters; runtime environment, including runtime path, library path, and configuration path; resource limits, including CPU, memory, and storage limits; and configuration of each functional module, including the start / stop status, runtime parameters, and detection function switches of each functional module.
[0011] S2: Read and parse the configuration file, perform semantic recognition on the configuration content according to the preset configuration hierarchy rules and semantic rules, and then divide the configuration content into global configuration items, module-level configuration items and function control configuration items;
[0012] S3: Based on the parsing results of step S2, establish the mapping relationship between the configuration content and the compile-time configuration, and determine the corresponding conversion rules according to the type and control semantics of the configuration content, converting the configuration content into a configuration definition that the compiler can recognize;
[0013] S4: Set the format of the configuration file to the format directly referenced in the compiler. Generate a configuration file for compilation based on the configuration definition obtained in step S3. This configuration file is used to control the start and stop of system functions, module combination and parameter values during the compilation process.
[0014] S5: Input the compile-time configuration file generated in step S4 and the source code into the compiler to compile and generate an executable file.
[0015] This invention relates to a code generation and compilation configuration method based on configuration description files. It constructs a unified structured configuration description file as the sole input source for system configuration. This configuration description file is organized according to a preset configuration hierarchy and uses a text format that can be directly read by humans. The configuration file is read and parsed, and the configuration content is divided into global configuration items, module-level configuration items, and function control configuration items. Then, according to the type and control semantics of the configuration content, the configuration content is converted into a configuration definition that can be recognized by the compiler. Then, a configuration file for compilation is generated. Finally, the compilation configuration file and the source code are input into the compiler to compile and generate an executable file.
[0016] The present invention has the following beneficial effects:
[0017] 1) This invention combines structured configuration description files with the compile-time configuration process, realizing automated processing of configuration building, parsing, mapping, generation, and compilation integration. This decouples system configuration from source code, significantly improving the standardization, flexibility, and maintainability of configuration management while ensuring the determinism of compilation results. Moreover, the responsibilities of configuration personnel and code developers are clear and do not interfere with each other, which can effectively improve the efficiency of engineering collaboration. Thus, it has achieved significant technical effects and application benefits in terms of system building efficiency, configuration consistency, and engineering implementation costs.
[0018] 2) This invention can be widely applied to embedded systems and vehicle system software, network security, intrusion detection and protection systems, industrial control and automation systems, system software that requires compile-time macro control of functional modules, and software product construction scenarios with multiple versions and deployment forms, and has significant engineering application value. Attached Figure Description
[0019] Figure 1 This is a flowchart illustrating a specific implementation method of the code generation and compilation configuration method based on a configuration description file according to the present invention. Detailed Implementation
[0020] The specific embodiments of the present invention will now be described with reference to the accompanying drawings to enable those skilled in the art to better understand the invention. It should be particularly noted that in the following description, detailed descriptions of known functions and designs that might obscure the main content of the invention will be omitted here.
[0021] Example
[0022] Figure 1 This is a flowchart illustrating a specific implementation of the code generation and compilation configuration method based on a configuration description file according to the present invention. Figure 1 As shown, the specific steps of the code generation and compilation configuration method based on the configuration description file of the present invention include:
[0023] S101: Building a unified structured configuration description file:
[0024] A unified, structured configuration description file serves as the sole input source for system configuration. This file is organized according to a predefined configuration hierarchy. To facilitate development and maintenance, the configuration description file is in a human-readable text format. The configuration content within the file includes: basic system information, such as version number and startup parameters; the runtime environment, including runtime path, library path, and configuration path; resource limitations, including CPU, memory, and storage limits; and configurations for each functional module, including their start / stop status, runtime parameters, and detection function switches.
[0025] In this embodiment, the configuration description file adopts JSON format. A unified format enables centralized management, clear expression, and efficient maintenance of system configurations.
[0026] S102: Configuration Reading and Semantic Parsing
[0027] The configuration file is read and parsed. Based on preset configuration hierarchy and semantic rules, the configuration content is semantically identified and then divided into global configuration items, module-level configuration items, and function control configuration items. This classification provides a semantic foundation for subsequent code generation or system integration. The criteria for each type can be set according to actual needs. In this embodiment, the classification method for configuration content is as follows:
[0028] For each configuration item, determine whether it belongs to a specific functional module. If it does not, it is a global configuration item. If it does, further determine whether it is used to control the specific function of that functional module. If not, it is a module configuration item; otherwise, it is a function control configuration item.
[0029] Based on the above classification method, global configuration items apply to the entire system, such as version number and startup parameters. Module configuration items are secondary groups of the entire configuration file and are used to correspond to independent functional modules in the system. Function control configuration items are located inside the functional module and include function start / stop, parameter thresholds, and detection function on / off.
[0030] S103: Configuration mapping and conversion functions confirmed:
[0031] Based on the parsing results of step S102, a mapping relationship between configuration content and compile-time configuration is established, and according to the type and control semantics of configuration content, the corresponding conversion rules are determined to convert the configuration content into a configuration definition that the compiler can recognize.
[0032] In this embodiment, the compiler-recognizable configuration definitions adopt macro definitions. Following the naming convention of "module key_function key", the structured configuration is converted into macro definition form for code generation or low-level system integration. The conversion rules in this embodiment include naming conventions, value type mapping rules, and semantic level processing rules, wherein:
[0033] The naming convention is as follows: use an underscore '_' to connect module keys and function keys, keep the original key names, and use uppercase letters.
[0034] The value type mapping rules are as follows: for string types, the original string value is retained; for boolean types, if the value is true, it is converted to 1, and if the value is false, it is converted to 0; for numeric types, the numeric value is directly retained.
[0035] The semantic hierarchy processing rule is as follows: if the configuration content is a global configuration item, it is generated according to the rule "#define function key function value", otherwise it is generated according to the rule "#define module key_function key function value".
[0036] For global configuration items, taking the version number as an example, the converted macro definition is #define VERSION "0.2.0". For module / function configuration items, the converted macro definition for enabling / disabling a function is #define FUNCTION_ENABLE1, and the converted macro definition for a parameter threshold is #define FUNCTION_CPUMAX 10.
[0037] By parsing configuration files and converting them according to a conversion mechanism, automated mapping from structured configuration files to code-level macro definitions can be achieved, ensuring accurate semantic transmission of configuration content and efficient system integration.
[0038] S104: Generate compile-time configuration files:
[0039] Set the format of the configuration file to the format directly referenced in the compiler, and generate a configuration file for compilation based on the configuration definition obtained in step S103. This configuration file is used to control the start and stop of system functions, module combination, and parameter values during the compilation process.
[0040] To ensure compatibility with mainstream compilers (GCC, Clang, ARMCC, etc.), the output format of the configuration file in this embodiment adopts a C / C++ header file (.h). The configuration file is generally named config_{device}.h. In practical applications, it can be automatically generated using script automation tools (such as Python, Shell).
[0041] Based on the mapping relationships and conversion rules, a configuration file for compile-time is automatically generated. This configuration file exists in a form that the compiler can directly reference (such as a C / C++ header file) and is used to control system function startup and shutdown, module combination, and parameter values during the compilation process. An example of the generated configuration file is shown below:
[0042] #ifndef CONFIG_{DEVICE}_H_
[0043] #define CONFIG_{DEVICE}_H_
[0044] / / Auto-generated from config.json
[0045] / / DO NOT EDIT THIS FILE MANUALLY
[0046] / *========================================================
[0047] * Global configuration macro definitions
[0048] *=====================================================* /
[0049] / *=======================================================
[0050] * Module / Function Configuration Macro Definitions
[0051] *=====================================================* /
[0052] #endif / / CONFIG_{DEVICE}_H_
[0053] As can be seen, the compilation configuration file of this invention can be automatically generated, avoiding syntax or logic errors caused by manual editing of macro definitions.
[0054] S105: Compile to generate an executable file:
[0055] Input the compile-time configuration file generated in step S104 and the source code into the compiler to compile and generate an executable file.
[0056] As can be seen from the above, this invention can automatically convert user-friendly configuration descriptions into configuration files that can be directly used during compilation, thereby completing the compilation-time configuration of system functions, parameters, and module combinations without modifying the source code. In practical applications, the configuration content in the configuration description file can be used to quickly switch between various compilation modes without modifying the source code. Table 1 is an example table of compilation mode settings in this embodiment.
[0057] Full-featured version Enable all modules / features: true Includes all functions of all modules Lightweight version Basic (necessary) modules / functions enable: true Includes only core module detection functions, with low resource consumption. Debug version debug: true Enable detailed logging to facilitate problem localization. Release version debug: false, log encryption enc: true Disable debug output and enable security features. Feature-reduced version Disable specific detection items Customize detection capabilities according to scenario requirements.
[0058] Table 1
[0059] As shown in Table 1, by modifying the configuration description file, compilation modes can be quickly switched, and different versions of executable files can be recompiled. Therefore, the source code does not need to contain any hard-coded configurations, improving the maintainability of the source code. Configuration content is centralized in the structured configuration description file, reducing direct modifications to the source code. Sensitive configuration content can be centrally managed in the configuration description file, reducing the risk of source code leakage. Furthermore, all compilation modes can use the same set of source code, reducing development and maintenance costs. The configuration description file can be incorporated into version management, ensuring that the build results correspond to the configuration, and supporting parallel builds of multiple modes with automated coverage.
[0060] In summary, this invention, through a configuration description file-driven mechanism, enables the system to achieve fully configuration-driven compilation and building, simplifying operations such as product form management, feature trimming, and parameter tuning to mere changes to the configuration description file, significantly improving development efficiency, release flexibility, and product quality consistency.
[0061] Although the illustrative specific embodiments of the present invention have been described above to enable those skilled in the art to understand the invention, it should be understood that the invention is not limited to the scope of the specific embodiments. For those skilled in the art, various changes are obvious as long as they are within the spirit and scope of the invention as defined and determined by the appended claims, and all inventions utilizing the concept of the present invention are protected.
Claims
1. A code generation and compilation configuration method based on a configuration description file, characterized in that, Includes the following steps: S1: Construct a unified structured configuration description file as the sole input source for system configuration. This configuration description file is organized according to a preset configuration hierarchy and uses a text format that can be directly read by humans. The configuration content includes: basic system information, including version number and startup parameters; runtime environment, including runtime path, library path, and configuration path; resource limits, including CPU, memory, and storage limits; and configuration of each functional module, including the start / stop status, runtime parameters, and detection function switches of each functional module. S2: Read and parse the configuration file, perform semantic recognition on the configuration content according to the preset configuration hierarchy rules and semantic rules, and then divide the configuration content into global configuration items, module-level configuration items and function control configuration items; S3: Based on the parsing results of step S2, establish the mapping relationship between the configuration content and the compile-time configuration, and determine the corresponding conversion rules according to the type and control semantics of the configuration content, converting the configuration content into a configuration definition that the compiler can recognize; S4: Set the format of the configuration file to the format directly referenced in the compiler. Generate a configuration file for compilation based on the configuration definition obtained in step S3. This configuration file is used to control the start and stop of system functions, module combination and parameter values during the compilation process. S5: Input the compile-time configuration file generated in step S4 and the source code into the compiler to compile and generate an executable file.
2. The method of claim 1, wherein the configuration description file is a configuration description file for a code generation and compilation configuration. In step S1, the configuration description file is in JSON format.
3. The method of claim 1, wherein the configuration description file is a configuration description file for a code generation and compilation configuration. The specific method for classifying configuration content in step S2 is as follows: For each configuration content, determine whether it belongs to a specific functional module. If it does not belong, it is a global configuration item. If it does belong, further determine whether it is used to control the specific function of the functional module. If not, it is a module configuration item; otherwise, it is a function control configuration item.
4. The method of claim 1, wherein the configuration description file is a configuration file for a code generation and compilation configuration. The conversion rules in step S3 include naming conventions, value type mapping rules, and semantic hierarchy processing rules, wherein: The naming convention is as follows: use an underscore '_' to connect module keys and function keys, keep the original key names, and use uppercase letters. The value type mapping rules are as follows: for string types, the original string value is retained; for boolean types, if the value is true, it is converted to 1, and if the value is false, it is converted to 0; for numeric types, the numeric value is directly retained. The semantic hierarchy processing rule is as follows: if the configuration content is a global configuration item, it is generated according to the rule "#define function key function value", otherwise it is generated according to the rule "#define module key_function key function value".
5. The method of claim 1, wherein the configuration description file is a configuration description file for a code generation and compilation configuration. In step S4, the output format of the configuration file adopts a C / C++ header file.