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

Commit bd876f6

Browse files
authored
Add experimental CMake support (#238)
This addresses #86
1 parent 0958052 commit bd876f6

23 files changed

+869
-11
lines changed

.cmake-format.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Specify structure for custom cmake functions
2+
additional_commands = {
3+
"opencensus_lib": {
4+
"flags": [
5+
"PUBLIC",
6+
],
7+
"kwargs": {
8+
"HEADERS": "*",
9+
"DEPENDS": "*",
10+
"SOURCES": "*"
11+
}
12+
}
13+
}
14+
15+
# If comment markup is enabled, don't reflow the first comment block in
16+
# eachlistfile. Use this to preserve formatting of your
17+
# copyright/licensestatements.
18+
first_comment_is_literal = True

.gitignore

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,5 @@ bin
2121
# Other
2222
TAGS
2323

24-
# Bazel
25-
bazel-bin
26-
bazel-genfiles
27-
bazel-opencensus-cpp
28-
bazel-out
29-
bazel-testlogs
24+
/bazel-*
25+
/.build

CMakeLists.txt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Copyright 2018, OpenCensus Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
cmake_minimum_required(VERSION 3.5)
16+
17+
# Use ccache if it's present.
18+
find_program(CCACHE_PROGRAM ccache)
19+
if(CCACHE_PROGRAM)
20+
set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE "${CCACHE_PROGRAM}")
21+
set_property(GLOBAL PROPERTY RULE_LAUNCH_LINK "${CCACHE_PROGRAM}")
22+
endif()
23+
24+
project(opencensus-cpp VERSION 0.3.0 LANGUAGES CXX)
25+
26+
set(CMAKE_CXX_STANDARD 11)
27+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
28+
set(OPENCENSUS_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
29+
30+
include(CTest) # Defines option BUILD_TESTING.
31+
enable_testing()
32+
33+
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
34+
include(OpenCensusDeps)
35+
include(OpenCensusHelpers)
36+
37+
# OpenCensus code.
38+
add_subdirectory(opencensus)
39+
40+
# Example code only if testing is enabled.
41+
if(BUILD_TESTING)
42+
add_subdirectory(examples)
43+
endif()

cmake/OpenCensusDeps.cmake

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Copyright 2018, OpenCensus Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
if(BUILD_TESTING)
16+
if(NOT TARGET gtest_main)
17+
message(STATUS "Dependency: googletest (BUILD_TESTING=${BUILD_TESTING})")
18+
19+
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/googletest.CMakeLists.txt
20+
${CMAKE_BINARY_DIR}/googletest-download/CMakeLists.txt)
21+
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
22+
RESULT_VARIABLE result
23+
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download)
24+
if(result)
25+
message(FATAL_ERROR "CMake step failed: ${result}")
26+
endif()
27+
execute_process(COMMAND ${CMAKE_COMMAND} --build .
28+
RESULT_VARIABLE result
29+
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download)
30+
if(result)
31+
message(FATAL_ERROR "Build step failed: ${result}")
32+
endif()
33+
34+
add_subdirectory(${CMAKE_BINARY_DIR}/googletest-src
35+
${CMAKE_BINARY_DIR}/googletest-build EXCLUDE_FROM_ALL)
36+
endif()
37+
endif()
38+
39+
# Load abseil second, it depends on googletest.
40+
if(NOT TARGET absl::base)
41+
message(STATUS "Dependency: abseil")
42+
43+
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/abseil.CMakeLists.txt
44+
${CMAKE_BINARY_DIR}/abseil-download/CMakeLists.txt)
45+
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
46+
RESULT_VARIABLE result
47+
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/abseil-download)
48+
if(result)
49+
message(FATAL_ERROR "CMake step failed: ${result}")
50+
endif()
51+
execute_process(COMMAND ${CMAKE_COMMAND} --build .
52+
RESULT_VARIABLE result
53+
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/abseil-download)
54+
if(result)
55+
message(FATAL_ERROR "Build step failed: ${result}")
56+
endif()
57+
58+
add_subdirectory(${CMAKE_BINARY_DIR}/abseil-src
59+
${CMAKE_BINARY_DIR}/abseil-build EXCLUDE_FROM_ALL)
60+
endif()

