Main Content

Build Process Customization

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 class RTW.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 from buildInfo.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:

To call the methods, use the syntax:

method_name(buildInfo,input_arg1,...,input_argN)

Alternatively, you can enter:

buildInfo.method_name(input_arg1,...,input_argN)

To use the build information object after code generation is complete, load the buildInfo.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 the coder.ExternalDependency class. The coder.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 and coder.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 FormatResult
ScriptScript can gain access to the project (top-level function) name and the build information directly.
FunctionFunction can receive the project name and the build information as arguments.

To write the post code-generation command as a script, set PostCodeGenCommand to the script name. You can access the project name in the variable projectName and the RTW.BuildInfo object in the variable buildInfo. 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, include projectName 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 the addLinkFlags 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:

  1. To open the Generate dialog box, on the Generate Code page, click the Generate arrow .

  2. Click More Settings.

  3. On the Custom Code tab, set the Post-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

| | | |

Related Topics