Skip to content
This repository has been archived by the owner on Jul 31, 2023. It is now read-only.

Support find_package and install #256

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,27 @@ project(opencensus-cpp VERSION 0.3.0 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(OPENCENSUS_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}")

option(BUILD_SHARED_LIBS "Build shared libraries" OFF)

IF(MSVC)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: be consistent IF or if everywhere (I think the trend is towards lowercase).

add_definitions(-DNOMINMAX)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you want target_compile_definitions() in the targets that need this definition, not need to push it on everybody.

ENDIF()

if(CMAKE_BUILD_TYPE STREQUAL "Release")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unusual... is that to avoid installing the headers twice?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so. To be honest I took that example from somewhere else. What is the proper way to do it? :)

option(INSTALL_HEADERS "Install header files" ON)
else()
option(INSTALL_HEADERS "Install header files" OFF)
endif()

list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)

find_package(googletest REQUIRED)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CMake has a native module to find gtest:

https://cmake.org/cmake/help/latest/module/FindGTest.html

why not use it here?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, because you want to download it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, this is the rationale for changing from "inline" ExternalProject_Add to doing it via find_package

  1. Adding install/export failed because I then also had to export the abseil targets, which seemed wrong to me
  2. find_package is then a point of customization: in vcpkg I'll just delete the FindModule.cmake for abseil and googletest, and use the built-in packages for vcpkg
  3. when abseil supports find_package directly (with a install/targets and a config-file) this approach will still work
  4. for GTest the native module is missing gmock, unfortunately :( - else I would be happy to use that
  5. the only thing that does not work properly is that I have to cmake build the abseil and googletest targets before I can build the opencensus. This is because (I think) ExternaProject_Add runs at build time (not configure time) and for some reason cmake is not smart enough to build the dependencies on demand.
    What I want is e.g. to build an opencensus test and automatically build abseil and gtest when they are required, while also supporting install/export for opencensus. I found an approach on stackoverflow, see the other reply. If this works, might that be okay? :)

find_package(abseil REQUIRED)

include(CTest) # Defines option BUILD_TESTING.
enable_testing()

list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
include(OpenCensusDeps)
include(OpenCensusHelpers)

# OpenCensus code.
Expand All @@ -41,3 +55,10 @@ add_subdirectory(opencensus)
if(BUILD_TESTING)
add_subdirectory(examples)
endif()

install(
EXPORT opencensus-targets
FILE opencensus-config.cmake
NAMESPACE opencensus::
DESTINATION share/opencensus
)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: newline at end of file.

278 changes: 278 additions & 0 deletions cmake/Findabseil.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,278 @@
# Copyright 2018 The Cartographer Authors
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the right copyright?

#
# 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.

cmake_minimum_required(VERSION 3.2)

if(NOT TARGET absl_base)
set(prefix ${CMAKE_STATIC_LIBRARY_PREFIX})
set(suffix ${CMAKE_STATIC_LIBRARY_SUFFIX})
include(${CMAKE_ROOT}/Modules/ExternalProject.cmake)
set(ABSEIL_PROJECT_NAME abseil)
set(ABSEIL_PROJECT_SRC_DIR
${CMAKE_CURRENT_BINARY_DIR}/${ABSEIL_PROJECT_NAME}/src/${ABSEIL_PROJECT_NAME})
set(ABSEIL_PROJECT_BUILD_DIR
${CMAKE_CURRENT_BINARY_DIR}/${ABSEIL_PROJECT_NAME}/src/${ABSEIL_PROJECT_NAME}-build)
set(ABSEIL_INCLUDE_DIRS ${ABSEIL_PROJECT_SRC_DIR})

ExternalProject_Add(${ABSEIL_PROJECT_NAME}
PREFIX ${ABSEIL_PROJECT_NAME}
GIT_REPOSITORY https://github.com/abseil/abseil-cpp.git
GIT_TAG 5441bbe1db5d0f2ca24b5b60166367b0966790af
INSTALL_COMMAND ""
BUILD_COMMAND ${CMAKE_COMMAND} --build "${ABSEIL_PROJECT_BUILD_DIR}"
CMAKE_CACHE_ARGS "-DCMAKE_POSITION_INDEPENDENT_CODE:BOOL=ON;-DBUILD_TESTING:BOOL=OFF"
BUILD_BYPRODUCTS "${ABSEIL_LIBRARY_PATH};${ABSEIL_DEPENDENT_LIBRARIES}"
)

