gusucode.com > target工具箱matlab源码程序 > target/foundation/utils/writeMacroFile.m
function writeMacroFile(infoStruct, targetBlockInfo) %WRITEMACROFILE Create a header file containing macro declarations to %switch between host or target implementations % Copyright 2009-2011 The MathWorks, Inc. % Open the output file [fid, msg] = fopen(targetBlockInfo.files.host.HeaderFiles{1}, 'w'); if fid==-1 DAStudio.error('Simulink:tools:LCTErrorCannotOpenFile',... infoStruct.Specs.SFunctionName, fext, ['(',msg,')']); end % Write Header section fprintf(fid, '#ifndef %s\n', upper([targetBlockInfo.files.host.HeaderFiles{1}(1:end-2) '_h'])); fprintf(fid, '#define %s\n', upper([targetBlockInfo.files.host.HeaderFiles{1}(1:end-2) '_h'])); % Get rid of irregular function call expressions with 'size' before defining macro startStr = removeOutputArgs(targetBlockInfo.legacyfunc.Start); OutputStr = removeOutputArgs(targetBlockInfo.legacyfunc.Output); TerminateStr = removeOutputArgs(targetBlockInfo.legacyfunc.Terminate); InitializeConditionsStr = removeOutputArgs(targetBlockInfo.legacyfunc.InitializeConditions); if( ~isempty(startStr) && ~isempty(regexp( startStr, 'size', 'once')) ) headerIdx = regexp(startStr, '.(size)','end'); tailIdx = regexp(startStr, '.(size)\s*\(\s*\w*\W*\s*,\s*\d*\s*\)','end') +1; startStr = [ startStr(1:headerIdx) startStr(tailIdx:end)]; end if(~isempty(OutputStr) && ~isempty(regexp( OutputStr, 'size', 'once')) ) headerIdx = regexp(OutputStr, '.(size)','end'); tailIdx = regexp(OutputStr, '.(size)\s*\(\s*\w*\W*\s*,\s*\d*\s*\)','end') +1; OutputStr = [ OutputStr(1:headerIdx) OutputStr(tailIdx:end)]; end if(~isempty(TerminateStr) && ~isempty(regexp( TerminateStr, 'size', 'once')) ) headerIdx = regexp(TerminateStr, '.(size)','end'); tailIdx = regexp(TerminateStr, '.(size)\s*\(\s*\w*\W*\s*,\s*\d*\s*\)','end') +1; TerminateStr = [ TerminateStr(1:headerIdx) TerminateStr(tailIdx:end)]; end if(~isempty(InitializeConditionsStr) && ~isempty(regexp( InitializeConditionsStr, 'size', 'once')) ) headerIdx = regexp(InitializeConditionsStr, '.(size)','end'); tailIdx = regexp(InitializeConditionsStr, '.(size)\s*\(\s*\w*\W*\s*,\s*\d*\s*\)','end') +1; InitializeConditionsStr = [ InitializeConditionsStr(1:headerIdx) InitializeConditionsStr(tailIdx:end)]; end if( ~isempty(startStr) && ~isempty(regexp( startStr, 'type', 'once')) ) headerIdx = regexp(startStr, '.(type)','end'); tailIdx = regexp(startStr, '.(type)\s*\(\s*\w*\W*\s*,\s*\d*\s*\)','end') +1; startStr = [ startStr(1:headerIdx) startStr(tailIdx:end)]; end if(~isempty(OutputStr) && ~isempty(regexp( OutputStr, 'type', 'once')) ) headerIdx = regexp(OutputStr, '.(type)','end'); tailIdx = regexp(OutputStr, '.(type)\s*\(\s*\w*\W*\s*,\s*\d*\s*\)','end') +1; OutputStr = [ OutputStr(1:headerIdx) OutputStr(tailIdx:end)]; end if(~isempty(TerminateStr) && ~isempty(regexp( TerminateStr, 'type', 'once')) ) headerIdx = regexp(TerminateStr, '.(type)','end'); tailIdx = regexp(TerminateStr, '.(type)\s*\(\s*\w*\W*\s*,\s*\d*\s*\)','end') +1; TerminateStr = [ TerminateStr(1:headerIdx) TerminateStr(tailIdx:end)]; end if(~isempty(InitializeConditionsStr) && ~isempty(regexp( InitializeConditionsStr, 'type', 'once')) ) headerIdx = regexp(InitializeConditionsStr, '.(type)','end'); tailIdx = regexp(InitializeConditionsStr, '.(type)\s*\(\s*\w*\W*\s*,\s*\d*\s*\)','end') +1; InitializeConditionsStr = [ InitializeConditionsStr(1:headerIdx) InitializeConditionsStr(tailIdx:end)]; end % Write Header section fprintf(fid, '\n#if defined(MATLAB_MEX_FILE)\n'); fprintf(fid, '/* This will be compiled by MATLAB to create the Simulink block: */\n\n'); if ~isempty(startStr) fprintf(fid, '\n/* Model Start function*/\n'); fprintf(fid, '#define %s (0)\n', startStr); end if ~isempty(OutputStr) fprintf(fid, '\n/* Model Step function*/\n'); fprintf(fid, '#define %s (0)\n', OutputStr); end if ~isempty(TerminateStr) fprintf(fid, '\n/* Model Terminate function*/\n'); fprintf(fid, '#define %s (0)\n', TerminateStr); end if ~isempty(InitializeConditionsStr) fprintf(fid, '\n/* Model Initialization function*/\n'); fprintf(fid, '#define %s (0)\n', InitializeConditionsStr); end fprintf(fid, '\n#else\n\n'); fprintf(fid, '/* This will be called by the target compiler: */\n'); if ~isempty(targetBlockInfo.files.target.HeaderFiles) for numTgtHeader=1:numel(targetBlockInfo.files.target.HeaderFiles) fprintf(fid, '#include "%s"\n', targetBlockInfo.files.target.HeaderFiles{numTgtHeader}); end end fprintf(fid, '\n /*Following prototype mapping is done in the code generation*/\n\n'); if( ~isempty(targetBlockInfo.legacyfunc.HWStart) ) fprintf(fid, '#define %s %s\n', startStr, targetBlockInfo.legacyfunc.HWStart); end if( ~isempty(targetBlockInfo.legacyfunc.HWInit) ) fprintf(fid, '#define %s %s\n', InitializeConditionsStr, targetBlockInfo.legacyfunc.HWInit); end if( ~isempty(targetBlockInfo.legacyfunc.HWOutput) ) fprintf(fid, '#define %s %s\n', OutputStr, targetBlockInfo.legacyfunc.HWOutput); end if( ~isempty(targetBlockInfo.legacyfunc.HWTerminate) ) fprintf(fid, '#define %s %s\n', TerminateStr, targetBlockInfo.legacyfunc.HWTerminate); end fprintf(fid, '\n\n#endif /*MATLAB_MEX_FILE*/\n'); fprintf(fid, '#endif /*%s*/\n ', targetBlockInfo.files.host.HeaderFiles{1}); fprintf(fid, '\n'); % Close the file fclose(fid); %-------------------------------------------------------------------------- function fcnStr = removeOutputArgs(fcnStr) fcnStr = regexprep(fcnStr, '\s', ''); % Eliminate spaces tmp = regexp(fcnStr, '=', 'split'); if (numel(tmp) == 1) fcnStr = tmp{1}; elseif (numel(tmp) == 2) fcnStr = tmp{2}; else DAStudio.error('TARGETFOUNDATION:utils:tbbUnexpectedFuncProto', fcnStr); end