compile.cmake 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # LISTIFY
  2. # Given a string of space-delimited tokens, reparse as a string of
  3. # semi-colon delimited tokens, which in CMake land is exactly equivalent
  4. # to a list
  5. macro(listify OUT_LIST IN_STRING)
  6. string(REPLACE " " ";" ${OUT_LIST} ${IN_STRING})
  7. endmacro()
  8. # listify multiple-argument inputs
  9. listify(MEX_INCLUDE_DIRS_LIST ${MEX_INCLUDE_DIRS})
  10. if (${CONFIGURATION} MATCHES "Debug")
  11. listify(MEX_LIBS_LIST ${MEX_DEBUG_LIBS})
  12. else()
  13. listify(MEX_LIBS_LIST ${MEX_LIBS})
  14. endif()
  15. # if it's MSVC building a Debug configuration, don't build bindings
  16. if ("${CONFIGURATION}" MATCHES "Debug")
  17. message(STATUS "Matlab bindings are only available in Release configurations. Skipping...")
  18. return()
  19. endif()
  20. # -----------------------------------------------------------------------------
  21. # Compile
  22. # -----------------------------------------------------------------------------
  23. # for each generated source file:
  24. # 1. check if the file has already been compiled
  25. # 2. attempt compile if required
  26. # 3. if the compile fails, throw an error and cancel compilation
  27. file(GLOB SOURCE_FILES "${CMAKE_CURRENT_BINARY_DIR}/src/*.cpp")
  28. list(LENGTH SOURCE_FILES __size)
  29. message("Matlab: compiling ${__size} files")
  30. set(__index 0)
  31. foreach(SOURCE_FILE ${SOURCE_FILES})
  32. MATH(EXPR __index "${__index}+1")
  33. # strip out the filename
  34. get_filename_component(FILENAME ${SOURCE_FILE} NAME_WE)
  35. message("[${__index}/${__size}] Compiling: ${FILENAME}")
  36. # compile the source file using mex
  37. if (NOT EXISTS "${CMAKE_CURRENT_BINARY_DIR}/+cv/${FILENAME}.${MATLAB_MEXEXT}" OR
  38. "${SOURCE_FILE}" IS_NEWER_THAN "${CMAKE_CURRENT_BINARY_DIR}/+cv/${FILENAME}.${MATLAB_MEXEXT}"
  39. )
  40. execute_process(
  41. COMMAND ${MATLAB_MEX_SCRIPT} ${MEX_OPTS} "CXXFLAGS=\$CXXFLAGS ${MEX_CXXFLAGS}" ${MEX_INCLUDE_DIRS_LIST}
  42. ${MEX_LIB_DIR} ${MEX_LIBS_LIST} ${SOURCE_FILE}
  43. WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/+cv
  44. OUTPUT_QUIET
  45. ERROR_VARIABLE FAILED
  46. )
  47. endif()
  48. # TODO: If a mex file fails to compile, should we error out?
  49. # TODO: Warnings are currently treated as errors...
  50. if (FAILED)
  51. message(FATAL_ERROR "Failed to compile ${FILENAME}: ${FAILED}")
  52. endif()
  53. endforeach()