xref: /aosp_15_r20/external/tink/cmake/HttpArchive.cmake (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1# Copyright 2019 Google LLC
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
15include(FetchContent)
16include(CMakeParseArguments)
17
18# Download, unpack and configure a dependency.
19#
20# The project is added as a subdirectory of Tink, unless DATA_ONLY is
21# specified. This makes all target defined by it available as dependencies.
22#
23# This rule also defines two variables:
24#   - <NAME>_SOURCE_DIR points to the root directory of the downloaded package;
25#     it can be used to reference data in tests, or append extra include/link
26#     paths in the Workspace file.
27#   - <NAME>_BINARY_DIR points to the build directory.
28#
29# Parameters:
30#   NAME name of the dependency.
31#   URL url to fetch a source archive from.
32#   SHA256 hash of the file downloaded from URL.
33#
34# Optional parameters:
35#   CMAKE_SUBDIR subdirectory of the downloaded archive where the root
36#     CMakeLists.txt file for the project is located. Defaults to the root.
37#   CMAKE_ARGS any additional argument that should be passed to cmake when
38#     configuring the downloaded archive. Defaults to empty.
39#   DATA_ONLY flag, if present the package will only be downloaded, verified and
40#     unpacked. No configuration step is performed, and no target included. This
41#     is useful for downloading archives of test vectors or artifacts.
42#     False by default.
43#
44function(http_archive)
45  cmake_parse_arguments(PARSE_ARGV 0 http_archive
46    "DATA_ONLY"
47    "NAME;URL;SHA256;CMAKE_SUBDIR"
48    "CMAKE_ARGS"
49  )
50  FetchContent_Declare(
51    ${http_archive_NAME}
52    URL       ${http_archive_URL}
53    URL_HASH  SHA256=${http_archive_SHA256}
54  )
55  message(STATUS "Fetching ${http_archive_NAME}")
56  FetchContent_GetProperties(${http_archive_NAME})
57  if(NOT ${http_archive_NAME}_POPULATED)
58    FetchContent_Populate(${http_archive_NAME})
59    if (NOT http_archive_DATA_ONLY)
60      add_subdirectory(
61        ${${http_archive_NAME}_SOURCE_DIR}/${http_archive_CMAKE_SUBDIR}
62        ${${http_archive_NAME}_BINARY_DIR}
63        EXCLUDE_FROM_ALL)
64    endif()
65    # Expose these variables to the caller.
66    set(
67      "${http_archive_NAME}_SOURCE_DIR"
68      "${${http_archive_NAME}_SOURCE_DIR}"
69      PARENT_SCOPE)
70    set(
71      "${http_archive_NAME}_BINARY_DIR"
72      "${${http_archive_NAME}_BINARY_DIR}"
73      PARENT_SCOPE)
74  endif()
75endfunction(http_archive)
76