function code_machine_header_file_sfun(fileNameInfo)
% CODE_MACHINE_HEADER_FILE(FILENAMEINFO)

%   Copyright 1995-2020 The MathWorks, Inc.
%     

	global gTargetInfo gMachineInfo
	
    function emit(format,varargin)
        fprintf(file,format,varargin{:});
    end

	fileName = fullfile(fileNameInfo.targetDirName,fileNameInfo.machineHeaderFile);
   sf_echo_generating('Coder',fileName);
   machine = gMachineInfo.machineId;
    
   file = fopen(fileName,'Wt');
   if file<3
      construct_coder_error([],sprintf('Failed to create file: %s.',fileName),1);
      return;
   end             
	
fprintf(file,'#ifndef __%s_%s_h__\n',gMachineInfo.machineName,gMachineInfo.targetName);
fprintf(file,'#define __%s_%s_h__\n',gMachineInfo.machineName,gMachineInfo.targetName);

fprintf(file,'\n');
fprintf(file,'/* Include files */   \n');
fprintf(file,'#define S_FUNCTION_NAME sf_sfun\n');
fprintf(file,'#include "covrt.h"\n');
fprintf(file,'#include "sf_runtime/sfc_sf.h"\n');
fprintf(file,'#include "sf_runtime/MessageServiceLayer.h"\n');
fprintf(file,'#include "sf_runtime/DebuggerRuntimeInterface.h"\n');
fprintf(file,'#include "sf_runtime/sfc_mex.h"\n');
fprintf(file,'#include "sf_runtime/sf_runtime_errors.h"\n');
fprintf(file,'#include "sf_runtime/sf_partitioning_execution_bridge.h"\n');
fprintf(file,'#include "rtwtypes.h"\n');
fprintf(file,'#include "simtarget/slSimTgtClientServerAPIBridge.h"\n');
fprintf(file,'#include "sf_runtime/sfc_sdi.h"\n');
fprintf(file,'#include "sf_runtime/sf_test_language.h"\n');
   if slfeature('EMLOvCount')
fprintf(file,'#include "simlogCIntrf.h"\n');
   end
   if sf('feature','Half_MLFB')
fprintf(file,'#include "half_type.h" \n');
   end
 
   if fileNameInfo.multiword_types
fprintf(file,'#include "multiword_types.h"\n');
   end

fprintf(file,'#include "sf_runtime/sfc_messages.h"        \n');
    if gTargetInfo.codingLegacyDebug
fprintf(file,'#include "sf_runtime/sfcdebug.h"\n');
    end
fprintf(file,'#include "slccrt.h"\n');
fprintf(file,'\n');
fprintf(file,'#define rtInf (mxGetInf())\n');
fprintf(file,'#define rtMinusInf (-(mxGetInf()))\n');
fprintf(file,'#define rtNaN (mxGetNaN())\n');
fprintf(file,'#define rtInfF ((real32_T)mxGetInf())\n');
fprintf(file,'#define rtMinusInfF (-(real32_T)mxGetInf())\n');
fprintf(file,'#define rtNaNF ((real32_T)mxGetNaN())\n');
fprintf(file,'#define rtIsNaN(X) ((int)mxIsNaN(X))\n');
fprintf(file,'#define rtIsInf(X) ((int)mxIsInf(X))\n');
fprintf(file,'\n');
   if gTargetInfo.codingLegacyDebug
       assert(isfield(gMachineInfo, 'debugInstanceStructName'));
fprintf(file,'struct SfDebugInstanceStruct;\n');
fprintf(file,'extern struct SfDebugInstanceStruct* %s;\n',gMachineInfo.debugInstanceStructName);
   end
fprintf(file,'\n');
if ~isempty(fileNameInfo.auxInfo.includeFiles)
    emit('/* Auxiliary Header Files */\n');
    for i=1:numel(fileNameInfo.auxInfo.includeFiles)
        includeFile = fileNameInfo.auxInfo.includeFiles{i};
        if includeFile(1) == '<' || includeFile(1) == '"'
            delim = '';
        else
            delim = '"';
        end
        emit('#include %s%s%s\n', delim, includeFile, delim);
    end
    emit('\n');
end
if fileNameInfo.hasSLCCCustomCode && ~isempty(fileNameInfo.customCodeInterfaceHeader)
    [~, hFileName,ext] = fileparts(fileNameInfo.customCodeInterfaceHeader);
    emit('#include "%s"\n', [hFileName,ext]);

    if slfeature('CustomCodeIntegrationCoverage')
fprintf(file,'#include "sl_sfcn_cov/sl_sfcn_cov_bridge.h"\n');
    end
else
   % No extra file then emit the custom code
   % in the machine's header file
   customCodeSettings = get_custom_code_settings(gMachineInfo.target,gMachineInfo.parentTarget);
   customCodeString = customCodeSettings.customCode;
   if ~isempty(customCodeString)
fprintf(file,'/* Custom Code from Simulation Target dialog*/\n');
fprintf(file,'%s\n',customCodeString);
fprintf(file,'\n');
   end
end

   file = dump_module(fileName,file,machine,'header');
   if file < 3
     return;
   end

fprintf(file,'/* We load infoStruct for rtw_optimation_info on demand in mdlSetWorkWidths and\n');
fprintf(file,'  free it immediately in mdlStart. Given that this is machine-wide as\n');
fprintf(file,'  opposed to chart specific, we use NULL check to make sure it gets loaded\n');
fprintf(file,'  and unloaded once per machine even though the  methods mdlSetWorkWidths/mdlStart\n');
fprintf(file,'  are chart/instance specific. The following methods abstract this out. */\n');
fprintf(file,'extern mxArray* load_%s_optimization_info(boolean_T isRtwGen, boolean_T isModelRef, boolean_T isExternal);\n',gMachineInfo.machineName);
fprintf(file,'extern void unload_%s_optimization_info(void);\n',gMachineInfo.machineName);
fprintf(file,'#endif\n');
fprintf(file,'\n');

	fclose(file);
	try_indenting_file(fileName);
end
