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

Commit

Permalink
cmake improvements (#474)
Browse files Browse the repository at this point in the history
1. support install and find_package
2. fix dependencies version
3. use external abseil and prometheus-cpp if possible
4. fix abseil build option
5. add flag to disable test
6. fix issue that target name is different if is included as sub directory
7. enable ci for cmake build
  • Loading branch information
wangzw authored Jan 23, 2023
1 parent b2696f6 commit 5e5f263
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 78 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,24 @@ jobs:
- uses: actions/checkout@v3
- run: env BAZEL_CXXOPTS=-std=c++14 bazel build -k //...
- run: env BAZEL_CXXOPTS=-std=c++14 bazel test -k //... --test_tag_filters=-noci
build-and-test-cmake:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Build
run : |
set -eux
CPUS=$(grep -c ^processor /proc/cpuinfo)
mkdir ${GITHUB_WORKSPACE}/build && cd ${GITHUB_WORKSPACE}/build
cmake -DCMAKE_BUILD_TYPE=Release -DOpenCensus_BUILD_TESTING=ON -DCMAKE_INSTALL_PREFIX=${GITHUB_WORKSPACE}/install ..
make -j ${CPUS}
make install
- name: Test
run: |
set -eux
CPUS=$(grep -c ^processor /proc/cpuinfo)
cd ${GITHUB_WORKSPACE}/build
make test -j ${CPUS}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# IntelliJ IDEA
.idea
*.iml
/cmake-build-*

# Eclipse
.classpath
Expand Down
18 changes: 17 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ project(
LANGUAGES CXX)

option(FUZZER "Either OFF or e.g. -fsanitize=fuzzer,address" OFF)
option(OpenCensus_BUILD_TESTING "build test and example" OFF)

if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 11)
Expand All @@ -46,6 +47,21 @@ include(OpenCensusHelpers)
add_subdirectory(opencensus)

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

install(DIRECTORY ${CMAKE_SOURCE_DIR}/opencensus TYPE INCLUDE FILES_MATCHING PATTERN "*.h")

install(EXPORT OpenCensusTargets
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/OpenCensus
NAMESPACE ${PROJECT_NAME}::)

include(CMakePackageConfigHelpers)

configure_package_config_file(cmake/OpenCensusConfig.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/OpenCensusConfig.cmake
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/OpenCensus)

install(FILES ${CMAKE_CURRENT_BINARY_DIR}/OpenCensusConfig.cmake
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/OpenCensus)
4 changes: 4 additions & 0 deletions cmake/OpenCensusConfig.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@PACKAGE_INIT@

include(${CMAKE_CURRENT_LIST_DIR}/OpenCensusTargets.cmake)
check_required_components(OpenCensus)
78 changes: 44 additions & 34 deletions cmake/OpenCensusDeps.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,23 @@ include(FetchContent)
FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest
GIT_TAG master)
GIT_TAG v1.13.0)
FetchContent_Declare(
abseil
GIT_REPOSITORY https://github.com/abseil/abseil-cpp
GIT_TAG master)
GIT_TAG 20220623.1)
FetchContent_Declare(
prometheus
GIT_REPOSITORY https://github.com/jupp0r/prometheus-cpp
GIT_TAG master)
GIT_TAG v1.1.0)
FetchContent_Declare(
benchmark
GIT_REPOSITORY https://github.com/google/benchmark
GIT_TAG main)
GIT_TAG v1.5.6)

FetchContent_GetProperties(googletest)
if(BUILD_TESTING)
message(STATUS "Dependency: googletest (BUILD_TESTING=${BUILD_TESTING})")
if(BUILD_TESTING AND OpenCensus_BUILD_TESTING)
message(STATUS "Dependency: download googletest")
if(NOT googletest_POPULATED)
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
# All the libraries in the build must use either /MD or /MT (runtime
Expand All @@ -54,39 +54,49 @@ if(BUILD_TESTING)
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.
find_package(absl CONFIG QUIET)
if(NOT TARGET absl::config AND NOT absl_FOUND)
FetchContent_GetProperties(abseil)
if(NOT abseil_POPULATED)
message(STATUS "Dependency: download abseil")
set(ABSL_BUILD_TESTING OFF) # Don't include abseil tests.
set(ABSL_PROPAGATE_CXX_STD ON)
set(ABSL_ENABLE_INSTALL ON)
FetchContent_Populate(abseil)
add_subdirectory(${abseil_SOURCE_DIR} ${abseil_BINARY_DIR} EXCLUDE_FROM_ALL)
endif()
else()
message("Using external abseil")
endif()

