Ben
Ben

Reputation: 873

A NuGet-aware find_package for CMake

I'm building a cross-platform library with CMake which has a few (pretty common) dependencies (e.g. PCRE). The dependencies are available through the usual package managers (APT on Ubuntu/Debian, Homebrew on OSX), and also through NuGet on Windows. In my CMakeLists.txt, I use the "module" version of find_package to locate these dependencies and set the right include/library flags.

This question provides one way of integrating CMake + NuGet, but also suggests that CMake and NuGet aren't likely to play nice together, and I can't seem to find a way to get find_package to find the installed dependencies. Is there some way to get CMake to read the NuGet config files (ala the way pkg_check_modules works on systems with pkg-config) and populate the appropriate CMake variables from there? Or do I have to hand-roll my own solution in FindPCRE.cmake?

Upvotes: 12

Views: 5013

Answers (2)

Kevin
Kevin

Reputation: 18323

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.

Upvotes: 3

Ben
Ben

Reputation: 873

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()

Upvotes: 7

Related Questions