| 1 |
!> Implementation of the meta data for libraries. |
| 2 |
!> |
| 3 |
!> A library table can currently have the following fields |
| 4 |
!> |
| 5 |
!>```toml |
| 6 |
!>[library] |
| 7 |
!>source-dir = "path" |
| 8 |
!>include-dir = ["path1","path2"] |
| 9 |
!>build-script = "file" |
| 10 |
!>``` |
| 11 |
module fpm_manifest_library |
| 12 |
use fpm_error, only : error_t, syntax_error, fatal_error |
| 13 |
use fpm_strings, only: string_t, string_cat, operator(==) |
| 14 |
use tomlf, only : toml_table, toml_key, toml_stat |
| 15 |
use fpm_toml, only : get_value, get_list, serializable_t, set_value, & |
| 16 |
set_list, set_string, get_value, has_list |
| 17 |
implicit none |
| 18 |
private |
| 19 |
|
| 20 |
public :: library_config_t, new_library |
| 21 |
|
| 22 |
|
| 23 |
!> Configuration meta data for a library |
| 24 |
type, extends(serializable_t) :: library_config_t |
| 25 |
|
| 26 |
!> Source path prefix |
| 27 |
character(len=:), allocatable :: source_dir |
| 28 |
|
| 29 |
!> Include path prefix |
| 30 |
type(string_t), allocatable :: include_dir(:) |
| 31 |
|
| 32 |
!> Alternative build script to be invoked |
| 33 |
character(len=:), allocatable :: build_script |
| 34 |
|
| 35 |
!> Shared / Static / Monolithic library types (can be multiple) |
| 36 |
type(string_t), allocatable :: lib_type(:) |
| 37 |
|
| 38 |
contains |
| 39 |
|
| 40 |
!> Print information on this instance |
| 41 |
procedure :: info |
| 42 |
|
| 43 |
!> Serialization interface |
| 44 |
procedure :: serializable_is_same => library_is_same |
| 45 |
procedure :: dump_to_toml |
| 46 |
procedure :: load_from_toml |
| 47 |
|
| 48 |
!> Check library types |
| 49 |
procedure, non_overridable :: monolithic |
| 50 |
procedure, non_overridable :: shared |
| 51 |
procedure, non_overridable :: static |
| 52 |
|
| 53 |
end type library_config_t |
| 54 |
|
| 55 |
character(*), parameter, private :: class_name = 'library_config_t' |
| 56 |
|
| 57 |
|
| 58 |
contains |
| 59 |
|
| 60 |
!> Check if this is a shared library config |
| 61 |
!> (full packages built as shared libs) |
| 62 |
elemental logical function shared(self) |
| 63 |
!> Instance of the library configuration |
| 64 |
class(library_config_t), intent(in) :: self |
| 65 |
|
| 66 |
integer :: i |
| 67 |
|
| 68 |
shared = .false. |
| 69 |
if (allocated(self%lib_type)) then |
| 70 |
do i = 1, size(self%lib_type) |
| 71 |
if (self%lib_type(i)%s == "shared") then |
| 72 |
shared = .true. |
| 73 |
return |
| 74 |
end if |
| 75 |
end do |
| 76 |
endif |
| 77 |
|
| 78 |
end function shared |
| 79 |
|
| 80 |
|
| 81 |
!> Check if this is a static library config |
| 82 |
!> (full packages built as static libs) |
| 83 |
elemental logical function static(self) |
| 84 |
!> Instance of the library configuration |
| 85 |
class(library_config_t), intent(in) :: self |
| 86 |
|
| 87 |
integer :: i |
| 88 |
|
| 89 |
static = .false. |
| 90 |
if (allocated(self%lib_type)) then |
| 91 |
do i = 1, size(self%lib_type) |
| 92 |
if (self%lib_type(i)%s == "static") then |
| 93 |
static = .true. |
| 94 |
return |
| 95 |
end if |
| 96 |
end do |
| 97 |
endif |
| 98 |
end function static |
| 99 |
|
| 100 |
|
| 101 |
!> Check if this is a monolithic library config |
| 102 |
!> (single monolithic archive with all objects used by this project) |
| 103 |
elemental logical function monolithic(self) |
| 104 |
!> Instance of the library configuration |
| 105 |
class(library_config_t), intent(in) :: self |
| 106 |
|
| 107 |
integer :: i |
| 108 |
|
| 109 |
if (allocated(self%lib_type)) then |
| 110 |
monolithic = .false. |
| 111 |
do i = 1, size(self%lib_type) |
| 112 |
if (self%lib_type(i)%s == "monolithic") then |
| 113 |
monolithic = .true. |
| 114 |
return |
| 115 |
end if |
| 116 |
end do |
| 117 |
else |
| 118 |
! Default: monolithic |
| 119 |
monolithic = .true. |
| 120 |
endif |
| 121 |
end function monolithic |
| 122 |
|
| 123 |
|
| 124 |
!> Construct a new library configuration from a TOML data structure |
| 125 |
subroutine new_library(self, table, error) |
| 126 |
|
| 127 |
!> Instance of the library configuration |
| 128 |
type(library_config_t), intent(out) :: self |
| 129 |
|
| 130 |
!> Instance of the TOML data structure |
| 131 |
type(toml_table), intent(inout) :: table |
| 132 |
|
| 133 |
!> Error handling |
| 134 |
type(error_t), allocatable, intent(out) :: error |
| 135 |
|
| 136 |
integer :: stat, i |
| 137 |
character(len=:), allocatable :: single_type |
| 138 |
|
| 139 |
call check(table, error) |
| 140 |
if (allocated(error)) return |
| 141 |
|
| 142 |
if (has_list(table, "source-dir")) then |
| 143 |
call syntax_error(error, "Manifest key [library.source-dir] does not allow list input") |
| 144 |
return |
| 145 |
end if |
| 146 |
|
| 147 |
! library.type can now be either a single value or a list |
| 148 |
|
| 149 |
call get_value(table, "source-dir", self%source_dir, "src") |
| 150 |
call get_value(table, "build-script", self%build_script) |
| 151 |
|
| 152 |
call get_list(table, "include-dir", self%include_dir, error) |
| 153 |
if (allocated(error)) return |
| 154 |
|
| 155 |
! Parse library type - can be single value or array |
| 156 |
if (has_list(table, "type")) then |
| 157 |
! Array of types |
| 158 |
call get_list(table, "type", self%lib_type, error) |
| 159 |
if (allocated(error)) return |
| 160 |
else |
| 161 |
! Single type - convert to array for consistency |
| 162 |
call get_value(table, "type", single_type, "monolithic") |
| 163 |
self%lib_type = [string_t(single_type)] |
| 164 |
end if |
| 165 |
|
| 166 |
if (.not.allocated(self%lib_type)) then |
| 167 |
self%lib_type = [string_t("monolithic")] |
| 168 |
end if |
| 169 |
|
| 170 |
! Validate all types in the array |
| 171 |
do i = 1, size(self%lib_type) |
| 172 |
select case(self%lib_type(i)%s) |
| 173 |
case("shared","static","monolithic") |
| 174 |
! OK |
| 175 |
case default |
| 176 |
call fatal_error(error,"Value of library.type cannot be '"//self%lib_type(i)%s & |
| 177 |
//"', choose shared/static/monolithic (default)") |
| 178 |
return |
| 179 |
end select |
| 180 |
end do |
| 181 |
|
| 182 |
! Check that monolithic is not specified together with static or shared |
| 183 |
if (monolithic(self) .and. (static(self) .or. shared(self))) then |
| 184 |
call fatal_error(error,"library.type 'monolithic' cannot be specified together with 'static' or 'shared'") |
| 185 |
return |
| 186 |
end if |
| 187 |
|
| 188 |
! Set default value of include-dir if not found in manifest |
| 189 |
if (.not.allocated(self%include_dir)) then |
| 190 |
self%include_dir = [string_t("include")] |
| 191 |
end if |
| 192 |
|
| 193 |
|
| 194 |
end subroutine new_library |
| 195 |
|
| 196 |
|
| 197 |
!> Check local schema for allowed entries |
| 198 |
subroutine check(table, error) |
| 199 |
|
| 200 |
!> Instance of the TOML data structure |
| 201 |
type(toml_table), intent(inout) :: table |
| 202 |
|
| 203 |
!> Error handling |
| 204 |
type(error_t), allocatable, intent(out) :: error |
| 205 |
|
| 206 |
type(toml_key), allocatable :: list(:) |
| 207 |
integer :: ikey |
| 208 |
|
| 209 |
call table%get_keys(list) |
| 210 |
|
| 211 |
! table can be empty |
| 212 |
if (size(list) < 1) return |
| 213 |
|
| 214 |
do ikey = 1, size(list) |
| 215 |
select case(list(ikey)%key) |
| 216 |
case default |
| 217 |
call syntax_error(error, "Key "//list(ikey)%key//" is not allowed in library") |
| 218 |
exit |
| 219 |
|
| 220 |
case("source-dir", "include-dir", "build-script", "type") |
| 221 |
continue |
| 222 |
|
| 223 |
end select |
| 224 |
end do |
| 225 |
|
| 226 |
end subroutine check |
| 227 |
|
| 228 |
|
| 229 |
!> Write information on instance |
| 230 |
subroutine info(self, unit, verbosity) |
| 231 |
|
| 232 |
!> Instance of the library configuration |
| 233 |
class(library_config_t), intent(in) :: self |
| 234 |
|
| 235 |
!> Unit for IO |
| 236 |
integer, intent(in) :: unit |
| 237 |
|
| 238 |
!> Verbosity of the printout |
| 239 |
integer, intent(in), optional :: verbosity |
| 240 |
|
| 241 |
integer :: pr |
| 242 |
character(len=*), parameter :: fmt = '("#", 1x, a, t30, a)' |
| 243 |
|
| 244 |
if (present(verbosity)) then |
| 245 |
pr = verbosity |
| 246 |
else |
| 247 |
pr = 1 |
| 248 |
end if |
| 249 |
|
| 250 |
if (pr < 1) return |
| 251 |
|
| 252 |
write(unit, fmt) "Library target" |
| 253 |
if (allocated(self%source_dir)) then |
| 254 |
write(unit, fmt) "- source directory", self%source_dir |
| 255 |
end if |
| 256 |
if (allocated(self%include_dir)) then |
| 257 |
write(unit, fmt) "- include directory", string_cat(self%include_dir,",") |
| 258 |
end if |
| 259 |
|
| 260 |
write(unit, fmt) "- library type", string_cat(self%lib_type,",") |
| 261 |
|
| 262 |
if (allocated(self%build_script)) then |
| 263 |
write(unit, fmt) "- custom build", self%build_script |
| 264 |
end if |
| 265 |
|
| 266 |
end subroutine info |
| 267 |
|
| 268 |
logical function library_is_same(this,that) |
| 269 |
class(library_config_t), intent(in) :: this |
| 270 |
class(serializable_t), intent(in) :: that |
| 271 |
|
| 272 |
library_is_same = .false. |
| 273 |
|
| 274 |
select type (other=>that) |
| 275 |
type is (library_config_t) |
| 276 |
if (.not.this%include_dir==other%include_dir) return |
| 277 |
if (allocated(this%source_dir).neqv.allocated(other%source_dir)) return |
| 278 |
if (allocated(this%source_dir)) then |
| 279 |
if (.not.this%source_dir==other%source_dir) return |
| 280 |
end if |
| 281 |
if (allocated(this%build_script).neqv.allocated(other%build_script)) return |
| 282 |
if (allocated(this%build_script)) then |
| 283 |
if (.not.this%build_script==other%build_script) return |
| 284 |
end if |
| 285 |
if (allocated(this%lib_type).neqv.allocated(other%lib_type)) return |
| 286 |
if (allocated(this%lib_type)) then |
| 287 |
if (.not.this%lib_type==other%lib_type) return |
| 288 |
end if |
| 289 |
class default |
| 290 |
! Not the same type |
| 291 |
return |
| 292 |
end select |
| 293 |
|
| 294 |
!> All checks passed! |
| 295 |
library_is_same = .true. |
| 296 |
|
| 297 |
end function library_is_same |
| 298 |
|
| 299 |
!> Dump install config to toml table |
| 300 |
subroutine dump_to_toml(self, table, error) |
| 301 |
|
| 302 |
!> Instance of the serializable object |
| 303 |
class(library_config_t), intent(inout) :: self |
| 304 |
|
| 305 |
!> Data structure |
| 306 |
type(toml_table), intent(inout) :: table |
| 307 |
|
| 308 |
!> Error handling |
| 309 |
type(error_t), allocatable, intent(out) :: error |
| 310 |
|
| 311 |
call set_string(table, "source-dir", self%source_dir, error, class_name) |
| 312 |
if (allocated(error)) return |
| 313 |
call set_string(table, "build-script", self%build_script, error, class_name) |
| 314 |
if (allocated(error)) return |
| 315 |
call set_list(table, "include-dir", self%include_dir, error) |
| 316 |
if (allocated(error)) return |
| 317 |
call set_list(table, "type", self%lib_type, error) |
| 318 |
if (allocated(error)) return |
| 319 |
|
| 320 |
end subroutine dump_to_toml |
| 321 |
|
| 322 |
!> Read install config from toml table (no checks made at this stage) |
| 323 |
subroutine load_from_toml(self, table, error) |
| 324 |
|
| 325 |
!> Instance of the serializable object |
| 326 |
class(library_config_t), intent(inout) :: self |
| 327 |
|
| 328 |
!> Data structure |
| 329 |
type(toml_table), intent(inout) :: table |
| 330 |
|
| 331 |
!> Error handling |
| 332 |
type(error_t), allocatable, intent(out) :: error |
| 333 |
|
| 334 |
call get_value(table, "source-dir", self%source_dir) |
| 335 |
if (allocated(error)) return |
| 336 |
call get_value(table, "build-script", self%build_script) |
| 337 |
if (allocated(error)) return |
| 338 |
call get_list(table, "include-dir", self%include_dir, error) |
| 339 |
if (allocated(error)) return |
| 340 |
call get_list(table, "type", self%lib_type, error) |
| 341 |
if (allocated(error)) return |
| 342 |
|
| 343 |
end subroutine load_from_toml |
| 344 |
|
| 345 |
|
| 346 |
end module fpm_manifest_library |