friendlyanon / cmake-init

The missing CMake project initializer
GNU General Public License v3.0
1.98k stars 75 forks source link

cmake does not detect some vcpkg installed libraries when using cmake-init #65

Closed TinyAtoms closed 2 years ago

TinyAtoms commented 2 years ago

I have created a sample project without cmake-init to test if vcpkg/cmakke/etc were working correctly.

With a CMakeLists.txt containing

cmake_minimum_required(VERSION 3.20)
project(fibo CXX)

find_package(fmt  REQUIRED)
find_package(range-v3  REQUIRED)

add_executable(fibo main.cpp)
target_compile_features(fibo PRIVATE cxx_std_17)

target_link_libraries(fibo
  PRIVATE
    fmt::fmt
    range-v3::range-v3)

everything works fine.

When I create a project with cmake-init, it stops being able to detect range-v3, but it does detect fmt.

environment:

cmake version 3.20.2 cmake-init version 0.31.2

steps to reproduce

  1. create a cmake-init project, select vcpkg as the package manager
  2. Open project in clion, and add "-DCMAKE_TOOLCHAIN_FILE=C:/tools/vcpkg/scripts/buildsystems/vcpkg.cmake" to the cmake profile.
  3. add these lines to the generated CMakeLists.txt:
    find_package(fmt  REQUIRED)
    find_package(range-v3  REQUIRED)
    target_link_libraries(cmtest_lib PRIVATE fmt::fmt range-v3::range-v3)
  4. reset cmake cache and reload project

Result:

"C:\Program Files\JetBrains\CLion 2021.2.3\bin\cmake\win\bin\cmake.exe" -DCMAKE_BUILD_TYPE=Debug -DCMAKE_TOOLCHAIN_FILE=C:/tools/vcpkg/scripts/buildsystems/vcpkg.cmake -DCMAKE_DEPENDS_USE_COMPILER=FALSE -G "CodeBlocks - NMake Makefiles" C:\Users\Massiveatoms\Desktop\compsci\cmtest
-- Running vcpkg install
Detecting compiler hash for triplet x64-windows...
Detecting compiler hash for triplet x86-windows...
All requested packages are currently installed.
Restored 0 package(s) from C:\Users\Massiveatoms\AppData\Local\vcpkg\archives in 336.4 us. Use --debug to see more details.

Total elapsed time: 14.25 s

The package fmt provides CMake targets:

    find_package(fmt CONFIG REQUIRED)
    target_link_libraries(main PRIVATE fmt::fmt)

    # Or use the header-only version
    find_package(fmt CONFIG REQUIRED)
    target_link_libraries(main PRIVATE fmt::fmt-header-only)

-- Running vcpkg install - done
CMake Error at C:/tools/vcpkg/scripts/buildsystems/vcpkg.cmake:824 (_find_package):
  By not providing "Findrange-v3.cmake" in CMAKE_MODULE_PATH this project has
  asked CMake to find a package configuration file provided by "range-v3",
  but CMake did not find one.

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

    range-v3Config.cmake
    range-v3-config.cmake

  Add the installation prefix of "range-v3" to CMAKE_PREFIX_PATH or set
  "range-v3_DIR" to a directory containing one of the above files.  If
  "range-v3" provides a separate development package or SDK, be sure it has
  been installed.
Call Stack (most recent call first):
  CMakeLists.txt:32 (find_package)

-- Configuring incomplete, errors occurred!
See also "C:/Users/Massiveatoms/Desktop/compsci/cmtest/cmake-build-debug/CMakeFiles/CMakeOutput.log".

[Failed to reload]

I have no idea what other information you need, so if you need further information, feel free to ask

friendlyanon commented 2 years ago

Based on the output in your result section it appears that you did not add range-v3 to the manifest file as a dependency. Can you confirm?

friendlyanon commented 2 years ago

Also, I recommend using the dev preset instead of manual setup. See the CLion examples in the README.

TinyAtoms commented 2 years ago

Oh yeah, completely forgot to talk about that. Adding range-v3 in vcpkg.json makes it fail differently.

  "dependencies": [
    {
      "name": "fmt",
      "version>=": "8.1.1#1"
    },
    "range-v3"
  ],

in the default debug target/profile from clion

It passes the configuration stage:

"C:\Program Files\JetBrains\CLion 2021.2.3\bin\cmake\win\bin\cmake.exe" -DCMAKE_BUILD_TYPE=Debug -DCMAKE_TOOLCHAIN_FILE=C:/tools/vcpkg/scripts/buildsystems/vcpkg.cmake -DCMAKE_DEPENDS_USE_COMPILER=FALSE -G "CodeBlocks - NMake Makefiles" C:\Users\Massiveatoms\Desktop\compsci\cmtest
-- Running vcpkg install
waiting to take filesystem lock on C:\tools\vcpkg\.vcpkg-root...
Detecting compiler hash for triplet x64-windows...
Detecting compiler hash for triplet x86-windows...
The following packages will be built and installed:
    range-v3[core]:x86-windows -> 2021-11-02 -- C:\tools\vcpkg\buildtrees\versioning_\versions\range-v3\78edaab693f001b38b155c3ef35a85036fd938ae
Restored 1 package(s) from C:\Users\Massiveatoms\AppData\Local\vcpkg\archives in 787.5 ms. Use --debug to see more details.
Installing 1/1 range-v3:x86-windows...
Elapsed time to handle range-v3:x86-windows: 1.638 s

Total elapsed time: 33.52 s

The package fmt provides CMake targets:

    find_package(fmt CONFIG REQUIRED)
    target_link_libraries(main PRIVATE fmt::fmt)

    # Or use the header-only version
    find_package(fmt CONFIG REQUIRED)
    target_link_libraries(main PRIVATE fmt::fmt-header-only)

range-v3 provides CMake targets:
    # this is heuristically generated, and may not be correct
    find_package(range-v3 CONFIG REQUIRED)
    # note: 2 additional targets are not displayed.
    target_link_libraries(main PRIVATE range-v3 range-v3-meta range-v3::meta range-v3-concepts)

-- Running vcpkg install - done
-- The CXX compiler identification is MSVC 19.29.30146.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.29.30133/bin/Hostx86/x86/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/Massiveatoms/Desktop/compsci/cmtest/cmake-build-debug

[Finished]

But it does not build.

"C:\Program Files\JetBrains\CLion 2021.2.3\bin\cmake\win\bin\cmake.exe" --build C:\Users\Massiveatoms\Desktop\compsci\cmtest\cmake-build-debug --target cmtest_exe
Scanning dependencies of target cmtest_lib
[ 33%] Building CXX object CMakeFiles/cmtest_lib.dir/source/lib.cpp.obj
lib.cpp
[ 33%] Built target cmtest_lib
Scanning dependencies of target cmtest_exe
[ 66%] Building CXX object CMakeFiles/cmtest_exe.dir/source/main.cpp.obj
main.cpp
C:\Users\Massiveatoms\Desktop\compsci\cmtest\source\main.cpp(5): fatal error C1083: Cannot open include file: 'range/v3/all.hpp': No such file or directory
NMAKE : fatal error U1077: 'C:\PROGRA~2\MICROS~2\2019\COMMUN~1\VC\Tools\MSVC\1429~1.301\bin\Hostx86\x86\cl.exe' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\bin\HostX86\x86\nmake.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\bin\HostX86\x86\nmake.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\bin\HostX86\x86\nmake.exe"' : return code '0x2'
Stop.

Generated dev profile from cmake-init

