Build Process Customization - MATLAB & Simulink (original) (raw)
For certain applications, you might want to control aspects of the build process that occur after C/C++ source code generation but before compilation. For example, you can specify compiler or linker options. You can get and modify all the generated source files to add a copyright disclaimer. You can control the build process in a variety of ways. Customize the build process by:
- Using the function coder.updateBuildInfo.
- Using the methods of an RTW.BuildInfo object.
- Modifying the build information by using a coder.ExternalDependency class.
- Modifying the build information with a script or function executed by the
PostCodeGenCommand
configuration property. This script or function is called a post-code-generation command.
All of these approaches work by altering the makefile that is generated and used to build your code. As a best practice, it is recommended to use the first three approaches, coder.updateBuildInfo
,RTW.BuildInfo
, and coder.ExternalDependency
. These approaches enable you to preconfigure your MATLAB® code with the build information that you require. Alternatively, the post-code generation command can provide an additional, highly customizable approach, based around an independent function or script.
The coder.ExternalDependency
class and the post-code-generation command provide access to the build information object, buildInfo
. You can use build information methods on buildInfo
to configure project, build, and dependency information. MATLAB Coderâ„¢ creates buildInfo
from the classRTW.BuildInfo
at the start of the build. This object is stored in a MAT-file buildInfo.mat
and saved in the build folder.
After code generation, you can access the build information object by loading it frombuildInfo.mat
. Do not confuse the build information object with the build configuration object, coder.BuildConfig, which provides specific functionality for configuring build within a coder.ExternalDependency
class.
RTW.BuildInfo
Methods
To access or write data to the build information object, use RTW.BuildInfo methods. Using these methods you can modify:
- Compiler options
- Linker options
- Preprocessor identifier definitions
- Source files and paths
- Include files and paths
- Precompiled external libraries
- Packaging options.
See Package Code for Other Development Environments.
To call the methods, use the syntax:
methodname(buildInfo,inputarg1,...,inputargN)
Alternatively, you can enter:
buildInfo.methodname(inputarg1,...,inputargN)
To use the build information object after code generation is complete, load thebuildInfo.mat
file from your generated code. For example:
load(fullfile('.','raspberrypi_generated_code','buildInfo.mat')); packNGo(buildInfo, 'fileName','copy_to_raspberrypi');
coder.updateBuildInfo
Function
The coder.updateBuildInfo
function provides a convenient way to customize the build process from within your MATLAB code. For more information and examples, see the coder.updateBuildInfo and RTW.BuildInfo reference pages.
coder.ExternalDependency
Class
When you are working with external code integration or you have multiple functions that use the same build information, customize the build process by using thecoder.ExternalDependency
class. Thecoder.ExternalDependency
class provides access to the build information object and methods. For more information and examples, see Develop Interface for External C/C++ Code and the coder.ExternalDependency reference page.
Post-Code-Generation Command
As a best practice, customize your build process by using the first two approaches,coder.updateBuildInfo
andcoder.ExternalDependency
. A third approach that provides additional flexibility is a post-code-generation command. A post-code-generation command is a function or script executed by the PostCodeGenCommand
configuration object property. Set the command by using your code generation configuration object (coder.MexCodeConfig, coder.CodeConfig or coder.EmbeddedCodeConfig).
Command Format | Result |
---|---|
Script | Script can gain access to the project (top-level function) name and the build information directly. |
Function | Function can receive the project name and the build information as arguments. |
To write the post code-generation command as a script, setPostCodeGenCommand
to the script name. You can access the project name in the variable projectName
and theRTW.BuildInfo
object in the variablebuildInfo
. At the command line, enter:
cfg = coder.config('lib'); cfg.PostCodeGenCommand = 'ScriptName';
When you define the command as a function, you can specify an arbitrary number of input arguments. If you want to access the project name, includeprojectName
as an argument. If you want to modify or access build information, add buildInfo
as an argument. At the command line, enter:
cfg = coder.config('lib'); cfg.PostCodeGenCommand = 'FunctionName(projectName, buildInfo)';
For example, consider the function setbuildargs
that takes the build information object as a parameter and adds linker options by using theaddLinkFlags
method.
function setbuildargs(buildInfo)
% The example being compiled requires pthread support.
% The -lpthread flag requests that the pthread library be included
% in the build
linkFlags = {'-lpthread'};
buildInfo.addLinkFlags(linkFlags);
To use this function as a post-code-generation command, create a configuration object. Use this configuration object when you generate code. For example:
cfg = coder.config('dll'); cfg.PostCodeGenCommand = 'setbuildargs(buildInfo)'; codegen -config cfg foo
To set a post-code-generation command from the MATLAB Coder app:
- To open the Generate dialog box, on theGenerate Code page, click theGenerate arrow
.
- Click More Settings.
- On the Custom Code tab, set thePost-code-generation command parameter.
If your post-code-generation command calls user-defined functions, make sure that the functions are on the MATLAB path. If the build process cannot find a function that you use in your command, the process fails.
See Also
coder.MexCodeConfig | coder.CodeConfig | coder.EmbeddedCodeConfig | coder.updateBuildInfo | coder.ExternalDependency