FetchContent_GetProperties(prometheus)
if(NOT prometheus_POPULATED)
message(STATUS "Dependency: prometheus")
set(ENABLE_PUSH
OFF
CACHE BOOL "Build prometheus-cpp push library" FORCE)
set(ENABLE_PULL
OFF
CACHE BOOL "Build prometheus-cpp pull library" FORCE)
set(ENABLE_COMPRESSION
OFF
CACHE BOOL "Enable gzip compression for prometheus-cpp" FORCE)
set(ENABLE_TESTING
OFF
CACHE BOOL "Build test for prometheus-cpp" FORCE)
FetchContent_Populate(prometheus)
add_subdirectory(${prometheus_SOURCE_DIR} ${prometheus_BINARY_DIR}
EXCLUDE_FROM_ALL)
find_package(prometheus-cpp CONFIG QUIET)
if(NOT TARGET prometheus-cpp::core AND NOT prometheus-cpp_FOUND)
FetchContent_GetProperties(prometheus)
if(NOT prometheus_POPULATED)
message(STATUS "Dependency: download prometheus")
set(ENABLE_PUSH
OFF
CACHE BOOL "Build prometheus-cpp push library" FORCE)
set(ENABLE_PULL
OFF
CACHE BOOL "Build prometheus-cpp pull library" FORCE)
set(ENABLE_COMPRESSION
OFF
CACHE BOOL "Enable gzip compression for prometheus-cpp" FORCE)
set(ENABLE_TESTING
OFF
CACHE BOOL "Build test for prometheus-cpp" FORCE)
FetchContent_Populate(prometheus)
add_subdirectory(${prometheus_SOURCE_DIR} ${prometheus_BINARY_DIR}
EXCLUDE_FROM_ALL)
endif()
else()
message("Using external prometheus-cpp")
endif()

