function [C_STANDARD_OPTS,CPP_STANDARD_OPTS] = getStdCompilerOptions(toolChainInfo, buildInfo)
%

%   Copyright 2019-2020 The MathWorks, Inc.

codeReplacementLib = '';  % from TGT_FCN_LIB token
tmfTokens = get( buildInfo.Tokens, 'Key' );
tmfTokenVals = get( buildInfo.Tokens, 'Value' );
aIdx = find( strcmp( tmfTokens, '|>TGT_FCN_LIB<|' ) );
if ~isempty( aIdx )
    codeReplacementLib = tmfTokenVals{ aIdx };
end

switch codeReplacementLib
    case 'ISO_C'
        cStandard = 'C99 (ISO)';
        cppStandard = 'C99 (ISO)';
    case 'GNU'
        cStandard = 'GNU C99 extensions';
        cppStandard = 'GNU C99 extensions';
    case 'ISO_C++11'
        cStandard = 'C89/C90 (ANSI)';
        cppStandard = 'C++11 (ISO)';
    otherwise
        cStandard = 'C89/C90 (ANSI)';
        cppStandard = 'C++03 (ISO)';
end

C_STANDARD_OPTS = locGetOptsForStd(toolChainInfo, 'C', cStandard);
CPP_STANDARD_OPTS = locGetOptsForStd(toolChainInfo, 'C++', cppStandard);

C_STANDARD_OPTS = regexprep(C_STANDARD_OPTS, '\s+', ' ');
CPP_STANDARD_OPTS = regexprep(CPP_STANDARD_OPTS, '\s+', ' ');


end


function opts = locGetOptsForStd(tcInfo, language, std)
    
standards = {'C99 (ISO)', 'GNU C99 extensions', 'C89/C90 (ANSI)', ...
             'C++11 (ISO)', 'C++03 (ISO)'};

assert(any(strcmp(std, standards)), 'std, %s, must be one of allowed values', std)
       
% buildToolName is either 'C Compiler' or 'C++ Compiler'
buildToolName = [language ' Compiler'];

if ismember({buildToolName},tcInfo.getBuildTools)
    buildTool = tcInfo.getBuildTool(buildToolName);
    stdMaps = buildTool.SupportedStandard.getLangStandardMaps;
    cOptWildCard = stdMaps.getCompilerOptions('*');
    if isempty(cOptWildCard)
        opts = stdMaps.getCompilerOptions(std);
    else
        opts = cOptWildCard;
    end
else
    opts = '';
end

end
