Main Content

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 thecoderrandFunction

In this example, you generate code for thecoderrandfunction. This MATLAB® function simply generates a random scalar value from the standard uniform distribution on the open interval (0,1).

typecoderrand
function y = coderrand %#codegen % Copyright 2012 The MathWorks, Inc. y = rand();

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 fileintel_tc.m.

tc = intel_tc
tc = ####################################################### # Toolchain Name: Intel v14 | nmake makefile (64-bit Windows) # Supported Toolchain Version: 14 # Toolchain Specification Format Version: 2021b # 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_COMPILER14%\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 # ------------ CompileFlag = Debug = -Zi ErrorPattern = FileNamePattern = FileSeparator = \ Include = IncludeSearchPath = -I LineNumberPattern = OutputFlag = -Fo PreprocessFile = PreprocessorDefine = -D WarningPattern = # ----------------- # 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 LibrarySearchPathRuntime = 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 # ------------ CompileFlag = Debug = -Zi ErrorPattern = FileNamePattern = FileSeparator = \ Include = IncludeSearchPath = -I LineNumberPattern = OutputFlag = -Fo PreprocessFile = PreprocessorDefine = -D WarningPattern = # ----------------- # 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 LibrarySearchPathRuntime = 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_CPPFLAGS = MEX_CPPLDFLAGS = 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_CPPFLAGS = MEX_CPPLDFLAGS = 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_CPPFLAGS = MEX_CPPLDFLAGS = MEX_CFLAGS = MEX_LDFLAGS = MAKE_FLAGS = -f $(MAKEFILE) SHAREDLIB_LDFLAGS = $(ldebug) $(conflags) $(LIBS_TOOLCHAIN) -dll -def:$(DEF_FILE) $(LDDEBUG)
saveintel_tctc

Registering a Toolchain

Toolchains are registered throughRTW.TargetRegistry. To register the toolchain, you can also usertwTargetInfowhich will be loaded by the system automatically.

copyfilemyRtwTargetInfoCustom.txtrtwTargetInfo.mtypertwTargetInfo
function rtwTargetInfo(tr) %RTWTARGETINFO Registration file for custom toolchains. % Copyright 2012-2016 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 v14 | 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 theTargetRegistryto pick up the newrtwTargetInfo.

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 thecodegen生成代码和makefile that uses the new toolchain.

codegen-configcfgcoderrand
Code generation successful.

Once thecodegenis finished, and you had Intel compilers installed, you can usesystem('coderrand.exe')to run the executable.

Cleanup

You can reset theTargetRegistry删除上面的工具链,你注册。

delete./rtwTargetInfo.mRTW.TargetRegistry.getInstance('reset');