forked from MarkSchofield/WindowsToolchain
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNuGet.cmake
126 lines (105 loc) · 5.13 KB
/
NuGet.cmake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#----------------------------------------------------------------------------------------------------------------------
# MIT License
#
# Copyright (c) 2021 Mark Schofield
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#----------------------------------------------------------------------------------------------------------------------
include_guard()
include("${CMAKE_CURRENT_LIST_DIR}/ToolchainCommon.cmake")
find_program(NUGET_PATH
nuget nuget.exe
PATHS
${NUGET_PATH}
)
# If NuGet isn't found, download it.
#
# NuGet.exe will be downloaded to `TOOLCHAIN_TOOLS_PATH`. If `TOOLCHAIN_TOOLS_PATH` is not set then it will
# be downloaded to '${CMAKE_BINARY_DIR}/__tools'.
if(NUGET_PATH STREQUAL "NUGET_PATH-NOTFOUND")
if(NOT NUGET_VERSION)
set(NUGET_VERSION "6.0.0")
set(NUGET_HASH "SHA256=04EB6C4FE4213907E2773E1BE1BBBD730E9A655A3C9C58387CE8D4A714A5B9E1")
else()
if(NOT NUGET_HASH)
message(FATAL_ERROR "NUGET_VERSION is set to ${NUGET_VERSION}. NUGET_HASH must be set if NUGET_VERSION is set.")
endif()
endif()
if(NOT TOOLCHAIN_TOOLS_PATH)
set(TOOLCHAIN_TOOLS_PATH "${CMAKE_BINARY_DIR}/__tools")
endif()
set(NUGET_PATH "${TOOLCHAIN_TOOLS_PATH}/nuget.exe")
toolchain_download_file(
URL "https://dist.nuget.org/win-x86-commandline/v${NUGET_VERSION}/nuget.exe"
PATH ${NUGET_PATH}
EXPECTED_HASH ${NUGET_HASH}
)
endif()
#[[====================================================================================================================
install_nuget_package
---------------------
Downloads a NuGet package.
install_nuget_package(
<package name>
<package version>
<variable name>
[PRERELEASE <ON|OFF>]
[PACKAGESAVEMODE <nuspec|nupkg|nuspec;nupkg>]
)
The packages will be downloaded to `NUGET_PACKAGE_ROOT_PATH`. If `NUGET_PACKAGE_ROOT_PATH` is not set, then
packages will be downloaded to `${CMAKE_BINARY_DIR}/__nuget`.
Note: Since `CMAKE_BINARY_DIR` is platform specific, the default download location will change by platform,
resulting in NuGet packages being downloaded once for each platform that is built. Setting
`NUGET_PACKAGE_ROOT_PATH` to a platform-independent path (e.g. relative to the root of the repository) will
allow NuGet packages to be downloaded once for all platforms.
====================================================================================================================]]#
function(install_nuget_package NUGET_PACKAGE_NAME NUGET_PACKAGE_VERSION VARIABLE_NAME)
set(OPTIONS)
set(ONE_VALUE_KEYWORDS PRERELEASE)
set(MULTI_VALUE_KEYWORDS)
cmake_parse_arguments(PARSE_ARGV 0 NUGET "${OPTIONS}" "${ONE_VALUE_KEYWORDS}" "${MULTI_VALUE_KEYWORDS}")
if(NOT NUGET_PACKAGE_ROOT_PATH)
set(NUGET_PACKAGE_ROOT_PATH ${CMAKE_BINARY_DIR}/__nuget)
endif()
set(NUGET_PACKAGE_PATH "${NUGET_PACKAGE_ROOT_PATH}/${NUGET_PACKAGE_NAME}.${NUGET_PACKAGE_VERSION}")
if(NOT EXISTS "${NUGET_PACKAGE_PATH}")
set(NUGET_COMMAND ${NUGET_PATH} install ${NUGET_PACKAGE_NAME})
list(APPEND NUGET_COMMAND -OutputDirectory ${NUGET_PACKAGE_ROOT_PATH})
list(APPEND NUGET_COMMAND -Version ${NUGET_PACKAGE_VERSION})
if(NUGET_PRERELEASE)
list(APPEND NUGET_COMMAND -Prerelease)
endif()
if(NUGET_PACKAGESAVEMODE)
list(APPEND NUGET_COMMAND -PackageSaveMode ${NUGET_PACKAGESAVEMODE})
endif()
message(STATUS "Downloading ${NUGET_PACKAGE_NAME} ${NUGET_PACKAGE_VERSION}")
message(VERBOSE "install_nuget_package: NUGET_COMMAND = ${NUGET_COMMAND}")
execute_process(
COMMAND ${NUGET_COMMAND}
OUTPUT_VARIABLE NUGET_OUTPUT
ERROR_VARIABLE NUGET_ERROR
RESULT_VARIABLE NUGET_RESULT
)
message(VERBOSE "install_nuget_package: NUGET_OUTPUT = ${NUGET_OUTPUT}")
if(NOT (NUGET_RESULT STREQUAL 0))
message(FATAL_ERROR "install_nuget_package: Install failed with: ${NUGET_ERROR}")
endif()
endif()
set(${VARIABLE_NAME} "${NUGET_PACKAGE_PATH}" PARENT_SCOPE)
endfunction()