Skip to content

Commit

Permalink
Use Drake built with Bazel for MATLAB bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamie Snape committed Nov 1, 2017
1 parent 3adcb81 commit f9d6548
Show file tree
Hide file tree
Showing 19 changed files with 371 additions and 9 deletions.
123 changes: 123 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
# -*- mode: cmake -*-
# vi: set ft=cmake :

cmake_minimum_required(VERSION 3.5.1)
project(drake)

list(INSERT CMAKE_MODULE_PATH 0 "${PROJECT_SOURCE_DIR}/cmake/modules")

include(CTest)

configure_file(CTestCustom.cmake.in CTestCustom.cmake @ONLY)

if(CYGWIN OR NOT UNIX)
message(FATAL_ERROR "Cygwin and non-Unix platforms are NOT supported")
endif()

find_package(Bazel 0.6.1 MODULE REQUIRED)

get_filename_component(C_COMPILER_REALPATH "${CMAKE_C_COMPILER}" REALPATH)
get_filename_component(C_COMPILER_NAME "${C_COMPILER_REALPATH}" NAME)

get_filename_component(CXX_COMPILER_REALPATH "${CMAKE_CXX_COMPILER}" REALPATH)
get_filename_component(CXX_COMPILER_NAME "${CXX_COMPILER_REALPATH}" NAME)

if(C_COMPILER_NAME STREQUAL ccache OR CXX_COMPILER_NAME STREQUAL ccache)
message(FATAL_ERROR
"Compilation with ccache is NOT supported due to incompatibility with Bazel"
)
endif()

if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
set(CMAKE_BUILD_TYPE Release CACHE STRING
"Choose the type of build, options are None Debug Release RelWithDebInfo MinSizeRel"
FORCE
)
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY
STRINGS None Debug Release RelWithDebInfo MinSizeRel
)
endif()

if(CMAKE_BUILD_TYPE STREQUAL Debug)
set(BAZEL_COMPILATION_MODE dbg)
else()
set(BAZEL_COMPILATION_MODE opt)
endif()

set(BAZEL_ENV "CC=${CMAKE_C_COMPILER}" "CXX=${CMAKE_CXX_COMPILER}")

set(BAZEL_ARGS
--compilation_mode=${BAZEL_COMPILATION_MODE}
--config=unsupported_crosstool
--cxxopt=${CMAKE_CXX14_STANDARD_COMPILE_OPTION}
--host_cxxopt=${CMAKE_CXX14_STANDARD_COMPILE_OPTION}
)

if(CMAKE_VERBOSE_MAKEFILE)
list(APPEND BAZEL_ARGS --subcommands)
endif()

option(WITH_GUROBI "Build with support for GUROBI. Experimental." OFF)

if(WITH_GUROBI)
list(APPEND BAZEL_ARGS --config=gurobi)
endif()

option(WITH_MATLAB "Build with support for MATLAB" OFF)

if(WITH_MATLAB)
list(APPEND BAZEL_ARGS --define=NO_IPOPT=ON)
endif()

option(WITH_MOSEK "Build with support for MOSEK. Experimental." OFF)

if(WITH_MOSEK)
list(APPEND BAZEL_ARGS --config=mosek)
endif()

option(WITH_SNOPT "Build with support for SNOPT" OFF)

if(WITH_SNOPT)
list(APPEND BAZEL_ARGS --config=snopt)
endif()

