MATLAB Coder

Adding a Custom Toolchain

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.

Create a New Folder and Copy Relevant Files

The following code creates a folder in your current working folder (pwd). The new folder only 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.

Run Command: Create a New Folder and Copy Relevant Files

coderdemo_setup('coderdemo_intel_compiler');

About the 'coderrand' Function

The coderrand.mcoderrand.m function simply generates a random scalar value from the standard uniform distribution on the open interval (0,1).

type coderrand
function y = coderrand %#codegen

%   Copyright 2012 The MathWorks, Inc.

y = rand();

Toolchain Info

A toolchain is a collection of tools required to compile, link, download and run on 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.

edit intel_tc
tc = intel_tc
save intel_tc tc
tc = 

###########################################################################
# Toolchain Name: Intel v12.1 | nmake makefile (64-bit Windows)
# Supported Toolchain Version: 12.1
# Toolchain Specification Format Version: R2014a
# Toolchain Specification Revision: 1.0
###########################################################################

# Supported Host Platform = win64
# Supported Languages = C/C++

# ---------------
# Setup/Cleanup
# ---------------
MATLAB Setup: (none)
MATLAB Cleanup: (none)
Shell Setup: 
	call %ICPP_COMPILER12%\bin\compilervars.bat intel64
Shell Cleanup: (none)

# ------------
# Attributes
# ------------
RequiresBatchFile        = true
RequiresCommandFile      = true
TransformPathsWithSpaces = true


# ----------------------------------------------------------------------
# Macros intrinsic to the toolchain or assumed to be defined elsewhere
# ----------------------------------------------------------------------
# ldebug
# conflags
# cflags

# --------
# MACROS
# --------
MW_EXTERNLIB_DIR    = $(MATLAB_ROOT)\extern\lib\win64\microsoft
MW_LIB_DIR          = $(MATLAB_ROOT)\lib\win64
CFLAGS_ADDITIONAL   = -D_CRT_SECURE_NO_WARNINGS
CPPFLAGS_ADDITIONAL = -EHs -D_CRT_SECURE_NO_WARNINGS
LIBS_TOOLCHAIN      = $(conlibs)
CVARSFLAG           = 

###########################################################################
# Build Tool: Intel C Compiler
###########################################################################

Language              : 'C'
OptionsRegistry       : {'C Compiler', 'CFLAGS'}
InputFileExtensions   : {Source}
OutputFileExtensions  : {Object}
DerivedFileExtensions : {}
SupportedOutputs      : {*}
CommandPattern        : '|>TOOL<| |>TOOL_OPTIONS<| |>OUTPUT_FLAG<||>OUTPUT<|'

# ---------
# Command
# ---------
CC = icl
CC_PATH =

# ------------
# Directives
# ------------
Debug              = -Zi
FileSeparator      = \
Include            = 
IncludeSearchPath  = -I
OutputFlag         = -Fo
PreprocessorDefine = -D

# -----------------
# File Extensions
# -----------------
Header = .h
Object = .obj
Source = .c

###########################################################################
# Build Tool: Intel C/C++ Linker
###########################################################################

Language              : 'C'
OptionsRegistry       : {'Linker', 'LDFLAGS', 'Shared Library Linker', 'SHAREDLIB_LDFLAGS'}
InputFileExtensions   : {}
OutputFileExtensions  : {'Executable', 'Shared Library'}
DerivedFileExtensions : {}
SupportedOutputs      : {coder.make.enum.BuildOutput.EXECUTABLE, coder.make.enum.BuildOutput.SHARED_LIBRARY}
CommandPattern        : '|>TOOL<| |>TOOL_OPTIONS<| |>OUTPUT_FLAG<||>OUTPUT<|'

# ---------
# Command
# ---------
LD = xilink
LD_PATH =

# ------------
# Directives
# ------------
Debug             = 
FileSeparator     = \
Library           = -L
LibrarySearchPath = -I
OutputFlag        = -out:

# -----------------
# File Extensions
# -----------------
Executable     = .exe
Shared Library = .dll

###########################################################################
# Build Tool: Intel C++ Compiler
###########################################################################

Language              : 'C++'
OptionsRegistry       : {'C++ Compiler', 'CPPFLAGS'}
InputFileExtensions   : {Source}
OutputFileExtensions  : {Object}
DerivedFileExtensions : {}
SupportedOutputs      : {*}
CommandPattern        : '|>TOOL<| |>TOOL_OPTIONS<| |>OUTPUT_FLAG<||>OUTPUT<|'

