On Windows,
instead of executing "reg query" it's much simpler and more robust
to use cmake's built in registry query.
Remove unused variables. Significantly reduces amount of code in
function.
Parsing the version file is much faster than running Matlab.
It also improves reliability as Matlab silently quits or hangs
in some misconfigured environments.
For old Matlab, falls back to running Matlab.
also refactor file(STRINGS => file(READ as whole file was read anyway
Fixes: #25209
Add the `SYSTEM` flag for include directories provided by MATLAB, as the
the pragmas used are not compatible with GCC and generate a warning.
Fixes: #24166
this last resort check is for some HPC with "module load matlab"
not enacted that fail to catch in earlier checks. That would
error CMake configure even if find_package(Matlab) is not REQUIRED
Fix logic added by commit 7fdd5128b1 (FindMatlab: Fix version selection
if a version is given, 2021-07-02, v3.22.0-rc1~66^2). Ensure that
`_list_index` is always initialized to -1, akin to `list(FIND)` not
finding a match.
Issue: #22377
Recent MCR versions have the following `VersionInfo.xml`:
<version>9.11.0.1837725</version>
Fix parsing of multiple digits in the version components
so that we do not mistake this for version `9.1`.
On Windows, when FindMatlab.cmake searches the registry for installed Matlab versions, it sorts these versions alphabetically.
Since Matlab 2021a (version 9.10) came out this became a problem as now version 9.10 is placed after 9.1 instead of after a higher version less than 9.10.
The result is that FindMatlab doesn't return the highest version by default.
This fix uses the natural sort comparison which was introduced in CMake 3.18.
In commit bda5e2ac8f (FindMatlab: Only include engine and dataarray
libraries if they are found, 2020-12-11, v3.20.0-rc1~297^2~1) we fixed
the imported target to contain optional libraries only if they are
found. Do the same for `Matlab_LIBRARIES`.
An "unknown" version does not always mean an old version. Setting this
macro by mistake does not result in a compilation error, but not setting
it does. I had this error when compiling from a user that does not have
a matlab license.