zeromq / czmq

High-level C binding for ØMQ
czmq.zeromq.org
Mozilla Public License 2.0
1.18k stars 525 forks source link

Trouble configuring czmq, zmq, and sodium with CMake #2185

Open DavidM-Fox opened 3 years ago

DavidM-Fox commented 3 years ago

I'm having trouble configuring/building czmq with CMake. This is the current project structure with the three libraries' source under "extlibs":

root
  |--CMakeLists.txt
  |--src
  |--include
  |--extlibs
  |   |--CMakeLists.txt
  |   |--sodium
  |   |--zmq
  |   |--czmq

Since I am building the libzmq library, how can I structure my CMake so that czmq won't complain about libzmq.lib not being present at config time? I have tested building zmq with sodium with what I have and it works, just adding the additional build_libczmq() makes it fail. I have each library installing to "root/build/x64/extlibs/". I have tried a custom target dependency structure but am having no luck with getting czmq to be okay with not seeing the .lib at config time. What can I try to make this work? Thanks!

The root CMakeLists.txt:

cmake_minimum_required (VERSION 3.15)
set(project czmq_build_project)
set(project_root ${CMAKE_CURRENT_SOURCE_DIR})
set(extlibs ${project_root}/extlibs)
set(msbuild "C:/Program Files (x86)/Microsoft Visual Studio/2019/Enterprise/MSBuild/Current/Bin/MSBuild.exe")
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED on)
set(project_cfg Release)
project(${project} CXX)

# Add source
add_executable (${project} "src/main.cpp" "include/class.hpp")

# Directories to search for headers -----------
include_directories(${extlibs})
include_directories(${project_root}/include)

# Add 3rd Party Libraries
add_subdirectory(${extlibs})

And the "extlibs/CMakeLists.txt"

# ----------------------------------------------
#           Third Party Libraries
# Note: All libraries are being built under a
# static release config for x64 (at the moment)
# ----------------------------------------------
# czmq requires libzmq. Additionaly, libsodium is 
# used for the curve encryption library for libzmq. 
# Therefore, we build: libsodium->libzmq->czmq.
# ----------------------------------------------

include(ExternalProject)
set(lib_out "${CMAKE_BINARY_DIR}/extlibs")

add_custom_target(buildsodium ALL)

# Configure libsodium -------------------------
function(build_libsodium)
    set(sodium_root "${extlibs}/sodium")
    set(sodium_sln "${sodium_root}/builds/msvc/vs2019/libsodium.sln")
    set(sodium_bin "${sodium_root}/bin")
    set(sodium_lib "${sodium_root}/bin/x64/${project_cfg}/v142/static/libsodium.lib")
    set(sodium_out "${lib_out}/sodium/libsodium.lib")
    set(sodium_bldcmd ${msbuild} -m -t:Build -p:configuration=Static${project_cfg} -p:platform=x64 ${sodium_sln})
    ExternalProject_Add(libsodium
      SOURCE_DIR        "${sodium_root}"
      CONFIGURE_COMMAND ""
      BUILD_COMMAND     ${sodium_bldcmd}
      INSTALL_COMMAND ""
      ALWAYS TRUE
    )
    add_custom_command(TARGET libsodium POST_BUILD
        BYPRODUCTS ${sodium_out}
        COMMAND echo "Copying libsodium.lib to ${sodium_out}"
        COMMAND ${CMAKE_COMMAND} -E copy ${sodium_lib} ${sodium_out}
        COMMAND echo "Removing ${sodium_bin}"
        COMMAND ${CMAKE_COMMAND} -E remove_directory ${sodium_bin}
    )
    add_dependencies(buildsodium libsodium)
endfunction()

add_custom_target(buildzmq ALL)

