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