Skip to content

Commit c2bb8b4

Browse files
authored
feat: support cmake compile (#124)
1 parent 90da327 commit c2bb8b4

File tree

17 files changed

+921
-0
lines changed

17 files changed

+921
-0
lines changed

.github/workflows/main.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ jobs:
3737
- name: Run bazel test with CLANG c++17
3838
run: |
3939
bazel test --config=clang --cxxopt=-std=c++17 //...
40+
- name: Install cmake dependencies and run cmake compile
41+
run: |
42+
sudo apt update
43+
sudo apt -y install cmake
44+
sudo git clone -b v9.1.0 https://github.com/apache/skywalking-data-collect-protocol.git ./3rdparty/skywalking-data-collect-protocol
45+
sudo git clone -b v1.46.6 https://github.com/grpc/grpc.git --recursive
46+
sudo cmake -S ./grpc -B ./grpc/build
47+
sudo cmake --build ./grpc/build --parallel 8 --target install
48+
sudo cmake -S . -B ./build
49+
sudo cmake --build ./build
4050
- name: Install lcov and genhtml and link llvm
4151
run: |
4252
sudo apt update

CMakeLists.txt

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
2+
3+
if(POLICY CMP0091)
4+
cmake_policy(SET CMP0091 NEW) # recognize CMAKE_MSVC_RUNTIME_LIBRARY
5+
endif()
6+
7+
project(cpp2sky
8+
VERSION 0.5.1
9+
DESCRIPTION "Distributed tracing and monitor SDK in CPP for Apache SkyWalking APM"
10+
HOMEPAGE_URL "https://github.com/SkyAPM/cpp2sky"
11+
)
12+
13+
option(OVERRIDE_CXX_STANDARD_FLAGS "Force building with -std=c++11 even if the CXXFLAGS are configured differently" ON)
14+
option(SPDLOG_FETCHCONTENT "Using spdlog FetchContent to build" ON)
15+
option(FMTLIB_FETCHCONTENT "Using fmt FetchContent to build" ON)
16+
option(HTTPLIB_FETCHCONTENT "Using httplib FetchContent to build" ON)
17+
option(CPP2SKY_INSTALL "Generate the install target." OFF)
18+
option(GENERATE_CPP2SKY_PKGCONFIG "Generate and install pkg-config files for UNIX" OFF)
19+
20+
21+
if(OVERRIDE_CXX_STANDARD_FLAGS)
22+
set(CMAKE_CXX_STANDARD 11)
23+
set(CMAKE_CXX_EXTENSIONS OFF)
24+
endif()
25+
26+
include(GNUInstallDirs)
27+
set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
28+
find_package(Threads)
29+
30+
31+
file(GLOB_RECURSE SOURCE_CC_FILES "source/*.cc")
32+
file(GLOB_RECURSE SOURCE_HDR_FILES "source/*.h")
33+
add_library(${PROJECT_NAME} STATIC ${SOURCE_CC_FILES} ${SOURCE_HDR_FILES})
34+
target_include_directories(${PROJECT_NAME} PUBLIC
35+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
36+
)
37+
38+
add_executable(example-sample "example/sample.cc")
39+
add_executable(example-sample-client "example/sample_client.cc")
40+
target_link_libraries(example-sample ${PROJECT_NAME})
41+
target_link_libraries(example-sample-client ${PROJECT_NAME})
42+
43+
44+
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
45+
include(fmtlib)
46+
include(spdlog)
47+
include(httplib)
48+
include(grpc)
49+
include(proto2cpp)
50+
51+
target_link_libraries(${PROJECT_NAME}
52+
PUBLIC $<BUILD_INTERFACE:proto_lib>
53+
${_REFLECTION}
54+
${_GRPC_GRPCPP}
55+
${_PROTOBUF_LIBPROTOBUF}
56+
fmt
57+
spdlog
58+
$<$<BOOL:${HTTPLIB_FETCHCONTENT}>:httplib>
59+
)
60+
61+
62+
if(CPP2SKY_INSTALL)
63+
include(CMakePackageConfigHelpers)
64+
# Install the static library.
65+
install(
66+
TARGETS ${PROJECT_NAME}
67+
EXPORT ${PROJECT_NAME}-targets
68+
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
69+
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
70+
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
71+
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
72+
)
73+
74+
# Install export
75+
install(
76+
EXPORT ${PROJECT_NAME}-targets
77+
NAMESPACE ${PROJECT_NAME}::
78+
FILE ${PROJECT_NAME}-targets.cmake
79+
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
80+
)
81+
82+
# Install the project include
83+
install(DIRECTORY cpp2sky
84+
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
85+
FILES_MATCHING
86+
PATTERN "*.h"
87+
PATTERN "BUILD" EXCLUDE
88+
PATTERN "*.proto" EXCLUDE
89+
)
90+
91+
# Install *.cmake.in
92+
configure_package_config_file(
93+
${PROJECT_SOURCE_DIR}/cmake/cpp2sky-config.cmake.in
94+
${PROJECT_BINARY_DIR}/${PROJECT_NAME}-config.cmake
95+
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
96+
)
97+
98+
install(
99+
FILES ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-config.cmake
100+
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
101+
)
102+
endif()
103+
104+
105+
if(GENERATE_CPP2SKY_PKGCONFIG)
106+
# see https://github.com/jupp0r/prometheus-cpp/issues/587
107+
if(IS_ABSOLUTE "${CMAKE_INSTALL_INCLUDEDIR}")
108+
set(CPP2SKY_PKGCONFIG_INCLUDEDIR "${CMAKE_INSTALL_INCLUDEDIR}")
109+
else()
110+
set(CPP2SKY_PKGCONFIG_INCLUDEDIR "\${prefix}/${CMAKE_INSTALL_INCLUDEDIR}")
111+
endif()
112+
113+
if(IS_ABSOLUTE "${CMAKE_INSTALL_LIBDIR}")
114+
set(CPP2SKY_PKGCONFIG_LIBDIR "${CMAKE_INSTALL_LIBDIR}")
115+
else()
116+
set(CPP2SKY_PKGCONFIG_LIBDIR "\${exec_prefix}/${CMAKE_INSTALL_LIBDIR}")
117+
endif()
118+
configure_file(
119+
${PROJECT_SOURCE_DIR}/cmake/cpp2sky.pc.in
120+
${PROJECT_BINARY_DIR}/cpp2sky.pc
121+
@ONLY
122+
)
123+
124+
# Install the pkgconfig
125+
install(
126+
FILES ${PROJECT_BINARY_DIR}/cpp2sky.pc
127+
DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig"
128+
)
129+
130+
endif()

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,30 @@ cc_binary(
3131
],
3232
)
3333
```
34+
#### Cmake
35+
You can compile this project, according to the following steps:
36+
```
37+
step 01: git clone [email protected]:SkyAPM/cpp2sky.git
38+
step 02: git clone -b v9.1.0 https://github.com/apache/skywalking-data-collect-protocol.git ./3rdparty/skywalking-data-collect-protocol
39+
step 03: git clone -b v1.46.6 https://github.com/grpc/grpc.git --recursive
40+
step 04: cmake -S ./grpc -B ./grpc/build && cmake --build ./grpc/build --parallel 8 --target install
41+
step 05: cmake -S . -B ./build && cmake --build ./build
42+
```
43+
44+
You can also use find_package to get target libary in your project. Like this:
45+
```
46+
find_package(cpp2sky CONFIG REQUIRED)
47+
target_link_libraries(${PROJECT_NAME} cpp2sky::cpp2sky proto_lib)
48+
```
49+
Of course, if OS is similar to Unix, you can also use pkgconfig to build the project. Like this:
50+
```
51+
find_package(PkgConfig REQUIRED)
52+
pkg_check_modules(CPP2SKY_PKG REQUIRED cpp2sky)
53+
```
3454

55+
Note:
56+
- If you want to build this project over c11, you must update grpc version(current version:v1.46.6).
57+
- Only test cmake using Centos and Ubuntu.
3558
#### Docs
3659

3760
cpp2sky configration is based on protobuf, and docs are generated by [protodoc](https://github.com/etcd-io/protodoc). If you have any API change, you should run below.

cmake/cpp2sky-config.cmake.in

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
@PACKAGE_INIT@
2+
include(CMakeFindDependencyMacro)
3+
4+
set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
5+
find_dependency(Threads)
6+
unset(CMAKE_THREAD_PREFER_PTHREAD)
7+
8+
find_dependency(fmt)
9+
find_dependency(spdlog)
10+
find_dependency(httplib)
11+
find_dependency(Protobuf)
12+
find_dependency(gRPC)
13+
14+
15+
include(${CMAKE_CURRENT_LIST_DIR}/cpp2sky-targets.cmake)

cmake/cpp2sky.pc.in

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
prefix=@CMAKE_INSTALL_PREFIX@
2+
exec_prefix=@CMAKE_INSTALL_PREFIX@
3+
libdir=@CPP2SKY_PKGCONFIG_LIBDIR@
4+
includedir=@CPP2SKY_PKGCONFIG_INCLUDEDIR@
5+
6+
Name: cpp2sky
7+
Description: Distributed tracing and monitor SDK in CPP for Apache SkyWalking APM
8+
Version: @CPP2SKY_VERSION@
9+
Libs: -L${libdir} -lcpp2sky
10+
Cflags: -I${includedir}/skywalking-protocol

cmake/fmtlib.cmake

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
cmake_minimum_required(VERSION 3.14)
2+
3+
if(MSVC)
4+
add_definitions(-D_WIN32_WINNT=0x600)
5+
endif()
6+
7+
find_package(Threads REQUIRED)
8+
9+
if(FMTLIB_AS_SUBMODULE)
10+
# using submodule in case of git clone timeout
11+
if(CPP2SKY_INSTALL)
12+
set(FMT_INSTALL ON)
13+
endif(CPP2SKY_INSTALL)
14+
add_subdirectory(3rdparty/fmt ${CMAKE_CURRENT_BINARY_DIR}/fmt)
15+
message(STATUS "Using fmt via add_subdirectory.")
16+
elseif(FMTLIB_FETCHCONTENT)
17+
# using FetchContent to install spdlog
18+
include(FetchContent)
19+
if(${CMAKE_VERSION} VERSION_LESS 3.14)
20+
include(add_FetchContent_MakeAvailable.cmake)
21+
endif()
22+
23+
FetchContent_Declare(
24+
fmtlib
25+
URL https://github.com/fmtlib/fmt/releases/download/8.1.1/fmt-8.1.1.zip
26+
URL_HASH SHA256=23778bad8edba12d76e4075da06db591f3b0e3c6c04928ced4a7282ca3400e5d
27+
)
28+
FetchContent_MakeAvailable(fmtlib)
29+
else()
30+
find_package(fmt CONFIG REQUIRED)
31+
message(STATUS "Using fmt by find_package")
32+
endif()

cmake/grpc.cmake

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# Copyright 2018 gRPC 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 build file for C++ route_guide example.
16+
# Assumes protobuf and gRPC have been installed using cmake.
17+
# See cmake_externalproject/CMakeLists.txt for all-in-one cmake build
18+
# that automatically builds all the dependencies before building route_guide.
19+
20+
cmake_minimum_required(VERSION 3.14)
21+
22+
if(MSVC)
23+
add_definitions(-D_WIN32_WINNT=0x600)
24+
endif()
25+
26+
find_package(Threads REQUIRED)
27+
28+
if(GRPC_AS_SUBMODULE)
29+
# One way to build a projects that uses gRPC is to just include the
30+
# entire gRPC project tree via "add_subdirectory".
31+
# This approach is very simple to use, but the are some potential
32+
# disadvantages:
33+
# * it includes gRPC's CMakeLists.txt directly into your build script
34+
# without and that can make gRPC's internal setting interfere with your
35+
# own build.
36+
# * depending on what's installed on your system, the contents of submodules
37+
# in gRPC's third_party/* might need to be available (and there might be
38+
# additional prerequisites required to build them). Consider using
39+
# the gRPC_*_PROVIDER options to fine-tune the expected behavior.
40+
#
41+
# A more robust approach to add dependency on gRPC is using
42+
# cmake's ExternalProject_Add (see cmake_externalproject/CMakeLists.txt).
43+
44+
# Include the gRPC's cmake build (normally grpc source code would live
45+
# in a git submodule called "third_party/grpc", but this example lives in
46+
# the same repository as gRPC sources, so we just look a few directories up)
47+
if(NOT GRPC_ROOT_DIR)
48+
set(GRPC_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/grpc)
49+
endif()
50+
add_subdirectory(${GRPC_ROOT_DIR} 3rdparty/grpc)
51+
message(STATUS "Using gRPC via add_subdirectory.")
52+
# After using add_subdirectory, we can now use the grpc targets directly from
53+
# this build.
54+
set(_PROTOBUF_LIBPROTOBUF libprotobuf)
55+
set(_REFLECTION grpc++_reflection)
56+
if(CMAKE_CROSSCOMPILING)
57+
find_program(_PROTOBUF_PROTOC protoc)
58+
else()
59+
set(_PROTOBUF_PROTOC $<TARGET_FILE:protoc>)
60+
endif()
61+
set(_GRPC_GRPCPP grpc++)
62+
if(CMAKE_CROSSCOMPILING)
63+
find_program(_GRPC_CPP_PLUGIN_EXECUTABLE grpc_cpp_plugin)
64+
else()
65+
set(_GRPC_CPP_PLUGIN_EXECUTABLE $<TARGET_FILE:grpc_cpp_plugin>)
66+
endif()
67+
elseif(GRPC_FETCHCONTENT)
68+
# Another way is to use CMake's FetchContent module to clone gRPC at
69+
# configure time. This makes gRPC's source code available to your project,
70+
# similar to a git submodule.
71+
message(STATUS "Using gRPC via add_subdirectory (FetchContent).")
72+
include(FetchContent)
73+
FetchContent_Declare(
74+
grpc
75+
URL https://github.com/grpc/grpc/archive/a3ae8e00a2c5553c806e83fae83e33f0198913f0.tar.gz
76+
URL_HASH SHA256=1ccc2056b68b81ada8df61310e03dfa0541c34821fd711654d0590a7321db9c8
77+
)
78+
FetchContent_MakeAvailable(grpc)
79+
80+
# Since FetchContent uses add_subdirectory under the hood, we can use
81+
# the grpc targets directly from this build.
82+
set(_PROTOBUF_LIBPROTOBUF libprotobuf)
83+
set(_REFLECTION grpc++_reflection)
84+
set(_PROTOBUF_PROTOC $<TARGET_FILE:protoc>)
85+
set(_GRPC_GRPCPP grpc++)
86+
if(CMAKE_CROSSCOMPILING)
87+
find_program(_GRPC_CPP_PLUGIN_EXECUTABLE grpc_cpp_plugin)
88+
else()
89+
set(_GRPC_CPP_PLUGIN_EXECUTABLE $<TARGET_FILE:grpc_cpp_plugin>)
90+
endif()
91+
else()
92+
# This branch assumes that gRPC and all its dependencies are already installed
93+
# on this system, so they can be located by find_package().
94+
message(STATUS "gRPC and all its dependencies should be able to located by find_package().")
95+
96+
# Find Protobuf installation
97+
# Looks for protobuf-config.cmake file installed by Protobuf's cmake installation.
98+
option(protobuf_MODULE_COMPATIBLE TRUE)
99+
find_package(Protobuf CONFIG REQUIRED)
100+
message(STATUS "Using protobuf ${Protobuf_VERSION}")
101+
102+
set(_PROTOBUF_LIBPROTOBUF protobuf::libprotobuf)
103+
set(_REFLECTION gRPC::grpc++_reflection)
104+
105+
message(STATUS "CMAKE_CROSSCOMPILING: ${CMAKE_CROSSCOMPILING}")
106+
107+
if(CMAKE_CROSSCOMPILING)
108+
find_program(_PROTOBUF_PROTOC protoc)
109+
else()
110+
set(_PROTOBUF_PROTOC $<TARGET_FILE:protobuf::protoc>)
111+
endif()
112+
113+
# Find gRPC installation
114+
# Looks for gRPCConfig.cmake file installed by gRPC's cmake installation.
115+
find_package(gRPC CONFIG REQUIRED)
116+
message(STATUS "Using gRPC ${gRPC_VERSION}")
117+
118+
set(_GRPC_GRPCPP gRPC::grpc++)
119+
if(CMAKE_CROSSCOMPILING)
120+
find_program(_GRPC_CPP_PLUGIN_EXECUTABLE grpc_cpp_plugin)
121+
else()
122+
set(_GRPC_CPP_PLUGIN_EXECUTABLE $<TARGET_FILE:gRPC::grpc_cpp_plugin>)
123+
endif()
124+
125+
endif()

0 commit comments

Comments
 (0)