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

Experimental CMake support #238

Merged
merged 9 commits into from
Nov 29, 2018
Merged
Show file tree
Hide file tree
Changes from 8 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
18 changes: 18 additions & 0 deletions .cmake-format.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Specify structure for custom cmake functions
additional_commands = {
"opencensus_lib": {
"flags": [
"PUBLIC",
],
"kwargs": {
"HEADERS": "*",
"DEPENDS": "*",
"SOURCES": "*"
}
}
}

# If comment markup is enabled, don't reflow the first comment block in
# eachlistfile. Use this to preserve formatting of your
# copyright/licensestatements.
first_comment_is_literal = True
8 changes: 2 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,5 @@ bin
# Other
TAGS

# Bazel
bazel-bin
bazel-genfiles
bazel-opencensus-cpp
bazel-out
bazel-testlogs
/bazel-*
/.build
43 changes: 43 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Copyright 2018, OpenCensus 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.5)

# Use ccache if it's present.
find_program(CCACHE_PROGRAM ccache)
if(CCACHE_PROGRAM)
set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE "${CCACHE_PROGRAM}")
set_property(GLOBAL PROPERTY RULE_LAUNCH_LINK "${CCACHE_PROGRAM}")
endif()

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}")

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

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

# OpenCensus code.
add_subdirectory(opencensus)

# Example code only if testing is enabled.
if(BUILD_TESTING)
add_subdirectory(examples)
endif()
60 changes: 60 additions & 0 deletions cmake/OpenCensusDeps.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Copyright 2018, OpenCensus 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.

if(BUILD_TESTING)
Copy link

Choose a reason for hiding this comment

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

Why not use externalproject_add() for googletest too?

Copy link
Contributor Author

@g-easy g-easy Nov 22, 2018

Choose a reason for hiding this comment

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

I thought I was. If BUILD_TESTING is enabled, I do the usual dance starting on L19 with configure_file, which uses cmake/googletest.CMakeLists.txt which has the externalproject_add() call.

if(NOT TARGET gtest_main)
message(STATUS "Dependency: googletest (BUILD_TESTING=${BUILD_TESTING})")

configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/googletest.CMakeLists.txt
${CMAKE_BINARY_DIR}/googletest-download/CMakeLists.txt)
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download)
if(result)
message(FATAL_ERROR "CMake step failed: ${result}")
endif()
execute_process(COMMAND ${CMAKE_COMMAND} --build .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download)
if(result)
message(FATAL_ERROR "Build step failed: ${result}")
endif()

add_subdirectory(${CMAKE_BINARY_DIR}/googletest-src
${CMAKE_BINARY_DIR}/googletest-build EXCLUDE_FROM_ALL)
endif()
endif()

# Load abseil second, it depends on googletest.
if(NOT TARGET absl::base)
message(STATUS "Dependency: abseil")

configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/abseil.CMakeLists.txt
${CMAKE_BINARY_DIR}/abseil-download/CMakeLists.txt)
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/abseil-download)
if(result)
message(FATAL_ERROR "CMake step failed: ${result}")
endif()
execute_process(COMMAND ${CMAKE_COMMAND} --build .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/abseil-download)
if(result)
message(FATAL_ERROR "Build step failed: ${result}")
endif()

add_subdirectory(${CMAKE_BINARY_DIR}/abseil-src
${CMAKE_BINARY_DIR}/abseil-build EXCLUDE_FROM_ALL)
endif()
59 changes: 59 additions & 0 deletions cmake/OpenCensusHelpers.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Copyright 2018, OpenCensus 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.

# 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})
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 ${OPENCENSUS_INCLUDE_DIR})
else()
add_library(${_NAME} INTERFACE)
target_link_libraries(${_NAME} INTERFACE ${ARG_DEPS})
target_include_directories(${_NAME} INTERFACE ${OPENCENSUS_INCLUDE_DIR})
endif()
if(ARG_PUBLIC)
add_library(${PROJECT_NAME}::${NAME} ALIAS ${_NAME})
endif()
endfunction()
27 changes: 27 additions & 0 deletions cmake/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Experimental CMake support

The files in this directory are mostly for dealing with dependencies.

We use
[ExternalProject](https://cmake.org/cmake/help/latest/module/ExternalProject.html)
to download and build missing dependencies, during CMake's configuration time.

Still TODO for CMake support:
- Benchmarks.
- Top-level examples other than helloworld.
- Leaf examples (e.g. trace/examples/ dir).
- Exporters other than stdout.
- CI (Travis) support.
- No shared library for now.

## Quickstart:

```shell
cmake -H. -B.build
cmake --build .build
(cd .build && ctest --output-on-failure)
./.build/examples/helloworld/opencensus_examples_helloworld
```

Using `.build` as the build dir so that it doesn't collide with the uppercase
BUILD file on Windows.
32 changes: 32 additions & 0 deletions cmake/abseil.CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright 2018, OpenCensus 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.5)

project(abseil-download NONE)

include(ExternalProject)
ExternalProject_Add(abseil_project
GIT_REPOSITORY https://github.com/abseil/abseil-cpp
GIT_TAG "master"
SOURCE_DIR "${CMAKE_BINARY_DIR}/abseil-src"
BINARY_DIR "${CMAKE_BINARY_DIR}/abseil-build"
UPDATE_COMMAND ""
PATCH_COMMAND ""
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
TEST_COMMAND ""
LOG_DOWNLOAD ON
)
32 changes: 32 additions & 0 deletions cmake/googletest.CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright 2018, OpenCensus 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.5)

project(googletest-download NONE)

include(ExternalProject)
ExternalProject_Add(googletest_project
GIT_REPOSITORY https://github.com/abseil/googletest
GIT_TAG "master"
SOURCE_DIR "${CMAKE_BINARY_DIR}/googletest-src"
BINARY_DIR "${CMAKE_BINARY_DIR}/googletest-build"
UPDATE_COMMAND ""
PATCH_COMMAND ""
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
TEST_COMMAND ""
LOG_DOWNLOAD ON
)
17 changes: 17 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright 2018, OpenCensus 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.

# add_subdirectory(grpc) TODO
Copy link

Choose a reason for hiding this comment

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

Instead of using grpc as a subdirectory consider using it via externalproject_add like you do for Abseil. That way you do not get their targets in your namespace, and you would be testing a configuration that more closely resembles what you get via find_package().

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

But this is examples/grpc/ which is a hello world client + server.

I haven't gotten as far as adding the grpc integration yet. It will probably happen in a followup PR.


add_subdirectory(helloworld)
24 changes: 24 additions & 0 deletions examples/helloworld/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright 2018, OpenCensus 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.

find_package(Threads REQUIRED)

add_executable(opencensus_examples_helloworld helloworld.cc)
target_link_libraries(opencensus_examples_helloworld
absl::strings
opencensus-cpp::exporters_stats_stdout
opencensus-cpp::exporters_trace_stdout
opencensus-cpp::stats
opencensus-cpp::trace
Threads::Threads)
20 changes: 20 additions & 0 deletions opencensus/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright 2018, OpenCensus 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.

add_subdirectory(common)
add_subdirectory(context)
add_subdirectory(exporters)
add_subdirectory(stats)
add_subdirectory(tags)
add_subdirectory(trace)
15 changes: 15 additions & 0 deletions opencensus/common/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright 2018, OpenCensus 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.

add_subdirectory(internal)
Loading