Create Custom CMake Toolchain Definition
CMake is a third-party, open-source tool for build process management. To
build code generated from MATLAB® code, the software provides CMake toolchain definitions that you can
select from the Toolchain drop-down list. You can also create custom CMake
toolchain definitions by using the target
namespace. When you add a custom CMake toolchain definition to an internal
database, the toolchain is available for building generated code. This example shows how you can
create a CMake toolchain definition using the Ninja generator. You can use the toolchain
definition on computers with these operating systems:
Windows® (provided MATLAB Support for MinGW®-w64 C/C++ Compiler is installed).
Linux®
Mac
Create a target.Toolchain
object.
tc = target.create('Toolchain', 'Name', 'Example Custom CMake Toolchain');
Use the Builder
property and target.CMakeBuilder
object to configure how CMake builds generated code.
tc.Builder = target.create('CMakeBuilder'); tc.Builder.Generator = 'Ninja'; tc.Builder.ToolchainFile = fullfile(pwd, 'ExampleCMakeToolchain.cmake'); 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);
Using an editor, create ExampleCMakeToolchain.cmake
, a file that contains these lines.
# 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")
Use the first element of the EnvironmentConfiguration
property to provide system environment setup commands and paths for the Windows operating system.
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)'};
Use the second element of the EnvironmentConfiguration
property to provide system environment setup commands and paths for Linux and Mac operating systems.
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)'};
By default, the software uses the CMake executable file that ships with MATLAB. To specify another version of CMake, you can create a target.CMake
target.CMake
(Embedded Coder) object that
provides the required path.
Optionally, associate the toolchain definition with your target devices, for example:
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);
Add the toolchain definition to the internal database:
target.add(tc);
To use the custom toolchain definition for building generated code, in the MATLAB Coder™ app, on the Hardware pane:
Select your target device by setting Hardware Board to
None - Select Device Below
, Device vendor toIntel
and Device type tox86-64 (Windows64)
.From the Toolchain list, select
Example Custom CMake Toolchain
. The Build configuration list contains these values:Release
Debug
RelWithDebInfo
MinSizeRel
FastMath
FastMathWithDebug
Specify
Click Validate, which generates a validation report.
When you generate code, the build process uses the custom toolchain to build generated code.
If you want to remove the custom toolchain definition from the internal database, run:
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"
See Also
target.create
| target.Toolchain
| target.CMake
| target.CMakeBuilder
| target.CMakeBuildType
| target.CMakeCacheEntry
| target.EnvironmentConfiguration
| target.HardwareComponentSupport