%% Add Custom Toolchains to MATLAB® Coder™ Build Process
% This example shows how to register and use a toolchain to compile an executable. This example uses Intel® Compiler, but the concepts and API shown below can be used for any toolchain. The registered toolchain can be selected from a list of toolchains and a makefile will be generated to build the code using that toolchain.
%% About the coderrand Function
% In this example, you generate code for the coderrand function. This MATLAB® function simply generates a random scalar value from the standard uniform distribution on the open interval (0,1).

type coderrand

%
%% Toolchain Info
% A toolchain is a collection of tools that is required for compiling and linking generated code for a specified platform. A toolchain has multiple tools, such as a compiler, linker and archiver. Each of these tools can take multiple options, which can be grouped into configurations like Faster Builds, Faster Runs, Debug. A toolchain object describes the basic information of the toolchain. The toolchain object has methods to describe all of the above. The object can be saved into a MATLAB file and shared across installations.
% This example uses the toolchain definition file intel_tc.m.

tc = intel_tc
save intel_tc tc

%
%% Registering a Toolchain
% Toolchains are registered through RTW.TargetRegistry. To register the toolchain, you can also use rtwTargetInfo which will be loaded by the system automatically.

copyfile myRtwTargetInfoCustom.txt rtwTargetInfo.m
type rtwTargetInfo

% Now, you can reset the TargetRegistry to pick up the new rtwTargetInfo.

RTW.TargetRegistry.getInstance('reset');

%
%% Choosing the Toolchain
% You can now create the config object that is configured to create an executable using the new toolchain.

cfg = coder.config('exe');
cfg.CustomSource = 'coderrand_main.c';
cfg.CustomInclude = pwd;
cfg.Toolchain = 'Intel v14';

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

cfg.GenCodeOnly = true;

% Run the codegen to generate the code and makefile that uses the new toolchain.

codegen -config cfg coderrand 

% Once the codegen is finished, and you had Intel compilers installed, you can use system('coderrand.exe') to run the executable.
%
%% Cleanup
% You can reset the TargetRegistry to remove the toolchain that you registered above.

delete ./rtwTargetInfo.m
RTW.TargetRegistry.getInstance('reset');

% Copyright 2018 The MathWorks, Inc.