# ---------
# Command
# ---------
CPP = icl
CPP_PATH =

# ------------
# Directives
# ------------
Debug              = -Zi
FileSeparator      = \
Include            = 
IncludeSearchPath  = -I
OutputFlag         = -Fo
PreprocessorDefine = -D

# -----------------
# File Extensions
# -----------------
Header = .hpp
Object = .obj
Source = .cpp

###########################################################################
# Build Tool: Intel C/C++ Linker
###########################################################################

Language              : 'C++'
OptionsRegistry       : {'C++ Linker', 'CPP_LDFLAGS', 'C++ Shared Library Linker', 'CPP_SHAREDLIB_LDFLAGS'}
InputFileExtensions   : {}
OutputFileExtensions  : {'Executable', 'Shared Library'}
DerivedFileExtensions : {}
SupportedOutputs      : {coder.make.enum.BuildOutput.EXECUTABLE, coder.make.enum.BuildOutput.SHARED_LIBRARY}
CommandPattern        : '|>TOOL<| |>TOOL_OPTIONS<| |>OUTPUT_FLAG<||>OUTPUT<|'

# ---------
# Command
# ---------
CPP_LD = xilink
CPP_LD_PATH =

# ------------
# Directives
# ------------
Debug             = 
FileSeparator     = \
Library           = -L
LibrarySearchPath = -I
OutputFlag        = -out:

# -----------------
# File Extensions
# -----------------
Executable     = .exe
Shared Library = .dll

###########################################################################
# Build Tool: Intel C/C++ Archiver
###########################################################################

Language              : 'C'
OptionsRegistry       : {'Archiver', 'ARFLAGS'}
InputFileExtensions   : {}
OutputFileExtensions  : {Static Library}
DerivedFileExtensions : {}
SupportedOutputs      : {coder.make.enum.BuildOutput.STATIC_LIBRARY}
CommandPattern        : '|>TOOL<| |>TOOL_OPTIONS<| |>OUTPUT_FLAG<||>OUTPUT<|'

# ---------
# Command
# ---------
AR = xilib
AR_PATH =

# ------------
# Directives
# ------------
Debug             = 
FileSeparator     = \
LibrarySearchPath = 
OutputFlag        = -out:

# -----------------
# File Extensions
# -----------------
Static Library = .lib

###########################################################################
# Build Tool: Download
###########################################################################

Language              : ''
OptionsRegistry       : {'Download', 'DOWNLOAD_FLAGS'}
InputFileExtensions   : {}
OutputFileExtensions  : {}
DerivedFileExtensions : {}
SupportedOutputs      : {coder.make.enum.BuildOutput.EXECUTABLE}
CommandPattern        : '|>TOOL<| |>TOOL_OPTIONS<|'

# ---------
# Command
# ---------
DOWNLOAD =
DOWNLOAD_PATH =

# ------------
# Directives
# ------------
(none)

# -----------------
# File Extensions
# -----------------
(none)

###########################################################################
# Build Tool: Execute
###########################################################################

Language              : ''
OptionsRegistry       : {'Execute', 'EXECUTE_FLAGS'}
InputFileExtensions   : {}
OutputFileExtensions  : {}
DerivedFileExtensions : {}
SupportedOutputs      : {coder.make.enum.BuildOutput.EXECUTABLE}
CommandPattern        : '|>TOOL<| |>TOOL_OPTIONS<|'

# ---------
# Command
# ---------
EXECUTE = $(PRODUCT)
EXECUTE_PATH =

# ------------
# Directives
# ------------
(none)

# -----------------
# File Extensions
# -----------------
(none)

###########################################################################
# Build Tool: NMAKE Utility
###########################################################################

Language              : ''
OptionsRegistry       : {'Make Tool', 'MAKE_FLAGS'}
InputFileExtensions   : {}
OutputFileExtensions  : {}
DerivedFileExtensions : {}
SupportedOutputs      : {*}
CommandPattern        : '|>TOOL<| |>TOOL_OPTIONS<|'

# ---------
# Command
# ---------
MAKE = nmake
MAKE_PATH =

# ------------
# Directives
# ------------
Comment                = #
DeleteCommand          = @del
DisplayCommand         = @echo
FileSeparator          = \
ImpliedFirstDependency = $<
ImpliedTarget          = $@
IncludeFile            = !include
LineContinuation       = \
MoveCommand            = @ren
ReferencePattern       = \$\($1\)
RunScriptCommand       = @cmd /C

