function addToolchainInfo(lTargetRegistry, info)
%coder.make.internal.addToolchainInfo adds a new toolchain into the registry

% Copyright 2012-2020 The MathWorks, Inc.

% Refresh all ToolchainInfoRegistry entries
refreshToolchain(lTargetRegistry)

if nargin ~= 2    
    DAStudio.error('RTW:targetRegistry:invalidNumInput');    
end

% check info class
if ~isa(info, 'coder.make.ToolchainInfoRegistry')
    DAStudio.error('RTW:targetRegistry:invalidMember')
end

% get existing configs
infos = lTargetRegistry.ToolchainInfos;

% check ConfigName uniqueness
if ~isempty(infos)
    ind = find(strcmp({infos(:).Name},info.Name));
    if ~isempty(ind)
        if (numel(ind) > 1) 
            %This is not possible. But if someone hacked in we will catch here
            DAStudio.error('RTW:targetRegistry:duplicateToolchainInfoName', ...
                            info.Name)
        end
        if (~isequal(infos(ind).FileName, info.FileName))
            DAStudio.error('RTW:targetRegistry:duplicateToolchainInfoName', ...
                        info.Name)
        else
            return; %Already registered. Just ignore.
        end
    end
end

% add the config - note that ToolchainInfoRegistry is a value class so copying
% into ToolchainInfos is an implicit copy operation
if ~isempty(info) && ...
        ~isempty(info.Name) &&...
        (~isempty(info.FileName) || ~isempty(info.Handle)) &&...
        (isempty(info.Platform) || ismember('*', info.Platform) || ismember(computer('arch'), info.Platform))
    
    % Add the new toolchain info
    appendToolchainInfo(lTargetRegistry, info)
end