FetchContent_GetProperties(benchmark)
if(BUILD_TESTING)
message(STATUS "Dependency: benchmark (BUILD_TESTING=${BUILD_TESTING})")
if(BUILD_TESTING AND OpenCensus_BUILD_TESTING)
message(STATUS "Dependency: download benchmark")
if(NOT benchmark_POPULATED)
set(BENCHMARK_ENABLE_TESTING
OFF
Expand Down
79 changes: 39 additions & 40 deletions cmake/OpenCensusHelpers.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,18 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# Prepends opencensus_ to all deps that aren't in a :: namespace.
function(prepend_opencensus OUT DEPS)
set(_DEPS "")
foreach(dep ${DEPS})
if("${dep}" MATCHES "::")
list(APPEND _DEPS "${dep}")
else()
list(APPEND _DEPS "opencensus_${dep}")
endif()
endforeach()
set(${OUT}
${_DEPS}
PARENT_SCOPE)
endfunction()

# Helper function like bazel's cc_test. Usage:
#
# opencensus_test(trace_some_test internal/some_test.cc dep1 dep2...)
function(opencensus_test NAME SRC)
if(BUILD_TESTING)
set(_NAME "opencensus_${NAME}")
add_executable(${_NAME} ${SRC})
prepend_opencensus(DEPS "${ARGN}")
target_link_libraries(${_NAME} "${DEPS}" gmock gtest_main)
add_test(NAME ${_NAME} COMMAND ${_NAME})
if(BUILD_TESTING AND OpenCensus_BUILD_TESTING)
add_executable(${NAME} ${SRC})
target_include_directories(${NAME}
PUBLIC
"$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>"
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
target_link_libraries(${NAME} "${ARGN}" gmock gtest_main)
add_test(NAME ${NAME} COMMAND ${NAME})
endif()
endfunction()

Expand All @@ -45,31 +32,41 @@ endfunction()
# opencensus_benchmark(trace_some_benchmark internal/some_benchmark.cc dep1
# dep2...)
function(opencensus_benchmark NAME SRC)
if(BUILD_TESTING)
set(_NAME "opencensus_${NAME}")
add_executable(${_NAME} ${SRC})
prepend_opencensus(DEPS "${ARGN}")
target_link_libraries(${_NAME} "${DEPS}" benchmark)
if(BUILD_TESTING AND OpenCensus_BUILD_TESTING)
add_executable(${NAME} ${SRC})
target_include_directories(${NAME}
PUBLIC
"$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>"
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
target_link_libraries(${NAME} "${ARGN}" benchmark)
endif()
endfunction()

# 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})
set(_NAME "opencensus_${NAME}")
prepend_opencensus(ARG_DEPS "${ARG_DEPS}")
if(ARG_SRCS)
add_library(${_NAME} ${ARG_SRCS})
target_link_libraries(${_NAME} PUBLIC ${ARG_DEPS})
target_include_directories(${_NAME} PUBLIC ${PROJECT_SOURCE_DIR})
add_library(${NAME} ${ARG_SRCS})
target_link_libraries(${NAME} PUBLIC ${ARG_DEPS})
target_include_directories(${NAME}
PUBLIC
"$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>"
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
else()
add_library(${_NAME} INTERFACE)
target_link_libraries(${_NAME} INTERFACE ${ARG_DEPS})
target_include_directories(${_NAME} INTERFACE ${PROJECT_SOURCE_DIR})
add_library(${NAME} INTERFACE)
target_link_libraries(${NAME} INTERFACE ${ARG_DEPS})
target_include_directories(${NAME}
INTERFACE
"$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>"
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
endif()
install(TARGETS ${NAME} EXPORT OpenCensusTargets
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
if(ARG_PUBLIC)
add_library(${PROJECT_NAME}::${NAME} ALIAS ${_NAME})
add_library(${PROJECT_NAME}::${NAME} ALIAS ${NAME})
endif()
endfunction()

Expand All @@ -78,10 +75,12 @@ endfunction()
# opencensus_fuzzer(trace_some_fuzzer internal/some_fuzzer.cc dep1 dep2...)
function(opencensus_fuzzer NAME SRC)
if(FUZZER)
set(_NAME "opencensus_${NAME}")
add_executable(${_NAME} ${SRC})
prepend_opencensus(DEPS "${ARGN}")
target_link_libraries(${_NAME} "${DEPS}" ${FUZZER})
target_compile_options(${_NAME} PRIVATE ${FUZZER})
add_executable(${NAME} ${SRC})
target_link_libraries(${NAME} "${DEPS}" ${FUZZER})
target_compile_options(${NAME} PRIVATE ${FUZZER})
target_include_directories(${NAME}
PUBLIC
"$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>"
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
endif()
endfunction()
2 changes: 1 addition & 1 deletion opencensus/common/internal/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ opencensus_lib(
opencensus_lib(common_stats_object DEPS absl::time)

# Define NOMINMAX to fix build errors when compiling with MSVC.
target_compile_definitions(opencensus_common_stats_object
target_compile_definitions(common_stats_object
INTERFACE $<$<CXX_COMPILER_ID:MSVC>:NOMINMAX>)

opencensus_lib(common_string_vector_hash DEPS absl::hash)
Expand Down
2 changes: 1 addition & 1 deletion opencensus/stats/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ opencensus_lib(
absl::span)

# Define NOMINMAX to fix build errors when compiling with MSVC.
target_compile_definitions(opencensus_stats_core
target_compile_definitions(stats_core
PUBLIC $<$<CXX_COMPILER_ID:MSVC>:NOMINMAX>)

opencensus_lib(
Expand Down
2 changes: 1 addition & 1 deletion opencensus/trace/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ opencensus_lib(
absl::span)

# Define NOMINMAX to fix build errors when compiling with MSVC.
target_compile_definitions(opencensus_trace
target_compile_definitions(trace
PUBLIC $<$<CXX_COMPILER_ID:MSVC>:NOMINMAX>)

opencensus_lib(
Expand Down

0 comments on commit 5e5f263

Please sign in to comment.