MATLAB: How to use the Code Generation Template (CGT) File

Embedded Coder

I'm trying to introduce some additional code into the generated code output from Embedded Coder and I came across the Code Generation Template file. I was wondering how this file is used and what sorts of capabilities it has to modify the generated code output?

Best Answer

1.) The simplest use for the CGT is to be used for code markup, in fact much of the surface level functionality is geared towards this. For example we provide four built in definitions for code mark up:
File Banner Section
(Optional) This section contains comments and tokens you use in generating a custom file banner.
Function Banner Section
(Optional) This section contains comments and tokens for use in generating a custom function banner.
Shared Utility Function Banner Section
(Optional) This section contains comments and tokens for use in generating a custom shared utility function banner.
File Trailer Section
(Optional) This section contains comments for use in generating a custom trailer banner.
An example syntax would be something like this:
<FunctionBanner style = "open_box" width = "80">
Model Name : %<ModelName>
FunctionName : %<FunctionName>
Return Type : %<ReturnType>
Arguments : %<Arguments>
Block : %<GeneratedFor>
Abstract : %<FunctionDescription>
Block Description : %<BlockDescription style = "content_only">
</FunctionBanner>
Which would subsequently generate this in the code:
More in-depth syntax for these sections is described on this page here:
And the example that this was drawn from can be opened using
>> rtwdemo_codetemplate
2.) You can also use the CGT file to simply drop text you would like to have placed in the generated code before or after a desired section. For example if I modify this section of the CGT from this:
%<Includes>

%<Defines>

%<Types>

%<Enums>

%<Definitions>

%<Declarations>

%<Functions>

To this:
#include topinclude.h
%<Includes>
#include bottominclude.h
%<Defines>
%<Types>
%<Enums>
%<Definitions>
%<Declarations>
%<Functions>
then the subsequent code generation will produce the following:
This covers the basic functionality of the CGT file.
3.) Now, for more advanced use and specification of custom tokens, sections and subsections we need to bring in the Custom File Processing (CFP) template file described here:
The CFP file can be used for a few purposes, the most important of which are, generating your own additional source and header files, and modifying the predefined source and header files that normally get generated. The following code in a CFP file can be used to modify the standard generated header file, and serves as a good template for how to modify the predefined sections in the CGT file:
%%Add a #define to the model's public header file model.h
%assign pubName = LibGetMdlPubHdrBaseName()
%assign modelH = LibCreateSourceFile("Header", "Simulink", pubName)
%openfile tmpBuf
#define ACCELERATION 9.81
%closefile tmpBuf
%<LibSetSourceFileSection(modelH,"Defines",tmpBuf)>
The %assign statements are getting the name of the generated header file and grabbing the file itself
The text inside the %openfile %closefile pair is what we want to append to the header file
And the final statement is pushing the text from the buffer tmpBuf and appending it to modelH (the header file we specifed earlier) at the "Defines" section.
The entirety of the CFP doc page is a very good start if you want to dive deeper into TLC code generation and manipulating the Code Generation Output and I would suggest following through the example if you want to understand more about the creation of custom sections to use in the CGT file, or other customization of the code generation process. I would also just like to mention that if you do make custom modifications to the code generation process, we cannot guarantee the functionality of the newly modified code, so always make sure to check the effects of modifying these files.