Customize the Post-Code-Generation Build Process

For certain applications, you might want to control aspects of the build process that occur after code generation but before compilation. For example, you might want to specify compiler or linker options. You can customize build processing that occurs after code generation using MATLAB® Coder™ for MEX functions, C/C++ libraries and C/C++ executables.

You can customize your build using:

Customize Build Using coder.updateBuildInfo

To customize the post-code-generation build from your MATLAB code:

  1. In your MATLAB code, call coder.updateBuildInfo to update the build information object. You specify a build information object method and the input arguments for the method. See coder.updateBuildInfo and Build Information Methods.

  2. Generate code from your MATLAB code using the codegen command or from the project interface.

Customize Build Using Post-Code-Generation Command

To customize your build using the post-code-generation command:

  1. Write Post-Code-Generation Command. Typically, you use this command to get the project name and build information or to add data to the build information object.

  2. Use Post-Code-Generation Command to Customize Build.

Build Information Object

At the start of a build, the MATLAB Coder build process logs the following project, build option, and dependency information to a build information object, RTW.BuildInfo:

  • Compiler options

  • Preprocessor identifier definitions

  • Linker options

  • Source files and paths

  • Include files and paths

  • Precompiled external libraries

Use the Build Information Methods to access this information in the build information object. Write Post-Code-Generation Command explains how to use the functions to control a post-code-generation build.

When code generation is complete, MATLAB Coder creates a buildInfo.mat file in the build folder.

Build Information Methods

Use these methods to access or write data to the build information object. The syntax is:

buildInfo.method_name(input_arg1, ..., input_argn)

 addCompileFlags

 addDefines

 addIncludeFiles

 addIncludePaths

 addLinkFlags

 addLinkObjects

 addNonBuildFiles

 addSourceFiles

 addSourcePaths

 addTMFTokens

 findIncludeFiles

 getCompileFlags

 getDefines

 getFullFileList

 getIncludeFiles

 getIncludePaths

 getLinkFlags

 getNonBuildFiles

 getSourceFiles

 getSourcePaths

 packNGo

 updateFilePathsAndExtensions

 updateFileSeparator

Write Post-Code-Generation Command

A post-code-generation command is a MATLAB file that typically calls functions that get data from or add data to the build information object. For example, you can access the project name in the variable projectName and the RTW.BuildInfo object in the variable buildInfo. You can write the command as a script or a function.

If You Write the Command as aThen the
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.

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.

You can call combinations of build information functions to customize the post-code-generation build. See Write and Use Post-Code-Generation Command at the Command Line

Write Post-Code-Generation Command as a Script

Set PostCodeGenCommand to the script name.

At the command line, enter:

cfg = coder.config('lib');
cfg.PostCodeGenCommand = 'ScriptName';

Write Post-Code-Generation Command as a Function

Set PostCodeGenCommand to the function signature. 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)';

Use Post-Code-Generation Command to Customize Build

After you have written a post-code-generation command, you must include this command in the build processing. You can include the command from the project settings dialog box or the command line.

Use Post-Code-Generation Command in 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.

    How you use the PostCodeGenCommand option depends on whether you write the command as a script or a function. See Use Post-Code-Generation Command at the Command Line and Use Post-Code-Generation Command in the MATLAB Coder App..

Use Post-Code-Generation Command at the Command Line

Set the PostCodeGenCommand option for the code generation configuration object (coder.MexCodeConfig, coder.CodeConfig or coder.EmbeddedCodeConfig).

How you use the PostCodeGenCommand option depends on whether you write the command as a script or a function. See Use Post-Code-Generation Command at the Command Line and Use Post-Code-Generation Command in the MATLAB Coder App..

Write and Use Post-Code-Generation Command at the Command Line

The following example shows how to write and use a post-code-generation command as a function. The setbuildargs function takes the build information object as a parameter, sets up link options, and adds them to the build information object.

  1. Create a post-code-generation command as a function, setbuildargs, which takes the buildInfo object as a parameter:

    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);

  2. Create a code generation configuration object. Set the PostCodeGenCommand option to 'setbuildargs(buildInfo)' so that this command is included in the build processing:

    cfg = coder.config('mex');
    cfg.PostCodeGenCommand = 'setbuildargs(buildInfo)';
    

  3. Using the -config option, generate a MEX function passing the configuration object to codegen. For example, for the function foo that has no input parameters:

    codegen -config cfg foo
    

Was this topic helpful?