Skip to content

Commit 4310bb1

Browse files
committed
[macos] allow .mex for octave run older octave
1 parent 5e079a0 commit 4310bb1

File tree

2 files changed

+43
-9
lines changed

2 files changed

+43
-9
lines changed

PKG_ADD

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,46 @@ if(exist(file_in_loadpath('zipmat.mex')))
33
else
44
autoload('zipmat',file_in_loadpath(['octave' filesep regexprep(computer('arch'), 'darwin[0-9.]+-', 'darwin-') filesep 'zipmat.mex']))
55
end
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

src/Makefile

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -197,15 +197,6 @@ $(OUTPUT_DIR)/$(BINARY): makedirs $(OBJS) blosc
197197
$(OUTPUT_DIR)/$(BINARY): $(OBJS)
198198
@$(ECHO) Building $@
199199
$(AR) $(ARFLAGS) $(OUTPUTFLAG) $@ $(OBJS) $(LINKOPT) $(USERLINKOPT)
200-
ifeq ($(MAKECMDGOALS),oct)
201-
ifeq ($(findstring Darwin,$(PLATFORM)), Darwin)
202-
@octmex_path=$$(otool -L $@ | awk '/liboctmex/{print $$1}'); \
203-
if [ -n "$$octmex_path" ]; then \
204-
echo "Fixing liboctmex path: $$octmex_path -> @rpath/liboctmex.1.dylib"; \
205-
install_name_tool -change "$$octmex_path" "@rpath/liboctmex.1.dylib" $@; \
206-
fi
207-
endif
208-
endif
209200

210201
%$(OBJSUFFIX): %.cpp
211202
@$(ECHO) Building $@

0 commit comments

Comments
 (0)