Main Content

Create Custom CMake Toolchain Definition

CMake is a third-party, open-source tool for build process management. To build code generated from MATLAB® code (when using MATLAB Coder™) or from Simulink® models (when using Simulink Coder™) , 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, and specify its Name, MakeToolType, Generator, and ToolchanFile to configure how CMake builds generated code.

tc = target.create("Toolchain", ...
                    Name="Example Custom CMake Toolchain", ...
                    MakeToolType="CMake", ...
                    Generator="Ninja", ...
                    ToolchainFile=fullfile(pwd, "ExampleCMakeToolchain.cmake"));

Specify the supported build types for this Toolchain object.

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")

For information about CMake toolchain files, see https://cmake.org/cmake/help/book/mastering-cmake/chapter/Cross%20Compiling%20With%20CMake.html.

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 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);
"target.add" summary:

    Objects added to internal database for current MATLAB session:
        target.Toolchain    "Example Custom CMake Toolchain"
    Objects not added because they already exist:
        target.Processor    "Intel-x86-64 (Linux 64)"
        target.Processor    "Intel-x86-64 (Mac OS X)"
        target.Processor    "Intel-x86-64 (Windows64)"

To use the custom toolchain definition for building generated code, in the Hardware pane (when using MATLAB Coder app) or in the Configuration Parameters dialog box of the model (when using Simulink Coder):

Select your target device:

  • If using MATLAB Coder - Select your target device by setting Hardware Board to None - Select Device Below, Device vendor to Intel, and Device type to x86-64 (Windows64).

  • If using Simulink Coder - On the Hardware Implementation pane, select your target device by setting Device vendor to Intel and Device type to x86-64 (Windows64).

On the Hardware pane (if using MATLAB Coder) or on the Code Generation pane (if using the Simulink Coder), select Example Custom CMake Toolchain from the Toolchain list. The Build configuration list contains these values:

  • Release

  • Debug

  • RelWithDebInfo

  • MinSizeRel

  • FastMath

  • FastMathWithDebug

  • Specify

Validate the report. If using MATLAB Coder, on the Hardware pane, click Validate. If using Simulink Coder, under Advanced parameters, click Validate Toolchain

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, enter:

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

(Simulink) | | | | | | | | |

Related Topics

External Websites