microsoft / vcpkg

C++ Library Manager for Windows, Linux, and MacOS
MIT License
22.8k stars 6.3k forks source link

[Boost] Latest CMake is not able to find Boost due to missing BoostConfig.cmake files #40561

Open sstarosz opened 3 weeks ago

sstarosz commented 3 weeks ago

Operating system

Windows 10

Compiler

MSVC

Steps to reproduce the behavior

Have installed CMake 3.30 or newer

Add boost dependencies to project using manifest mode

Set up cmake_minimum_required(VERSION 3.30) in CMakeLists.txt

Try to use find_package(Boost REQUIRED) inside CMake, just as vcpkg suggests.

Configure CMake and except that vcpkg instalation will run successfully, but CMake will throw an error due to missing BoostConfig.cmake file.

Failure logs

Output From CMake:

"C:\Program Files\CMake\bin\cmake.EXE" -DCMAKE_C_COMPILER=cl.exe -DCMAKE_CXX_COMPILER=cl.exe -DCMAKE_TOOLCHAIN_FILE=C:/Users/Sebastian/vcpkg/scripts/buildsystems/vcpkg.cmake -DCMAKE_INSTALL_PREFIX=C:/Users/Sebastian/Desktop/HelloWorld/out/install/x64-MSVC-Debug -SC:/Users/Sebastian/Desktop/HelloWorld -BC:/Users/Sebastian/Desktop/HelloWorld/out/build/x64-MSVC-Debug -G Ninja
-- Running vcpkg install
Loading dependency information for 152 packages...
Detecting compiler hash for triplet x64-windows...
Compiler found: C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.41.34120/bin/Hostx64/x64/cl.exe
All requested packages are currently installed.
Total install time: 200 ns
-- Running vcpkg install - done
CMake Error at out/build/x64-MSVC-Debug/vcpkg_installed/x64-windows/share/boost/vcpkg-cmake-wrapper.cmake:11 (_find_package):
  By not providing "FindBoost.cmake" in CMAKE_MODULE_PATH this project has
  asked CMake to find a package configuration file provided by "Boost", but
  CMake did not find one.

  Could not find a package configuration file provided by "Boost" with any of
  the following names:

    BoostConfig.cmake
    boost-config.cmake

  Add the installation prefix of "Boost" to CMAKE_PREFIX_PATH or set
  "Boost_DIR" to a directory containing one of the above files.  If "Boost"
  provides a separate development package or SDK, be sure it has been
  installed.
Call Stack (most recent call first):
  C:/Users/Sebastian/vcpkg/scripts/buildsystems/vcpkg.cmake:813 (include)
  CMakeLists.txt:6 (find_package)

-- Configuring incomplete, errors occurred!
The command: "C:\Program Files\CMake\bin\cmake.EXE" -DCMAKE_C_COMPILER=cl.exe -DCMAKE_CXX_COMPILER=cl.exe -DCMAKE_TOOLCHAIN_FILE=C:/Users/Sebastian/vcpkg/scripts/buildsystems/vcpkg.cmake -DCMAKE_INSTALL_PREFIX=C:/Users/Sebastian/Desktop/HelloWorld/out/install/x64-MSVC-Debug -SC:/Users/Sebastian/Desktop/HelloWorld -BC:/Users/Sebastian/Desktop/HelloWorld/out/build/x64-MSVC-Debug -G Ninja exited with code: 1

Additional context

It looks like CMake in version 3.30 remove FindBoost module: CMP0167

From CMake version 3.30 it looks like execution of find_package(Boost REQUIRED) looks for BoostConfig.cmake or boost-config.cmake. Even if CONFIG is not specified in find package.

So when user try to use find_package(Boost REQUIRED [COMPONENTS ...]) just like vcpkg sugest, the missing file causes an error in CMake.

As a workaround, you can set cmake_minimum_required(VERSION 3.29), but this still produce a warning in the newest CMake.

I tried to find BoostConfig.cmake or boost-config.cmake in the folders where vcpkg install Boost but I didn't find them. Maybe they are not generated by vpckg or I missed them, or vcpkg doesn't set the paths where they can be found. I think vcpkg should provide these files to be able to use boost in latest CMake.

This problem also applies to single boost libraries dependencies for example boost-algorithm or boost-filesystem

Content of files for reproduction

Manifest mode

{
    "$schema": "https://raw.githubusercontent.com/microsoft/vcpkg-tool/main/docs/vcpkg.schema.json",
    "dependencies": [
        "boost"
    ]
}

CMakePresets.json

{
    "version": 8,
    "configurePresets": [
        {
            "name": "x64-MSVC-Debug",
            "generator": "Ninja",
            "architecture": {
                "value": "x64",
                "strategy": "external"
            },
            "binaryDir": "${sourceDir}/out/build/${presetName}",
            "installDir": "${sourceDir}/out/install/${presetName}",
            "toolchainFile": "$env{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake",
            "cacheVariables": {
                "CMAKE_C_COMPILER": "cl.exe",
                "CMAKE_CXX_COMPILER": "cl.exe"
            }
        }
    ],
    "buildPresets": [
        {
            "name": "x64-MSVC-Debug-debug",
            "configurePreset": "x64-MSVC-Debug",
            "configuration": "Debug"
        }
    ]
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.30)

project(HelloWorld)

find_package(Boost REQUIRED COMPONENTS graph)

add_executable(HelloWorld main.cpp)

target_link_libraries(HelloWorld Boost::graph)

main.cpp


#include <iostream>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graphviz.hpp>

using Graph = boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS>;

int main() {
    Graph g;

    // Add vertices
    auto v1 = boost::add_vertex(g);
    auto v2 = boost::add_vertex(g);
    auto v3 = boost::add_vertex(g);

    // Add edges
    boost::add_edge(v1, v2, g);
    boost::add_edge(v2, v3, g);
    boost::add_edge(v3, v1, g);

    // Output the graph in Graphviz format
    boost::write_graphviz(std::cout, g);

    return 0;
}
dg0yt commented 3 weeks ago

From CMake version 3.30 it looks like execution of find_package(Boost REQUIRED) looks for BoostConfig.cmake or boost-config.cmake. Even if CONFIG is not specified in find package.

It is normal that CMake tries config mode after module mode.

dg0yt commented 3 weeks ago

cmake_minimum_required(VERSION 3.30)

FTR this disables CMake's FindBoost.cmake.

JonLiu1993 commented 3 weeks ago

@sstarosz , Thanks for your issue. At present, the latest camke does not provide FindBoost module. We use boost components in the way of findpackage(Boost REQUIRED [COMPONENTS ...]). The latest version of vcpkg provides boost[***]-config.cmake.

dg0yt commented 3 weeks ago

As a workaround, you can set cmake_minimum_required(VERSION 3.29),

Actually you only have to change a single policy to enable the old behavior.

if(POLICY CMP0167)
   cmake_policy(SET CMP0167 OLD)
endif()