-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCMakeLists.txt
126 lines (103 loc) · 4.83 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
cmake_minimum_required(VERSION 3.3.2)
project(mex-moos)
set(CMakeScripts_MODULE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
set(CMAKE_MODULE_PATH ${CMakeScripts_MODULE_DIR})
find_package(MOOS 10 REQUIRED)
find_package( Matlab REQUIRED MEX_COMPILER )
set(LIBRARY_FULL_NAME mex-moos)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
# Declare the mex target name and associated source file.
set(${LIBRARY_FULL_NAME}_MEX_TARGET mexmoos)
set(${LIBRARY_FULL_NAME}_MEX_SOURCE mex-moos.cpp )
# Explicitly set the output location for the mex file to be build/lib, and set
# the extension to match the mex extension for the current platform.
set(${LIBRARY_FULL_NAME}_MEX_FILE
${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${${LIBRARY_FULL_NAME}_MEX_TARGET}.${Matlab_MEX_EXTENSION})
# Add custom command to build the mex file.
#
# Build the -I<DIR> include arguments for mex. We do _not_ include the MRG
# library dependency includes here, as they are explicitly not in the public
# API and thus not required by mex.
set(MEX_API_PUBLIC_INCLUDE_DIRS
${MOOS_INCLUDE_DIRS}
${${LIBRARY_FULL_NAME}_EXTERNAL_DEPEND_INCLUDE_DIRS}
${${LIBRARY_FULL_NAME}_SELF_INCLUDE_DIRS})
list(REMOVE_DUPLICATES MEX_API_PUBLIC_INCLUDE_DIRS)
foreach (INC_DIR ${MEX_API_PUBLIC_INCLUDE_DIRS})
list(APPEND MEX_BUILD_INCLUDE_DIRS_CMDS "-I${INC_DIR}")
endforeach()
# We only need to _explicitly_ link against libraries that are used _directly_
# in the MEX file. As such we do not need to explicitly link against glog or
# Ceres as they come in through the linking of -l${LIBRARY_FULL_NAME}.
#
# Note that we explicitly disable ensures in Sophus library when compiling mex
# as the default ensure uses printf which is a macro for mexPrintf in MEX files
# which does not have the same arguments.
if (${CMAKE_SYSTEM_NAME} MATCHES "Linux")
# On Linux, the mex compiler does not properly use rpath linking (up to
# at least 2013b), thus although the mex file will link, it will not save
# full paths to the required libraries, thus otool -L / ldd with show
# 'libmx -> not found' or similar. Thus we have to explicitly set the
# -rpath list to include BOTH the MATLAB library directory, AND the directory
# containing our libraries (passed to -L as well). We have to do this for
# MATLAB as well as MATLAB **INCORRECTLY** sets the rpath (WTF?).
#
# Note that due to an incomprehensible reason, -rpath fails if you have
# spaces between it and the previous / next argument, see [1].
#
# [1]: http://www-01.ibm.com/support/docview.wss?uid=swg21329145
option(FORCE_RPATH_LINKING "linking fix for up to r2013b" TRUE)
if(FORCE_RPATH_LINKING)
set(FORCE_AND_FIX_MEX_TO_USE_RPATH_LINKING_ARGS
"LDFLAGS='$$LDFLAGS,-rpath=${MATLAB_MEX_LIBRARIES_DIR},-rpath=${CMAKE_LIBRARY_OUTPUT_DIRECTORY}'")
endif()
endif()
# IMPORTANT: DO **NOT** add VERBATIM to the end of this add_custom_command(),
# it screws up the odd MEX_LD_FLAGS escaping (adds wrapping "" and
# does crazy things with the $LDFLAGS statement).
# Strip namespace prefixes from library binary names
string(REGEX REPLACE ".+::"
"" MOOS_LIB_BINARY_NAME
${MOOS_LIBRARIES} )
if (APPLE)
find_program(INSTALL_NAME_TOOL install_name_tool)
add_custom_command( OUTPUT ${${LIBRARY_FULL_NAME}_MEX_FILE}
COMMAND ${Matlab_MEX_COMPILER}
ARGS -DSOPHUS_DISABLE_ENSURES
${MEX_BUILD_INCLUDE_DIRS_CMDS}
${FORCE_AND_FIX_MEX_TO_USE_RPATH_LINKING_ARGS}
-L${MOOS_DIR}/lib
-l${MOOS_LIB_BINARY_NAME}
-output ${${LIBRARY_FULL_NAME}_MEX_FILE}
${${LIBRARY_FULL_NAME}_MEX_SOURCE}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
DEPENDS ${${LIBRARY_FULL_NAME}_MEX_SOURCE}
COMMAND ${INSTALL_NAME_TOOL}
ARGS -add_rpath ${MOOS_DIR}/lib
${${LIBRARY_FULL_NAME}_MEX_FILE}
DEPENDS ${${LIBRARY_FULL_NAME}_MEX_SOURCE}
COMMENT "Building MEX interface for ${LIBRARY_FULL_NAME}.")
else()
add_custom_command( OUTPUT ${${LIBRARY_FULL_NAME}_MEX_FILE}
COMMAND ${Matlab_MEX_COMPILER}
ARGS -DSOPHUS_DISABLE_ENSURES
${MEX_BUILD_INCLUDE_DIRS_CMDS}
${FORCE_AND_FIX_MEX_TO_USE_RPATH_LINKING_ARGS}
-L${MOOS_DIR}/lib
-l${MOOS_LIB_BINARY_NAME}
-output ${${LIBRARY_FULL_NAME}_MEX_FILE}
${${LIBRARY_FULL_NAME}_MEX_SOURCE}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
DEPENDS ${${LIBRARY_FULL_NAME}_MEX_SOURCE}
COMMENT "Building MEX interface for ${LIBRARY_FULL_NAME}.")
endif()
# Add custom target which is always built, that depends on the mex file to drive
# it's creation.
add_custom_target( ${${LIBRARY_FULL_NAME}_MEX_TARGET}
ALL DEPENDS ${${LIBRARY_FULL_NAME}_MEX_FILE}
SOURCES ${${LIBRARY_FULL_NAME}_MEX_SOURCE}
VERBATIM )
message(STATUS "==> IMPORTANT: To use ${LIBRARY_FULL_NAME} MEX "
"library, add the following to MATLAB's path:")
message(STATUS "==> -- ${CMAKE_CURRENT_SOURCE_DIR}")
message(STATUS "==> -- ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}")