Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: build(uwebsockets): add cmakelists #1788

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/cpp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ jobs:
$ENV:CFLAGS='-I C:\vcpkg\installed\x64-windows\include';
$ENV:LDFLAGS='-L C:\vcpkg\installed\x64-windows\lib';
$ENV:CXX='clang++'; $ENV:EXEC_SUFFIX='.exe'; $ENV:WITH_LIBUV='1'; nmake
cmake -G "NMake Makefiles" -B build -S .
ls
- name: Run smoke test
run: |
Expand Down
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[submodule "uSockets"]
path = uSockets
url = https://github.com/uNetworking/uSockets.git
url = https://github.com/Napolitain/uSockets
[submodule "fuzzing/libEpollFuzzer"]
path = fuzzing/libEpollFuzzer
url = https://github.com/uNetworking/libEpollFuzzer
Expand Down
111 changes: 111 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
cmake_minimum_required(VERSION 3.10)

project(uWebSockets)
message(STATUS "Building ${PROJECT_NAME}")

# Compiler flags
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
# Set some default CXXFLAGS and LDFLAGS if not already set
if (NOT DEFINED CXXFLAGS)
set(CXXFLAGS "-march=native -O3 -Wpedantic -Wall -Wextra -Wsign-conversion -Wconversion")
endif()

set(LDFLAGS_UWS "")

# If not building with LTO, remove the flag
option(WITH_LTO "Enable Link-Time Optimization" ON)
if (WITH_LTO)
set(CXXFLAGS "${CXXFLAGS} -flto")
endif()

# Optionally build with Zlib
option(WITH_ZLIB "Enable Zlib (permessage-deflate)" ON)
if (WITH_ZLIB)
find_package(ZLIB REQUIRED)
set(LDFLAGS_UWS "${LDFLAGS_UWS} ${ZLIB_LIBRARIES}")
else()
add_definitions(-DUWS_NO_ZLIB)
endif()

# Optionally build with BoringSSL
option(WITH_BORINGSSL "Build with BoringSSL support" OFF)
option(WITH_OPENSSL "Build with OpenSSL support" OFF)
option(WITH_WOLFSSL "Build with WolfSSL support" OFF)

if (WITH_BORINGSSL)
include_directories(uSockets/boringssl/include)
set(LDFLAGS_UWS "${LDFLAGS_UWS} uSockets/boringssl/build/ssl/libssl.a uSockets/boringssl/build/crypto/libcrypto.a")
add_definitions(-DLIBUS_USE_OPENSSL)
elseif (WITH_OPENSSL)
find_package(OpenSSL REQUIRED)
set(LDFLAGS_UWS "${LDFLAGS_UWS} ${OPENSSL_LIBRARIES}")
elseif (WITH_WOLFSSL)
set(LDFLAGS_UWS "${LDFLAGS_UWS} -L/usr/local/lib -lwolfssl")
endif()

# Optionally build with QUIC (Http3)
option(WITH_QUIC "Build with Http3 QUIC support" OFF)
if (WITH_QUIC)
add_definitions(-DLIBUS_USE_QUIC)
set(LDFLAGS_UWS "${LDFLAGS_UWS} -pthread -lz -lm uSockets/lsquic/src/liblsquic/liblsquic.a")
endif()

# Optionally build with libuv or ASIO as event loop
option(WITH_LIBUV "Build with libuv event loop" OFF)
option(WITH_ASIO "Build with ASIO event loop" OFF)

if (WITH_LIBUV)
find_package(libuv CONFIG REQUIRED)
set(LDFLAGS_UWS "${LDFLAGS_UWS} ${LIBUV_LIBRARIES}")
elseif (WITH_ASIO)
set(CXXFLAGS "${CXXFLAGS} -pthread")
set(LDFLAGS_UWS "${LDFLAGS_UWS} -lpthread")
endif()

# Optionally build with AddressSanitizer (ASAN)
option(WITH_ASAN "Build with AddressSanitizer" OFF)
if (WITH_ASAN)
set(CXXFLAGS "${CXXFLAGS} -fsanitize=address -g")
set(LDFLAGS_UWS "${LDFLAGS_UWS} -lasan")
endif()

# Include paths
include_directories(src)
include_directories(uSockets/src)

# uSockets target (as it was in your Makefile)
add_subdirectory(uSockets)

# Add example executables
set(EXAMPLE_FILES "CachingApp" "HelloWorldThreaded" "Http3Server" "Broadcast" "HelloWorld" "Crc32" "ServerName"
"EchoServer" "BroadcastingEchoServer" "UpgradeSync" "UpgradeAsync" "ParameterRoutes")


