Skip to content

Commit cfe49a1

Browse files
authored
Matlab: If mex fails, print mex arguments (#2013)
For all mex calls, catch exception, print mex invocation, rethrow. Will make bug reports way more informative and simplify debugging. Resolves #913
1 parent 34057d1 commit cfe49a1

File tree

2 files changed

+43
-11
lines changed

2 files changed

+43
-11
lines changed

matlab/@amimodel/compileAndLinkModel.m

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,15 @@ function compileAndLinkModel(modelname, modelSourceFolder, coptim, debug, funs,
127127
sources = cellfun(@(x) ['"' fullfile(modelSourceFolder,[modelname '_' x '.cpp']) '"'],funsForRecompile,'UniformOutput',false);
128128
sources = strjoin(sources,' ');
129129

130-
eval(['mex ' DEBUG COPT ...
130+
cmd = ['mex ' DEBUG COPT ...
131131
' -c -outdir "' modelObjectFolder '" ' ...
132-
sources ' ' ...
133-
includesstr ]);
132+
sources ' ' includesstr];
133+
try
134+
eval(cmd);
135+
catch ME
136+
disp(cmd);
137+
rethrow(ME);
138+
end
134139
cellfun(@(x) updateFileHashSource(modelSourceFolder, modelObjectFolder, [modelname '_' x]),funsForRecompile,'UniformOutput',false);
135140
end
136141

@@ -156,10 +161,17 @@ function compileAndLinkModel(modelname, modelSourceFolder, coptim, debug, funs,
156161

157162
% compile the wrapfunctions object
158163
fprintf('wrapfunctions | ');
159-
eval(['mex ' DEBUG COPT ...
164+
cmd = ['mex ' DEBUG COPT ...
160165
' -c -outdir "' modelObjectFolder '" "' ...
161166
fullfile(modelSourceFolder,'wrapfunctions.cpp') '" ' model_cpp ...
162-
includesstr]);
167+
includesstr];
168+
try
169+
eval(cmd);
170+
catch ME
171+
disp(cmd);
172+
rethrow(ME);
173+
end
174+
163175
objectsstr = [objectsstr, ' "' fullfile(modelObjectFolder,['wrapfunctions' objectFileSuffix]) '" ' model_cpp_obj];
164176

165177
% now we have compiled everything model-specific, so we can replace hashes.mat to prevent recompilation
@@ -186,8 +198,15 @@ function compileAndLinkModel(modelname, modelSourceFolder, coptim, debug, funs,
186198
end
187199

188200
mexFilename = fullfile(modelSourceFolder,['ami_' modelname]);
189-
eval(['mex ' DEBUG ' ' COPT ' ' CLIBS ...
190-
' -output "' mexFilename '" ' objectsstr])
201+
cmd = ['mex ' DEBUG ' ' COPT ' ' CLIBS ...
202+
' -output "' mexFilename '" ' objectsstr];
203+
try
204+
eval(cmd);
205+
catch ME
206+
disp(cmd);
207+
rethrow(ME);
208+
end
209+
191210
end
192211

193212
function [objectStrAmici] = compileAmiciBase(amiciRootPath, objectFolder, objectFileSuffix, includesstr, DEBUG, COPT)
@@ -217,8 +236,15 @@ function compileAndLinkModel(modelname, modelSourceFolder, coptim, debug, funs,
217236
baseFilename = fullfile(amiciSourcePath, sourcesForRecompile{j});
218237
sourceStr = [sourceStr, ' "', baseFilename, '.cpp"'];
219238
end
220-
eval(['mex ' DEBUG COPT ' -c -outdir "' objectFolder '" ' ...
221-
includesstr ' ' sourceStr]);
239+
cmd = ['mex ' DEBUG COPT ' -c -outdir "' objectFolder '" ' ...
240+
includesstr ' ' sourceStr];
241+
try
242+
eval(cmd);
243+
catch ME
244+
disp(cmd);
245+
rethrow(ME);
246+
end
247+
222248
cellfun(@(x) updateFileHashSource(amiciSourcePath, objectFolder, x), sourcesForRecompile);
223249
updateHeaderFileHashes(amiciIncludePath, objectFolder);
224250
end

matlab/auxiliary/compileAMICIDependencies.m

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,15 @@
4848

4949
% compile
5050
if(~strcmp(sourcesToCompile, ''))
51-
eval(['mex ' DEBUG ' ' COPT ' -c -outdir "' ...
51+
cmd = ['mex ' DEBUG ' ' COPT ' -c -outdir "' ...
5252
objectFolder '" ' ...
53-
includesstr ' ' sourcesToCompile ]);
53+
includesstr ' ' sourcesToCompile];
54+
try
55+
eval(cmd);
56+
catch ME
57+
disp(cmd);
58+
rethrow(ME);
59+
end
5460
end
5561

5662
% only write versions.txt if we are done compiling

0 commit comments

Comments
 (0)