Main Content

Customize Post-Code-Generation Build Processing

The code generator provides a set of tools, including a build information object, you can use to customize build processing that occurs after code generation. You can use such customizations for target development or the integration of third-party tools into your application development environment.

Workflow for Setting Up Customizations

The following figure and the steps that follow show the general workflow for setting up post-code-generation customizations.

  1. Program the post code generation command.

  2. Define the post code generation command.

  3. Suppress makefile generation, if applicable.

  4. Build the model.

  5. Modify the command and rebuild the model until the build results are acceptable.

Build Information Object

At the start of a model build, the build process logs the following build option and dependency information to a temporary build information object:

  • Compiler options

  • Preprocessor identifier definitions

  • Linker options

  • Source files and paths

  • Include files and paths

  • Precompiled external libraries

You can retrieve information from and add information to this object by using an extensive set of functions. For a list of available functions and detailed function descriptions, see Code Compilation Customization. Program a Post Code Generation Command explains how to use the functions to control post code generation build processing.

Program a Post Code Generation Command

For certain applications, you could want to control aspects of the build process after the code generation. For example, you can use this approach if you develop your own target, or you want to apply an analysis tool to the generated code before continuing with the build process. You can apply this level of control to the build process by programming and then defining a post code generation command.

A post code generation command is a MATLAB® language file that typically calls functions that get data from or add data to the build information object of the model. You can program the command as a script or function.

If you program the command as a:Then the:
ScriptScript can gain access to the model name and the build information directly
FunctionFunction can pass the model 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 you use in your command, the build process errors out.

You can then call a combination of build information functions, as listed in Code Compilation Customization, to customize the post code generation build processing of the model.

The following example shows a fragment of a post code generation command that gets the file names and paths of the source and include files generated for a model for analysis.

function analyzegencode(buildInfo)
% Get the names and paths of source and include files
% generated for the model and then analyze them.

% buildInfo - build information for my model.

% Define cell array to hold data.
MyBuildInfo={};

% Get source file information.
MyBuildInfo.srcfiles=getSourceFiles(buildInfo, true, true);
MyBuildInfo.srcpaths=getSourcePaths(buildInfo, true);

% Get include (header) file information.
MyBuildInfo.incfiles=getIncludeFiles(buildInfo, true, true);
MyBuildInfo.incpaths=getIncludePaths(buildInfo, true);

% Analyze generated code.
.
.
.

Define a Post Code Generation Command

After you program a post code generation command, inform the build process that the command exists and to add it to the build processing of the model. Define the command with the PostCodeGenCommand model configuration parameter. When you define a post code generation command, the build process evaluates the command after generating and writing the generated code to disk and before generating a makefile.

As the following syntax lines show, the arguments that you specify when setting the configuration parameter varies depending on whether you program the command as a script, function, or set of functions.

Note

When defining the command as a function, you can specify an arbitrary number of input arguments. To pass the name and build information of the model to the function, specify identifiers modelName and buildInfo as arguments.

Script

set_param(model, 'PostCodeGenCommand',...
'pcgScriptName');

Function

set_param(model, 'PostCodeGenCommand',...
 'pcgFunctionName(modelName)');

Multiple Functions

pcgFunctions=...
'pcgFunction1Name(modelName);...
pcgFunction2Name(buildInfo)';
set_param(model, 'PostCodeGenCommand',...
 pcgFunctions);

The following call to set_param defines PostCodGenCommand to evaluate the function analyzegencode.

set_param(model, 'PostCodeGenCommand',...
'analyzegencode(buildInfo)');

Customize Build Process with PostCodeGenCommand and Relocate Generated Code to an External Environment

This example shows how to use the build information programming interface and the model configuration parameter Post code generation command (PostCodeGenCommand) to customize the build process. For this example, Post code generation command is set to BuildInfoModel_data. The build process invokes that function after code generation.

The example also shows how to use build configuration function rtwmakecfg.m.

Open Example Model

Open the example model BuildInfoModel.

open_system('BuildInfoModel');

Generate Code from Model

Generate code. The code generator produces the file BuildInfo.html, which documents the build information object.

Examine the Build Process Customizations and Output

Examine the build process customizations and the post code generation query of the build information object. In a Web browser, open the file BuildInfo.html. The file provides hyperlinks that you can use to examine the code produced for the model. Alternatively, in the MATLAB Command Window:

  • To view the function configured to execute during the post code generation stage of the build process, enter:

get_param('BuildInfoModel','PostCodeGenCommand');

  • To study the programming interface for the build information object, enter:

BuildInfoModel_data(buildInfo);

  • To study how the example uses the rtwmakecfg function, enter:

edit rtwmakecfg.m;

  • To view the build information object data that the build process saves in the buildInfo.mat file, open the file BuildInfoModel_grt_rtw\buildInfo.mat.

  • To view the contents of the post code generation function BuildInfoModel_data.m, enter:

edit BuildInfoModel_data.m;

At the end of the file, the function calls packNGo to package the source and object files that the build information object identifies for relocation.

Related Topics

Suppress Makefile Generation

The code generator lets you suppress makefile generation during the build process. For example, you can use this support when you integrate tools into the build process that do not use makefiles.

To instruct the code generator not to produce a makefile, do one of the following:

  • Clear the Generate makefile option on the Code Generation pane of the Configuration Parameters dialog box.

  • Set the value of the configuration parameter GenerateMakefile to off.

When you suppress makefile generation,

See Also