# absl_algorithm

add_library(absl_algorithm STATIC IMPORTED GLOBAL)
set_target_properties(absl_algorithm
PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES
${ABSEIL_INCLUDE_DIRS}
IMPORTED_LOCATION
${ABSEIL_PROJECT_BUILD_DIR}/absl/algorithm/${prefix}absl_algorithm${suffix}
)

# absl_base

add_library(absl_base STATIC IMPORTED GLOBAL)
set_target_properties(absl_base
PROPERTIES IMPORTED_LOCATION
${ABSEIL_PROJECT_BUILD_DIR}/absl/base/${prefix}absl_base${suffix}
INTERFACE_INCLUDE_DIRECTORIES
${ABSEIL_PROJECT_SRC_DIR}
)

set_property(TARGET absl_base
PROPERTY INTERFACE_LINK_LIBRARIES
${ABSEIL_PROJECT_BUILD_DIR}/absl/base/${prefix}absl_dynamic_annotations${suffix}
${ABSEIL_PROJECT_BUILD_DIR}/absl/base/${prefix}absl_malloc_internal${suffix}
${ABSEIL_PROJECT_BUILD_DIR}/absl/base/${prefix}absl_spinlock_wait${suffix}
${ABSEIL_PROJECT_BUILD_DIR}/absl/base/${prefix}absl_throw_delegate${suffix}
)

# absl_container

add_library(absl_container STATIC IMPORTED GLOBAL)
set_target_properties(absl_container
PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
${ABSEIL_INCLUDE_DIRS}
IMPORTED_LOCATION
${ABSEIL_PROJECT_BUILD_DIR}/absl/container/${prefix}absl_container${suffix}
)
set_property(TARGET absl_container
PROPERTY INTERFACE_LINK_LIBRARIES
${ABSEIL_PROJECT_BUILD_DIR}/absl/container/${prefix}test_instance_tracker_lib${suffix}
)

# absl_debugging

add_library(absl_debugging STATIC IMPORTED GLOBAL)
set_target_properties(absl_debugging
PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
${ABSEIL_INCLUDE_DIRS}
IMPORTED_LOCATION
${ABSEIL_PROJECT_BUILD_DIR}/absl/debugging/${prefix}absl_debugging${suffix}
)
set_property(TARGET absl_debugging
PROPERTY INTERFACE_LINK_LIBRARIES
${ABSEIL_PROJECT_BUILD_DIR}/absl/debugging/${prefix}absl_symbolize${suffix}
${ABSEIL_PROJECT_BUILD_DIR}/absl/debugging/${prefix}absl_examine_stack${suffix}
${ABSEIL_PROJECT_BUILD_DIR}/absl/debugging/${prefix}absl_failure_signal_handler${suffix}
${ABSEIL_PROJECT_BUILD_DIR}/absl/debugging/${prefix}absl_leak_check${suffix}
${ABSEIL_PROJECT_BUILD_DIR}/absl/debugging/${prefix}absl_stack_consumption${suffix}
${ABSEIL_PROJECT_BUILD_DIR}/absl/debugging/${prefix}absl_stacktrace${suffix}
)

# absl_memory

add_library(absl_memory STATIC IMPORTED GLOBAL)
set_target_properties(absl_memory
PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
${ABSEIL_INCLUDE_DIRS}
IMPORTED_LOCATION
${ABSEIL_PROJECT_BUILD_DIR}/absl/memory/${prefix}absl_memory${suffix}
)

# absl_meta

add_library(absl_meta STATIC IMPORTED GLOBAL)
set_target_properties(absl_meta
PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
${ABSEIL_INCLUDE_DIRS}
IMPORTED_LOCATION
${ABSEIL_PROJECT_BUILD_DIR}/absl/meta/${prefix}absl_meta${suffix}
)

