cmake_mex_wrapper 4.27 KB
Newer Older
1
2
3
4
5
6
7
# This file figures out where MATLAB is and then defines a macro, add_mex_function(name)
# which when called instructs CMake to build a mex file from a file called name.cpp.  Note
# that additional library dependencies can be added like this: add_mex_function(name lib1 dlib libetc).
# That is, just add more libraries after the name and they will be build into the mex file.

cmake_minimum_required(VERSION 2.8.4)

8
set(BUILDING_MATLAB_MEX_FILE true)
9
set(CMAKE_POSITION_INDEPENDENT_CODE True)
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

# Find MATLAB's include directory and needed libraries 
find_program(MATLAB_EXECUTABLE matlab PATHS
        "C:/Program Files/MATLAB/*/bin"
        "C:/Program Files (x86)/MATLAB/*/bin"
        )
# Resolve symbolic links to try and get the real path to the MATLAB executable
get_filename_component(MATLAB_EXECUTABLE ${MATLAB_EXECUTABLE} REALPATH)
# Now get MATLAB root directory
get_filename_component(MATLAB_HOME ${MATLAB_EXECUTABLE} PATH)
get_filename_component(MATLAB_HOME ${MATLAB_HOME} PATH)
set(MATLAB_LIB_FOLDERS
   "${MATLAB_HOME}/extern/lib/win64/microsoft"
   "${MATLAB_HOME}/bin/glnxa64"
   )
25
26
27
28
29
30
31
32
# If there is a MATLAB_HOME environment variable then look there as well.
if (DEFINED ENV{MATLAB_HOME})
    set(MATLAB_LIB_FOLDERS
        "$ENV{MATLAB_HOME}/extern/lib/win64/microsoft"
        "$ENV{MATLAB_HOME}/bin/glnxa64"
        ${MATLAB_LIB_FOLDERS}
    )
endif()
33
34
35
36
37
38
39
40
41
42
43
# Find the MATLAB libraries that need to get linked into the mex file
if (WIN32)
   find_library(MATLAB_MEX_LIBRARY libmex PATHS ${MATLAB_LIB_FOLDERS} )
   find_library(MATLAB_MX_LIBRARY  libmx  PATHS ${MATLAB_LIB_FOLDERS} )
   find_library(MATLAB_ENG_LIBRARY libeng PATHS ${MATLAB_LIB_FOLDERS} )
else()
   find_library(MATLAB_MEX_LIBRARY mex    PATHS ${MATLAB_LIB_FOLDERS} )
   find_library(MATLAB_MX_LIBRARY  mx     PATHS ${MATLAB_LIB_FOLDERS} )
   find_library(MATLAB_ENG_LIBRARY eng    PATHS ${MATLAB_LIB_FOLDERS} )
endif()
set(MATLAB_LIBRARIES ${MATLAB_MEX_LIBRARY} ${MATLAB_MX_LIBRARY} ${MATLAB_ENG_LIBRARY})
44
45
46
47
48
49
50
# Figure out the path to MATLAB's mex.h so we can add it to the include search path.
find_path(mex_header_path mex.h
    PATHS "$ENV{MATLAB_HOME}/extern/include"
        "${MATLAB_HOME}/extern/include"
    )
INCLUDE_DIRECTORIES(${mex_header_path})

51
52
53
# Determine the path to cmake_mex_wrapper file so we can add it to the include search path..
string(REGEX REPLACE "cmake_mex_wrapper$" "" dlib_matlab_binding_path ${CMAKE_CURRENT_LIST_FILE})
INCLUDE_DIRECTORIES("${dlib_matlab_binding_path}")
54
55
# Also add dlib to the include search path 
INCLUDE_DIRECTORIES(${dlib_matlab_binding_path}/../..) 
56

57
include(${dlib_matlab_binding_path}/../cmake_utils/add_global_compiler_switch.cmake)
58
add_global_define(MATLAB_MEX_FILE)
59
60
61
62
63
64
65
66
67
68
69
70

# Determine the path to our CMakeLists.txt file.  This is the file that
# includeded the one you are reading right now.  So here we make it so that
# when you run the install target it will copy the compiled mex files into the
# same folder as the parent CMakeLists.txt file.
string(REGEX REPLACE "CMakeLists.txt$" "" install_dir ${CMAKE_PARENT_LIST_FILE})
set(CMAKE_INSTALL_PREFIX "${install_dir}")
set(CMAKE_INSTALL_SYSTEM_RUNTIME_DESTINATION "${install_dir}")
INCLUDE(InstallRequiredSystemLibraries)


MACRO(add_mex_function name )
71
72
73
74
75
76
77
78
79
80
81
    ADD_LIBRARY(${name} MODULE ${name}.cpp )
    if (UNIX)
        # Doing this prevents our mex function from exporting any symbols
        # other than mexFunction().  This sometimes doesn't matter but sometimes
        # avoids causing errors or otherwise bad behavior in MATLAB.
        if (DEFINED ENV{MATLAB_HOME})
            set_target_properties(${name} PROPERTIES LINK_FLAGS "-Wl,--version-script,$ENV{MATLAB_HOME}/extern/lib/glnxa64/mexFunction.map")
        else()
            set_target_properties(${name} PROPERTIES LINK_FLAGS "-Wl,--version-script,${MATLAB_HOME}/extern/lib/glnxa64/mexFunction.map")
        endif()
    endif()
82

83
84
85
86
87
88
89
90
91
92
93
94
95
96
    # Change the output file extension to a mex extension.
   if (WIN32)
      set_target_properties(${name} PROPERTIES SUFFIX ".mexw64")
   elseif(APPLE)
      set_target_properties(${name} PROPERTIES SUFFIX ".mexmaci64")
   else()
      set_target_properties(${name} PROPERTIES SUFFIX ".mexa64")
   endif()
   set_target_properties(${name} PROPERTIES PREFIX "")
   TARGET_LINK_LIBRARIES(${name} ${MATLAB_LIBRARIES} ${ARGN})
   install(TARGETS ${name} DESTINATION "${install_dir}")
ENDMACRO()