xref: /aosp_15_r20/external/grpc-grpc/cmake/download_archive.cmake (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1# Copyright 2021 The gRPC Authors
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15set(_download_archive_TEMPORARY_DIR ${CMAKE_BINARY_DIR}/http_archives)
16file(MAKE_DIRECTORY ${_download_archive_TEMPORARY_DIR})
17
18# This is basically Bazel's http_archive.
19# Note that strip_prefix strips the directory path prefix of the extracted
20# archive content, and it may strip multiple directories.
21function(download_archive destination url hash strip_prefix)
22  # Fetch and validate
23  set(_TEMPORARY_FILE ${_download_archive_TEMPORARY_DIR}/${strip_prefix}.tar.gz)
24  message(STATUS "Downloading from ${url}, if failed, please try configuring again")
25  file(DOWNLOAD ${url} ${_TEMPORARY_FILE}
26       TIMEOUT 60
27       EXPECTED_HASH SHA256=${hash}
28       TLS_VERIFY ON)
29  # Extract
30  execute_process(COMMAND
31                  ${CMAKE_COMMAND} -E tar xvf ${_TEMPORARY_FILE}
32                  WORKING_DIRECTORY ${_download_archive_TEMPORARY_DIR}
33                  OUTPUT_QUIET)
34  get_filename_component(_download_archive_Destination_Path ${destination} DIRECTORY)
35  file(MAKE_DIRECTORY ${_download_archive_Destination_Path})
36  file(RENAME ${_download_archive_TEMPORARY_DIR}/${strip_prefix} ${destination})
37  # Clean up
38  file(REMOVE ${_download_archive_TEMPORARY_DIR}/${strip_prefix})
39  file(REMOVE ${_TEMPORARY_FILE})
40endfunction()
41