function code_chart_source_file_sfun(fileNameInfo, chart, specsIdx)


%   Copyright 1995-2013 The MathWorks, Inc.


   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   %%%%%  GLOBAL VARIABLES
   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

   %%%%%%%%%%%%%%%%%%%%%%%%% Coding options
   global gTargetInfo  gChartInfo gMachineInfo %#ok<GVMIS>

 	chartNumber = sf('get',chart,'chart.number');
	chartUniqueName = sf('CodegenNameOf',chart);

   fileName = fullfile(fileNameInfo.targetDirName,fileNameInfo.chartSourceFiles{chartNumber+1}{specsIdx});
   sf_echo_generating('Coder',fileName);

   file = fopen(fileName,'Wt');
   if file<3
      construct_coder_error([],getString(message('Stateflow:sfprivate:error_FailedToCreateFile',fileName)),1);
      return;
   end

fprintf(file,'/* Include files */\n');
   % include simstruc & fixedpoint header files if dataset signal logging is
   % enabled because in this case we might generate cpp
   % files and c++ compiler is not able to build the generated sfun code without these header
   % files - g890413
   if ( ~isempty(sf('find', gMachineInfo.mainMachineId, '.sigLogFormat', 'SIGLOG_DATASET')) && ...
               gChartInfo.hasLoggedSignals )
fprintf(file,'#include "simstruc.h"\n');
fprintf(file,'#include "fixedpoint.h"\n');
fprintf(file,'#include "simtarget/slSimTgtLogLoadBlocksSfcnBridge.h"\n');
   end

   if ~isempty(fileNameInfo.openMPIncludeFile)
fprintf(file,'#include "%s"\n',fileNameInfo.openMPIncludeFile);
   end
fprintf(file,'#include "%s"\n',[fileNameInfo.machineHeaderFile(1:end-length(fileNameInfo.headerExtension)),'.h']);
fprintf(file,'#include "%s"\n',[fileNameInfo.chartHeaderFiles{chartNumber+1}{specsIdx}(1:end-length(fileNameInfo.headerExtension)),'.h']);
   % Make sure all recorded TFL header files are included into the source file
   tfl = gMachineInfo.ctxInfo.usedTargetFunctionLibH;
   tflHeaders = tfl.getRecordedUsedHeaders;
   for fileIdx = 1:length(tflHeaders)
fprintf(file,'#include %s\n',tflHeaders{fileIdx});
   end
   tfl.resetUsageCounts;

   if gTargetInfo.leavingCtrlCChecks
fprintf(file,'#define _SF_MEX_LISTEN_FOR_CTRL_C(S) sf_mex_listen_for_ctrl_c(S);\n');
   end

fprintf(file,'    \n');
fprintf(file,'#ifdef utFree\n');
fprintf(file,'#undef utFree\n');
fprintf(file,'#endif\n');
fprintf(file,'#ifdef utMalloc\n');
fprintf(file,'#undef utMalloc\n');
fprintf(file,'#endif\n');
fprintf(file,'#ifdef __cplusplus\n');
fprintf(file,'extern "C" void *utMalloc(size_t size);\n');
fprintf(file,'extern "C" void utFree(void*);\n');
fprintf(file,'#else\n');
fprintf(file,'extern void *utMalloc(size_t size);\n');
fprintf(file,'extern void utFree(void*);\n');
fprintf(file,'#endif\n');
fprintf(file,'\n');

   if fileNameInfo.hasSLCCCustomCode && ~isempty(fileNameInfo.customCodeChecksum)
      cMacroFcnNames = sf('Cg', 'get_macro_fcn_names', chart);
      for i = 1:numel(cMacroFcnNames)
fprintf(file,'#define %s %s_%s\n',cMacroFcnNames{i},cMacroFcnNames{i},fileNameInfo.customCodeChecksum);
      end
   end

   file = dump_module(fileName,file,chart,'source');
   if file < 3
     return;
   end

   file = code_sfun_glue_code(fileNameInfo,file,chart,chartUniqueName,specsIdx);
   fclose(file);
   try_indenting_file(fileName);