C:\Program Files\JetBrains\CLion 2021.2.3\bin\cmake\win\bin\cmake.exe" -G "Visual Studio 16 2019" -DCMAKE_TOOLCHAIN_FILE=C:\tools\vcpkg/scripts/buildsystems/vcpkg.cmake -DBUILD_MCSS_DOCS=ON -DCMAKE_CXX_EXTENSIONS=OFF -DCMAKE_CXX_STANDARD=17 -DVCPKG_MANIFEST_FEATURES=test "-DCMAKE_CXX_FLAGS=/utf-8 /W4 /permissive- /volatile:iso /Zc:preprocessor /EHsc /Zc:__cplusplus /Zc:externConstexpr /Zc:throwingNew" -DCMAKE_CXX_STANDARD_REQUIRED=ON -DVCPKG_TARGET_TRIPLET=x64-windows-static-md -Dcmtest_DEVELOPER_MODE=ON C:\Users\Massiveatoms\Desktop\compsci\cmtest
-- Running vcpkg install
Detecting compiler hash for triplet x64-windows...
Detecting compiler hash for triplet x64-windows-static-md...
The following packages will be built and installed:
    range-v3[core]:x64-windows-static-md -> 2021-11-02 -- C:\tools\vcpkg\buildtrees\versioning_\versions\range-v3\78edaab693f001b38b155c3ef35a85036fd938ae
Restored 1 package(s) from C:\Users\Massiveatoms\AppData\Local\vcpkg\archives in 1.854 s. Use --debug to see more details.
Installing 1/1 range-v3:x64-windows-static-md...
Elapsed time to handle range-v3:x64-windows-static-md: 2.615 s

Total elapsed time: 16.93 s

The package fmt provides CMake targets:

    find_package(fmt CONFIG REQUIRED)
    target_link_libraries(main PRIVATE fmt::fmt)

    # Or use the header-only version
    find_package(fmt CONFIG REQUIRED)
    target_link_libraries(main PRIVATE fmt::fmt-header-only)

range-v3 provides CMake targets:
    # this is heuristically generated, and may not be correct
    find_package(range-v3 CONFIG REQUIRED)
    # note: 2 additional targets are not displayed.
    target_link_libraries(main PRIVATE range-v3 range-v3-meta range-v3::meta range-v3-concepts)

catch2 provides CMake targets:
    # this is heuristically generated, and may not be correct
    find_package(Catch2 CONFIG REQUIRED)
    target_link_libraries(main PRIVATE Catch2::Catch2 Catch2::Catch2WithMain)

-- Running vcpkg install - done
-- Selecting Windows SDK version 10.0.19041.0 to target Windows 10.0.19044.
-- The CXX compiler identification is MSVC 19.29.30146.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Error at C:/Program Files/JetBrains/CLion 2021.2.3/bin/cmake/win/share/cmake-3.20/Modules/FindPackageHandleStandardArgs.cmake:230 (message):
  Could NOT find Python3 (missing: Python3_EXECUTABLE Interpreter) (Required
  is at least version "3.6")

      Reason given by package: 
          Interpreter: Cannot use the interpreter "C:/Users/Massiveatoms/.pyenv/pyenv-win/shims/python3"

Call Stack (most recent call first):
  C:/Program Files/JetBrains/CLion 2021.2.3/bin/cmake/win/share/cmake-3.20/Modules/FindPackageHandleStandardArgs.cmake:594 (_FPHSA_FAILURE_MESSAGE)
  C:/Program Files/JetBrains/CLion 2021.2.3/bin/cmake/win/share/cmake-3.20/Modules/FindPython/Support.cmake:3165 (find_package_handle_standard_args)
  C:/Program Files/JetBrains/CLion 2021.2.3/bin/cmake/win/share/cmake-3.20/Modules/FindPython3.cmake:485 (include)
  C:/tools/vcpkg/scripts/buildsystems/vcpkg.cmake:824 (_find_package)
  cmake/docs.cmake:19 (find_package)
  cmake/dev-mode.cmake:17 (include)
  CMakeLists.txt:67 (include)

