robotology / ycm-cmake-modules

YCM (YCM CMake modules) is a collection of various useful CMake modules.
http://robotology.github.io/ycm-cmake-modules
Other
50 stars 22 forks source link

FindGLFW3 fails when used with the main project and with a dependency fetched with FetchContent #390

Closed S-Dafarra closed 2 years ago

S-Dafarra commented 2 years ago

For example, the following code

find_package(YCM REQUIRED)
find_package(YARP 3.4 COMPONENTS os sig dev math idl_tools REQUIRED)
find_package(Threads REQUIRED)
find_package(OpenXR 1.0 REQUIRED)
find_package(GLFW3 REQUIRED)
find_package(GLEW REQUIRED) #Helps with the OpenGL configuration on Windows
find_package(Eigen3 REQUIRED)

### Dependencies
# Fetching GLFont for displaying labels
option(YARP_OPENXR_USES_SYSTEM_GLFont OFF)
if(YARP_OPENXR_USES_SYSTEM_GLFont)
     find_package(GLFont REQUIRED)
else()
    include(FetchContent)
    FetchContent_Declare(glfont
      GIT_REPOSITORY https://github.com/ami-iit/GLFont
      GIT_TAG master)

    FetchContent_GetProperties(glfont)
    if(NOT glfont_POPULATED)
      message(STATUS "Fetching GLFont...")
      FetchContent_MakeAvailable(glfont)
    endif()
endif()

fails with the error

-- Found YCM: /home/sdafarra/Software/robotology-superbuild/build/install/share/cmake/YCM (found version "0.13.0.3-20211103+git20fb1bf")
-- Found YARP: /home/sdafarra/Software/robotology-superbuild/build/install/lib/cmake/YARP (found version "3.5.100+20211228.31+git89bbe2bb1")
-- Fetching GLFont...
-- Found YCM: /home/sdafarra/Software/robotology-superbuild/build/install/share/cmake/YCM (found version "0.13.0.3-20211103+git20fb1bf")
CMake Error at /home/sdafarra/Software/robotology-superbuild/build/install/share/YCM/find-modules/FindGLFW3.cmake:108 (add_library):
  add_library cannot create imported target "GLFW3::GLFW3" because another
  target with the same name already exists.
Call Stack (most recent call first):
  build/_deps/glfont-src/test/CMakeLists.txt:1 (find_package)

-- Configuring incomplete, errors occurred!
See also "/home/sdafarra/Software/yarp-device-openxrheadset/build/CMakeFiles/CMakeOutput.log".
See also "/home/sdafarra/Software/yarp-device-openxrheadset/build/CMakeFiles/CMakeError.log".
CMake process exited with exit code 1.

Elapsed time: 00:01.

Everything works fine if the dependency fetched with FetchContent is placed first.

I guess this is because the target GLFW3::GLFW3 is always added even if it exists already (https://github.com/robotology/ycm/blob/1860bad63dcdf6727c1ce0746561e1cada0dccef/find-modules/FindGLFW3.cmake#L105)

cc @traversaro @drdanz

traversaro commented 2 years ago

Perhaps we could try to reproduce this without FetchContent?

I mean, is the following one: CMakeLists.txt:

find_package(GLFW3 REQUIRED)
find_package(GLFW3 REQUIRED)

working?

If this one is working, we could try the following:

CMakeLists.txt:

find_package(GLFW3 REQUIRED)
add_subdirectory(nested)

nested/CMakeLists.txt:

find_package(GLFW3 REQUIRED)
S-Dafarra commented 2 years ago

Perhaps we could try to reproduce this without FetchContent?

I mean, is the following one: CMakeLists.txt:

find_package(GLFW3 REQUIRED)
find_package(GLFW3 REQUIRED)

working?

Good point. This does not work already

traversaro commented 2 years ago

Great!

traversaro commented 2 years ago

Fixed in https://github.com/robotology/ycm/pull/398 and released in 0.13.2 .