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
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 exist('xxsim') 0042 xxsimVerbose = xxsim.verbose; 0043 end 0044 0045 submodelName = ''; 0046 if length(varargin)> 4 0047 error('Too many input arguments.'); 0048 end 0049 if length(varargin)< 2 0050 error('Too few input arguments.'); 0051 end 0052 0053 % first argument must hold target information 0054 if ~ischar(varargin{1}) 0055 0056 if isnumeric(varargin{1}) 0057 targetIndex = varargin{1}; 0058 0059 % get a list of C-Code targets, to determine the index 0060 targets = xxsimGetCCodeTargets(); 0061 0062 if length(targets) < targetIndex 0063 error('Wrong target index given'); 0064 end 0065 0066 % and set the name of this target 0067 targetName = targets(targetIndex).name; 0068 0069 else 0070 error('target name must be a string'); 0071 end 0072 else 0073 targetName = varargin{1}; 0074 end 0075 0076 if length(varargin)>=2 0077 if ~ischar(varargin{2}) 0078 error('output directory must be a string'); 0079 end 0080 outputDirectory = varargin{2}; 0081 end 0082 0083 if length(varargin)>=3 0084 if ~ischar(varargin{3}) 0085 error('submodel name must be a string'); 0086 end 0087 submodelName = varargin{3}; 0088 else 0089 submodelName = ''; 0090 end 0091 0092 if length(varargin)>=4 0093 if ~ischar(varargin{4}) 0094 error('templateDirectory must be a string'); 0095 end 0096 templateDirectory = varargin{4}; 0097 else 0098 templateDirectory = ''; 0099 end 0100 0101 if( xxsimVerbose ) 0102 fprintf('Generating C-Code for : '); 0103 end 0104 0105 args = struct('targetName', targetName, 'outputDirectory', outputDirectory, 'submodelName', submodelName, 'templateDirectory', templateDirectory); 0106 codeGenResult = xrlinvoke('xxsim.simulator.generateCCode', args); 0107 if( isfield(codeGenResult, 'errors') && length(codeGenResult.errors) ~= 0 ) 0108 result = 0; 0109 else 0110 result = 1; 0111 end 0112 0113 % check if the code generation itself returned an error 0114 if result ~= 0 0115 if isfield(codeGenResult, 'result') 0116 result = codeGenResult.result; 0117 else 0118 result = false; 0119 end 0120 end 0121 0122 %optionally return the outputs as the third variable 0123 if isfield(codeGenResult, 'warnings') 0124 warnings = codeGenResult.warnings; 0125 else 0126 warnings = []; 0127 end 0128 0129 if isfield(codeGenResult, 'errors') 0130 errors = codeGenResult.errors; 0131 else 0132 errors = []; 0133 end 0134 0135 if( xxsimVerbose ) 0136 if( result ) 0137 fprintf('Success\n'); 0138 else 0139 fprintf('Failure\n'); 0140 end 0141 0142 % print the errors and warnings 0143 for i = 1:length(codeGenResult.errors) 0144 fprintf('\tError: %s\n',codeGenResult.errors{i}); 0145 end 0146 for i = 1:length(codeGenResult.warnings) 0147 fprintf('\tWarning: %s\n', codeGenResult.warnings{i}); 0148 end 0149 fprintf('\Code generation: %d error(s), %d warning(s)\n', length(codeGenResult.errors), length(codeGenResult.warnings)); 0150 end 0151 end 0152 %------------- END CODE --------------