How to avoid two packages colliding?
stickM4N opened this issue · comments
Issue description
I'm adding my dependencies Protobuf and Snappy (both from Google) and they build gmock (other of Google libraries) included and it's built twice, which throws and error. How can I avoid that behavior?
CMakeLists.txt
...
add_package("gh:protocolbuffers/protobuf#main")
add_package("gh:google/snappy#main")
...
Error
CMake Error at C:/Users/jcgal/.cpm/snappy/59b0f9bb82a31b56cf6197fb63af2a03d76f9331/third_party/googletest/googletest/cmake/internal_utils.cmake:150 (add_library):
add_library cannot create target "gmock" because another target with the
same name already exists. The existing target is a static library created
in source directory
"C:/Users/jcgal/.cpm/protobuf/29522687be03e21b7683f4277ecb0c4d3bd83954".
See documentation for policy CMP0002 for more details.
Call Stack (most recent call first):
C:/Users/jcgal/.cpm/snappy/59b0f9bb82a31b56cf6197fb63af2a03d76f9331/third_party/googletest/googletest/cmake/internal_utils.cmake:206 (cxx_library_with_type)
C:/Users/jcgal/.cpm/snappy/59b0f9bb82a31b56cf6197fb63af2a03d76f9331/third_party/googletest/googlemock/CMakeLists.txt:101 (cxx_library)
CMake Error at C:/Users/jcgal/.cpm/snappy/59b0f9bb82a31b56cf6197fb63af2a03d76f9331/third_party/googletest/googlemock/CMakeLists.txt:102 (target_link_libraries):
Attempt to add link library "gtest" to target "gmock" which is not built in
this directory.
This is allowed only when policy CMP0079 is set to NEW.
CMake Error at C:/Users/jcgal/.cpm/snappy/59b0f9bb82a31b56cf6197fb63af2a03d76f9331/third_party/googletest/googletest/cmake/internal_utils.cmake:150 (add_library):
add_library cannot create target "gmock_main" because another target with
the same name already exists. The existing target is a static library
created in source directory
"C:/Users/jcgal/.cpm/protobuf/29522687be03e21b7683f4277ecb0c4d3bd83954".
See documentation for policy CMP0002 for more details.
Call Stack (most recent call first):
C:/Users/jcgal/.cpm/snappy/59b0f9bb82a31b56cf6197fb63af2a03d76f9331/third_party/googletest/googletest/cmake/internal_utils.cmake:206 (cxx_library_with_type)
C:/Users/jcgal/.cpm/snappy/59b0f9bb82a31b56cf6197fb63af2a03d76f9331/third_party/googletest/googlemock/CMakeLists.txt:104 (cxx_library)
CMake Warning (dev) at C:/Users/jcgal/.cpm/snappy/59b0f9bb82a31b56cf6197fb63af2a03d76f9331/third_party/googletest/googlemock/CMakeLists.txt:105 (target_link_libraries):
Policy CMP0023 is not set: Plain and keyword target_link_libraries
signatures cannot be mixed. Run "cmake --help-policy CMP0023" for policy
details. Use the cmake_policy command to set the policy and suppress this
warning.
The plain signature for target_link_libraries has already been used with
the target "gmock_main". All uses of target_link_libraries with a target
should be either all-keyword or all-plain.
The uses of the plain signature are here:
* C:/Users/jcgal/.cpm/protobuf/29522687be03e21b7683f4277ecb0c4d3bd83954/cmake/gtest.cmake:42 (target_link_libraries)
This warning is for project developers. Use -Wno-dev to suppress it.
CMake Error at C:/Users/jcgal/.cpm/snappy/59b0f9bb82a31b56cf6197fb63af2a03d76f9331/third_party/googletest/googlemock/CMakeLists.txt:105 (target_link_libraries):
Attempt to add link library "gmock" to target "gmock_main" which is not
built in this directory.
This is allowed only when policy CMP0079 is set to NEW.
fix it in your project
You can set SNAPPY_BUILD_TESTS
to OFF
& protobuf_BUILD_TESTS
to OFF
Snappy fix:
Other solution is to wrap:
https://github.com/google/snappy/blob/92f18e66fdf34bdde3d5116ca3fac3910fe14fc5/CMakeLists.txt#L315-L316
In a if(NOT TARGET gtest)
in snappy to support super build.
protobuf fix:
They should add a
if(TARGET gtest)
return()
endif()
Thanks for the quick reply. I'll test it soon and let you know.
I was looking for a more general solution from a CPM angle for avoiding this type of collisions, but seen you even took the time to review my case in particular I asume there is no such approach.
No cpm rely on the fact cmakelists are for super build, but often people don't write cmake with that in my mind.
Fix is often easy.
It worked just fine. Thanks a lot!