Skip to content

Instantly share code, notes, and snippets.

@Riztazz
Last active August 10, 2023 07:15
Show Gist options
  • Save Riztazz/367bb705c76ec7f3d16f0dc97f767cd0 to your computer and use it in GitHub Desktop.
Save Riztazz/367bb705c76ec7f3d16f0dc97f767cd0 to your computer and use it in GitHub Desktop.
Copy VCPKG installed packages licenses into a folder
/*
Riztazz https://www.buymeacoffee.com/riztazz
License:
Copyright © 2023 <Riztazz>
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.
*/
# Adjust paths and execute-process as needed, currently it extracts a node "packages" from spdx.json file.
# Searches for copyright or license file in folders in /share/
# Modify the path as needed
set(COPY_DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/licenses)
file(GLOB DEPENDENCIES "${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/share/*")
foreach(DEPENDENCY ${DEPENDENCIES})
get_filename_component(DEPENDENCY_NAME ${DEPENDENCY} NAME)
file(GLOB COPYRIGHT_FILE "${DEPENDENCY}/copyright")
file(GLOB LICENSE_FILE "${DEPENDENCY}/license")
if(COPYRIGHT_FILE OR LICENSE_FILE)
if(EXISTS ${COPYRIGHT_FILE})
set(COPY_FILE ${COPYRIGHT_FILE})
else()
set(COPY_FILE ${LICENSE_FILE})
endif()
# Run PowerShell script to extract JSON data
execute_process(
COMMAND powershell -Command "$jsonContent = Get-Content -Raw -Path ${DEPENDENCY}/vcpkg.spdx.json | ConvertFrom-Json; $jsonContent.packages | ConvertTo-Json"
OUTPUT_VARIABLE JSON_OUTPUT
OUTPUT_STRIP_TRAILING_WHITESPACE
)
# Write JSON data to a temporary file
set(JSON_TEMP_FILE "${CMAKE_BINARY_DIR}/${DEPENDENCY_NAME}_spdx.json")
file(WRITE ${JSON_TEMP_FILE} "${JSON_OUTPUT}")
# Copy copyright/license file and JSON to COPY_DESTINATION, we could move the temp file instead
configure_file(${COPY_FILE} "${COPY_DESTINATION}/${DEPENDENCY_NAME}_Copyright" COPYONLY)
configure_file(${JSON_TEMP_FILE} "${COPY_DESTINATION}/${DEPENDENCY_NAME}_spdx.json" COPYONLY)
message(STATUS "Copied license/copyright file and JSON for ${DEPENDENCY_NAME} to ${COPY_DESTINATION}")
else()
message(STATUS "No license/copyright file found for ${DEPENDENCY_NAME}")
endif()
endforeach()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment