Main Content

Build Code in Windows Subsystem for Linux

If you install Windows Subsystem for Linux (WSL) on your Windows® computer, you can use CMake toolchain definitions to build generated code in the WSL environment. For example, you can:

  • Cross-compile generated code for a Linux® computer.

  • Use Linux-supported toolchains to build generated code for your target hardware.

  • Use the Embedded Coder® Support Package for Linux Applications to build AUTOSAR Adaptive or DDS Blockset code.

  • Cross-compile generated code from a Windows protected model for a Linux computer.

For the build process, you can specify:

  • The predefined toolchain GNU gcc/g++ | CMake/gmake (Windows Subsystem for Linux)

  • Custom toolchain definitions that you create by using the target namespace

Set Up Environment

Before you can build generated code, you must follow these steps:

  1. On your Windows computer, install Windows Subsystem for Linux (WSL).

  2. In the WSL environment, install a Linux distribution, for example, Debian®.

  3. In the Linux distribution, install CMake and the required build tools, for example, GNU® Make and GCC and G++ compilers.

  4. Check that you can access the code generation and MATLAB® installation folders on your Windows file system from within the WSL environment. You can provide access to the folders by using the DrvFs file system plugin to create a mount point. For more information, see Mount a Linux disk in WSL 2.

    For example, if both folders are on the C: drive, check that:

    • /mnt/c in the WSL file system points to the C: drive on your Windows file system.

    • The mount point has read/write access.

    • The default user within the WSL environment owns the mount point.

Specify Predefined Toolchain Definition in WSL

Before building generated code, in the Configuration Parameters dialog box:

  1. Set Device vendor to Intel and Device type to x86-64 (Linux 64).

  2. Set Toolchain to GNU gcc/g++ | CMake/gmake (Windows Subsystem for Linux).

When you build your model (Ctrl+B), the build process uses the specified toolchain to build generated code.

Create Custom Toolchain Definition for WSL

This command-line example assumes that:

  • The Debian Linux distribution is installed in the WSL environment.

  • CMake, the Ninja build system, and AArch64 cross-compilers are installed in the Debian Linux distribution.

Define a CMake-based toolchain that runs in WSL.

aarchTc = target.create('Toolchain', ...
    'Name', 'GNU gcc/g++ aarch64 | CMake/Ninja', ...
    'MakeToolType', 'CMake', ...
    'Generator', 'Ninja', ...
    'RunsOn', target.ExecutionContext.WSL);
aarchTc.HostOperatingSystemSupport = target.HostOperatingSystemSupport.WindowsOnly;

Customize the toolchain to use the cross-compiler.

aarchTc.Builder.CommandLineCacheEntries(end+1) = target.create('CMakeCacheEntry', ...
              'Name', 'CMAKE_C_COMPILER', ...
              'Value', 'aarch64-linux-gnu-gcc');
aarchTc.Builder.CommandLineCacheEntries(end+1) = target.create('CMakeCacheEntry', ...
              'Name', 'CMAKE_CXX_COMPILER', ...
              'Value', 'aarch64-linux-gnu-g++');
aarchTc.Builder.CommandLineCacheEntries(end+1) = target.create('CMakeCacheEntry', ...
              'Name', 'CMAKE_SYSTEM_NAME', ...
              'Value', 'Linux');
aarchTc.Builder.CommandLineCacheEntries(end+1) = target.create('CMakeCacheEntry', ...
              'Name', 'CMAKE_FIND_ROOT_PATH', ...
              'Value', '/usr/aarch64-linux-gnu');
aarchTc.Builder.CommandLineCacheEntries(end+1) = target.create('CMakeCacheEntry', ...
              'Name', 'CMAKE_FIND_ROOT_PATH_MODE_PROGRAM', ...
              'Value', 'NEVER');
aarchTc.Builder.CommandLineCacheEntries(end+1) = target.create('CMakeCacheEntry', ...
              'Name', 'CMAKE_FIND_ROOT_PATH_MODE_LIBRARY', ...
              'Value', 'ONLY');
aarchTc.Builder.CommandLineCacheEntries(end+1) = target.create('CMakeCacheEntry', ...
              'Name', 'CMAKE_FIND_ROOT_PATH_MODE_INCLUDE', ...
              'Value', 'ONLY');

Add the toolchain definition to the internal database.

target.add(aarchTc)

In the model configuration, specify the toolchain definition.

modelName = 'myModel';
load_system(modelName);
set_param(modelName, ...
          'ProdHWDeviceType', 'ARM Compatible->ARM 64-bit (LP64)');
set_param(modelName, ...
          'Toolchain', aarchTc.Name);

When you build the model, for example, by running slbuild(modelName), the build process uses the custom toolchain to build the generated code.

If you want to remove the custom toolchain definition from the internal database, enter these commands.

customToolChainDef = target.get('Toolchain', ...
                                'GNU gcc/g++ aarch64 | CMake/Ninja');
target.remove(customToolChainDef);

See Also

| | | | | | |

Topics

External Websites