Skip to content

Commit 3e27775

Browse files
committed
mex: is_wsl()
1 parent f837bc9 commit 3e27775

19 files changed

+118
-45
lines changed

+stdlib/is_rosetta.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
%% IS_ROSETTA on Apple Silicon via Rosetta (Matlab optional MEX)
1+
%% IS_ROSETTA on Apple Silicon via Rosetta (optional MEX)
22
% true if Matlab on Apple Silicon CPU is built for Intel x86_64
33

44
function r = is_rosetta()

+stdlib/is_symlink.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
%% IS_SYMLINK is path a symbolic link (Matlab optional MEX)
1+
%% IS_SYMLINK is path a symbolic link (optional MEX)
22

33
function ok = is_symlink(p)
44
arguments

+stdlib/is_wsl.m

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
1-
%% IS_WSL detect if running under WSL
1+
%% IS_WSL detect if running under WSL (optional MEX)
22
% Detects if Matlab or GNU Octave is installed and running from within
33
% Windows Subsystem for Linux
44

55
function w = is_wsl()
66

7-
persistent wsl;
7+
w = 0;
88

9-
if isempty(wsl)
10-
wsl = false;
11-
if isunix && ~ismac
12-
fid = fopen('/proc/version');
13-
if fid >= 1
14-
v = fscanf(fid,'%s');
15-
fclose(fid);
16-
wsl = ~isempty(strfind(v, 'microsoft-standard')); %#ok<*STREMP>
9+
if isunix && ~ismac
10+
fid = fopen('/proc/version');
11+
if fid >= 1
12+
v = fscanf(fid, '%s');
13+
if fclose(fid) ~= 0
14+
w = -1;
15+
elseif endsWith(v, "microsoft-standard-WSL2")
16+
w = 2;
17+
elseif endsWith(v, "-Microsoft")
18+
w = 1;
1719
end
1820
end
1921
end
2022

21-
w = wsl; % has to be a separate line/variable for matlab
23+
w = int32(w);
2224

2325
end
2426

25-
%!assert(islogical(is_wsl()))
27+
%!assert(class(is_wsl()), "int32")

+stdlib/windows_shortname.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
%% WINDOWS_SHORTNAME Retrieves the Windows short name (Matlab optional MEX)
1+
%% WINDOWS_SHORTNAME Retrieves the Windows short name (optional MEX)
22
% (8.3 character) filename
33
%
44
% Example of using a COM server (Scripting.FileSystemObject) in Windows

buildfile.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,15 @@ function publishTask(context)
8888
end
8989

9090
mac = "src/macos.cpp";
91+
linux = "src/linux_fs.cpp";
9192

9293
sym = "src/symlink_fs.cpp";
9394

9495

9596
srcs = {
9697
["src/is_admin.cpp", "src/admin_fs.cpp"] ...
9798
"src/is_char_device.cpp", ...
99+
["src/is_wsl.cpp", "src/linux_fs.cpp"], ...
98100
"src/set_permissions.cpp", ...
99101
["src/is_rosetta.cpp", mac], ...
100102
["src/windows_shortname.cpp", win], ...

octave_build.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
%% specific source
1111
srcs = {
12+
fullfile(d, "is_wsl.cpp"), ...
1213
fullfile(d, "is_rosetta.cpp"), ...
1314
fullfile(d, "is_admin.cpp"), ...
1415
};

src/is_symlink.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include "mexAdapter.hpp"
66

77
#include <string>
8-
// note: <string_view> causes compile failures with MSVC at least
98

109
#include <vector>
1110
#include <memory>

src/is_wsl.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include "mex.hpp"
2+
#include "mexAdapter.hpp"
3+
4+
#include <vector>
5+
#include <memory>
6+
7+
#include "linux_fs.h"
8+
9+
10+
class MexFunction : public matlab::mex::Function {
11+
public:
12+
void operator()(matlab::mex::ArgumentList outputs, matlab::mex::ArgumentList inputs) {
13+
// boilerplate engine & ArrayFactory setup
14+
std::shared_ptr<matlab::engine::MATLABEngine> matlabEng = getEngine();
15+
matlab::data::ArrayFactory factory;
16+
17+
if (inputs.size() != 0) {
18+
matlabEng->feval(u"error", 0,
19+
std::vector<matlab::data::Array>({ factory.createScalar("Mex: No input required") }));
20+
}
21+
22+
// actual function algorithm / computation
23+
int wsl = fs_is_wsl();
24+
25+
outputs[0] = factory.createScalar<int>(wsl);
26+
}
27+
};

src/linux_fs.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <string_view>
2+
3+
#if __has_include(<sys/utsname.h>)
4+
#define HAVE_UTSNAME
5+
#include <sys/utsname.h>
6+
#endif
7+
8+
#include "linux_fs.h"
9+
10+
11+
int fs_is_wsl()
12+
{
13+
14+
#ifdef HAVE_UTSNAME
15+
struct utsname buf;
16+
if (uname(&buf) != 0)
17+
return -1;
18+
19+
std::string_view s(buf.sysname);
20+
std::string_view r(buf.release);
21+
22+
if(s != "Linux")
23+
return 0;
24+
#ifdef __cpp_lib_starts_ends_with // C++20
25+
if (r.ends_with("microsoft-standard-WSL2"))
26+
return 2;
27+
if (r.ends_with("-Microsoft"))
28+
return 1;
29+
#endif
30+
#endif
31+
return 0;
32+
}

src/linux_fs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
int fs_is_wsl();

0 commit comments

Comments
 (0)