function [tcName, tcAlias] = getDefaultToolchain(varargin)
% GETDEFAULTTOOLCHAIN Get the default host-based toolchain.

% Copyright 2012-2020 The MathWorks, Inc.

% Get the default host toolchain based on 'getMexCompilerInfo'.
% This function follows the following logic when choosing the default
% toolchain:
% 1. Check the toolchain configured via 'mex -setup'
% 2. If toolchain in #1 is not a supported code-gen toolchain, select from
%    a list of predefined toolchains.


% getMexCompilerInfo is very expensive to compute, so it is cached in the
% configset.internal.data.ConfigSetAdapter class during launch of the
% configset dialog and passed as the first argument to this function if
% available.  May be empty if no compilers are installed
if nargin > 0 && (isstruct(varargin{1}) || isempty(varargin{1}))
    tcStruct = varargin{1};
    args = varargin(2:end);
    if isempty(tcStruct)
        tcStruct = coder.make.internal.getMexCompilerInfo('installedOrFirstSupported');
    end
else
    tcStruct = coder.make.internal.getMexCompilerInfo('installedOrFirstSupported');
    args = varargin;
end

% The toolchain alias is reliable. Use this to locate the corresponding
% toolchain name.
tcAlias = tcStruct.compStr;
tcName  = coder.make.internal.getToolchainName(tcAlias, tcStruct, args{:});

% LocalWords:  toolchain toolchaininfos toolchains nmake gmake
