Skip to content

Commit

Permalink
add example
Browse files Browse the repository at this point in the history
  • Loading branch information
oathdruid committed May 14, 2024
1 parent 1abcbd3 commit 16d927f
Show file tree
Hide file tree
Showing 19 changed files with 254 additions and 20 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ set_source_files_properties(
"${CMAKE_CURRENT_SOURCE_DIR}/src/babylon/reusable/patch/arena.cpp"
PROPERTIES COMPILE_FLAGS "-fno-access-control")

if(NOT BUILD_DEPS)
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
install(TARGETS babylon EXPORT babylon)
install(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/src/babylon"
DESTINATION include
Expand Down
108 changes: 93 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,34 +26,112 @@ Babylon是一个用于支持C++高性能服务端开发的基础库,从内存

## 编译并使用

Babylon使用[Bazel](https://bazel.build)进行构建和依赖管理
- 构建`bazel build ...`
- 单测`bazel test ...`
- Asan单测`bazel test --config asan ...`
- Tsan单测`bazel test --config tsan ...`
### Bazel

通过[bzlmod](https://bazel.build/external/module)机制依赖
Babylon使用[Bazel](https://bazel.build)进行构建并使用[bzlmod](https://bazel.build/external/module)进行依赖管理,增加如下几个环节就可以将babylon作为依赖引入到项目
- 增加仓库注册表
```
# in .bazelrc
# babylon自身发布在独立注册表
common --registry=https://baidu.github.io/babylon/registry
# babylon依赖的boost发布在bazelboost项目的注册表,当然如果有其他源可以提供boost的注册表也可以替换
# 只要同样能够提供boost.preprocessor和boost.spirit模块寻址即可
common --registry=https://raw.githubusercontent.com/bazelboost/registry/main
```
- 增加依赖项
```
# in MODULE.bazel
bazel_dep(name = 'babylon', version = '1.1.4')
```
- 精细化使用子模块依赖目标`@babylon//:any``@babylon/:concurrent`等,详见[BUILD](BUILD)文件
- 或者使用All in One依赖目标`@babylon//:babylon`
- 添加依赖的子模块到编译目标,全部可用子模块可以参照[模块功能文档](#模块功能文档),或者[BUILD](BUILD)文件
```
# in BUILD
cc_library(
...
deps = [
...
'@babylon//:any',
'@babylon//:concurrent',
],
)
```
- 也可以直接添加All in One依赖目标`@babylon//:babylon`
-[example/depend-use-bzlmod](example/depend-use-bzlmod)可以找到一个如何通过Bazel依赖的简单样例

### CMake

Babylon也支持使用[CMake](https://cmake.org)进行构建,并支持通过[find_package](https://cmake.org/cmake/help/latest/command/find_package.html)[add_subdirectory](https://cmake.org/cmake/help/latest/command/add_subdirectory.html)[FetchContent](https://cmake.org/cmake/help/latest/module/FetchContent.html)进行依赖引入

#### FetchContent

Babylon也支持[CMake](https://cmake.org)进行构建,以及通过[FetchContent](https://cmake.org/cmake/help/latest/module/FetchContent.html)进行自动依赖下载
- 环境准备
- 使用预编译依赖`cmake -Bbuild`
- 使用自动依赖下载`cmake -Bbuild -DBUILD_DEPS=ON`
- 编译`cmake --build build`
- 单测`ctest --test-dir build`
- CMake编译目前只提供All in One依赖目标`babylon::babylon`
- 增加依赖项到目标项目
```
# in CMakeList.txt
set(BUILD_DEPS ON)
include(FetchContent)
FetchContent_Declare(
babylon
URL "https://github.com/baidu/babylon/archive/refs/tags/v1.1.4.tar.gz"
URL_HASH SHA256=2e7efea3f0a8aeffc03f908ff2875a82eb5a94cd1de3e591b6dc388f7a992411
)
FetchContent_MakeAvailable(babylon)
```
- 添加依赖到编译目标,CMake编译目前只提供All in One依赖目标`babylon::babylon`
```
# in CMakeList.txt
target_link_libraries(your_target babylon::babylon)
```
- 编译目标项目
- `cmake -Bbuild`
- `cmake --build build -j$(nproc)`
-[example/depend-use-cmake-fetch](example/depend-use-cmake-fetch)可以找到一个如何通过CMake FetchContent依赖的简单样例

#### find_package

- 编译并安装`boost``abseil-cpp``protobuf`或者直接使用apt等包管理工具安装对应平台的预编译包
- 编译并安装babylon
- `cmake -Bbuild -DCMAKE_INSTALL_PREFIX=/your/install/path -DCMAKE_PREFIX_PATH=/your/install/path`
- `cmake --build build -j$(nproc)`
- `cmake --install build`
- 增加依赖项到目标项目
```
# in CMakeList.txt
find_package(babylon REQUIRED)
```
- 添加依赖到编译目标,CMake编译目前只提供All in One依赖目标`babylon::babylon`
```
# in CMakeList.txt
target_link_libraries(your_target babylon::babylon)
```
- 编译目标项目
- `cmake -Bbuild -DCMAKE_PREFIX_PATH=/your/install/path`
- `cmake --build build -j$(nproc)`
-[example/depend-use-cmake-find](example/depend-use-cmake-find)可以找到一个如何通过CMake find_package依赖的简单样例

#### add_subdirectory

- 下载`boost``abseil-cpp``protobuf``babylon`源码
- 增加依赖项到目标项目
```
# in CMakeList.txt
set(BOOST_INCLUDE_LIBRARIES preprocessor spirit)
add_subdirectory(boost EXCLUDE_FROM_ALL)
set(ABSL_ENABLE_INSTALL ON)
add_subdirectory(abseil-cpp)
set(protobuf_BUILD_TESTS OFF)
add_subdirectory(protobuf)
add_subdirectory(babylon)
```
- 添加依赖到编译目标,CMake编译目前只提供All in One依赖目标`babylon::babylon`
```
# in CMakeList.txt
target_link_libraries(your_target babylon::babylon)
```
- 编译目标项目
- `cmake -Bbuild`
- `cmake --build build -j$(nproc)`
-[example/depend-use-cmake-subdir](example/depend-use-cmake-subdir)可以找到一个如何通过CMake sub_directory依赖的简单样例

## 模块功能文档

Expand Down
2 changes: 2 additions & 0 deletions example/depend-use-bzlmod/.bazeliskrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
BAZELISK_BASE_URL=https://github.com/bazelbuild/bazel/releases/download
USE_BAZEL_VERSION=7.1.1
5 changes: 5 additions & 0 deletions example/depend-use-bzlmod/.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
common --registry=https://baidu.github.io/babylon/registry
common --registry=https://raw.githubusercontent.com/bazelboost/registry/main
common --registry=https://bcr.bazel.build

build --cxxopt=-std=c++17 --host_cxxopt=-std=c++17
8 changes: 8 additions & 0 deletions example/depend-use-bzlmod/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
cc_binary(
name = 'example',
srcs = ['example.cpp'],
deps = [
'@babylon//:future',
'@babylon//:logging',
],
)
1 change: 1 addition & 0 deletions example/depend-use-bzlmod/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bazel_dep(name = 'babylon', version = '1.1.4')
4 changes: 4 additions & 0 deletions example/depend-use-bzlmod/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
set -ex

bazel build example
23 changes: 23 additions & 0 deletions example/depend-use-bzlmod/example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "babylon/future.h"
#include "babylon/logging/interface.h"

#include <thread>
#include <vector>

int main() {
::babylon::CountDownLatch<> latch(10);
auto future = latch.get_future();
::std::vector<::std::thread> threads;
for (size_t i = 0; i < 10; ++i) {
threads.emplace_back([&, i] () {
BABYLON_LOG(INFO) << "finish " << i;
latch.count_down();
});
}
future.get();
BABYLON_LOG(INFO) << "finish all";
for (auto& thread : threads) {
thread.join();
}
return 0;
}
12 changes: 12 additions & 0 deletions example/depend-use-cmake-fetch/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
set(BUILD_DEPS ON)

include(FetchContent)
FetchContent_Declare(
babylon
URL "https://github.com/baidu/babylon/archive/refs/tags/v1.1.4.tar.gz"
URL_HASH SHA256=2e7efea3f0a8aeffc03f908ff2875a82eb5a94cd1de3e591b6dc388f7a992411
)
FetchContent_MakeAvailable(babylon)

add_executable(example example.cpp)
target_link_libraries(example babylon::babylon)
5 changes: 5 additions & 0 deletions example/depend-use-cmake-fetch/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh
set -ex

cmake -Bbuild
cmake --build build
23 changes: 23 additions & 0 deletions example/depend-use-cmake-fetch/example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "babylon/future.h"
#include "babylon/logging/interface.h"

#include <thread>
#include <vector>

int main() {
::babylon::CountDownLatch<> latch(10);
auto future = latch.get_future();
::std::vector<::std::thread> threads;
for (size_t i = 0; i < 10; ++i) {
threads.emplace_back([&, i] () {
BABYLON_LOG(INFO) << "finish " << i;
latch.count_down();
});
}
future.get();
BABYLON_LOG(INFO) << "finish all";
for (auto& thread : threads) {
thread.join();
}
return 0;
}
4 changes: 4 additions & 0 deletions example/depend-use-cmake-find/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
find_package(babylon REQUIRED)

add_executable(example example.cpp)
target_link_libraries(example babylon::babylon)
5 changes: 5 additions & 0 deletions example/depend-use-cmake-find/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh
set -ex

cmake -Bbuild
cmake --build build
23 changes: 23 additions & 0 deletions example/depend-use-cmake-find/example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "babylon/future.h"
#include "babylon/logging/interface.h"

#include <thread>
#include <vector>

int main() {
::babylon::CountDownLatch<> latch(10);
auto future = latch.get_future();
::std::vector<::std::thread> threads;
for (size_t i = 0; i < 10; ++i) {
threads.emplace_back([&, i] () {
BABYLON_LOG(INFO) << "finish " << i;
latch.count_down();
});
}
future.get();
BABYLON_LOG(INFO) << "finish all";
for (auto& thread : threads) {
thread.join();
}
return 0;
}
10 changes: 10 additions & 0 deletions example/depend-use-cmake-subdir/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
set(BOOST_INCLUDE_LIBRARIES preprocessor spirit)
add_subdirectory(boost EXCLUDE_FROM_ALL)
set(ABSL_ENABLE_INSTALL ON)
add_subdirectory(abseil-cpp)
set(protobuf_BUILD_TESTS OFF)
add_subdirectory(protobuf)
add_subdirectory(babylon)

add_executable(example example.cpp)
target_link_libraries(example babylon::babylon)
5 changes: 5 additions & 0 deletions example/depend-use-cmake-subdir/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh
set -ex

cmake -Bbuild
cmake --build build
23 changes: 23 additions & 0 deletions example/depend-use-cmake-subdir/example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "babylon/future.h"
#include "babylon/logging/interface.h"

#include <thread>
#include <vector>

int main() {
::babylon::CountDownLatch<> latch(10);
auto future = latch.get_future();
::std::vector<::std::thread> threads;
for (size_t i = 0; i < 10; ++i) {
threads.emplace_back([&, i] () {
BABYLON_LOG(INFO) << "finish " << i;
latch.count_down();
});
}
future.get();
BABYLON_LOG(INFO) << "finish all";
for (auto& thread : threads) {
thread.join();
}
return 0;
}
2 changes: 1 addition & 1 deletion registry/modules/babylon/1.1.4/source.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"url": "https://github.com/baidu/babylon/archive/refs/tags/v1.1.4.tar.gz",
"strip_prefix": "babylon-1.1.4",
"integrity": "sha256-Okm+CB5XgGPs+p3y3yLjCbFO2GFth/o5d2wgHh2DlJQ="
"integrity": "sha256-Ln7+o/Corv/AP5CP8odagutalM0d4+WRttw4j3qZJBE="
}
9 changes: 6 additions & 3 deletions src/babylon/logging/interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,19 @@ class DefaultLogStreamProvider : public LogStreamProvider {
inline S() noexcept : LogStream(*::std::cerr.rdbuf()) {}
#endif // !__clang__ && BABYLON_GCC_VERSION < 50000
virtual void do_begin() noexcept override {
mutex.lock();
mutex().lock();
operator<<(SEVERITY_NAME[severity])
<< '[' << file << ':' << line << "] ";
}
virtual void do_end() noexcept override {
operator<<('\n');
mutex.unlock();
mutex().unlock();
}
static ::std::mutex& mutex() noexcept {
static ::std::mutex instance;
return instance;
}

::std::mutex mutex;
int severity;
StringView file;
int line;
Expand Down

0 comments on commit 16d927f

Please sign in to comment.