# absl_numeric

add_library(absl_numeric STATIC IMPORTED GLOBAL)
set_target_properties(absl_numeric
PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
${ABSEIL_INCLUDE_DIRS}
IMPORTED_LOCATION
${ABSEIL_PROJECT_BUILD_DIR}/absl/numeric/${prefix}absl_numeric${suffix}
)
set_property(TARGET absl_numeric
PROPERTY INTERFACE_LINK_LIBRARIES
${ABSEIL_PROJECT_BUILD_DIR}/absl/numeric/${prefix}absl_int128${suffix}
)

# absl_strings

add_library(absl_strings STATIC IMPORTED GLOBAL)
set_target_properties(absl_strings
PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
${ABSEIL_INCLUDE_DIRS}
IMPORTED_LOCATION
${ABSEIL_PROJECT_BUILD_DIR}/absl/strings/${prefix}absl_strings${suffix}
)
set_property(TARGET absl_strings
PROPERTY INTERFACE_LINK_LIBRARIES
${ABSEIL_PROJECT_BUILD_DIR}/absl/strings/${prefix}str_format_internal${suffix}
${ABSEIL_PROJECT_BUILD_DIR}/absl/strings/${prefix}str_format_extension_internal${suffix}
${ABSEIL_PROJECT_BUILD_DIR}/absl/strings/${prefix}absl_str_format${suffix}
)

# absl_synchronization

add_library(absl_synchronization STATIC IMPORTED GLOBAL)
set_target_properties(absl_synchronization
PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
${ABSEIL_INCLUDE_DIRS}
IMPORTED_LOCATION
${ABSEIL_PROJECT_BUILD_DIR}/absl/synchronization/${prefix}absl_synchronization${suffix}
)

# absl_time

add_library(absl_time STATIC IMPORTED GLOBAL)
set_target_properties(absl_time
PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
${ABSEIL_INCLUDE_DIRS}
IMPORTED_LOCATION
${ABSEIL_PROJECT_BUILD_DIR}/absl/time/${prefix}absl_time${suffix}
)

# absl_utility

add_library(absl_utility STATIC IMPORTED GLOBAL)
set_target_properties(absl_utility
PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
${ABSEIL_INCLUDE_DIRS}
IMPORTED_LOCATION
${ABSEIL_PROJECT_BUILD_DIR}/absl/utility/${prefix}absl_utility${suffix}
)

# absl_span

add_library(absl_span STATIC IMPORTED GLOBAL)
set_target_properties(absl_span
PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
${ABSEIL_INCLUDE_DIRS}
IMPORTED_LOCATION
${ABSEIL_PROJECT_BUILD_DIR}/absl/types/${prefix}absl_span${suffix}
)

# absl_optional

add_library(absl_optional STATIC IMPORTED GLOBAL)
set_target_properties(absl_optional
PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
${ABSEIL_INCLUDE_DIRS}
IMPORTED_LOCATION
${ABSEIL_PROJECT_BUILD_DIR}/absl/types/${prefix}absl_optional${suffix}
)

# absl_variant

add_library(absl_variant STATIC IMPORTED GLOBAL)
set_target_properties(absl_variant
PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
${ABSEIL_INCLUDE_DIRS}
IMPORTED_LOCATION
${ABSEIL_PROJECT_BUILD_DIR}/absl/types/${prefix}absl_variant${suffix}
)


# missing: absl_hash "${ABSEIL_PROJECT_BUILD_DIR}/absl/hash/${prefix}absl_hash${suffix}"

# absl_types

#add_library(absl_types STATIC IMPORTED GLOBAL)
#set_target_properties(absl_types
# PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
# ${ABSEIL_INCLUDE_DIRS}
#)
#set_target_properties(absl_types
# PROPERTIES INTERFACE_LINK_LIBRRARIES
# "${ABSEIL_PROJECT_BUILD_DIR}/absl/types/${prefix}absl_any${suffix}
# ${ABSEIL_PROJECT_BUILD_DIR}/absl/types/${prefix}absl_bad_any_cast${suffix}
# ${ABSEIL_PROJECT_BUILD_DIR}/absl/types/${prefix}absl_bad_optional_access${suffix}
#
#
# "
#)
#add_dependencies(absl_types ${ABSEIL_PROJECT_NAME})
#add_library(absl::types ALIAS absl_types)