set(BAZEL_TARGETS //:install)

if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
set(CMAKE_INSTALL_PREFIX "${PROJECT_BINARY_DIR}/install" CACHE STRING
"Install path prefix, prepended onto install directories" FORCE
)
endif()

include(ExternalProject)

ExternalProject_Add(drake_cxx_python
SOURCE_DIR "${PROJECT_SOURCE_DIR}/drake"
CONFIGURE_COMMAND :
BUILD_COMMAND ${BAZEL_ENV} "${Bazel_EXECUTABLE}" build ${BAZEL_ARGS} ${BAZEL_TARGETS}
BUILD_IN_SOURCE 1
BUILD_ALWAYS 1
INSTALL_COMMAND ${BAZEL_ENV} "${Bazel_EXECUTABLE}" run ${BAZEL_ARGS} ${BAZEL_TARGETS} -- "${CMAKE_INSTALL_PREFIX}"
USES_TERMINAL_BUILD 1
USES_TERMINAL_INSTALL 1
)

list(APPEND CMAKE_PREFIX_PATH "${CMAKE_INSTALL_PREFIX}")

if(WITH_MATLAB)
ExternalProject_Add(drake_matlab
DEPENDS drake_cxx_python
SOURCE_DIR "${PROJECT_SOURCE_DIR}/matlab"
CMAKE_ARGS
-DBUILD_TESTING:BOOL=${BUILD_TESTING}
-DCMAKE_BUILD_TYPE:STRING=${CMAKE_BUILD_TYPE}
"-DCMAKE_CXX_COMPILER:FILEPATH=${CMAKE_CXX_COMPILER}"
"-DCMAKE_CXX_FLAGS:STRING=${CMAKE_CXX_FLAGS}"
"-DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_INSTALL_PREFIX}"
"-DCMAKE_PREFIX_PATH:PATH=${CMAKE_PREFIX_PATH}"
"-DCMAKE_SHARED_LINKER_FLAGS:STRING=${CMAKE_SHARED_LINKER_FLAGS}"
-DCMAKE_VERBOSE_MAKEFILE:BOOL=${CMAKE_VERBOSE_MAKEFILE}
BINARY_DIR "${PROJECT_BINARY_DIR}/drake_matlab-build"
BUILD_ALWAYS 1
)
endif()
9 changes: 9 additions & 0 deletions CTestConfig.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# -*- mode: cmake -*-
# vi: set ft=cmake :

set(CTEST_PROJECT_NAME drake)
set(CTEST_NIGHTLY_START_TIME "00:00:00 EST")
set(CTEST_DROP_METHOD https)
set(CTEST_DROP_SITE drake-cdash.csail.mit.edu)
set(CTEST_DROP_LOCATION "/submit.php?project=${CTEST_PROJECT_NAME}")
set(CTEST_DROP_SITE_CDASH ON)
20 changes: 20 additions & 0 deletions CTestCustom.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- mode: cmake -*-
# vi: set ft=cmake :

list(APPEND CTEST_CUSTOM_COVERAGE_EXCLUDE
".*/test/.*"
".*/third_party/.*"
)

list(APPEND CTEST_CUSTOM_ERROR_EXCEPTION "^WARNING: ")

list(APPEND CTEST_CUSTOM_ERROR_MATCH
"^ERROR: "
"^FAIL: "
"^TIMEOUT: "
)

list(APPEND CTEST_CUSTOM_WARNING_MATCH "^WARNING: ")

set(CTEST_CUSTOM_MAXIMUM_NUMBER_OF_ERRORS 100)
set(CTEST_CUSTOM_MAXIMUM_NUMBER_OF_WARNINGS 100)
35 changes: 35 additions & 0 deletions cmake/modules/FindBazel.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# -*- mode: cmake -*-
# vi: set ft=cmake :

find_program(Bazel_EXECUTABLE bazel)

execute_process(COMMAND "${Bazel_EXECUTABLE}" version
RESULT_VARIABLE _BAZEL_VERSION_EXECUTE_PROCESS_RESULT_VARIABLE
OUTPUT_VARIABLE _BAZEL_VERSION_EXECUTE_PROCESS_OUTPUT_VARIABLE
)

set(Bazel_VERSION)

if(_BAZEL_VERSION_EXECUTE_PROCESS_RESULT_VARIABLE EQUAL 0)
string(REGEX MATCH "Build label: ([0-9a-zA-Z.]+)"
_BAZEL_VERSION_REGEX_MATCH_OUTPUT_VARIABLE
"${_BAZEL_VERSION_EXECUTE_PROCESS_OUTPUT_VARIABLE}"
)

if(CMAKE_MATCH_1)
set(Bazel_VERSION "${CMAKE_MATCH_1}")
endif()

unset(_BAZEL_VERSION_REGEX_MATCH_OUTPUT_VARIABLE)
endif()

unset(_BAZEL_VERSION_EXECUTE_PROCESS_OUTPUT_VARIABLE)
unset(_BAZEL_VERSION_EXECUTE_PROCESS_RESULT_VARIABLE)

include(FindPackageHandleStandardArgs)

find_package_handle_standard_args(Bazel
FOUND_VAR Bazel_FOUND
REQUIRED_VARS Bazel_EXECUTABLE
VERSION_VAR Bazel_VERSION
)
1 change: 0 additions & 1 deletion drake/matlab/.mlintopts

This file was deleted.

35 changes: 35 additions & 0 deletions matlab/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# -*- mode: cmake -*-
# vi: set ft=cmake :

cmake_minimum_required(VERSION 3.5.1)
project(drake_matlab LANGUAGES CXX)

if(CMAKE_VERSION VERSION_LESS 3.10)
list(INSERT CMAKE_MODULE_PATH 0
"${PROJECT_SOURCE_DIR}/../third_party/com_kitware_gitlab_cmake_cmake/3.10/Modules"
)
endif()

include(CTest)

include(cmake/flags.cmake)
include(cmake/functions.cmake)
include(cmake/packages.cmake)

configure_file(addpath_drake.m.in addpath_drake.m @ONLY)

drake_matlab_add_mex(mex_util mex_util.cc mex_util.h)
target_link_libraries(mex_util drake::drake Eigen3::Eigen protobuf::libprotobuf)

drake_matlab_add_mex(call_matlab_client call_matlab_client.cc)
target_link_libraries(call_matlab_client drake::drake mex_util)

install(FILES addpath_drake_install.m DESTINATION share/drake/matlab
RENAME addpath_drake.m
)

install(TARGETS call_matlab_client mex_util DESTINATION share/drake/matlab)

if(BUILD_TESTING)
add_subdirectory(test)
endif()
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
function [] = addpath_drake()

root = fileparts(mfilename('fullpath'));
addpath(fullfile(root, 'util'));
addpath(root);

drake_python_path = fullfile('@CMAKE_INSTALL_PREFIX@', 'lib', 'python2.7', 'site-packages');

Expand Down
10 changes: 10 additions & 0 deletions matlab/addpath_drake_install.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function [] = addpath_drake()

root = fileparts(mfilename('fullpath'));
addpath(root);

drake_python_path = fullfile(root, '..', '..', '..', 'lib', 'python2.7', 'site-packages');

if count(py.sys.path, drake_python_path) == 0
insert(py.sys.path, int32(0), drake_python_path);
end
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
#include <sys/stat.h>
#include <sys/types.h>

#include "google/protobuf/io/coded_stream.h"
#include "google/protobuf/io/zero_copy_stream_impl.h"
#include <google/protobuf/io/coded_stream.h>
#include <google/protobuf/io/zero_copy_stream_impl.h>

#include "drake/common/drake_assert.h"
#include "drake/common/proto/matlab_rpc.pb.h"
#include "drake/matlab/util/mex_util.h"
#include "drake/common/unused.h"

#include "mex_util.h"

// Mex client for matlab remote procedure calls (RPCs).

Expand All @@ -31,6 +33,9 @@
// manually delete a client variable.

void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]) {
drake::unused(nlhs);
drake::unused(plhs);

std::string filename = "/tmp/matlab_rpc";

if (nrhs == 1) {
Expand Down
30 changes: 30 additions & 0 deletions matlab/cmake/flags.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

if(CMAKE_CXX_COMPILER_ID STREQUAL AppleClang OR CMAKE_CXX_COMPILER_ID STREQUAL Clang)
set(CXX_FLAGS
-Werror=all
-Werror=ignored-qualifiers
-Werror=inconsistent-missing-override
-Werror=non-virtual-dtor
-Werror=overloaded-virtual
-Werror=return-stack-address
-Werror=shadow
-Werror=sign-compare
)
elseif(CMAKE_CXX_COMPILER_ID STREQUAL GNU)
set(CXX_FLAGS
-Werror=all
-Werror=extra
-Werror=ignored-qualifiers
-Werror=non-virtual-dtor
-Werror=overloaded-virtual
-Werror=return-local-addr
-Werror=unused-but-set-parameter
)
endif()

foreach(CXX_FLAG ${CXX_FLAGS})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CXX_FLAG}")
endforeach()
69 changes: 69 additions & 0 deletions matlab/cmake/functions.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
function(drake_matlab_add_mex NAME)
matlab_add_mex(NAME ${NAME} SHARED SRC ${ARGN})

set_target_properties(${NAME} PROPERTIES
LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}"
)

