Skip to content

Commit

Permalink
Extend CMake functions for reuse in other Cartographer subprojects. (#45
Browse files Browse the repository at this point in the history
)

- Change CARTOGRAPHER_LIBRARIES to be absolute path to adhere to the
  CMake documentation.
  • Loading branch information
SirVer committed Oct 12, 2016
1 parent f6c9f95 commit 8e7996f
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 46 deletions.
48 changes: 9 additions & 39 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,7 @@ set(CARTOGRAPHER_VERSION ${CARTOGRAPHER_MAJOR_VERSION}.${CARTOGRAPHER_MINOR_VERS
set(CARTOGRAPHER_SOVERSION ${CARTOGRAPHER_MAJOR_VERSION}.${CARTOGRAPHER_MINOR_VERSION})

include("${CMAKE_SOURCE_DIR}/cmake/functions.cmake")
SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules)

if(NOT CMAKE_BUILD_TYPE OR CMAKE_BUILD_TYPE STREQUAL "")
set(CMAKE_BUILD_TYPE Release)
endif()

if(CMAKE_BUILD_TYPE STREQUAL "Release")
google_add_flag(GOOG_CXX_FLAGS "-O3 -DNDEBUG")
elseif(CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")
google_add_flag(GOOG_CXX_FLAGS "-O3 -g -DNDEBUG")
elseif(CMAKE_BUILD_TYPE STREQUAL "Debug")
message(FATAL_ERROR "Cartographer is too slow to be useful in debug mode.")
else()
message(FATAL_ERROR "Unknown CMAKE_BUILD_TYPE: ${CMAKE_BUILD_TYPE}")
endif()

message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
google_initialize_cartographer_project()

find_package(Boost REQUIRED COMPONENTS system iostreams)
find_package(Ceres REQUIRED)
Expand All @@ -52,26 +36,6 @@ if(SPHINX_FOUND)
add_subdirectory("docs")
endif()

set(GMOCK_SRC_DIR "/usr/src/gmock" CACHE STRING "Path to google-mock sources.")

add_subdirectory(${GMOCK_SRC_DIR} "${CMAKE_CURRENT_BINARY_DIR}/gmock")

google_add_flag(GOOG_CXX_FLAGS "-std=c++11")

if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
google_add_flag(GOOG_CXX_FLAGS "-Weverything")
google_add_flag(GOOG_CXX_FLAGS "-Werror=deprecated")
google_add_flag(GOOG_CXX_FLAGS "-Werror=non-pod-varargs")
else()
google_add_flag(GOOG_CXX_FLAGS "-Wall")
google_add_flag(GOOG_CXX_FLAGS "-Wpedantic")
endif()

# Turn some warnings into errors. These are defined for clang and gcc.
google_add_flag(GOOG_CXX_FLAGS "-Werror=format-security")
google_add_flag(GOOG_CXX_FLAGS "-Werror=return-type")
google_add_flag(GOOG_CXX_FLAGS "-Werror=uninitialized")

enable_testing()

SET(ALL_LIBRARIES "" CACHE INTERNAL "ALL_LIBRARIES")
Expand All @@ -82,6 +46,8 @@ install(FILES package.xml DESTINATION share/cartographer)
set(CARTOGRAPHER_CONFIGURATION_FILES_DIRECTORY "${CMAKE_INSTALL_PREFIX}/share/cartographer/configuration_files")
install(DIRECTORY configuration_files DESTINATION share/cartographer/)

install(DIRECTORY cmake DESTINATION share/cartographer/)

add_subdirectory("cartographer")

include(CMakePackageConfigHelpers)
Expand All @@ -96,11 +62,15 @@ list(APPEND CARTOGRAPHER_INCLUDE_DIRS "${LUA_INCLUDE_DIR}")
list(APPEND CARTOGRAPHER_INCLUDE_DIRS "${CERES_INCLUDE_DIRS}")
list(APPEND CARTOGRAPHER_INCLUDE_DIRS "${PROTOBUF_INCLUDE_DIR}")

set(CARTOGRAPHER_CMAKE_DIR "${CMAKE_INSTALL_PREFIX}/share/cartographer/cmake")

google_combined_library(cartographer
SRCS "${ALL_LIBRARIES}"
)

get_property(CARTOGRAPHER_LIBRARY_FILE TARGET cartographer PROPERTY LOCATION)
get_filename_component(CARTOGRAPHER_LIBRARY_FILE_BASENAME
${CARTOGRAPHER_LIBRARY_FILE} NAME)
install(
FILES
${CARTOGRAPHER_LIBRARY_FILE}
Expand All @@ -109,7 +79,7 @@ install(
)

set(CARTOGRAPHER_LIBRARIES "")
list(APPEND CARTOGRAPHER_LIBRARIES "cartographer")
list(APPEND CARTOGRAPHER_LIBRARIES "${CMAKE_INSTALL_PREFIX}/lib/${CARTOGRAPHER_LIBRARY_FILE_BASENAME}")
list(APPEND CARTOGRAPHER_LIBRARIES "${CERES_LIBRARIES}")
list(APPEND CARTOGRAPHER_LIBRARIES "${Boost_LIBRARIES}")
list(APPEND CARTOGRAPHER_LIBRARIES "${LUA_LIBRARIES}")
Expand All @@ -119,7 +89,7 @@ list(APPEND CARTOGRAPHER_LIBRARIES "webp")
CONFIGURE_PACKAGE_CONFIG_FILE(
cartographer-config.cmake.in
"${CMAKE_BINARY_DIR}/cmake/cartographer/cartographer-config.cmake"
PATH_VARS CARTOGRAPHER_INCLUDE_DIRS CARTOGRAPHER_LIBRARY_DIRS
PATH_VARS CARTOGRAPHER_INCLUDE_DIRS CARTOGRAPHER_LIBRARY_DIRS CARTOGRAPHER_CMAKE_DIR
INSTALL_DESTINATION ${CMAKE_INSTALL_PREFIX}/share/cartographer
)

Expand Down
1 change: 1 addition & 0 deletions cartographer-config.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

set(CARTOGRAPHER_INCLUDE_DIRS "@PACKAGE_CARTOGRAPHER_INCLUDE_DIRS@")
set(CARTOGRAPHER_LIBRARY_DIRS "@PACKAGE_CARTOGRAPHER_LIBRARY_DIRS@")
set(CARTOGRAPHER_CMAKE_DIR "@PACKAGE_CARTOGRAPHER_CMAKE_DIR@")

set(CARTOGRAPHER_LIBRARIES
"@CARTOGRAPHER_LIBRARIES@"
Expand Down
85 changes: 78 additions & 7 deletions cmake/functions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ macro(_parse_arguments ARGS)
USES_GLOG
USES_GFLAGS
)

# Options only used by projects using Cartographers cmake files.
list(APPEND OPTIONS
USES_CARTOGRAPHER
USES_ROS
USES_ZLIB
USES_PCL
)
set(ONE_VALUE_ARG )
set(MULTI_VALUE_ARGS SRCS HDRS DEPENDS)
cmake_parse_arguments(ARG
Expand All @@ -33,31 +41,29 @@ endmacro(_parse_arguments)

macro(_common_compile_stuff VISIBILITY)
set(TARGET_COMPILE_FLAGS "${TARGET_COMPILE_FLAGS} ${GOOG_CXX_FLAGS}")
set_target_properties(${NAME} PROPERTIES
COMPILE_FLAGS ${TARGET_COMPILE_FLAGS})

if(ARG_USES_EIGEN)
target_include_directories("${NAME}" SYSTEM ${VISIBILITY}
"${EIGEN3_INCLUDE_DIR}")
target_link_libraries("${NAME}" "${EIGEN3_LIBRARIES}")
target_link_libraries("${NAME}" ${EIGEN3_LIBRARIES})
endif()

if(ARG_USES_CERES)
target_include_directories("${NAME}" SYSTEM ${VISIBILITY}
"${CERES_INCLUDE_DIRS}")
target_link_libraries("${NAME}" "${CERES_LIBRARIES}")
target_link_libraries("${NAME}" ${CERES_LIBRARIES})
endif()

if(ARG_USES_LUA)
target_include_directories("${NAME}" SYSTEM ${VISIBILITY}
"${LUA_INCLUDE_DIR}")
target_link_libraries("${NAME}" "${LUA_LIBRARIES}")
target_link_libraries("${NAME}" ${LUA_LIBRARIES})
endif()

if(ARG_USES_BOOST)
target_include_directories("${NAME}" SYSTEM ${VISIBILITY}
"{Boost_INCLUDE_DIRS}")
target_link_libraries("${NAME}" "${Boost_LIBRARIES}")
target_link_libraries("${NAME}" ${Boost_LIBRARIES})
endif()

if(ARG_USES_WEBP)
Expand All @@ -73,6 +79,38 @@ macro(_common_compile_stuff VISIBILITY)
target_link_libraries("${NAME}" gflags)
endif()

if(ARG_USES_ROS)
target_include_directories("${NAME}" SYSTEM ${VISIBILITY}
"${catkin_INCLUDE_DIRS}")
target_link_libraries("${NAME}" ${catkin_LIBRARIES})
add_dependencies("${NAME}" ${catkin_EXPORTED_TARGETS}
)
endif()

if(ARG_USES_ZLIB)
target_include_directories("${NAME}" SYSTEM ${VISIBILITY}
"${ZLIB_INCLUDE_DIRS}")
target_link_libraries("${NAME}" ${ZLIB_LIBRARIES})
endif()

if(ARG_USES_CARTOGRAPHER)
target_include_directories("${NAME}" SYSTEM ${VISIBILITY}
"${CARTOGRAPHER_INCLUDE_DIRS}")
target_link_libraries("${NAME}" ${CARTOGRAPHER_LIBRARIES})
endif()

if(ARG_USES_PCL)
target_include_directories("${NAME}" SYSTEM ${VISIBILITY}
"${PCL_INCLUDE_DIRS}")
target_link_libraries("${NAME}" ${PCL_LIBRARIES})
foreach(DEFINITION ${PCL_DEFINITIONS})
set(TARGET_COMPILE_FLAGS "${TARGET_COMPILE_FLAGS} ${DEFINITION}")
endforeach()
endif()

set_target_properties(${NAME} PROPERTIES
COMPILE_FLAGS ${TARGET_COMPILE_FLAGS})

# Add the binary directory first, so that port.h is included after it has
# been generated.
target_include_directories("${NAME}" ${VISIBILITY} "${CMAKE_BINARY_DIR}")
Expand Down Expand Up @@ -265,5 +303,38 @@ function(google_proto_library NAME)
"${PROTOBUF_INCLUDE_DIR}")
# TODO(hrapp): This should not explicityly list pthread and use
# PROTOBUF_LIBRARIES, but that failed on first try.
target_link_libraries("${NAME}" "${PROTOBUF_LIBRARY}" pthread)
target_link_libraries("${NAME}" ${PROTOBUF_LIBRARY} pthread)
endfunction()

macro(google_initialize_cartographer_project)
SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules)
set(GOOG_CXX_FLAGS "-pthread -std=c++11 ${GOOG_CXX_FLAGS}")

google_add_flag(GOOG_CXX_FLAGS "-Wall")
google_add_flag(GOOG_CXX_FLAGS "-Wpedantic")

# Turn some warnings into errors.
google_add_flag(GOOG_CXX_FLAGS "-Werror=format-security")
google_add_flag(GOOG_CXX_FLAGS "-Werror=reorder")
google_add_flag(GOOG_CXX_FLAGS "-Werror=return-type")
google_add_flag(GOOG_CXX_FLAGS "-Werror=uninitialized")

if(NOT CMAKE_BUILD_TYPE OR CMAKE_BUILD_TYPE STREQUAL "")
set(CMAKE_BUILD_TYPE Release)
endif()

if(CMAKE_BUILD_TYPE STREQUAL "Release")
google_add_flag(GOOG_CXX_FLAGS "-O3 -DNDEBUG")
elseif(CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")
google_add_flag(GOOG_CXX_FLAGS "-O3 -g -DNDEBUG")
elseif(CMAKE_BUILD_TYPE STREQUAL "Debug")
message(FATAL_ERROR "Compiling in debug mode is not supported.")
else()
message(FATAL_ERROR "Unknown CMAKE_BUILD_TYPE: ${CMAKE_BUILD_TYPE}")
endif()

message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")

set(GMOCK_SRC_DIR "/usr/src/gmock" CACHE STRING "Path to google-mock sources.")
add_subdirectory(${GMOCK_SRC_DIR} "${CMAKE_CURRENT_BINARY_DIR}/gmock")
endmacro()

0 comments on commit 8e7996f

Please sign in to comment.