# 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.11)

set(BUILDING_MATLAB_MEX_FILE true)
set(CMAKE_POSITION_INDEPENDENT_CODE True)

# 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"
   )
# 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()
# 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})
# 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})

# 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}")
# Also add dlib to the include search path 
INCLUDE_DIRECTORIES(${dlib_matlab_binding_path}/../..) 

include(${dlib_matlab_binding_path}/../cmake_utils/add_global_compiler_switch.cmake)
add_global_define(MATLAB_MEX_FILE)

# 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 )
    ADD_LIBRARY(${name} MODULE ${name}.cpp )
    target_compile_definitions(${name} PRIVATE -DMEX_FILENAME=${name})
    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()

    # 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()


