Skip to content

Commit

Permalink
Simplified MATLAB detection
Browse files Browse the repository at this point in the history
  • Loading branch information
vsoftco committed Mar 26, 2024
1 parent 9fb8b15 commit 246f771
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 123 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ option(SANITIZE "Enable code sanitizing (only for gcc/clang)" OFF)
include(cmake/qpp_eigen3.cmake)

# Dependencies, do not modify unless you know what you're doing
include(cmake/qpp_dependencies.cmake)
include(cmake/qpp_MATLAB.cmake)

# Dependencies, do not modify unless you know what you're doing
include(cmake/qpp_MATLAB.cmake)
include(cmake/qpp_dependencies.cmake)

# Unit testing
add_subdirectory(${CMAKE_SOURCE_DIR}/unit_tests/ EXCLUDE_FROM_ALL SYSTEM)
Expand Down
21 changes: 2 additions & 19 deletions INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,8 @@ as [Windows](https://www.microsoft.com/en-us/windows).
Python 3 wrapper
- [MATLAB](https://www.mathworks.com/products/matlab/) compiler shared
libraries and include header files, in case you want to enable
interoperability with MATLAB. If enabled, allows applications build with
interoperability with MATLAB. If enabled, allows applications built with
**Quantum++** to save/load **Quantum++** matrices and vectors to/from MATLAB.
The locations of the MATLAB compiler shared libraries and header files are
platform-specific, e.g., under Linux, they may be located under
`/usr/local/MATLAB/R2021a/bin/glnxa64` and
`/usr/local/MATLAB/R2021a/extern/include`, respectively. On your platform the
locations may of course differ.

---

Expand Down Expand Up @@ -105,18 +100,6 @@ their corresponding `CMakeLists.txt` via `findpackage(qpp ...)`.
| | | |
| `SANITIZE` | `ON/OFF` [`OFF` by default] | Enable code sanitizing (only for gcc/clang) |

If `QPP_MATLAB=ON` and the system could not detect your MATLAB installation,
you can manually specify the path to MATLAB's installation directory via the
additional CMake argument

MATLAB_INSTALL_DIR=/path/to/MATLAB

If you are still receiving errors, you can manually specify the path to
MATLAB's required libraries and header files via the additional arguments

MATLAB_LIB_DIR=/path/to/MATLAB/libs
MATLAB_INCLUDE_DIR=/path/to/MATLAB/headers

---

## Installing Quantum++
Expand Down Expand Up @@ -274,7 +257,7 @@ with [MATLAB](https://www.mathworks.com/products/matlab/) support, please add
the
location of
`libmx.dll` and `libmat.dll` (the `.dll` **and not** the `.lib` files) to
your `PATH` environment variable. In our case they are located
your `PATH` environment variable. For example, on our platform they are located
under `C:\Program Files\MATLAB\R2021a\bin\win64`.

---
Expand Down
2 changes: 1 addition & 1 deletion cmake/examples.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ add_custom_target(examples COMMENT "Examples")
foreach(file ${EXAMPLE_FILES})
get_filename_component(TARGET_NAME ${file} NAME_WE)
# Do not build "examples/matlab_io.cpp" if there's no MATLAB support
if(${TARGET_NAME} STREQUAL "matlab_io" AND NOT BUILD_WITH_MATLAB)
if(${TARGET_NAME} STREQUAL "matlab_io" AND NOT MATLAB_FOUND)
continue()
endif()
add_executable(${TARGET_NAME} EXCLUDE_FROM_ALL ${file})
Expand Down
106 changes: 6 additions & 100 deletions cmake/qpp_MATLAB.cmake
Original file line number Diff line number Diff line change
@@ -1,107 +1,13 @@
# MATLAB support, disabled by default
option(QPP_MATLAB "MATLAB support" OFF)
if(${QPP_MATLAB})
message(STATUS "Detecting MATLAB")
# Try to find it automatically
find_package(
Matlab
OPTIONAL_COMPONENTS MX_LIBRARY MAT_LIBRARY
QUIET)
message(STATUS "Detecting MATLAB...")
find_package(Matlab REQUIRED COMPONENTS MX_LIBRARY MAT_LIBRARY)
if(MATLAB_FOUND)
message(STATUS "Detecting MATLAB - done (in ${Matlab_ROOT_DIR})")
include_directories(SYSTEM ${Matlab_INCLUDE_DIRS})
if(WIN32)
if(MSVC)
set(MATLAB_LIB_DIR
"${Matlab_ROOT_DIR}/extern/lib/win64/microsoft"
CACHE PATH "Custom path to MATLAB lib directory")
elseif(MINGW)
set(MATLAB_LIB_DIR
"${Matlab_ROOT_DIR}/extern/lib/win64/mingw64"
CACHE PATH "Custom path to MATLAB lib directory")
else()
message(FATAL_ERROR "Platform not supported, aborting.")
endif()
elseif(UNIX AND NOT APPLE)
set(MATLAB_LIB_DIR
"${Matlab_ROOT_DIR}/bin/glnxa64"
CACHE PATH "Custom path to MATLAB lib directory")
elseif(APPLE)
if(${CMAKE_HOST_SYSTEM_PROCESSOR} MATCHES "arm64")
message(
FATAL_ERROR
"arm64 architecture is not (yet) supported by MATLAB, aborting.")
endif()
set(MATLAB_LIB_DIR
"${Matlab_ROOT_DIR}/bin/maci64"
CACHE PATH "Custom path to MATLAB lib directory")
else()
message(FATAL_ERROR "Platform not supported, aborting.")
endif()
link_directories(${MATLAB_LIB_DIR})
add_compile_definitions(QPP_MATLAB)
set(BUILD_WITH_MATLAB TRUE)
else() # Location manually specified
set(MATLAB_INSTALL_DIR
""
CACHE PATH "Custom path to MATLAB installation")
if(IS_DIRECTORY ${MATLAB_INSTALL_DIR})
# MATLAB include files
set(MATLAB_INCLUDE_DIR
"${MATLAB_INSTALL_DIR}/extern/include"
CACHE PATH "Custom path to MATLAB include directory")
if(IS_DIRECTORY ${MATLAB_INCLUDE_DIR})
include_directories(SYSTEM ${MATLAB_INCLUDE_DIR})
else()
message(FATAL_ERROR "Possibly corrupted MATLAB include headers")
endif()
# MATLAB linker files
if(WIN32)
if(MSVC)
set(MATLAB_LIB_DIR
"${MATLAB_INSTALL_DIR}/extern/lib/win64/microsoft"
CACHE PATH "Custom path to MATLAB lib directory")
elseif(MINGW64)
set(MATLAB_LIB_DIR
"${MATLAB_INSTALL_DIR}/extern/lib/win64/mingw64"
CACHE PATH "Custom path to MATLAB lib directory")
else()
message(FATAL_ERROR "Platform not supported, aborting.")
endif()
elseif(UNIX AND NOT APPLE)
set(MATLAB_LIB_DIR
"${MATLAB_INSTALL_DIR}/bin/glnxa64"
CACHE PATH "Custom path to MATLAB lib directory")
elseif(APPLE)
set(MATLAB_LIB_DIR
"${MATLAB_INSTALL_DIR}/bin/maci64"
CACHE PATH "Custom path to MATLAB lib directory")
else()
message(FATAL_ERROR "Platform not supported, aborting.")
endif()
if(IS_DIRECTORY ${MATLAB_LIB_DIR})
link_directories(${MATLAB_LIB_DIR})
else()
message(FATAL_ERROR "Possibly corrupted MATLAB compiler libraries")
endif()
# Everything is OK, inject definition (as #define) in the source
message(STATUS "Detecting MATLAB - done (in ${MATLAB_INSTALL_DIR})")
add_compile_definitions(QPP_MATLAB)
set(BUILD_WITH_MATLAB TRUE)
else()
message(FATAL_ERROR "Could not detect MATLAB, aborting")
endif()
endif()
# MATLAB linking dependencies to be injected in the main CMakeLists.txt
if(${BUILD_WITH_MATLAB})
if(WIN32)
if(MSVC)
set(QPP_MATLAB_LINK_DEPS libmx libmat)
elseif(MINGW)
set(QPP_MATLAB_LINK_DEPS mx mat)
endif()
else()
set(QPP_MATLAB_LINK_DEPS mx mat)
endif()
set(QPP_MATLAB_LINK_DEPS Matlab::mat Matlab::mx)
message(STATUS "Detected MATLAB in: ${Matlab_ROOT_DIR}")
else()
message(FATAL_ERROR "Could not detect MATLAB, aborting")
endif()
endif()
2 changes: 1 addition & 1 deletion unit_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ cmake_policy(SET CMP0076 NEW)
# Build all tests in ${TEST_FILES}
foreach(file ${TEST_FILES})
# Do not build "tests/MATLAB/matlab.cpp" if there's no MATLAB support
if(${file} STREQUAL "tests/MATLAB/matlab.cpp" AND NOT BUILD_WITH_MATLAB)
if(${file} STREQUAL "tests/MATLAB/matlab.cpp" AND NOT MATLAB_FOUND)
continue()
endif()
target_sources(${TARGET_NAME} PUBLIC ${file})
Expand Down

0 comments on commit 246f771

Please sign in to comment.