Skip to content

Commit 8ea56f9

Browse files
authored
Merge pull request #8 from Hakkadaikon/develop
develop -> main
2 parents c541651 + e4fd8c6 commit 8ea56f9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+2472
-291
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ perf.data.old
6060
examples/echoback/bin/*
6161
examples/echoback/obj/*
6262

63+
# Log
64+
*.txt
65+
6366
# Other
6467
tests/build
6568
build
@@ -70,3 +73,6 @@ tmp/
7073
native/src
7174
native/lib
7275
native/obj
76+
77+
!CMakeLists.txt
78+
!.gitignore

CMakeLists.txt

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
11
cmake_minimum_required(VERSION 3.13)
22
project(wsserver C)
3+
enable_language(ASM)
34

45
# Set a default build type if not specified (for single-config generators)
56
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
67
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose the type of build." FORCE)
78
endif()
89

9-
# Collect all .c files from the src directory and its subdirectories
10-
file(GLOB_RECURSE SRC_FILES "${CMAKE_CURRENT_SOURCE_DIR}/src/*.c")
10+
if (${CMAKE_SYSTEM_NAME} STREQUAL "Darwin")
11+
# Collect all .c files from the src directory and its subdirectories
12+
file(GLOB_RECURSE SRC_FILES "${CMAKE_CURRENT_SOURCE_DIR}/src/*.c")
13+
else()
14+
# Collect all .c files from the src directory and its subdirectories
15+
file(GLOB_RECURSE SRC_FILES "${CMAKE_CURRENT_SOURCE_DIR}/src/*.c")
16+
# Collect all assembly files (*.S) from the src directory and its subdirectories
17+
file(GLOB_RECURSE ASM_FILES "${CMAKE_CURRENT_SOURCE_DIR}/src/*.S")
18+
endif()
1119

12-
# Create the static library target
13-
add_library(wsserver STATIC ${SRC_FILES})
20+
# Create the static library target including C and assembly files
21+
add_library(wsserver STATIC ${SRC_FILES} ${ASM_FILES})
22+
add_library(wsserver_shared SHARED ${SRC_FILES} ${ASM_FILES})
1423

1524
# Set the output directory for the static library to the "lib" folder in the build directory
1625
set_target_properties(wsserver PROPERTIES
@@ -28,17 +37,28 @@ else()
2837
set(DEBUG_COMPILE_OPTIONS "-O0" "-static-libasan" "-g")
2938
endif()
3039

31-
# Add compile options and definitions for each build type using generator expressions
32-
target_compile_options(wsserver PRIVATE
33-
$<$<CONFIG:Debug>:${DEBUG_COMPILE_OPTIONS}>
34-
$<$<CONFIG:Release>:-O3>
35-
$<$<CONFIG:Release>:-fno-lto>
36-
$<$<CONFIG:Release>:-mtune=native>
37-
$<$<CONFIG:Release>:-ffast-math>
38-
$<$<CONFIG:Release>:-fno-math-errno>
39-
$<$<CONFIG:Release>:-falign-functions>
40-
$<$<CONFIG:Release>:-flto=auto>
41-
)
40+
if (${CMAKE_SYSTEM_NAME} STREQUAL "Darwin")
41+
# Add compile options and definitions for each build type using generator expressions
42+
target_compile_options(wsserver PRIVATE
43+
$<$<CONFIG:Debug>:${DEBUG_COMPILE_OPTIONS}>
44+
$<$<CONFIG:Release>:-O3>
45+
$<$<CONFIG:Release>:-mtune=native>
46+
$<$<CONFIG:Release>:-ffast-math>
47+
$<$<CONFIG:Release>:-fno-math-errno>
48+
$<$<CONFIG:Release>:-falign-functions>
49+
)
50+
else()
51+
# Add compile options and definitions for each build type using generator expressions
52+
target_compile_options(wsserver PRIVATE
53+
$<$<CONFIG:Debug>:${DEBUG_COMPILE_OPTIONS}>
54+
$<$<CONFIG:Release>:-O3>
55+
$<$<CONFIG:Release>:-mtune=native>
56+
$<$<CONFIG:Release>:-ffast-math>
57+
$<$<CONFIG:Release>:-fno-math-errno>
58+
$<$<CONFIG:Release>:-falign-functions>
59+
$<$<CONFIG:Release>:-flto=auto>
60+
)
61+
endif()
4262

4363
target_compile_definitions(wsserver PRIVATE
4464
$<$<CONFIG:Debug>:LOG_LEVEL_DEBUG>

examples/echoback/Makefile

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,30 +44,49 @@ CFLAGS := \
4444
-O0 \
4545
-g \
4646
-DLOG_LEVEL_DEBUG
47+
48+
LDLIBS := -L../../build/lib
49+
LDFLAGS := -lwsserver
4750
else
4851
CFLAGS := \
4952
-I../../src \
5053
-O0 \
5154
-static-libasan \
5255
-g \
56+
-fPIC \
5357
-DLOG_LEVEL_DEBUG
54-
endif
5558

5659
LDLIBS := -L../../build/lib
57-
LDFLAGS := -flto -lwsserver
60+
LDFLAGS := -lwsserver
61+
endif
5862

5963
else
6064
ifeq ($(BUILD), release)
65+
ifeq ($(OS), Darwin)
6166
CFLAGS := \
6267
-I../../src \
6368
-O3 \
64-
-fno-lto \
69+
-flto=auto \
6570
-mtune=native \
6671
-ffast-math \
6772
-fno-math-errno \
6873
-falign-functions \
74+
-DLOG_LEVEL_ERROR
75+
76+
LDLIBS := -L../../build/lib
77+
LDFLAGS := -flto -lwsserver
78+
else
79+
CFLAGS := \
80+
-I../../src \
81+
-O3 \
6982
-flto=auto \
83+
-mtune=native \
84+
-ffast-math \
85+
-fno-math-errno \
86+
-falign-functions \
87+
-fPIC \
7088
-DLOG_LEVEL_ERROR
89+
endif
7190

7291
LDLIBS := -L../../build/lib
7392
LDFLAGS := -flto -lwsserver

examples/echoback/src/main.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
#include <stddef.h>
2-
#include <stdio.h>
31
//#include <websocket.h>
2+
//#include <stdio.h>
43
#include "../../../src/websocket/websocket.h"
54

65
void websocket_receive_callback(

shell/check_ldd.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/bash
2+
3+
cd $(dirname $0)
4+
5+
ECHOBACK_SERVER_PATH=../examples/echoback
6+
ECHOBACK_SERVER_BIN_PATH=${ECHOBACK_SERVER_PATH}/bin/wsserver
7+
8+
wsDebugBuild() {
9+
rm -rf ../build 1>/dev/null
10+
cmake -S .. -B ../build -DCMAKE_BUILD_TYPE=Debug 1>/dev/null
11+
cmake --build ../build 1>/dev/null
12+
make clean -C ${ECHOBACK_SERVER_PATH} 1>/dev/null
13+
BUILD=debug make -C ${ECHOBACK_SERVER_PATH} 1>/dev/null
14+
}
15+
16+
echo "check link (examples/echoback/bin/wsserver)..."
17+
wsDebugBuild
18+
ldd ../examples/echoback/bin/wsserver

shell/run.sh

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,50 @@
22

33
cd $(dirname $0)
44

5+
ECHOBACK_SERVER_PATH=../examples/echoback
6+
ECHOBACK_SERVER_BIN_PATH=${ECHOBACK_SERVER_PATH}/bin/wsserver
7+
8+
wsDebugBuild() {
9+
rm -rf ../build 1>/dev/null
10+
make clean -C ${ECHOBACK_SERVER_PATH} 1>/dev/null
11+
cmake -S .. -B ../build -DCMAKE_BUILD_TYPE=Debug 1>/dev/null
12+
cmake --build ../build 1>/dev/null
13+
BUILD=debug make -C ${ECHOBACK_SERVER_PATH} 1>/dev/null
14+
}
15+
16+
wsReleaseBuild() {
17+
rm -rf ../build 1>/dev/null
18+
make clean -C ${ECHOBACK_SERVER_PATH} 1>/dev/null
19+
cmake -S .. -B ../build -DCMAKE_BUILD_TYPE=Release 1>/dev/null
20+
cmake --build ../build 1>/dev/null
21+
BUILD=release make -C ${ECHOBACK_SERVER_PATH} 1>/dev/null
22+
}
23+
524
case "$1" in
6-
memcheck)
7-
make debug-build -C ..
8-
valgrind --tool=memcheck --leak-check=full --show-leak-kinds=all --track-origins=yes -s ../result/bin/ws-server
9-
;;
10-
helgrind)
11-
make debug-build -C ..
12-
valgrind --tool=helgrind --history-level=approx -s ../result/bin/ws-server
13-
;;
14-
*)
15-
cd .. && nix-build
16-
result/bin/ws-server
17-
;;
25+
memcheck)
26+
wsDebugBuild
27+
echo "debug run (memcheck)"
28+
valgrind --tool=memcheck --leak-check=full --show-leak-kinds=all --track-origins=yes -s ${ECHOBACK_SERVER_BIN_PATH}
29+
;;
30+
helgrind)
31+
wsDebugBuild
32+
echo "debug run (helgrind)"
33+
valgrind --tool=helgrind --history-level=approx -s ${ECHOBACK_SERVER_BIN_PATH}
34+
;;
35+
debug)
36+
wsDebugBuild
37+
echo "debug run"
38+
#gdb --args ${ECHOBACK_SERVER_BIN_PATH}
39+
${ECHOBACK_SERVER_BIN_PATH}
40+
;;
41+
release)
42+
wsReleaseBuild
43+
echo "release run"
44+
${ECHOBACK_SERVER_BIN_PATH}
45+
;;
46+
*)
47+
wsReleaseBuild
48+
echo "release run"
49+
${ECHOBACK_SERVER_BIN_PATH}
50+
;;
1851
esac

src/arch/accept.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#ifndef NOSTR_INTERNAL_ACCEPT_H_
2+
#define NOSTR_INTERNAL_ACCEPT_H_
3+
4+
#ifdef __APPLE__
5+
#include "darwin/accept.h"
6+
#else
7+
#include "linux/x86_64/accept.h"
8+
#endif
9+
10+
static inline int internal_accept(const int sock_fd, struct sockaddr* addr, socklen_t* addrlen, const int flags)
11+
{
12+
#ifdef __APPLE__
13+
return darwin_accept(sock_fd, addr, addrlen, flags);
14+
#else
15+
return linux_x8664_accept4(sock_fd, addr, addrlen, flags);
16+
#endif
17+
}
18+
19+
#endif

src/arch/close.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#ifndef NOSTR_INTERNAL_CLOSE_H_
2+
#define NOSTR_INTERNAL_CLOSE_H_
3+
4+
#ifdef __APPLE__
5+
#include "darwin/close.h"
6+
#else
7+
#include "linux/x86_64/close.h"
8+
#endif
9+
10+
static inline int internal_close(int fd)
11+
{
12+
#ifdef __APPLE__
13+
return darwin_close(fd);
14+
#else
15+
return linux_x8664_close(fd);
16+
#endif
17+
}
18+
19+
#endif

src/arch/darwin/accept.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#ifndef NOSTR_DARWIN_ACCEPT_H_
2+
#define NOSTR_DARWIN_ACCEPT_H_
3+
4+
#include <netinet/in.h>
5+
6+
static inline int darwin_accept(const int sock_fd, struct sockaddr* addr, socklen_t* addrlen, const int flags)
7+
{
8+
return accept(sock_fd, addr, addrlen);
9+
}
10+
11+
#endif

src/arch/darwin/close.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#ifndef NOSTR_DARWIN_CLOSE_H_
2+
#define NOSTR_DARWIN_CLOSE_H_
3+
4+
#include <unistd.h>
5+
6+
static inline int darwin_close(int fd)
7+
{
8+
return close(fd);
9+
}
10+
11+
#endif

0 commit comments

Comments
 (0)