Source file construction method and apparatus, device, and readable storage medium
Patent Information
- Application Number
- PCT/CN2023/141013
- Authority / Receiving Office
- WO · WO
- Patent Type
- Applications
- Current Assignee / Owner
- Filing Date
- 2023-12-22
- Publication Date
- 2025-08-21
AI Technical Summary
In the prior art, source code construction requires manual writing of complex text files, such as Makefile or CMakeLists.txt, which makes it difficult for users to operate, have high technical thresholds, and is difficult to adapt to different operating system platforms.
Provide a source file construction method. By displaying a visual configuration interface, the user inputs the setting configuration information, generates the construction configuration file, obtains parameters based on the configuration file and constructs the source file, and supports different operating system platforms.
It simplifies user operation processes, reduces learning costs, reduces technical requirements and configuration errors, realizes the automated construction of source files, is suitable for different operating system platforms, and improves construction efficiency.
Smart Images

Figure CN2023141013_21082025_PF_FP_ABST
Abstract
Description
Source file construction method, device, equipment and readable storage medium Technical Field
[0001] The present application relates to the field of computer science and technology, and in particular to a source file construction method, apparatus, device, and readable storage medium. Background Art
[0002] Source file construction refers to converting high-level programming language source code into executable programs, library files, or other forms of output files. Construction includes compilation, assembly, and linking to generate the final executable file or library. For example, a source code file with the suffix .c in C language is compiled into an executable file with the suffix .exe that can be run directly on a computer.
[0003] In the related art, building source code usually requires first writing a text file used to describe the construction process of the code project, such as CMakeLists.txt or Makefile file. The text file defines the dependencies between source files and how to build source files into executable files or libraries, so that the source code is built according to the rules and commands defined in the text file to obtain an executable file or library.
[0004] However, the text files in the above source code construction method use specific syntax and rules, requiring users to understand the syntax and rules to write according to the actual situation of the project. There is a technical threshold and it is difficult for users to operate.
[0005] Summary of the Invention
[0006] In view of this, in order to solve the above technical problems, the present application provides a source file construction method, device, equipment and readable storage medium.
[0007] Specifically, this application is implemented through the following technical solutions:
[0008] According to a first aspect of an embodiment of the present application, a method for constructing a source file is provided, the method comprising:
[0009] In response to receiving the first instruction, displaying a visual configuration interface, wherein the interface is used to instruct a user to input setting configuration information;
[0010] Generate a build configuration file for the code project based on the received setting configuration information;
[0011] Acquire multiple parameters according to the construction configuration file, the parameters including a first parameter for determining a source file to be processed in the code project, and a second parameter for source file construction;
[0012] In response to receiving the second instruction, the source file to be processed is constructed according to the second parameter to obtain an output file of a specified type.
[0013] Optionally, the first parameter includes a path list of source files in the code project, files that do not need to be compiled, and directory dependencies of header files included in the source files. The method further includes:
[0014] Determine all source files in the path list of the source files as first source files;
[0015] Determine the path list of the header files according to the path list of the source files and the directory dependency relationship of the header files included in the source files;
[0016] Determine all files in the path list of the header file as second source files;
[0017] The source files other than the files that do not need to be compiled in the first source file and the second source file are determined as the source files to be processed.
[0018] Optionally, determining all source files in the path list of the source files as first source files includes:
[0019] Performing hash calculation on all source files in the path list of the source files to obtain a unique first identifier for each source file;
[0020] Matching the first identifier of the source file with the second identifier of the locally stored source file; the second identifier is an identifier corresponding to a source file that has been built in the code project;
[0021] In response to the presence of a third source file having a second identifier identical to the first identifier, source files other than the third source file in the path list of source files are determined to be the first source files.
[0022] Optionally, the construction configuration file is a JSON format file; and obtaining multiple parameters according to the construction configuration file includes:
[0023] Deserialize the JSON file to obtain a deserialization result.
[0024] According to the data structure type of the deserialization result, the parameters included in the construction configuration file are obtained from the deserialization result.
[0025] Optionally, the second parameter includes a compiler type and an output file type; and constructing the source file to be processed according to the second parameter to obtain an output file of a specified type includes:
[0026] According to the compiler type, calling the corresponding compilation tool chain;
[0027] A build command is started to enable the compilation tool chain to convert the source file to be processed into an output file that conforms to the output file type.
[0028] Optionally, the parameters further include a third parameter for indicating a path list of source files to be analyzed; in response to the source file to be analyzed being the source file to be processed, the method further includes:
[0029] After converting the source file to be analyzed into the output file, obtaining the log files and intermediate files generated during the construction process;
[0030] Extracting code analysis results based on the log file and the intermediate file;
[0031] An analysis report of the source file to be analyzed is generated based on the code analysis result.
[0032] Optionally, the method further includes:
[0033] Extracting metadata information of the output file, wherein the metadata information at least includes symbol table information and shared library link status;
[0034] A code analysis report of the output file is generated according to the metadata information.
[0035] According to a second aspect of an embodiment of the present application, a source file construction device is provided, the device comprising:
[0036] a configuration information acquisition module, configured to display a visual configuration interface in response to receiving the first instruction, wherein the interface is used to instruct a user to input setting configuration information;
[0037] A configuration file generation module, configured to generate a build configuration file for a code project based on the received configuration information;
[0038] A parameter acquisition module, configured to acquire a plurality of parameters according to the construction configuration file, wherein the parameters include a first parameter for determining a source file to be processed in the code project and a second parameter for source file construction;
[0039] A construction module is used to construct the source file to be processed according to the second parameter in response to receiving the second instruction to obtain an output file of a specified type.
[0040] In some embodiments, the first parameter includes a path list of source files in the code project, files that do not need to be compiled, and directory dependencies of header files included in the source files, and the apparatus further includes:
[0041] A first source file determining module, configured to determine all source files in the source file path list as first source files;
[0042] A header file path determination module, configured to determine a path list of the header files according to the path list of the source files and the directory dependency relationship of the header files included in the source files;
[0043] A second source file determining module, configured to determine all files in the path list of the header file as second source files;
[0044] The to-be-processed source file determining module is configured to determine the source files other than the files that do not need to be compiled in the first source file and the second source file as the to-be-processed source files.
[0045] In some embodiments, the first source file determination module is specifically configured to:
[0046] Performing hash calculation on all source files in the path list of the source files to obtain a unique first identifier for each source file;
[0047] Matching the first identifier of the source file with the second identifier of the locally stored source file; the second identifier is an identifier corresponding to a source file that has been built in the code project;
[0048] In response to the presence of a third source file having a second identifier identical to the first identifier, source files other than the third source file in the path list of source files are determined to be the first source files.
[0049] In some embodiments, the construction configuration file is a JSON format file; the parameter acquisition module is specifically used to:
[0050] Deserialize the JSON file to obtain a deserialization result.
[0051] According to the data structure type of the deserialization result, the parameters included in the construction configuration file are obtained from the deserialization result.
[0052] In some embodiments, the second parameter includes a compiler type and an output file type; and the building module is specifically configured to:
[0053] According to the compiler type, calling the corresponding compilation tool chain;
[0054] A build command is started to enable the compilation tool chain to convert the source file to be processed into an output file that conforms to the output file type.
[0055] In some embodiments, the parameters further include a third parameter for indicating a path list of source files to be analyzed; in response to the source file to be analyzed being the source file to be processed, the apparatus further includes:
[0056] After converting the source file to be analyzed into the output file, obtaining the log files and intermediate files generated during the construction process;
[0057] Extracting code analysis results based on the log file and the intermediate file;
[0058] An analysis report of the source file to be analyzed is generated based on the code analysis result.
[0059] In some embodiments, the apparatus further comprises:
[0060] Extracting metadata information of the output file, wherein the metadata information at least includes symbol table information and shared library link status;
[0061] A code analysis report of the output file is generated according to the metadata information.
[0062] According to a third aspect of an embodiment of the present application, an electronic device is provided, comprising: a memory and a processor; the memory is used to store a computer program; and the processor is used to execute the above-mentioned source file construction method by calling the computer program.
[0063] According to a fourth aspect of an embodiment of the present application, a computer-readable storage medium is provided, on which a computer program is stored, and when the program is executed by a processor, the above-mentioned source file construction method is implemented.
[0064] The technical solutions provided by the embodiments of the present application may have the following beneficial effects:
[0065] In the technical solution provided in the above-mentioned application, a source file construction method is provided, which is specifically used to generate a static library, shared library or executable program suitable for a specific computer platform from a source code file. The method is suitable for different operating system platforms, is easy to port and adapt, and has strong scalability. By setting a visual configuration interface and generating a construction configuration file based on the input, the user can intuitively enter the required parameters and options without manually writing complex configuration files, and can flexibly adjust the construction parameters according to specific needs, which simplifies the user's operation process, reduces the user's learning cost, reduces the user's technical requirements and potential configuration errors, and can automatically obtain and process the source files to be processed according to the parameters in the configuration file, and construct them in the manner specified by the user, realizing the automated construction requirements of the source files, reducing human intervention, and improving construction efficiency.
[0066] It should be understood that the above general description and the detailed description below are merely exemplary and explanatory and cannot limit the present application. In addition, any embodiment in the present application does not necessarily achieve all the effects described above. BRIEF DESCRIPTION OF THE DRAWINGS
[0067] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the following briefly introduces the drawings required for use in the description of the embodiments. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without any creative work.
[0068] FIG1 is a flow chart of a source file construction method according to an exemplary embodiment of the present application;
[0069] FIG2 is a flowchart illustrating a method of determining a source file to be processed according to a first parameter according to an exemplary embodiment of the present application;
[0070] FIG3 is a flowchart of another method for determining a source file to be processed, shown in an exemplary embodiment of the present application;
[0071] FIG4 is a flowchart of obtaining parameters from a JSON configuration file according to an exemplary embodiment of the present application;
[0072] FIG5 is a flowchart of a construction process for a source file to be processed, shown in an exemplary embodiment of the present application;
[0073] FIG6 is a flowchart illustrating a method of completing initialization configuration according to a visual configuration interface according to an exemplary embodiment of the present application;
[0074] FIG6A is a schematic diagram of a visual configuration interface for instructing a user to input the name of a source file to be processed, according to an exemplary embodiment of the present application;
[0075] FIG6B is a schematic diagram of a visual configuration interface for instructing a user to input a generated file type, according to an exemplary embodiment of the present application;
[0076] FIG6C is a schematic diagram of a visual configuration interface for instructing a user to select a source file to construct a compilation tool chain required by an exemplary embodiment of the present application;
[0077] FIG6D is a schematic diagram of a visual configuration interface for indicating stored configuration file information by a user inputting configuration information, according to an exemplary embodiment of the present application;
[0078] FIG7 is a flowchart illustrating a method of constructing a source file to be processed according to parameters in a configuration file according to an exemplary embodiment of the present application;
[0079] FIG8 is a schematic diagram showing a comparison of changes in file targets and stored files before and after a construction process, according to an exemplary embodiment of the present application;
[0080] FIG9 is a schematic structural diagram of a source file construction method and apparatus according to an exemplary embodiment of the present application;
[0081] FIG10 is a hardware diagram of an electronic device shown in an exemplary embodiment of the present application. DETAILED DESCRIPTION
[0082] The following will be combined with the drawings in the embodiments of this application to clearly and completely describe the technical solutions in the embodiments of this application. Obviously, the embodiments described are only part of the embodiments of this application, not all of the embodiments. Based on the embodiments in this application, all other embodiments obtained by ordinary technicians in this field without making creative efforts are within the scope of protection of this application.
[0083] The terms used in this application are for the purpose of describing specific embodiments only and are not intended to limit this application. As used in this application and the appended claims, the singular forms "a," "an," "the," and "the" are intended to include the plural forms, unless the context clearly indicates otherwise. It should also be understood that the term "and / or" as used herein refers to and encompasses any and all possible combinations of one or more of the associated listed items.
[0084] It should be understood that although the terms first, second, third, etc. may be used in this application to describe various information, such information should not be limited to these terms. These terms are only used to distinguish information of the same type from each other. For example, without departing from the scope of this application, the first classification threshold may also be referred to as the second classification threshold, and similarly, the second classification threshold may also be referred to as the first classification threshold. Depending on the context, the word "if" as used herein may be interpreted as "at the time of" or "when" or "in response to determining".
[0085] In software development, a source file typically refers to a file containing program source code, such as source code files in programming languages like C, C++, and Java. Traditionally, building source files into executable files or libraries requires first writing a text file like CMakeLists.txt or Makefile to describe the compilation and build process. This text file uses specific syntax and rules to define information such as source code files, compilation options, and link libraries. Through specific build operations, the code is compiled into a static library, shared library, or executable program for a specific computer platform.
[0086] For example, a Makefile is a script file used for automated builds. It uses Makefile syntax to define a series of rules and commands describing how to compile and link source code files. The Makefile uses the make tool to execute these rules and commands. After the source code is written, a Makefile is written using Makefile syntax based on the actual build requirements of the source code. The malefile tool is then called to parse the Makefile, build the source code according to the defined rules and commands, and generate a build system file suitable for the specific platform.
[0087] In the above source file construction process, users need to understand the basic syntax and writing rules of Makefile files, which requires high technical skills of users. In addition, if there are many source files in the code project to be built, it is difficult for users to manually write Makefile files.
[0088] To solve the above technical problems, the present application provides a source file construction method that can be applied to different development environments and programming languages, and can provide cross-platform support to run on different operating environments; the source file construction method can be executed by electronic devices that support displaying a visual interface, such as tablets, computers, etc. Referring to Figure 1, the source file construction method may include the following steps:
[0089] S101, in response to receiving a first instruction, displaying a visual configuration interface, wherein the interface is used to instruct a user to input setting configuration information;
[0090] The first instruction is used to instruct the electronic device to display the visual configuration interface. The first instruction is triggered by the electronic device itself when a specified event is detected or is sent to the electronic device by other devices.
[0091] The visual configuration interface refers to a graphical interface that provides a series of options, input boxes, and check boxes for guiding the user to input information required during the construction process.
[0092] The configuration information refers to the various parameters, options, and settings displayed on the preset visual configuration interface, which are used to describe the project's build requirements and constraints. The configuration information may include, but is not limited to, the path of the source file to be processed, header file dependencies, the build tool chain, and output file related information such as name, storage path, file type, etc.; among them, the file type of the output file can be static library static, shared library shared, and executable program exe.
[0093] Users can use the visual interface to select the required compiler, linker, compilation options, target platform, etc., and specify the location and name of the output file. They can also configure project dependencies, introduce external libraries, etc. to meet the needs of source file construction.
[0094] S102, generating a build configuration file for the code project according to the received setting configuration information;
[0095] That is, after the user completes inputting information on the visual interface, the various parameters and settings entered on the interface are converted into data in a pre-set configuration file format and written to the build configuration file. In this embodiment, the build configuration file can be a JSON format file, which organizes the configuration information entered on the visual configuration interface into a corresponding JSON object, and writes the JSON object into the JSON file.
[0096] Before executing the source file construction method, a customized script or program can be written and pre-installed in the electronic device according to the specific code project requirements and various compiler types, so as to run the script or program to generate a corresponding construction configuration file according to the received configuration information; after the construction configuration file is generated, the user can preview the construction configuration file and update the information in the construction configuration file, thereby improving efficiency and the accuracy of configuration information.
[0097] The build configuration file records the user's settings and parameters and can be saved and reused. When the project needs to be rebuilt, the reusable information in the build configuration file, such as checkboxes, can be displayed by default in the visual configuration interface for user confirmation to improve the repeatability of the build.
[0098] S103, acquiring a plurality of parameters according to the construction configuration file, wherein the parameters may include at least a first parameter for determining a source file to be processed in the code project and a second parameter for source file construction;
[0099] The first parameter is used to indicate the path of the source file in the code project that needs to be converted from source code into an executable file or library. The execution entity can find the source file to be processed from the code project based on the first parameter; the second parameter refers to various parameters required in the process of compiling, assembling, linking and generating output files from the source file, such as compilation options, various link parameters passed to the linker, the name and type of the output file, etc., which are used to be passed to each node in the construction process to obtain the correct output file corresponding to the source file.
[0100] According to the file format of the construction configuration file, the method for obtaining each parameter stored in the file can be determined. For example, if the construction configuration file is an XML configuration file, the XML file can be parsed into a JavaScript object, and the required parameter values can be obtained through the property path of the object; for example, if the construction configuration file is a JSON configuration file, the parameters can be obtained through deserialization. Deserialization refers to the process of restoring data from a serialized format to its original data structure or object. For the JSON configuration file, the deserialization process will map the key-value pairs in the JSON string to the corresponding properties or fields.
[0101] S104 : In response to receiving the second instruction, construct the source file to be processed according to the second parameter to obtain an output file of a specified type.
[0102] The second instruction can trigger the electronic device to issue the instruction when the source file to be processed is determined, so that when the instruction is detected or received, the construction processing of the source file to be processed is started, wherein the second instruction can carry information indicating the source file to be processed.
[0103] The output files of the specified type may include three types of files: static libraries, shared libraries, and executable files. Among them, a static library is an archive file containing reusable code. The code of the static library will be completely copied to the final executable file corresponding to the source file that references the static library, so that the executable file can run independently and does not rely on external library files; a shared library is also called a dynamic link library, which is a binary file containing reusable code. Unlike static libraries, the code of a shared library is dynamically loaded and linked into an executable file at runtime, so that multiple executable files can share the same library file, thereby saving memory space and providing better maintainability; an executable file is a program file that can be directly run after compilation and linking. It is generated by processing the source code through a compiler and a linker and can be directly executed in the operating system.
[0104] The construction of the source file to be processed includes processing stages such as preprocessing, compilation, assembly, and linking, and the input parameters required for each processing stage are obtained according to the second parameter. Preprocessing means that the preprocessor processes the preprocessing instructions in the source code, such as #include and #define, and generates processed intermediate code; compilation means using a specified compiler to translate the preprocessed intermediate code into assembly language code; assembly is the process of converting the assembly language code into a target file, which contains instructions and data in binary form; linking means linking the target file with the required library files to generate the final executable file or library file.
[0105] In the embodiments of the present disclosure, a universal construction method is provided, which can be applied to different development environments and programming languages without being limited to a specific integrated development environment, and is more flexible and independent; it supports cross-platform project construction, so that the same configuration files and construction methods can be used, ensuring the execution of the construction process on different platforms; by setting a visual configuration interface and generating a construction configuration file based on the input, the user can intuitively enter the required parameters and options without manually writing complex configuration files, and can flexibly adjust the construction parameters according to specific needs, which simplifies the user's operation process, reduces the user's learning cost, reduces the user's technical requirements and potential configuration errors, and can automatically obtain and process the source files to be processed according to the parameters in the configuration file, and construct them in the manner specified by the user, thereby realizing the automated construction of source files, reducing human intervention, and improving construction efficiency.
[0106] In some embodiments, the first parameter in step S103 may include a path list of source files in the code project, files that do not need to be compiled, and directory dependencies of header files included in the source files. Based on the first parameter, as shown in FIG2 , the source file construction method may further include a step of determining source files to be processed, which may include:
[0107] S201, determining all source files in the path list of the source files as first source files;
[0108] The path list of source files refers to a set of paths containing source files, which can be relative paths or absolute paths in the file system. The storage location of the source files in the code project is found based on the path list of source files, and each source file in the storage location is determined as the first source file. For example, if the path list of source files is ". / src / *.c", all source files ending with .c in the src directory are determined as the first source files.
[0109] S202, determining a path list of the header files according to the path list of the source files and the directory dependency relationship of the header files included in the source files;
[0110] In software development, source files usually contain some header files, such as standard library header files or custom header files. These header files are usually located in different directories. The directory dependency relationship of the header files included in the source files is used to describe the directory structure where the header files are located, as well as the relationship between the source files and the header files.
[0111] Obtaining a path list of header files based on the path list of source files and the directory dependency of header files can be achieved through the following steps: traverse the path list of source files and read the contents of each source file one by one; for each source file, parse the include directive include therein to find the header files it contains; according to the directory dependency of the header files, match the name of each header file with the directory where it is located to obtain the complete header file path; add the obtained header file path to the list to obtain the path list of the header files.
[0112] S203, determining all files in the path list of the header file as second source files;
[0113] That is, the storage location of each header file can be found according to the path list of the header files, and each header file in the storage location is determined as the second source file.
[0114] S204: Determine the source files other than the files that do not need to be compiled in the first source file and the second source file as the source files to be processed.
[0115] Based on the fact that the path list of the source files and header files may include files that do not need to be compiled, the user can enter the suffix of the file that does not need to be compiled or specify the file name in the visual configuration interface. After determining the first source file and the second source file, the file that does not need to be compiled is removed from the first source file and the second source file, and the remaining source files are determined as source files to be processed, thereby reducing unnecessary work and speeding up the construction of source files.
[0116] In the disclosed embodiments, by accurately analyzing the dependency relationship between source files and header files, the source files that need to be built are automatically determined based on the path list of the source files and the included header files. By eliminating user-specified files that do not need to be built, the number of source files to be processed is reduced, making the build process more efficient and reliable, thereby improving the overall efficiency and quality of source code construction.
[0117] In some embodiments, with respect to the aforementioned step S201 of determining all source files in the path list of source files as first source files, before determining the first source file, as shown in FIG3 , the following steps may be further included to avoid repeated processing of source files that have already been constructed and processed:
[0118] S301, performing hash calculation on all source files in the path list of the source files to obtain a unique first identifier for each source file;
[0119] That is, the hash calculation can be performed using the contents of the source file as input, or the source file and the contents of the header file contained in the source file can be concatenated together according to some agreed method, and then the concatenated contents are used as input for the hash calculation. The hash value generated by the hash algorithm is calculated based on the contents of the source file. When the source file content changes, the corresponding hash value will change. Therefore, the generated hash value can be used as a unique identifier for the file content.
[0120] The hash calculation can use a hash algorithm such as MD5, SHA-1, SHA-256 to calculate the input and obtain a hash value of a fixed length as the unique identifier of the source file. The hash value can be used as a fingerprint of the file to distinguish and identify different source files.
[0121] S302, matching the first identifier of the source file with the second identifier of the locally stored source file; the second identifier is an identifier corresponding to a source file that has been constructed in the code project;
[0122] The second identifier of each source file that has been built and processed and is locally stored on the electronic device is traversed. The second identifier is a unique identifier generated when the code project is built before the current source file is built. The second identifier and the first identifier of the source file are calculated using the same hash method. For example, when the code project is first built, the identifiers of each source file are generated. At this time, the second identifiers of the source files stored locally are empty. After the construction of each source file is completed, the identifiers of the processed source files can be stored locally as the second identifiers of the source files.
[0123] The first identifier of each source file in the source file path list is matched with each second identifier in sequence to determine whether the first identifier and the second identifier are the same. Since the identifier is calculated based on the file content, if the first identifier and the second identifier are the same, it means that the content of a source file in the source file path list is consistent with the content of a source file that has already been built, and the source file in the source file path list does not need to be built again during the current source file build process.
[0124] S303 : In response to the existence of a third source file whose second identifier is identical to the first identifier, determine the source files other than the third source file in the path list of source files as the first source files.
[0125] By calculating the unique identifier of each source file in the path list of the source file, the source files that have been built and processed can be compared with the source files in the path list. By comparing whether the unique identifiers are the same, it is possible to determine which source files have been built and processed and which source files have changed, thereby identifying the source files that need to be rebuilt without having to rebuild the entire project.
[0126] When the first source file is constructed, the newly generated file is used to update the existing file with the same name, and after the construction process is completed, the first identifier corresponding to the first source file can be stored locally on the electronic device.
[0127] In the embodiment of the present disclosure, by calculating the unique identifier of each source file in the source file path list and comparing the unique identifier with the identifier of the locally stored source file that has been built and processed, it can be used to determine which source files have been built, avoiding repeated compilation of source files that have already been built, thereby narrowing the scope of source files to be processed, reducing compilation time and construction costs, and improving compilation efficiency and construction speed.
[0128] In some embodiments, the construction configuration file is a JSON format file. Based on the format of the configuration file, as shown in FIG4 , obtaining multiple parameters according to the construction configuration file can be achieved in the following manner:
[0129] S401, deserialize the JSON format file to obtain a deserialization result;
[0130] The implementation method of the deserialization process can be determined according to the high-level programming language used to generate the JSON format file.
[0131] For C language, you can use the JSON-C library to deserialize JSON configuration files. JSON-C is an open source C language JSON parser that provides functions such as parsing JSON strings, generating JSON strings, and accessing and modifying JSON objects. Through the JSON-C library, you can parse the JSON configuration file into a JSON object, and then obtain parameters by accessing the key-value pairs of the JSON object.
[0132] For C++ language, you can use third-party libraries such as RapidJSON and nlohmann / JSON to deserialize JSON configuration files. The library provides a simple and easy-to-use API that can parse JSON strings or files into C++ objects to obtain parameters.
[0133] S402 : Obtain parameters included in the construction configuration file from the deserialization result according to the data structure type of the deserialization result.
[0134] Taking the deserialization result obtained by the above-mentioned JSON-C library as an example, the deserialization result is a JSON object, which contains key-value pairs, that is, the data structure type of the deserialization result is a JSON object. By accessing the JSON and reading the values of each key-value pair in turn, the parameters contained in the construction configuration file can be obtained.
[0135] In the embodiments of the present disclosure, JSON is used as the configuration file format, which makes the configuration file easier to read and write and has good compatibility between different programming languages and platforms. At the same time, the configuration file is deserialized into a data structure in the program, and the various fields and data therein can be directly accessed without manually parsing the JSON string, thereby improving the readability and maintainability of the configuration file. In addition, the deserialization will map it to a suitable data structure or object according to the data type in the JSON, ensuring type safety when accessing parameters and avoiding potential type conversion problems.
[0136] In some embodiments, as shown in Figure 5, the second parameter may include a compiler type and an output file type, and the compiler type may include but is not limited to a GNU tool chain, a Microsoft tool chain, and an LLVM tool chain; the output file type is the file type of the build target entered by the user in the visual configuration interface, and the output file type includes one of an executable file, a shared library, or a static library.
[0137] Based on the compiler type and the output file type, the aforementioned step S104 of constructing the source file to be processed according to the second parameter to obtain an output file of a specified type can be achieved in the following manner:
[0138] S501, calling a corresponding compilation tool chain according to the compiler type;
[0139] S502 : Start a build command to enable the compilation tool chain to convert the source file to be processed into an output file that conforms to the output file type.
[0140] A compilation toolchain is a set of tools used to convert source code into executable programs or libraries. It usually includes a compiler, assembler, linker, and other auxiliary tools for building software on the target platform.
[0141] Generating executable files or libraries using a compiler, assembler, and linker can include the following steps: (1) Preprocessing: The compiler preprocesses the source code, including processing macro definitions, header file inclusion, and other operations. The preprocessor modifies the source code according to the preprocessing instructions to generate a preprocessed code file; (2) Compilation: The compiler performs syntax analysis, semantic analysis, and code optimization on the preprocessed code and converts it into assembly language, that is, converting high-level languages such as C and C++ into assembly instructions; (3) Assembly: The assembler translates the assembly code into machine code instructions, that is, the assembler converts the assembly code into a binary representation of machine code and generates target files such as ELF, COFF, and other format files; (4) Linking: The linker parses the symbolic references in the target file, resolves the reference relationship between functions and variables, and combines the code, data, etc. in the target file, links the target file with other required library files, and generates an executable file or library file.
[0142] In the embodiment of the present disclosure, by selecting a compilation tool chain suitable for the code project to build the source file, the quality and efficiency of the source code file can be improved, the compilation time can be reduced, and the compilation efficiency can be improved.
[0143] In some embodiments, the parameters may further include a third parameter for indicating a path list of source files to be analyzed; when the source files to be analyzed are the source files to be processed, the method may further include the following steps to implement a code analysis function for the specified source files:
[0144] After converting the source file to be analyzed into the output file, obtaining the log file and the intermediate file generated during the construction process; extracting the code analysis results based on the log file and the intermediate file; and generating an analysis report for the source file to be analyzed based on the code analysis results.
[0145] The log file is used to record warnings, errors, and other additional information generated during the source file construction process. The intermediate file refers to the intermediate file generated by calling the compilation tool chain to build the source file, such as the compiler preprocessed code and assembly code. This intermediate file contains the compiler's analysis and conversion results of the source code, which can provide developers with richer code analysis information. Static analysis tools can be used to analyze the source files and intermediate files to analyze code control flow, data flow, symbol references, etc., and combined with the log file to obtain more comprehensive code analysis results.
[0146] In the compilation tool chain, some compilation options can be configured to output the intermediate files and log files. These options may include enabling static code analysis, turning on warning output, etc. The specific configuration method and options depend on the compilation tool chain used, such as GCC, Clang, etc.
[0147] For source files to be analyzed that are not source files to be processed and need to be built, lexical and syntactic analysis can be performed on the source files to be analyzed using open source tools such as flex and bison. Information such as the number and reference status of all functions, global variables, and classes in the source files to be analyzed can be extracted. A static HTML document can be generated based on the information, and the information can be visualized.
[0148] The analysis report of the source file to be analyzed can provide detailed information and statistical data about the source file to be analyzed to help developers better understand and evaluate the quality of the code. The content included in the code analysis report can be modified and set according to the needs of the developer. The code analysis report can include information about potential problems in the source file to be analyzed, such as potential errors, warnings, code style violations, etc., to help developers evaluate code quality and promptly discover and repair code defects; it can also provide performance analysis data and code measurement indicator information of the code, such as time complexity, memory usage, number of lines of code, number of functions, number of classes, and comment ratio, to help developers evaluate the size and complexity of the code and the parts that need to be optimized, thereby improving the execution efficiency of the code; in addition, the report can also list other source files, libraries or modules that the source file to be analyzed depends on, to help developers clarify the structure and dependencies of the code, so as to better perform maintenance and reconstruction.
[0149] The analysis report for the source file to be analyzed can be presented in various formats. For example, the report can be generated as an HTML page that developers can access and view the analysis results through a browser. Alternatively, the report can be generated as a document, such as a PDF or text file, for developers to download and review. Furthermore, the report can be generated as an email attachment and sent to the appropriate personnel, or uploaded to a cloud platform or other pre-defined storage location for developers to preview and view.
[0150] In the embodiment of the present disclosure, by parsing the log files and intermediate files generated during the construction process of the source files to be processed, code analysis results can be extracted, thereby providing richer information about the source files on the basis of completing the source file construction, helping developers to better understand the code project.
[0151] In some embodiments, the method may further include the step of performing code analysis on the obtained output file, which may include:
[0152] Extract metadata information of the output file, where the metadata information at least includes symbol table information and shared library link status; and generate a code analysis report of the output file based on the metadata information.
[0153] The metadata information of an output file is a set of data describing the file, which is used to describe the structure, various attributes and characteristics of the output file. It can be obtained through a specific command line or metadata extraction tool. The metadata information of an executable file may include but is not limited to file format information, symbol table information, shared link status, section header table information, segment header table information, dynamic linker information and other metadata specific to the output file format.
[0154] Among them, the file format information describes the file format of the executable file, such as ELF (Executable and Linkable Format), PE (Portable Executable), etc.; the symbol table information contains the name, type, address and other information of the functions, variables and other symbols in the executable file; the shared link status is used to describe the shared libraries and their paths that the executable file depends on; the section header table information and the segment header table information respectively describe the layout and properties of each section and each segment in the file, such as code segment, data segment, etc.; the dynamic linker information describes the relevant information of the dynamic linker, including the search path of the dynamic link library, dynamic symbol resolution, etc.
[0155] After obtaining the metadata information, the metadata information can be parsed to generate a static HTML document, and the document content can be visualized; or a static code analysis tool can be used to perform static analysis on the output file. The static analysis tool can check the syntax, structure, dependencies, performance issues, etc. of the code, and generate a code analysis report including code quality, dependency graph, performance, etc. based on the analysis results of the static analysis tool and the extracted metadata information.
[0156] The code analysis report can include potential problems and security vulnerabilities in the code corresponding to the output file, such as code duplication, excessive complexity, unused variables, buffer overflow, SQL injection, etc., so that developers can optimize and reconstruct the code of the output file according to the report, and can promptly fix these vulnerabilities to improve the security of the code. Since the output file is generated after multiple stages of processing such as compilation and linking, the structure and components of the file can be provided, such as symbol table information, shared link status, section header table information, segment header table information, dynamic linker information, etc., so that developers can understand the relationship and calling relationship between each module in the output file, which is helpful for modular development and code maintenance. The code analysis report can also provide performance data of the output file, such as function execution time, CPU occupancy, memory usage, etc. Developers can further identify performance bottlenecks by analyzing this data and make corresponding optimizations.
[0157] When analyzing static libraries, shared libraries, and executable programs under the Linux platform, it can be achieved in the following way: calling Linux commands such as "nm", "readelf", "objdump", and "ldd" to obtain the metadata information; based on the metadata information, counting all symbol table information and shared library link status in the output file; generating a static HTML document based on all the symbol table information and shared library link status, and visually presenting the results, such as in the form of a chart or list.
[0158] Similar to the analysis report of the source file to be analyzed, the code analysis report of the output file can also be output as an HTML page or document for developers to view. For details, please refer to the above content, and this application will not go into details.
[0159] In the embodiments of the present disclosure, by performing code analysis on the output files obtained by building source files, more information about executable files or libraries can be provided to developers, helping developers to better understand code projects and discover problems, thereby improving software quality and providing a basis for subsequent development and maintenance.
[0160] Next, in order to enable those skilled in the art to better understand the source file construction method provided by this application, the solution of this application is explained by taking the conversion of source code files in a code project into executable files as an example.
[0161] The generation of an executable file in the embodiment of the present application includes three stages: initialization configuration, configuration file parsing, and build processing. The initialization configuration is used to generate a configuration file based on user input; the configuration file parsing is used to obtain various parameters in the configuration file; and the build processing is used to convert the source code file into an executable file based on the various parameters.
[0162] (1) Initialization configuration:
[0163] As shown in FIG6 , when the electronic device detects or receives a first instruction, such as an initialization command “init”, a visual configuration interface may pop up, and the visual configuration interface may guide the user to fill in necessary configuration information.
[0164] The configuration information includes, but is not limited to, the code project name, the storage path of the source files, files that do not need to be built, the directory dependencies of the header files included in the source files, the generated file type, the build tool chain type, the linker parameter list, and other information. The storage path of the source files is used to specify the source files that need to be built; the files that do not need to be built are used to specify the file type or name that will not be built; based on the embodiment of the present application, when the source code files in the code project are used to generate executable files, the generated file type value is the executable file.
[0165] After the user completes the visual interface input, a configuration file is generated based on the received configuration information to complete the initialization configuration. In this embodiment, the configuration file can be a configuration file in JSON format.
[0166] 6A-6D , the interface shown in FIG6A is used to prompt the user to input the name of the source file to be built, and the source code file name entered by the user is "helloworld"; the interface shown in FIG6B is used to instruct the user to input the type of file to be generated, and the user is shown to have selected to build the source code file into an executable file; the interface shown in FIG6C is used to instruct the user to select a build tool chain, and the user is shown to have selected to use the GCC tool chain to convert the source code file into an executable file; the interface shown in FIG6D is used to input the name of the configuration file, and based on the information input on this interface, after the user completes the visual interface input, the received configuration information is written to a JSON configuration file named "antel".
[0167] (2) Configuration file parsing:
[0168] For the JSON configuration file, the configuration file can be deserialized first to convert the JSON configuration file into a data structure such as a dictionary or a class object. Various parameters are obtained from the data structure according to the type of the data structure. The parameters are used to determine the source file to be processed and passed to the build tool chain to generate an executable file.
[0169] (3) Build process:
[0170] When the electronic device detects or receives the second instruction, such as the build instruction or the rebuild instruction, it starts to perform a build process on the source file, as shown in FIG7 , which may include the following steps:
[0171] S701, determining the source file to be processed according to the parameters for determining the source file to be processed obtained from the configuration file;
[0172] S702, respectively calculating a hash value of each of the source files to be processed, and using the hash value as a third identifier of the source file to be processed;
[0173] S703, comparing the third identifier of the source file to be processed with the fourth identifiers of each source file that has been constructed and processed and stored locally, to determine whether there is a fourth source file with the same fourth identifier as the third identifier;
[0174] If the fourth identifier of the fourth source file does not exist and is the same as the third identifier, the source file to be processed is directly constructed and processed, a corresponding executable file is output, and the third identifier of the source file to be processed is stored locally.
[0175] S704: In response to the existence of a fourth source file having a fourth identifier that is identical to the third identifier, determining the source files other than the fourth source file among the source files to be processed as new source files to be processed;
[0176] S705, calling the tool chain to build the source file to be processed and output the corresponding executable file;
[0177] The build process includes compilation, assembly, and linking to output an executable file. As shown in Figure 8, after the source code is built, the target adds some files, generates a JSON configuration file antel based on the visual configuration interface, and uses this JSON configuration file to convert the source code file helloworld.cc into the executable file helloworld. A series of log files and intermediate files are also generated and stored in the folders log and obj, respectively.
[0178] S706: Store the third identifier of the source file to be processed locally.
[0179] In addition to providing the above-mentioned construction function of converting source files into executable files or libraries, the embodiments of the present application can also provide the function of analyzing the processed source files and the output executable files or libraries and generating visual analysis reports. For specific implementation methods, please refer to the above-mentioned embodiments.
[0180] In the disclosed embodiments, a source file construction method is provided for generating a static library, shared library, or executable program suitable for a specific computer platform from a source code file. By providing users with an easy-to-understand visual configuration interface and JSON configuration, the learning cost of building source code files is reduced for developers. The construction method is simple, avoiding complex external configuration, achieving the same source file construction requirements, applicable to different operating system platforms, easy to port and adapt, and highly scalable. Moreover, on the basis of completing the source code file construction, additional functions such as source file analysis and generated file analysis are also provided to the outside world, helping developers understand the project status in a more intuitive way and improving the developer experience.
[0181] Corresponding to the embodiment of the aforementioned source file construction method, as shown in FIG9 , the present application further provides an embodiment of a source file construction device, the device comprising:
[0182] The configuration information acquisition module 901 is configured to display a visual configuration interface in response to receiving a first instruction, wherein the interface is configured to instruct a user to input setting configuration information;
[0183] Configuration file generating module 902, for generating a build configuration file of the code project according to the received setting configuration information;
[0184] A parameter acquisition module 903 is configured to acquire a plurality of parameters according to the construction configuration file, wherein the parameters include a first parameter for determining a source file to be processed in the code project and a second parameter for source file construction;
[0185] The construction module 904 is configured to, in response to receiving the second instruction, construct the source file to be processed according to the second parameter to obtain an output file of a specified type.
[0186] In some embodiments, the first parameter includes a path list of source files in the code project, files that do not need to be compiled, and directory dependencies of header files included in the source files, and the apparatus further includes:
[0187] A first source file determining module, configured to determine all source files in the source file path list as first source files;
[0188] A header file path determination module, configured to determine a path list of the header files according to the path list of the source files and the directory dependency relationship of the header files included in the source files;
[0189] A second source file determining module, configured to determine all files in the path list of the header file as second source files;
[0190] The to-be-processed source file determining module is configured to determine the source files other than the files that do not need to be compiled in the first source file and the second source file as the to-be-processed source files.
[0191] In some embodiments, the first source file determination module is specifically configured to:
[0192] Performing hash calculation on all source files in the path list of the source files to obtain a unique first identifier for each source file;
[0193] Matching the first identifier of the source file with the second identifier of the locally stored source file; the second identifier is an identifier corresponding to a source file that has been built in the code project;
[0194] In response to the presence of a third source file having a second identifier identical to the first identifier, source files other than the third source file in the path list of source files are determined to be the first source files.
[0195] In some embodiments, the construction configuration file is a JSON format file; the parameter acquisition module is specifically used to:
[0196] Deserialize the JSON file to obtain a deserialization result.
[0197] According to the data structure type of the deserialization result, the parameters included in the construction configuration file are obtained from the deserialization result.
[0198] In some embodiments, the second parameter includes a compiler type and an output file type; and the building module is specifically configured to:
[0199] According to the compiler type, calling the corresponding compilation tool chain;
[0200] A build command is started to enable the compilation tool chain to convert the source file to be processed into an output file that conforms to the output file type.
[0201] In some embodiments, the parameters further include a third parameter for indicating a path list of source files to be analyzed; in response to the source file to be analyzed being the source file to be processed, the apparatus further includes:
[0202] After converting the source file to be analyzed into the output file, obtaining the log files and intermediate files generated during the construction process;
[0203] Extracting code analysis results based on the log file and the intermediate file;
[0204] An analysis report of the source file to be analyzed is generated based on the code analysis result.
[0205] In some embodiments, the apparatus further comprises:
[0206] Extracting metadata information of the output file, wherein the metadata information at least includes symbol table information and shared library link status;
[0207] A code analysis report of the output file is generated according to the metadata information.
[0208] The implementation process of the functions and effects of each unit in the above-mentioned device is specifically described in the implementation process of the corresponding steps in the above-mentioned method, and will not be repeated here.
[0209] For the device embodiments, since they basically correspond to the method embodiments, the relevant parts can be referred to the partial description of the method embodiments. The device embodiments described above are merely illustrative, wherein the units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed across multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the solution of this embodiment. A person of ordinary skill in the art can understand and implement it without expending creative work.
[0210] An embodiment of the present application also provides an electronic device, a structural diagram of the electronic device is shown in Figure 10, the electronic device 1000 includes at least one processor 1001, a memory 1002 and a bus 1003, and the at least one processor 1001 is electrically connected to the memory 1002; the memory 1002 is configured to store at least one computer-executable instruction, and the processor 1001 is configured to execute the at least one computer-executable instruction, thereby executing any step of the source file construction method provided in any embodiment or any optional implementation method in the present application.
[0211] Furthermore, the processor 1001 may be a Field-Programmable Gate Array (FPGA) or other devices with logic processing capabilities, such as a Microcontroller Unit (MCU) or a Central Processing Unit (CPU).
[0212] An embodiment of the present application also provides another readable storage medium storing a computer program, which is used to implement the steps of any source file construction method provided in any embodiment or any optional implementation method of the present application when executed by a processor.
[0213] The readable storage media provided in the embodiments of the present application include, but are not limited to, any type of disk (including floppy disks, hard disks, optical disks, CD-ROMs, and magneto-optical disks), ROM (Read-Only Memory), RAM (Random Access Memory), EPROM (Erasable Programmable Read-Only Memory), EEPROM (Electrically Erasable Programmable Read-Only Memory), flash memory, magnetic cards, or optical cards. In other words, readable storage media include any medium that can store or transmit information in a readable form by a device (e.g., a computer).
[0214] It is necessary to understand that specific embodiments of the subject matter have been described. Other embodiments are within the scope of the following claims. In some cases, the actions recited in the claims can be performed in a different order and still achieve the desired results. Furthermore, the processes depicted in the accompanying figures do not necessarily require the particular order shown or sequential sequence to achieve the desired results. In some implementations, multitasking and parallel processing may be advantageous.
[0215] Although this specification includes many specific implementation details, these should not be interpreted as limiting the scope of any invention or the scope of protection claimed, but are mainly used to describe the features of specific embodiments of specific inventions. Certain features described in multiple embodiments within this specification may also be implemented in combination in a single embodiment. On the other hand, the various features described in a single embodiment may also be implemented separately in multiple embodiments or in any suitable sub-combination. In addition, although features may work in certain combinations as described above and even initially claimed as such, one or more features from the claimed combination may be removed from the combination in some cases, and the claimed combination may point to a sub-combination or a variation of the sub-combination.
[0216] The above description is only a preferred embodiment of the present application and is not intended to limit the present application. Any modifications, equivalent replacements, improvements, etc. made within the spirit and principles of the present application shall be included in the scope of protection of the present application.
Claims
1. A source file construction method, characterized in that, The method includes: In response to receiving a first instruction, display a visualization configuration interface for instructing a user to input set configuration information; Generate a build configuration file for a code project according to the received set configuration information; According to the build configuration file, obtain a plurality of parameters, where the parameters include a first parameter for determining source files to be processed in the code project and a second parameter for source file building; In response to receiving a second instruction, build the source files to be processed according to the second parameter to obtain an output file of a specified type.
2. The method according to claim 1, characterized in that The first parameter includes a path list of source files in the code project, files that do not need to be compiled, and directory dependencies of header files included in the source files. The method further includes: Determine all source files in the path list of the source files as first source files; Determine a path list of the header files according to the path list of the source files and the directory dependencies of the header files included in the source files; Determine all files in the path list of the header files as second source files; Determine source files other than the files that do not need to be compiled in the first source files and the second source files as the source files to be processed.
3. The method according to claim 2, wherein The determining all source files in the path list of the source files as first source files includes: Perform a hash calculation on all source files in the path list of the source files to obtain a unique first identifier for each source file; Match the first identifier of the source file with a second identifier of the source file stored locally; the second identifier is an identifier corresponding to a source file that has been built in the code project; In response to there being a second identifier of a third source file that is the same as the first identifier, determine source files in the path list of the source files other than the third source file as the first source files.
4. The method according to claim 1, wherein The build configuration file is a JSON format file; The obtaining a plurality of parameters according to the build configuration file includes: Perform deserialization processing on the JSON format file to obtain a deserialization result; According to the data structure type of the deserialization result, obtain the parameters included in the build configuration file from the deserialization result.
5. The method according to claim 1, wherein The second parameter includes a compiler type and an output file type. The building the source files to be processed according to the second parameter to obtain an output file of a specified type includes: Call a corresponding compilation toolchain according to the compiler type; Start a build command so that the compilation toolchain converts the source files to be processed into an output file that conforms to the output file type.
6. The method according to claim 1, characterized in that The parameters further include a third parameter for indicating a path list of source files to be analyzed. In response to the source files to be analyzed being the source files to be processed, the method further includes: After converting the source files to be analyzed into the output file, obtain log files and intermediate files generated during the build process; Extract code analysis results according to the log files and the intermediate files; Generate an analysis report for the source files to be analyzed according to the code analysis results.
7. The method according to claim 1, wherein The method further includes: Extract the metadata information of the output file, where the metadata information at least includes symbol table information and shared library linking status; Generate a code analysis report of the output file according to the metadata information.
8. A source file construction device, characterized in that, The device includes: A configuration information acquisition module, configured to display a visual configuration interface in response to receiving a first instruction, where the interface is used to instruct a user to input set configuration information; A configuration file generation module, configured to generate a build configuration file of a code project according to the received set configuration information; A parameter acquisition module, configured to acquire a plurality of parameters according to the build configuration file, where the parameters include a first parameter for determining a source file to be processed in the code project and a second parameter for source file building; A build module, configured to build the source file to be processed according to the second parameter in response to receiving a second instruction to obtain an output file of a specified type.
9. An electronic device, characterized in that, Including: A processor and a memory; The memory is used to store a computer program; The processor is configured to execute the source file building method according to any one of claims 1-7 by calling the computer program.
10. A readable storage medium, on which a computer program is stored, characterized in that, When the program is executed by the processor, it implements the source file building method according to any one of claims 1-7.