Skip to content

Commit

Permalink
fix cmake option & doc (#636)
Browse files Browse the repository at this point in the history
  • Loading branch information
poor-circle authored Mar 18, 2024
1 parent 05daeb2 commit d8836aa
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 63 deletions.
29 changes: 28 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -329,13 +329,40 @@ void basic_usage() {

## coro_http

coro_http is a C++20 coroutine http(https) client, include: get/post, websocket, multipart file upload, chunked and ranges download etc.
coro_http is a C++20 coroutine http(https) library, include server and client, functions: get/post, websocket, multipart file upload, chunked and ranges download etc. [more examples](https://github.com/alibaba/yalantinglibs/blob/main/src/coro_http/examples/example.cpp)

### get/post
```c++
#include "ylt/coro_http/coro_http_server.hpp"
#include "ylt/coro_http/coro_http_client.hpp"
using namespace coro_http;

async_simple::coro::Lazy<void> basic_usage() {
coro_http_server server(1, 9001);
server.set_http_handler<GET>(
"/get", [](coro_http_request &req, coro_http_response &resp) {
resp.set_status_and_content(status_type::ok, "ok");
});

server.set_http_handler<GET>(
"/coro",
[](coro_http_request &req,
coro_http_response &resp) -> async_simple::coro::Lazy<void> {
resp.set_status_and_content(status_type::ok, "ok");
co_return;
});
server.aync_start(); // aync_start() don't block, sync_start() will block.
std::this_thread::sleep_for(300ms); // wait for server start

coro_http_client client{};
auto result = co_await client.async_get("http://127.0.0.1:9001/get");
assert(result.status == 200);
assert(result.resp_body == "ok");
for (auto [key, val] : result.resp_headers) {
std::cout << key << ": " << val << "\n";
}
}

async_simple::coro::Lazy<void> get_post(coro_http_client &client) {
std::string uri = "http://www.example.com";
auto result = co_await client.async_get(uri);
Expand Down
22 changes: 11 additions & 11 deletions cmake/config.cmake
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
message(STATUS "-------------YLT CONFIG SETTING-------------")
option(YLT_ENABLE_SSL "Enable ssl support" OFF)
message(STATUS "ENABLE_SSL: ${YLT_ENABLE_SSL}")
message(STATUS "YLT_ENABLE_SSL: ${YLT_ENABLE_SSL}")
if (YLT_ENABLE_SSL)
find_package(OpenSSL REQUIRED)
if(CMAKE_PROJECT_NAME STREQUAL "yaLanTingLibs")
Expand All @@ -13,7 +13,7 @@ if (YLT_ENABLE_SSL)
endif ()

option(YLT_ENABLE_PMR "Enable pmr support" OFF)
message(STATUS "ENABLE_PMR: ${YLT_ENABLE_PMR}")
message(STATUS "YLT_ENABLE_PMR: ${YLT_ENABLE_PMR}")
if (YLT_ENABLE_PMR)
if(CMAKE_PROJECT_NAME STREQUAL "yaLanTingLibs")
add_compile_definitions("YLT_ENABLE_PMR" IGUANA_ENABLE_PMR)
Expand All @@ -23,7 +23,7 @@ if (YLT_ENABLE_PMR)
endif ()

option(YLT_ENABLE_IO_URING "Enable io_uring" OFF)
message(STATUS "ENABLE_IO_URING: ${YLT_ENABLE_IO_URING}")
message(STATUS "YLT_ENABLE_IO_URING: ${YLT_ENABLE_IO_URING}")
if (YLT_ENABLE_IO_URING)
find_package(uring REQUIRED)
message(STATUS "Use IO_URING for all I/O in linux")
Expand All @@ -40,7 +40,7 @@ option(YLT_ENABLE_FILE_IO_URING "Enable file io_uring" OFF)
if (NOT YLT_ENABLE_IO_URING)
if(YLT_ENABLE_FILE_IO_URING)
find_package(uring REQUIRED)
message(STATUS "Enable io_uring for file I/O in linux")
message(STATUS "YLT: Enable io_uring for file I/O in linux")
if(CMAKE_PROJECT_NAME STREQUAL "yaLanTingLibs")
add_compile_definitions(ASIO_HAS_IO_URING ASIO_HAS_FILE "YLT_ENABLE_FILE_IO_URING")
link_libraries(uring)
Expand All @@ -51,19 +51,19 @@ if (NOT YLT_ENABLE_IO_URING)
endif()
endif()

option(ENABLE_STRUCT_PACK_UNPORTABLE_TYPE "enable struct_pack unportable type(like wchar_t)" OFF)
message(STATUS "ENABLE_STRUCT_PACK_UNPORTABLE_TYPE: ${ENABLE_STRUCT_PACK_UNPORTABLE_TYPE}")
if(ENABLE_STRUCT_PACK_UNPORTABLE_TYPE)
option(YLT_ENABLE_STRUCT_PACK_UNPORTABLE_TYPE "enable struct_pack unportable type(like wchar_t)" OFF)
message(STATUS "YLT_ENABLE_STRUCT_PACK_UNPORTABLE_TYPE: ${YLT_ENABLE_STRUCT_PACK_UNPORTABLE_TYPE}")
if(YLT_ENABLE_STRUCT_PACK_UNPORTABLE_TYPE)
add_compile_definitions(STRUCT_PACK_ENABLE_UNPORTABLE_TYPE)
endif()

option(YLT_ENABLE_STRUCT_PACK_OPTIMIZE "enable struct_pack optimize(but cost more compile time)" OFF)
message(STATUS "ENABLE_STRUCT_PACK_OPTIMIZE: ${YLT_ENABLE_STRUCT_PACK_OPTIMIZE}")
if(ENABLE_STRUCT_PACK_OPTIMIZE)
message(STATUS "YLT_ENABLE_STRUCT_PACK_OPTIMIZE: ${YLT_ENABLE_STRUCT_PACK_OPTIMIZE}")
if(YLT_ENABLE_STRUCT_PACK_OPTIMIZE)
if(CMAKE_PROJECT_NAME STREQUAL "yaLanTingLibs")
add_compile_definitions(ENABLE_STRUCT_PACK_OPTIMIZE)
add_compile_definitions(YLT_ENABLE_STRUCT_PACK_OPTIMIZE)
else ()
target_compile_definitions(yalantinglibs INTERFACE ENABLE_STRUCT_PACK_OPTIMIZE)
target_compile_definitions(yalantinglibs INTERFACE YLT_ENABLE_STRUCT_PACK_OPTIMIZE)
endif ()
endif()
message(STATUS "--------------------------------------------")
3 changes: 3 additions & 0 deletions cmake/install.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,8 @@ if (INSTALL_THIRDPARTY)
$<INSTALL_INTERFACE:include/ylt/thirdparty>
)
endif()
elseif(VCPKG_INSTALL_THIRDPARTY)
install(DIRECTORY "${yaLanTingLibs_SOURCE_DIR}/include/ylt/thirdparty/cinatra" DESTINATION include/ylt/thirdparty/cinatra)
install(DIRECTORY "${yaLanTingLibs_SOURCE_DIR}/include/ylt/thirdparty/iguana" DESTINATION include/ylt/thirdparty/iguana)
endif()
message(STATUS "--------------------------------------------")
148 changes: 97 additions & 51 deletions website/docs/en/guide/what_is_yalantinglibs.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@

[中文版](../../zh/guide/what_is_yalantinglibs.md)

yaLanTingLibs is a collection of C++20 libraries, now it contains struct_pack, struct_json, struct_xml, struct_yaml, struct_pb, easylog, coro_rpc, coro_http and async_simple, more and more cool libraries will be added into yaLanTingLibs(such as http.) in the future.
yaLanTingLibs is a collection of modern c++ util libraries, now it contains struct_pack, struct_json, struct_xml, struct_yaml, struct_pb, easylog, coro_rpc, coro_io, coro_http and async_simple, more and more cool libraries will be added into yaLanTingLibs in the future.

The target of yaLanTingLibs: provide very easy and high performance C++20 libraries for C++ developers, it can help to quickly build high performance applications.
The target of yaLanTingLibs: provide very easy and high performance modern C++ libraries for developers, it can help to quickly build high performance applications.

| OS (Compiler Version) | Status |
|------------------------------------------------|-----------------------------------------------------------------------------------------------------------|
Expand All @@ -25,16 +25,28 @@ The target of yaLanTingLibs: provide very easy and high performance C++20 librar
# Quick Start
## compiler requirements

make sure you have such compilers:
If your compiler don't support C++20, yalantinglibs will only compile the serialization libraries (struct_pack, struct_json, struct_xml, struct_yaml, easylog support C++17).
Make sure you have such compilers:

- g++9 above;
- clang++6 above
- msvc 14.20 above;

Otherwise, yalantinglibs will compile all the libraries.
Make sure you have such compilers:

- g++10 above;
- clang++13 above (with stdlib = libc++-13/libstdc++-8 or later version);
- clang++13 above
- msvc 14.29 above;

You can also use cmake option `-DENABLE_CPP_20=ON` or `-DENABLE_CPP_20=OFF` to control it.

## Install & Compile

Yalantinglibs is a head-only library. You can just copy `./include/ylt` directory into your project. But we suggest you use cmake to install it.

### Install

1. clone repo

```shell
Expand All @@ -52,6 +64,15 @@ cd build
cmake ..
cmake --build . --config debug # add -j, if you have enough memory to parallel compile
ctest . # run tests

```

- Build in bazel:
```shell
bazel build ylt # Please make sure bazel in you bin path.
bazel build coro_http_example # Or replace in anyone you want to build and test.
# Actually you might take it in other project in prefix @com_alibaba_yalangtinglibs, like
bazel build @com_alibaba_yalangtinglibs://ylt
```

You can see the test/example/benchmark executable file in `./build/output/`.
Expand All @@ -61,7 +82,6 @@ You can see the test/example/benchmark executable file in `./build/output/`.
```shell
# You can use those option to skip build unit-test & benchmark & example:
cmake .. -DBUILD_EXAMPLES=OFF -DBUILD_BENCHMARK=OFF -DBUILD_UNIT_TESTS=OFF
cmake --build .
```

3. install
Expand All @@ -83,15 +103,39 @@ cmake ..
cmake --build .
```

- Compile Manually:
### Cmake FetchContent

You can also import ylt by cmake FetchContent

```cmake
cmake_minimum_required(VERSION 3.15)
project(ylt_test)
include(FetchContent)
FetchContent_Declare(
yalantinglibs
GIT_REPOSITORY https://github.com/JYLeeLYJ/yalantinglibs.git
GIT_TAG feat/fetch # optional ( default master / main )
GIT_SHALLOW 1 # optional ( --depth=1 )
)
FetchContent_MakeAvailable(yalantinglibs)
add_executable(main main.cpp)
target_link_libraries(main yalantinglibs::yalantinglibs)
target_compile_features(main PRIVATE cxx_std_20)
```

### Compile Manually:

1. Add `include/` directory to include path(skip it if you have install ylt into system default path).
2. Add `include/ylt/thirdparty` to include path(skip it if you have install thirdparty independency by the cmake option -DINSTALL_INDEPENDENT_THIRDPARTY=ON).
3. Enable `c++20` standard by option `-std=c++20`(g++/clang++) or `/std:c++20`(msvc)
3. If you use any header with `coro_` prefix, add link option `-pthread` in linux and add option `-fcoroutines` when you use g++.
4. That's all. We could find other options in `example/cmakelist.txt`.

- More Details:
### More Details:
For more details, see the cmake file [here](https://github.com/alibaba/yalantinglibs/blob/main/CMakeLists.txt) and [there](https://github.com/alibaba/yalantinglibs/tree/main/cmake).

# Introduction
Expand Down Expand Up @@ -157,19 +201,6 @@ Based on compile-time reflection, very easy to use, high performance serializati

Only one line code to finish serialization and deserialization, 2-20x faster than protobuf.

[English Introduction](https://alibaba.github.io/yalantinglibs/en/struct_pack/struct_pack_intro.html)

[English API] (TODO)

[(Slides) A Faster Serialization Library Based on Compile-time Reflection and C++ 20](https://alibaba.github.io/yalantinglibs/resource/A%20Faster%20Serialization%20Library%20Based%20on%20Compile-time%20Reflection%20and%20C++%2020.pdf) of struct_pack on CppCon2022


[(Video) A Faster Serialization Library Based on Compile-time Reflection and C++ 20](https://www.youtube.com/watch?v=myhB8ZlwOlE) on cppcon2022

[(Slides)(Chinese)](https://alibaba.github.io/yalantinglibs/resource/CppSummit_struct_pack.pdf) of struct_pack on purecpp conference.

[(Video)(Chinese)](https://live.csdn.net/room/csdnlive1/bKFbKP7T) on purecpp conference, start from 01:32:20 of the video record.

### quick example
```cpp
struct person {
Expand All @@ -187,8 +218,26 @@ std::vector<char> buffer = struct_pack::serialize(person1);
// one line code deserialization
auto person2 = deserialize<person>(buffer);
```
struct_pack is very fast.
![](https://alibaba.github.io/yalantinglibs/assets/struct_pack_bench_serialize.4ffb0ce6.png)
[English Introduction](https://alibaba.github.io/yalantinglibs/en/struct_pack/struct_pack_intro.html)
[English API] (TODO)
[(Slides) A Faster Serialization Library Based on Compile-time Reflection and C++ 20](https://alibaba.github.io/yalantinglibs/resource/A%20Faster%20Serialization%20Library%20Based%20on%20Compile-time%20Reflection%20and%20C++%2020.pdf) of struct_pack on CppCon2022
[(Video) A Faster Serialization Library Based on Compile-time Reflection and C++ 20](https://www.youtube.com/watch?v=myhB8ZlwOlE) on cppcon2022
[(Slides)(Chinese)](https://alibaba.github.io/yalantinglibs/resource/CppSummit_struct_pack.pdf) of struct_pack on purecpp conference.
[(Video)(Chinese)](https://live.csdn.net/room/csdnlive1/bKFbKP7T) on purecpp conference, start from 01:32:20 of the video record.
See more examples [here](https://alibaba.github.io/yalantinglibs/en/struct_pack/struct_pack_intro.html#serialization).
## struct_json
reflection-based json lib, very easy to do struct to json and json to struct.
Expand Down Expand Up @@ -386,18 +435,36 @@ See [async_simple](https://github.com/alibaba/async_simple)
# Details
## CMAKE OPTION
## CMAKE OPTION
## config option
These option maybe useful for your project. You can enable it in your project if you import ylt by cmake fetchContent or find_package.
These CMake options is used for yalantinglibs developing/installing itself. They are not effected for your project, because ylt is a head-only.
|option|default value|description|
|----------|------------|------|
|YLT_ENABLE_SSL|OFF|enable optional ssl support for rpc/http|
|YLT_ENABLE_PMR|OFF|enable pmr optimize|
|YLT_ENABLE_IO_URING|OFF|enable io_uring in linux|
|YLT_ENABLE_FILE_IO_URING|OFF|enable file io_uring as backend in linux|
|YLT_ENABLE_STRUCT_PACK_UNPORTABLE_TYPE|OFF|enable unportable type(like wstring, int128_t) for struct_pack|
|YLT_ENABLE_STRUCT_PACK_OPTIMIZE|OFF|optimize struct_pack by radical template unwinding(will cost more compile time)|
### INSTALL OPTION
## thirdparty installation option
In default, yalantinglibs will install thirdparty librarys in `ylt/thirdparty`. You need add it to include path when compile.
If you don't want to install the thirdparty librarys, you can turn off cmake option `-DINSTALL_THIRDPARTY=OFF`.
If you want to install the thirdparty independently (direct install it in system include path so that you don't need add `ylt/thirdparty` to include path), you can use turn on cmake option `-DINSTALL_INDEPENDENT_THIRDPARTY=ON`.
|option|default value|
|----------|------------|
|INSTALL_THIRDPARTY|ON|
|INSTALL_INDEPENDENT_THIRDPARTY|OFF|
### ylt develop option
## develop option
These CMake options is used for yalantinglibs developing/installing itself. They are not effected for your project, because ylt is a head-only.
|option|default value|
|----------|------------|
Expand All @@ -409,25 +476,8 @@ These CMake options is used for yalantinglibs developing/installing itself. They
|GENERATE_BENCHMARK_DATA|ON|
|CORO_RPC_USE_OTHER_RPC|ON|
### ylt config option
These option maybe useful for your project. If you want to enable it in your project, see the cmake code [here](https://github.com/alibaba/yalantinglibs/tree/main/cmake/config.cmake)
|option|default value|
|----------|------------|
|ENABLE_SSL|OFF|
|ENABLE_PMR|OFF|
|ENABLE_IO_URING|OFF|
|ENABLE_FILE_IO_URING|OFF|
|ENABLE_STRUCT_PACK_UNPORTABLE_TYPE|OFF|
|ENABLE_STRUCT_PACK_OPTIMIZE|OFF|
## Thirdparty Dependency
In default, yalantinglibs will install thirdparty librarys in `ylt/thirdparty`. You need add it to include path when compile.
If you don't want to install the thirdparty librarys, you can turn off cmake option `-DINSTALL_THIRDPARTY=OFF`.
If you want to install the thirdparty independently (direct install it in system include path so that you don't need add `ylt/thirdparty` to include path), you can use turn on cmake option `-DINSTALL_INDEPENDENT_THIRDPARTY=ON`.
## Thirdparty Dependency List
Here are the thirdparty libraries we used(Although async_simple is a part of ylt, it open source first, so we import it as a independence thirdparty library).
Expand Down Expand Up @@ -457,19 +507,11 @@ No dependency.
No dependency.
### struct_json
- [iguana](https://github.com/qicosmos/iguana)
### struct_pb (optional)
- [protobuf](https://protobuf.dev/)
### struct_xml
- [iguana](https://github.com/qicosmos/iguana)
### struct_yaml
### struct_json、struct_xml、struct_yaml
- [iguana](https://github.com/qicosmos/iguana)
Expand All @@ -494,6 +536,10 @@ see [Build Website](https://github.com/alibaba/yalantinglibs/blob/main/website/R
4. Choose one or more reviewers from contributors: (e.g., qicosmos, poor-circle, PikachuHyA).
5. Get approved and merged.

# Discussion group

DingTalk group id: 645010455

## License

yaLanTingLibs is distributed under the Apache License (Version 2.0)
Expand Down

0 comments on commit d8836aa

Please sign in to comment.