function out = isToolchain(obj, toolchain)
%ISTOOLCHAIN Determine if toolchain used is the specified one.

% NOTE: This is not a novel way of determining the toolchain because it
% relies on string comparison. However, since there are a number of
% callsites that require this logic, it is better to have (this) one central
% utility.

% Copyright 2013-2020 The MathWorks, Inc.

% Get the alias
if isa(obj, 'RTW.BuildInfo')
    tcAlias = obj.getBuildToolInfo('Alias');
elseif isa(obj, 'coder.make.ToolchainInfo')
    tcAlias = obj.Alias;
end

% Parse the alias for desired string or substring
switch upper(toolchain)
    case 'LCC'
        out = any(strcmpi(tcAlias, 'LCC-x'));
        
    case 'MSVC'
        out = false;
        for i=1:numel(tcAlias)
            if strncmp(tcAlias{i}, 'Microsoft-', numel('Microsoft-'))
                out = true;
                break;
            end
        end
        
    case 'INTEL'
        out = false;
        for i=1:numel(tcAlias)
            if strncmp(tcAlias{i}, 'INTELC', numel('INTELC'))
                out = true;
                break;
            end
        end
        
    case 'GCC' %for GCC, use toolchain attributes to decide because custom toolchains may not have alias.
        % Get the toolchainInfo
        if isa(obj, 'RTW.BuildInfo')
            tc = obj.getBuildToolInfo('ToolchainInfo');
        elseif isa(obj, 'coder.make.ToolchainInfo')
            tc = obj;
        end
        % Check the attribute
        if tc.isAttribute('GCCBased')
            out = true;
        else
            out = false;
        end     
        
    otherwise
        assert(false, ['Utility (' mfilename ') cannot be used determine if the toolchain is ''' toolchain '''.']);
end

% LocalWords:  toolchain TMF INTELC
