Skip to content

Commit c732f7d

Browse files
authored
Search standard directory for Linux systems (#197)
* Search standard directory for Linux systems * Normalize to LF line ending * Skip linked executables for the master matlab root directory
1 parent 08ca1ca commit c732f7d

File tree

1 file changed

+118
-109
lines changed

1 file changed

+118
-109
lines changed

deps/build.jl

Lines changed: 118 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -1,109 +1,118 @@
1-
import Libdl
2-
3-
const depsfile = joinpath(@__DIR__, "deps.jl")
4-
5-
function find_matlab_root()
6-
# Determine MATLAB library path and provide facilities to load libraries with this path
7-
matlab_root = get(ENV, "MATLAB_ROOT",
8-
get(ENV, "MATLAB_HOME", nothing))
9-
if isnothing(matlab_root)
10-
matlab_exe = Sys.which("matlab")
11-
if !isnothing(matlab_exe)
12-
matlab_root = dirname(dirname(matlab_exe))
13-
else
14-
if Sys.isapple()
15-
default_dir = "/Applications"
16-
if isdir(default_dir)
17-
dirs = readdir(default_dir)
18-
filter!(app -> occursin(r"^MATLAB_R[0-9]+[ab]\.app$", app), dirs)
19-
if !isempty(dirs)
20-
matlab_root = joinpath(default_dir, maximum(dirs))
21-
end
22-
end
23-
elseif Sys.iswindows()
24-
default_dir = Sys.WORD_SIZE == 32 ? "C:\\Program Files (x86)\\MATLAB" : "C:\\Program Files\\MATLAB"
25-
if isdir(default_dir)
26-
dirs = readdir(default_dir)
27-
filter!(dir -> occursin(r"^R[0-9]+[ab]$", dir), dirs)
28-
if !isempty(dirs)
29-
matlab_root = joinpath(default_dir, maximum(dirs))
30-
end
31-
end
32-
end
33-
end
34-
end
35-
!isnothing(matlab_root) && isdir(matlab_root) && @info("Detected MATLAB root folder at \"$matlab_root\"")
36-
return matlab_root
37-
end
38-
39-
function find_matlab_libpath(matlab_root)
40-
# get path to MATLAB libraries
41-
matlab_libdir = if Sys.islinux()
42-
Sys.WORD_SIZE == 32 ? "glnx86" : "glnxa64"
43-
elseif Sys.isapple()
44-
Sys.WORD_SIZE == 32 ? "maci" : "maci64"
45-
elseif Sys.iswindows()
46-
Sys.WORD_SIZE == 32 ? "win32" : "win64"
47-
end
48-
matlab_libpath = joinpath(matlab_root, "bin", matlab_libdir)
49-
isdir(matlab_libpath) && @info("Detected MATLAB library path at \"$matlab_libpath\"")
50-
return matlab_libpath
51-
end
52-
53-
function find_matlab_cmd(matlab_root)
54-
if Sys.iswindows()
55-
matlab_cmd = joinpath(matlab_root, "bin", (Sys.WORD_SIZE == 32 ? "win32" : "win64"), "matlab.exe")
56-
isfile(matlab_cmd) && @info("Detected MATLAB executable at \"$matlab_cmd\"")
57-
else
58-
matlab_exe = joinpath(matlab_root, "bin", "matlab")
59-
isfile(matlab_exe) && @info("Detected MATLAB executable at \"$matlab_exe\"")
60-
matlab_cmd = "exec $(Base.shell_escape(matlab_exe))"
61-
end
62-
return matlab_cmd
63-
end
64-
65-
matlab_root = find_matlab_root()
66-
67-
if !isnothing(matlab_root)
68-
matlab_libpath = find_matlab_libpath(matlab_root)
69-
matlab_cmd = find_matlab_cmd(matlab_root)
70-
libmx_size = filesize(Libdl.dlpath(joinpath(matlab_libpath, "libmx")))
71-
open(depsfile, "w") do io
72-
println(io,
73-
"""
74-
# This file is automatically generated, do not edit.
75-
76-
function check_deps()
77-
if libmx_size != filesize(Libdl.dlpath(joinpath(matlab_libpath, "libmx")))
78-
error("MATLAB library has changed, re-run Pkg.build(\\\"MATLAB\\\")")
79-
end
80-
end
81-
"""
82-
)
83-
println(io, "const matlab_libpath = \"$(escape_string(matlab_libpath))\"")
84-
println(io, "const matlab_cmd = \"$(escape_string(matlab_cmd))\"")
85-
println(io, "const libmx_size = $libmx_size")
86-
end
87-
elseif get(ENV, "JULIA_REGISTRYCI_AUTOMERGE", nothing) == "true" || get(ENV, "CI", nothing) == "true"
88-
# We need to be able to install and load this package without error for
89-
# Julia's registry AutoMerge to work, so we just use dummy values.
90-
# Similarly we want to also be able to install and load this package for CI.
91-
matlab_libpath = ""
92-
matlab_cmd = ""
93-
libmx_size = 0
94-
95-
open(depsfile, "w") do io
96-
println(io,
97-
"""
98-
# This file is automatically generated, do not edit.
99-
100-
check_deps() = nothing
101-
"""
102-
)
103-
println(io, "const matlab_libpath = \"$(escape_string(matlab_libpath))\"")
104-
println(io, "const matlab_cmd = \"$(escape_string(matlab_cmd))\"")
105-
println(io, "const libmx_size = $libmx_size")
106-
end
107-
else
108-
error("MATLAB cannot be found. Set the \"MATLAB_ROOT\" environment variable to the MATLAB root directory and re-run Pkg.build(\"MATLAB\").")
109-
end
1+
import Libdl
2+
3+
const depsfile = joinpath(@__DIR__, "deps.jl")
4+
5+
function find_matlab_root()
6+
# Determine MATLAB library path and provide facilities to load libraries with this path
7+
matlab_root = get(ENV, "MATLAB_ROOT",
8+
get(ENV, "MATLAB_HOME", nothing))
9+
if isnothing(matlab_root)
10+
matlab_exe = Sys.which("matlab")
11+
if !isnothing(matlab_exe) && !islink(matlab_exe) # guard against /usr/local
12+
matlab_root = dirname(dirname(matlab_exe))
13+
else
14+
if Sys.isapple()
15+
default_dir = "/Applications"
16+
if isdir(default_dir)
17+
dirs = readdir(default_dir)
18+
filter!(app -> occursin(r"^MATLAB_R[0-9]+[ab]\.app$", app), dirs)
19+
if !isempty(dirs)
20+
matlab_root = joinpath(default_dir, maximum(dirs))
21+
end
22+
end
23+
elseif Sys.iswindows()
24+
default_dir = Sys.WORD_SIZE == 32 ? "C:\\Program Files (x86)\\MATLAB" : "C:\\Program Files\\MATLAB"
25+
if isdir(default_dir)
26+
dirs = readdir(default_dir)
27+
filter!(dir -> occursin(r"^R[0-9]+[ab]$", dir), dirs)
28+
if !isempty(dirs)
29+
matlab_root = joinpath(default_dir, maximum(dirs))
30+
end
31+
end
32+
elseif Sys.islinux()
33+
default_dir = "/usr/local/MATLAB"
34+
if isdir(default_dir)
35+
dirs = readdir(default_dir)
36+
filter!(dir -> occursin(r"^R[0-9]+[ab]$", dir), dirs)
37+
if !isempty(dirs)
38+
matlab_root = joinpath(default_dir, maximum(dirs))
39+
end
40+
end
41+
end
42+
end
43+
end
44+
!isnothing(matlab_root) && isdir(matlab_root) && @info("Detected MATLAB root folder at \"$matlab_root\"")
45+
return matlab_root
46+
end
47+
48+
function find_matlab_libpath(matlab_root)
49+
# get path to MATLAB libraries
50+
matlab_libdir = if Sys.islinux()
51+
Sys.WORD_SIZE == 32 ? "glnx86" : "glnxa64"
52+
elseif Sys.isapple()
53+
Sys.WORD_SIZE == 32 ? "maci" : "maci64"
54+
elseif Sys.iswindows()
55+
Sys.WORD_SIZE == 32 ? "win32" : "win64"
56+
end
57+
matlab_libpath = joinpath(matlab_root, "bin", matlab_libdir)
58+
isdir(matlab_libpath) && @info("Detected MATLAB library path at \"$matlab_libpath\"")
59+
return matlab_libpath
60+
end
61+
62+
function find_matlab_cmd(matlab_root)
63+
if Sys.iswindows()
64+
matlab_cmd = joinpath(matlab_root, "bin", (Sys.WORD_SIZE == 32 ? "win32" : "win64"), "matlab.exe")
65+
isfile(matlab_cmd) && @info("Detected MATLAB executable at \"$matlab_cmd\"")
66+
else
67+
matlab_exe = joinpath(matlab_root, "bin", "matlab")
68+
isfile(matlab_exe) && @info("Detected MATLAB executable at \"$matlab_exe\"")
69+
matlab_cmd = "exec $(Base.shell_escape(matlab_exe))"
70+
end
71+
return matlab_cmd
72+
end
73+
74+
matlab_root = find_matlab_root()
75+
76+
if !isnothing(matlab_root)
77+
matlab_libpath = find_matlab_libpath(matlab_root)
78+
matlab_cmd = find_matlab_cmd(matlab_root)
79+
libmx_size = filesize(Libdl.dlpath(joinpath(matlab_libpath, "libmx")))
80+
open(depsfile, "w") do io
81+
println(io,
82+
"""
83+
# This file is automatically generated, do not edit.
84+
85+
function check_deps()
86+
if libmx_size != filesize(Libdl.dlpath(joinpath(matlab_libpath, "libmx")))
87+
error("MATLAB library has changed, re-run Pkg.build(\\\"MATLAB\\\")")
88+
end
89+
end
90+
"""
91+
)
92+
println(io, "const matlab_libpath = \"$(escape_string(matlab_libpath))\"")
93+
println(io, "const matlab_cmd = \"$(escape_string(matlab_cmd))\"")
94+
println(io, "const libmx_size = $libmx_size")
95+
end
96+
elseif get(ENV, "JULIA_REGISTRYCI_AUTOMERGE", nothing) == "true" || get(ENV, "CI", nothing) == "true"
97+
# We need to be able to install and load this package without error for
98+
# Julia's registry AutoMerge to work, so we just use dummy values.
99+
# Similarly we want to also be able to install and load this package for CI.
100+
matlab_libpath = ""
101+
matlab_cmd = ""
102+
libmx_size = 0
103+
104+
open(depsfile, "w") do io
105+
println(io,
106+
"""
107+
# This file is automatically generated, do not edit.
108+
109+
check_deps() = nothing
110+
"""
111+
)
112+
println(io, "const matlab_libpath = \"$(escape_string(matlab_libpath))\"")
113+
println(io, "const matlab_cmd = \"$(escape_string(matlab_cmd))\"")
114+
println(io, "const libmx_size = $libmx_size")
115+
end
116+
else
117+
error("MATLAB cannot be found. Set the \"MATLAB_ROOT\" environment variable to the MATLAB root directory and re-run Pkg.build(\"MATLAB\").")
118+
end

0 commit comments

Comments
 (0)