Skip to content

Commit cf98a20

Browse files
committed
250812.212651.CST matlab: update set_compiler_options and restore_compiler_options
1 parent 3a40237 commit cf98a20

File tree

4 files changed

+54
-6
lines changed

4 files changed

+54
-6
lines changed

matlab/tests/private/get_solvers.m

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,10 @@
9999
try
100100

101101
if with_compiler_options
102-
set_compiler_options(options.compiler_options);
102+
sco_success = set_compiler_options(options.compiler_options);
103+
if ~sco_success
104+
warning('Failed to set compiler options!!!');
105+
end
103106
end
104107

105108
for is = 1 : length(solvers)
@@ -155,8 +158,11 @@
155158
end
156159

157160
% Restore the compiler options.
158-
if with_compiler_options
159-
restore_compiler_options();
161+
if with_compiler_options && sco_success
162+
rco_success = restore_compiler_options();
163+
if ~rco_success
164+
warning('Failed to restore compiler options!!!');
165+
end
160166
end
161167
% Go back to the old directory.
162168
cd(olddir);

matlab/tests/private/iswritable.m

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function iw = iswritable(fdname)
2+
%ISWRITABLE returns true or false to indicate whether the file or directory is writable.
3+
[status, attributes] = fileattrib(fdname);
4+
5+
iw = false;
6+
if status == 1
7+
iw = attributes.UserWrite;
8+
else
9+
errid = 'IsWritable:FDNotExists';
10+
error(errid, 'IsWritable:Error checking the attribute of %s. The file or directory does not exist', fdname);
11+
end

matlab/tests/private/restore_compiler_options.m

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
1-
function restore_compiler_options()
1+
function success = restore_compiler_options()
22
%Restore the compiler options by restoring the mexopts files. It is hacky!!!
33

44
if ~isunix || ismac
55
error('Configuration of compiler options supports only Linux.')
66
end
77

8+
success = false;
9+
810
config_dir = fullfile(matlabroot,'bin', 'glnxa64', 'mexopts');
911
config_files = {dir(fullfile(config_dir, 'gfortran*.xml')).name};
12+
fileattrib(config_dir, '+w');
13+
if ~iswritable(config_dir)
14+
warning('The directory %s is not writable. Compile options not restored.', config_dir);
15+
return;
16+
end
1017
for ifile = 1 : length(config_files)
1118
cfile = fullfile(config_dir, config_files{ifile});
1219
cfile_orig = fullfile(config_dir, [config_files{ifile}, '.orig']);
@@ -38,10 +45,17 @@ function restore_compiler_options()
3845
mex_setup_file = fullfile(prefdir, ['mex_FORTRAN_', computer('arch'), '.xml']);
3946
if exist(mex_setup_file, 'file')
4047
fileattrib(prefdir, '+w');
48+
if ~iswritable(prefdir)
49+
warning('The directory %s is not writable. The restored compile options may not take effect.', prefdir);
50+
end
4151
fileattrib(mex_setup_file, '+w');
52+
if ~iswritable(mex_setup_file)
53+
warning('The file %s is not writable. The restored compile options may not take effect.', mex_setup_file);
54+
end
4255
delete(mex_setup_file);
4356
end
4457

4558
fprintf('\nCompiler options restored successfully.\n\n');
59+
success = true;
4660

4761
return

matlab/tests/private/set_compiler_options.m

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,29 @@
1-
function set_compiler_options(compiler_options)
1+
function success = set_compiler_options(compiler_options)
22
%Configure the compiler options by editing the mexopts files. It is hacky!!!
33

44
if ~isunix || ismac
55
error('Configuration of compiler options supports only Linux.')
66
end
77

8+
success = false;
9+
810
mfilepath = fileparts(mfilename('fullpath')); % The directory containing this setup script
911

1012
% Modify mexopts files in `config_dir` after making backups.
1113
config_dir = fullfile(matlabroot, 'bin', 'glnxa64', 'mexopts');
1214
config_files = {dir(fullfile(config_dir, 'gfortran*.xml')).name};
1315
fileattrib(config_dir, '+w');
16+
if ~iswritable(config_dir)
17+
warning('The directory %s is not writable. Compile options not set.', config_dir);
18+
return;
19+
end
1420
for ifile = 1 : length(config_files)
1521
cfile = fullfile(config_dir, config_files{ifile});
1622
fileattrib(cfile, '+w')
23+
if ~iswritable(cfile)
24+
warning('The file %s is not writable. Compile options not set.', cfile);
25+
return;
26+
end
1727

1828
cfile_orig = fullfile(config_dir, [config_files{ifile}, '.orig']);
1929
if ~exist(cfile_orig, 'file')
@@ -41,15 +51,22 @@ function set_compiler_options(compiler_options)
4151
% We delete it so that MEX will be reconfigured according to `config_files`.
4252
% Why not making a backup for it? Because the existing version may be generated using the modified
4353
% `config_files` when this script was called the last time (N.B.: `mex_setup_file` does not exist
44-
% in a fresh installation of MATLAB), in which case it would be wrong to store % this copy and
54+
% in a fresh installation of MATLAB), in which case it would be wrong to store this copy and
4555
% restore `mex_setup_file` using it.
4656
mex_setup_file = fullfile(prefdir, ['mex_FORTRAN_', computer('arch'), '.xml']);
4757
if exist(mex_setup_file, 'file')
4858
fileattrib(prefdir, '+w');
59+
if ~iswritable(prefdir)
60+
warning('The directory %s is not writable. The modified compile options may not take effect.', prefdir);
61+
end
4962
fileattrib(mex_setup_file, '+w');
63+
if ~iswritable(mex_setup_file)
64+
warning('The file %s is not writable. The modified compile options may not take effect.', mex_setup_file);
65+
end
5066
delete(mex_setup_file);
5167
end
5268

5369
fprintf('\nCompiler options set to \n\n%s\n\n', compiler_options);
70+
success = true;
5471

5572
return

0 commit comments

Comments
 (0)