function resp = getTargetDependencies(h, buildInfo, objOnly)
%

%   Copyright 2014-2019 The MathWorks, Inc.

% ---------------------------------------------------
% Normal target
% ---------------------------------------------------
target = h.Components.FinalProduct.TargetInfo;
resp = {  };
macros = h.getMacroToken( target.TargetDepsMacro );
for i = 1:numel( target.TargetDepsMacro )
    if ~any( strcmp( macros{ i }, resp ) )
        resp = horzcat( resp, macros{ i } );  %#ok<AGROW>
    end
end

% Add dependency on other ModelRef libs
modelRefLibToken = h.getMacroToken( h.getMacroOf( 'modelref libraries' ) );
if h.isModelReferenceCodeGenTarget() || objOnly
    % Do not depend on other ModelRef libs if:
    % 1. Building a static lib for a referenced model.
    % 2. Only building object code
    % Remove dependency on ModelRef libs
    resp = resp(~strcmp(resp, modelRefLibToken));
else
    if ~isempty( buildInfo.ModelRefs ) && ~isempty( buildInfo.ModelRefs( 1 ).Name ) && ~any( strcmp( modelRefLibToken, resp ) )
        resp = horzcat( resp, modelRefLibToken );
    end
end
%end
% ---------------------------------------------------
% Add MAIN_OBJ as target dependency for executable targets
% ---------------------------------------------------
if h.isStandaloneExecutableTarget && ~objOnly
    mainObjIdx = strcmp( h.Components.ObjectFiles.macro, h.getMacroOf( 'main object file' ) );
    if any( mainObjIdx )
        mainObjToken = h.getMacroToken( h.getMacroOf( 'main object file' ) );
        resp = horzcat( resp, mainObjToken );
    end
end
% ---------------------------------------------------
% Add the PREBUILT_OBJECTS as target dependencies for 
% software or processor-in-the-loop executable
% ---------------------------------------------------
if ~isempty( h.Components.PrebuiltObjectFiles )
    objToken = h.getMacroToken( h.getMacroOf( 'object files' ) );
    prebuiltObjToken = h.getMacroToken( h.Components.PrebuiltObjectFiles.macro );
    resp = h.insertCellElement( resp, prebuiltObjToken, objToken );
end
