Customize Build Process with STF_make_rtw_hook File
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.
STF_make_rtw_hook File
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 STFert 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
Conventions for Using the STF_make_rtw_hook File
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.mThe 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 filemytarget.tlc, name your hook filemytarget_make_rtw_hook.m, and name the hook function implemented within the filemytarget_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.
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 thefunction is called. The following flow chart summarizes the build process, highlighting the hook points. Valid values forSTF_make_rtw_hookhookMethodare'entry','before_tlc','after_tlc','before_make','after_make','exit', and'error'. Thefunction dispatches to the relevant code with aSTF_make_rtw_hookswitchstatement.
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 fieldcodeWasUpToDate. Valid for the'before_make','after_make', and'exit'stages only.buildArgs: Character vector containing the argument tomake_rtw. When you invoke the build process,buildArgsis 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
buildArgsargument does not apply for toolchain approach builds because these builds do not allow adding make arguments to themake_rtwcall. 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: TheRTW.BuildInfoobject that contains information for compiling and linking generated code. Available only for the'after_tlc','before_make','after_make', and'exit'stages. The information in the object at the end of the'after_tlc'stage might not be complete. In later stages, the'before_make'and'after_make'hook methods can also add information to the object. For more information about using theRTW.BuildInfoobject, see Code Compilation Customization.
Applications for STF_make_rtw_hook.m
This section shows how you can use the
hook
methods.STF_make_rtw_hook.m
In general, 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 can 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 can 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
Do not use the
file to:STF_make_rtw_hook.m
Change the model configuration. For example, do not use a hook method to:
Switch between model variants.
Call the
set_paramfunction.
Changing the model configuration might produce unexpected code generation results.
Run commands that compile the model. Compiling the model might produce unexpected behavior.
Control Code Regeneration Using 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', thebuildOptsstructure passed to the hook has a Boolean fieldcodeWasUpToDate. The field is set totrueif model code was up to date and code was not regenerated, orfalseif 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 ...
Use STF_make_rtw_hook.m for Your Build Procedure
To create a custom
hook file for
your build procedure, copy and edit this example file:STF_make_rtw_hook
Use these steps:
Copy
grt_make_rtw_hook.mto a folder on the MATLAB path. Rename the file in accordance with the naming conventions described in Conventions for Using the STF_make_rtw_hook File.Rename the
grt_make_rtw_hook.mfunction within the file to match the file name.Implement the hooks that you require by adding code to
casestatements within theswitch hookMethodstatement.
Hook Method after_tlc
The after_tlc hook method is available solely for backwards
compatibility.
The format of the generated code during the after_tlc stage is
not the final format.
