microsoft / vcpkg

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

vcpkg toolchain file yields error for CMake .NET SDK targets #38511

Open gtsc opened 2 weeks ago

gtsc commented 2 weeks ago

Describe the bug I would like to use vcpkg in a CMake project that also includes .NET targets (e.g. relevant when having one project that has both C++, C++/CLI and .NET components, all built with CMake). When providing /path/to/vcpkg/scripts/buildsystems/vcpkg.cmake as CMAKE_TOOLCHAIN_FILE cache variable, I get the following error for every .NET target in the project:

CMake Error in CMakeLists.txt:
  The target "TARGET_NAME" does not currently support add_custom_command as
  the Visual Studio generators have not yet learned how to generate custom
  commands in .Net SDK-style projects.

This is unfortunate, as it prevents me using vcpkg in such project (or forces me to split up the project in some way).

Environment

To Reproduce For easy reproduction, do the following:

  1. Create a project with an empty vcpkg.json file and an empty Program.cs file at the root directory. In the CMakeLists.txt add the following lines:
cmake_minimum_required(VERSION 3.24)
project(TestProject LANGUAGES CSharp)
set(CMAKE_DOTNET_SDK "Microsoft.NET.Sdk")
set(CMAKE_DOTNET_TARGET_FRAMEWORK "net8.0")
add_executable(HelloWorld "Program.cs")
  1. Run the following command: cmake -S /path/to/repo -B /path/to/build/dir -G "Visual Studio 17 2022" -DCMAKE_TOOLCHAIN_FILE="/path/to/vcpkg/scripts/buildsystems/vcpkg.cmake"

This will yield the error message shown above (with "HelloWorld" as TARGET_NAME).

Expected behavior I would expect the inclusion of the vcpkg toolchain file to not influence .NET targets.

Failure logs See attached file for logs obtained by running the CMake command shown above with --trace. cmake-trace.txt

Skimming through the logs makes me assume that /path/to/vcpkg/scripts/buildsystems/vcpkg.cmake(612) might be the problematic add_custom_command call (however, I don't 100% understand what is happening in here). Ideally, we should not do the add_custom_command for .NET targets to resolve this issue.

Osyotr commented 2 weeks ago

Try setting VCPKG_APPLOCAL_DEPS=OFF. These custom commands are used to automatically copy DLLs to the output folder.