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

CMake stackdriver enhancements #447

Open
wants to merge 12 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
39 changes: 39 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ project(

option(FUZZER "Either OFF or e.g. -fsanitize=fuzzer,address" OFF)

option(BUILD_STACKDRIVER_EXPORTER "Build the stackdriver exporter" OFF)

if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
Expand All @@ -38,14 +40,51 @@ enable_testing()

list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)

# There are cyclical dependencies ( betwen libopencensus_trace.so and
# libopencensus_context.so ) that prevent from building the code as is as shared
# libraries without undefined symbols...
#
# Moreover, because of the way I built gRPC c++ apis within google-cloud-cpp as
# shared libs, with a specific abseil version _as shared lib_

if(BUILD_SHARED_LIBS)
set(POSITION_INDEPENDENT_CODE ON)
set(BUILD_SHARED_LIBS NO)
endif()
# clearly there are problems with cyclical deps in this code ( context, trace )
if(BUILD_SHARED_LIBS)
add_link_options("LINKER:--no-undefined")
Copy link
Author

Choose a reason for hiding this comment

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

this requires a version of cmake that's more recent than the one in the CI containers... consider a platform-aware version of this:

set( CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--no-undefined" )

endif()

include(GNUInstallDirs)

include(OpenCensusDeps)

include(OpenCensusHelpers)

if(BUILD_SHARED_LIBS)
Copy link
Author

Choose a reason for hiding this comment

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

redundant

add_link_options("LINKER:--no-undefined")
endif()

# OpenCensus code.
add_subdirectory(opencensus)

# Example code only if testing is enabled.
if(BUILD_TESTING)
add_subdirectory(examples)
endif()

install(EXPORT opencensus-cpp-targets
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/opencensus-cpp)

# we still need to create and export opencensus-cpp-config.cmake

# Create and install the CMake configuration files.
configure_file("${CMAKE_CURRENT_LIST_DIR}/cmake/config.cmake.in"
"opencensus-cpp-config.cmake" @ONLY)
configure_file("${CMAKE_CURRENT_LIST_DIR}/cmake/config-version.cmake.in"
"opencensus-cpp-config-version.cmake" @ONLY)

install(FILES "${CMAKE_CURRENT_BINARY_DIR}/opencensus-cpp-config.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/opencensus-cpp-config-version.cmake"
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/opencensus-cpp")
131 changes: 75 additions & 56 deletions cmake/OpenCensusDeps.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -14,56 +14,67 @@

include(FetchContent)

FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest
GIT_TAG master)
FetchContent_Declare(
abseil
GIT_REPOSITORY https://github.com/abseil/abseil-cpp
GIT_TAG master)
FetchContent_Declare(
prometheus
GIT_REPOSITORY https://github.com/jupp0r/prometheus-cpp
GIT_TAG master)
FetchContent_Declare(
benchmark
GIT_REPOSITORY https://github.com/google/benchmark
GIT_TAG master)
find_package(googletest)
if(NOT googletest_FOUND)
FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest
GIT_TAG master)

FetchContent_GetProperties(googletest)
if(BUILD_TESTING)
message(STATUS "Dependency: googletest (BUILD_TESTING=${BUILD_TESTING})")
if(NOT googletest_POPULATED)
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
# All the libraries in the build must use either /MD or /MT (runtime
# library to link)
#
# force this option to ON so that Google Test will use /MD instead of /MT
# /MD is now the default for Visual Studio, so it should be our default,
# too
option(
gtest_force_shared_crt
"Use shared (DLL) run-time lib even when Google Test is built as static lib."
ON)
endif()
FetchContent_GetProperties(googletest)
if(BUILD_TESTING)
message(STATUS "Dependency: googletest (BUILD_TESTING=${BUILD_TESTING})")
if(NOT googletest_POPULATED)
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
# All the libraries in the build must use either /MD or /MT (runtime
# library to link)
#
# force this option to ON so that Google Test will use /MD instead of
# /MT /MD is now the default for Visual Studio, so it should be our
# default, too
option(
gtest_force_shared_crt
"Use shared (DLL) run-time lib even when Google Test is built as static lib."
ON)
endif()

FetchContent_Populate(googletest)
add_subdirectory(${googletest_SOURCE_DIR} ${googletest_BINARY_DIR}
EXCLUDE_FROM_ALL)
FetchContent_Populate(googletest)
add_subdirectory(${googletest_SOURCE_DIR} ${googletest_BINARY_DIR}
EXCLUDE_FROM_ALL)
endif()
endif()
endif()

