|
| 1 | +# This file is part of mstore. |
| 2 | +# SPDX-Identifier: Apache-2.0 |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +# Handling of subproject dependencies |
| 17 | +macro( |
| 18 | + "mstore_find_package" |
| 19 | + package |
| 20 | + methods |
| 21 | + url |
| 22 | +) |
| 23 | + string(TOLOWER "${package}" _pkg_lc) |
| 24 | + string(TOUPPER "${package}" _pkg_uc) |
| 25 | + |
| 26 | + foreach(method in ITEMS ${methods}) |
| 27 | + |
| 28 | + if(TARGET "${package}::${package}") |
| 29 | + break() |
| 30 | + endif() |
| 31 | + |
| 32 | + if("${method}" STREQUAL "cmake") |
| 33 | + message(STATUS "${package}: Find installed package") |
| 34 | + find_package("${package}" CONFIG) |
| 35 | + if("${package}_FOUND") |
| 36 | + message(STATUS "${package}: Found installed package") |
| 37 | + break() |
| 38 | + endif() |
| 39 | + endif() |
| 40 | + |
| 41 | + if("${method}" STREQUAL "pkgconf") |
| 42 | + find_package(PkgConfig QUIET) |
| 43 | + pkg_check_modules("${_pkg_uc}" QUIET "${package}") |
| 44 | + if("${_pkg_uc}_FOUND") |
| 45 | + message(STATUS "Found ${package} via pkg-config") |
| 46 | + |
| 47 | + add_library("${package}::${package}" INTERFACE IMPORTED) |
| 48 | + target_link_libraries( |
| 49 | + "${package}::${package}" |
| 50 | + INTERFACE |
| 51 | + "${${_pkg_uc}_LINK_LIBRARIES}" |
| 52 | + ) |
| 53 | + target_include_directories( |
| 54 | + "${package}::${package}" |
| 55 | + INTERFACE |
| 56 | + "${${_pkg_uc}_INCLUDE_DIRS}" |
| 57 | + ) |
| 58 | + break() |
| 59 | + endif() |
| 60 | + endif() |
| 61 | + |
| 62 | + if("${method}" STREQUAL "subproject") |
| 63 | + set("${_pkg_uc}_SOURCE_DIR" "${PROJECT_SOURCE_DIR}/subprojects/${package}") |
| 64 | + set("${_pkg_uc}_BINARY_DIR" "${PROJECT_BINARY_DIR}/subprojects/${package}") |
| 65 | + if(EXISTS "${${_pkg_uc}_SOURCE_DIR}/CMakeLists.txt") |
| 66 | + message(STATUS "Include ${package} from subprojects") |
| 67 | + add_subdirectory( |
| 68 | + "${${_pkg_uc}_SOURCE_DIR}" |
| 69 | + "${${_pkg_uc}_BINARY_DIR}" |
| 70 | + ) |
| 71 | + |
| 72 | + add_library("${package}::${package}" INTERFACE IMPORTED) |
| 73 | + target_link_libraries("${package}::${package}" INTERFACE "${package}") |
| 74 | + |
| 75 | + # We need the module directory in the subproject before we finish the configure stage |
| 76 | + if(NOT EXISTS "${${_pkg_uc}_BINARY_DIR}/include") |
| 77 | + make_directory("${${_pkg_uc}_BINARY_DIR}/include") |
| 78 | + endif() |
| 79 | + |
| 80 | + break() |
| 81 | + endif() |
| 82 | + endif() |
| 83 | + |
| 84 | + if("${method}" STREQUAL "fetch") |
| 85 | + message(STATUS "Retrieving ${package} from ${url}") |
| 86 | + include(FetchContent) |
| 87 | + FetchContent_Declare( |
| 88 | + "${_pkg_lc}" |
| 89 | + GIT_REPOSITORY "${url}" |
| 90 | + GIT_TAG "HEAD" |
| 91 | + ) |
| 92 | + FetchContent_MakeAvailable("${_pkg_lc}") |
| 93 | + |
| 94 | + add_library("${package}::${package}" INTERFACE IMPORTED) |
| 95 | + target_link_libraries("${package}::${package}" INTERFACE "${package}") |
| 96 | + |
| 97 | + # We need the module directory in the subproject before we finish the configure stage |
| 98 | + FetchContent_GetProperties("${_pkg_lc}" BINARY_DIR "${_pkg_uc}_BINARY_DIR") |
| 99 | + if(NOT EXISTS "${${_pkg_uc}_BINARY_DIR}/include") |
| 100 | + make_directory("${${_pkg_uc}_BINARY_DIR}/include") |
| 101 | + endif() |
| 102 | + |
| 103 | + break() |
| 104 | + endif() |
| 105 | + |
| 106 | + endforeach() |
| 107 | + |
| 108 | + unset(_pkg_lc) |
| 109 | + unset(_pkg_uc) |
| 110 | + |
| 111 | + if(NOT TARGET "${package}::${package}") |
| 112 | + message(FATAL_ERROR "Could not find dependency ${package}") |
| 113 | + endif() |
| 114 | + |
| 115 | +endmacro() |
0 commit comments