foreach(_CONFIGURATION_TYPE ${CMAKE_CONFIGURATION_TYPES})
string(TOUPPER "${_CONFIGURATION_TYPE}" _CONFIGURATION_TYPE_UPPER)
set_target_properties(${NAME} PROPERTIES
LIBRARY_OUTPUT_DIRECTORY_${_CONFIGURATION_TYPE_UPPER}
"${PROJECT_BINARY_DIR}"
)
endforeach()
endfunction()

function(drake_matlab_add_unit_test)
cmake_parse_arguments("" "" "COMMAND;NAME;SIZE" "" ${ARGN})

if(NOT _NAME)
message(FATAL_ERROR
"The NAME argument to drake_matlab_add_unit_test is required"
)
endif()

string(REPLACE "'" "''" _COMMAND ${_COMMAND})

set(_CUSTOM_TEST_COMMAND
"try, addpath_drake; eval('${_COMMAND}'); catch ex, disp(getReport(ex, 'extended')); disp(' '); exit(1); end; exit(0)"
)

set(_TEST_ARGS TEST_ARGS ${_UNPARSED_ARGUMENTS})

matlab_add_unit_test(
NAME ${_NAME}
ADDITIONAL_PATH "${CMAKE_CURRENT_SOURCE_DIR};${PROJECT_BINARY_DIR}"
CUSTOM_TEST_COMMAND "${_CUSTOM_TEST_COMMAND}"
TIMEOUT -1
WORKING_DIRECTORY "${PROJECT_BINARY_DIR}"
${_TEST_ARGS}
)

