function [buildInfos, buildInfoPathsOriginalAbs, buildInfoPathsOriginalRelative, ...
componentNames] = ...
getSharedBuildInfos(buildInfo, lInstrPartOfPath, excludeGroup)
% Return an array of buildInfo objects containing shared source files that must be instrumented;
% includes buildInfo for source files in the main shared utilities folder as well buildInfo for each
% reusable library source file

% Copyright 2009-2021 The MathWorks, Inc.

arguments
buildInfo
lInstrPartOfPath
excludeGroup = ''
end

buildInfoPathsOriginalAbs = {};
buildInfoPathsOriginalRelative = {};
componentNames = {};

% TopTester: test/toolbox/coder/build/sl/general/tRLS_SIL_PWS_Hierarchy.m
sharedLibs = buildInfo.LinkObjsDirect;
sharedLibs = sharedLibs(:)';

if ~isempty(excludeGroup)
keepIdx = ~strcmp({sharedLibs.Group}, excludeGroup);
sharedLibs = sharedLibs(keepIdx);
end

if isempty(sharedLibs)
buildInfos = [];
return;
end

% For each shared library, load the referenced buildInfo, if any
for i=1:length(sharedLibs)
loadLinkObjBuildInfo(sharedLibs(i));
end

hasBuildInfoIdx = {sharedLibs.BuildInfoHandle};
hasBuildInfoIdx = ~cellfun(@isempty, hasBuildInfoIdx);
sharedLibs = sharedLibs(hasBuildInfoIdx);

if isempty(sharedLibs)
buildInfos = [];
return;
end

buildInfos = [sharedLibs(:).BuildInfoHandle];
buildInfoPathsTokenized = {sharedLibs.Path};
componentNames = {buildInfos(:).ModelName};

[buildInfoPathsOriginalAbs, buildInfoPathsOriginalRelative] = ...
coder.coverage.getOriginalPathsFromInstrumented(buildInfo, ...
lInstrPartOfPath, buildInfoPathsTokenized);
end