function code_machine_header_file_rtw(fileNameInfo)
% CODE_MACHINE_HEADER_FILE(FILENAMEINFO)

% Copyright 1995-2017 The MathWorks, Inc.

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

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

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);
end
fprintf(file,'%%implements "machineHeader" "C"\n');
fprintf(file,'%%function CacheOutputs(block,system) void\n');

if gTargetInfo.codingMemcpy && ~isempty(gMachineInfo.machineData)
fprintf(file,' %%<LibAddToCommonIncludes("<string.h>")>\n');
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% A few useful defines and includes
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fprintf(file,' %%assign srcFileName = SLibGetFullFileNameForSystemCode("sys_src_incl", system.SystemIdx)\n');
if gTargetInfo.codingLibrary
customCodeSettings = get_custom_code_settings(gMachineInfo.target,gMachineInfo.parentTarget);
if customCodeSettings.isLocalLibrary
customCodeString = customCodeSettings.customCode;
if(~isempty(customCodeString))
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Custom Code Included on the target
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fprintf(file,' %%openfile ccBuf\n');
fprintf(file,' %s\n',customCodeString);
fprintf(file,'\n');
fprintf(file,' %%closefile ccBuf\n');
fprintf(file,' %%<SLibCacheCodeToFile("sf_machine_incl",ccBuf)>\n');
fprintf(file,' %%if !WHITE_SPACE(ccBuf)\n');
fprintf(file,' %%<SLibUpdateHeadersNeededByFile(srcFileName, SLibGetFullFileNameForCode("sf_machine_incl"))>\n');
fprintf(file,' %%endif\n');
fprintf(file,'\n');
end
end
end

if gTargetInfo.modelReferenceSimTarget
fprintf(file,' %%<LibAddToCommonIncludes("sf_runtime/sfc_sdi.h")>\n');
end

% slrealtime.tlc requires verify/verifyIntrf.h while slrt.tlc requires xpcimports.h
if gTargetInfo.codingSLRT
if strcmp(get_param(sf('get', gMachineInfo.mainMachineId, '.name'), 'SystemTargetFile'),'slrealtime.tlc')
fprintf(file,' %%<LibAddToCommonIncludes("verify/verifyIntrf.h")>\n');
else
fprintf(file,' %%<LibAddToCommonIncludes("xpcimports.h")>\n');
end
end

if gTargetInfo.codingRTW && Simulink.harness.internal.isInstalled
chartIds = sf('ChartsOf', machine);
if any(sf('get', chartIds, '.sfunInfo.chartUsesVerify'))
dworkInfo = sf('get', chartIds, '.rtwInfo.dWorkVarInfo');
if iscell(dworkInfo)
dworkInfo = [dworkInfo{:}];
end
if ismember('slTestBlockInfo', [dworkInfo.varName])
if gTargetInfo.codingSVDPI
fprintf(file,' %%<LibAddToCommonIncludes("svdpi_verify.h")>\n');
end

if gTargetInfo.codingSLRT || gTargetInfo.codingSVDPI
fprintf(file,' %%<LibAddToCommonIncludes("rt_mxclassid.h")>\n');
fprintf(file,' %%<LibAddToCommonIncludes("sf_runtime/slTestResult.h")>\n');
fprintf(file,' %%<LibAddToCommonIncludes("sf_runtime/slTestTypes.h")>\n');
else
fprintf(file,' %%<LibAddToCommonIncludes("sf_runtime/sf_test_language.h")>\n');
end
end
end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Types
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fprintf(file,'%%openfile typedefsBuf \n');
types = sf('Cg','get_types',machine);
for type = types
codeStr = sf('Cg','get_type_def',type,0);
fprintf(file,' %s \n',codeStr);
end
fprintf(file,'%%closefile typedefsBuf\n');
fprintf(file,'%%<SLibCacheCodeToFile("sf_machine_typedef",typedefsBuf)>\n');
fprintf(file,'%%if !WHITE_SPACE(typedefsBuf)\n');
fprintf(file,' %%<SLibUpdateHeadersNeededByFile(srcFileName, SLibGetFullFileNameForCode("sf_machine_typedef"))>\n');
fprintf(file,'%%endif\n');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Named Constants
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fprintf(file,'%%openfile definesBuf \n');
namedConsts = sf('Cg','get_named_consts',machine);
for namedConst = namedConsts
codeStr = sf('Cg','get_named_const_def',namedConst,0);
fprintf(file,' %s \n',strip_trailing_new_lines(codeStr));
end
fprintf(file,'%%closefile definesBuf\n');
fprintf(file,'%%<SLibCacheCodeToFile("sf_machine_data_define",definesBuf)>\n');
fprintf(file,'%%if !WHITE_SPACE(definesBuf)\n');
fprintf(file,' %%<SLibUpdateHeadersNeededByFile(srcFileName, SLibGetFullFileNameForCode("sf_machine_data_define"))>\n');
fprintf(file,'%%endif\n');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Vars
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fprintf(file,'%%openfile externDataBuf\n');
vars = sf('Cg','get_non_exported_vars',machine);
for var = vars
codeStr = sf('Cg','get_var_decl',var,0);
fprintf(file,' %s \n',strip_trailing_new_lines(codeStr));
end
fprintf(file,'%%closefile externDataBuf\n');
fprintf(file,'%%<SLibCacheCodeToFile("sf_machine_extern_data_decl",externDataBuf)>\n');
fprintf(file,'%%if !WHITE_SPACE(externDataBuf)\n');
fprintf(file,' %%<SLibUpdateHeadersNeededByFile(srcFileName, SLibGetFullFileNameForCode("sf_machine_extern_data_decl"))>\n');
fprintf(file,'%%endif\n');

fprintf(file,'%%openfile externDataBuf\n');
vars = sf('Cg','get_exported_vars',machine);
for var = vars
codeStr = sf('Cg','get_var_decl',var,0);
fprintf(file,' %s \n',strip_trailing_new_lines(codeStr));
end
fprintf(file,'%%closefile externDataBuf\n');
fprintf(file,'%%<SLibCacheCodeToFile("sf_machine_public_extern_data_decl",externDataBuf)>\n');
fprintf(file,'%%if !WHITE_SPACE(externDataBuf)\n');
fprintf(file,' %%<SLibUpdateHeadersNeededByFile(srcFileName, SLibGetFullFileNameForCode("sf_machine_public_extern_data_decl"))>\n');
fprintf(file,'%%endif\n');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% function Decls
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fprintf(file,'%%openfile externDataBuf\n');
fprintf(file,'\n');
funcs = sf('Cg','get_functions',machine);
for func = funcs
codeStr = sf('Cg','get_fcn_decl',func,0);
fprintf(file,' %s \n',strip_trailing_new_lines(codeStr));
end

fprintf(file,'%%closefile externDataBuf\n');
fprintf(file,'%%<SLibCacheCodeToFile("sf_machine_extern_fcn_decl",externDataBuf)>\n');
fprintf(file,'%%if !WHITE_SPACE(externDataBuf)\n');
fprintf(file,' %%<SLibUpdateHeadersNeededByFile(srcFileName, SLibGetFullFileNameForCode("sf_machine_extern_fcn_decl"))>\n');
fprintf(file,'%%endif\n');

fprintf(file,'%%endfunction %%%% CacheOutputs\n');
fprintf(file,' \n');
fprintf(file,'\n');
fclose(file);
try_indenting_file(fileName);