string(STRIP "${LDFLAGS_UWS}" LDFLAGS_STRIPPED_UWS) # Strip leading/trailing whitespace

foreach(EXAMPLE_FILE IN LISTS EXAMPLE_FILES)
set(target_name ${EXAMPLE_FILE})
add_executable(${target_name} examples/${EXAMPLE_FILE}.cpp)
target_link_libraries(${target_name} uSockets ${LDFLAGS_STRIPPED_UWS})
target_compile_options(${target_name} PRIVATE ${CXXFLAGS})
endforeach()


# Install target (mimicking the 'install' section of your Makefile)
install(DIRECTORY src/ DESTINATION ${CMAKE_INSTALL_PREFIX}/include/uWebSockets)

# Clean target (CMake provides a clean target natively)
# If additional custom clean behavior is required, you can add commands here.

# Default target is all
add_custom_target(all_examples DEPENDS ${EXAMPLE_FILES})

add_subdirectory(tests)
add_subdirectory(benchmarks)
# If linux, add epoll benchmarker
if (UNIX AND NOT APPLE)
add_subdirectory(libEpollBenchmarker)
endif ()

47 changes: 47 additions & 0 deletions benchmarks/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
cmake_minimum_required(VERSION 3.16)
project(uWebSocketsBenchmarks)
message(STATUS "Building ${PROJECT_NAME}")

# Set C and C++ standards
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)

# Enable LTO if supported
include(CheckIPOSupported)
check_ipo_supported(RESULT lto_supported OUTPUT error)
if(lto_supported)
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
else()
message(WARNING "IPO / LTO is not supported: ${error}")
endif()

# Compile options: enable optimizations, march=native
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CMAKE_CXX_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -march=native")

# Add uSockets include directory
include_directories(${CMAKE_SOURCE_DIR}/libs/uWebSockets/uSockets/src)

# Define the parser executable
add_executable(${PROJECT_NAME}parser parser.cpp)
target_compile_options(${PROJECT_NAME}parser PRIVATE -flto)
target_link_options(${PROJECT_NAME}parser PRIVATE -flto)

# Compile tests with uSockets
add_executable(${PROJECT_NAME}broadcast_test broadcast_test.c)
target_compile_definitions(${PROJECT_NAME}broadcast_test PRIVATE -DLIBUS_USE_OPENSSL)
target_link_libraries(${PROJECT_NAME}broadcast_test ssl crypto ${LDFLAGS_STRIPPED_UWS} uSockets)
target_compile_options(${PROJECT_NAME}broadcast_test PRIVATE -flto)
target_link_options(${PROJECT_NAME}broadcast_test PRIVATE -flto)

add_executable(${PROJECT_NAME}load_test load_test.c)
target_compile_definitions(${PROJECT_NAME}load_test PRIVATE -DLIBUS_USE_OPENSSL)
target_link_libraries(${PROJECT_NAME}load_test ssl crypto ${LDFLAGS_STRIPPED_UWS} uSockets)
target_compile_options(${PROJECT_NAME}load_test PRIVATE -flto)
target_link_options(${PROJECT_NAME}load_test PRIVATE -flto)

add_executable(${PROJECT_NAME}scale_test scale_test.c)
target_compile_definitions(${PROJECT_NAME}scale_test PRIVATE -DLIBUS_USE_OPENSSL)
target_link_libraries(${PROJECT_NAME}scale_test ssl crypto ${LDFLAGS_STRIPPED_UWS} uSockets)
target_compile_options(${PROJECT_NAME}scale_test PRIVATE -flto)
target_link_options(${PROJECT_NAME}scale_test PRIVATE -flto)
24 changes: 23 additions & 1 deletion benchmarks/load_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,30 @@
#define htole64(x) OSSwapHostToLittleInt64(x)
#define be64toh(x) OSSwapBigToHostInt64(x)
#define le64toh(x) OSSwapLittleToHostInt64(x)
#else

#elif defined(_WIN32)
#include <windows.h>

#define htobe16(x) _byteswap_ushort(x)
#define htole16(x) (x)
#define be16toh(x) _byteswap_ushort(x)
#define le16toh(x) (x)

#define htobe32(x) _byteswap_ulong(x)
#define htole32(x) (x)
#define be32toh(x) _byteswap_ulong(x)
#define le32toh(x) (x)