cmake/OpenCensusHelpers.cmake

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Copyright 2018, OpenCensus Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# Prepends opencensus_ to all deps that aren't in a :: namespace.
16+
function(prepend_opencensus OUT DEPS)
17+
set(_DEPS "")
18+
foreach(dep ${DEPS})
19+
if("${dep}" MATCHES "::")
20+
list(APPEND _DEPS "${dep}")
21+
else()
22+
list(APPEND _DEPS "opencensus_${dep}")
23+
endif()
24+
endforeach()
25+
set(${OUT} ${_DEPS} PARENT_SCOPE)
26+
endfunction()
27+
28+
# Helper function like bazel's cc_test. Usage:
29+
#
30+
# opencensus_test(trace_some_test internal/some_test.cc dep1 dep2...)
31+
function(opencensus_test NAME SRC)
32+
if(BUILD_TESTING)
33+
set(_NAME "opencensus_${NAME}")
34+
add_executable(${_NAME} ${SRC})
35+
prepend_opencensus(DEPS "${ARGN}")
36+
target_link_libraries(${_NAME} "${DEPS}" gmock gtest_main)
37+
add_test(NAME ${_NAME} COMMAND ${_NAME})
38+
endif()
39+
endfunction()
40+
41+
# Helper function like bazel's cc_library. Libraries are namespaced as
42+
# opencensus_* and public libraries are also aliased as opencensus-cpp::*.
43+
function(opencensus_lib NAME)
44+
cmake_parse_arguments(ARG "PUBLIC" "" "SRCS;DEPS" ${ARGN})
45+
set(_NAME "opencensus_${NAME}")
46+
prepend_opencensus(ARG_DEPS "${ARG_DEPS}")
47+
if(ARG_SRCS)
48+
add_library(${_NAME} ${ARG_SRCS})
49+
target_link_libraries(${_NAME} PUBLIC ${ARG_DEPS})
50+
target_include_directories(${_NAME} PUBLIC ${OPENCENSUS_INCLUDE_DIR})
51+
else()
52+
add_library(${_NAME} INTERFACE)
53+
target_link_libraries(${_NAME} INTERFACE ${ARG_DEPS})
54+
target_include_directories(${_NAME} INTERFACE ${OPENCENSUS_INCLUDE_DIR})
55+
endif()
56+
if(ARG_PUBLIC)
57+
add_library(${PROJECT_NAME}::${NAME} ALIAS ${_NAME})
58+
endif()
59+
endfunction()

cmake/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Experimental CMake support
2+
3+
The files in this directory are mostly for dealing with dependencies.
4+
5+
We use
6+
[ExternalProject](https://cmake.org/cmake/help/latest/module/ExternalProject.html)
7+
to download and build missing dependencies, during CMake's configuration time.
8+
9+
Still TODO for CMake support:
10+
- Benchmarks.
11+
- Top-level examples other than helloworld.
12+
- Leaf examples (e.g. trace/examples/ dir).
13+
- Exporters other than stdout.
14+
- CI (Travis) support.
15+
- No shared library for now.
16+
17+
## Quickstart:
18+
19+
```shell
20+
cmake -H. -B.build
21+
cmake --build .build
22+
(cd .build && ctest --output-on-failure)
23+
./.build/examples/helloworld/opencensus_examples_helloworld
24+
```
25+
26+
Using `.build` as the build dir so that it doesn't collide with the uppercase
27+
BUILD file on Windows.

cmake/abseil.CMakeLists.txt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Copyright 2018, OpenCensus Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
cmake_minimum_required(VERSION 3.5)
16+
17+
project(abseil-download NONE)
18+
19+
include(ExternalProject)
20+
ExternalProject_Add(abseil_project
21+
GIT_REPOSITORY https://github.com/abseil/abseil-cpp
22+
GIT_TAG "master"
23+
SOURCE_DIR "${CMAKE_BINARY_DIR}/abseil-src"
24+
BINARY_DIR "${CMAKE_BINARY_DIR}/abseil-build"
25+
UPDATE_COMMAND ""
26+
PATCH_COMMAND ""
27+
CONFIGURE_COMMAND ""
28+
BUILD_COMMAND ""
29+
INSTALL_COMMAND ""
30+
TEST_COMMAND ""
31+
LOG_DOWNLOAD ON
32+
)

cmake/googletest.CMakeLists.txt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Copyright 2018, OpenCensus Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
cmake_minimum_required(VERSION 3.5)
16+
17+
project(googletest-download NONE)
18+
19+
include(ExternalProject)
20+
ExternalProject_Add(googletest_project
21+
GIT_REPOSITORY https://github.com/abseil/googletest
22+
GIT_TAG "master"
23+
SOURCE_DIR "${CMAKE_BINARY_DIR}/googletest-src"
24+
BINARY_DIR "${CMAKE_BINARY_DIR}/googletest-build"
25+
UPDATE_COMMAND ""
26+
PATCH_COMMAND ""
27+
CONFIGURE_COMMAND ""
28+
BUILD_COMMAND ""
29+
INSTALL_COMMAND ""
30+
TEST_COMMAND ""
31+
LOG_DOWNLOAD ON
32+
)

examples/CMakeLists.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Copyright 2018, OpenCensus Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# add_subdirectory(grpc) TODO
16+
17+
add_subdirectory(helloworld)

examples/helloworld/CMakeLists.txt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Copyright 2018, OpenCensus Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
find_package(Threads REQUIRED)
16+
17+
add_executable(opencensus_examples_helloworld helloworld.cc)
18+
target_link_libraries(opencensus_examples_helloworld
19+
absl::strings
20+
opencensus-cpp::exporters_stats_stdout
21+
opencensus-cpp::exporters_trace_stdout
22+
opencensus-cpp::stats
23+
opencensus-cpp::trace
24+
Threads::Threads)

0 commit comments

Comments
 (0)