target_link_libraries(absl_algorithm INTERFACE absl_base absl_meta)
target_link_libraries(absl_container INTERFACE absl_algorithm absl_base absl_memory)
target_link_libraries(absl_debugging INTERFACE absl_base)
target_link_libraries(absl_memory INTERFACE absl_meta)
target_link_libraries(absl_meta INTERFACE absl_base)
target_link_libraries(absl_numeric INTERFACE absl_base)
target_link_libraries(absl_strings INTERFACE absl_base absl_memory absl_meta absl_numeric)
target_link_libraries(absl_synchronization INTERFACE absl_base absl_time absl_debugging)
target_link_libraries(absl_time INTERFACE absl_base absl_numeric)
target_link_libraries(absl_utility INTERFACE absl_base absl_meta)
target_link_libraries(absl_span INTERFACE absl_base absl_utility absl_meta absl_algorithm absl_strings)
target_link_libraries(absl_optional INTERFACE absl_utility absl_meta absl_algorithm absl_strings)
target_link_libraries(absl_variant INTERFACE absl_utility absl_meta absl_algorithm absl_strings)

add_dependencies(absl_algorithm ${ABSEIL_PROJECT_NAME})
add_dependencies(absl_base ${ABSEIL_PROJECT_NAME})
add_dependencies(absl_container ${ABSEIL_PROJECT_NAME})
add_dependencies(absl_debugging ${ABSEIL_PROJECT_NAME})
add_dependencies(absl_memory ${ABSEIL_PROJECT_NAME})
add_dependencies(absl_meta ${ABSEIL_PROJECT_NAME})
add_dependencies(absl_numeric ${ABSEIL_PROJECT_NAME})
add_dependencies(absl_strings ${ABSEIL_PROJECT_NAME})
add_dependencies(absl_synchronization ${ABSEIL_PROJECT_NAME})
add_dependencies(absl_time ${ABSEIL_PROJECT_NAME})
add_dependencies(absl_utility ${ABSEIL_PROJECT_NAME})
add_dependencies(absl_span ${ABSEIL_PROJECT_NAME})
add_dependencies(absl_optional ${ABSEIL_PROJECT_NAME})
add_dependencies(absl_variant ${ABSEIL_PROJECT_NAME})

add_library(absl::algorithm ALIAS absl_algorithm)
add_library(absl::base ALIAS absl_base)
add_library(absl::container ALIAS absl_container)
add_library(absl::debugging ALIAS absl_debugging)
add_library(absl::memory ALIAS absl_memory)
add_library(absl::meta ALIAS absl_meta)
add_library(absl::numeric ALIAS absl_numeric)
add_library(absl::strings ALIAS absl_strings)
add_library(absl::synchronization ALIAS absl_synchronization)
add_library(absl::time ALIAS absl_time)
add_library(absl::utility ALIAS absl_utility)
add_library(absl::span ALIAS absl_span)
add_library(absl::optional ALIAS absl_optional)
add_library(absl::variant ALIAS absl_variant)

unset(prefix)
unset(suffix)
endif()
74 changes: 74 additions & 0 deletions cmake/Findgoogletest.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# Copyright 2018 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.

cmake_minimum_required(VERSION 3.2)

if(NOT TARGET gtest)
set(prefix ${CMAKE_STATIC_LIBRARY_PREFIX})
set(suffix ${CMAKE_STATIC_LIBRARY_SUFFIX})
include(${CMAKE_ROOT}/Modules/ExternalProject.cmake)
set(GOOGLETEST_PROJECT_NAME googletest)
set(GOOGLETEST_PROJECT_SRC_DIR
"${CMAKE_CURRENT_BINARY_DIR}/${GOOGLETEST_PROJECT_NAME}-prefix/src/${GOOGLETEST_PROJECT_NAME}")
set(GOOGLETEST_PROJECT_BUILD_DIR
"${CMAKE_CURRENT_BINARY_DIR}/${GOOGLETEST_PROJECT_NAME}-prefix/src/${GOOGLETEST_PROJECT_NAME}-build")
set(GOOGLETEST_INCLUDE_DIRS ${GOOGLETEST_PROJECT_SRC_DIR})

