ericniebler / range-v3

Range library for C++14/17/20, basis for C++20's std::ranges
Other
4.11k stars 440 forks source link

CMake find_package and add_subdirectory #1621

Closed SmorkalovG closed 3 years ago

SmorkalovG commented 3 years ago

Hi. It seems impossible to use add_subdirectory(range-v3) and find_package(range-v3) in one project. In my case there is a parent project, that contains

add_subdirectory(range-v3) 
add_subdirectory(<lib that uses find_package(range-v3)>) 

cmake exits with error:

CMake Error at range-v3/cmake/range-v3-config.cmake:1 (include):
  include could not find requested file:

    C:/projects/testCmake/range-v3/cmake/range-v3-targets.cmake

The minmal case is project that contains clone of range-v3 and CMakeLists.txt:

cmake_minimum_required(VERSION 3.5)
project(testCmake)

add_subdirectory(range-v3)
find_package(range-v3 REQUIRED)

I think it should work and there is a commit that says that fixes this behaviour: 8473ac782a3501a399a83c3517505b1bfd2627f9 But it does not fix anything. if (TARGET range-v3::meta) should be placed before include because range-v3-targets.cmake is generated by cmake install and there is no such file in add_subdirectory mode.

The only thing we need to fix is moving one line in cmake/range-v3-config.cmake

if (TARGET range-v3::meta)
  return()
endif()

include("${CMAKE_CURRENT_LIST_DIR}/range-v3-targets.cmake")
....

I can make PR soon if it helps.

ericniebler commented 3 years ago

A PR would be great, thanks.