function obj = createFromToolchainInfoRegistryObject(info)
%CREATEFROMTOOLCHAININFOREGISTRYOBJECT Converts a ToolchainInfoRegistry
%object to a target.internal.ToolchainInfoBuildMethod

%   Copyright 2020-2022 The MathWorks, Inc.

obj = target.internal.create('ToolchainBuildMethod');
obj.Name = info.Name;
obj.Filename = info.FileName;
obj.Alias = info.Alias;
obj.SupportedHardware = locGetSupportedHardware(info.TargetHWDeviceType);

if isempty(info.Platform)
    obj.HostOperatingSystemSupport = target.internal.HostOperatingSystemSupport.All;
else    
    % Reset the platform support to none
    obj.HostOperatingSystemSupport = target.internal.create('HostOperatingSystemSupport', ...
        'Mac', 0, ...
        'Linux', 0,...
        'Windows', 0);
    
    % Set the host operating system support
    for i = 1:numel(info.Platform)
        switch info.Platform{i}
            case '*'
                obj.HostOperatingSystemSupport = target.internal.HostOperatingSystemSupport.All;
            case {'win32', 'win64'}
                obj.HostOperatingSystemSupport.Windows = true;
            case {'maci64', 'maca64'}
                obj.HostOperatingSystemSupport.Mac = true;
            case 'glnxa64'
                obj.HostOperatingSystemSupport.Linux = true;
            otherwise
                error(message('coder_compile:toolchain:BadPlatform', info.Platform{i}));
        end
    end
end
end

function hwCompSupp = locGetSupportedHardware(targetHWDeviceType)

hwCompSupp = target.internal.HardwareComponentSupport.empty();
if ~any(strcmp(targetHWDeviceType, '*'))
    % Empty implies the build method is associated with every language 
    % implementation
    helper = targetrepository.getHardwareImplementationHelper;
    for kDev = 1:numel(targetHWDeviceType)
        device = helper.getDevice(targetHWDeviceType{kDev});
        if ~isempty(device)
            hwCompSupp(end+1) = ...
                target.internal.create('HardwareComponentSupport', 'Component', device); %#ok<AGROW> 
        end
        
    end
    
end
end
