Skip to content

Commit 52303f4

Browse files
committed
fixups for unix lib finding; lsl_get_dll now additionally returns lsl_include_dir
1 parent 2a0014e commit 52303f4

File tree

2 files changed

+45
-38
lines changed

2 files changed

+45
-38
lines changed

build_mex.m

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
files = dir('mex/*.c');
1010

1111
% Find liblsl, possibly downloading it if it can't be found locally.
12-
lsl_fname = lsl_get_dll();
12+
[lsl_fname, lsl_include_dir] = lsl_get_dll();
1313

1414
% Build cell array of libray dependencies (liblsl and maybe others)
1515
libs = {};
@@ -24,14 +24,6 @@
2424
libs{end+1} = '-ldl';
2525
end
2626

27-
% Find liblsl headers. If liblsl was downloaded above then check there.
28-
% Otherwise assume they are in a sister directory.
29-
if exist(fullfile(script_dir, 'bin', 'liblsl_archive', 'include'), 'dir')
30-
incl_dir = fullfile(script_dir, 'bin', 'liblsl_archive', 'include');
31-
else
32-
incl_dir = fullfile(script_dir, '..', 'liblsl', 'include');
33-
end
34-
3527
disp('Building mex files. This may take a few minutes.');
3628
binarypath = fullfile(script_dir, 'bin');
3729
cd(binarypath);
@@ -40,7 +32,7 @@
4032
[~, base, ~] = fileparts(f.name);
4133
targetstats = dir([base, ext]);
4234
if isempty(targetstats) || f.datenum > targetstats.datenum
43-
mex(['-I', incl_dir], '-L.', libs{:}, ['../mex/', f.name]);
35+
mex(['-I', lsl_include_dir], '-L.', libs{:}, ['../mex/', f.name]);
4436
else
4537
disp([base, ext, ' up to date']);
4638
end

lsl_get_dll.m

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function lsl_fname = lsl_get_dll(binarypath, debugging)
1+
function [lsl_fname, lsl_include_dir] = lsl_get_dll(binarypath, debugging)
22
% Search for the lsl library
33
% [lsl_fname] = lsl_get_dll(binarypath, debugging)
44
%
@@ -11,27 +11,25 @@
1111
% Out:
1212
% lsl_fname : the filename of the library
1313

14+
script_dir = fileparts(mfilename('fullpath'));
1415
if ~exist('binarypath','var') || isempty(binarypath)
15-
binarypath = fullfile(fileparts(mfilename('fullpath')), 'bin');
16+
binarypath = fullfile(script_dir, 'bin');
1617
end
1718
if ~exist('debugging','var') || isempty(debugging)
1819
debugging = false;
1920
end
2021

2122
if ispc
23+
prefix = '';
2224
ext = '.dll';
2325
elseif ismac
26+
prefix = 'lib';
2427
ext = '.dylib';
2528
elseif isunix
29+
prefix = 'lib';
2630
ext = '.so';
2731
else
28-
error('Your operating system is not supported by this version of the lab streaming layer API.');
29-
end
30-
31-
if contains(computer,'64')
32-
bitness = '64';
33-
else
34-
bitness = '32';
32+
error('Operating system not recognized. Cannot identify liblsl binaries.');
3533
end
3634

3735
if debugging
@@ -40,22 +38,25 @@
4038
debug = '';
4139
end
4240

43-
so_fname = sprintf('liblsl%s%s%s', bitness, debug, ext);
44-
lsl_fname = fullfile(binarypath, so_fname);
41+
so_fname = sprintf('%slsl%s%s', prefix, debug, ext);
4542

