The build process lets you supply optional custom code in hook methods that are executed at specified points in the code generation and make process. You can use hook methods to add target-specific actions to the build process.
You can modify hook methods in a file generically referred to as
, where
STF
_make_rtw_hook.m
is the name of a system
target file, such as STF
ert
or mytarget
. This
file implements a function,
, that dispatches
to a specific action, depending on the STF
_make_rtw_hookhookMethod
argument passed
in.
The build process calls
, passing in the
STF
_make_rtw_hookhookMethod
argument and other arguments. You implement only
those hook methods that your build process requires.
If your model contains reference models, you can implement an
for each
reference model as required. The build process calls each
STF
_make_rtw_hook.m
for
reference models, processing these files recursively (in dependency order).STF
_make_rtw_hook
For the build process to call the
, check that the
following conditions are met:STF
_make_rtw_hook
The
file
is on the MATLAB® path.STF
_make_rtw_hook.m
The file name is the name of your system target file
(STF
), appended to the text
_make_rtw_hook.m
. For example, if you generate code
with a custom system target file mytarget.tlc
, name your
hook file mytarget_make_rtw_hook.m
, and name the hook
function implemented within the file
mytarget_make_rtw_hook
.
The hook function implemented in the file uses the function prototype described in STF_make_rtw_hook.m Function Prototype and Arguments.
The function prototype for
is:STF
_make_rtw_hook
function STF_make_rtw_hook(hookMethod, modelName, rtwRoot, templateMakefile, buildOpts, buildArgs, buildInfo)
The arguments are defined as:
hookMethod
: Character vector specifying the stage of
build process from which the
function
is called. The following flow chart summarizes the build process,
highlighting the hook points. Valid values for STF
_make_rtw_hookhookMethod
are 'entry'
, 'before_tlc'
,
'after_tlc'
, 'before_make'
,
'after_make'
, 'exit'
, and
'error'
. The
function
dispatches to the relevant code with a STF
_make_rtw_hookswitch
statement.
modelName
: Character vector specifying the name of the
model. Valid at all stages of the build
process.
rtwRoot
: Reserved.
templateMakefile
: Name of template makefile.
buildOpts
: A MATLAB structure that contains the Boolean field
codeWasUpToDate
. Valid for the
'before_make'
, 'after_make'
, and
'exit'
stages only. The buildOpts
fields include:
buildArgs
: Character vector containing the argument to
make_rtw
. When you invoke the build process,
buildArgs
is copied from the argument following
"make_rtw"
in the Configuration Parameters + Code Generation + Make command field.
For example, the following make arguments from the Make command field
make_rtw VAR1=0 VAR2=4
generate the following:
% make -f untitled.mk VAR1=0 VAR2=4
The buildArgs
argument does not apply for toolchain
approach builds because these builds do not allow adding make arguments to
the make_rtw
call. On the compiler command line, to
provide custom definitions (for example, VAR1=0 VAR2=4
)
that apply for both TMF approach and toolchain approach builds, use the Configuration Parameters > Code Generation > Custom Code > Defines field.
buildInfo
: The MATLAB structure that contains the model build information fields.
Valid for the 'after_tlc'
,
'before_make'
, 'after_make'
, and
'exit'
stages only. For information about these
fields and functions to access them, see Build Process Customization.
Here are some examples of how to apply the
hook
methods.STF
_make_rtw_hook.m
In general, you can use the 'entry'
hook to initialize the
build process, for example, to change or validate settings before code is generated.
One application for the 'entry'
hook is to rerun the
auto-configuration script that initially ran at target selection time to compare
model parameters before and after the script executes, for validation
purposes.
The other hook points, 'before_tlc'
,
'after_tlc'
, 'before_make'
,
'after_make'
, 'exit'
, and
'error'
are useful for interfacing with external tool chains,
source control tools, and other environment tools.
For example, you could use the
file at a
stage after STF
_make_rtw_hook.m'entry'
to obtain the path to the build folder. At
the 'exit'
stage, you could then locate generated code files
within the build folder and check them into your version control system. You can use
'error'
to clean up static or global data used by the hook
function when an error occurs during code generation or the build process.
Note
The build process temporarily changes the MATLAB working folder to the build folder for stages
'before_make'
, 'after_make'
,
'exit'
, and 'error'
. Your
file
must not make incorrect assumptions about the location of the build folder. At a
point after the STF
_make_rtw_hook.m'entry'
stage, you can obtain the path to the
build folder. In the following MATLAB code example, the build folder path is returned as a character
vector to the variable buildDirPath
.
buildDirPath = rtwprivate('get_makertwsettings',gcs,'BuildDirectory');
Note
A change to model configuration within
file
(including switching Variants) might lead to unexpected results in code
generation.STF
_make_rtw_hook.m
When you rebuild a model, by default, the build process performs checks to determine whether changes to the model or relevant settings require regeneration of the top model code. (For details on the criteria, see Control Regeneration of Top Model Code.) If the checks determine that top model code generation is required, the build process fully regenerates and compiles the model code. If the checks indicate that the top model generated code is current with respect to the model, and model settings do not require full regeneration, the build process omits regeneration of the top model code.
Regardless of whether the top model code is regenerated, the build process
subsequently calls the build process hooks, including
functions
and the post code generation command. The following mechanisms allow you to perform
actions related to code regeneration in the
STF
_make_rtw_hook
functions:STF
_make_rtw_hook
To force code regeneration, use the following function call from the
'entry'
hook:
rtw.targetNeedsCodeGen('set', true);
In hooks from 'before_tlc'
through
'exit'
, the buildOpts
structure
passed to the hook has a Boolean field codeWasUpToDate
.
The field is set to true
if model code was up to date and
code was not regenerated, or false
if code was not up to
date and code was regenerated. You can customize hook actions based on the
value of this field. For example:
... case 'before_tlc' if buildOpts.codeWasUpToDate %Perform hook actions for up to date model else %Perform hook actions for full code generation end ...
To create a custom
hook file for your build procedure, copy and edit the example STF
_make_rtw_hookert_make_rtw_hook.m
file, which is located in the
folder
(open), as follows:matlabroot
/toolbox/coder/embeddedcoder
Copy ert_make_rtw_hook.m
to a folder in
the MATLAB path. Rename it in accordance with the naming conventions
described in Conventions for Using the STF_make_rtw_hook File.
For example, to use it with the GRT target grt.tlc
,
rename it to grt_make_rtw_hook.m
.
Rename the ert_make_rtw_hook
function
within the file to match the file name.
Implement the hooks that you require by adding code to
case statements within the switch hookMethod
statement.