-
Notifications
You must be signed in to change notification settings - Fork 159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
wip: Add native cmake build support #15
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,63 @@ | ||
cmake_minimum_required(VERSION 2.8.3) | ||
project(ifopt) | ||
find_package(catkin REQUIRED) | ||
catkin_metapackage() | ||
option(CATKIN_BUILD "CATKIN_BUILD" ON) | ||
MESSAGE(STATUS "CATKIN_BUILD is " ${CATKIN_BUILD}) | ||
|
||
if(${CATKIN_BUILD}) | ||
find_package(catkin REQUIRED) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This actually returns a variable CATKIN_FOUND that could be used instead of the CATKIN_BUILD, that way this flag does not have to be set by the user. |
||
catkin_metapackage() | ||
else() | ||
# do native cmake build | ||
# add gtest and gmock support | ||
# - see http://www.kaizou.org/2014/11/gtest-cmake/ | ||
# ------------- BEGIN FROM BLOG ------------------ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This whole section for unit testing with gtest seems a bit long, as well as there exist some native cmake functions that should handle that (https://cmake.org/cmake/help/v2.8.3/cmake.html#module:FindGTest). I would prefer to do something like they did here (create and download manually, cmake only checks if folder exists,...): |
||
# We need thread support | ||
find_package(Threads REQUIRED) | ||
|
||
# Enable ExternalProject CMake module | ||
include(ExternalProject) | ||
|
||
# Download and install GoogleTest | ||
ExternalProject_Add( | ||
gtest | ||
URL https://github.com/google/googletest/archive/master.zip | ||
PREFIX ${CMAKE_CURRENT_BINARY_DIR}/gtest | ||
# Disable install step | ||
INSTALL_COMMAND "" | ||
) | ||
|
||
# Get GTest source and binary directories from CMake project | ||
ExternalProject_Get_Property(gtest source_dir binary_dir) | ||
|
||
# Create a libgtest target to be used as a dependency by test programs | ||
add_library(libgtest IMPORTED STATIC GLOBAL) | ||
add_dependencies(libgtest gtest) | ||
|
||
# Set libgtest properties | ||
set_target_properties(libgtest PROPERTIES | ||
"IMPORTED_LOCATION" "${binary_dir}/googlemock/gtest/libgtest.a" | ||
"IMPORTED_LINK_INTERFACE_LIBRARIES" "${CMAKE_THREAD_LIBS_INIT}" | ||
) | ||
|
||
# Create a libgmock target to be used as a dependency by test programs | ||
add_library(libgmock IMPORTED STATIC GLOBAL) | ||
add_dependencies(libgmock gtest) | ||
|
||
# Set libgmock properties | ||
set_target_properties(libgmock PROPERTIES | ||
"IMPORTED_LOCATION" "${binary_dir}/googlemock/libgmock.a" | ||
"IMPORTED_LINK_INTERFACE_LIBRARIES" "${CMAKE_THREAD_LIBS_INIT}" | ||
) | ||
|
||
# I couldn't make it work with INTERFACE_INCLUDE_DIRECTORIES | ||
include_directories("${source_dir}/googletest/include" | ||
"${source_dir}/googlemock/include") | ||
# ------------- END FROM BLOG ------------------ | ||
|
||
enable_testing() | ||
|
||
# add subdirectories | ||
add_subdirectory("../ifopt_core" "../../ifopt_core/build") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if we want to add the base and the "/build" repos here. |
||
add_subdirectory("../ifopt_ipopt" "../../ifopt_ipopt/build") | ||
add_subdirectory("../ifopt_snopt" "../../ifopt_snopt/build") | ||
endif() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,21 @@ | ||
cmake_minimum_required(VERSION 2.8.3) | ||
project(ifopt_core) | ||
option(CATKIN_BUILD "CATKIN_BUILD" ON) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could be set from the return value of |
||
|
||
add_compile_options(-std=c++11) | ||
|
||
find_package(catkin REQUIRED) | ||
find_package(Eigen3 REQUIRED) | ||
|
||
|
||
################################### | ||
## catkin specific configuration ## | ||
################################### | ||
catkin_package( | ||
INCLUDE_DIRS include ${EIGEN3_INCLUDE_DIR} | ||
LIBRARIES ${PROJECT_NAME} | ||
) | ||
|
||
if(${CATKIN_BUILD}) | ||
find_package(catkin REQUIRED) | ||
catkin_package( | ||
INCLUDE_DIRS include ${EIGEN3_INCLUDE_DIR} | ||
LIBRARIES ${PROJECT_NAME} | ||
) | ||
endif() | ||
|
||
########### | ||
## Build ## | ||
|
@@ -34,35 +35,54 @@ add_library(${PROJECT_NAME} | |
############# | ||
## Install ## | ||
############# | ||
# Mark library for installation | ||
install( | ||
TARGETS ${PROJECT_NAME} | ||
ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION} | ||
LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION} | ||
RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION} | ||
) | ||
|
||
# Mark header files for installation | ||
install( | ||
DIRECTORY include/ifopt/ | ||
DESTINATION ${CATKIN_GLOBAL_INCLUDE_DESTINATION}/ifopt | ||
FILES_MATCHING PATTERN "*.h" | ||
) | ||
|
||
if(${CATKIN_BUILD}) | ||
# Mark library for installation | ||
install( | ||
TARGETS ${PROJECT_NAME} | ||
ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION} | ||
LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION} | ||
RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION} | ||
) | ||
|
||
############# | ||
## Testing ## | ||
############# | ||
if (CATKIN_ENABLE_TESTING) | ||
catkin_add_gtest(${PROJECT_NAME}-test | ||
test/gtest_main.cc | ||
test/composite_test.cc | ||
test/problem_test.cc) | ||
if(TARGET ${PROJECT_NAME}-test) | ||
target_link_libraries(${PROJECT_NAME}-test | ||
${PROJECT_NAME} | ||
pthread | ||
) | ||
endif() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be great if the code for cmake build wouldn't have to be repeated for catkin build. But i'm not sure how to handle this in the most flexible way. |
||
# Mark header files for installation | ||
install( | ||
DIRECTORY include/ifopt/ | ||
DESTINATION ${CATKIN_GLOBAL_INCLUDE_DESTINATION}/ifopt | ||
FILES_MATCHING PATTERN "*.h" | ||
) | ||
############# | ||
## Testing ## | ||
############# | ||
if (CATKIN_ENABLE_TESTING) | ||
catkin_add_gtest(${PROJECT_NAME}-test | ||
test/gtest_main.cc | ||
test/composite_test.cc | ||
test/problem_test.cc) | ||
if(TARGET ${PROJECT_NAME}-test) | ||
target_link_libraries(${PROJECT_NAME}-test | ||
${PROJECT_NAME} | ||
pthread | ||
) | ||
endif() | ||
endif() | ||
else() | ||
# cmake native build | ||
file(GLOB CORE_HFILES include/ifopt/*.h) | ||
install(FILES ${CORE_HFILES} DESTINATION include) | ||
install(TARGETS ${PROJECT_NAME} DESTINATION lib) | ||
|
||
############# | ||
## Testing ## | ||
############# | ||
file(GLOB CORE_TEST test/*.cc) | ||
add_executable(${PROJECT_NAME}-test ${CORE_TEST}) | ||
target_link_libraries(${PROJECT_NAME}-test | ||
${PROJECT_NAME} | ||
libgtest | ||
libgmock | ||
) | ||
add_test( | ||
NAME ${PROJECT_NAME}-test | ||
COMMAND ${PROJECT_NAME}-test | ||
) | ||
endif() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Best case this is just
cmake ..
and the system automatically detects that it should build without catkin and where the files should be installed. Possibly its even better to avoid system-wide installation and just keep all files local in thebuild
folder.