|
| 1 | +include_guard() |
| 2 | + |
| 3 | +#[[==================================================================================================================== |
| 4 | + install_nuget_package |
| 5 | + --------------------- |
| 6 | + Downloads a NuGet package and returns the path to it. |
| 7 | +
|
| 8 | + install_nuget_package( |
| 9 | + <package name> |
| 10 | + <package version> |
| 11 | + <variable name> |
| 12 | + ) |
| 13 | +====================================================================================================================]]# |
| 14 | +function(install_nuget_package NUGET_PACKAGE_NAME NUGET_PACKAGE_VERSION NUGET_PACKAGE_PATH_PROPERTY) |
| 15 | + if(NOT NUGET_PACKAGE_ROOT_PATH) |
| 16 | + set(NUGET_PACKAGE_ROOT_PATH ${CMAKE_BINARY_DIR}/__nuget) |
| 17 | + endif() |
| 18 | + |
| 19 | + set(NUGET_PACKAGE_PATH "${NUGET_PACKAGE_ROOT_PATH}/${NUGET_PACKAGE_NAME}.${NUGET_PACKAGE_VERSION}") |
| 20 | + |
| 21 | + if(NOT EXISTS "${NUGET_PACKAGE_PATH}") |
| 22 | + find_program(NUGET_PATH |
| 23 | + NAMES nuget nuget.exe |
| 24 | + ) |
| 25 | + |
| 26 | + if(NUGET_PATH STREQUAL "NUGET_PATH-NOTFOUND") |
| 27 | + message(FATAL_ERROR "nuget.exe cannot be found.") |
| 28 | + endif() |
| 29 | + |
| 30 | + set(NUGET_COMMAND ${NUGET_PATH} install ${NUGET_PACKAGE_NAME}) |
| 31 | + list(APPEND NUGET_COMMAND -OutputDirectory ${NUGET_PACKAGE_ROOT_PATH}) |
| 32 | + list(APPEND NUGET_COMMAND -Version ${NUGET_PACKAGE_VERSION}) |
| 33 | + list(APPEND NUGET_COMMAND -PackageSaveMode nuspec) |
| 34 | + |
| 35 | + message(STATUS "Downloading ${NUGET_PACKAGE_NAME} ${NUGET_PACKAGE_VERSION}") |
| 36 | + message(VERBOSE "install_nuget_package: NUGET_COMMAND = ${NUGET_COMMAND}") |
| 37 | + |
| 38 | + execute_process( |
| 39 | + COMMAND ${NUGET_COMMAND} |
| 40 | + OUTPUT_VARIABLE NUGET_OUTPUT |
| 41 | + ERROR_VARIABLE NUGET_ERROR |
| 42 | + RESULT_VARIABLE NUGET_RESULT |
| 43 | + ) |
| 44 | + |
| 45 | + message(VERBOSE "install_nuget_package: NUGET_OUTPUT = ${NUGET_OUTPUT}") |
| 46 | + if(NOT (NUGET_RESULT STREQUAL 0)) |
| 47 | + message(FATAL_ERROR "install_nuget_package: Install failed with: ${NUGET_ERROR}") |
| 48 | + endif() |
| 49 | + endif() |
| 50 | + |
| 51 | + set(${NUGET_PACKAGE_PATH_PROPERTY} "${NUGET_PACKAGE_PATH}" PARENT_SCOPE) |
| 52 | +endfunction() |
| 53 | + |
| 54 | +#[[==================================================================================================================== |
| 55 | + add_nuget_package |
| 56 | + ----------------- |
| 57 | + Creates a target to package files into a NuGet package. |
| 58 | +
|
| 59 | + add_nuget_package(<target> |
| 60 | + <nuspec file> |
| 61 | + VERSION <version> |
| 62 | + [PROPERTIES |
| 63 | + <<PROPERTY_NAME> <PROPERTY_VALUE>>+ |
| 64 | + ] |
| 65 | + [FILES |
| 66 | + <<SOURCE> <TARGET>>+ |
| 67 | + ] |
| 68 | + ) |
| 69 | +
|
| 70 | +====================================================================================================================]]# |
| 71 | +function(add_nuget_package TARGET NUSPEC_FILE) |
| 72 | + find_program(NUGET_PATH |
| 73 | + NAMES nuget nuget.exe |
| 74 | + ) |
| 75 | + |
| 76 | + if(NUGET_PATH STREQUAL "NUGET_PATH-NOTFOUND") |
| 77 | + message(FATAL_ERROR "nuget.exe cannot be found.") |
| 78 | + endif() |
| 79 | + |
| 80 | + set(OPTIONS) |
| 81 | + set(ONE_VALUE_KEYWORDS VERSION) |
| 82 | + set(MULTI_VALUE_KEYWORDS FILES PROPERTIES) |
| 83 | + |
| 84 | + cmake_parse_arguments(PARSE_ARGV 2 NUGET_PACK "${OPTIONS}" "${ONE_VALUE_KEYWORDS}" "${MULTI_VALUE_KEYWORDS}") |
| 85 | + |
| 86 | + if(NOT NUGET_PACK_VERSION) |
| 87 | + message(FATAL_ERROR "add_nuget_package: 'VERSION' must be specified.") |
| 88 | + endif() |
| 89 | + |
| 90 | + # Set NUGET_BASE_NAME to NUSPEC_FILE without '.nuspec'. |
| 91 | + string(REPLACE ".nuspec" "" NUGET_BASE_NAME ${NUSPEC_FILE}) |
| 92 | + |
| 93 | + # Walk the 'FILES' and: |
| 94 | + # 1. Build the 'NUGET_DEPENDENCIES' property that will be used for dependency checking the custom build step. |
| 95 | + # 2. Build the 'NUGET_FILES' property that will be added to the '<files\>' section of the configured file. |
| 96 | + set(NUGET_FILES) |
| 97 | + set(NUGET_DEPENDENCIES ${NUSPEC_FILE}) |
| 98 | + while(NUGET_PACK_FILES) |
| 99 | + list(POP_FRONT NUGET_PACK_FILES FILE_SOURCE) |
| 100 | + list(POP_FRONT NUGET_PACK_FILES FILE_TARGET) |
| 101 | + |
| 102 | + list(APPEND NUGET_DEPENDENCIES ${FILE_SOURCE}) |
| 103 | + |
| 104 | + # To allow '$<CONFIG>' usage in FILE_SOURCE, replace '$<CONFIG>'-->'$configuration$' and use nuget.exe support |
| 105 | + # to replace $configuration$ in the target. |
| 106 | + string(REPLACE "\$<CONFIG>" "$configuration$" FILE_SOURCE ${FILE_SOURCE}) |
| 107 | + set(NUGET_FILES "${NUGET_FILES}\n <file src=\"${FILE_SOURCE}\" target=\"${FILE_TARGET}\" />") |
| 108 | + endwhile() |
| 109 | + |
| 110 | + configure_file(${NUSPEC_FILE} ${CMAKE_CURRENT_BINARY_DIR}/${NUSPEC_FILE}) |
| 111 | + |
| 112 | + set(NUGET_COMMAND ${NUGET_PATH} pack ${CMAKE_CURRENT_BINARY_DIR}/${NUSPEC_FILE}) |
| 113 | + list(APPEND NUGET_COMMAND -OutputDirectory ${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG>) |
| 114 | + list(APPEND NUGET_COMMAND -Version ${NUGET_PACK_VERSION}) |
| 115 | + |
| 116 | + while(NUGET_PACK_PROPERTIES) |
| 117 | + list(POP_FRONT NUGET_PACK_PROPERTIES PROPERTY_NAME) |
| 118 | + list(POP_FRONT NUGET_PACK_PROPERTIES PROPERTY_VALUE) |
| 119 | + |
| 120 | + list(APPEND NUGET_COMMAND -Properties "\"${PROPERTY_NAME}=${PROPERTY_VALUE}\"") |
| 121 | + endwhile() |
| 122 | + list(APPEND NUGET_COMMAND -Properties "\"configuration=$<CONFIG>\"") |
| 123 | + |
| 124 | + message(VERBOSE "add_nuget_package: NUGET_COMMAND = ${NUGET_COMMAND}") |
| 125 | + |
| 126 | + add_custom_command( |
| 127 | + OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG>/${NUGET_BASE_NAME}.${NUGET_PACK_VERSION}.nupkg |
| 128 | + COMMAND ${NUGET_COMMAND} |
| 129 | + DEPENDS ${NUGET_DEPENDENCIES} |
| 130 | + COMMENT "NuGet: ${NUSPEC_FILE}" |
| 131 | + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} |
| 132 | + ) |
| 133 | + |
| 134 | + add_custom_target(${TARGET} ALL |
| 135 | + DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG>/${NUGET_BASE_NAME}.${NUGET_PACK_VERSION}.nupkg |
| 136 | + ) |
| 137 | +endfunction() |
0 commit comments