You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FindThirdPartyMatlab.cmake 4.3KB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # ---------------------------------
  2. # Finds Matlab toolkit
  3. # ---------------------------------
  4. GET_PROPERTY(OV_PRINTED GLOBAL PROPERTY OV_TRIED_ThirdPartyMatlab)
  5. # Clear cached variables, otherwise repeated builds lead to trouble
  6. SET(Matlab_EXECUTABLE "Matlab_EXECUTABLE-NOTFOUND")
  7. SET(Matlab_INCLUDE "Matlab_INCLUDE-NOTFOUND")
  8. SET(Matlab_ROOT "Matlab_ROOT-NOTFOUND")
  9. # See if we can locate the matlab executable
  10. IF (WIN32)
  11. # Unfortunately there doesn't seem to be a neat way to discover both 32 and 64bit matlabs
  12. IF("${CMAKE_SIZEOF_VOID_P}" EQUAL "8")
  13. SET(MATLAB_PATHS "C:/Program Files/MATLAB/r2015b/bin")
  14. ELSEIF("${CMAKE_SIZEOF_VOID_P}" EQUAL "4")
  15. SET(MATLAB_PATHS "C:/Program Files (x86)/MATLAB/r2013b/bin")
  16. ELSE()
  17. SET(MATLAB_PATHS "")
  18. ENDIF()
  19. # Ignore path on the first call, if that fails, try defaults
  20. FIND_PROGRAM(Matlab_EXECUTABLE MATLAB PATHS ${MATLAB_PATHS} NO_DEFAULT_PATH)
  21. FIND_PROGRAM(Matlab_EXECUTABLE MATLAB)
  22. ENDIF(WIN32)
  23. IF (UNIX)
  24. FIND_PROGRAM(Matlab_EXECUTABLE matlab)
  25. IF(NOT Matlab_EXECUTABLE)
  26. # Alternative way to try to find matlab
  27. FILE(GLOB_RECURSE Executable_Candidates1 "/usr/local/matlab*/matlab")
  28. FILE(GLOB_RECURSE Executable_Candidates2 "/usr/local/MATLAB*/matlab")
  29. SET(Executable_Candidates ${Executable_Candidates1} ${Executable_Candidates2})
  30. IF(Executable_Candidates)
  31. LIST(GET Executable_Candidates 0 Matlab_EXECUTABLE)
  32. ENDIF()
  33. ENDIF()
  34. ENDIF(UNIX)
  35. # Figure out the paths to libs and includes
  36. IF(Matlab_EXECUTABLE)
  37. # OV_PRINT(OV_PRINTED "Have Matlab_EXECUTABLE ${Matlab_EXECUTABLE}")
  38. # Try relative to the executable path
  39. GET_FILENAME_COMPONENT(Matlab_ROOT ${Matlab_EXECUTABLE} PATH)
  40. IF(Matlab_ROOT)
  41. # OV_PRINT(OV_PRINTED "Have Matlab_ROOT ${Matlab_ROOT}")
  42. SET(Matlab_ROOT ${Matlab_ROOT}/../)
  43. # OV_PRINT(OV_PRINTED " -> ${Matlab_ROOT}")
  44. FIND_PATH(Matlab_INCLUDE "mex.h" PATHS ${Matlab_ROOT}/extern/include ${Matlab_ROOT}/extern/include/extern)
  45. ENDIF()
  46. # matlab executable path might have been pointing to a symbolic link elsewhere, try something else
  47. IF((NOT Matlab_INCLUDE) AND UNIX)
  48. EXECUTE_PROCESS(COMMAND matlab -e COMMAND grep "^MATLAB=" COMMAND sed "s/^MATLAB=//g" COMMAND tr "\n" "/"
  49. OUTPUT_VARIABLE Matlab_ROOT)
  50. FIND_PATH(Matlab_INCLUDE "mex.h" PATHS ${Matlab_ROOT}/extern/include ${Matlab_ROOT}/extern/include/extern)
  51. ENDIF()
  52. IF(Matlab_INCLUDE)
  53. # OV_PRINT(OV_PRINTED "Have Matlab_INCLUDE ${Matlab_INCLUDE}")
  54. IF(UNIX)
  55. SET(Matlab_LIBRARIES mex mx eng)
  56. IF(CMAKE_SIZEOF_VOID_P EQUAL 4)
  57. SET(Matlab_LIB_DIRECTORIES ${Matlab_ROOT}/bin/glnx86)
  58. ELSE()
  59. SET(Matlab_LIB_DIRECTORIES ${Matlab_ROOT}/bin/glnxa64)
  60. ENDIF()
  61. ENDIF(UNIX)
  62. IF(WIN32)
  63. SET(Matlab_LIBRARIES libmex libmx libeng) #mclmcrrt
  64. IF(CMAKE_SIZEOF_VOID_P EQUAL 4)
  65. SET(Matlab_LIB_DIRECTORIES ${Matlab_ROOT}/extern/lib/win32/microsoft)
  66. ELSE()
  67. SET(Matlab_LIB_DIRECTORIES ${Matlab_ROOT}/extern/lib/win64/microsoft)
  68. ENDIF()
  69. # for delayed importation on windows
  70. TARGET_LINK_LIBRARIES(${PROJECT_NAME} Delayimp )
  71. SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES LINK_FLAGS "/DELAYLOAD:libeng.dll /DELAYLOAD:libmx.dll")
  72. # /DELAYLOAD:libmex.dll /DELAYLOAD:mclmcrrt.dll --> useless, no import
  73. ENDIF(WIN32)
  74. SET(Matlab_FOUND TRUE)
  75. ENDIF()
  76. ENDIF(Matlab_EXECUTABLE)
  77. IF(Matlab_FOUND)
  78. OV_PRINT(OV_PRINTED " Found Matlab in [${Matlab_ROOT}]")
  79. SET(Matlab_LIB_FOUND TRUE)
  80. INCLUDE_DIRECTORIES(${Matlab_INCLUDE})
  81. FOREACH(Matlab_LIB ${Matlab_LIBRARIES})
  82. SET(Matlab_LIB1 "Matlab_LIB1-NOTFOUND")
  83. FIND_LIBRARY(Matlab_LIB1 NAMES ${Matlab_LIB} PATHS ${Matlab_LIB_DIRECTORIES} NO_DEFAULT_PATH)
  84. FIND_LIBRARY(Matlab_LIB1 NAMES ${Matlab_LIB})
  85. IF(Matlab_LIB1)
  86. OV_PRINT(OV_PRINTED " [ OK ] Third party lib ${Matlab_LIB1}")
  87. TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${Matlab_LIB1})
  88. ELSE(Matlab_LIB1)
  89. OV_PRINT(OV_PRINTED " [FAILED] Third party lib ${Matlab_LIB}")
  90. SET(Matlab_LIB_FOUND FALSE)
  91. ENDIF(Matlab_LIB1)
  92. ENDFOREACH(Matlab_LIB)
  93. IF(Matlab_LIB_FOUND)
  94. ADD_DEFINITIONS(-DTARGET_HAS_ThirdPartyMatlab)
  95. ELSE(Matlab_LIB_FOUND)
  96. OV_PRINT(OV_PRINTED " FAILED to find Matlab Libs, the plugins won't be built. You need a valid MATLAB installation, bitness matching platform target ${PLATFORM_TARGET}.")
  97. ENDIF(Matlab_LIB_FOUND)
  98. ELSE()
  99. OV_PRINT(OV_PRINTED " FAILED to find Matlab (optional) ...")
  100. ENDIF()
  101. SET_PROPERTY(GLOBAL PROPERTY OV_TRIED_ThirdPartyMatlab "Yes")