Skip to content

Commit f3bf5ec

Browse files
committed
mex: is_rosetta()
1 parent aec286a commit f3bf5ec

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

private/get_mex_sources.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
% Matlab and Java don't have these functions
99
srcs = {fullfile(top, "src/is_char_device.cpp"), ...
1010
fullfile(top, "src/set_permissions.cpp"), ...
11+
fullfile(top, "src/is_rosetta.cpp"), ...
1112
};
1213

1314
s = fullfile(top, "src/windows_shortname.cpp");

src/is_rosetta.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include "mex.hpp"
2+
#include "mexAdapter.hpp"
3+
4+
#include <vector>
5+
#include <memory>
6+
7+
8+
#if defined(__APPLE__) && defined(__MACH__)
9+
#include <cerrno>
10+
#include <sys/sysctl.h>
11+
#endif
12+
13+
static bool fs_is_rosetta()
14+
{
15+
#if defined(__APPLE__) && defined(__MACH__)
16+
// https://developer.apple.com/documentation/apple-silicon/about-the-rosetta-translation-environment
17+
int ret = 0;
18+
size_t size = sizeof(ret);
19+
20+
if (sysctlbyname("sysctl.proc_translated", &ret, &size, nullptr, 0) < 0)
21+
return false;
22+
23+
return ret == 1;
24+
#else
25+
return false;
26+
#endif
27+
}
28+
29+
30+
class MexFunction : public matlab::mex::Function {
31+
public:
32+
void operator()(matlab::mex::ArgumentList outputs, matlab::mex::ArgumentList inputs) {
33+
// boilerplate engine & ArrayFactory setup
34+
std::shared_ptr<matlab::engine::MATLABEngine> matlabPtr = getEngine();
35+
36+
matlab::data::ArrayFactory factory;
37+
// boilerplate input checks
38+
if (inputs.size() != 0) {
39+
matlabPtr->feval(u"error", 0,
40+
std::vector<matlab::data::Array>({ factory.createScalar("No input required") }));
41+
}
42+
43+
// actual function algorithm / computation
44+
bool y = fs_is_rosetta();
45+
46+
// convert to Matlab output
47+
// https://www.mathworks.com/help/matlab/matlab_external/create-matlab-array-with-matlab-data-cpp-api.html
48+
// https://www.mathworks.com/help/matlab/apiref/matlab.data.arrayfactory.html
49+
50+
outputs[0] = factory.createScalar<bool>(y);
51+
}
52+
};

0 commit comments

Comments
 (0)