2014-12-17 12:09:14 +03:00
# Builds Matlab (or Octave) interface. In case of Matlab caffe must be
# compield as shared library. Octave can link static or shared caffe library
# To install octave run: sudo apt-get install liboctave-dev
if ( NOT BUILD_matlab )
return ( )
endif ( )
if ( HAVE_MATLAB AND Octave_compiler )
set ( build_using ${ Matlab_build_mex_using } )
elseif ( HAVE_MATLAB AND NOT Octave_compiler )
set ( build_using "Matlab" )
elseif ( NOT HAVE_MATLAB AND Octave_compiler )
set ( build_using "Octave" )
else ( )
return ( )
endif ( )
if ( NOT BUILD_SHARED_LIBS AND build_using MATCHES Matlab )
message ( FATAL_ERROR "Matlab MEX interface (with default mex options file) can only be built if caffe is compiled as shared library. Please enable 'BUILD_SHARED_LIBS' in CMake. Aternativelly you can switch to Octave compiler." )
endif ( )
# helper function to set proper mex file extention
function ( caffe_fetch_and_set_proper_mexext mexfile_variable )
execute_process ( COMMAND ${ Matlab_mexext } OUTPUT_STRIP_TRAILING_WHITESPACE RESULT_VARIABLE res OUTPUT_VARIABLE ext )
if ( res MATCHES 0 )
get_filename_component ( folder ${ ${mexfile_variable } } PATH )
get_filename_component ( name_we ${ ${mexfile_variable } } NAME_WE )
set ( ${ mexfile_variable } ${ folder } / ${ name_we } . ${ ext } PARENT_SCOPE )
endif ( )
endfunction ( )
# global settings
2015-05-28 11:24:30 +03:00
file ( GLOB Matlab_srcs +caffe/private/caffe_.cpp )
set ( Matlab_caffe_mex ${ PROJECT_SOURCE_DIR } /matlab/+caffe/private/caffe_.mex )
2014-12-17 12:09:14 +03:00
caffe_get_current_cflags ( cflags )
2015-02-01 17:45:31 +03:00
caffe_parse_linker_libs ( Caffe_LINKER_LIBS folders libflags macos_frameworks )
2014-12-17 12:09:14 +03:00
set ( folders $< TARGET_LINKER_FILE_DIR:caffe > ${ folders } )
# prepare linker flag lists
string ( REPLACE ";" ";-L" link_folders "-L${folders}" )
string ( REPLACE ";" ":" rpath_folders "${folders}" )
if ( build_using MATCHES "Matlab" )
set ( libflags -lcaffe ${ CAffe_POSTFIX } ${ libflags } ) # Matlab R2014a complans for -Wl,--whole-archive
caffe_fetch_and_set_proper_mexext ( Matlab_caffe_mex )
add_custom_command ( OUTPUT ${ Matlab_caffe_mex } COMMAND ${ Matlab_mex }
A R G S - o u t p u t $ { M a t l a b _ c a f f e _ m e x } $ { M a t l a b _ s r c s } $ { c f l a g s } $ { l i n k _ f o l d e r s } $ { l i b f l a g s }
D E P E N D S c a f f e C O M M E N T " B u i l d i n g M a t l a b i n t e r f a c e : $ { M a t l a b _ c a f f e _ m e x } " V E R B A T I M )
add_custom_target ( matlab ALL DEPENDS ${ Matlab_caffe_mex } SOURCES ${ Matlab_srcs } )
elseif ( build_using MATCHES "Octave" )
if ( "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" )
set ( libflags -Wl,-force_load, $< TARGET_LINKER_FILE:caffe > ${ libflags } )
elseif ( "${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" )
set ( libflags -Wl,--whole-archive -lcaffe ${ CAffe_POSTFIX } -Wl,--no-whole-archive ${ libflags } )
endif ( )
add_custom_command ( OUTPUT ${ Matlab_caffe_mex } COMMAND ${ Octave_compiler }
A R G S - - m e x - o $ { M a t l a b _ c a f f e _ m e x } $ { M a t l a b _ s r c s } $ { c f l a g s } $ { l i n k _ f o l d e r s } $ { l i b f l a g s } - W l , - r p a t h , $ { r p a t h _ f o l d e r s }
D E P E N D S c a f f e C O M M E N T " B u i l d i n g O c t a v e i n t e r f a c e : $ { M a t l a b _ c a f f e _ m e x } " V E R B A T I M )
add_custom_target ( octave ALL DEPENDS ${ Matlab_caffe_mex } SOURCES ${ Matlab_srcs } )
endif ( )
# ---[ Install
file ( GLOB mfiles caffe/*.m )
install ( FILES ${ mfiles } ${ Matlab_caffe_mex } DESTINATION matlab )