简体   繁体   中英

cmake issue with FetchContent same protobuf dependency between grpc and or-tools

im using the FetchContent from cmake to manage my dependencies

FetchContent_Declare(
        protobuf
        GIT_REPOSITORY https://github.com/protocolbuffers/protobuf.git
        GIT_TAG        v3.13.0
        SOURCE_SUBDIR  cmake
)
## END PROTOBUF ##

## GRPC ##
FetchContent_Declare(
        grpc
        GIT_REPOSITORY https://github.com/grpc/grpc.git
        GIT_TAG        v1.33.x
        GIT_PROGRESS   TRUE
)
## END GRPC ##

## OR-TOOLS ##
FetchContent_Declare(
        or-tools
        GIT_REPOSITORY https://github.com/google/or-tools.git
        GIT_TAG        master
        GIT_PROGRESS   TRUE
        PATCH_COMMAND git apply "${PROJECT_SOURCE_DIR}/patches/or-tools-protobuf.patch"
)
FetchContent_MakeAvailable(protobuf grpc or-tools)

the problem is that both or-tools and grpc use protobuf internally too as FetchContent_Declare so the OR-TOOLS complains about that protobuf already exists

CMake Error at build/_deps/protobuf-src/cmake/libprotobuf-lite.cmake:64 (add_library):
  add_library cannot create target "libprotobuf-lite" because another target
  with the same name already exists.  The existing target is a static library
  created in source directory
  "/Users/samuaz/Projects/itbit/securities-settlement/cpp/third_party/grpc-src/third_party/protobuf/cmake".
  See documentation for policy CMP0002 for more details.
Call Stack (most recent call first):
  build/_deps/protobuf-src/cmake/CMakeLists.txt:250 (include) 

if I try to disable the protobuf for or-tools set(BUILD_Protobuf OFF), the or-tools complan aboud package protobuf is not found

--   No package 'protobuf' found
CMake Error at /Users/samuaz/Library/Application Support/JetBrains/Toolbox/apps/CLion/ch-0/202.6948.80/CLion.app/Contents/bin/cmake/mac/share/cmake-3.17/Modules/FindPkgConfig.cmake:497 (message):
  A required package was not found

how can avoid this issue or make the package protobuf available from the previous protobuf FetchContent_Declare?

thanks!

cross posting... already answered here: https://github.com/google/or-tools/issues/2219

modify deps.cmake (or cpp.cmake on stable v8.0) to

if(NOT BUILD_Protobuf AND NOT TARGET protobuf::libprotobuf)
  find_package(Protobuf REQUIRED)
endif()

related to: https://gitlab.kitware.com/cmake/cmake/-/issues/17735

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM