Main Content

coder.BuildConfig Class

Namespace: coder

Build context during code generation

Description

The code generator creates an object of this class to facilitate access to the build context. The build context encapsulates the settings used by the code generator including:

  • Target language

  • Code generation target

  • Target hardware

  • Build toolchain

Use coder.BuildConfig methods in the methods that you write for the coder.ExternalDependency class.

Class Attributes

Abstract
true

For information on class attributes, see Class Attributes.

Creation

The code generator creates objects of this class.

Methods

expand all

Examples

collapse all

This example shows how to use coder.BuildConfig methods to access the build context in coder.ExternalDependency methods. In this example, you use:

  • coder.BuildConfig.isMatlabHostTarget to verify that the code generation target is the MATLAB® host. If the host is not MATLAB report an error.

  • coder.BuildConfig.getStdLibInfo to get the link-time and run-time library file extensions. Use this information to update the build information.

Write a class definition file for an external library that contains the function adder.

%================================================================
% This class abstracts the API to an external Adder library.
% It implements static methods for updating the build information
% at compile time and build time.
%================================================================

classdef AdderAPI < coder.ExternalDependency
    %#codegen
    
    methods (Static)
        
        function bName = getDescriptiveName(~)
            bName = 'AdderAPI';
        end
        
        function tf = isSupportedContext(buildContext)
            if  buildContext.isMatlabHostTarget()
                tf = true;
            else
                error('adder library not available for this target');
            end
        end
        
        function updateBuildInfo(buildInfo, buildContext)
            % Get file extensions for the current platform
            [~, linkLibExt, execLibExt, ~] = buildContext.getStdLibInfo();
            
            % Add file paths
            hdrFilePath = fullfile(pwd, 'codegen', 'dll', 'adder');
            buildInfo.addIncludePaths(hdrFilePath);

            % Link files
            linkFiles = strcat('adder', linkLibExt);
            linkPath = hdrFilePath;
            linkPriority = '';
            linkPrecompiled = true;
            linkLinkOnly = true;
            group = '';
            buildInfo.addLinkObjects(linkFiles, linkPath, ...
                linkPriority, linkPrecompiled, linkLinkOnly, group);

            % Non-build files for packaging
            nbFiles = 'adder';
            nbFiles = strcat(nbFiles, execLibExt);
            buildInfo.addNonBuildFiles(nbFiles,'','');
        end
        
        %API for library function 'adder'
        function c = adder(a, b)
            if coder.target('MATLAB')
                % running in MATLAB, use built-in addition
                c = a + b;
            else
                % Add the required include statements to the generated function code
                coder.cinclude('adder.h');
                coder.cinclude('adder_initialize.h');
                coder.cinclude('adder_terminate.h');
                c = 0;
                
                % Because MATLAB Coder generated adder, use the
                % housekeeping functions before and after calling
                % adder with coder.ceval.

                coder.ceval('adder_initialize');
                c = coder.ceval('adder', a, b);
                coder.ceval('adder_terminate');
            end
        end
    end
end

Version History

Introduced in R2013b