Skip to content

VS: Nuget restore packages for msbuild projects

Hello.

The setup

  • I have a project that uses the VS_PACKAGE_REFERENCES feature to reference nuget packages.
  • The project is attached. CmakeNugetTestProject.zip
  • The project is a csharp project that references a nuget package that contains additional build steps (Esri.ArcGISRuntime_100.7.0).
  • The nuget packages are restored using a custom command.

Restoring of nuget packages can be done using msbuild -t:restore <solution>.sln. However, adding it as a custom command, it will be executed inside another msbuild instance, the instance that is currently building the project. Thus, I beleive that this already running instance won't see the restored packages and their extra build steps.

Example of custom command:

add_custom_command(PRE_BUILD OUTPUT ${CMAKE_BINARY_DIR}/restore_has_been_run
	COMMAND ${CMAKE_MAKE_PROGRAM} TestProject.sln -t:restore WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
	COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_BINARY_DIR}/restore_has_been_run)

The executable depends on the restore_has_been_run file:

add_executable(Test ${SRC} ${CMAKE_BINARY_DIR}/restore_has_been_run)

The Problem

The problem is that I currently have to execute the build twice, before actually having built the project correctly.

  • Building once does not create the build/Release/arcgisruntime100.7 folder. 👎

  • Building TWICE does create the build/Release/arcgisruntime100.7 folder. 👍

  • Generating project and executing msbuild -t:restore TestProject.sln followed by a cmake --build . --config Release does create the build/Release/arcgisruntime100.7 folder. 👍

  • Opening solution file in Visual Studio and building does create the build/Release/arcgisruntime100.7 folder. 👍

Builds

Building once:

mkdir build
cd build
cmake ..
cmake --build . --config Release

Building twice:

mkdir build
cd build
cmake ..
cmake --build . --config Release
cmake --build . --config Release

Building with msbuild -t:restore first:

mkdir build
cd build
cmake ..
C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/MSBuild/Current/Bin/MSBuild.exe -t:restore TestProject.sln
cmake --build . --config Release

Building with Visual Studio

mkdir build
cd build
cmake ..
<open visual studio and build the project in Release mode>

Wanted

What I would really like is to run msbuild -t:restore <solution>.sln before starting the cmake build. However, using the add_custom_command won't work, as this is executed INSIDE an msbuild instance. Furthermore, I cannot execute a command after cmake generation, as that does not seem supported.

Proposed solution

A variable CMAKE_RESTORE_NUGET_PACKAGES variable could be introduced, and if set to YES it would run msbuild -t:restore <solution>.sln after cmake generation, possibly detecting if any changes were made to any VS_PACKAGE_REFERENCES.

Question

Is there any way to execute a custom command OUTSIDE of msbuild.exe? Either just after generation or just before a build?

Edited by Brad King
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information