|
| 1 | +import Libdl |
| 2 | + |
| 3 | +const depsfile = joinpath(@__DIR__, "deps.jl") |
| 4 | + |
| 5 | +# Determine MATLAB library path and provide facilities to load libraries with |
| 6 | +# this path |
| 7 | + |
| 8 | +function find_matlab_homepath() |
| 9 | + matlab_home = get(ENV, "MATLAB_HOME", nothing) |
| 10 | + if isnothing(matlab_home) |
| 11 | + matlab_exe = Sys.which("matlab") |
| 12 | + matlab_home = !isnothing(matlab_exe) ? dirname(dirname(matlab_exe)) : nothing |
| 13 | + if isnothing(matlab_home) |
| 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_home = 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_home = joinpath(default_dir, maximum(dirs)) |
| 30 | + end |
| 31 | + end |
| 32 | + end |
| 33 | + end |
| 34 | + end |
| 35 | + if isnothing(matlab_home) |
| 36 | + return nothing |
| 37 | + else |
| 38 | + @info("Found MATLAB home path at $matlab_home") |
| 39 | + return matlab_home |
| 40 | + end |
| 41 | +end |
| 42 | + |
| 43 | +function find_matlab_libpath(matlab_home) |
| 44 | + # get path to MATLAB libraries |
| 45 | + matlab_lib_dir = if Sys.islinux() |
| 46 | + Sys.WORD_SIZE == 32 ? "glnx86" : "glnxa64" |
| 47 | + elseif Sys.isapple() |
| 48 | + Sys.WORD_SIZE == 32 ? "maci" : "maci64" |
| 49 | + elseif Sys.iswindows() |
| 50 | + Sys.WORD_SIZE == 32 ? "win32" : "win64" |
| 51 | + end |
| 52 | + matlab_libpath = joinpath(matlab_home, "bin", matlab_lib_dir) |
| 53 | + if !isdir(matlab_libpath) |
| 54 | + @warn("The MATLAB library path could not be found.") |
| 55 | + end |
| 56 | + return matlab_libpath |
| 57 | +end |
| 58 | + |
| 59 | +function find_matlab_cmd(matlab_home) |
| 60 | + if !Sys.iswindows() |
| 61 | + matlab_cmd = joinpath(matlab_home, "bin", "matlab") |
| 62 | + if !isfile(matlab_cmd) |
| 63 | + @warn("The MATLAB path is invalid. Ensure the \"MATLAB_HOME\" evironmental variable to the MATLAB root directory.") |
| 64 | + end |
| 65 | + matlab_cmd = "exec $(Base.shell_escape(matlab_cmd))" |
| 66 | + elseif Sys.iswindows() |
| 67 | + matlab_cmd = joinpath(matlab_home, "bin", (Sys.WORD_SIZE == 32 ? "win32" : "win64"), "MATLAB.exe") |
| 68 | + if !isfile(matlab_cmd) |
| 69 | + error("The MATLAB path is invalid. Ensure the \"MATLAB_HOME\" evironmental variable to the MATLAB root directory.") |
| 70 | + end |
| 71 | + end |
| 72 | + return matlab_cmd |
| 73 | +end |
| 74 | + |
| 75 | +matlab_homepath = find_matlab_homepath() |
| 76 | + |
| 77 | +if !isnothing(matlab_homepath) |
| 78 | + matlab_libpath = find_matlab_libpath(matlab_homepath) |
| 79 | + matlab_cmd = find_matlab_cmd(matlab_homepath) |
| 80 | + libmx_size = filesize(Libdl.dlpath(joinpath(matlab_libpath, "libmx"))) |
| 81 | + open(depsfile, "w") do io |
| 82 | + println(io, |
| 83 | + """ |
| 84 | + # This file is automatically generated, do not edit. |
| 85 | +
|
| 86 | + function check_deps() |
| 87 | + if libmx_size != filesize(Libdl.dlpath(joinpath(matlab_libpath, "libmx"))) |
| 88 | + error("MATLAB library has changed, re-run Pkg.build(\\\"MATLAB\\\")") |
| 89 | + end |
| 90 | + end |
| 91 | + """ |
| 92 | + ) |
| 93 | + println(io, "const matlab_libpath = \"$(escape_string(matlab_libpath))\"") |
| 94 | + println(io, "const matlab_cmd = \"$(escape_string(matlab_cmd))\"") |
| 95 | + println(io, "const libmx_size = $libmx_size") |
| 96 | + end |
| 97 | +elseif get(ENV, "JULIA_REGISTRYCI_AUTOMERGE", nothing) == "true" || get(ENV, "CI", nothing) == "true" |
| 98 | + # We need to be able to install and load this package without error for |
| 99 | + # Julia's registry AutoMerge to work, so we just use dummy values. |
| 100 | + # Similarly we want to also be able to install and load this package for CI. |
| 101 | + matlab_libpath = "" |
| 102 | + matlab_cmd = "" |
| 103 | + libmx_size = 0 |
| 104 | + |
| 105 | + open(depsfile, "w") do io |
| 106 | + println(io, |
| 107 | + """ |
| 108 | + # This file is automatically generated, do not edit. |
| 109 | +
|
| 110 | + check_deps() = nothing |
| 111 | + """ |
| 112 | + ) |
| 113 | + println(io, "const matlab_libpath = \"$(escape_string(matlab_libpath))\"") |
| 114 | + println(io, "const matlab_cmd = \"$(escape_string(matlab_cmd))\"") |
| 115 | + println(io, "const libmx_size = $libmx_size") |
| 116 | + end |
| 117 | +else |
| 118 | + error("MATLAB cannot be found. Set the \"MATLAB_HOME\" environment variable to the MATLAB root directory and re-run Pkg.build(\"MATLAB\").") |
| 119 | +end |
0 commit comments