function [sourceExtensions, objectExtensions] = ...
    getFileExtensionsForToolchain(toolchainInfo)

% Copyright 2017-2019 The MathWorks, Inc.


cc = toolchainInfo.getBuildTool('C Compiler');
cext = cc.getIOFileExtensions('input');
oext = cc.getIOFileExtensions('output');

oext = locExtendDimensionsOfObjectExtensions(cext, oext);

foundCxx = strfind( toolchainInfo.SupportedLanguages, 'C++' );
foundCxx = ~isempty( foundCxx{1} );
if foundCxx
    cppc = toolchainInfo.getBuildTool('C++ Compiler');
    cppext = cppc.getIOFileExtensions('input');
    oppext = cppc.getIOFileExtensions('output');
    oppext = locExtendDimensionsOfObjectExtensions(cppext, oppext);
else
    cppext = {};
    oppext = {};
end

foundAssembler = strfind( toolchainInfo.SupportedLanguages, 'Asm' );
foundAssembler = ~isempty( foundAssembler{1} );
if foundAssembler
    asmc = toolchainInfo.getBuildTool('Assembler');
    asmext = asmc.getIOFileExtensions('input');
    oAsmExt = asmc.getIOFileExtensions('output');
    oAsmExt = locExtendDimensionsOfObjectExtensions(asmext, oAsmExt);
else
    asmext = {};
    oAsmExt = {};
end

sourceExtensions = [cext(:); cppext(:); asmext(:)];
objectExtensions = [oext(:); oppext(:); oAsmExt(:)];


function objectExtensionsExtended = ...
    locExtendDimensionsOfObjectExtensions(sourceExtensions, objectExtensions)
% Ensure a one-to-one mapping between source file extensions and object file
% extensions.
if length(sourceExtensions) == length(objectExtensions)
    % No extension needed
    objectExtensionsExtended = objectExtensions;
else
    % Extend object code file extensions so we have a one-to-one correspondence
    % between source code and object code file extensions
    assert(all(strcmp(objectExtensions, objectExtensions{1})), ...
           'Object file extensions must be consistent');
    objectExtensionsExtended = repmat(objectExtensions(1), size(sourceExtensions));
end
