function incCodeGenInfo = compute_inc_codegen_info(fileNameInfo,codingRebuildAll)

global gMachineInfo gTargetInfo %#ok<GVMIS>

numCharts = length(gMachineInfo.charts);
incCodeGenInfo.flags = cell(1, numCharts);
for i = 1:numCharts
    numSpecs = length(gMachineInfo.specializations{i});
    incCodeGenInfo.flags{i} = ones(1, numSpecs);
end
incCodeGenInfo.infoStruct = [];

if(~gTargetInfo.codingLibrary && gTargetInfo.codingSFunction)
    infoStruct = sfprivate('infomatman','load','dll',gMachineInfo.machineId,gMachineInfo.mainMachineId,gMachineInfo.targetName);
else
    infoStruct = sfprivate('infomatman','load','binary',gMachineInfo.machineId,gMachineInfo.mainMachineId,gMachineInfo.targetName);
end
jitIncCodegenInfoForMachine = [];
if(gTargetInfo.codingSFunction)
    jitIncCodegenInfo = sf('get',gMachineInfo.mainMachineId,'machine.jitIncCodeGenInfo');
    if(~isempty(jitIncCodegenInfo) && ...
            isfield(jitIncCodegenInfo,gMachineInfo.machineName) &&...
            isfield(jitIncCodegenInfo.(gMachineInfo.machineName),'info'))
        jitIncCodegenInfoForMachine = jitIncCodegenInfo.(gMachineInfo.machineName).info;
    end
end
incCodeGenInfo.jitIncCodegenInfoForMachine = jitIncCodegenInfoForMachine;

try
    lastBuildDate = infoStruct.date;
catch ME %#ok<NASGU>
    infoStruct.date = 0.0;
    lastBuildDate = 0.0;
end
regenerateCodeForallCharts = codingRebuildAll;

if(~regenerateCodeForallCharts)
    regenerateCodeForallCharts = ~isequal(infoStruct.machineChecksum,sf('get',gMachineInfo.machineId,'machine.checksum'));
end

if(~regenerateCodeForallCharts)
    regenerateCodeForallCharts = ~isequal(infoStruct.targetChecksum,sf('get',gMachineInfo.target,'target.checksumSelf'));
end

if(~regenerateCodeForallCharts)
    regenerateCodeForallCharts = fileNameInfo.isOverflowLoggingEnabled;
end

if((gTargetInfo.codingSFunction || gTargetInfo.codingRTW) && regenerateCodeForallCharts)
    clean_code_gen_dir(fileNameInfo.targetDirName);
end

incCodeGenInfo.infoStruct = infoStruct;

if(regenerateCodeForallCharts)
    return;
end

for i = 1:length(gMachineInfo.charts)
    chart = gMachineInfo.charts(i);
    [chartNumber,chartFileNumber] = sf('get',chart,'chart.number','chart.chartFileNumber');
    index = find(infoStruct.chartFileNumbers==chartFileNumber);
    chartAiInfo = [];
    forceRebuildChart = false;
    if ~isempty(index)
        forceRebuildChart = infoStruct.forceRebuildChartFlags(index);
    end
    
    if forceRebuildChart
        continue;
    end
    
    numSpecs = length(gMachineInfo.specializations{i});
    for j = 1:numSpecs
        if(gTargetInfo.codingSFunction && ~isempty(incCodeGenInfo.jitIncCodegenInfoForMachine))            
            %JIT feature on           
            thisSpec = gMachineInfo.specializations{i}{j};
            specInfo = jitIncCodegenInfoForMachine.(thisSpec);
            incCodeGenInfo.flags{i}(j) = specInfo.generateJitEngine || specInfo.generateCode;
            
            if(specInfo.foundInJit)
                gMachineInfo.codingLLVM{i}(j) = 1;
            end
            
            if incCodeGenInfo.flags{i}(j) == 0 && specInfo.needEMLReport
                % we have to regenerate this chart if EML report is needed
                incCodeGenInfo.flags{i}(j) = 1;
                continue;
            end
            
            if incCodeGenInfo.flags{i}(j) == 0 && ...
                specInfo.foundInDll && ...
                jitIncCodegenInfo.(gMachineInfo.machineName).reCodegenCandidates > 0
                    incCodeGenInfo.flags{i}(j) = verify_spec_files_for_inc_codegen_info(j);                
            end
        else 
            needsRegen = false;
            if gTargetInfo.codingSFunction
                if isempty(chartAiInfo)
                    key = sf('SyncAutoinheritanceChecksum', chart);
                    chartAiInfo = sfprivate('get_autoinheritance_info',gMachineInfo.machineName, gMachineInfo.mainMachineName, chartNumber, key, chart);
                end
                if (~isempty(chartAiInfo))
                    needsRegen = ~chartAiInfo.hasBreakpoints && sf('get', chart, '.hadBreakpoints');
                end
            end
            incCodeGenInfo.flags{i}(j) = verify_spec_files_for_inc_codegen_info(j) || needsRegen;
        end
    end
end

    %*****************************************************%
    function specIncCodegenVal = verify_spec_files_for_inc_codegen_info(specIdx) %Nested Function
        specIncCodegenVal = 1;
        if gTargetInfo.codingSFunction || gTargetInfo.codingRTW
            sourceFileName = fullfile(fileNameInfo.targetDirName,fileNameInfo.chartSourceFiles{chartNumber+1}{specIdx});
            headerFileName = fullfile(fileNameInfo.targetDirName,fileNameInfo.chartHeaderFiles{chartNumber+1}{specIdx});
        else
            sourceFileName = fullfile(fileNameInfo.targetDirName,fileNameInfo.chartSourceFiles{chartNumber+1});
            headerFileName = fullfile(fileNameInfo.targetDirName,fileNameInfo.chartHeaderFiles{chartNumber+1});
        end
        
        if(check_if_file_is_in_sync(sourceFileName,lastBuildDate) && ...
                check_if_file_is_in_sync(headerFileName,lastBuildDate))
            if numSpecs == 1
                checksum = [];
                if(~isempty(index))
                    checksum = infoStruct.chartChecksums(index,:);
                end
                regenerateCodeForThisChart = ~isequal(checksum,sf('get',chart,'chart.checksum'));
            else
                regenerateCodeForThisChart = 0;
            end
        else
            regenerateCodeForThisChart = 1;
        end
        if(~regenerateCodeForThisChart)
            specIncCodegenVal = 0;
        end
    end
    %*****************************************************%
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function result = check_if_file_is_in_sync(fileName,buildDate)

result = sfprivate('check_if_file_is_in_sync',fileName,buildDate);
end
