Enable adding as a CMake subproject (#203)

This is done by replacing CMAKE_*_DIR variables with PROJECT_*_ variables so
that CMake uses correct paths when libcartographer is a subproject

Move AllFiles.cmake into the project binary dir instead of the root CMake binary dir,
use a project-dependent detect_changes target name

This makes overriding it from an upper level CMakeLists.txt possible, which is required
when doing a super-build which doesn't require installing libcartographer.
master
Juraj Oršulić 2017-02-09 15:57:54 +01:00 committed by Holger Rapp
parent b2175f0d93
commit 370e1e52df
3 changed files with 23 additions and 21 deletions

View File

@ -22,7 +22,7 @@ set(CARTOGRAPHER_PATCH_VERSION 0)
set(CARTOGRAPHER_VERSION ${CARTOGRAPHER_MAJOR_VERSION}.${CARTOGRAPHER_MINOR_VERSION}.${CARTOGRAPHER_PATCH_VERSION})
set(CARTOGRAPHER_SOVERSION ${CARTOGRAPHER_MAJOR_VERSION}.${CARTOGRAPHER_MINOR_VERSION})
include("${CMAKE_SOURCE_DIR}/cmake/functions.cmake")
include("${PROJECT_SOURCE_DIR}/cmake/functions.cmake")
google_initialize_cartographer_project()
google_enable_testing()
@ -44,7 +44,9 @@ endif()
# Install catkin package.xml
install(FILES package.xml DESTINATION share/cartographer)
set(CARTOGRAPHER_CONFIGURATION_FILES_DIRECTORY ${CMAKE_INSTALL_PREFIX}/share/cartographer/configuration_files)
set(CARTOGRAPHER_CONFIGURATION_FILES_DIRECTORY ${CMAKE_INSTALL_PREFIX}/share/cartographer/configuration_files
CACHE PATH ".lua configuration files directory")
install(DIRECTORY configuration_files DESTINATION share/cartographer/)
install(DIRECTORY cmake DESTINATION share/cartographer/)
@ -59,19 +61,19 @@ file(GLOB_RECURSE ALL_PROTOS "*.proto")
set(ALL_PROTO_SRCS)
set(ALL_PROTO_HDRS)
foreach(ABS_FIL ${ALL_PROTOS})
file(RELATIVE_PATH REL_FIL ${CMAKE_SOURCE_DIR} ${ABS_FIL})
file(RELATIVE_PATH REL_FIL ${PROJECT_SOURCE_DIR} ${ABS_FIL})
get_filename_component(DIR ${REL_FIL} DIRECTORY)
get_filename_component(FIL_WE ${REL_FIL} NAME_WE)
list(APPEND ALL_PROTO_SRCS "${CMAKE_BINARY_DIR}/${DIR}/${FIL_WE}.pb.cc")
list(APPEND ALL_PROTO_HDRS "${CMAKE_BINARY_DIR}/${DIR}/${FIL_WE}.pb.h")
list(APPEND ALL_PROTO_SRCS "${PROJECT_BINARY_DIR}/${DIR}/${FIL_WE}.pb.cc")
list(APPEND ALL_PROTO_HDRS "${PROJECT_BINARY_DIR}/${DIR}/${FIL_WE}.pb.h")
add_custom_command(
OUTPUT "${CMAKE_BINARY_DIR}/${DIR}/${FIL_WE}.pb.cc"
"${CMAKE_BINARY_DIR}/${DIR}/${FIL_WE}.pb.h"
OUTPUT "${PROJECT_BINARY_DIR}/${DIR}/${FIL_WE}.pb.cc"
"${PROJECT_BINARY_DIR}/${DIR}/${FIL_WE}.pb.h"
COMMAND ${PROTOBUF_PROTOC_EXECUTABLE}
ARGS --cpp_out ${CMAKE_BINARY_DIR} -I
${CMAKE_SOURCE_DIR} ${ABS_FIL}
ARGS --cpp_out ${PROJECT_BINARY_DIR} -I
${PROJECT_SOURCE_DIR} ${ABS_FIL}
DEPENDS ${ABS_FIL}
COMMENT "Running C++ protocol buffer compiler on ${ABS_FIL}"
VERBATIM
@ -119,8 +121,8 @@ target_link_libraries(${PROJECT_NAME} PUBLIC ${PROTOBUF_LIBRARY} pthread)
# Add the binary directory first, so that port.h is included after it has
# been generated.
target_include_directories(${PROJECT_NAME} PUBLIC
$<BUILD_INTERFACE:${CMAKE_BINARY_DIR}>
$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}>
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}>
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>
$<INSTALL_INTERFACE:include>
)
@ -144,7 +146,7 @@ install(
# Install source headers.
file(GLOB_RECURSE hdrs "*.h")
foreach(HDR ${hdrs})
file(RELATIVE_PATH REL_FIL ${CMAKE_SOURCE_DIR} ${HDR})
file(RELATIVE_PATH REL_FIL ${PROJECT_SOURCE_DIR} ${HDR})
get_filename_component(INSTALL_DIR ${REL_FIL} DIRECTORY)
install(
FILES
@ -157,12 +159,12 @@ endforeach()
# Install generated headers.
file(GLOB_RECURSE hdrs "*.h.in")
foreach(HDR ${hdrs})
file(RELATIVE_PATH REL_FIL ${CMAKE_SOURCE_DIR} ${HDR})
file(RELATIVE_PATH REL_FIL ${PROJECT_SOURCE_DIR} ${HDR})
get_filename_component(DIR ${REL_FIL} DIRECTORY)
get_filename_component(FIL_WE ${REL_FIL} NAME_WE)
install(
FILES
${CMAKE_BINARY_DIR}/${DIR}/${FILE_WE}
${PROJECT_BINARY_DIR}/${DIR}/${FILE_WE}
DESTINATION
include/${DIR}
)
@ -170,7 +172,7 @@ endforeach()
# Install proto headers.
foreach(HDR ${ALL_PROTO_HDRS})
file(RELATIVE_PATH REL_FIL ${CMAKE_BINARY_DIR} ${HDR})
file(RELATIVE_PATH REL_FIL ${PROJECT_BINARY_DIR} ${HDR})
get_filename_component(DIR ${REL_FIL} DIRECTORY)
install(
FILES
@ -184,7 +186,7 @@ set(CARTOGRAPHER_CMAKE_DIR share/cartographer/cmake)
include(CMakePackageConfigHelpers)
configure_package_config_file(
cartographer-config.cmake.in
${CMAKE_BINARY_DIR}/cmake/cartographer/cartographer-config.cmake
${PROJECT_BINARY_DIR}/cmake/cartographer/cartographer-config.cmake
PATH_VARS CARTOGRAPHER_CMAKE_DIR
INSTALL_DESTINATION ${CMAKE_INSTALL_PREFIX}/share/cartographer
)
@ -196,6 +198,6 @@ install(
)
install(
FILES ${CMAKE_BINARY_DIR}/cmake/cartographer/cartographer-config.cmake
FILES ${PROJECT_BINARY_DIR}/cmake/cartographer/cartographer-config.cmake
DESTINATION share/cartographer/
)

View File

@ -22,7 +22,7 @@ namespace common {
constexpr char kConfigurationFilesDirectory[] =
"@CARTOGRAPHER_CONFIGURATION_FILES_DIRECTORY@";
constexpr char kSourceDirectory[] = "@CMAKE_SOURCE_DIR@";
constexpr char kSourceDirectory[] = "@PROJECT_SOURCE_DIR@";
} // namespace common
} // namespace cartographer

View File

@ -107,10 +107,10 @@ macro(google_initialize_cartographer_project)
message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
# Add a hook that reruns CMake when source files are added or removed.
set(LIST_FILES_CMD "find ${CMAKE_SOURCE_DIR}/ -not -iwholename '*.git*' | sort | sed 's/^/#/'")
set(FILES_LIST_PATH "${CMAKE_BINARY_DIR}/AllFiles.cmake")
set(LIST_FILES_CMD "find ${PROJECT_SOURCE_DIR}/ -not -iwholename '*.git*' | sort | sed 's/^/#/'")
set(FILES_LIST_PATH "${PROJECT_BINARY_DIR}/AllFiles.cmake")
set(DETECT_CHANGES_CMD "bash" "-c" "${LIST_FILES_CMD} | diff -N -q ${FILES_LIST_PATH} - || ${LIST_FILES_CMD} > ${FILES_LIST_PATH}")
add_custom_target(cartographer_detect_changes ALL
add_custom_target(${PROJECT_NAME}_detect_changes ALL
COMMAND ${DETECT_CHANGES_CMD}
VERBATIM
)