Skip to content
Snippets Groups Projects
Commit a7347898 authored by Nicola Vigano's avatar Nicola Vigano
Browse files

Matlab functions compilation: improved error reporting

parent fb59015d
No related branches found
No related tags found
No related merge requests found
function gtCompileFunctions( force, mfiles, funcs_to_compile ) function gtCompileFunctions( force, mfiles, funcs_to_compile )
error_log = {};
wrong_names = {};
force = strcmp(force, 'force'); force = strcmp(force, 'force');
tot_keys = fieldnames(mfiles); should_compile_all = isempty(funcs_to_compile) ...
if (isempty(funcs_to_compile) ... || (numel(funcs_to_compile) == 1 && strcmp(funcs_to_compile{1}, 'all'));
|| (numel(funcs_to_compile) == 1 && strcmp(funcs_to_compile{1}, 'all')) )
funcs_to_compile = tot_keys;
else
valid = true(numel(funcs_to_compile), 1);
for ii = 1:numel(funcs_to_compile)
if (~ismember(funcs_to_compile{ii}, tot_keys))
valid(ii) = false;
wrong_names(1, end+1) = funcs_to_compile(ii);
end
end
all_keys = fieldnames(mfiles);
if (should_compile_all)
wrong_names = {};
funcs_to_compile = all_keys;
else
valid = ismember(funcs_to_compile, all_keys);
wrong_names = funcs_to_compile(~valid);
funcs_to_compile = funcs_to_compile(valid); funcs_to_compile = funcs_to_compile(valid);
end end
funcs_to_compile = reshape(funcs_to_compile, [1 numel(funcs_to_compile)]); funcs_to_compile = reshape(funcs_to_compile, [1 numel(funcs_to_compile)]);
num_funcs = numel(funcs_to_compile);
for funcName = funcs_to_compile error_log = cell(num_funcs, 2);
disp(['Checking function: "' funcName{1} '":']) for ii_f = 1:numel(funcs_to_compile)
func = mfiles.(funcName{1}); func_name = funcs_to_compile{ii_f};
disp(['Checking function: "' func_name '":'])
func = mfiles.(func_name);
mat_file_path = func.('in_mfile'); mat_file_path = func.('in_mfile');
comp_file_path = func.('out_file'); comp_file_path = func.('out_file');
[upToDate, msg] = gtCheckFunctionUpToDate(mat_file_path, comp_file_path); [up_to_date, msg] = gtCheckFunctionUpToDate(mat_file_path, comp_file_path);
fprintf(msg); fprintf(msg);
if (force || ~upToDate) if (force || ~up_to_date)
disp(['Compiling function: ' funcName{1}]); disp(['Compiling function: ' func_name]);
comp_file_dir = fileparts(comp_file_path); comp_file_dir = fileparts(comp_file_path);
try try
gt_mcc(funcName{1}, 'out_path', comp_file_dir) gtMcc(func_name, 'bin_path', comp_file_dir)
catch mexc catch mexc
error_log(1, end+1) = funcName; error_log(ii_f, :) = {func_name, mexc};
end end
end end
end end
for err_msg = error_log errors = find(~isempty(error_log(:, 1)));
disp(['Error while compiling: ' err_msg{1}]); for ii_e = 1:numel(errors)
func_name = error_log{errors(ii_e), 1};
mexc = error_log{errors(ii_e), 2};
gtPrintException(mexc, ...
sprintf('Could not compile function: %s', func_name))
end end
for wr_name = wrong_names for wr_name = wrong_names
disp(['Wrong m-file name: ' wr_name{1}]); disp(['Wrong m-file name: ' wr_name{1}]);
end end
......
function gt_mcc(myfunctionname, varargin) function gtMcc(myfunctionname, varargin)
% gt_mcc.m Compiles an executable and puts it in the right place % gt_mcc.m Compiles an executable and puts it in the right place
if (isempty(whos('global', 'GT_MATLAB_HOME'))) if (isempty(whos('global', 'GT_MATLAB_HOME')))
gtError('COMPILE:no_such_variable', ... error('COMPILE:no_such_variable', ...
['GT_MATLAB_HOME variable doesn''t exist in global workspace, '... ['GT_MATLAB_HOME variable doesn''t exist in global workspace, '...
'your environment is not sane, and you need to either ' ... 'your environment is not sane, and you need to either ' ...
're-initialise or re-run matlab.']) 're-initialise or re-run matlab.'])
...@@ -23,30 +23,24 @@ function gt_mcc(myfunctionname, varargin) ...@@ -23,30 +23,24 @@ function gt_mcc(myfunctionname, varargin)
end end
% Parsing arguments % Parsing arguments
conf = []; conf = struct( ...
conf.not_really = false; 'dry_run', false, ...
conf.out_path = ''; 'bin_path', fullfile(GT_MATLAB_HOME, 'bin', 'compiled'), ...
conf.licence_warning = false; 'licence_warning', false, ...
'remove_old', false );
conf = parse_pv_pairs(conf, varargin); conf = parse_pv_pairs(conf, varargin);
if (isempty(conf.out_path))
path_for_binaries = fullfile(GT_MATLAB_HOME, 'bin', 'compiled');
else
path_for_binaries = conf.out_path;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Modify the M name if necessary % Modify the M name if necessary
[~, ~, ext] = fileparts(myfunctionname); [~, ~, ext] = fileparts(myfunctionname);
if ~strcmp(ext, '.m') if (~strcmp(ext, '.m'))
myfunctionname = [myfunctionname '.m']; myfunctionname = [myfunctionname '.m'];
end end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% check the code is visible at the moment % check the code is visible at the moment
if ~exist(myfunctionname, 'file') if (~exist(myfunctionname, 'file'))
gtError('gt_mcc:wrong_file_name', ... error('gtMcc:wrong_file_name', ...
'Cannot find your function - is it in the path?') 'Cannot find your function - is it in the path?')
end end
...@@ -55,7 +49,7 @@ function gt_mcc(myfunctionname, varargin) ...@@ -55,7 +49,7 @@ function gt_mcc(myfunctionname, varargin)
globalVars = whos('global'); globalVars = whos('global');
eval(['global' sprintf(' %s', globalVars.('name'))]); eval(['global' sprintf(' %s', globalVars.('name'))]);
if (isempty(whos('global', 'GT_DB'))) if (isempty(whos('global', 'GT_DB')))
gtError('COMPILE:no_such_variable', ... error('gtMcc:no_such_variable', ...
['GT_DB variable doesn''t exist in global workspace, '... ['GT_DB variable doesn''t exist in global workspace, '...
'your environment is not sane, and you need to either ' ... 'your environment is not sane, and you need to either ' ...
're-initialise or re-run matlab.']) 're-initialise or re-run matlab.'])
...@@ -65,27 +59,30 @@ function gt_mcc(myfunctionname, varargin) ...@@ -65,27 +59,30 @@ function gt_mcc(myfunctionname, varargin)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% startup the compiler % startup the compiler
disp('Starting to compile - this may take a few minutes...'); disp('Starting to compile - this may take a few minutes...');
disp('- Removing previously compiled code'); if (conf.remove_old)
compFile = fullfile(path_for_binaries, [myfunctionname(1:end-2)]); fprintf('- Removing previously compiled code..');
runFile = fullfile(path_for_binaries, ['run_' myfunctionname(1:end-1) 'sh']); compFile = fullfile(conf.bin_path, myfunctionname(1:end-2));
filesToDelele = {compFile, runFile}; runFile = fullfile(conf.bin_path, ['run_' myfunctionname(1:end-1) 'sh']);
for fileToDelele = filesToDelele filesToDelele = {compFile, runFile};
if (conf.not_really) for fileToDelele = filesToDelele
disp(['I should now be deleting: ' fileToDelele{1}]); if (conf.dry_run)
elseif exist(fileToDelele{1}, 'file') disp(['I should now be deleting: ' fileToDelele{1}]);
delete(fileToDelele{1}); elseif exist(fileToDelele{1}, 'file')
delete(fileToDelele{1});
end
end end
fprintf('\b\b: Done.\n')
end end
disp(['- Placing result in ' path_for_binaries]); disp(['- Placing binary in: ' conf.bin_path]);
if (conf.not_really) if (conf.dry_run)
cmd = sprintf('mcc -m "%s" -d "%s" -a workspaceGlobal.mat', myfunctionname, path_for_binaries); cmd = sprintf('mcc -m "%s" -d "%s" -a workspaceGlobal.mat', myfunctionname, conf.bin_path);
disp(['I should now be executing: ''' cmd '''']); disp(['I should now be executing: ''' cmd '''']);
else else
try try
mcc('-m', myfunctionname, '-d', path_for_binaries, '-a', 'workspaceGlobal.mat'); mcc('-m', myfunctionname, '-d', conf.bin_path, '-a', 'workspaceGlobal.mat');
catch mexc catch mexc
if gtCheckExceptionType(mexc, 'mcc_err_checkout_failed') if (gtCheckExceptionType(mexc, 'mcc_err_checkout_failed'))
disp('Could not get a compiler license.'); disp('Could not get a compiler license.');
disp('Surf to http://compweb/public/nice/matlab_licence.php'); disp('Surf to http://compweb/public/nice/matlab_licence.php');
disp('and call the person with the compiler license to see if you could use it!'); disp('and call the person with the compiler license to see if you could use it!');
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment