@@ -3,3 +3,46 @@ if(exist(file_in_loadpath('zipmat.mex')))
33else
44 autoload('zipmat',file_in_loadpath(['octave' filesep regexprep(computer('arch'), 'darwin[0-9.]+-', 'darwin-') filesep 'zipmat.mex']))
55end
6+
7+ %% On macOS, mkoctfile embeds versioned Octave library paths at build time that may
8+ %% not match the user's installed Octave version. Patch the MEX in-place to fix this.
9+ %% Handles Octave 8+ (liboctmex.1.dylib) and older Octave (liboctinterp/liboctave with
10+ %% a version number in the filename, e.g. liboctinterp.7.dylib).
11+ if ismac()
12+ mexpath = file_in_loadpath('zipmat.mex');
13+ if isempty(mexpath)
14+ mexpath = file_in_loadpath(['octave' filesep regexprep(computer('arch'), 'darwin[0-9.]+-', 'darwin-') filesep 'zipmat.mex']);
15+ end
16+ if ~isempty(mexpath)
17+ [~, octlibdir] = system('mkoctfile -p OCTLIBDIR 2>/dev/null');
18+ octlibdir = strtrim(octlibdir);
19+ if ~isempty(octlibdir)
20+ %% Collect all liboct* versioned dependencies embedded in the MEX
21+ [~, deplibs] = system(['otool -L "' mexpath '" 2>/dev/null | grep liboct | awk ''{print $1}''']);
22+ deplib_list = strsplit(strtrim(deplibs), char(10));
23+ changed = false;
24+ for k = 1:numel(deplib_list)
25+ curlib = strtrim(deplib_list{k});
26+ if isempty(curlib)
27+ continue
28+ end
29+ [~, libname, libext] = fileparts(curlib);
30+ newlib = fullfile(octlibdir, [libname libext]);
31+ if ~exist(newlib, 'file')
32+ %% Filename contains a version number (e.g. liboctinterp.7.dylib):
33+ %% strip the numeric suffix and glob for the installed version
34+ stripped = regexprep([libname libext], '\.[0-9]+\.dylib$', '');
35+ [~, found] = system(['ls "' octlibdir '"/' stripped '.*.dylib 2>/dev/null | head -1']);
36+ newlib = strtrim(found);
37+ end
38+ if ~isempty(newlib) && ~strcmp(curlib, newlib) && exist(newlib, 'file')
39+ system(['install_name_tool -change "' curlib '" "' newlib '" "' mexpath '" 2>/dev/null']);
40+ changed = true;
41+ end
42+ end
43+ if changed
44+ system(['codesign --force --sign - "' mexpath '" 2>/dev/null']);
45+ end
46+ end
47+ end
48+ end
0 commit comments