Skip to content

Commit 55e12fc

Browse files
committed
toolbox_used: protect from spurious OpenMP error when parallel toolbox is installed
1 parent ce70078 commit 55e12fc

2 files changed

Lines changed: 49 additions & 6 deletions

File tree

+stdlib/toolbox_used.m

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,56 @@
11
%% TOOLBOX_USED list all Matlab toolboxes used by all functions in pkgPath
22
%
33
%%% inputs
4-
% * pkgPath: path to user namespace or file. Example: '+stdlib' or 'stdlib.cpu_arch'
4+
% * pkgPath: path to user namespace or file. Example: 'stdlib' or 'stdlib.cpu_arch'
55
%%% Outputs
66
% * tbxMathworks: Mathworks toolbox names used
77
% * funUser: all user function files under pkgPath
88

9-
function [tbxMathworks, funUser] = toolbox_used(pkgPath)
9+
function [tbxMathworks, funUser] = toolbox_used(name)
1010

11-
[user, mathworks] = matlab.codetools.requiredFilesAndProducts(pkgPath);
11+
n = name;
12+
13+
try
14+
15+
f = namespaceFunctions(name);
16+
if ~isempty(f)
17+
n = f(1).NamespaceName + "." + string({f.Name});
18+
end
19+
20+
catch e
21+
22+
if ~strcmp(e.identifier, 'MATLAB:UndefinedFunction')
23+
rethrow(e)
24+
end
25+
26+
end
27+
28+
if ispc()
29+
old = getenv('KMP_DUPLICATE_LIB_OK');
30+
setenv('KMP_DUPLICATE_LIB_OK', 'TRUE');
31+
32+
% otherwise,
33+
% matlab -batch "buildtool test"
34+
% or
35+
% matlab -batch "stdlib.toolbox_used('stdlib')"
36+
% crash Matlab with:
37+
% OMP: Error #15: Initializing libiomp5md.dll, but found libiomp5md.dll already initialized.
38+
%
39+
% calling toolbox_used on any one function doesn't fail.
40+
end
41+
42+
try
43+
[user, mathworks] = matlab.codetools.requiredFilesAndProducts(n);
44+
catch e
45+
if ispc()
46+
setenv('KMP_DUPLICATE_LIB_OK', old)
47+
end
48+
rethrow(e)
49+
end
50+
51+
if ispc()
52+
setenv('KMP_DUPLICATE_LIB_OK', old)
53+
end
1254

1355
tbxMathworks = string({mathworks.Name}).';
1456

test/TestPlatform.m

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,16 @@ function test_has_parallel_toolbox(tc)
3131

3232
function test_toolbox_used(tc)
3333
tc.assumeFalse(stdlib.matlabOlderThan('R2018a'))
34-
r = fullfile(fileparts(fileparts(mfilename('fullpath'))), '+stdlib');
35-
[mathworksUsed, userFun] = stdlib.toolbox_used(r);
34+
35+
[mathworksUsed, userFun] = stdlib.toolbox_used('stdlib');
36+
3637
Nlicense = length(mathworksUsed);
3738
tc.verifyGreaterThanOrEqual(Nlicense, 1)
3839
tc.verifyTrue(ismember('MATLAB', mathworksUsed))
3940
tc.verifyGreaterThan(length(userFun), 200) % we have over 200 stdlib functions
4041

4142
% don't use paid toolboxes without checking they exist, otherwise this function fails
42-
[mathworksUsed, userFun] = stdlib.toolbox_used(["which", "disp"]);
43+
[mathworksUsed, userFun] = stdlib.toolbox_used('disp');
4344
tc.verifyEqual(userFun, string.empty)
4445
tc.verifyEqual(length(mathworksUsed), 1)
4546
end

0 commit comments

Comments
 (0)