#define htobe64(x) _byteswap_uint64(x)
#define htole64(x) (x)
#define be64toh(x) _byteswap_uint64(x)
#define le64toh(x) (x)

#elif defined(__linux__)
#include <endian.h>

#else
#error "Platform not supported"
#endif


Expand Down
22 changes: 22 additions & 0 deletions libEpollBenchmarker/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Set the CMake minimum version
cmake_minimum_required(VERSION 3.10)

# Project name
project(uWSLibEpollBenchmarker)
message(STATUS "Building ${PROJECT_NAME}")

# Set C++ standard
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Include directories
include_directories(../src ../uSockets/src)

# Linker flags for wrapped syscalls
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--wrap=recv,--wrap=bind,--wrap=listen,--wrap=send,--wrap=socket,--wrap=epoll_wait,--wrap=accept4,--wrap=epoll_ctl")

# Add the executable
add_executable(${PROJECT_NAME} ../examples/HelloWorld.cpp epoll_benchmarker.cpp)

# Link with uSockets library
target_link_libraries(${PROJECT_NAME} PRIVATE uSockets)
67 changes: 67 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
cmake_minimum_required(VERSION 3.10)

# Project name and language
project(uWebSocketsTests)
message(STATUS "Building ${PROJECT_NAME}")

# Set the C++ standard
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Address sanitizer flag
set(SANITIZE_FLAGS "-fsanitize=address")

# Define targets names with ${PROJECT_NAME} prefix
set(Query ${PROJECT_NAME}Query)
set(ChunkedEncoding ${PROJECT_NAME}ChunkedEncoding)
set(TopicTree ${PROJECT_NAME}TopicTree)
set(HttpRouter ${PROJECT_NAME}HttpRouter)
set(BloomFilter ${PROJECT_NAME}BloomFilter)
set(ExtensionsNegotiator ${PROJECT_NAME}ExtensionsNegotiator)
set(HttpParser ${PROJECT_NAME}HttpParser)

# Define the executables and their sources
add_executable(${Query} Query.cpp)
add_executable(${ChunkedEncoding} ChunkedEncoding.cpp)
add_executable(${TopicTree} TopicTree.cpp)
add_executable(${HttpRouter} HttpRouter.cpp)
add_executable(${BloomFilter} BloomFilter.cpp)
add_executable(${ExtensionsNegotiator} ExtensionsNegotiator.cpp)
add_executable(${HttpParser} HttpParser.cpp)

# Add the address sanitizer flags to each target
target_compile_options(${Query} PRIVATE ${SANITIZE_FLAGS})
target_link_options(${Query} PRIVATE ${SANITIZE_FLAGS})

target_compile_options(${ChunkedEncoding} PRIVATE ${SANITIZE_FLAGS})
target_link_options(${ChunkedEncoding} PRIVATE ${SANITIZE_FLAGS})

target_compile_options(${TopicTree} PRIVATE ${SANITIZE_FLAGS})
target_link_options(${TopicTree} PRIVATE ${SANITIZE_FLAGS})

target_compile_options(${HttpRouter} PRIVATE ${SANITIZE_FLAGS})
target_link_options(${HttpRouter} PRIVATE ${SANITIZE_FLAGS})

target_compile_options(${BloomFilter} PRIVATE ${SANITIZE_FLAGS})
target_link_options(${BloomFilter} PRIVATE ${SANITIZE_FLAGS})

target_compile_options(${ExtensionsNegotiator} PRIVATE ${SANITIZE_FLAGS})
target_link_options(${ExtensionsNegotiator} PRIVATE ${SANITIZE_FLAGS})

target_compile_options(${HttpParser} PRIVATE ${SANITIZE_FLAGS})
target_link_options(${HttpParser} PRIVATE ${SANITIZE_FLAGS})

# Performance target
add_custom_target(${PROJECT_NAME}performance
COMMAND ${CMAKE_COMMAND} -E env CXXFLAGS=-O3 ${CMAKE_COMMAND} --build . --target HttpRouter
COMMAND ./HttpRouter
)

# Smoke test target
add_custom_target(${PROJECT_NAME}smoke
COMMAND ../Crc32 &
COMMAND ${CMAKE_COMMAND} -E sleep 1
COMMAND ~/.deno/bin/deno run --allow-net smoke.mjs
COMMAND node smoke.mjs
COMMAND pkill Crc32
)
2 changes: 1 addition & 1 deletion uSockets
Submodule uSockets updated 2 files
+117 −0 CMakeLists.txt
+1 −1 boringssl