2

I want to use uWebSockets for my own project which uses CMake.

There are two issues for me:

  1. uWebSocket uses Make as well as I think its own build system/file build.c?
  2. It depends itself on other libraries of which some use CMake others Make.

I would be happy if I can make it work with CMake even if not all of the project's possibilities are reflected, e.g. building examples or linking optional dependencies like boost asio for uSockets (one of its dependencies).

Is it possible to fetch the libraries via CMake's FetchContent? Note, uWebSockets has its dependency uSocket setup as a git submodule.

Does it make sense instead of rewriting the whole Makefiles in CMake to rather just call make from within CMake?

Regarding Conan: it throws an error which I am also not sure how to fix (cannot find MSBuild python package on linux?):

ERROR: Package 'usockets/0.8.5' not resolved: usockets/0.8.5: Cannot load recipe.
Error loading conanfile at '/home/<USER>/.conan2/p/usock995f8272b80d4/e/conanfile.py': Unable to load conanfile in /home/<USER>/.conan2/p/usock995f8272b80d4/e/conanfile.py
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/home/<USER>/.conan2/p/usock995f8272b80d4/e/conanfile.py", line 7, in <module>
    from conans import MSBuild, AutoToolsBuildEnvironment
ImportError: cannot import name 'MSBuild' from 'conans' (/usr/lib/python3.10/site-packages/conans/__init__.py)

PS: The uWebSocket project does not have a Getting Started guide and the maintainers are not open for any other build system than Make.

PSS: I found this discussion about a CMake integration. Though I am not sure if it is still up to date or longer than it needs to be.

jack
  • 1,658
  • 1
  • 7
  • 18
  • The usockets recipe is still not ported to Conan 2.0, which means, you can not run it with your current Conan version. All recipes are being ported to work with Conan 2.0, but it will take some time. To get some priority, please, open an issue to https://github.com/conan-io/conan-center-index/issues Meanwhile, you can use conan 1.59 to install usocket as workaround: `pip install conan==1.59.0` – uilianries Mar 18 '23 at 17:33

1 Answers1

2

I want to describe how I managed a minimal build via CMake.

Project folder structure:

$ROOT/
  CMakeLists.txt
  external/
    CMakeLists.txt
    uSockets/
      CMakeLists.txt
    uWebSockets/
      CMakeLists.txt
  main.cpp

The separate CMakeLists.txts:

# $ROOT/CMakeLists.txt
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
project(MyProj LANGUAGES C CXX)
set(CMAKE_CXX_STANDARD 23)
add_subdirectory(external)
add_executable(Main main.cpp)
target_link_libraries(Main uWebSockets)
# $ROOT/external/CMakeLists.txt
add_subdirectory(uSockets)
add_subdirectory(uWebSockets)
# $ROOT/external/uSockets/CMakeLists.txt
cmake_minimum_required(VERSION 3.24)
set(CMAKE_C_STANDARD 11)
include(FetchContent)
FetchContent_Declare(
  uSockets_content
  GIT_REPOSITORY https://github.com/uNetworking/uSockets
  GIT_TAG v0.8.5
  GIT_SHALLOW ON
  GIT_SUBMODULES ""
)
FetchContent_MakeAvailable(uSockets_content)
file(GLOB_RECURSE SOURCES ${usockets_content_SOURCE_DIR}/src/*.c)
add_library(uSockets ${SOURCES})
target_include_directories(uSockets PUBLIC ${usockets_content_SOURCE_DIR}/src)
target_compile_definitions(uSockets PRIVATE LIBUS_NO_SSL)
# $ROOT/external/uWebSockets/CMakeLists.txt
cmake_minimum_required(VERSION 3.24)
include(FetchContent)
FetchContent_Declare(
  uWebSockets_content
  GIT_REPOSITORY https://github.com/uNetworking/uWebSockets
  GIT_TAG v20.37.0
  GIT_SHALLOW ON
  GIT_SUBMODULES ""
)
FetchContent_MakeAvailable(uWebSockets_content)
find_package(ZLIB REQUIRED)
add_library(uWebSockets INTERFACE)
target_include_directories(uWebSockets INTERFACE ${uwebsockets_content_SOURCE_DIR}/src/)
target_link_libraries(uWebSockets INTERFACE uSockets ${ZLIB_LIBRARIES})

I would very gladly receive some feedback on that solution.

jack
  • 1,658
  • 1
  • 7
  • 18