Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CMake-based projects fail to build using Visual Studio 2019 with toolset v141 #7318

Closed
ceranco opened this issue Jul 18, 2019 · 16 comments
Closed
Assignees
Labels
category:vcpkg-bug The issue is with the vcpkg system (including helper scripts in `scripts/cmake/`)

Comments

@ceranco
Copy link

ceranco commented Jul 18, 2019

Originally this issue was the following: #6475, but because this affects many packages I thought it was better to open a new issue.

Environment

Windows 10
Visual Studio 2019 with v141 toolset installed.
Vcpkg version: 2019.06.26-nohash

The Problem

When trying to build osg with the following triplet:

set(VCPKG_TARGET_ARCHITECTURE x64)
set(VCPKG_CRT_LINKAGE dynamic)
set(VCPKG_LIBRARY_LINKAGE dynamic)
set(VCPKG_PLATFORM_TOOLSET v141)

many of its dependencies (and itself) all fail with the same error:

[1/2] cmd /c "cd .. && "C:/Program Files/CMake/bin/cmake.exe" "C:/vcpkg/buildtrees/szip/src/szip-2.1.1-6b767acfa7" "-DSZIP_INSTALL_DATA_DIR=share/szip/data" "-DSZIP_INSTALL_CMAKE_DIR=share/szip" "-DBUILD_SHARED_LIBS=ON" "-DVCPKG_CHAINLOAD_TOOLCHAIN_FILE=C:/vcpkg/scripts/toolchains/windows.cmake" "-DVCPKG_TARGET_TRIPLET=x64-windows-v141" "-DVCPKG_SET_CHARSET_FLAG=ON" "-DVCPKG_PLATFORM_TOOLSET=v141" "-DCMAKE_EXPORT_NO_PACKAGE_REGISTRY=ON" "-DCMAKE_FIND_PACKAGE_NO_PACKAGE_REGISTRY=ON" "-DCMAKE_FIND_PACKAGE_NO_SYSTEM_PACKAGE_REGISTRY=ON" "-DCMAKE_INSTALL_SYSTEM_RUNTIME_LIBS_SKIP=TRUE" "-DCMAKE_VERBOSE_MAKEFILE=ON" "-DVCPKG_APPLOCAL_DEPS=OFF" "-DCMAKE_TOOLCHAIN_FILE=C:/vcpkg/scripts/buildsystems/vcpkg.cmake" "-DCMAKE_ERROR_ON_ABSOLUTE_INSTALL_DESTINATION=ON" "-DVCPKG_CXX_FLAGS=" "-DVCPKG_CXX_FLAGS_RELEASE=" "-DVCPKG_CXX_FLAGS_DEBUG=" "-DVCPKG_C_FLAGS=" "-DVCPKG_C_FLAGS_RELEASE=" "-DVCPKG_C_FLAGS_DEBUG=" "-DVCPKG_CRT_LINKAGE=dynamic" "-DVCPKG_LINKER_FLAGS=" "-DVCPKG_TARGET_ARCHITECTURE=x64" "-DCMAKE_INSTALL_LIBDIR:STRING=lib" "-DCMAKE_INSTALL_BINDIR:STRING=bin" "-G" "Visual Studio 15 2017 Win64" "-DCMAKE_BUILD_TYPE=Release" "-DCMAKE_INSTALL_PREFIX=C:/vcpkg/packages/szip_x64-windows-v141""
FAILED: ../CMakeCache.txt 
cmd /c "cd .. && "C:/Program Files/CMake/bin/cmake.exe" "C:/vcpkg/buildtrees/szip/src/szip-2.1.1-6b767acfa7" "-DSZIP_INSTALL_DATA_DIR=share/szip/data" "-DSZIP_INSTALL_CMAKE_DIR=share/szip" "-DBUILD_SHARED_LIBS=ON" "-DVCPKG_CHAINLOAD_TOOLCHAIN_FILE=C:/vcpkg/scripts/toolchains/windows.cmake" "-DVCPKG_TARGET_TRIPLET=x64-windows-v141" "-DVCPKG_SET_CHARSET_FLAG=ON" "-DVCPKG_PLATFORM_TOOLSET=v141" "-DCMAKE_EXPORT_NO_PACKAGE_REGISTRY=ON" "-DCMAKE_FIND_PACKAGE_NO_PACKAGE_REGISTRY=ON" "-DCMAKE_FIND_PACKAGE_NO_SYSTEM_PACKAGE_REGISTRY=ON" "-DCMAKE_INSTALL_SYSTEM_RUNTIME_LIBS_SKIP=TRUE" "-DCMAKE_VERBOSE_MAKEFILE=ON" "-DVCPKG_APPLOCAL_DEPS=OFF" "-DCMAKE_TOOLCHAIN_FILE=C:/vcpkg/scripts/buildsystems/vcpkg.cmake" "-DCMAKE_ERROR_ON_ABSOLUTE_INSTALL_DESTINATION=ON" "-DVCPKG_CXX_FLAGS=" "-DVCPKG_CXX_FLAGS_RELEASE=" "-DVCPKG_CXX_FLAGS_DEBUG=" "-DVCPKG_C_FLAGS=" "-DVCPKG_C_FLAGS_RELEASE=" "-DVCPKG_C_FLAGS_DEBUG=" "-DVCPKG_CRT_LINKAGE=dynamic" "-DVCPKG_LINKER_FLAGS=" "-DVCPKG_TARGET_ARCHITECTURE=x64" "-DCMAKE_INSTALL_LIBDIR:STRING=lib" "-DCMAKE_INSTALL_BINDIR:STRING=bin" "-G" "Visual Studio 15 2017 Win64" "-DCMAKE_BUILD_TYPE=Release" "-DCMAKE_INSTALL_PREFIX=C:/vcpkg/packages/szip_x64-windows-v141""
CMake Error at CMakeLists.txt:2 (PROJECT):
  Generator

    Visual Studio 15 2017 Win64

  could not find any instance of Visual Studio.



