Register Custom Toolchain and Build Executable
This example shows how to register and use a toolchain to build an executable file. This example uses the Intel® compiler. However, the concepts and programming interface apply for other toolchains. Once you register a toolchain, you can configure a model such that the code generator uses the toolchain to build an executable file for the model.
A toolchain is a collection of tools required to compile and link code for a specified platform. A toolchain consists of multiple tools, such as a compiler, linker, and archiver. You can configure the tools in a toolchain with multiple options and group tool specifications into types of configurations.
Create a New Folder and Copy Relevant Files
The following code creates a folder in your current working folder (pwd). The new folder contains files for this example. If you do not want to affect the current folder, or cannot generate files in this folder, change your working folder before invoking the command.
copyfile(fullfile(matlabroot,'toolbox','rtw','rtwdemos','toolchain_demo'),'toolchain_demo','f') cd toolchain_demo
Open a Model
rtwdemo_counter model. By default, this model is configured to use the Generic Real-Time Target (GRT) configuration and the toolchain approach for building an executable file. The capability demonstrated in this example is available for models configured to use the toolchain approach.
model = 'rtwdemo_counter'; open_system(model)
Choose a Toolchain
Open the Configuration Parameters dialog box. Select Code Generation. The Toolchain settings section contains parameters for configuring a toolchain. From the Toolchain drop-down list, select the toolchain installed on your development system for building an executable file from the code generated from your model.
By default, the Faster Runs build configuration is selected. Click Show settings to see the toolchain flags specified for building the generated code. Choose a build configuration based on your current application development goal.
Close the Configuration Parameters dialog box.
Create a ToolchainInfo Object
This example shows how you can register a custom toolchain and add it as a selectable Toolchain option.
The first step to registering a custom toolchain is to create a
ToolchainInfo object that contains information about the toolchain. Methods are available to set toolchain specifications. You can share a
ToolchainInfo object across installations.
Open the toolchain definition file for an Intel compiler. This file creates a
ToolchainInfo object that contains information about the Intel toolchain on a 64-bit Windows® platform.
You can display the errors and warnings in the diagnostics messages pane from builds generated by custom compilers. For more information, see Diagnostic Message Pane.
Run the toolchain definition file to generate the
tc = intel_tc;
tc to a MAT file.
save intel_tc tc
Register the Custom Toolchain
After you create the
ToolchainInfo object for the new toolchain, register it. Register a toolchain in
RTW.TargetRegistry. To register the toolchain, write an
rtwTargetInfo.m file. Then, add that file to the MATLAB path so the system loads it automatically.
TargetRegistry to use the new
Choose the Custom Toolchain
Reopen the Configuration Parameters dialog box. Click Toolchain. You should see the new toolchain in the list. Select the
Intel v18 toolchain.
Programmatically, you can accomplish the same task with the following commands:
cs = getActiveConfigSet(model); set_param(cs, 'Toolchain', tc.Name)
Verify your selection.
toolchain = get_param(cs, 'Toolchain')
toolchain = 'Intel v18 | nmake makefile (64-bit Windows)'
Build the Model Using the Custom Toolchain
You can now build the model with the new custom toolchain.
Note: If you do not have the Intel toolchain installed, you can use the following command to generate the code and makefile only.
set_param(cs, 'GenCodeOnly', 'on')
Build the model to generate the code and makefile that uses the new toolchain.
### Starting build procedure for: rtwdemo_counter ### Successful completion of code generation for: rtwdemo_counter Build Summary Top model targets built: Model Action Rebuild Reason =================================================================================== rtwdemo_counter Code generated Code generation information file does not exist. 1 of 1 models built (0 models already up to date) Build duration: 0h 0m 16.819s
Obtain the build directory information.
dirInfo = RTW.getBuildDir(model);
Examine the generated makefile.
edit(fullfile(dirInfo.BuildDirectory, [model '.mk']))
If Intel compilers are installed, then when the build process is complete, you can run the executable file.
if ispc system([model '.exe']) else system(model) end
You can optionally remove the folder you created earlier.
cd .. rmdir('toolchain_demo', 's')
TargetRegistry to remove the toolchain that you registered above.
Close the model.
Clear the variables introduced in the workspace.
clear INC K LIMIT RESET model tc cs toolchain