Main Content

このページの内容は最新ではありません。最新版の英語を参照するには、ここをクリックします。

MATLAB® Coder™ ビルド プロセスへのカスタム ツールチェーンの追加

この例では、ツールチェーンを登録および使用して実行可能ファイルをコンパイルする方法を説明します。この例では Intel® Compiler を使用しますが、以下に示す概念と API はすべてのツールチェーンで使用できます。登録されたツールチェーンはツールチェーンのリストから選択でき、そのツールチェーンを使用して makefile が生成され、コードがビルドされます。

関数 coderrand について

この例では、関数 coderrand のコードを生成します。この MATLAB® 関数は、開区間 (0,1) 上の標準一様分布からランダムなスカラー値を単純に生成します。

type coderrand
function y = coderrand %#codegen

%   Copyright 2012 The MathWorks, Inc.

y = rand();

ツールチェーンについて

ツールチェーンとは、特定のプラットフォーム向けに生成されたコードのコンパイルおよびリンクを行うのに必要なツールの集合です。ツールチェーンは、コンパイラ、リンカー、アーカイバーなどの複数のツールを含みます。これらの各ツールには複数のオプションがある場合もあり、高速なビルド、高速に実行、デバッグなどの構成にグループ化できます。ツールチェーン オブジェクトはツールチェーンの基本情報を示します。ツールチェーン オブジェクトには前記のすべてを記述する手段が用意されています。このオブジェクトは MATLAB ファイルに保存され、インストール間で共有できます。

次の例では、ツールチェーン定義ファイル intel_tc.m を使用します。

tc = intel_tc
tc = 
###########################################################################
# Toolchain Name: Intel v14 | nmake makefile (64-bit Windows)
# Supported Toolchain Version: 14
# Toolchain Specification Format Version: 2023b
# 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) 



save intel_tc tc

ツールチェーンの登録

ツールチェーンは RTW.TargetRegistry を使用して登録されます。システムによって自動的に読み込まれる rtwTargetInfo を使用してツールチェーンを登録することもできます。

copyfile myRtwTargetInfoCustom.txt rtwTargetInfo.m
type rtwTargetInfo
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

TargetRegistry をリセットすると、新規の rtwTargetInfo を取得できます。

RTW.TargetRegistry.getInstance('reset');

ツールチェーンの選択

config オブジェクトを作成し、新規のツールチェーンを使用して実行可能ファイルを作成するよう構成できます。

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

Intel Compiler をインストールしていない場合は、以下のコマンドを使用してコードおよび makefile のみを生成できます。

cfg.GenCodeOnly = true;

codegen を実行して、新規のツールチェーンを使用するコードおよび makefile を生成します。

codegen -config cfg coderrand 
Code generation successful.

codegen の完了後、Intel Compiler をインストールしている場合には、system('coderrand.exe') を使用して実行可能ファイルを実行できます。

クリーンアップ

TargetRegistry をリセットすると、前記で登録したツールチェーンを削除できます。

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