A NuGet-aware find_package for CMake
As a (somewhat filthy) workaround, I'm relying on the nuget
cli tool being present and using
find_program(NUGET nuget)if(NOT NUGET) message(FATAL "Cannot find nuget command line tool.\nInstall it with e.g. choco install nuget.commandline")else() execute_process(COMMAND ${NUGET} install foolib)endif()
As of CMake 3.15, CMake now supports referencing Nuget packages with VS_PACKAGE_REFERENCES
, without the need for the Nuget CLI, or hard-coding paths to references. To add a Nuget package reference to a CMake target, we can use the syntax <package-name>_<package-version>
. Here is a simple example for the Nuget logging package Serilog
version 2.9.0:
set_property(TARGET MyLibrary PROPERTY VS_PACKAGE_REFERENCES "Serilog_2.9.0")
The linked documentation shows how you can add multiple Nuget packages by semicolon-delimiting ;
the package arguments.