eBay / NuRaft

C++ implementation of Raft core logic as a replication library
Apache License 2.0
996 stars 236 forks source link

How to use NuRaft with CMake's FetchContent? #458

Closed tisonkun closed 1 year ago

tisonkun commented 1 year ago
FetchContent_Declare(
        asio
        URL https://ghproxy.com/https://github.com/boostorg/asio/archive/boost-1.83.0.zip
        URL_HASH 0
)
FetchContent_MakeAvailable(nuraft)

returns

CMake Error at cmake-build-debug/_deps/nuraft-src/CMakeLists.txt:55 (message):
  Can't find ASIO header files

Ref -

tisonkun commented 1 year ago

Successful with:

FetchContent_Declare(
        nuraft
        GIT_REPOSITORY https://ghproxy.com/https://github.com/eBay/NuRaft.git
        GIT_TAG 188947bcc73ce38ab1c3cf9d01015ca8a29decd9
        GIT_SUBMODULES_RECURSE TRUE
)
FetchContent_MakeAvailable(nuraft)
tisonkun commented 1 year ago

No. Still cannot #include in any C++ file.

JosiahWI commented 1 year ago

@tisonkun I don't think you need to find_package() if you are using the FetchContent system.

tisonkun commented 1 year ago

@JosiahWI

include(FetchContent)

FetchContent_Declare(
        NuRaft
        GIT_REPOSITORY https://ghproxy.com/https://github.com/eBay/NuRaft.git
        GIT_TAG 188947bcc73ce38ab1c3cf9d01015ca8a29decd9
        GIT_SUBMODULES_RECURSE TRUE
)
FetchContent_Populate(NuRaft)

add_executable(mephisto main.cpp)
target_link_libraries(mephisto PRIVATE NuRaft::static_lib)

Gives

-- The C compiler identification is AppleClang 14.0.3.14030022
-- The CXX compiler identification is AppleClang 14.0.3.14030022
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /Library/Developer/CommandLineTools/usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /Library/Developer/CommandLineTools/usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done (19.4s)
CMake Error at CMakeLists.txt:17 (target_link_libraries):
  Target "mephisto" links to:

    NuRaft::static_lib

  but the target was not found.  Possible reasons include:

    * There is a typo in the target name.
    * A find_package call is missing for an IMPORTED target.
    * An ALIAS target is missing.

-- Generating done (0.0s)
CMake Generate step failed.  Build files cannot be regenerated correctly.

Is my target_link_libraries config wrong?

JosiahWI commented 1 year ago

With FetchContent you will usually use add_subdirectory? See https://cliutils.gitlab.io/modern-cmake/chapters/projects/fetch.html for example as well as the great documentation: https://cmake.org/cmake/help/latest/module/FetchContent.html.

JosiahWI commented 1 year ago

@tisonkun The target should be static_lib, without the NuRaft::static_lib. I read the documentation more carefully and you did everything else correctly.

tisonkun commented 1 year ago

@JosiahWI This one can configured, but still no target to be included when compile:

include(FetchContent)

FetchContent_Declare(
        NuRaft
        GIT_REPOSITORY https://ghproxy.com/https://github.com/eBay/NuRaft.git
        GIT_TAG 188947bcc73ce38ab1c3cf9d01015ca8a29decd9
        GIT_SUBMODULES_RECURSE TRUE
)
FetchContent_MakeAvailable(NuRaft)

add_executable(mephisto main.cpp)
target_link_libraries(mephisto PRIVATE static_lib)

The setup is not complex. You can open a new CMake project with CLion and paste the snippet above to verify. Nothing extra.

tisonkun commented 1 year ago
➜  cmake-build-debug cmake -G Ninja ..
-- Build type: Debug
-- Build Install Prefix : /usr/local
-- ASIO include path: /Users/tison/Brittani/mephisto/cmake-build-debug/_deps/nuraft-src/asio/asio/include
-- deps prefix is not given
-- OpenSSL library path: /opt/homebrew/opt/openssl@1.1/lib/libssl.a
-- OpenSSL include path: /opt/homebrew/opt/openssl@1.1/include
-- Output library file name: libnuraft.a
-- Configuring done (0.2s)
-- Generating done (0.0s)
-- Build files have been written to: /Users/tison/Brittani/mephisto/cmake-build-debug
➜  cmake-build-debug ninja
[5/71] Building CXX object CMakeFiles/mephisto.dir/main.cpp.o
FAILED: CMakeFiles/mephisto.dir/main.cpp.o 
/Library/Developer/CommandLineTools/usr/bin/c++   -g -std=gnu++2b -arch arm64 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk -fcolor-diagnostics -MD -MT CMakeFiles/mephisto.dir/main.cpp.o -MF CMakeFiles/mephisto.dir/main.cpp.o.d -o CMakeFiles/mephisto.dir/main.cpp.o -c /Users/tison/Brittani/mephisto/main.cpp
/Users/tison/Brittani/mephisto/main.cpp:2:10: fatal error: 'libnuraft/nuraft.hxx' file not found
#include "libnuraft/nuraft.hxx"
         ^~~~~~~~~~~~~~~~~~~~~~
1 error generated.
[18/71] Building CXX object _deps/nuraft-build/CMakeFiles/RAFT_CORE_OBJ.dir/src/asio_service.cxx.o
ninja: build stopped: subcommand failed.
#include <iostream>
#include "libnuraft/nuraft.hxx"

int main() {
    std::cout << "Hello, World!" << std::endl;
    return 0;
}
JosiahWI commented 1 year ago

This is a bug, thanks for reporting.

There is an easy workaround; you can call target_include_directories(mephisto PRIVATE path/to/nuraft/include/dir) on your target. I will open an issue for the bug and submit a patch.

tisonkun commented 1 year ago

Thank you! Your workaround helps. The following configs compiled now:

cmake_minimum_required(VERSION 3.26)
project(mephisto)

set(CMAKE_CXX_STANDARD 23)

include(FetchContent)

FetchContent_Declare(
        NuRaft
        GIT_REPOSITORY https://ghproxy.com/https://github.com/eBay/NuRaft.git
        GIT_TAG 188947bcc73ce38ab1c3cf9d01015ca8a29decd9
        GIT_SUBMODULES_RECURSE TRUE
)
FetchContent_MakeAvailable(NuRaft)

add_executable(mephisto main.cpp)
target_include_directories(mephisto PRIVATE "${NuRaft_SOURCE_DIR}/include")
target_link_libraries(mephisto PRIVATE static_lib)
#include <iostream>
#include "libnuraft/nuraft.hxx"

int main() {
    std::cout << "Hello, World!" << std::endl;
    return 0;
}
tisonkun commented 1 year ago

I don't know if @JosiahWI you open a new issue to track the bug and I can close this one as workaround provided. Or if you want to close this issue associated with your PR, I'll keep it open.

JosiahWI commented 1 year ago

You can close this one. I'll use the more specific issues to track the corresponding bugfixes.