# -----------------
# File Extensions
# -----------------
Makefile = .mk

###########################################################################
# Build Configuration : Faster Runs
# Description         : Minimize run time
###########################################################################

ARFLAGS              = /nologo 
CFLAGS               = $(cflags) $(CVARSFLAG) $(CFLAGS_ADDITIONAL) /c /O2 
CPPFLAGS             = $(cflags) $(CVARSFLAG) $(CPPFLAGS_ADDITIONAL) /c /O2 
CPP_LDFLAGS          = $(ldebug) $(conflags) $(LIBS_TOOLCHAIN) 
CPP_SHAREDLIB_LDFLAGS =
DOWNLOAD_FLAGS       =
EXECUTE_FLAGS        =
LDFLAGS              = $(ldebug) $(conflags) $(LIBS_TOOLCHAIN) 
MEX_CFLAGS           =
MEX_LDFLAGS          =
MAKE_FLAGS           = -f $(MAKEFILE) 
SHAREDLIB_LDFLAGS    = $(ldebug) $(conflags) $(LIBS_TOOLCHAIN) -dll -def:$(DEF_FILE) 

###########################################################################
# Build Configuration : Faster Builds
# Description         : Minimize compilation and linking time
###########################################################################

ARFLAGS              = /nologo 
CFLAGS               = $(cflags) $(CVARSFLAG) $(CFLAGS_ADDITIONAL) /c /Od 
CPPFLAGS             = $(cflags) $(CVARSFLAG) $(CPPFLAGS_ADDITIONAL) /c /Od 
CPP_LDFLAGS          = $(ldebug) $(conflags) $(LIBS_TOOLCHAIN) 
CPP_SHAREDLIB_LDFLAGS =
DOWNLOAD_FLAGS       =
EXECUTE_FLAGS        =
LDFLAGS              = $(ldebug) $(conflags) $(LIBS_TOOLCHAIN) 
MEX_CFLAGS           =
MEX_LDFLAGS          =
MAKE_FLAGS           = -f $(MAKEFILE) 
SHAREDLIB_LDFLAGS    = $(ldebug) $(conflags) $(LIBS_TOOLCHAIN) -dll -def:$(DEF_FILE) 

###########################################################################
# Build Configuration : Debug
# Description         : Build with debug information
###########################################################################

ARFLAGS              = /nologo $(ARDEBUG) 
CFLAGS               = $(cflags) $(CVARSFLAG) $(CFLAGS_ADDITIONAL) /c /Od $(CDEBUG) 
CPPFLAGS             = $(cflags) $(CVARSFLAG) $(CPPFLAGS_ADDITIONAL) /c /Od $(CPPDEBUG) 
CPP_LDFLAGS          = $(ldebug) $(conflags) $(LIBS_TOOLCHAIN) $(CPPLDDEBUG) 
CPP_SHAREDLIB_LDFLAGS =
DOWNLOAD_FLAGS       =
EXECUTE_FLAGS        =
LDFLAGS              = $(ldebug) $(conflags) $(LIBS_TOOLCHAIN) $(LDDEBUG) 
MEX_CFLAGS           =
MEX_LDFLAGS          =
MAKE_FLAGS           = -f $(MAKEFILE) 
SHAREDLIB_LDFLAGS    = $(ldebug) $(conflags) $(LIBS_TOOLCHAIN) -dll -def:$(DEF_FILE) $(LDDEBUG) 



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.

type rtwTargetInfo
function rtwTargetInfo(tr)
%RTWTARGETINFO Registration file for custom toolchains.

% Copyright 2012-2013 The MathWorks, Inc.

tr.registerTargetInfo(@loc_createToolchain);

end

% -------------------------------------------------------------------------
% Create the ToolchainInfoRegistry entries
% -------------------------------------------------------------------------
function config = loc_createToolchain

config(1)                       = coder.make.ToolchainInfoRegistry;
config(1).Name                  = 'Intel v12.1 | nmake makefile (64-bit Windows)';
config(1).FileName              = fullfile(fileparts(mfilename('fullpath')), 'intel_tc.mat');
config(1).TargetHWDeviceType    = {'*'};
config(1).Platform              =  {computer('arch')};

end

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 v12.1';

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

Remove files and return to original folder.

Run Command: Cleanup

cleanup

You can reset the TargetRegistry to remove the toolchain that you registered above.

RTW.TargetRegistry.getInstance('reset');