compile.cmake 1.91 KB
Newer Older
1 2 3 4
# LISTIFY
# Given a string of space-delimited tokens, reparse as a string of
# semi-colon delimited tokens, which in CMake land is exactly equivalent
# to a list
5 6 7 8
macro(listify OUT_LIST IN_STRING)
    string(REPLACE " " ";" ${OUT_LIST} ${IN_STRING})
endmacro()

9
# listify multiple-argument inputs
10
listify(MEX_INCLUDE_DIRS_LIST ${MEX_INCLUDE_DIRS})
11 12 13 14 15 16
if (${CONFIGURATION} MATCHES "Debug")
    listify(MEX_LIBS_LIST ${MEX_DEBUG_LIBS})
else()
    listify(MEX_LIBS_LIST ${MEX_LIBS})
endif()

17 18 19 20 21 22
# if it's MSVC building a Debug configuration, don't build bindings
if ("${CONFIGURATION}" MATCHES "Debug")
    message(STATUS "Matlab bindings are only available in Release configurations. Skipping...")
    return()
endif()

23 24 25
# -----------------------------------------------------------------------------
# Compile
# -----------------------------------------------------------------------------
26 27 28 29
# for each generated source file:
# 1. check if the file has already been compiled
# 2. attempt compile if required
# 3. if the compile fails, throw an error and cancel compilation
30 31
file(GLOB SOURCE_FILES "${CMAKE_CURRENT_BINARY_DIR}/src/*.cpp")
foreach(SOURCE_FILE ${SOURCE_FILES})
hbristow's avatar
hbristow committed
32 33
    # strip out the filename
    get_filename_component(FILENAME ${SOURCE_FILE} NAME_WE)
34
    # compile the source file using mex
35 36
    if (NOT EXISTS ${CMAKE_CURRENT_BINARY_DIR}/+cv/${FILENAME}.${MATLAB_MEXEXT})
        execute_process(
37
            COMMAND ${MATLAB_MEX_SCRIPT} ${MEX_OPTS} "CXXFLAGS=\$CXXFLAGS ${MEX_CXXFLAGS}" ${MEX_INCLUDE_DIRS_LIST}
38
                    ${MEX_LIB_DIR} ${MEX_LIBS_LIST} ${SOURCE_FILE}
39
            WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/+cv
40
            OUTPUT_QUIET
41 42 43
            ERROR_VARIABLE FAILED
        )
    endif()
44
    # TODO: If a mex file fails to compile, should we error out?
45
    # TODO: Warnings are currently treated as errors...
hbristow's avatar
hbristow committed
46 47 48
    if (FAILED)
        message(FATAL_ERROR "Failed to compile ${FILENAME}: ${FAILED}")
    endif()
49
endforeach()