Skip to content

Commit 9a3e87a

Browse files
committed
Autodownload liblsl release. On Windows, also extract.
1 parent 43c0203 commit 9a3e87a

5 files changed

Lines changed: 73 additions & 23 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/bin/
22
.idea
33
.venv
4-
*.m~
4+
*.m~
5+
*.asv

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ This is the MATLAB interface for liblsl.
66
* Using the MATLAB GUI, use File/Set Path...
77
* Alternatively, in a script, use `addpath(genpath('path/to/liblsl-Matlab'));`
88
* The `bin/` directory needs to contain an up-to-date build of the library file for your MATLAB version.
9-
* see [here](https://github.com/sccn/labstreaminglayer/blob/master/doc/BUILD.md#note-about-architectures--binaries) for more information which library you need
9+
* see [here](https://github.com/sccn/labstreaminglayer/blob/master/doc/BUILD.md#note-about-architectures--binaries) for more information about which library you need
1010
* download precompiled libraries from the [liblsl release page](https://github.com/sccn/liblsl/releases)
11-
* e.g. `liblsl64.dll` for 64-bit MATLAB on Windows.
11+
* e.g. `liblsl64.dll` for 64-bit MATLAB on Windows. (Note: Next LSL releases will change these to `lsl.dll`)
1212
* e.g. `liblsl64.dylib` and `liblsl64.1.4.0.dylib` for 64-bit MATLAB on MacOS.
1313
* e.g. `liblsl64.so` and `liblsl64.so.1.4.0` for 64-bit MATLAB in Linux
1414
* Once this taken care of, see the example files in the examples/ directory for how to use this interface in a MATLAB program.
@@ -22,4 +22,6 @@ This is the MATLAB interface for liblsl.
2222
If you get an error similar to `lsl_loadlib_ undefined`, then you may need to run the `build_mex.m` script from within the liblsl-Matlab directory.
2323
(From the command line: `matlab -nodesktop -nosplash -r 'build_mex'`)
2424

25+
If `build_mex` seems to have completed successfully but the error persists, please make sure the liblsl-Matlab/bin folder is on the path.
26+
2527
On MacOS, you may still get an error similar to `Invalid MEX-file [...] lsl_loadlib_.mexmaci64; Reason: image not found.`. To fix this run the following command in a Terminal window from within the liblsl-Matlab directory: `install_name_tool -add_rpath "@loader_path/" bin/lsl_loadlib_.mexmaci64`

build_mex.m

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,28 @@
22
% For Octave on Linux, you need the package liboctave-dev installed
33
% You also need the liblsl64 binary in the bin folder and a configured
44
% C compiler (mex -setup)
5-
6-
libs = {'-llsl64'};
7-
if isunix
5+
binarypath = fullfile(fileparts(mfilename('fullpath')), 'bin');
6+
if ispc
7+
if exist(fullfile(binarypath, 'liblsl64.lib'), 'file')
8+
libs = {'-llsl64'};
9+
elseif exist(fullfile(binarypath, 'lsl.lib'), 'file')
10+
libs = {'-llsl'};
11+
else
12+
error('Neither liblsl64.lib nor lsl.lib found in bin/');
13+
end
14+
elseif isunix
815
libs = {'-llsl64','-ldl'};
9-
end
10-
11-
12-
if ispc && isempty(dir('bin/liblsl64.lib'))
13-
error('liblsl64.lib not found in bin/');
16+
else
17+
libs = {'-llsl64'};
1418
end
1519

1620
ext = ['.' mexext];
1721

1822
files = dir('mex/*.c');
19-
cd('bin');
23+
24+
orig_path = pwd();
25+
disp('Building mex files. This may take a few minutes.');
26+
cd(binarypath);
2027
for i = 1:length(files)
2128
f = files(i);
2229
[~, base, ~] = fileparts(f.name);
@@ -30,4 +37,4 @@
3037
if ismac
3138
system('install_name_tool -add_rpath "@loader_path/" lsl_loadlib_.mexmaci64')
3239
end
33-
cd('..');
40+
cd(orig_path);

lsl_get_dll.m

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
% lsl_fname : the filename of the library
1313

1414
if ~exist('binarypath','var') || isempty(binarypath)
15-
binarypath = [fileparts(mfilename('fullpath')) filesep 'bin'];
15+
binarypath = fullfile(fileparts(mfilename('fullpath')), 'bin');
1616
end
1717
if ~exist('debugging','var') || isempty(debugging)
1818
debugging = false;
@@ -28,7 +28,7 @@
2828
error('Your operating system is not supported by this version of the lab streaming layer API.');
2929
end
3030

31-
if strfind(computer,'64')
31+
if contains(computer,'64')
3232
bitness = '64';
3333
else
3434
bitness = '32';
@@ -40,18 +40,56 @@
4040
debug = '';
4141
end
4242

43-
dll_fname = sprintf('liblsl%s%s%s', bitness, debug, ext);
44-
lsl_fname = fullfile(binarypath, dll_fname);
43+
so_fname = sprintf('liblsl%s%s%s', bitness, debug, ext);
44+
lsl_fname = fullfile(binarypath, so_fname);
4545

46-
if ~exist(lsl_fname, 'file') && ~ispc
47-
new_dllpath = fullfile('/usr/lib/', dll_fname);
48-
if exist(new_dllpath, 'file')
49-
lsl_fname = new_dllpath;
46+
if ~exist(lsl_fname, 'file')
47+
if ispc
48+
% On Windows, also try simply 'lsl.dll'
49+
new_sopath = fullfile(binarypath, 'lsl.dll');
50+
else
51+
new_sopath = fullfile('/usr/lib/', so_fname);
52+
end
53+
if exist(new_sopath, 'file')
54+
lsl_fname = new_sopath;
5055
end %if
5156
end %if
5257

5358
if ~exist(lsl_fname,'file')
54-
error(['Apparently the file "' dllpath '" is missing on your computer. Cannot load the lab streaming layer.']);
59+
disp(['Could not locate the file "' so_fname '" on your computer. Attempting to download...']);
60+
LIBLSL_TAG = 'v1.14.0b1';
61+
LIBLSL_VER = '1.14.0';
62+
liblsl_url = ['https://github.com/sccn/liblsl/releases/download/' LIBLSL_TAG '/'];
63+
if ispc
64+
liblsl_url_fname = ['liblsl-' LIBLSL_VER '-Win' bitness '.zip'];
65+
elseif ismac
66+
liblsl_url_fname = ['liblsl-' LIBLSL_VER '-OSX64.tar.bz2'];
67+
68+
elseif isunix
69+
liblsl_url_fname = ['liblsl-' LIBLSL_VER '-Linux64-bionic.deb'];
70+
end
71+
try
72+
websave(fullfile(binarypath, liblsl_url_fname),...
73+
[liblsl_url liblsl_url_fname]);
74+
catch ME
75+
disp(['Unable to download ' liblsl_url]);
76+
rethrow(ME);
77+
end
78+
if ispc
79+
unzip(fullfile(binarypath, liblsl_url_fname),...
80+
fullfile(binarypath, 'liblsl_archive'));
81+
lsl_fname = fullfile(binarypath, 'lsl.dll');
82+
copyfile(fullfile(binarypath, 'liblsl_archive', 'bin', 'lsl.dll'), lsl_fname);
83+
copyfile(fullfile(binarypath, 'liblsl_archive', 'lib', 'lsl.lib'),...
84+
fullfile(binarypath, 'lsl.lib'));
85+
elseif ismac
86+
untar(fullfile(binarypath, liblsl_url_fname),...
87+
fullfile(binarypath, 'liblsl_archive'));
88+
error('TODO: copyfile from liblsl_archive to lsl_fname on mac.');
89+
elseif isunix
90+
error(['Automatic extraction of debian package not yet supported.', ...
91+
' Please install manually: ' fullfile(binarypath, liblsl_url_fname)]);
92+
end
5593
end
5694

5795
end

lsl_loadlib.m

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@
4343
try
4444
hlib = lsl_loadlib_(dllpath);
4545
catch e
46-
disp('See https://github.com/labstreaminglayer/liblsl-Matlab/#troubleshooting for troubleshooting tips');
46+
disp('This error is probably because lsl_loadlib_ mex file is not on the path.');
47+
disp('Try running build_mex.m. If that was successful, make sure the liblsl-Matlab/bin folder is added to the path.');
48+
disp('See https://github.com/labstreaminglayer/liblsl-Matlab/#troubleshooting for further troubleshooting tips');
4749
error(['Error loading the liblsl library: ', e.message]);
4850
end
4951

0 commit comments

Comments
 (0)