FetchContent_GetProperties(abseil)
if(NOT abseil_POPULATED)
message(STATUS "Dependency: abseil")
set(orig_BUILD_TESTING "${BUILD_TESTING}")
set(BUILD_TESTING OFF) # Don't include abseil tests.
FetchContent_Populate(abseil)
add_subdirectory(${abseil_SOURCE_DIR} ${abseil_BINARY_DIR} EXCLUDE_FROM_ALL)
set(BUILD_TESTING "${orig_BUILD_TESTING}") # Restore value.
# the right thing to do would be to _always_ build abseil as a static library
# with -fPIC in case BUILD_SHARED_LIBS is on. it is problematic with the way I
# currently build googleapis cpp files within google-cloud-cpp
#
# the crux of the matter is that I need to link opencensus-cpp shared libs into
# applications that already have their own libcurl, libgrpc* ...

find_package(absl)
if(NOT absl_FOUND)
FetchContent_Declare(
abseil
GIT_REPOSITORY https://github.com/abseil/abseil-cpp
GIT_TAG master)

FetchContent_GetProperties(abseil)
if(NOT abseil_POPULATED)
message(STATUS "Dependency: abseil")
set(orig_BUILD_TESTING "${BUILD_TESTING}")
set(BUILD_TESTING OFF) # Don't include abseil tests.
FetchContent_Populate(abseil)
add_subdirectory(${abseil_SOURCE_DIR} ${abseil_BINARY_DIR} EXCLUDE_FROM_ALL)
set(BUILD_TESTING "${orig_BUILD_TESTING}") # Restore value.
endif()
endif()

FetchContent_Declare(
prometheus
GIT_REPOSITORY https://github.com/jupp0r/prometheus-cpp
GIT_TAG master)

FetchContent_GetProperties(prometheus)
if(NOT prometheus_POPULATED)
message(STATUS "Dependency: prometheus")
Expand All @@ -84,18 +95,26 @@ if(NOT prometheus_POPULATED)
EXCLUDE_FROM_ALL)
endif()

FetchContent_GetProperties(benchmark)
if(BUILD_TESTING)
message(STATUS "Dependency: benchmark (BUILD_TESTING=${BUILD_TESTING})")
if(NOT benchmark_POPULATED)
set(BENCHMARK_ENABLE_TESTING
OFF
CACHE BOOL "Enable testing of the benchmark library." FORCE)
set(BENCHMARK_ENABLE_GTEST_TESTS
OFF
CACHE BOOL "Enable building the unit tests which depend on gtest" FORCE)
FetchContent_Populate(benchmark)
add_subdirectory(${benchmark_SOURCE_DIR} ${benchmark_BINARY_DIR}
EXCLUDE_FROM_ALL)
find_package(benchmark)
if(NOT benchmark_FOUND)
FetchContent_Declare(
benchmark
GIT_REPOSITORY https://github.com/google/benchmark
GIT_TAG master)
FetchContent_GetProperties(benchmark)
if(BUILD_TESTING)
message(STATUS "Dependency: benchmark (BUILD_TESTING=${BUILD_TESTING})")
if(NOT benchmark_POPULATED)
set(BENCHMARK_ENABLE_TESTING
OFF
CACHE BOOL "Enable testing of the benchmark library." FORCE)
set(BENCHMARK_ENABLE_GTEST_TESTS
OFF
CACHE BOOL "Enable building the unit tests which depend on gtest"
FORCE)
FetchContent_Populate(benchmark)
add_subdirectory(${benchmark_SOURCE_DIR} ${benchmark_BINARY_DIR}
EXCLUDE_FROM_ALL)
endif()
endif()
endif()
88 changes: 84 additions & 4 deletions cmake/OpenCensusHelpers.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function(prepend_opencensus OUT DEPS)
set(_DEPS "")
foreach(dep ${DEPS})
if("${dep}" MATCHES "::")
list(APPEND _DEPS "${dep}")
list(APPEND _DEPS "$<BUILD_INTERFACE:${dep}>")
else()
list(APPEND _DEPS "opencensus_${dep}")
endif()
Expand Down Expand Up @@ -53,23 +53,103 @@ function(opencensus_benchmark NAME SRC)
endif()
endfunction()

include(GNUInstallDirs)

#
# install( TARGETS PUBLIC_HEADERS DESTINATION foo ) does not work well when we
# declare opencensus_lib( libfoo PUBLIC HDRS bar/baz.h SRCS bar/bar.cc ) it
# will generate foo/baz.h not foo/bar/baz.hh
#
# install_headers_with_subdirectories( PUBLIC_HEADERS item1 item2 ..
# INSTALL_FOLDER dir EXPORT export_name )
#
function(install_headers_with_subdirectories)
set(options "")
set(singleValued "INSTALL_FOLDER")
set(multiValued "PUBLIC_HEADER")

