function varargout = checkMacros(h,varargin)
% --------------------------------------------------
% Process inputs
% --------------------------------------------------

%   Copyright 2014-2020 The MathWorks, Inc.

mode = 'factory';
if nargin>1
    if ischar( varargin{ 1 } )
        selectedConfig = varargin{ 1 };
        mode = 'selected';
    elseif iscell( varargin{ 1 } )
        customToolchainOptions = varargin{ 1 };
        mode = 'custom';
    end
end
% --------------------------------------------------
% Get all known and standard macros
% --------------------------------------------------
codeGenMacros = coder.make.Builder.getStandardMacros( h );
toolchainMacros = h.getAllMacros();
allDeclaredMacros = horzcat( toolchainMacros, codeGenMacros );
% --------------------------------------------------
% Extract all macros from the build configurations
% --------------------------------------------------
switch mode
    case 'custom'
        % -----------------------------
        % Extract from custom options
        % -----------------------------
        if isempty( customToolchainOptions )
            assert( false, [ 'You need to pass non-empty cell array of custom ', 'toolchain options to ''ToolchainInfo:checkMacros'' ', 'if the build configuration is ''Specify''.' ] );
        end
        for idx = 1:2:numel( customToolchainOptions )
            optionValue = customToolchainOptions{ idx + 1 };
            result = regexp( optionValue, 's*\$\(\w*\)s*', 'match' );
            if ~isempty( result )
                usedMacros = horzcat( usedMacros, result{ 1 } );  %#ok<AGROW>
            end
        end
    case { 'factory', 'selected' }
        % -----------------------------
        % Extract from factory options
        % -----------------------------
        if strcmpi( mode, 'factory' )
            selectedConfigs = h.getBuildConfigurations();
        else  % strcmpi(mode, 'selected')
            selectedConfigs = { selectedConfig };
        end
        usedMacros = {  };
        for i = 1:numel( selectedConfigs )
            bc = h.getBuildConfiguration( selectedConfigs{ i } );
            buildItems = bc.Options.values;
            for idx = 1:numel( buildItems )
                optionStr = buildItems{ idx }.getValue();
                result = regexp( optionStr, 's*\$\(\w*\)s*', 'match' );
                if ~isempty( result )
                    usedMacros = horzcat( usedMacros, result{ 1 } );  %#ok<AGROW>
                end
            end
        end
        usedMacros = regexprep( usedMacros, '\$\(|\)', '' );
end
% -----------------------------
% Locate any undeclared macros
% -----------------------------
undeclaredMacros = setdiff( unique( usedMacros ), allDeclaredMacros );
pass = isempty( undeclaredMacros );
msg = '';
if ~pass
    msgobj = message( 'coder_compile:toolchain:UndeclaredMacros', coder.make.internal.cell2str( undeclaredMacros, ',' ) );
    msg = msgobj.getString();
end
if nargout==0 && ~pass
    warning( msgobj )
end
if nargout>0
    varargout{ 1 } = true;
end
if nargout>1
    varargout{ 2 } = msg;
end
end