if(NOT _SIZE)
set(_SIZE medium)
endif()

if(_SIZE STREQUAL small)
set(_TIMEOUT 60)
elseif(_SIZE STREQUAL medium)
set(_TIMEOUT 300)
elseif(_SIZE STREQUAL large)
set(_TIMEOUT 900)
elseif(_SIZE STREQUAL enormous)
set(_TIMEOUT 2700)
else()
message(FATAL_ERROR
"The SIZE argument to drake_matlab_add_unit_test is invalid"
)
endif()

set(_COST ${_TIMEOUT})
set(_LABELS ${_SIZE})

set_tests_properties(${_NAME} PROPERTIES
COST ${_TIMEOUT}
LABELS ${_SIZE}
TIMEOUT ${_TIMEOUT}
)
endfunction()
9 changes: 9 additions & 0 deletions matlab/cmake/packages.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
set(CMAKE_FIND_PACKAGE_NO_PACKAGE_REGISTRY ON)
set(CMAKE_FIND_PACKAGE_NO_SYSTEM_PACKAGE_REGISTRY ON)

find_package(drake CONFIG REQUIRED)
find_package(Eigen3 3.3.3 CONFIG REQUIRED)
find_package(Matlab 9 MODULE REQUIRED COMPONENTS
MAIN_PROGRAM MEX_COMPILER MX_LIBRARY
)
find_package(Protobuf 2.6 MODULE REQUIRED)
2 changes: 1 addition & 1 deletion drake/matlab/util/mex_util.cc → matlab/mex_util.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "drake/matlab/util/mex_util.h"
#include "mex_util.h"

#include <limits>

Expand Down
File renamed without changes.
Loading

0 comments on commit f9d6548

Please sign in to comment.