主要内容

Customize Build Process for Target Hardware Using Target Framework

R2026b

This example shows how to supply optional custom code in hook methods of a custom MATLAB® class to execute at specific points in the code generation and make process. You can then register these hook methods for your specific hardware using the Target Framework, and access build information via context objects generated by the hook methods.

You can implement custom build operations at any of these three build hook points. Each hook point method is executed during a distinct phase of the build process.

  • entry: Initialize the build process, for example, to change or validate MATLAB code settings before code is generated.

  • afterCodeGeneration: Prepare dependencies for compilation and linking, such as include paths, preprocessor directives, or custom compiler or linker flags.

  • afterMake: Post-process the compiled executable.

Implement Custom Build Hook Class

The coder.MATLABCoderBuildHooks base class provides an interface with methods corresponding to each build hook point. To implement your custom build hook class, derive your class from coder.MATLABCoderBuildHooks and override some or all of its methods. Methods that you do not override default to empty implementations.

The MyTargetMATLABCoderBuildHooks.m file implements a custom hook class MyTargetMATLABCoderBuildHooks that overrides all three hook point methods.

classdef MyTargetMATLABCoderBuildHooks < coder.MATLABCoderBuildHooks
    methods
        function entry(this, context)
            % Entry operations
        end

        function afterCodeGeneration(this, context)
            % afterCodeGeneration operations
        end

        function afterMake(this, context)
            % afterMake operations
        end
    end
end

To implement operations when the build process exits or errors, define a custom destructor for your build hook class. For details, see Handle Class Destructor.

Register Build Hooks with Target Hardware

To register your build hooks to target specific hardware, use the target API. For more information, see Register New Hardware Devices.

For example, on Windows®, create a target object for the custom board My Custom Target.

board = target.create("Board", ...
    "Name", "My Custom Target", ...
    "Processors", target.get("Processor", "Intel-x86-64 (Windows64)"));
target.add(board, "SuppressOutput", true);

Create a support object associating the custom board with the build hooks.

support = target.create("Support", "Name", "MATLAB Coder Build Hooks for My Custom Target");

Associate the hook class MyTargetMATLABCoderBuildHooks with your target board.

support.For = board;
support.Requirements = target.create("APIImplementation", ...
    "Name", "MATLAB Coder Build Hooks for My Custom Target", ...
    "API", target.get("API", "MATLAB Coder Build Hooks"), ...
    "BuildDependencies", target.create("MATLABDependencies", ...
    "Classes", "MyTargetMATLABCoderBuildHooks"));
target.add(support, "SuppressOutput", true);

Configure the code generator for the selected hardware.

cfg = coder.config("exe");
cfg.Hardware = coder.hardware(board.Name);

Get Information About Build Process Using Context Objects

You can access context objects of the coder.buildhookcontext.MATLABCoder class to get information about the build process and modify the RTW.BuildInfo object.

In MyTargetMATLABCoderBuildHooks.m, place breakpoints at the end of each hook point method. Then build your MATLAB code. The program pauses execution at the end of the entry hook point.

Examine the context object for the entry hook point. For instance:

context = 
  MATLABCoder with properties:

       Configuration: [1×1 coder.EmbeddedCodeConfig]
           BuildInfo: [1×1 RTW.BuildInfo]
    GenerateCodeOnly: 0
          IsPILBuild: 0
          IsSILBuild: 0
       HardwareBoard: [1×1 target.Board]
           Toolchain: [0×0 target.Toolchain]
       ToolchainName: 'MinGW64 | gmake (64-bit Windows)'

Continue program execution to the other hook points. At the next hook point afterCodeGeneration, the ComponentBuildInfo property becomes available. This property returns a handle to the RTW.BuildInfo object for the build, enabling you to modify the build process via the context object.

See Also

|

Topics