Contenu principal

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 five 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 model settings before code is generated.

  • beforeTLC: Prepare dependencies for code generation, such as outputting data or dynamically generated TLC files for custom inlined S-function blocks.

  • afterTLC: Post-process the generated code before the build stage.

  • 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.SimulinkCoderBuildHooks 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.SimulinkCoderBuildHooks and override some or all of its methods. Methods that you do not override default to empty implementations.

The MyTargetSimulinkCoderBuildHooks.m file implements a custom hook class MyTargetSimulinkCoderBuildHooks that overrides all five hook point methods.

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

        function beforeTLC(this, context)
            % beforeTLC operations
        end

        function afterTLC(this, context)
            % afterTLC 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.

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

Create a target object for the API implementation of the build hook, then retrieve the associated API.

slcHooks = target.create("APIImplementation", "Name", "Simulink Coder Build Hooks for My Board");
slcHooks.API = target.get("API", "Simulink Coder Build Hooks");

Create a target object for the MATLAB class MyTargetSimulinkCoderBuildHooks.

slcHooks.BuildDependencies = target.create("MATLABDependencies", "Classes", "MyTargetSimulinkCoderBuildHooks");

Associate the build hook with your target.

slcSupport = target.create('Support', 'Name', 'Simulink Coder Build Hooks for My Custom Target');
slcSupport.For = board1;
slcSupport.Requirements = slcHooks;

Register the build hook.

target.add([board1, slcHooks, slcSupport]);
target.add summary:

    Objects added to internal database for current MATLAB session:
        target.APIImplementation    "Simulink Coder Build Hooks for My Board"
        target.Board                "My Custom Target"
        target.Support              "Simulink Coder Build Hooks for My Custom Target"
    4 objects not added because they already exist.

Configure My Custom Target as the hardware board for your model. Open the Configuration Parameters dialog box for your model. Navigate to the Hardware Implementation pane. From the Hardware board list, select My Custom Target.

Get Information About Build Process Using Context Objects

You can access context objects to get information about the build process, and modify the RTW.BuildInfo object. Each hook point method has a corresponding context class and generates an instance of that class as its second argument. For example, the entry hook method generates a context object of the coder.buildhookcontext.Entry class.

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

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

context = 
  Entry with properties:

                ModelName: 'myModel'
       ComponentConfigSet: [1×1 Simulink.ConfigSet]
            HardwareBoard: [1×1 target.Board]
                Toolchain: [0×0 target.Toolchain]
            ToolchainName: 'MinGW64 | gmake (64-bit Windows)'
         GenerateCodeOnly: 0
    IsModelReferenceBuild: 0
     SystemTargetFileName: path/to/systemtargetfile
               IsPILBuild: 0
               IsSILBuild: 0

Continue program execution to the other hook points. At the next hook point beforeTLC, the ComponentBuildInfo property becomes available:

context = 
  BeforeTLC with properties:

             AnchorFolder: path/to/anchorfolder
              BuildFolder: path/to/buildfolder
       ComponentBuildInfo: [1×1 RTW.BuildInfo]
       IsNewGeneratedCode: 1
                ModelName: 'myModel'
       ComponentConfigSet: [1×1 Simulink.ConfigSet]
            HardwareBoard: [1×1 target.Board]
                Toolchain: [0×0 target.Toolchain]
            ToolchainName: 'MinGW64 | gmake (64-bit Windows)'
         GenerateCodeOnly: 0
    IsModelReferenceBuild: 0
     SystemTargetFileName: path/to/systemtargetfile
               IsPILBuild: 0
               IsSILBuild: 0

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