%lct_rtwmakecfg   Adds include and source directories to the make files 

function makeInfo = lct_rtwmakecfg()
%   Copyright 2005-2020 The MathWorks, Inc.
%RTWMAKECFG adds include and source directories to the make files.
%    makeInfo=
%    Simulink version    : 10.4 (R2021b) 14-May-2021
%    MATLAB file generated on : 22-Dec-2021 00:56:28

% Verify the Simulink version
verify_simulink_version();

% Get the current directory
currDir = pwd;

% Get the MATLAB search path, remove the toolbox sub-directories except LCT demos
matlabPath = legacycode.lct.util.getSearchPath();

% Declare cell arrays for storing the paths
allIncPaths = {};
allSrcPaths = {};

% Get the build type
isSimTarget = is_simulation_target();
% Get the serialized paths information
info = get_serialized_info();

% Get all S-Function's name in the current model
sfunNames = {};
if ~isempty(bdroot)
    sfunBlks = find_system(bdroot,...
        'LookUnderMasks', 'all',...
        'FollowLinks', 'on',...
        'BlockType', 'S-Function'...
    );
    sfunNames = get_param(sfunBlks, 'FunctionName');
end

for ii = 1:numel(info)
    sfunInfo = info(ii);
    % If the S-Function isn't part of the current build then skip its path info
    if ~any(strcmp(sfunInfo.SFunctionName, sfunNames))
        continue
    end

    % Call the helper
    pathInfo = legacycode.lct.util.resolvePaths(sfunInfo, isSimTarget==1, currDir, matlabPath);

    % Concatenate known include and source directories
    allIncPaths = RTW.uniquePath([allIncPaths pathInfo.IncPaths(:)']);
    allSrcPaths = RTW.uniquePath([allSrcPaths pathInfo.SrcPaths(:)']);
end

% Additional include directories
makeInfo.includePath = correct_path_name(allIncPaths);

% Additional source directories
makeInfo.sourcePath = correct_path_name(allSrcPaths);

%% ------------------------------------------------------------------------
function info = get_serialized_info()

% Allocate the output structure array
info(1:18) = struct(...
    'SFunctionName', '',...
    'IncPaths', {{}},...
    'SrcPaths', {{}},...
    'LibPaths', {{}},...
    'SourceFiles', {{}},...
    'HostLibFiles', {{}},...
    'TargetLibFiles', {{}},...
    'singleCPPMexFile', false,...
    'Language', ''...
    );

info(1).SFunctionName = 'sldemo_sfun_counterbus';
info(1).IncPaths = {'sldemo_lct_src'};
info(1).SrcPaths = {'sldemo_lct_src'};
info(1).SourceFiles = {'counterbus.c'};
info(1).Language = 'C';

info(2).SFunctionName = 'sldemo_sfun_gain_fixpt';
info(2).IncPaths = {'sldemo_lct_src'};
info(2).SrcPaths = {'sldemo_lct_src'};
info(2).SourceFiles = {'timesS16.c'};
info(2).Language = 'C';

info(3).SFunctionName = 'sldemo_sfun_times_s16';
info(3).IncPaths = {'sldemo_lct_src'};
info(3).SrcPaths = {'sldemo_lct_src'};
info(3).SourceFiles = {'timesS16.c'};
info(3).Language = 'C';

info(4).SFunctionName = 'sldemo_sfun_gain_scalar';
info(4).IncPaths = {'sldemo_lct_src'};
info(4).SrcPaths = {'sldemo_lct_src'};
info(4).SourceFiles = {'gainScalar.c'};
info(4).Language = 'C';

info(5).SFunctionName = 'sldemo_sfun_fault';
info(5).IncPaths = {'sldemo_lct_src'};
info(5).SrcPaths = {'sldemo_lct_src'};
info(5).SourceFiles = {'fault.c'};
info(5).Language = 'C';

info(6).SFunctionName = 'sldemo_sfun_work';
info(6).IncPaths = {'sldemo_lct_src'};
info(6).SrcPaths = {'sldemo_lct_src'};
info(6).SourceFiles = {'memory_bus.c'};
info(6).Language = 'C';

info(7).SFunctionName = 'sldemo_sfun_ndarray_add';
info(7).IncPaths = {'sldemo_lct_src'};
info(7).SrcPaths = {'sldemo_lct_src'};
info(7).SourceFiles = {'ndarray_ops.c'};
info(7).Language = 'C';

info(8).SFunctionName = 'sldemo_sfun_cplx_gain';
info(8).IncPaths = {'sldemo_lct_src'};
info(8).SrcPaths = {'sldemo_lct_src'};
info(8).SourceFiles = {'cplxgain.c'};
info(8).Language = 'C';

info(9).SFunctionName = 'sldemo_sfun_adder_cpp';
info(9).IncPaths = {'sldemo_lct_src'};
info(9).SrcPaths = {'sldemo_lct_src'};
info(9).SourceFiles = {'adder_cpp.cpp'};
info(9).Language = 'C++';

info(10).SFunctionName = 'sldemo_sfun_filterV1';
info(10).IncPaths = {'sldemo_lct_src'};
info(10).SrcPaths = {'sldemo_lct_src'};
info(10).SourceFiles = {'filterV1.c'};
info(10).Language = 'C';

info(11).SFunctionName = 'sldemo_sfun_filterV2';
info(11).IncPaths = {'sldemo_lct_src'};
info(11).SrcPaths = {'sldemo_lct_src'};
info(11).SourceFiles = {'filterV2.c'};
info(11).Language = 'C';

info(12).SFunctionName = 'sldemo_sfun_mat_add';
info(12).IncPaths = {'sldemo_lct_src'};
info(12).SrcPaths = {'sldemo_lct_src'};
info(12).SourceFiles = {'mat_ops.c'};
info(12).Language = 'C';

info(13).SFunctionName = 'sldemo_sfun_mat_mult';
info(13).IncPaths = {'sldemo_lct_src'};
info(13).SrcPaths = {'sldemo_lct_src'};
info(13).SourceFiles = {'mat_ops.c'};
info(13).Language = 'C';

info(14).SFunctionName = 'sldemo_sfun_dlut3D';
info(14).IncPaths = {'sldemo_lct_src'};
info(14).SrcPaths = {'sldemo_lct_src'};
info(14).SourceFiles = {'directLookupTableND.c'};
info(14).Language = 'C';

info(15).SFunctionName = 'sldemo_sfun_dlut4D';
info(15).IncPaths = {'sldemo_lct_src'};
info(15).SrcPaths = {'sldemo_lct_src'};
info(15).SourceFiles = {'directLookupTableND.c'};
info(15).Language = 'C';

info(16).SFunctionName = 'sldemo_sfun_st_inherited';
info(16).IncPaths = {'sldemo_lct_src'};
info(16).SrcPaths = {'sldemo_lct_src'};
info(16).SourceFiles = {'gainScalar.c'};
info(16).Language = 'C';

info(17).SFunctionName = 'sldemo_sfun_st_fixed';
info(17).IncPaths = {'sldemo_lct_src'};
info(17).SrcPaths = {'sldemo_lct_src'};
info(17).SourceFiles = {'gainScalar.c'};
info(17).Language = 'C';

info(18).SFunctionName = 'sldemo_sfun_st_parameterized';
info(18).IncPaths = {'sldemo_lct_src'};
info(18).SrcPaths = {'sldemo_lct_src'};
info(18).SourceFiles = {'gainScalar.c'};
info(18).Language = 'C';

%% ------------------------------------------------------------------------
function verify_simulink_version()

% Retrieve Simulink version
slVerStruct = ver('simulink');
slVer = str2double(strsplit(slVerStruct.Version,'.'));
% Verify that the actual platform supports the function used
if slVer(1)<8 || (slVer(1)==8 && slVer(2)<7)
    DAStudio.error('Simulink:tools:LCTErrorBadSimulinkVersion', slVerStruct.Version)
end

%% ------------------------------------------------------------------------
function pathSet = correct_path_name(pathSet)

for ii = 1:numel(pathSet)
    pathSet{ii} = coder.make.internal.transformPaths(pathSet{ii});
end
pathSet = RTW.uniquePath(pathSet);

%% ------------------------------------------------------------------------
function isSimTarget = is_simulation_target()

% Default output value
isSimTarget = 0;

% Get the current model and the code generation type to decide
% if we must link with the host libraries or with the target libraries
try
    modelName = get_param(0, 'CurrentSystem');
    if ~isempty(modelName)
        modelName = bdroot(modelName);
        sysTarget = get_param(modelName, 'RTWSystemTargetFile');
        isSimTarget = ~isempty([strfind(sysTarget, 'rtwsfcn') strfind(sysTarget, 'accel')]);

        mdlRefSimTarget = get_param(modelName,'ModelReferenceTargetType');
        isSimTarget = strcmpi(mdlRefSimTarget, 'SIM') || isSimTarget;

        % Verify again it's not Accelerator
        if ~isSimTarget
            simMode = get_param(modelName, 'SimulationMode');
            simStat = get_param(modelName, 'SimulationStatus');
            isSimTarget = strcmp(simStat, 'initializing') & strcmp(simMode, 'accelerator');
        end
    end
catch
end