include(ExternalProject)
ExternalProject_Add(${GOOGLETEST_PROJECT_NAME}
GIT_REPOSITORY https://github.com/abseil/googletest
GIT_TAG "master"
INSTALL_COMMAND ""
BUILD_COMMAND ${CMAKE_COMMAND} --build "${GOOGLETEST_PROJECT_BUILD_DIR}"
CMAKE_CACHE_ARGS "-Dgtest_force_shared_crt:BOOL=ON"
)

file(MAKE_DIRECTORY ${GOOGLETEST_PROJECT_SRC_DIR}/googletest/include)
file(MAKE_DIRECTORY ${GOOGLETEST_PROJECT_SRC_DIR}/googlemock/include)

add_library(gtest STATIC IMPORTED)
set_target_properties(gtest PROPERTIES
IMPORTED_LOCATION_DEBUG ${GOOGLETEST_PROJECT_BUILD_DIR}/lib/${prefix}gtest${suffix}
IMPORTED_LOCATION_RELEASE ${GOOGLETEST_PROJECT_BUILD_DIR}/lib/${prefix}gtestd${suffix}
IMPORTED_LINK_INTERFACE_LIBRARIES "${CMAKE_THREAD_LIBS_INIT}"
INTERFACE_INCLUDE_DIRECTORIES ${GOOGLETEST_PROJECT_SRC_DIR}/googletest/include)
add_dependencies(gtest googletest)

add_library(gtest_main STATIC IMPORTED)
set_target_properties(gtest_main PROPERTIES
IMPORTED_LOCATION_DEBUG ${GOOGLETEST_PROJECT_BUILD_DIR}/lib/${prefix}gtest_maind${suffix}
IMPORTED_LOCATION_RELEASE ${GOOGLETEST_PROJECT_BUILD_DIR}/lib/${prefix}gtest_main${suffix}
IMPORTED_LINK_INTERFACE_LIBRARIES "${CMAKE_THREAD_LIBS_INIT}"
INTERFACE_INCLUDE_DIRECTORIES ${GOOGLETEST_PROJECT_SRC_DIR}/googletest/include)
add_dependencies(gtest_main googletest)

add_library(gmock STATIC IMPORTED)
set_target_properties(gmock PROPERTIES
IMPORTED_LOCATION_DEBUG ${GOOGLETEST_PROJECT_BUILD_DIR}/lib/${prefix}gmockd${suffix}
IMPORTED_LOCATION_RELEASE ${GOOGLETEST_PROJECT_BUILD_DIR}/lib/${prefix}gmock${suffix}
IMPORTED_LINK_INTERFACE_LIBRARIES "${CMAKE_THREAD_LIBS_INIT}"
INTERFACE_INCLUDE_DIRECTORIES ${GOOGLETEST_PROJECT_SRC_DIR}/googlemock/include)
add_dependencies(gmock googletest)

add_library(gmock_main STATIC IMPORTED)
set_target_properties(gmock_main PROPERTIES
IMPORTED_LOCATION_DEBUG ${GOOGLETEST_PROJECT_BUILD_DIR}/lib/${prefix}gmock_maind${suffix}
IMPORTED_LOCATION_RELEASE ${GOOGLETEST_PROJECT_BUILD_DIR}/lib/${prefix}gmock_main${suffix}
IMPORTED_LINK_INTERFACE_LIBRARIES "${CMAKE_THREAD_LIBS_INIT}"
INTERFACE_INCLUDE_DIRECTORIES ${GOOGLETEST_PROJECT_SRC_DIR}/googlemock/include)
add_dependencies(gmock_main googletest)

unset(prefix)
unset(suffix)
endif()
Loading