|
| 1 | +include(Utilities) |
| 2 | + |
| 3 | +# |
| 4 | +# nuget_install([pkg_name] [QUIET]) |
| 5 | +# |
| 6 | +# Installs nuget.exe in the output directory if not already present |
| 7 | +# and uses it to install pkg_name if provided. |
| 8 | +# |
| 9 | +# If the package contains a 'tools/bin' directory, it will be |
| 10 | +# appended to CMAKE_PROGRAM_PATH. |
| 11 | +# |
| 12 | +# With the QUIET option, no status messages will be printed. |
| 13 | +# |
| 14 | +# Throws a fatal error if any problems are encountered. |
| 15 | +# |
| 16 | +function(nuget_install pkg) |
| 17 | + if(ARGV1 STREQUAL QUIET) |
| 18 | + set(quiet TRUE) |
| 19 | + endif() |
| 20 | + |
| 21 | + if(NOT EXISTS "${CMAKE_BINARY_DIR}/nuget/nuget.exe") |
| 22 | + file_download("https://dist.nuget.org/win-x86-commandline/latest/nuget.exe" "${CMAKE_BINARY_DIR}/nuget/nuget.exe" REQUIRED) |
| 23 | + |
| 24 | + # Add nuget package source if not already present. |
| 25 | + execute_process( |
| 26 | + COMMAND nuget sources add -Name "NuGet official package source" -Source "https://api.nuget.org/v3/index.json" |
| 27 | + WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/nuget" |
| 28 | + OUTPUT_QUIET |
| 29 | + ERROR_QUIET |
| 30 | + ) |
| 31 | + endif() |
| 32 | + |
| 33 | + if("${pkg}" STREQUAL "") |
| 34 | + return() |
| 35 | + endif() |
| 36 | + |
| 37 | + execute_process( |
| 38 | + COMMAND nuget.exe install "${pkg}" -OutputDirectory "${CMAKE_BINARY_DIR}/nuget" |
| 39 | + WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/nuget" |
| 40 | + OUTPUT_VARIABLE install_output |
| 41 | + RESULT_VARIABLE install_exit_status |
| 42 | + ) |
| 43 | + |
| 44 | + if(NOT install_exit_status EQUAL 0) |
| 45 | + message(FATAL_ERROR "NuGet installation of package '${pkg}' failed, exit code: ${install_exit_status}") |
| 46 | + endif() |
| 47 | + |
| 48 | + string(REGEX REPLACE ".* package '[^0-9]+([0-9.]+)'.*" "\\1" pkg_ver "${install_output}") |
| 49 | + |
| 50 | + set(pkg_dir "${CMAKE_BINARY_DIR}/nuget/${pkg}.${pkg_ver}") |
| 51 | + |
| 52 | + if(NOT IS_DIRECTORY "${pkg_dir}") |
| 53 | + message(FATAL_ERROR "NuGet installation of package '${pkg}' failed, package directory '${pkg_dir}' does not exist.") |
| 54 | + endif() |
| 55 | + |
| 56 | + if(NOT quiet) |
| 57 | + message(STATUS "Installed NuGet package '${pkg}' successfully.") |
| 58 | + endif() |
| 59 | + |
| 60 | + set(pkg_bin "${pkg_dir}/tools/bin") |
| 61 | + |
| 62 | + if(IS_DIRECTORY "${pkg_bin}") |
| 63 | + list(APPEND CMAKE_PROGRAM_PATH "${pkg_bin}") |
| 64 | + set(CMAKE_PROGRAM_PATH "${CMAKE_PROGRAM_PATH}" PARENT_SCOPE) |
| 65 | + set(CMAKE_PROGRAM_PATH "${CMAKE_PROGRAM_PATH}" CACHE STRING "External Program Search Path" FORCE) |
| 66 | + |
| 67 | + if(NOT quiet) |
| 68 | + message(STATUS "Added tools/bin from NuGet package '${pkg}' to CMAKE_PROGRAM_PATH.") |
| 69 | + endif() |
| 70 | + endif() |
| 71 | +endfunction() |
| 72 | + |
| 73 | +# vim:set sw=8 ts=8 noet: |
0 commit comments