Main Content

setDirective

Class: target.BuildTool
Namespace: target

Set directive values

Since R2023a

Syntax

myBuildTool.setDirective(directiveName,directiveValue)
myBuildTool.setDirective(pairedDirectiveName,startValue,endValue)

Description

myBuildTool.setDirective(directiveName,directiveValue) sets the named directive to the specified value.

myBuildTool.setDirective(pairedDirectiveName,startValue,endValue) sets the start and end values of the named directive.

If the Directives property does not contain the directive, the method appends a new object of the appropriate subclass of target.BaseDirective.

If the directive name is not supported by the build tool type specified by the target.BuildToolType object, the method produces an error.

Input Arguments

expand all

Name of directive.

Example: cCompiler.setDirective('CommandFile', '@');

New value for named directive.

Example: cCompiler.setDirective('CommandFile', '@');

Name of directive that contains a pair of command-line flags.

Example: cLinker.setDirective('LibraryGroup', '-Wl,--start-group', '-Wl,--end-group');

New start value for paired directive.

Example: cLinker.setDirective('LibraryGroup', '-Wl,--start-group', '-Wl,--end-group');

New end value for paired directive.

Example: cLinker.setDirective('LibraryGroup', '-Wl,--start-group', '-Wl,--end-group');

Examples

expand all

Create a target.Toolchain object and specify the operating system.

mingwtc = target.create("Toolchain",Name="Example MinGW Toolchain", ...
      HostOperatingSystemSupport=target.HostOperatingSystemSupport.WindowsOnly);

Place the binary files folder of the MinGW® toolchain on the system search path.

mingwtc.EnvironmentConfiguration.SystemPaths{end+1} = "$(MW_MINGW64_LOC)/bin";

Associate the toolchain with the target hardware, which is your Windows® development computer.

mingwtc.SupportedHardware = target.create("HardwareComponentSupport", ...
      Component=target.get("Processor","Intel-x86-64 (Windows64)"));

Specify that the toolchain is based on a GNU Make makefile.

mingwtc.Builder = target.create("MakefileBuilder","GMake");

Specify the assembler.

assembler = target.create("BuildTool",Assembler="as", ...
      Name="Example GNU Assembler"); 
mingwtc.Tools(end+1) = assembler;

For the Windows operating system, specify C and C++ compilers that use command files and generate object files with the .obj extension.

cCompiler = target.create("BuildTool","C Compiler","gcc", ...
                          Name="MinGW GCC C Compiler");
cCompiler.setDirective("CommandFile","@");
cCompiler.setFileExtensions("Object",".obj");
mingwtc.Tools(end+1) = cCompiler;

cppCompiler = target.create("BuildTool","C++ Compiler","g++", ...
                            Name="MinGW GCC C++ Compiler");
cppCompiler.setDirective("CommandFile","@");
cppCompiler.setFileExtensions("Object",".obj");
mingwtc.Tools(end+1) = cppCompiler;

Through directives and file extension settings, specify C and C++ linkers that:

  • Support the generation of dynamic link libraries (DLLs)

  • Group libraries that have circular dependencies

  • Use command files

  • Create files for Windows

cLinker = target.create("BuildTool",Linker="gcc", ...
      Name="MinGW Linker", ...
      HostOperatingSystemSupport=target.HostOperatingSystemSupport.WindowsOnly);

cLinker.setDirective("Shared", ...
      "-shared -Wl,--out-implib,$(notdir $(basename $(PRODUCT))).lib");

cLinker.setDirective("LibraryGroup","-Wl,--start-group","-Wl,--end-group");
cLinker.setDirective("CommandFile","@");
cLinker.setFileExtensions("Object",".obj");
cLinker.setFileExtensions("Executable",".exe");
cLinker.setFileExtensions("Shared Library",".dll");
mingwtc.Tools(end+1) = cLinker;

cppLinker = target.create("BuildTool",Copy=cLinker, ...
    Name="MinGW C++ Linker", ...
    BuildToolType=target.get("BuildToolType","C++ Linker"), ...
    Command=target.create("Command","g++"));
mingwtc.Tools(end+1) = cppLinker;

In this example, you create the cppLinker object by creating a copy of cLinker and modifying properties of the copy.

Specify an archiver.

archiver = target.create("BuildTool",Archiver="ar ruvs", ...
      Name="Example GNU Archiver");
archiver.setFileExtensions("Object",".obj");
mingwtc.Tools(end+1) = archiver;

Specify a make tool.

maketool = target.create('BuildTool', 'Make Tool', 'mingw32-make -j$(MAX_MAKE_JOBS) -l$(MAX_MAKE_LOAD_AVG)', ...
    'Name', 'MinGW GNU Make', ...
    'HostOperatingSystemSupport', target.HostOperatingSystemSupport.WindowsOnly);
mingwtc.Tools(end+1) = maketool;

The -j flag enables parallel processing of the make command. The $(MAX_MAKE_JOBS) token expands to the minimum of these values:

  • Number of cores on your system

  • maxNumCompThreads, which avoids system overload when you have explicitly used maxNumCompThreads to limit utilization of resources.

  • 0.5 * system RAM size in GiB (rounded upwards to the nearest integer), which avoids memory oversubscription and reduced performance due to swap file usage.

  • 16

If the make command is run on a parallel build worker, the token expands to 1. This value prevents the number of spawned external processes from growing quadratically with respect to the number of system cores when parallelism is implemented at a higher level of the workflow.

If you want to always use the number of cores on your system, specify the $(NUM_CORES) token instead of $(MAX_MAKE_JOBS).

The -l flag prevents system overloading. The flag limits, based on the system load, the spawning of parallel make processes. The $(MAX_MAKE_LOAD_AVG) token expands to the number of cores on your system.

Specify basic system tools like echo, del, and move, which the makefile uses to display, delete, and move files. The predefined target.Toolset object contains the tool definitions.

basictools = target.get("Toolset","Windows system tools for Makefiles");
mingwtc.Tools(end+1) = basictools;

Specify standard dependencies:

  • C math and Winsock libraries to link with the compiled generated code

  • Compiler flags that are always passed to the C and C++ compilers

mingwtc.BuildRequirements.SharedLibraries{end+1} = "m";
mingwtc.BuildRequirements.SharedLibraries{end+1} = "ws2_32";

mingwtc.BuildRequirements.CompilerFlags{end+1} = "-fwrapv";
mingwtc.BuildRequirements.CompilerFlags{end+1} = "-fPIC";

mingwtc.BuildRequirements.LinkerFlags{end+1} = "-static";
mingwtc.BuildRequirements.LinkerFlags{end+1} = "-m64";

Add the toolchain definition to the internal database.

target.add(mingwtc);
target.add summary:

    Objects added to internal database for current MATLAB session:
        target.Toolchain       "Example MinGW Toolchain"
    10 objects not added because they already exist.

To use the custom toolchain definition for building generated code, in the Configuration Parameters dialog box:

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

  2. On the Code Generation pane, from the Toolchain list, select Example MinGW Toolchain.

  3. Click OK.

When you run, for example, the slbuild function or a software-in-the-loop (SIL) simulation, 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 MinGW Toolchain");
target.remove(customToolChainDef);
target.remove summary:

    Objects removed from internal database:
        target.Toolchain    "Example MinGW Toolchain"

Version History

Introduced in R2023a