# Configure libzmq ----------------------------
function(build_libzmq)
    set(ENABLE_CURVE OFF )
    set(WITH_LIBSODIUM_STATIC ON)
    set(ENABLE_CPACK OFF)
    set(BUILD_SHARED OFF)
    set(BUILD_STATIC ON)
    set(BUILD_TESTS OFF)
    set(CMAKE_INCLUDE_PATH ${extlibs}/sodium/src/libsodium/include)
    set(CMAKE_LIBRARY_PATH ${lib_out}/sodium)
    set(CMAKE_CXX_FLAGS_RELEASE "/MT /O2 /Ob2 /DNDEBUG")
    set(CMAKE_CXX_FLAGS_DEBUG "/MTd /Zi /Ob0 /Od /RTC1")
    set(CMAKE_INSTALL_PREFIX ${lib_out}/zmq)
    add_subdirectory(zmq)
    add_dependencies(buildzmq buildsodium)
endfunction()

add_custom_target(buildczmq ALL)

# Configure czmq ------------------------------
function(build_libczmq)
    set(ZeroMQ_DIR ${lib_out}/zmq/lib)
    set(CZMQ_BUILD_SHARED OFF)
    set(CZMQ_BUILD_STATIC ON)
    set(BUILD_TESTING OFF)
    set(CMAKE_PREFIX_PATH ${lib_out}/zmq)
    set(LIBZMQ_INCLUDE_DIRS ${extlibs}/zmq/include)
    set(LIBZMQ_LIBRARIES ${lib_out}/zmq/lib)
    set(CMAKE_CXX_FLAGS_RELEASE "-DZMQ_STATIC /MT /O2 /Ob2 /DNDEBUG")
    set(CMAKE_CXX_FLAGS_DEBUG "-DZMQ_STATIC /MTd /Zi /Ob0 /Od /RTC1")
    set(CMAKE_CONFIGURATION_TYPES "Debug;Release")
    set(CMAKE_INSTALL_PREFIX ${lib_out}/czmq)
    add_subdirectory(czmq)
    add_dependencies(buildczmq buildzmq)
endfunction()

build_libsodium()
build_libzmq()
build_libczmq()

CMake output:

 CMake generation started for configuration: 'x64'.