-- Configuring incomplete, errors occurred!
See also "C:/vcpkg/buildtrees/szip/x64-windows-v141-rel/CMakeFiles/CMakeOutput.log".
[2/2] cmd /c "cd ../../x64-windows-v141-dbg && "C:/Program Files/CMake/bin/cmake.exe" "C:/vcpkg/buildtrees/szip/src/szip-2.1.1-6b767acfa7" "-DSZIP_INSTALL_DATA_DIR=share/szip/data" "-DSZIP_INSTALL_CMAKE_DIR=share/szip" "-DBUILD_SHARED_LIBS=ON" "-DVCPKG_CHAINLOAD_TOOLCHAIN_FILE=C:/vcpkg/scripts/toolchains/windows.cmake" "-DVCPKG_TARGET_TRIPLET=x64-windows-v141" "-DVCPKG_SET_CHARSET_FLAG=ON" "-DVCPKG_PLATFORM_TOOLSET=v141" "-DCMAKE_EXPORT_NO_PACKAGE_REGISTRY=ON" "-DCMAKE_FIND_PACKAGE_NO_PACKAGE_REGISTRY=ON" "-DCMAKE_FIND_PACKAGE_NO_SYSTEM_PACKAGE_REGISTRY=ON" "-DCMAKE_INSTALL_SYSTEM_RUNTIME_LIBS_SKIP=TRUE" "-DCMAKE_VERBOSE_MAKEFILE=ON" "-DVCPKG_APPLOCAL_DEPS=OFF" "-DCMAKE_TOOLCHAIN_FILE=C:/vcpkg/scripts/buildsystems/vcpkg.cmake" "-DCMAKE_ERROR_ON_ABSOLUTE_INSTALL_DESTINATION=ON" "-DVCPKG_CXX_FLAGS=" "-DVCPKG_CXX_FLAGS_RELEASE=" "-DVCPKG_CXX_FLAGS_DEBUG=" "-DVCPKG_C_FLAGS=" "-DVCPKG_C_FLAGS_RELEASE=" "-DVCPKG_C_FLAGS_DEBUG=" "-DVCPKG_CRT_LINKAGE=dynamic" "-DVCPKG_LINKER_FLAGS=" "-DVCPKG_TARGET_ARCHITECTURE=x64" "-DCMAKE_INSTALL_LIBDIR:STRING=lib" "-DCMAKE_INSTALL_BINDIR:STRING=bin" "-G" "Visual Studio 15 2017 Win64" "-DCMAKE_BUILD_TYPE=Debug" "-DCMAKE_INSTALL_PREFIX=C:/vcpkg/packages/szip_x64-windows-v141/debug""
FAILED: ../../x64-windows-v141-dbg/CMakeCache.txt 
cmd /c "cd ../../x64-windows-v141-dbg && "C:/Program Files/CMake/bin/cmake.exe" "C:/vcpkg/buildtrees/szip/src/szip-2.1.1-6b767acfa7" "-DSZIP_INSTALL_DATA_DIR=share/szip/data" "-DSZIP_INSTALL_CMAKE_DIR=share/szip" "-DBUILD_SHARED_LIBS=ON" "-DVCPKG_CHAINLOAD_TOOLCHAIN_FILE=C:/vcpkg/scripts/toolchains/windows.cmake" "-DVCPKG_TARGET_TRIPLET=x64-windows-v141" "-DVCPKG_SET_CHARSET_FLAG=ON" "-DVCPKG_PLATFORM_TOOLSET=v141" "-DCMAKE_EXPORT_NO_PACKAGE_REGISTRY=ON" "-DCMAKE_FIND_PACKAGE_NO_PACKAGE_REGISTRY=ON" "-DCMAKE_FIND_PACKAGE_NO_SYSTEM_PACKAGE_REGISTRY=ON" "-DCMAKE_INSTALL_SYSTEM_RUNTIME_LIBS_SKIP=TRUE" "-DCMAKE_VERBOSE_MAKEFILE=ON" "-DVCPKG_APPLOCAL_DEPS=OFF" "-DCMAKE_TOOLCHAIN_FILE=C:/vcpkg/scripts/buildsystems/vcpkg.cmake" "-DCMAKE_ERROR_ON_ABSOLUTE_INSTALL_DESTINATION=ON" "-DVCPKG_CXX_FLAGS=" "-DVCPKG_CXX_FLAGS_RELEASE=" "-DVCPKG_CXX_FLAGS_DEBUG=" "-DVCPKG_C_FLAGS=" "-DVCPKG_C_FLAGS_RELEASE=" "-DVCPKG_C_FLAGS_DEBUG=" "-DVCPKG_CRT_LINKAGE=dynamic" "-DVCPKG_LINKER_FLAGS=" "-DVCPKG_TARGET_ARCHITECTURE=x64" "-DCMAKE_INSTALL_LIBDIR:STRING=lib" "-DCMAKE_INSTALL_BINDIR:STRING=bin" "-G" "Visual Studio 15 2017 Win64" "-DCMAKE_BUILD_TYPE=Debug" "-DCMAKE_INSTALL_PREFIX=C:/vcpkg/packages/szip_x64-windows-v141/debug""
CMake Error at CMakeLists.txt:2 (PROJECT):
  Generator

    Visual Studio 15 2017 Win64

  could not find any instance of Visual Studio.



