Skip to content

Commit a069a9a

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

2 files changed

Lines changed: 54 additions & 10 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: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,30 +27,32 @@ function test_has_parallel_toolbox(tc)
2727
end
2828

2929

30-
methods (Test, TestTags={'R2018a'})
31-
30+
methods (Test, TestTags={'R2026a'})
3231
function test_toolbox_used(tc)
33-
tc.assumeFalse(stdlib.matlabOlderThan('R2018a'))
34-
r = fullfile(fileparts(fileparts(mfilename('fullpath'))), '+stdlib');
35-
[mathworksUsed, userFun] = stdlib.toolbox_used(r);
32+
tc.assumeFalse(stdlib.matlabOlderThan('R2026a'))
33+
34+
[mathworksUsed, userFun] = stdlib.toolbox_used('stdlib');
35+
3636
Nlicense = length(mathworksUsed);
3737
tc.verifyGreaterThanOrEqual(Nlicense, 1)
3838
tc.verifyTrue(ismember('MATLAB', mathworksUsed))
3939
tc.verifyGreaterThan(length(userFun), 200) % we have over 200 stdlib functions
4040

4141
% don't use paid toolboxes without checking they exist, otherwise this function fails
42-
[mathworksUsed, userFun] = stdlib.toolbox_used(["which", "disp"]);
42+
[mathworksUsed, userFun] = stdlib.toolbox_used('disp');
4343
tc.verifyEqual(userFun, string.empty)
4444
tc.verifyEqual(length(mathworksUsed), 1)
4545
end
46+
end
47+
4648

49+
methods (Test, TestTags={'R2018a'})
4750
function test_perl(tc)
4851
tc.assumeFalse(stdlib.matlabOlderThan('R2018a'))
4952
tc.verifyNotEmpty(stdlib.perl_exe())
5053
tc.verifyNotEmpty(stdlib.perl_version())
5154
tc.verifyTrue(stdlib.has_perl(), 'Matlab docs indicate that Perl should always be available')
5255
end
53-
5456
end
5557

5658

0 commit comments

Comments
 (0)