CMakeLists.txt with add_custom_command

asked 2020-04-15 03:07:03 -0500

KenYN gravatar image

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?

edit retag flag offensive close merge delete

Comments

Ack! Simple error - it should be add_custom_target(ceres_cmake ALL on the third-last line.

KenYN gravatar image KenYN  ( 2020-04-15 03:46:41 -0500 )edit
1
gvdhoorn gravatar image gvdhoorn  ( 2020-04-15 04:46:04 -0500 )edit

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 the catkin_package(LIBRARIES) working.

KenYN gravatar image KenYN  ( 2020-04-15 19:14:49 -0500 )edit
1

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.

gvdhoorn gravatar image gvdhoorn  ( 2020-04-16 02:28:15 -0500 )edit