-- Configuring incomplete, errors occurred!
See also "C:/vcpkg/buildtrees/szip/x64-windows-v141-dbg/CMakeFiles/CMakeOutput.log".
ninja: build stopped: subcommand failed.

Basically CMake tries to use Visual Studio 2017 as its generator instead of Visual Studio 2019.

CMake-based projects that fail

  • proj4
  • freeglut
  • szip
  • collada-dom
  • osg
  • crashrpt (reported by @meastp)

CMake-based projects that succeed

  • bzip
  • curl
  • expat
  • geos
  • giflib
  • hdf5
  • jasper
  • libiconv
  • libjpeg-turbo
  • liblzma
  • libpng
  • libpq
  • libwebp
  • libxml2
  • minizip
  • netcdf-c
  • openexr
  • openjpeg
  • pcre
  • sqlite3
  • tiff
  • uriparser
  • zlib

Possible Reason

The difference between projects that do and don't is in the configure_cmake(...) call in their portfiles:
Projects that succeed use the PREFER_NINJA option:

vcpkg_configure_cmake(
    SOURCE_PATH ${SOURCE_PATH}
    PREFER_NINJA
    ...
)

Workaround

Manually adding the PREFER_NINJA option to the portfiles of the failing projects seems to fix the problem (or at least allows them too build).

@ceranco
Copy link
Author

ceranco commented Jul 18, 2019

Excuse me, I put the wrong vcpkg version in the issue:
Capture

I've updated the version written in the issue description to Vcpkg version: 2019.06.26-nohash accordingly.

@PhoebeHui
Copy link
Contributor

@ceranco, thanks for posting this!

From https://github.com/microsoft/vcpkg/blob/4498b6403e0ef1e441a3c4df36e45fda9b181237/docs/users/triplets.md#vcpkg_platform_toolset, I think you might need to install v142 toolset.

@ceranco
Copy link
Author

ceranco commented Jul 29, 2019

Hi @PhoebeHui,

Visual Studio 2019 comes installed with v142 by default, but you can optionally install and build with v141 for compatibility with Visual Studio 2017.
In the triplet I specifically ask for v141, which exists in my installation (as seen projects that use NINJA work).

From the docs:

  1. Determine the setting for VCPKG_PLATFORM_TOOLSET from the triplet or consider it unset
  2. Gather a list of all pairs of Visual Studio Instances with all toolsets available in those instances
    1. This is ordered first by instance type (Stable, Prerelease, Legacy) and then by toolset version (v142, v141, v140)
  3. Filter the list based on the settings for VCPKG_VISUAL_STUDIO_PATH and VCPKG_PLATFORM_TOOLSET.
  4. Select the best remaining option

