boostorg / boost_install

8 stars 32 forks source link

[CMake] Target for component is added even if the correct variant of a component is NOT found. #39

Closed jjELT closed 4 years ago

jjELT commented 4 years ago

Using Boost 1.72, when trying to find a component using find_package(Boost ...) a target for the component is added even though the correct component may not have been found, e.g. only the debug variant has been found, but the release variant is needed.

Taking Boost::log for example. In the file cmake\boost_log-1.72.0\boost_log-config.cmake generated by Boost.Build (b2), the target is added 'very early' in line 31. The check for the correct variant starts in line 66. Calling find_package(Boost ...) a second time is useless, because of lines 3 to 5:

if(TARGET Boost::log)
  return()
endif()

The if(TARGET ...) always returns true since it was already added in the previous call in line 31.

That prevents a mechanism of searching for a component, building the correct variant if the wrong one was found, and finding the correct variant on the second call.

I propose to call the code add_library(Boost::log UNKNOWN IMPORTED) only if a suitable build variant has been found since there is no way to remove the useless target by calling something like remove_library(Boost::log).

pdimov commented 4 years ago

I can't help but suspect that you're trying to solve a nonexistent problem. There's no need to "search" for debug and release components separately. Both should be found at once, as I already noted in #38.

If it doesn't work for you, please describe the scenario that fails.

jjELT commented 4 years ago

This is a slightly different problem.

I understand that find_package(Boost ...) is used to find the already built libraries.

I am trying to create a CMake project, a user can run without having any knowledge about all the different variants and configurations (static/shared, debug/release, etc.) and without having to deal with Boost.Build (b2) at all.

My idea is to search for the libraries via find_package(Boost ...). If they are not found, I build them on-the-fly within my CMake project and afterwards call find_package(Boost ...) again, which should now find them, since I have just built them.

I cannot call find_package(Boost ...) a second time though, since, well... what I have described before. The first call already adds a target, even though the libraries were not found.

It's not a big problem. I just have to run the CMake configuration step again, and on that next run, the boost libraries are already found in the first call to find_package(Boost ...).

It would just be much more elegant in my eyes, if the target was not added if the libraries are not found.

pdimov commented 4 years ago

This should be implemented in 1.73, currently in beta: https://www.boost.org/users/history/version_1_73_0.html