Merge pull request #3207 from ronghanghu/test-matcaffe-io
[platform/upstream/caffeonacl.git] / matlab / CMakeLists.txt
1 # Builds Matlab (or Octave) interface. In case of Matlab caffe must be
2 # compield as shared library. Octave can link static or shared caffe library
3 # To install octave run: sudo apt-get install liboctave-dev
4
5 if(NOT BUILD_matlab)
6   return()
7 endif()
8
9 if(HAVE_MATLAB AND Octave_compiler)
10   set(build_using ${Matlab_build_mex_using})
11 elseif(HAVE_MATLAB AND NOT Octave_compiler)
12   set(build_using "Matlab")
13 elseif(NOT HAVE_MATLAB AND Octave_compiler)
14   set(build_using "Octave")
15 else()
16   return()
17 endif()
18
19 if(NOT BUILD_SHARED_LIBS AND build_using MATCHES Matlab)
20   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.")
21 endif()
22
23 # helper function to set proper mex file extention
24 function(caffe_fetch_and_set_proper_mexext mexfile_variable)
25   execute_process(COMMAND ${Matlab_mexext} OUTPUT_STRIP_TRAILING_WHITESPACE RESULT_VARIABLE res OUTPUT_VARIABLE ext)
26   if(res MATCHES 0)
27     get_filename_component(folder  ${${mexfile_variable}} PATH)
28     get_filename_component(name_we ${${mexfile_variable}} NAME_WE)
29     set(${mexfile_variable} ${folder}/${name_we}.${ext} PARENT_SCOPE)
30   endif()
31 endfunction()
32
33 # global settings
34 file(GLOB Matlab_srcs +caffe/private/caffe_.cpp)
35 set(Matlab_caffe_mex ${PROJECT_SOURCE_DIR}/matlab/+caffe/private/caffe_.mex)
36
37 caffe_get_current_cflags(cflags)
38 caffe_parse_linker_libs(Caffe_LINKER_LIBS folders libflags macos_frameworks)
39 set(folders $<TARGET_LINKER_FILE_DIR:caffe> ${folders})
40
41 # prepare linker flag lists
42 string(REPLACE ";" ";-L" link_folders "-L${folders}")
43 string(REPLACE ";" ":"  rpath_folders   "${folders}")
44
45 if(build_using MATCHES "Matlab")
46   set(libflags -lcaffe${Caffe_POSTFIX} ${libflags}) # Matlab R2014a complans for -Wl,--whole-archive
47
48   caffe_fetch_and_set_proper_mexext(Matlab_caffe_mex)
49   add_custom_command(OUTPUT ${Matlab_caffe_mex} COMMAND ${Matlab_mex}
50       ARGS -output ${Matlab_caffe_mex} ${Matlab_srcs} ${cflags} ${link_folders} ${libflags}
51       DEPENDS caffe COMMENT "Building Matlab interface: ${Matlab_caffe_mex}" VERBATIM)
52   add_custom_target(matlab ALL DEPENDS ${Matlab_caffe_mex} SOURCES ${Matlab_srcs})
53
54 elseif(build_using MATCHES "Octave")
55
56   if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
57     set(libflags -Wl,-force_load,$<TARGET_LINKER_FILE:caffe> ${libflags})
58   elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
59     set(libflags -Wl,--whole-archive -lcaffe${Caffe_POSTFIX} -Wl,--no-whole-archive ${libflags})
60   endif()
61
62   add_custom_command(OUTPUT ${Matlab_caffe_mex} COMMAND ${Octave_compiler}
63       ARGS --mex -o ${Matlab_caffe_mex} ${Matlab_srcs} ${cflags} ${link_folders} ${libflags} -Wl,-rpath,${rpath_folders}
64       DEPENDS caffe COMMENT "Building Octave interface: ${Matlab_caffe_mex}" VERBATIM)
65
66   add_custom_target(octave ALL DEPENDS ${Matlab_caffe_mex} SOURCES ${Matlab_srcs})
67 endif()
68
69 # ---[ Install
70 file(GLOB mfiles caffe/*.m)
71 install(FILES ${mfiles} ${Matlab_caffe_mex} DESTINATION matlab)
72