Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 238
Ben Avatar asked Aug 15 '15 01:08

Ben


People also ask

What does Find_package in CMake do?

CMake searches for a file called Find<package>. cmake in the CMAKE_MODULE_PATH followed by the CMake installation. If the file is found, it is read and processed by CMake. It is responsible for finding the package, checking the version, and producing any needed messages.

Where does Find_package look CMake?

CMake ships with its own set of built-in find_package scripts, and their location is in the default CMAKE_MODULE_PATH.


1 Answers

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.

like image 52
Kevin Avatar answered Sep 22 '22 06:09

Kevin