From 62e59ef99f29816d9c58f93d947d75008e7c5d5f Mon Sep 17 00:00:00 2001 From: yujun411522 Date: Mon, 23 Oct 2023 16:15:23 +0800 Subject: [PATCH] Docs: add cmake install --- .github/workflows/ci.yml | 2 +- docs/en/setup_env.md | 70 +++++++++++++++++++++++++++++++++++++++ docs/zh/setup_env.md | 71 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 142 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0eb49710..53a77129 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -18,7 +18,7 @@ jobs: - name: Test run: | ./clean.sh - bazel coverage //trpc/... --test_output=all --coverage_report_generator="@bazel_tools//tools/test/CoverageOutputGenerator/java/com/google/devtools/coverageoutputgenerator:Main" --combined_report=lcov --nocache_test_results + bazel coverage //trpc/... --test_output=errors --coverage_report_generator="@bazel_tools//tools/test/CoverageOutputGenerator/java/com/google/devtools/coverageoutputgenerator:Main" --combined_report=lcov --nocache_test_results - name: Upload coverage reports to Codecov uses: codecov/codecov-action@v3 env: diff --git a/docs/en/setup_env.md b/docs/en/setup_env.md index 65f9d903..89d2647a 100644 --- a/docs/en/setup_env.md +++ b/docs/en/setup_env.md @@ -82,6 +82,72 @@ Since the FetchContent feature of CMake is required for fetching third-party dep ./run_examples_cmake.sh ``` +3. **How to use tRPC-Cpp** + +a. (Recommend) Use as external source code + +We recommend this as it can easily switch tRPC-Cpp version to the lastest and the libs framework depends on can also be imported via a simple SDK target named `trpc`. + +For example, you can import in CMakeLists.txt in this way: + +```shell +# Fetch tRPC-Cpp and add as library +include(FetchContent) +FetchContent_Declare( + trpc-cpp + GIT_REPOSITORY https://git.woa.com/trpc-cpp/open-source/trpc-cpp.git + GIT_TAG recommanded_always_use_latest_tag + SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/cmake_third_party/trpc-cpp +) +FetchContent_MakeAvailable(trpc-cpp) + +# Set path of stub code genretated tool(PROTOBUF_PROTOC_EXECUTABLE/TRPC_TO_CPP_PLUGIN will be filled after you import tRPC-Cpp) +set(PB_PROTOC ${PROTOBUF_PROTOC_EXECUTABLE}) +set(TRPC_CPP_PLUGIN ${TRPC_TO_CPP_PLUGIN}) + +# link lib trpc to your target +target_link_libraries(your_cmake_target trpc) +``` + +b. Use via make install + +Execute below commands, install tRPC-Cpp to your machine first: + +```shell +# You can install the lastest verion by: git checkout tags/vx.x.x +git clone https://github.com/trpc-group/trpc-cpp.git +cd trpc-cpp +mkdir build && cd build + +# By default, tRPC-Cpp will build as static lib. If you need dynamic lib, add cmake option: -DTRPC_BUILD_SHARED=ON +cmake .. +make -j8 +make install # install at /usr/local/trpc-cpp/trpc +``` + +Then, import trpc lib in your CMakeLists.txt as below: + +```shell +# set install path of tRPC-Cpp +set(TRPC_INSTALL_PATH /usr/local/trpc-cpp/trpc) + +# Load hearders and libs +include(${TRPC_INSTALL_PATH}/cmake/config/trpc_config.cmake) +include_directories(${INCLUDE_INSTALL_PATHS}) +link_directories(${LIBRARY_INSTALL_PATHS}) + +# Set path of stub code genretated tool +include(${TRPC_INSTALL_PATH}/cmake/tools/trpc_utils.cmake) +set(PB_PROTOC ${TRPC_INSTALL_PATH}/bin/protoc) +set(TRPC_CPP_PLUGIN ${TRPC_INSTALL_PATH}/bin/trpc_cpp_plugin) + +# add trpc and it's dependent libs +set(LIBRARY trpc ${LIBS_BASIC}) + +# link lib trpc to your target +target_link_libraries(your_cmake_target trpc) +``` + ## Ubuntu It recommend using **Ubuntu version 20.04 LTS or above** for compiling and running tRPC-Cpp. @@ -145,6 +211,10 @@ Since the FetchContent feature of CMake is required for fetching third-party dep ./run_examples_cmake.sh ``` +3. **How to use tRPC-Cpp** + +Same as CentOS section + # FAQ ## bazel --version" shows a "command not found" error? diff --git a/docs/zh/setup_env.md b/docs/zh/setup_env.md index e0141a5e..c20c9cf3 100644 --- a/docs/zh/setup_env.md +++ b/docs/zh/setup_env.md @@ -85,6 +85,72 @@ bazel 推荐使用 3.5.1及以后版本。 ./run_examples_cmake.sh ``` +3. **如何引入tRPC-Cpp** + +a. (推荐)以外部库形式源码引入 + +推荐使用这种方式,因为能很方便切换框架的版本,同时,框架相关依赖可随构建的SDK目标引入。 + +参考如下示例,在您项目的 CMakeLists.txt 以外部库源码方式引入: + +```shell +# 拉取并以库的形式添加tRPC-Cpp +include(FetchContent) +FetchContent_Declare( + trpc-cpp + GIT_REPOSITORY https://git.woa.com/trpc-cpp/open-source/trpc-cpp.git + GIT_TAG recommanded_always_use_latest_tag + SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/cmake_third_party/trpc-cpp +) +FetchContent_MakeAvailable(trpc-cpp) + +# 设置proto文件桩代码生成工具的路径(PROTOBUF_PROTOC_EXECUTABLE/TRPC_TO_CPP_PLUGIN将会在tRPC-Cpp被引入后自动填充) +set(PB_PROTOC ${PROTOBUF_PROTOC_EXECUTABLE}) +set(TRPC_CPP_PLUGIN ${TRPC_TO_CPP_PLUGIN}) + +# 在您的构建目标里link trpc库 +target_link_libraries(your_cmake_target trpc) +``` + +b. 通过make install引入 + +参考如下命令,先安装trpc到系统里: + +```shell +# 可checkout切换到最新的版本 +git clone https://github.com/trpc-group/trpc-cpp.git +cd trpc-cpp +mkdir build && cd build + +# 默认编译静态库,可以通过cmake选项编译动态库: -DTRPC_BUILD_SHARED=ON +cmake .. +make -j8 +make install +``` + +然后,参考如下步骤,在您项目的 CMakeLists.txt 里,引入trpc库: + +```shell +# 设置tRPC-Cpp的安装位置 +set(TRPC_INSTALL_PATH /usr/local/trpc-cpp/trpc) + +# 加载tRPC-Cpp头文件及库路径 +include(${TRPC_INSTALL_PATH}/cmake/config/trpc_config.cmake) +include_directories(${INCLUDE_INSTALL_PATHS}) +link_directories(${LIBRARY_INSTALL_PATHS}) + +# 设置proto文件桩代码生成工具的路径 - 具体使用方式见相关章节 +include(${TRPC_INSTALL_PATH}/cmake/tools/trpc_utils.cmake) +set(PB_PROTOC ${TRPC_INSTALL_PATH}/bin/protoc) +set(TRPC_CPP_PLUGIN ${TRPC_INSTALL_PATH}/bin/trpc_cpp_plugin) + +# 添加trpc库及其依赖的三方 +set(LIBRARY trpc ${LIBS_BASIC}) + +# 在您的构建目标里link trpc库 +target_link_libraries(your_cmake_target trpc) +``` + ## Ubuntu 推荐**Ubuntu 版本在 20.04 LTS 及以上版本**。 @@ -155,6 +221,11 @@ bazel 推荐使用3.5.1及以后版本。 ./run_examples_cmake.sh ``` +3. **安装及使用** + +同CentOS 部分。 + + # FAQ ## bazel --version 出现找不到 bazel 命令?