43+
% First check ./bin/ for the shared object.
44+
% Then check the sister liblsl/build/install/bin folder.
45+
% Finally, check other platform-dependent locations.
46+
lsl_fname = fullfile(binarypath, so_fname);
4647
if ~exist(lsl_fname, 'file')
47-
if ispc
48-
new_sopath = fullfile(binarypath, 'lsl.dll');
49-
elseif ismac && exist(fullfile(binarypath, 'liblsl.dylib'), 'file')
50-
new_sopath = fullfile(binarypath, 'liblsl.dylib');
51-
elseif exist('/usr/lib/liblsl.so', 'file')
52-
new_sopath = fullfile('/usr/lib/liblsl.so');
53-
else
54-
new_sopath = fullfile('/usr/lib/', so_fname);
48+
if exist(fullfile(script_dir, '..', 'liblsl', 'build', 'install', 'bin', so_fname), 'file')
49+
lsl_fname = fullfile(script_dir, '..', 'liblsl', 'build', 'install', 'bin', so_fname);
50+
lsl_include_dir = fullfile(script_dir, '..', 'liblsl', 'build', 'install', 'include');
51+
elseif ispc
52+
% TODO: Anywhere else to check on PC?
53+
elseif ismac
54+
% TODO: After liblsl gets a homebrew distribution, check there.
55+
elseif exist(fullfile('/usr/lib', so_fname), 'file')
56+
% Linux: Check /usr/lib
57+
lsl_fname = fullfile('/usr/lib', so_fname);
58+
lsl_include_dir = '/usr/include';
5559
end
56-
if exist(new_sopath, 'file')
57-
lsl_fname = new_sopath;
58-
end %if
5960
end %if
6061

6162
if ~exist(lsl_fname,'file')
@@ -70,13 +71,24 @@
7071
elseif ismac
7172
liblsl_url_fname = ['liblsl-' LIBLSL_VER '-OSX_amd64.tar.bz2'];
7273
elseif isunix
73-
liblsl_url_fname = ['liblsl-' LIBLSL_VER '-focal_amd64.deb'];
74+
% Check (xenial vs) bionic vs focal
75+
filetext = fileread('/etc/lsb-release');
76+
expr = '[^\n]*DISTRIB_CODENAME=(?<code>\w+)[^\n]*';
77+
res = regexp(filetext,expr,'names');
78+
liblsl_url_fname = ['liblsl-' LIBLSL_VER '-' res.code '_amd64.deb'];
7479
end
7580
try
7681
websave(fullfile(binarypath, liblsl_url_fname),...
7782
[liblsl_url liblsl_url_fname]);
7883
catch ME
79-
disp(['Unable to download ' liblsl_url]);
84+
disp(['Unable to download ' liblsl_url liblsl_url_fname]);
85+
if isunix
86+
extra_step = 'install it';
87+
else
88+
extra_step = ['extract it to ' fullfile(binarypath, 'liblsl_archive')];
89+
end
90+
disp(['You will have to manually download a liblsl release from https://github.com/sccn/liblsl/releases and ' extra_step ...
91+
' or build liblsl yourself, before reattempting to build the mex files.']);
8092
rethrow(ME);
8193
end
8294
if ispc
@@ -86,6 +98,9 @@
8698
copyfile(fullfile(binarypath, 'liblsl_archive', 'bin', 'lsl.dll'), lsl_fname);
8799
copyfile(fullfile(binarypath, 'liblsl_archive', 'lib', 'lsl.lib'),...
88100
fullfile(binarypath, 'lsl.lib'));
101+
lsl_include_dir = fullfile(binarypath, 'include');
102+
copyfile(fullfile(binarypath, 'liblsl_archive', 'include'), lsl_include_dir);
103+
rmdir(fullfile(binarypath, 'liblsl_archive'));
89104
elseif ismac
90105
% Use system tar because Matlab untar does not preserve symlinks.
91106
mkdir(fullfile(binarypath, 'liblsl_archive'));
@@ -94,11 +109,11 @@
94109
dylib_list = dir(fullfile(binarypath, '*.dylib'));
95110
[~, lib_ix] = min(cellfun(@length, {dylib_list.name}));
96111
lsl_fname = fullfile(dylib_list(lib_ix).folder, dylib_list(lib_ix).name);
112+
lsl_include_dir = fullfile(binarypath, 'include');
113+
copyfile(fullfile(binarypath, 'liblsl_archive', 'include'), lsl_include_dir);
114+
rmdir(fullfile(binarypath, 'liblsl_archive'));
97115
elseif isunix
98-
error(['liblsl debian package must be installed manually:', ...
116+
error(['Reattempt build after manual installation of liblsl debian package:', ...
99117
' sudo dpkg -i ' fullfile(binarypath, liblsl_url_fname)]);
100118
end
101119
end
102-
103-
end
104-

0 commit comments

Comments
 (0)