Skip to content

Commit 587bbdc

Browse files
committed
Clean up initialization
1 parent e27cc5b commit 587bbdc

File tree

3 files changed

+38
-29
lines changed

3 files changed

+38
-29
lines changed

src/MATLAB.jl

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,15 @@ include("matstr.jl")
4848

4949
function __init__()
5050

51-
# load libraries
51+
# initialize library paths
52+
53+
lib_path = matlab_lib_path()
5254

53-
_libmx[] = Libdl.dlopen(joinpath(matlab_lib_path, "libmx"), Libdl.RTLD_GLOBAL)
54-
_libmat[] = Libdl.dlopen(joinpath(matlab_lib_path, "libmat"), Libdl.RTLD_GLOBAL)
55-
_libeng[] = Libdl.dlopen(joinpath(matlab_lib_path, "libeng"), Libdl.RTLD_GLOBAL)
55+
# load libraries
56+
57+
_libmx[] = Libdl.dlopen(joinpath(lib_path, "libmx"), Libdl.RTLD_GLOBAL)
58+
_libmat[] = Libdl.dlopen(joinpath(lib_path, "libmat"), Libdl.RTLD_GLOBAL)
59+
_libeng[] = Libdl.dlopen(joinpath(lib_path, "libeng"), Libdl.RTLD_GLOBAL)
5660

5761

5862
# load functions to access mxArray

src/engine.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# Session open & close
66
#
77
###########################################################
8+
const default_startcmd = matlab_startcmd() * " -nosplash"
89

910
# 64 K buffer should be sufficient to store the output text in most cases
1011
const default_output_buffer_size = 64 * 1024
@@ -21,7 +22,6 @@ type MSession
2122
is_windows() && ccall(engfunc(:engSetVisible ), Cint, (Ptr{Void}, Cint), ep, 0)
2223

2324
buf = Array(UInt8, bufsize)
24-
2525
if bufsize > 0
2626
bufptr = pointer(buf)
2727
ccall(engfunc(:engOutputBuffer), Cint, (Ptr{Void}, Ptr{UInt8}, Cint),

src/mxbase.jl

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
# Determine MATLAB library path and provide facilities to load libraries with
22
# this path
33

4-
function get_paths()
4+
function matlab_home_path()
55
matlab_home = get(ENV, "MATLAB_HOME", "")
6-
76
if matlab_home == ""
87
if is_linux()
98
matlab_home = dirname(dirname(realpath(chomp(readstring(`which matlab`)))))
@@ -12,38 +11,30 @@ function get_paths()
1211
if isdir(default_dir)
1312
dirs = readdir(default_dir)
1413
filter!(app -> ismatch(r"^MATLAB_R[0-9]+[ab]\.app$", dirs), dirs)
14+
if ~isempty(dirs)
15+
matlab_home = joinpath(default_dir, maximum(dirs))
16+
end
1517
end
1618
elseif is_windows()
1719
default_dir = Sys.WORD_SIZE == 32 ? "C:\\Program Files (x86)\\MATLAB" : "C:\\Program Files\\MATLAB"
1820
if isdir(default_dir)
1921
dirs = readdir(default_dir)
2022
filter!(dir -> ismatch(r"^R[0-9]+[ab]$", dir), dirs)
23+
if ~isempty(dirs)
24+
matlab_home = joinpath(default_dir, maximum(dirs))
25+
end
2126
end
2227
end
2328
end
24-
25-
if ~isempty(dirs)
26-
matlab_home = joinpath(default_dir, maximum(dirs))
27-
end
2829
if matlab_home == ""
2930
error("The MATLAB path could not be found. Set the MATLAB_HOME environmental variable to specify the MATLAB path.")
3031
end
32+
return matlab_home
33+
end
3134

32-
if !is_windows()
33-
default_startcmd = joinpath(matlab_home, "bin", "matlab")
34-
if !isfile(default_startcmd)
35-
error("The MATLAB path is invalid. Set the MATLAB_HOME evironmental variable to the MATLAB root.")
36-
end
37-
default_startcmd = "exec $(Base.shell_escape(default_startcmd))"
38-
elseif is_windows()
39-
default_startcmd = joinpath(matlab_home, "bin", (Sys.WORD_SIZE == 32 ? "win32" : "win64"), "MATLAB.exe")
40-
if !isfile(default_startcmd)
41-
error("The MATLAB path is invalid. Set the MATLAB_HOME evironmental variable to the MATLAB root.")
42-
end
43-
end
44-
default_startcmd *= " -nosplash"
45-
46-
# Get path to MATLAB libraries
35+
function matlab_lib_path()
36+
# get path to MATLAB libraries
37+
matlab_home = matlab_home_path()
4738
matlab_lib_dir = if is_linux()
4839
Sys.WORD_SIZE == 32 ? "glnx86" : "glnxa64"
4940
elseif is_apple()
@@ -55,11 +46,25 @@ function get_paths()
5546
if !isdir(matlab_lib_path)
5647
error("The MATLAB library path could not be found.")
5748
end
58-
59-
return default_startcmd, matlab_lib_path
49+
return matlab_lib_path
6050
end
6151

62-
default_startcmd, matlab_lib_path = get_paths()
52+
function matlab_startcmd()
53+
matlab_home = matlab_home_path()
54+
if !is_windows()
55+
default_startcmd = joinpath(matlab_home, "bin", "matlab")
56+
if !isfile(default_startcmd)
57+
error("The MATLAB path is invalid. Set the MATLAB_HOME evironmental variable to the MATLAB root.")
58+
end
59+
default_startcmd = "exec $(Base.shell_escape(default_startcmd))"
60+
elseif is_windows()
61+
default_startcmd = joinpath(matlab_home, "bin", (Sys.WORD_SIZE == 32 ? "win32" : "win64"), "MATLAB.exe")
62+
if !isfile(default_startcmd)
63+
error("The MATLAB path is invalid. Set the MATLAB_HOME evironmental variable to the MATLAB root.")
64+
end
65+
end
66+
return default_startcmd
67+
end
6368

6469
# helper library access function
6570

0 commit comments

Comments
 (0)