CMakeLists.txt with add_custom_command
I want to add Ceres source to my project (we want to lock down to a specific release) so I am trying to wrap it as a package. The only problem is that the Ceres makefile generates a header file on the fly. So far I have got to this via various hints on the web.
cmake_minimum_required(VERSION 2.8.3)
project(ceres_library)
find_package(catkin REQUIRED COMPONENTS
whatever
)
set(CERES_SOLVER_DIR ceres-solver-1.14.0)
# This builds the project, but the install happens at the very end, so other projects that depend on this one fail with files not found
# add_subdirectory(${CERES_SOLVER_DIR})
# I've tried various path options here...
set(CERES_INCLUDE_PATH ${CMAKE_INSTALL_PREFIX}/${CERES_SOLVER_DIR}/include)
set(CERES_CERES_H_PATH ${CERES_INCLUDE_PATH}/ceres/ceres.h)
message("Ceres include path: ${CERES_INCLUDE_PATH}")
# catkin_package complains if the directory doesn't exist
file(MAKE_DIRECTORY ${CERES_INCLUDE_PATH}/ceres)
# Export the ceres include files, and the ceres library
catkin_package(
INCLUDE_DIRS ${CERES_INCLUDE_PATH}
LIBRARIES ceres
)
# Based on https://github.com/ethz-asl/ceres_catkin/blob/master/CMakeLists.txt
add_custom_command(OUTPUT ${CERES_CERES_H_PATH} PRE_BUILD
COMMAND cmake install
COMMENT "***Building ceres***"
DEPENDS ${CERES_SOLVER_DIR}
WORKING_DIRECTORY ${CERES_SOLVER_DIR}
)
add_custom_target(ceres_cmake
DEPENDS ${CERES_CERES_H_PATH}
)
The add_custom_command()
never executes; why?
Ack! Simple error - it should be
add_custom_target(ceres_cmake ALL
on the third-last line.Why not use ExternalProject_Add(..)?
Because I didn't know of that command! Looks useful, though, especially as the Ceres makefile looks ROS-friendly, but I also have to lock down the Eigen version too. I've further hacked the makefile to build a dummy library that links to the
libceres.a
so I can get thecatkin_package(LIBRARIES)
working.I would suggest to use
ExternalProject_Add(..)
at a minimum, and not go and try to reimplement it yourself.And I would probably recommend to just re-use ethz-asl/ceres_catkin. It already does the right things, and you should be able to update the
ExternalProject_Add(..)
call to checkout the revision you are looking for.