1> Working directory: ...<root>\build\x64
1> [CMake] -- Detected ZMQ Version - 4.3.4
1> [CMake] -- Could NOT find PkgConfig (missing: PKG_CONFIG_EXECUTABLE) 
1> [CMake] -- Not building draft classes and methods
1> [CMake] -- Using builtin sha1
1> [CMake] -- CURVE security is disabled
1> [CMake] -- Detected _WIN32_WINNT from CMAKE_SYSTEM_VERSION: 0x0A00
1> [CMake] -- Using polling method in I/O threads: epoll
1> [CMake] -- Including wepoll
1> [CMake] -- Using polling method in zmq_poll(er)_* API: poll
1> [CMake] -- Using 64 bytes alignment for lock-free data structures
1> [CMake] -- Using condition_variable_t implementation: stl11
1> [CMake] -- Checking whether noexcept is supported
1> [CMake] -- Could NOT find AsciiDoc (missing: ASCIIDOC_EXECUTABLE) 
1> [CMake] CMake Deprecation Warning at extlibs/zmq/tests/CMakeLists.txt:2 (cmake_minimum_required):
1> [CMake]   Compatibility with CMake < 2.8.12 will be removed from a future version of
1> [CMake]   CMake.
1> [CMake] 
1> [CMake]   Update the VERSION argument <min> value or use a ...<max> suffix to tell
1> [CMake]   CMake that the project does not need compatibility with older versions.
1> [CMake] 
1> [CMake] 
1> [CMake] CMake Warning (dev) at extlibs/zmq/tests/CMakeLists.txt:306 (message):
1> [CMake]   Test 'test_bind_stream_fuzzer' is not known to CTest.
1> [CMake] This warning is for project developers.  Use -Wno-dev to suppress it.
1> [CMake] 
1> [CMake] CMake Warning (dev) at extlibs/zmq/tests/CMakeLists.txt:306 (message):
1> [CMake]   Test 'test_bind_ws_fuzzer' is not known to CTest.
1> [CMake] This warning is for project developers.  Use -Wno-dev to suppress it.
1> [CMake] 
1> [CMake] CMake Warning (dev) at extlibs/zmq/tests/CMakeLists.txt:306 (message):
1> [CMake]   Test 'test_connect_stream_fuzzer' is not known to CTest.
1> [CMake] This warning is for project developers.  Use -Wno-dev to suppress it.
1> [CMake] 
1> [CMake] CMake Warning (dev) at extlibs/zmq/tests/CMakeLists.txt:306 (message):
1> [CMake]   Test 'test_connect_ws_fuzzer' is not known to CTest.
1> [CMake] This warning is for project developers.  Use -Wno-dev to suppress it.
1> [CMake] 
1> [CMake] CMake Warning (dev) at extlibs/zmq/tests/CMakeLists.txt:306 (message):
1> [CMake]   Test 'test_socket_options_fuzzer' is not known to CTest.
1> [CMake] This warning is for project developers.  Use -Wno-dev to suppress it.
1> [CMake] 
1> [CMake] CMake Deprecation Warning at extlibs/zmq/unittests/CMakeLists.txt:2 (cmake_minimum_required):
1> [CMake]   Compatibility with CMake < 2.8.12 will be removed from a future version of
1> [CMake]   CMake.
1> [CMake] 
1> [CMake]   Update the VERSION argument <min> value or use a ...<max> suffix to tell
1> [CMake]   CMake that the project does not need compatibility with older versions.
1> [CMake] 
1> [CMake] 
1> [CMake] CMake Warning (dev) at C:/Program Files (x86)/Microsoft Visual Studio/2019/Enterprise/Common7/IDE/CommonExtensions/Microsoft/CMake/CMake/share/cmake-3.19/Modules/FindPackageHandleStandardArgs.cmake:426 (message):
1> [CMake]   The package name passed to `find_package_handle_standard_args` (LIBZMQ)
1> [CMake]   does not match the name of the calling package (libzmq).  This can lead to
1> [CMake]   problems in calling code that expects `find_package` result variables
1> [CMake]   (e.g., `_FOUND`) to follow a certain pattern.
1> [CMake] Call Stack (most recent call first):
1> [CMake]   extlibs/czmq/Findlibzmq.cmake:83 (find_package_handle_standard_args)
1> [CMake]   extlibs/czmq/CMakeLists.txt:133 (find_package)
1> [CMake] This warning is for project developers.  Use -Wno-dev to suppress it.
1> [CMake] -- Could NOT find LIBZMQ (missing: LIBZMQ_LIBRARIES) 
1> [CMake] CMake Error at extlibs/czmq/CMakeLists.txt:144 (message):
1> [CMake]   libzmq not found.
1> [CMake] 
1> [CMake] 
1> [CMake] -- Configuring incomplete, errors occurred!
1> [CMake] See also "<root>/build/x64/CMakeFiles/CMakeOutput.log".
1> [CMake] See also "<root>/build/x64/CMakeFiles/CMakeError.log".
sphaero commented 3 years ago

I've only skimmed your issue quickly but at least I'm using czmq as a subdirectory in cmake. See here: https://github.com/hku-ect/gazebosc/blob/16c19a26cb987a0a75808dd42b7f10f287a84664/CMakeLists.txt#L66

We should be able to this better so if I can help I will

sphaero commented 2 years ago

From what I recall this is mostly about using zeromq projects as subdirectories in cmake. I think the ingescape project is doing this succesfully: see: https://github.com/zeromq/ingescape/blob/feb016e9c797e1a5c7b978d14ed6fcd78e2372e2/CMakeLists.txt#L161

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity for 90 days. It will be closed if no further activity occurs within 21 days. Thank you for your contributions.