xxsimGenerateCCode

PURPOSE ^

xxsimGenerateCCode - Generate C-Code for the active model using the specified C-code target template.

SYNOPSIS ^

function [result, warnings, errors] = xxsimGenerateCCode(varargin)

DESCRIPTION ^

 xxsimGenerateCCode - Generate C-Code for the active model using the specified C-code target template.

 Syntax: 
   [result, warnings, errors] = xxsimGenerateCCode(targetName, outputDirectory, submodelName, templateDirectory)
   result = xxsimGenerateCCode(targetName, outputDirectory)

 Inputs:
   targetName   = the name of the target to generate C-Code for.
   outputDirectory = the output directory for the generated C-Code.
   submodelName = (optional) the name of the submodel to generate C-Code for.
                   Only necessary for targets that need a submodel selection.
   templateDirectory = (optional) the directory where the code generation template is stored.
                       When omitted, 20-sim used the default 'C-Code Folders' setting instead.

 Outputs:
   result  = returns true if the C-Code generation was successful.
   warnings = warning(s) during C-Code generation, if any.
   errors  = error(s) during C-Code generation if, any.

 Examples:
    result = xxsimGenerateCCode('CFunction','c:/temp/controller','Controller')
        Generate code for a C-function based on the submodel Controller, and 
        store it in C:/temp/controller.
    result = xxsimGenerateCCode('StandAloneC','c:/temp/examples');
        Generate stand-alone C code and store it in C:/temp/examples.
    [result, warnings, errors] = xxsimGenerateCCode('CFunction','c:/temp/controller','Controller')
        Generate code for a C-function based on the submodel Controller, and 
        store it in C:/temp/controller. Also store the messages (warnings, errors).

 See also: xxsimGetCCodeTargets, xxsimGenerateMatlabCode

 Author: Controllab Products B.V.
 email: info@controllab.nl
 Website: http://www.controllab.nl
 October 2014

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [result, warnings, errors] = xxsimGenerateCCode(varargin)
0002 % xxsimGenerateCCode - Generate C-Code for the active model using the specified C-code target template.
0003 %
0004 % Syntax:
0005 %   [result, warnings, errors] = xxsimGenerateCCode(targetName, outputDirectory, submodelName, templateDirectory)
0006 %   result = xxsimGenerateCCode(targetName, outputDirectory)
0007 %
0008 % Inputs:
0009 %   targetName   = the name of the target to generate C-Code for.
0010 %   outputDirectory = the output directory for the generated C-Code.
0011 %   submodelName = (optional) the name of the submodel to generate C-Code for.
0012 %                   Only necessary for targets that need a submodel selection.
0013 %   templateDirectory = (optional) the directory where the code generation template is stored.
0014 %                       When omitted, 20-sim used the default 'C-Code Folders' setting instead.
0015 %
0016 % Outputs:
0017 %   result  = returns true if the C-Code generation was successful.
0018 %   warnings = warning(s) during C-Code generation, if any.
0019 %   errors  = error(s) during C-Code generation if, any.
0020 %
0021 % Examples:
0022 %    result = xxsimGenerateCCode('CFunction','c:/temp/controller','Controller')
0023 %        Generate code for a C-function based on the submodel Controller, and
0024 %        store it in C:/temp/controller.
0025 %    result = xxsimGenerateCCode('StandAloneC','c:/temp/examples');
0026 %        Generate stand-alone C code and store it in C:/temp/examples.
0027 %    [result, warnings, errors] = xxsimGenerateCCode('CFunction','c:/temp/controller','Controller')
0028 %        Generate code for a C-function based on the submodel Controller, and
0029 %        store it in C:/temp/controller. Also store the messages (warnings, errors).
0030 %
0031 % See also: xxsimGetCCodeTargets, xxsimGenerateMatlabCode
0032 %
0033 % Author: Controllab Products B.V.
0034 % email: info@controllab.nl
0035 % Website: http://www.controllab.nl
0036 % October 2014
0037 
0038 %------------- BEGIN CODE --------------
0039     global xxsim;
0040     xxsimVerbose = 0;
0041     if ~isempty(xxsim)
0042         xxsimVerbose = xxsim.verbose;
0043     end
0044 
0045     if length(varargin)> 4
0046         error('Too many input arguments.');
0047     end
0048     if length(varargin)< 2
0049         error('Too few input arguments.');
0050     end
0051     
0052     % first argument must hold target information
0053     if ~ischar(varargin{1})
0054     
0055         if isnumeric(varargin{1})
0056             targetIndex = varargin{1};
0057             
0058             % get a list of C-Code targets, to determine the index
0059             targets = xxsimGetCCodeTargets();
0060             
0061             if length(targets) < targetIndex 
0062                 error('Wrong target index given');
0063             end
0064             
0065             % and set the name of this target
0066             targetName = targets(targetIndex).name;
0067 
0068         else
0069             error('target name must be a string');
0070         end
0071     else
0072         targetName = varargin{1};
0073     end
0074 
0075     if length(varargin)>=2
0076         if ~ischar(varargin{2})
0077             error('output directory must be a string');
0078         end
0079         outputDirectory = varargin{2};
0080     end
0081 
0082     if length(varargin)>=3
0083         if ~ischar(varargin{3})
0084             error('submodel name must be a string');
0085         end
0086         submodelName = varargin{3};
0087     else
0088         submodelName = '';
0089     end
0090     
0091     if length(varargin)>=4
0092         if ~ischar(varargin{4})
0093             error('templateDirectory must be a string');
0094         end
0095         templateDirectory = varargin{4};
0096     else
0097         templateDirectory = '';
0098     end
0099 
0100     if( xxsimVerbose )
0101         fprintf('Generating C-Code for : ');
0102     end
0103 
0104     args = struct('targetName', targetName, 'outputDirectory', outputDirectory, 'submodelName', submodelName, 'templateDirectory', templateDirectory);
0105     codeGenResult = xrlinvoke('xxsim.simulator.generateCCode', args);
0106     if( isfield(codeGenResult, 'errors') && length(codeGenResult.errors) ~= 0 )
0107         result = 0;
0108     else
0109         result = 1;
0110     end
0111     
0112     % check if the code generation itself returned an error
0113     if result ~= 0
0114         if isfield(codeGenResult, 'result')
0115             result = codeGenResult.result;
0116         else
0117             result = false;
0118         end
0119     end
0120     
0121     %optionally return the outputs as the third variable
0122     if isfield(codeGenResult, 'warnings')
0123         warnings = codeGenResult.warnings;
0124     else
0125         warnings = [];
0126     end
0127     
0128     if isfield(codeGenResult, 'errors')
0129         errors = codeGenResult.errors;
0130     else
0131         errors = [];
0132     end
0133     
0134     if( xxsimVerbose )
0135         if( result )
0136             fprintf('Success\n');
0137         else
0138             fprintf('Failure\n');
0139         end
0140 
0141         % print the errors and warnings
0142         for i = 1:length(codeGenResult.errors)
0143             fprintf('\tError: %s\n',codeGenResult.errors{i});
0144         end
0145         for i = 1:length(codeGenResult.warnings)
0146             fprintf('\tWarning: %s\n', codeGenResult.warnings{i});
0147         end
0148         fprintf('\Code generation: %d error(s), %d warning(s)\n', length(codeGenResult.errors), length(codeGenResult.warnings));
0149     end
0150 end
0151 %------------- END CODE --------------

Generated on Thu 25-Apr-2024 10:30:24