2016-08-02 15:07:31 +08:00
|
|
|
# Copyright 2016 The Cartographer Authors
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
include(CMakeParseArguments)
|
|
|
|
|
|
|
|
macro(_parse_arguments ARGS)
|
2016-12-20 18:24:08 +08:00
|
|
|
set(OPTIONS)
|
2016-12-09 21:45:52 +08:00
|
|
|
set(ONE_VALUE_ARG)
|
2016-12-20 18:24:08 +08:00
|
|
|
set(MULTI_VALUE_ARGS SRCS)
|
2016-08-02 15:07:31 +08:00
|
|
|
cmake_parse_arguments(ARG
|
|
|
|
"${OPTIONS}" "${ONE_VALUE_ARG}" "${MULTI_VALUE_ARGS}" ${ARGS})
|
|
|
|
endmacro(_parse_arguments)
|
|
|
|
|
|
|
|
macro(_common_compile_stuff VISIBILITY)
|
|
|
|
set(TARGET_COMPILE_FLAGS "${TARGET_COMPILE_FLAGS} ${GOOG_CXX_FLAGS}")
|
|
|
|
|
2016-10-12 19:39:03 +08:00
|
|
|
set_target_properties(${NAME} PROPERTIES
|
|
|
|
COMPILE_FLAGS ${TARGET_COMPILE_FLAGS})
|
|
|
|
|
2016-12-20 18:24:08 +08:00
|
|
|
target_include_directories(${NAME} PUBLIC ${PROJECT_NAME})
|
|
|
|
target_link_libraries(${NAME} PUBLIC ${PROJECT_NAME})
|
2016-08-02 15:07:31 +08:00
|
|
|
endmacro(_common_compile_stuff)
|
|
|
|
|
2017-05-03 22:55:13 +08:00
|
|
|
function(google_test NAME ARG_SRC)
|
|
|
|
add_executable(${NAME} ${ARG_SRC})
|
2016-08-02 15:07:31 +08:00
|
|
|
_common_compile_stuff("PRIVATE")
|
|
|
|
|
|
|
|
# Make sure that gmock always includes the correct gtest/gtest.h.
|
|
|
|
target_include_directories("${NAME}" SYSTEM PRIVATE
|
2016-10-28 18:23:21 +08:00
|
|
|
"${GMOCK_INCLUDE_DIRS}")
|
2016-12-12 21:56:54 +08:00
|
|
|
target_link_libraries("${NAME}" PUBLIC ${GMOCK_LIBRARIES})
|
2016-08-02 15:07:31 +08:00
|
|
|
|
|
|
|
add_test(${NAME} ${NAME})
|
|
|
|
endfunction()
|
|
|
|
|
2016-10-11 21:18:59 +08:00
|
|
|
function(google_binary NAME)
|
|
|
|
_parse_arguments("${ARGN}")
|
|
|
|
|
2017-05-03 22:55:13 +08:00
|
|
|
add_executable(${NAME} ${ARG_SRCS})
|
2016-10-11 21:18:59 +08:00
|
|
|
|
|
|
|
_common_compile_stuff("PRIVATE")
|
|
|
|
|
|
|
|
install(TARGETS "${NAME}" RUNTIME DESTINATION bin)
|
|
|
|
endfunction()
|
|
|
|
|
2016-12-20 18:24:08 +08:00
|
|
|
# Create a variable 'VAR_NAME'='FLAG'. If VAR_NAME is already set, FLAG is
|
|
|
|
# appended.
|
|
|
|
function(google_add_flag VAR_NAME FLAG)
|
|
|
|
if (${VAR_NAME})
|
|
|
|
set(${VAR_NAME} "${${VAR_NAME}} ${FLAG}" PARENT_SCOPE)
|
|
|
|
else()
|
|
|
|
set(${VAR_NAME} "${FLAG}" PARENT_SCOPE)
|
|
|
|
endif()
|
2016-08-02 15:07:31 +08:00
|
|
|
endfunction()
|
2016-10-12 19:39:03 +08:00
|
|
|
|
|
|
|
macro(google_initialize_cartographer_project)
|
2016-10-28 21:43:13 +08:00
|
|
|
if(CARTOGRAPHER_CMAKE_DIR)
|
|
|
|
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH}
|
|
|
|
${CARTOGRAPHER_CMAKE_DIR}/modules)
|
|
|
|
else()
|
|
|
|
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH}
|
|
|
|
${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules)
|
|
|
|
endif()
|
2017-07-20 20:31:50 +08:00
|
|
|
set(GOOG_CXX_FLAGS "-pthread -std=c++11 -fPIC ${GOOG_CXX_FLAGS}")
|
2016-10-12 19:39:03 +08:00
|
|
|
|
|
|
|
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")
|
2016-10-20 17:29:12 +08:00
|
|
|
google_add_flag(GOOG_CXX_FLAGS "-Werror=missing-braces")
|
2016-10-12 19:39:03 +08:00
|
|
|
google_add_flag(GOOG_CXX_FLAGS "-Werror=reorder")
|
|
|
|
google_add_flag(GOOG_CXX_FLAGS "-Werror=return-type")
|
2017-05-02 22:56:10 +08:00
|
|
|
google_add_flag(GOOG_CXX_FLAGS "-Werror=switch")
|
2016-10-12 19:39:03 +08:00
|
|
|
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")
|
2017-03-30 23:00:50 +08:00
|
|
|
if(FORCE_DEBUG_BUILD)
|
|
|
|
message(WARNING "Building in Debug mode, expect very slow performance.")
|
|
|
|
google_add_flag(GOOG_CXX_FLAGS "-g")
|
|
|
|
else()
|
|
|
|
message(FATAL_ERROR
|
|
|
|
"Compiling in Debug mode is not supported and can cause severely degraded performance. "
|
|
|
|
"You should change the build type to Release. If you want to build in Debug mode anyway, "
|
|
|
|
"call CMake with -DFORCE_DEBUG_BUILD=True"
|
|
|
|
)
|
|
|
|
endif()
|
2017-07-17 16:34:14 +08:00
|
|
|
# Support for Debian packaging CMAKE_BUILD_TYPE
|
|
|
|
elseif(CMAKE_BUILD_TYPE STREQUAL "None")
|
|
|
|
message(WARNING "Building with CMAKE_BUILD_TYPE None, "
|
|
|
|
"please make sure you have set CFLAGS and CXXFLAGS according to your needs.")
|
2016-10-12 19:39:03 +08:00
|
|
|
else()
|
|
|
|
message(FATAL_ERROR "Unknown CMAKE_BUILD_TYPE: ${CMAKE_BUILD_TYPE}")
|
|
|
|
endif()
|
|
|
|
|
|
|
|
message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
|
|
|
|
|
2016-12-21 19:05:15 +08:00
|
|
|
# Add a hook that reruns CMake when source files are added or removed.
|
2017-02-09 22:57:54 +08:00
|
|
|
set(LIST_FILES_CMD "find ${PROJECT_SOURCE_DIR}/ -not -iwholename '*.git*' | sort | sed 's/^/#/'")
|
|
|
|
set(FILES_LIST_PATH "${PROJECT_BINARY_DIR}/AllFiles.cmake")
|
2016-12-21 19:05:15 +08:00
|
|
|
set(DETECT_CHANGES_CMD "bash" "-c" "${LIST_FILES_CMD} | diff -N -q ${FILES_LIST_PATH} - || ${LIST_FILES_CMD} > ${FILES_LIST_PATH}")
|
2017-02-09 22:57:54 +08:00
|
|
|
add_custom_target(${PROJECT_NAME}_detect_changes ALL
|
2016-12-21 19:05:15 +08:00
|
|
|
COMMAND ${DETECT_CHANGES_CMD}
|
|
|
|
VERBATIM
|
|
|
|
)
|
|
|
|
if(NOT EXISTS ${FILES_LIST_PATH})
|
|
|
|
execute_process(COMMAND ${DETECT_CHANGES_CMD})
|
|
|
|
endif()
|
|
|
|
include(${FILES_LIST_PATH})
|
2016-10-12 21:38:12 +08:00
|
|
|
endmacro()
|
|
|
|
|
|
|
|
macro(google_enable_testing)
|
|
|
|
enable_testing()
|
2016-10-28 18:23:21 +08:00
|
|
|
find_package(GMock REQUIRED)
|
2016-10-12 19:39:03 +08:00
|
|
|
endmacro()
|
2017-11-22 23:29:01 +08:00
|
|
|
|
|
|
|
macro(list_remove_item REMOVE_FROM TO_REMOVE)
|
|
|
|
if(${TO_REMOVE})
|
|
|
|
list(REMOVE_ITEM ${REMOVE_FROM} ${${TO_REMOVE}})
|
|
|
|
message(WARNING "Unnecessary use of list_remove_item. Consider using "
|
|
|
|
"list(REMOVE_ITEM ${REMOVE_FROM} \${${TO_REMOVE}}.")
|
|
|
|
endif()
|
|
|
|
endmacro()
|