Skip to content

Commit e68ce8a

Browse files
committed
build: add nuget_install() in cmake/NuGet.cmake
This function downloads nuget.exe to out_dir/nuget if necessary and installs the passed-in NuGet package to that directory. If the package contains a tools/bin directory, it is added to CMAKE_PROGRAM_PATH for find_program() to find any utilities. Add the supporting function file_download() to cmake/Utilities.cmake, which is a wrapper for the cmake file(DOWNLOAD ...) that takes a REQUIRED parameter to throw a fatal error if there are any problems. Signed-off-by: Rafael Kitover <[email protected]>
1 parent b4238e4 commit e68ce8a

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed
+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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:

contrib/buildsystems/cmake/Utilities.cmake

+40
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,43 @@
1+
#
2+
# file_download(
3+
# url dest_path
4+
# [STATUS status_out_var]
5+
# [REQUIRED]
6+
# )
7+
#
8+
# Wrapper for cmake file(DOWNLOAD ...) with optional error
9+
# checking. If REQUIRED is passed in, a fatal error will be thrown
10+
# on failure. Otherwise if STATUS is passed in, it will store the
11+
# status list in the out var just like file(DOWNLOAD ...).
12+
#
13+
function(file_download url dest_path)
14+
if(NOT url OR NOT dest_path)
15+
message(FATAL_ERROR "file_download: syntax: file_download(url destination_path [OPTIONS]")
16+
endif()
17+
18+
file(DOWNLOAD "${url}" "${dest_path}" STATUS status_list)
19+
list(GET status_list 0 status)
20+
21+
if("STATUS" IN_LIST ARGN)
22+
list(FIND ARGN STATUS status_idx)
23+
math(EXPR out_var_idx "${status_idx} + 1")
24+
list(GET ARGN "${out_var_idx}" out_var)
25+
26+
if("${out_var}" STREQUAL "")
27+
message(FATAL_ERROR "STATUS must be followed by output variable name for the status list.")
28+
endif()
29+
30+
set("${out_var}" "${status_list}" PARENT_SCOPE)
31+
endif()
32+
33+
if ("REQUIRED" IN_LIST ARGN)
34+
if(NOT status EQUAL 0)
35+
list(GET status_list 1 error_str)
36+
message(FATAL_ERROR "Download of '${url}' failed: ${error_str}")
37+
endif()
38+
endif()
39+
endfunction()
40+
141
#
242
# build_option(
343
# opt_name type help_string [default]

0 commit comments

Comments
 (0)