From step 2: v141 is determined by the triplet.
From step 3: the only available pairs are (Visual Studio 2019, v141) and (Visual Studio 2019, v142).
From step 4 + 5: the (Visual Studio 2019, v141) pair should be chosen, but seems that in CMake projects it gets the (Visual Studio 2017, v141) pair, even though it doesn't exist.

@PhoebeHui
Copy link
Contributor

@ceranco, see https://github.com/microsoft/vcpkg/blob/master/scripts/cmake/vcpkg_configure_cmake.cmake#L116, it use toolsets to match cmake generator here, so it try to get visual studio 2017. not sure if pair ( Visual Studio 2019, v141) will support in future.

Doc says that VCPKG_PLATFORM_TOOLSET valid settings is:
The Visual Studio 2019 platform toolset is v142.
The Visual Studio 2017 platform toolset is v141.
The Visual Studio 2015 platform toolset is v140.

@ras0219-msft ras0219-msft added the category:vcpkg-bug The issue is with the vcpkg system (including helper scripts in `scripts/cmake/`) label Jul 29, 2019
@Rastaban
Copy link
Contributor

vcpkg uses vswhere to find the toolset. The output of vswhere also includes the version of visual studio. To fix this issue vcpkg should insert a new cmake variable similar to VCPKG_PLATFORM_TOOLSET (VCPKG_VS_VERSION maybe?) that can be used in this file to select the correct generator.

@Rastaban Rastaban assigned vicroms and unassigned Rastaban Oct 3, 2019
@meastp
Copy link
Contributor

meastp commented Sep 1, 2020

@ceranco Please add crashrpt to the "ports that fail" list.

@ceranco
Copy link
Author

ceranco commented Sep 1, 2020

@meastp I've added crashrpt to the list.

Since opening this issue I've stopped using C++ and vcpkg, so I'm not sure if the packages and problems I originally listed have been fixed or if anything changed.

@Rastaban If this problem is still being worked on, it might be better to close the current one and have someone that is actively using vcpkg open a new one, whatever you prefer.

@meastp
Copy link
Contributor

meastp commented Sep 1, 2020

@ceranco Thanks - note that crashrpt does not build with PREFER_NINJA due to build errors with MFC/ATL/UNICODE which I haven't investigated thoroughly.

@Cheney-W
Copy link
Contributor

This issue still occurs in latest vcpkg(2021-08-03-1a6d67b8150e94b7f473c6ab1c0cfd) with osg port.

@longhuan2018
Copy link
Contributor

My idea is to add a VCPKG_GENERATOR_TOOLSET to correctly generate the -G parameter of cmake.
vcpkg_configure_cmake.cmake

        if(NOT DEFINED VCPKG_GENERATOR_TOOLSET)
            set(VCPKG_GENERATOR_TOOLSET ${VCPKG_PLATFORM_TOOLSET})
        endif()

        if("${VCPKG_GENERATOR_TOOLSET}" STREQUAL "v120" AND NOT "${VCPKG_TARGET_ARCHITECTURE}" STREQUAL "arm64")
            set(generator "Visual Studio 12 2013")
        elseif("${VCPKG_GENERATOR_TOOLSET}" STREQUAL "v140" AND NOT "${VCPKG_TARGET_ARCHITECTURE}" STREQUAL "arm64")
            set(generator "Visual Studio 14 2015")
        elseif("${VCPKG_GENERATOR_TOOLSET}" STREQUAL "v141")
            set(generator "Visual Studio 15 2017")
        elseif("${VCPKG_GENERATOR_TOOLSET}" STREQUAL "v142")
            set(generator "Visual Studio 16 2019")
        elseif("${VCPKG_GENERATOR_TOOLSET}" STREQUAL "v143")
            set(generator "Visual Studio 17 2022")
        endif()

        if("${VCPKG_TARGET_ARCHITECTURE}" STREQUAL "x86")
            set(generator_arch "Win32")
        elseif("${VCPKG_TARGET_ARCHITECTURE}" STREQUAL "x64")
            set(generator_arch "x64")
        elseif("${VCPKG_TARGET_ARCHITECTURE}" STREQUAL "arm")
            set(generator_arch "ARM")
        elseif("${VCPKG_TARGET_ARCHITECTURE}" STREQUAL "arm64")
            set(generator_arch "ARM64")
        endif()

        set(generator_toolset "-DCMAKE_GENERATOR_TOOLSET=${VCPKG_PLATFORM_TOOLSET}")