cmake_parse_arguments(ARG "${options}" "${singleValued}" "${multiValued}"
${ARGN})

foreach(header ${ARG_PUBLIC_HEADER})

get_filename_component(dir ${header} DIRECTORY)

install(FILES ${header} DESTINATION "${ARG_INSTALL_FOLDER}/${dir}")

endforeach()

endfunction(install_headers_with_subdirectories)

# Helper function like bazel's cc_library. Libraries are namespaced as
# opencensus_* and public libraries are also aliased as opencensus-cpp::*.
function(opencensus_lib NAME)
cmake_parse_arguments(ARG "PUBLIC" "" "SRCS;DEPS" ${ARGN})
cmake_parse_arguments(ARG "PUBLIC;PRIVATE" "" "HDRS;SRCS;DEPS" ${ARGN})
set(_NAME "opencensus_${NAME}")
prepend_opencensus(ARG_DEPS "${ARG_DEPS}")

string(REPLACE "${PROJECT_SOURCE_DIR}/" "" _current_dir_relative_path
"${CMAKE_CURRENT_LIST_DIR}")

if(ARG_SRCS)
add_library(${_NAME} ${ARG_SRCS})
target_link_libraries(${_NAME} PUBLIC ${ARG_DEPS})
target_include_directories(${_NAME} PUBLIC ${PROJECT_SOURCE_DIR})
target_include_directories(
${_NAME} PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>
$<INSTALL_INTERFACE:include>)
else()
add_library(${_NAME} INTERFACE)
target_link_libraries(${_NAME} INTERFACE ${ARG_DEPS})
target_include_directories(${_NAME} INTERFACE ${PROJECT_SOURCE_DIR})
target_include_directories(
${_NAME} INTERFACE $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>
$<INSTALL_INTERFACE:include>)
endif()
if(ARG_PUBLIC)
add_library(${PROJECT_NAME}::${NAME} ALIAS ${_NAME})

if(ARG_HDRS)
# this will install them set_target_properties( ${_NAME} PROPERTIES
# PUBLIC_HEADER "${ARG_HDRS}" )
install_headers_with_subdirectories(
PUBLIC_HEADER ${ARG_HDRS} INSTALL_FOLDER
"${CMAKE_INSTALL_INCLUDEDIR}/${_current_dir_relative_path}")
endif()

install(
TARGETS ${_NAME}
EXPORT opencensus-cpp-targets
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" # PUBLIC_HEADER DESTINATION
# "${CMAKE_INSTALL_INCLUDEDIR}/${_current_dir_relative_path}"
)
elseif(ARG_PRIVATE)
# what we really wanted to do, if linking private static libraries into
# public ones worked without having to export the private one...
else()

if(ARG_HDRS)
# I think we have API problems whereby the internal includes are required
# by clients... Comment out this line if it is not the case...
# set_target_properties( ${_NAME} PROPERTIES PRIVATE_HEADER "${ARG_HDRS}"
# )
install_headers_with_subdirectories(
PUBLIC_HEADER ${ARG_HDRS} INSTALL_FOLDER
"${CMAKE_INSTALL_INCLUDEDIR}/${_current_dir_relative_path}")
endif()

# fight export bug ? I don't want these installed, I don't want the
# associated headers installed I want them as depedencies to public static
# libraries that aggregate them....
install(
TARGETS ${_NAME}
EXPORT opencensus-cpp-targets
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" # PUBLIC_HEADER DESTINATION
# "${CMAKE_INSTALL_INCLUDEDIR}/${_current_dir_relative_path}"
)

endif()
endfunction()

Expand Down
13 changes: 13 additions & 0 deletions cmake/config-version.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#
set(PACKAGE_VERSION "@CMAKE_PROJECT_VERSION@")

# Check whether the requested PACKAGE_FIND_VERSION is compatible
if("${PACKAGE_VERSION}" VERSION_LESS "${PACKAGE_FIND_VERSION}")
set(PACKAGE_VERSION_COMPATIBLE FALSE)
else()
set(PACKAGE_VERSION_COMPATIBLE TRUE)
if ("${PACKAGE_VERSION}" VERSION_EQUAL "${PACKAGE_FIND_VERSION}")
set(PACKAGE_VERSION_EXACT TRUE)
endif()
endif()

Loading