File: xtb-utils.cmake

package info (click to toggle)
xtb 6.7.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,348 kB
  • sloc: f90: 139,236; fortran: 2,948; ansic: 2,215; makefile: 71; sh: 17; csh: 7; tcl: 7
file content (128 lines) | stat: -rw-r--r-- 4,156 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# This file is part of xtb.
# SPDX-Identifier: LGPL-3.0-or-later
#
# xtb is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# xtb is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with xtb.  If not, see <https://www.gnu.org/licenses/>.

# Handling of subproject dependencies
macro(
   "xtb_find_package"
   package
   methods
   url
)
string(TOLOWER "${package}" _pkg_lc)
string(TOUPPER "${package}" _pkg_uc)

# iterate through all methods
foreach(method ${methods})

   if(TARGET "${package}::${package}")
      break()
   endif()

   # cmake case
   if("${method}" STREQUAL "cmake")
      if(DEFINED "${_pkg_uc}_DIR")
         set("_${_pkg_uc}_DIR")
         set("${package}_DIR" "${_pkg_uc}_DIR")
      endif()
      find_package("${package}" CONFIG QUIET)
      if("${package}_FOUND")
         message(STATUS "Found ${package} via CMake config")
         break()
      endif()

   # pkgconf case
   elseif("${method}" STREQUAL "pkgconf")
      find_package("PkgConfig" QUIET) # built-in Find script 
      pkg_check_modules("${_pkg_uc}" QUIET "${package}") # check if it is a pkg-config module
      if("${_pkg_uc}_FOUND")
         message(STATUS "Found ${package} via pkg-config")
         add_library("${package}::${package}" INTERFACE IMPORTED) # interface library
         target_link_libraries(
            "${package}::${package}"
            INTERFACE
            "${${_pkg_uc}_LINK_LIBRARIES}"
         )
         target_include_directories(
            "${package}::${package}"
            INTERFACE
            "${${_pkg_uc}_INCLUDE_DIRS}"
         )
         break()
      endif()

   # subproject case
   elseif("${method}" STREQUAL "subproject")
      if(NOT DEFINED "${_pkg_uc}_SUBPROJECT")
         set("${_pkg_uc}_SUBPROJECT" "subprojects/${package}")
      endif()
      set("${_pkg_uc}_SOURCE_DIR" "${PROJECT_SOURCE_DIR}/${${_pkg_uc}_SUBPROJECT}")
      set("${_pkg_uc}_BINARY_DIR" "${PROJECT_BINARY_DIR}/${${_pkg_uc}_SUBPROJECT}")
      
      # if can be configured from the subprojects dir
      if(EXISTS "${${_pkg_uc}_SOURCE_DIR}/CMakeLists.txt")
         message(STATUS "Include ${package} from ${${_pkg_uc}_SUBPROJECT}")
         
         add_subdirectory(
            "${${_pkg_uc}_SOURCE_DIR}"
            "${${_pkg_uc}_BINARY_DIR}"
         )

         # create interface directory and manage it's dependencies
         add_library("${package}::${package}" INTERFACE IMPORTED)
         target_link_libraries("${package}::${package}" INTERFACE "${package}")

         # We need the module directory in the subproject before we finish the configure stage
         if(NOT EXISTS "${${_pkg_uc}_BINARY_DIR}/include")
            file(MAKE_DIRECTORY "${${_pkg_uc}_BINARY_DIR}/include")
         endif()

         break()
      endif()

   # fetch from url case
   elseif("${method}" STREQUAL "fetch")
      message(STATUS "Retrieving ${package} from ${url}")
      include(FetchContent) # module for fetching from repo
      if(CMAKE_BUILD_TYPE STREQUAL "Debug")
        set(FETCHCONTENT_QUIET FALSE)
      endif()
      FetchContent_Declare(
         "${_pkg_lc}"
         GIT_REPOSITORY "${url}"
         GIT_TAG "HEAD"
      )
      FetchContent_MakeAvailable("${_pkg_lc}")
      
      add_library("${package}::${package}" INTERFACE IMPORTED)
      target_link_libraries("${package}::${package}" INTERFACE "${package}")

      if(NOT EXISTS "${${_pkg_lc}_BINARY_DIR}/include")
         file(MAKE_DIRECTORY "${${_pkg_lc}_BINARY_DIR}/include")
      endif()

      break()
   endif()

endforeach()

unset(_pkg_lc)
unset(_pkg_uc)

# sanity check
if(NOT TARGET "${package}::${package}")
   message(FATAL_ERROR "Could not find dependency ${package}")
endif()
endmacro()