Add VCPKG_GENERATOR_TOOLSET output in vcpkg-tool
vcpkgpaths.h

    struct Toolset
    {
        Path visual_studio_root_path;
        Path dumpbin;
        Path vcvarsall;
        std::vector<std::string> vcvarsall_options;
        CStringView version;
        std::vector<ToolsetArchOption> supported_architectures;
        CStringView vs_version;  /*Record the version of the current visual studio installer*/
    };

build.cpp

static void get_generic_cmake_build_args(const VcpkgPaths& paths,
                                             Triplet triplet,
                                             const Toolset& toolset,
                                             std::vector<CMakeVariable>& out_vars)
    {
        Util::Vectors::append(&out_vars,
                              std::initializer_list<CMakeVariable>{
                                  {"CMD", "BUILD"},
                                  {"DOWNLOADS", paths.downloads},
                                  {"TARGET_TRIPLET", triplet.canonical_name()},
                                  {"TARGET_TRIPLET_FILE", paths.get_triplet_file_path(triplet)},
                                  {"VCPKG_BASE_VERSION", Commands::Version::base_version()},
                                  {"VCPKG_CONCURRENCY", std::to_string(get_concurrency())},
                                  {"VCPKG_PLATFORM_TOOLSET", toolset.version.c_str()},
                                  {"VCPKG_GENERATOR_TOOLSET", toolset.vs_version.c_str()}, /*Record the version of the current visual studio installer*/
                              });

visualstudio.cpp

std::vector<Toolset> find_toolset_instances_preferred_first(const VcpkgPaths& paths)
{
        using CPU = CPUArchitecture;
        ....
                CStringView vs_version; /*Record the version of the current visual studio installer*/
                if (major_version == "15")
                    vs_version = V_141;
                else if (major_version == "16")
                    vs_version = V_142;
                else if (major_version == "17")
                    vs_version = V_143;

                for (const Path& subdir : msvc_subdirectories)
                {
                    auto toolset_version_full = subdir.filename();
                    auto toolset_version_prefix = toolset_version_full.substr(0, 4);
                    CStringView toolset_version;
                    std::string vcvars_option;
                    ....
                    if (fs.exists(dumpbin_path, IgnoreErrors{}))
                    {
                        Toolset toolset{vs_instance.root_path,
                                        dumpbin_path,
                                        vcvarsall_bat,
                                        {vcvars_option},
                                        toolset_version,
                                        supported_architectures,
                                        vs_version};

@longhuan2018
Copy link
Contributor

@ceranco You can try this PR #21342, Just add one more line to the x64-windows.cmake file

set(VCPKG_TARGET_ARCHITECTURE x64)
set(VCPKG_CRT_LINKAGE dynamic)
set(VCPKG_LIBRARY_LINKAGE dynamic)
set(VCPKG_PLATFORM_TOOLSET v141)
set(VCPKG_GENERATOR_TOOLSET v142)

@JackBoosY JackBoosY assigned JackBoosY and unassigned NancyLi1013 Jun 10, 2022
@JackBoosY
Copy link
Contributor

I can confirm this issue was fixed using the following command:

.\vcpkg.exe install proj4:x64-windows-v141  --host-triplet=x64-windows-v141

@longhuan2018
Copy link
Contributor

@JackBoosY I created a new x64-windows-v141.cmake file

set(VCPKG_TARGET_ARCHITECTURE x64)
set(VCPKG_CRT_LINKAGE dynamic)
set(VCPKG_LIBRARY_LINKAGE dynamic)
set(VCPKG_PLATFORM_TOOLSET v141)

It is correct to implement it directly.

vcpkg install basisu:x64-windows-v141

However, when I delete the PREFER_NINJA in the portfile.cmake file of basisu port, it reports an error

CMake Error at CMakeLists.txt:2 (project):
  Generator

    Visual Studio 15 2017

  could not find any instance of Visual Studio.

@JackBoosY
Copy link
Contributor

set(VCPKG_PLATFORM_TOOLSET v142)

@longhuan2018
Copy link
Contributor

But I just want to compile with toolset v141 when only vs2019 is installed

@JackBoosY
Copy link
Contributor

Can you please replace vcpkg_configure_cmake / vcpkg_install_cmake with vcpkg_cmake_configure / vcpkg_cmake_install, remove PREFER_NINJA and add the following code to its vcpkg.json then try again:

{
    ...
    "dependencies": [
    ...
    {
        "name": "vcpkg-cmake",
        "host": true
    }
    ]
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
category:vcpkg-bug The issue is with the vcpkg system (including helper scripts in `scripts/cmake/`)
Projects
None yet
Development

No branches or pull requests

10 participants