Main Content

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.

Toolchain

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.

Open a Model

Open the CounterModel model. By default, this model is configured to use a makefile-based toolchain approach for building an executable file.

model = 'CounterModel';
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 computer for building an executable file from 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 make it a selectable Toolchain option.

You must 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.

edit intel_tc

You can use the diagnostic message pane to view build errors and warnings from custom compilers. For more information, see Diagnostic Message Pane.

Run the toolchain definition file to generate the ToolchainInfo object.

tc = intel_tc;

Save the ToolchainInfo object tc to a MAT file.

save intel_tc tc

Register the Custom Toolchain

After you create the ToolchainInfo object for the new toolchain, register the toolchain in RTW.TargetRegistry.

Write an rtwTargetInfo.m file and add the file to the MATLAB path.

copyfile rtwTargetInfoCopy.m rtwTargetInfo.m
edit rtwTargetInfo

To use the new rtwTargetInfo.m file, reset the TargetRegistry.

RTW.TargetRegistry.getInstance('reset');

Choose the Custom Toolchain

Reopen the Configuration Parameters dialog box. Click Toolchain. You see the new toolchain in the list. Select the Intel v18 toolchain.

Programmatically, you can perform the same task by using these 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.

If you have not installed the Intel toolchain, you can use the following command to only generate code and the makefile.

set_param(cs, 'GenCodeOnly', 'on')

To generate code and the makefile and build the generated code, run this command.

slbuild(model)
### Starting build procedure for: CounterModel
### Successful completion of code generation for: CounterModel

Build Summary

Top model targets:

Model         Build Reason                                         Status           Build Duration
==================================================================================================
CounterModel  Information cache folder or artifacts were missing.  Code generated.  0h 0m 12.426s 

1 of 1 models built (0 models already up to date)
Build duration: 0h 0m 13.516s

Obtain build folder 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

Restore

To deregister the toolchain, reset TargetRegistry.

cd ..
RTW.TargetRegistry.getInstance('reset');

Close the model.

close_system(model, 0)

Clear the variables introduced to the workspace.

clear INC K LIMIT RESET model tc cs toolchain