-- Configuring incomplete, errors occurred!
See also "C:/Users/Massiveatoms/Desktop/compsci/cmtest/build/dev/CMakeFiles/CMakeOutput.log".

[Failed to reload]
friendlyanon commented 2 years ago

range-v3 (wrongly) exports the target range-v3 (among a few others) which should really be range-v3::range-v3. Their documentation is lacking in this regard, you may open an issue there.

In your user preset please specify the location of the Python instance on your own machine that you wish to use by setting the Python3_ROOT_DIR cache variable.

TinyAtoms commented 2 years ago

Adding the python3 root dir to CMakeUserPresets.json["configurePresets"]["cacheVariables"]["Python3_ROOT_DIR"] = "C:/Users/Massiveatoms/.pyenv/pyenv-win/versions/3.10.2" makes the configuration stage pass. (I pointed it to one of the pyenv versions instead of the shim)

"C:\Program Files\JetBrains\CLion 2021.2.3\bin\cmake\win\bin\cmake.exe" -G "Visual Studio 16 2019" -DCMAKE_TOOLCHAIN_FILE=C:\tools\vcpkg/scripts/buildsystems/vcpkg.cmake -DBUILD_MCSS_DOCS=ON -DCMAKE_CXX_EXTENSIONS=OFF -DPython3_ROOT_DIR=C:/Users/Massiveatoms/.pyenv/pyenv-win/versions/3.10.2 -DCMAKE_CXX_STANDARD=17 -DVCPKG_MANIFEST_FEATURES=test "-DCMAKE_CXX_FLAGS=/utf-8 /W4 /permissive- /volatile:iso /Zc:preprocessor /EHsc /Zc:__cplusplus /Zc:externConstexpr /Zc:throwingNew" -DCMAKE_CXX_STANDARD_REQUIRED=ON -DVCPKG_TARGET_TRIPLET=x64-windows-static-md -Dcmtest_DEVELOPER_MODE=ON C:\Users\Massiveatoms\Desktop\compsci\cmtest
-- Running vcpkg install
waiting to take filesystem lock on C:\tools\vcpkg\.vcpkg-root...
Detecting compiler hash for triplet x64-windows...
Detecting compiler hash for triplet x64-windows-static-md...
All requested packages are currently installed.
Restored 0 package(s) from C:\Users\Massiveatoms\AppData\Local\vcpkg\archives in 322.4 us. Use --debug to see more details.

Total elapsed time: 25.43 s

The package fmt provides CMake targets:

    find_package(fmt CONFIG REQUIRED)
    target_link_libraries(main PRIVATE fmt::fmt)

    # Or use the header-only version
    find_package(fmt CONFIG REQUIRED)
    target_link_libraries(main PRIVATE fmt::fmt-header-only)

range-v3 provides CMake targets:
    # this is heuristically generated, and may not be correct
    find_package(range-v3 CONFIG REQUIRED)
    # note: 2 additional targets are not displayed.
    target_link_libraries(main PRIVATE range-v3 range-v3-meta range-v3::meta range-v3-concepts)

catch2 provides CMake targets:
    # this is heuristically generated, and may not be correct
    find_package(Catch2 CONFIG REQUIRED)
    target_link_libraries(main PRIVATE Catch2::Catch2 Catch2::Catch2WithMain)

-- Running vcpkg install - done
-- Selecting Windows SDK version 10.0.19041.0 to target Windows 10.0.19044.
-- The CXX compiler identification is MSVC 19.29.30146.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found Python3: C:/Users/Massiveatoms/.pyenv/pyenv-win/versions/3.10.2/python3.exe (found suitable version "3.10.2", minimum required is "3.6") found components: Interpreter 
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/Massiveatoms/Desktop/compsci/cmtest/build/dev

[Finished]

Building still fails:

====================[ Build | cmtest_exe | dev ]================================
"C:\Program Files\JetBrains\CLion 2021.2.3\bin\cmake\win\bin\cmake.exe" --build C:\Users\Massiveatoms\Desktop\compsci\cmtest\build\dev --target cmtest_exe --config Debug -j 4
Microsoft (R) Build Engine version 16.11.2+f32259642 for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved.

  Checking Build System
  Building Custom Rule C:/Users/Massiveatoms/Desktop/compsci/cmtest/CMakeLists.txt
  cmtest_lib.vcxproj -> C:\Users\Massiveatoms\Desktop\compsci\cmtest\build\dev\cmtest_lib.dir\Debug\cmtest_lib.lib
  Building Custom Rule C:/Users/Massiveatoms/Desktop/compsci/cmtest/CMakeLists.txt
  main.cpp
C:\Users\Massiveatoms\Desktop\compsci\cmtest\source\main.cpp(5,1): fatal error C1083: Cannot open include file: 'range/v3/all.hpp': No such file or directory [C:\Users\Massiveatoms\Desktop\compsci\cmtest\build\dev\cmtest_exe.vcxproj]

I also tried setting targett_link_libraries to target_link_libraries(cmtest_lib PRIVATE fmt::fmt range-v3 range-v3-meta range-v3::meta range-v3-concepts range-v3::range-v3) with the same results. Although, I did not really expect that to fix anything, since the sample project I used to check if vcpkg worked (based on vcpkg documentation) uses range-v3::range-v3 and builds just fine.

friendlyanon commented 2 years ago

Can't reproduce. Creating an executable project and adding range-v3:

diff --git a/CMakeLists.txt b/CMakeLists.txt
index cd7e422..b22ad77 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -29,7 +29,8 @@ target_include_directories(
 target_compile_features(proj_lib PUBLIC cxx_std_17)

 find_package(fmt REQUIRED)
-target_link_libraries(proj_lib PRIVATE fmt::fmt)
+find_package(range-v3 REQUIRED)
+target_link_libraries(proj_lib PRIVATE fmt::fmt range-v3)

 # ---- Declare executable ----

diff --git a/source/lib.cpp b/source/lib.cpp
index 965519c..0ec0b2c 100644
--- a/source/lib.cpp
+++ b/source/lib.cpp
@@ -1,6 +1,7 @@
 #include "lib.hpp"

 #include <fmt/core.h>
+#include <range/v3/all.hpp>

 library::library()
     : name {fmt::format("{}", "proj")}
diff --git a/vcpkg.json b/vcpkg.json
index d780b82..526cec5 100644
--- a/vcpkg.json
+++ b/vcpkg.json
@@ -5,7 +5,8 @@
     {
       "name": "fmt",
       "version>=": "8.1.1#1"
-    }
+    },
+       "range-v3"
   ],
   "default-features": [],
   "features": {

Works for me when built as instructed by cmake-init:

cmake --preset=dev
cmake --build --preset=dev
TinyAtoms commented 2 years ago

I have found the issue: A combination of misreading the error and misconfiguring the cmake file 💀 the error was C:\Users\Massiveatoms\Desktop\compsci\cmtest\source\main.cpp(5,1): fatal error C1083: Cannot open include file: 'range/v3/all.hpp': No such file or directory [C:\Users\Massiveatoms\Desktop\compsci\cmtest\build\dev\cmtest_exe.vcxproj]
The cmake file had

find_package(range-v3 REQUIRED)
target_link_libraries(cmtest_lib PRIVATE fmt::fmt range-v3 range-v3-meta range-v3::meta range-v3-concepts range-v3::range-v3)

It couldn't find the header because I i tried linking ranges-v3 to cmtest_lib, which does not include the main.cpp file. Changing the cmake file to this works:

target_link_libraries(cmtest_exe PRIVATE range-v3 range-v3-meta range-v3::meta range-v3-concepts range-v3::range-v3)

Thanks for your patience and sorry for wasting your time.

friendlyanon commented 2 years ago

You only need to link to range-v3 for regular use.

Glad that I could help!