Skip to content

Commit dd72a38

Browse files
authored
added github actions workflow to build on all platforms (#13)
1 parent 6d40f6c commit dd72a38

File tree

7 files changed

+136
-27
lines changed

7 files changed

+136
-27
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
name: CMake on multiple platforms
2+
3+
on:
4+
push:
5+
branches: [ "main" ]
6+
pull_request:
7+
branches: [ "main" ]
8+
9+
jobs:
10+
build:
11+
runs-on: ${{ matrix.os }}
12+
13+
strategy:
14+
fail-fast: false
15+
16+
matrix:
17+
os: [ubuntu-latest, windows-latest, macos-latest]
18+
build_type: [Release]
19+
compiler: [gcc, clang, cl]
20+
include:
21+
- os: windows-latest
22+
c_compiler: cl
23+
cpp_compiler: cl
24+
- os: ubuntu-latest
25+
c_compiler: gcc
26+
cpp_compiler: g++
27+
- os: ubuntu-latest
28+
c_compiler: clang
29+
cpp_compiler: clang++
30+
- os: macos-latest
31+
c_compiler: clang
32+
cpp_compiler: clang++
33+
exclude:
34+
- os: ubuntu-latest
35+
compiler: cl
36+
- os: macos-latest
37+
compiler: gcc
38+
- os: macos-latest
39+
compiler: cl
40+
41+
steps:
42+
- uses: actions/checkout@v4
43+
with:
44+
submodules: 'true'
45+
46+
- name: Set reusable strings
47+
# Turn repeated input strings (such as the build output directory) into step outputs. These step outputs can be used throughout the workflow file.
48+
id: strings
49+
shell: bash
50+
run: |
51+
echo "build-output-dir=${{ github.workspace }}/build" >> "$GITHUB_OUTPUT"
52+
53+
- name: Configure CMake
54+
# Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make.
55+
# See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type
56+
run: >
57+
cmake -B ${{ steps.strings.outputs.build-output-dir }}
58+
-DCMAKE_CXX_COMPILER=${{ matrix.cpp_compiler }}
59+
-DCMAKE_C_COMPILER=${{ matrix.c_compiler }}
60+
-DCMAKE_BUILD_TYPE=${{ matrix.build_type }}
61+
-S ${{ github.workspace }}
62+
63+
- name: Build
64+
# Build your program with the given configuration. Note that --config is needed because the default Windows generator is a multi-config generator (Visual Studio generator).
65+
run: cmake --build ${{ steps.strings.outputs.build-output-dir }} --config ${{ matrix.build_type }}
66+
67+
- name: Test
68+
working-directory: ${{ steps.strings.outputs.build-output-dir }}
69+
# Execute tests defined by the CMake configuration. Note that --build-config is needed because the default Windows generator is a multi-config generator (Visual Studio generator).
70+
# See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail
71+
run: ctest --build-config ${{ matrix.build_type }}

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ add_subdirectory(vendor/fmt)
77
add_subdirectory(znet)
88

99
# examples
10-
add_subdirectory(examples/demo-server)
11-
add_subdirectory(examples/demo-client)
10+
add_subdirectory(examples/example-server)
11+
add_subdirectory(examples/example-client)
1212

1313
# tests
1414
enable_testing()
Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
cmake_minimum_required(VERSION 3.16.3)
2-
project(demo-client)
2+
project(example-client)
33

44
set(CMAKE_CXX_STANDARD 20)
55

6-
set(CMAKE_BUILD_TYPE Debug)
7-
set(CMAKE_CXX_FLAGS_DEBUG -g -O0 CACHE STRING "")
8-
9-
add_executable(demo-client main.cc)
10-
target_link_libraries(demo-client PRIVATE znet)
11-
include_directories(../demo-common)
6+
add_executable(example-client main.cc)
7+
target_link_libraries(example-client PRIVATE znet)
8+
include_directories(../example-common)
129

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
cmake_minimum_required(VERSION 3.16.3)
2-
project(demo-server)
2+
project(example-server)
33

44
set(CMAKE_CXX_STANDARD 20)
55

6-
add_executable(demo-server main.cc)
7-
target_link_libraries(demo-server PRIVATE znet)
8-
include_directories(../demo-common)
6+
add_executable(example-server main.cc)
7+
target_link_libraries(example-server PRIVATE znet)
8+
include_directories(../example-common)
99

znet/CMakeLists.txt

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@ project(znet)
44
set(CMAKE_CXX_STANDARD 20)
55
set(CMAKE_CXX_STANDARD_REQUIRED ON)
66

7-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wvla -Werror=vla -fpermissive -Wno-deprecated")
7+
if(NOT MSVC)
8+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fpermissive -Werror=vla -Wno-deprecated")
9+
endif()
810
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
911
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
1012
endif()
1113
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
12-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libstdc++ -fconcepts")
13-
endif()
14-
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
15-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
14+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fconcepts")
1615
endif()
1716

1817
set(ZNET_SOURCES
@@ -44,7 +43,7 @@ if(WIN32)
4443
endif()
4544

4645
# FMT
47-
find_package(fmt CONFIG REQUIRED)
46+
#find_package(fmt CONFIG REQUIRED)
4847
target_link_libraries(znet fmt::fmt)
4948

5049
# OpenSSL
@@ -54,4 +53,8 @@ target_link_libraries(znet OpenSSL::SSL OpenSSL::Crypto)
5453
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
5554
find_package(Threads REQUIRED)
5655
target_link_libraries(znet Threads::Threads)
56+
endif()
57+
58+
if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
59+
target_link_libraries(znet stdc++)
5760
endif()

znet/include/znet/logger.h

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,46 @@
4747
#define ZNET_LOG_LEVEL ZNET_LOG_LEVEL_DEBUG
4848
#endif
4949

50-
#define ZNET_PRINTFN(fmsg, func, msg, args...) \
51-
fmt::print(fmsg, func, fmt::format(msg, ##args)); std::cout << std::flush
50+
#ifdef _MSC_VER
51+
#define ZNET_PRINTFN(fmsg, func, msg, ...) \
52+
fmt::print(fmsg, func, fmt::format(msg, __VA_ARGS__)); \
53+
std::cout << std::flush
54+
55+
#if ZNET_LOG_LEVEL <= ZNET_LOG_LEVEL_DEBUG
56+
#define ZNET_LOG_DEBUG(msg, ...) \
57+
ZNET_PRINTFN("\x1b[44m[debug]\x1b[0m \x1b[35m{}: \x1b[0m{}\x1b[0m\n", \
58+
ZNET_FUNC_SIGN, msg, __VA_ARGS__)
59+
#else
60+
#define ZNET_LOG_DEBUG(msg, ...)
61+
#endif
62+
63+
#if ZNET_LOG_LEVEL <= ZNET_LOG_LEVEL_INFO
64+
#define ZNET_LOG_INFO(msg, ...) \
65+
ZNET_PRINTFN("\x1b[42m[info ]\x1b[0m \x1b[35m{}: \x1b[0m{}\x1b[0m\n", \
66+
ZNET_FUNC_SIGN, msg, __VA_ARGS__)
67+
#else
68+
#define ZNET_LOG_INFO(msg, ...)
69+
#endif
70+
71+
#if ZNET_LOG_LEVEL <= ZNET_LOG_LEVEL_WARN
72+
#define ZNET_LOG_WARN(msg, ...) \
73+
ZNET_PRINTFN("\x1b[41m[warn ]\x1b[0m \x1b[35m{}: \x1b[31m{}\x1b[0m\n", \
74+
ZNET_FUNC_SIGN, msg, __VA_ARGS__)
75+
#else
76+
#define ZNET_LOG_WARN(msg, ...)
77+
#endif
78+
79+
#if ZNET_LOG_LEVEL <= ZNET_LOG_LEVEL_ERROR
80+
#define ZNET_LOG_ERROR(msg, ...) \
81+
ZNET_PRINTFN("\x1b[41m[error]\x1b[0m \x1b[35m{}: \x1b[31m{}\x1b[0m\n", \
82+
ZNET_FUNC_SIGN, msg, __VA_ARGS__)
83+
#else
84+
#define ZNET_LOG_ERROR(msg, ...)
85+
#endif
86+
#else
87+
#define ZNET_PRINTFN(fmsg, func, msg, args...) \
88+
fmt::print(fmsg, func, fmt::format(msg, ##args)); \
89+
std::cout << std::flush
5290

5391
#if ZNET_LOG_LEVEL <= ZNET_LOG_LEVEL_DEBUG
5492
#define ZNET_LOG_DEBUG(msg, args...) \
@@ -67,21 +105,21 @@
67105
#endif
68106

69107
#if ZNET_LOG_LEVEL <= ZNET_LOG_LEVEL_WARN
70-
#define ZNET_LOG_WARN(msg, args...) \
108+
#define ZNET_LOG_WARN(msg, args...) \
71109
ZNET_PRINTFN("\x1b[41m[warn ]\x1b[0m \x1b[35m{}: \x1b[31m{}\x1b[0m\n", \
72110
ZNET_FUNC_SIGN, msg, ##args)
73111
#else
74112
#define ZNET_LOG_WARN(msg, args...)
75113
#endif
76114

77115
#if ZNET_LOG_LEVEL <= ZNET_LOG_LEVEL_ERROR
78-
#define ZNET_LOG_ERROR(msg, args...) \
116+
#define ZNET_LOG_ERROR(msg, args...) \
79117
ZNET_PRINTFN("\x1b[41m[error]\x1b[0m \x1b[35m{}: \x1b[31m{}\x1b[0m\n", \
80118
ZNET_FUNC_SIGN, msg, ##args)
81119
#else
82120
#define ZNET_LOG_ERROR(msg, args...)
83121
#endif
84-
122+
#endif
85123

86124
class LoggerInitializer {
87125
public:

znet/src/transport.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@ Ref<Buffer> TransportLayer::Receive() {
5757
#ifdef WIN32
5858
int err = WSAGetLastError();
5959
if (err == WSAEWOULDBLOCK) {
60-
return; // no data received
60+
return nullptr; // no data received
6161
}
6262
ZNET_LOG_ERROR("Closing connection due to an error: ", GetLastErrorInfo());
63-
Close();
63+
session_.Close();
6464
#else
6565
if (errno == EWOULDBLOCK || errno == EAGAIN) {
6666
return nullptr; // no data received
@@ -119,7 +119,7 @@ bool TransportLayer::Send(Ref<Buffer> buffer) {
119119
new_buffer->Write(buffer->data() + buffer->read_cursor(), buffer->size());
120120

121121
// todo check
122-
while (send(socket_, new_buffer->data(), new_buffer->size(), MSG_DONTWAIT) < 0) {
122+
while (send(socket_, new_buffer->data(), new_buffer->size(), 0) < 0) {
123123
if (errno == EWOULDBLOCK || errno == EAGAIN) {
124124
continue;
125125
}

0 commit comments

Comments
 (0)