このページの内容は最新ではありません。最新版の英語を参照するには、ここをクリックします。
カスタムの CMake ツールチェーン定義の作成
CMake は、サードパーティが提供するオープンソースのビルド プロセス管理ツールです。MATLAB® コード (MATLAB Coder™ を使用する場合) または Simulink® モデル (Simulink Coder™ を使用する場合) から生成されたコードをビルドするために、このソフトウェアには、[ツールチェーン] ドロップダウン リストから選択できる CMake ツールチェーン定義が用意されています。target 名前空間を使用して、カスタムの CMake ツールチェーン定義を作成することもできます。カスタムの CMake ツールチェーン定義を内部データベースに追加すると、そのツールチェーンを使用して生成コードをビルドできるようになります。この例では、Ninja ジェネレーターを使用して CMake ツールチェーン定義を作成する方法を示します。ツールチェーン定義は、以下のオペレーティング システムのコンピューターで使用できます。
Windows® (MATLAB Support for MinGW®-w64 C/C++ Compiler がインストールされている場合)。
Linux®
Mac
target.Toolchain オブジェクトを作成し、その Name、MakeToolType、Generator、および ToolchanFile を指定して、CMake が生成されたコードをビルドする方法を構成します。
tc = target.create("Toolchain", ... Name="Example Custom CMake Toolchain", ... MakeToolType="CMake", ... Generator="Ninja", ... ToolchainFile=fullfile(pwd, "ExampleCMakeToolchain.cmake"));
この Toolchain オブジェクトに対してサポートされているビルド タイプを指定します。
tc.Builder.SupportedBuildTypes(end+1) = target.create("CMakeBuildType", ... Name="FastMath", ... GeneratesDebugSymbols=false, ... DebugBuildType="FastMathWithDebug"); tc.Builder.SupportedBuildTypes(end+1) = target.create("CMakeBuildType", ... Name="FastMathWithDebug", ... GeneratesDebugSymbols=true);
エディターを使用して、以下の行が含まれたファイル ExampleCMakeToolchain.cmake を作成します。
# Example CMake toolchain
# Configure to use GCC
if(WIN32)
list(APPEND CMAKE_PROGRAM_PATH "$ENV{MW_MINGW64_LOC}")
endif()
set(CMAKE_C_COMPILER "gcc")
set(CMAKE_CXX_COMPILER "g++")
# Add some custom build configurations: FastMath, and FastMathWithDebug
set(CMAKE_C_FLAGS_FASTMATH "-Ofast")
set(CMAKE_CXX_FLAGS_FASTMATH "-Ofast")
set(CMAKE_C_FLAGS_FASTMATHWITHDEBUG "-Ofast -g")
set(CMAKE_CXX_FLAGS_FASTMATHWITHDEBUG "-Ofast -g")
CMake ツールチェーン ファイルについては、https://cmake.org/cmake/help/book/mastering-cmake/chapter/Cross%20Compiling%20With%20CMake.html を参照してください。
EnvironmentConfiguration プロパティの 1 番目の要素を使用して、Windows オペレーティング システム用のシステム環境設定コマンドおよびパスを指定します。
tc.EnvironmentConfiguration(1).HostOperatingSystemSupport.Linux = false; tc.EnvironmentConfiguration(1).HostOperatingSystemSupport.Windows = true; tc.EnvironmentConfiguration(1).HostOperatingSystemSupport.Mac = false; tc.EnvironmentConfiguration(1).SystemPaths = ... {'$(MW_MINGW64_LOC)/bin','$(MATLAB_ROOT)/toolbox/shared/coder/ninja/$(ARCH)'};
EnvironmentConfiguration プロパティの 2 番目の要素を使用して、Linux および Mac オペレーティング システム用のシステム環境設定コマンドおよびパスを指定します。
tc.EnvironmentConfiguration(2) = target.create("EnvironmentConfiguration"); tc.EnvironmentConfiguration(2).HostOperatingSystemSupport.Linux = true; tc.EnvironmentConfiguration(2).HostOperatingSystemSupport.Windows = false; tc.EnvironmentConfiguration(2).HostOperatingSystemSupport.Mac = true; tc.EnvironmentConfiguration(2).SystemPaths = ... {'$(MATLAB_ROOT)/toolbox/shared/coder/ninja/$(ARCH)'};
既定では、このソフトウェアは MATLAB に付属の CMake 実行可能ファイルを使用します。別のバージョンの CMake を指定するために、必要なパスを提供する target.CMake オブジェクトを作成できます。
オプションで、ツールチェーン定義をターゲット デバイスに関連付けます。たとえば、以下のようにします。
windowsProc = target.get("Processor","Intel-x86-64 (Windows64)"); tc.SupportedHardware(end+1) = target.create("HardwareComponentSupport", ... Component=windowsProc); linuxProc = target.get("Processor","Intel-x86-64 (Linux 64)"); tc.SupportedHardware(end+1) = target.create("HardwareComponentSupport", ... Component=linuxProc); macProc = target.get("Processor","Intel-x86-64 (Mac OS X)"); tc.SupportedHardware(end+1) = target.create("HardwareComponentSupport", ... Component=macProc);
ツールチェーン定義を内部データベースに追加します。
target.add(tc);
target.add summary:
Objects added to internal database for current MATLAB session:
target.Toolchain "Example Custom CMake Toolchain"
3 objects not added because they already exist.
生成コードをビルドするためにカスタムのツールチェーン定義を使用するには、[ハードウェア] ペイン (MATLAB Coder アプリを使用する場合)、またはモデルの [コンフィギュレーション パラメーター] ダイアログ ボックス (Simulink Coder を使用する場合) で以下のようにします。
ターゲット デバイスを選択します。
MATLAB Coder を使用している場合 - [ハードウェア ボード] を
None - Select Device Below、[デバイス ベンダー] をIntel、[デバイス タイプ] をx86-64 (Windows64)に設定して、ターゲット デバイスを選択します。Simulink Coder を使用している場合 - [ハードウェア実行] ペインで [デバイス ベンダー] を
Intel、[デバイス タイプ] をx86-64 (Windows64)に設定してターゲット デバイスを選択します。
[ハードウェア] ペイン (MATLAB Coder を使用している場合) または [コード生成] ペイン (Simulink Coder を使用している場合) で、[ツールチェーン] リストから Example Custom CMake Toolchain を選択します。[ビルド構成] リストには以下の値が含まれています。
ReleaseDebugRelWithDebInfoMinSizeRelFastMathFastMathWithDebugSpecify
レポートを検証します。MATLAB Coder を使用している場合は、[ハードウェア] ペインで [検証] をクリックします。Simulink Coder を使用している場合は、[詳細設定パラメーター] で [ツールチェーンの検証] をクリックします。

コードを生成すると、ビルド プロセスはカスタムのツールチェーンを使用して生成コードをビルドします。
カスタムのツールチェーン定義を内部データベースから削除する場合は、以下を入力します。
customToolChainDef = target.get("Toolchain","Example Custom CMake Toolchain"); target.remove(customToolChainDef);
target.remove summary:
Objects removed from internal database:
target.Toolchain "Example Custom CMake Toolchain"
参考
slbuild | target | target.create | target.Toolchain | target.CMake | target.CMakeBuilder | target.CMakeBuildType | target.CMakeCacheEntry | target.EnvironmentConfiguration | target.HardwareComponentSupport