Debian Codegraph @

src/fpm/manifest/dependency.f90

raw
1 !> Implementation of the meta data for dependencies.
2 !>
3 !> A dependency table can currently have the following fields
4 !>
5 !>```toml
6 !>[dependencies]
7 !>"dep1" = { git = "url" }
8 !>"dep2" = { git = "url", branch = "name" }
9 !>"dep3" = { git = "url", tag = "name" }
10 !>"dep4" = { git = "url", rev = "sha1" }
11 !>"dep0" = { path = "path" }
12 !>"dep5" = { path = "path", features = ["feat1", "feat2"] }
13 !>"dep6" = { path = "path", profile = "myprofile" }
14 !>```
15 !>
16 !> The `features` and `profile` keys are mutually exclusive.
17 !> `features` provides a list of individual feature names;
18 !> `profile` provides a single profile name defined in the dependency's manifest.
19 !>
20 !> To reduce the amount of boilerplate code this module provides two constructors
21 !> for dependency types, one basic for an actual dependency (inline) table
22 !> and another to collect all dependency objects from a dependencies table,
23 !> which is handling the allocation of the objects and is forwarding the
24 !> individual dependency tables to their respective constructors.
25 !> The usual entry point should be the constructor for the super table.
26 !>
27 !> This objects contains a target to retrieve required `fpm` projects to
28 !> build the target declaring the dependency.
29 !> Resolving a dependency will result in obtaining a new package configuration
30 !> data for the respective project.
31 module fpm_manifest_dependency
32 use fpm_error, only: error_t, syntax_error, fatal_error
33 use fpm_git, only: git_target_t, git_target_tag, git_target_branch, &
34 & git_target_revision, git_target_default, git_matches_manifest
35 use tomlf, only: toml_table, toml_key, toml_stat
36 use fpm_toml, only: get_value, check_keys, serializable_t, add_table, &
37 & set_value, set_string, get_list, set_list
38 use fpm_filesystem, only: windows_path, join_path
39 use fpm_environment, only: get_os_type, OS_WINDOWS
40 use fpm_manifest_metapackages, only: metapackage_config_t, is_meta_package, new_meta_config, &
41 metapackage_request_t, new_meta_request
42 use fpm_versioning, only: version_t, new_version
43 use fpm_strings, only: string_t
44 use fpm_manifest_preprocess
45 implicit none
46 private
47
48 public :: dependency_config_t, new_dependency, new_dependencies, manifest_has_changed, &
49 & dependency_destroy, resize
50
51 !> Configuration meta data for a dependency
52 type, extends(serializable_t) :: dependency_config_t
53
54 !> Name of the dependency
55 character(len=:), allocatable :: name
56
57 !> Local target
58 character(len=:), allocatable :: path
59
60 !> Namespace which the dependency belongs to.
61 !> Enables multiple dependencies with the same name.
62 !> Required for dependencies that are obtained via the official registry.
63 character(len=:), allocatable :: namespace
64
65 !> The requested version of the dependency.
66 !> The latest version is used if not specified.
67 type(version_t), allocatable :: requested_version
68
69 !> Requested macros for the dependency
70 type(preprocess_config_t), allocatable :: preprocess(:)
71
72 !> Requested features for the dependency
73 type(string_t), allocatable :: features(:)
74
75 !> Requested profile for the dependency (mutually exclusive with features)
76 character(len=:), allocatable :: profile
77
78 !> Git descriptor
79 type(git_target_t), allocatable :: git
80
81 contains
82
83 !> Print information on this instance
84 procedure :: info
85
86 !> Add a preprocessor configuration
87 procedure :: add_preprocess
88
89 !> Serialization interface
90 procedure :: serializable_is_same => dependency_is_same
91 procedure :: dump_to_toml
92 procedure :: load_from_toml
93
94 end type dependency_config_t
95
96 !> Common output format for writing to the command line
97 character(len=*), parameter :: out_fmt = '("#", *(1x, g0))'
98
99 interface resize
100 module procedure resize_dependency_config
101 end interface resize
102
103 contains
104
105 !> Construct a new dependency configuration from a TOML data structure
106 subroutine new_dependency(self, table, root, error)
107
108 !> Instance of the dependency configuration
109 type(dependency_config_t), intent(out) :: self
110
111 !> Instance of the TOML data structure
112 type(toml_table), intent(inout) :: table
113
114 !> Root directory of the manifest
115 character(*), intent(in), optional :: root
116
117 !> Error handling
118 type(error_t), allocatable, intent(out) :: error
119
120 character(len=:), allocatable :: uri, value, requested_version
121
122 type(toml_table), pointer :: child
123
124 call check(table, error)
125 if (allocated(error)) return
126
127 call table%get_key(self%name)
128 call get_value(table, "namespace", self%namespace)
129
130 call get_value(table, "v", requested_version)
131 if (allocated(requested_version)) then
132 if (.not. allocated(self%requested_version)) allocate (self%requested_version)
133 call new_version(self%requested_version, requested_version, error)
134 if (allocated(error)) return
135 end if
136
137 !> Get optional preprocessor directives
138 call get_value(table, "preprocess", child, requested=.false.)
139 if (associated(child)) then
140 call new_preprocessors(self%preprocess, child, error)
141 if (allocated(error)) return
142 endif
143
144 !> Get optional features list
145 call get_list(table, "features", self%features, error)
146 if (allocated(error)) return
147
148 !> Get optional profile name
149 call get_value(table, "profile", self%profile)
150
151 call get_value(table, "path", uri)
152 if (allocated(uri)) then
153 if (get_os_type() == OS_WINDOWS) uri = windows_path(uri)
154 if (present(root)) uri = join_path(root,uri) ! Relative to the fpm.toml it’s written in
155 call move_alloc(uri, self%path)
156 return
157 end if
158
159 call get_value(table, "git", uri)
160 if (allocated(uri)) then
161 call get_value(table, "tag", value)
162 if (allocated(value)) then
163 self%git = git_target_tag(uri, value)
164 end if
165
166 if (.not. allocated(self%git)) then
167 call get_value(table, "branch", value)
168 if (allocated(value)) then
169 self%git = git_target_branch(uri, value)
170 end if
171 end if
172
173 if (.not. allocated(self%git)) then
174 call get_value(table, "rev", value)
175 if (allocated(value)) then
176 self%git = git_target_revision(uri, value)
177 end if
178 end if
179
180 if (.not. allocated(self%git)) then
181 self%git = git_target_default(uri)
182 end if
183 return
184 end if
185
186 end subroutine new_dependency
187
188 !> Check local schema for allowed entries
189 subroutine check(table, error)
190
191 !> Instance of the TOML data structure
192 type(toml_table), intent(inout) :: table
193
194 !> Error handling
195 type(error_t), allocatable, intent(out) :: error
196
197 character(len=:), allocatable :: name
198 type(string_t), allocatable :: string_list(:)
199 type(toml_key), allocatable :: list(:)
200 type(toml_table), pointer :: child
201
202 !> List of valid keys for the dependency table.
203 character(*), dimension(*), parameter :: valid_keys = [character(24) :: &
204 & "namespace", &
205 "v", &
206 "path", &
207 "git", &
208 "tag", &
209 "branch", &
210 "rev", &
211 "preprocess", &
212 "features", &
213 "profile" &
214 & ]
215
216 call table%get_key(name)
217 call table%get_keys(list)
218
219 if (size(list) < 1) then
220 call syntax_error(error, "Dependency '"//name//"' does not provide sufficient entries")
221 return
222 end if
223
224 call check_keys(table, valid_keys, error)
225 if (allocated(error)) return
226
227 if (table%has_key("path") .and. table%has_key("git")) then
228 call syntax_error(error, "Dependency '"//name//"' cannot have both git and path entries")
229 return
230 end if
231
232 if ((table%has_key("branch") .and. table%has_key("rev")) .or. &
233 (table%has_key("branch") .and. table%has_key("tag")) .or. &
234 (table%has_key("rev") .and. table%has_key("tag"))) then
235 call syntax_error(error, "Dependency '"//name//"' can only have one of branch, rev or tag present")
236 return
237 end if
238
239 if ((table%has_key("branch") .or. table%has_key("tag") .or. table%has_key("rev")) &
240 .and. .not. table%has_key("git")) then
241 call syntax_error(error, "Dependency '"//name//"' has git identifier but no git url")
242 return
243 end if
244
245 if (.not. table%has_key("path") .and. .not. table%has_key("git") &
246 .and. .not. table%has_key("namespace")) then
247 call syntax_error(error, "Please provide a 'namespace' for dependency '"//name// &
248 & "' if it is not a local path or git repository")
249 return
250 end if
251
252 if (table%has_key('v') .and. (table%has_key('path') .or. table%has_key('git'))) then
253 call syntax_error(error, "Dependency '"//name//"' cannot have both v and git/path entries")
254 return
255 end if
256
257 ! Check preprocess key
258 if (table%has_key('preprocess')) then
259
260 call get_value(table, 'preprocess', child)
261
262 if (.not.associated(child)) then
263 call syntax_error(error, "Dependency '"//name//"' has invalid 'preprocess' entry")
264 return
265 end if
266
267 end if
268
269 ! Check that features and profile are not both specified
270 if (table%has_key('features') .and. table%has_key('profile')) then
271 call syntax_error(error, "Dependency '"//name//"' cannot have both 'features' and 'profile' entries")
272 return
273 end if
274
275 end subroutine check
276
277 !> Construct new dependency array from a TOML data structure
278 subroutine new_dependencies(deps, table, root, meta, error)
279
280 !> Instance of the dependency configuration
281 type(dependency_config_t), allocatable, intent(out) :: deps(:)
282
283 !> (optional) metapackages
284 type(metapackage_config_t), optional, intent(out) :: meta
285
286 !> Instance of the TOML data structure
287 type(toml_table), intent(inout) :: table
288
289 !> Root directory of the manifest
290 character(*), intent(in), optional :: root
291
292 !> Error handling
293 type(error_t), allocatable, intent(out) :: error
294
295 type(toml_table), pointer :: node
296 type(toml_key), allocatable :: list(:)
297 type(dependency_config_t), allocatable :: all_deps(:)
298 type(metapackage_request_t) :: meta_request
299 logical, allocatable :: is_meta(:)
300 logical :: metapackages_allowed
301 integer :: idep, stat, ndep
302
303 call table%get_keys(list)
304 ! An empty table is okay
305 if (size(list) < 1) return
306
307 !> Flag dependencies that should be treated as metapackages
308 metapackages_allowed = present(meta)
309 allocate(is_meta(size(list)),source=.false.)
310 allocate(all_deps(size(list)))
311
312 !> Parse all meta- and non-metapackage dependencies
313 do idep = 1, size(list)
314
315 ! Check if this is a standard dependency node
316 call get_value(table, list(idep)%key, node, stat=stat)
317 is_standard_dependency: if (stat /= toml_stat%success) then
318
319 ! See if it can be a valid metapackage name
320 call new_meta_request(meta_request, list(idep)%key, table, error=error)
321
322 !> Neither a standard dep nor a metapackage
323 if (allocated(error)) then
324 call syntax_error(error, "Dependency "//list(idep)%key//" is not a valid metapackage or a table entry")
325 return
326 endif
327
328 !> Valid meta dependency
329 is_meta(idep) = .true.
330
331 else
332
333 ! Parse as a standard dependency
334 is_meta(idep) = .false.
335
336 call new_dependency(all_deps(idep), node, root, error)
337 if (allocated(error)) return
338
339 end if is_standard_dependency
340
341 end do
342
343 ! Non-meta dependencies
344 ndep = count(.not.is_meta)
345
346 ! Finalize standard dependencies
347 allocate(deps(ndep))
348 ndep = 0
349 do idep = 1, size(list)
350 if (is_meta(idep)) cycle
351 ndep = ndep+1
352 deps(ndep) = all_deps(idep)
353 end do
354
355 ! Finalize meta dependencies
356 if (metapackages_allowed) call new_meta_config(meta,table,is_meta,error)
357
358 end subroutine new_dependencies
359
360 !> Write information on instance
361 subroutine info(self, unit, verbosity)
362
363 !> Instance of the dependency configuration
364 class(dependency_config_t), intent(in) :: self
365
366 !> Unit for IO
367 integer, intent(in) :: unit
368
369 !> Verbosity of the printout
370 integer, intent(in), optional :: verbosity
371
372 integer :: pr, ilink
373 character(len=*), parameter :: fmt = '("#", 1x, a, t30, a)'
374
375 if (present(verbosity)) then
376 pr = verbosity
377 else
378 pr = 1
379 end if
380
381 write (unit, fmt) "Dependency"
382 if (allocated(self%name)) then
383 write (unit, fmt) "- name", self%name
384 end if
385
386 if (allocated(self%git)) then
387 write (unit, fmt) "- kind", "git"
388 call self%git%info(unit, pr - 1)
389 end if
390
391 if (allocated(self%path)) then
392 write (unit, fmt) "- kind", "local"
393 write (unit, fmt) "- path", self%path
394 end if
395
396 if (allocated(self%features)) then
397 write(unit, fmt) " - features"
398 do ilink = 1, size(self%features)
399 write(unit, fmt) " - " // self%features(ilink)%s
400 end do
401 end if
402
403 if (allocated(self%profile)) then
404 write(unit, fmt) " - profile", self%profile
405 end if
406
407 end subroutine info
408
409 !> Check if two dependency configurations are different
410 logical function manifest_has_changed(cached, manifest, verbosity, iunit) result(has_changed)
411
412 !> Two instances of the dependency configuration
413 class(dependency_config_t), intent(in) :: cached, manifest
414
415 !> Log verbosity
416 integer, intent(in) :: verbosity, iunit
417
418 has_changed = .true.
419
420 !> Perform all checks
421 if (allocated(cached%git).neqv.allocated(manifest%git)) then
422 if (verbosity>1) write(iunit,out_fmt) "GIT presence has changed. "
423 return
424 endif
425 if (allocated(cached%git)) then
426 if (.not.git_matches_manifest(cached%git,manifest%git,verbosity,iunit)) return
427 end if
428
429 !> All checks passed! The two instances are equal
430 has_changed = .false.
431
432 end function manifest_has_changed
433
434 !> Clean memory
435 elemental subroutine dependency_destroy(self)
436 class(dependency_config_t), intent(inout) :: self
437
438 if (allocated(self%name)) deallocate(self%name)
439 if (allocated(self%path)) deallocate(self%path)
440 if (allocated(self%namespace)) deallocate(self%namespace)
441 if (allocated(self%requested_version)) deallocate(self%requested_version)
442 if (allocated(self%git)) deallocate(self%git)
443 if (allocated(self%features)) deallocate(self%features)
444 if (allocated(self%profile)) deallocate(self%profile)
445
446 end subroutine dependency_destroy
447
448 !> Check that two dependency configs are equal
449 logical function dependency_is_same(this,that)
450 class(dependency_config_t), intent(in) :: this
451 class(serializable_t), intent(in) :: that
452
453 dependency_is_same = .false.
454
455 select type (other=>that)
456 type is (dependency_config_t)
457
458 if (allocated(this%name).neqv.allocated(other%name)) return
459 if (allocated(this%name)) then
460 if (.not.(this%name==other%name)) return
461 endif
462 if (allocated(this%path).neqv.allocated(other%path)) return
463 if (allocated(this%path)) then
464 if (.not.(this%path==other%path)) return
465 endif
466 if (allocated(this%namespace).neqv.allocated(other%namespace)) return
467 if (allocated(this%namespace)) then
468 if (.not.(this%namespace==other%namespace)) return
469 endif
470 if (allocated(this%requested_version).neqv.allocated(other%requested_version)) return
471 if (allocated(this%requested_version)) then
472 if (.not.(this%requested_version==other%requested_version)) return
473 endif
474 if (allocated(this%features).neqv.allocated(other%features)) return
475 if (allocated(this%features)) then
476 if (.not.(this%features==other%features)) return
477 endif
478 if (allocated(this%profile).neqv.allocated(other%profile)) return
479 if (allocated(this%profile)) then
480 if (.not.(this%profile==other%profile)) return
481 endif
482
483 if ((allocated(this%git).neqv.allocated(other%git))) return
484 if (allocated(this%git)) then
485 if (.not.(this%git==other%git)) return
486 endif
487
488 class default
489 ! Not the same type
490 return
491 end select
492
493 !> All checks passed!
494 dependency_is_same = .true.
495
496 end function dependency_is_same
497
498 !> Dump dependency to toml table
499 subroutine dump_to_toml(self, table, error)
500
501 !> Instance of the serializable object
502 class(dependency_config_t), intent(inout) :: self
503
504 !> Data structure
505 type(toml_table), intent(inout) :: table
506
507 !> Error handling
508 type(toml_table), pointer :: ptr
509 type(error_t), allocatable, intent(out) :: error
510
511 integer :: ierr
512
513 call set_string(table, "name", self%name, error, 'dependency_config_t')
514 if (allocated(error)) return
515 call set_string(table, "path", self%path, error, 'dependency_config_t')
516 if (allocated(error)) return
517 call set_string(table, "namespace", self%namespace, error, 'dependency_config_t')
518 if (allocated(error)) return
519 if (allocated(self%requested_version)) then
520 call set_string(table, "requested_version", self%requested_version%s(), error, 'dependency_config_t')
521 if (allocated(error)) return
522 endif
523 call set_list(table, "features", self%features, error)
524 if (allocated(error)) return
525 call set_string(table, "profile", self%profile, error, 'dependency_config_t')
526 if (allocated(error)) return
527
528 if (allocated(self%git)) then
529 call add_table(table, "git", ptr, error)
530 if (allocated(error)) return
531 call self%git%dump_to_toml(ptr, error)
532 if (allocated(error)) return
533 endif
534
535 end subroutine dump_to_toml
536
537 !> Read dependency from toml table (no checks made at this stage)
538 subroutine load_from_toml(self, table, error)
539
540 !> Instance of the serializable object
541 class(dependency_config_t), intent(inout) :: self
542
543 !> Data structure
544 type(toml_table), intent(inout) :: table
545
546 !> Error handling
547 type(error_t), allocatable, intent(out) :: error
548
549 !> Local variables
550 type(toml_key), allocatable :: list(:)
551 type(toml_table), pointer :: ptr
552 character(len=:), allocatable :: requested_version
553 integer :: ierr,ii
554
555 call dependency_destroy(self)
556
557 call get_value(table, "name", self%name)
558 call get_value(table, "path", self%path)
559 call get_value(table, "namespace", self%namespace)
560 call get_value(table, "requested_version", requested_version)
561 if (allocated(requested_version)) then
562 allocate(self%requested_version)
563 call new_version(self%requested_version, requested_version, error)
564 if (allocated(error)) then
565 error%message = 'dependency_config_t: version error from TOML table - '//error%message
566 return
567 endif
568 end if
569 call get_list(table, "features", self%features, error)
570 if (allocated(error)) return
571 call get_value(table, "profile", self%profile)
572
573 call table%get_keys(list)
574 add_git: do ii = 1, size(list)
575 if (list(ii)%key=="git") then
576 call get_value(table, list(ii)%key, ptr, stat=ierr)
577 if (ierr /= toml_stat%success) then
578 call fatal_error(error,'dependency_config_t: cannot retrieve git from TOML table')
579 exit
580 endif
581 allocate(self%git)
582 call self%git%load_from_toml(ptr, error)
583 if (allocated(error)) return
584 exit add_git
585 end if
586 end do add_git
587
588 end subroutine load_from_toml
589
590 !> Reallocate a list of dependencies
591 pure subroutine resize_dependency_config(var, n)
592 !> Instance of the array to be resized
593 type(dependency_config_t), allocatable, intent(inout) :: var(:)
594 !> Dimension of the final array size
595 integer, intent(in), optional :: n
596
597 type(dependency_config_t), allocatable :: tmp(:)
598 integer :: this_size, new_size
599 integer, parameter :: initial_size = 16
600
601 if (allocated(var)) then
602 this_size = size(var, 1)
603 call move_alloc(var, tmp)
604 else
605 this_size = initial_size
606 end if
607
608 if (present(n)) then
609 new_size = n
610 else
611 new_size = this_size + this_size/2 + 1
612 end if
613
614 allocate (var(new_size))
615
616 if (allocated(tmp)) then
617 this_size = min(size(tmp, 1), size(var, 1))
618 var(:this_size) = tmp(:this_size)
619 deallocate (tmp)
620 end if
621
622 end subroutine resize_dependency_config
623
624 subroutine add_preprocess(dep, preprocess)
625 !> Instance of the dependency config
626 class(dependency_config_t), intent(inout) :: dep
627 !> Instance of the preprocessor configuration
628 type(preprocess_config_t), intent(in) :: preprocess
629
630 integer :: i,n
631 type(preprocess_config_t), allocatable :: new_preprocess(:)
632
633 if (allocated(dep%preprocess)) then
634
635 n = size(dep%preprocess)
636
637 if (n<1) then
638 deallocate(dep%preprocess)
639 allocate(dep%preprocess(1),source=preprocess)
640 else
641
642 find_similar: do i=1,n
643 if (dep%preprocess(i)%name==dep%name) then
644 call dep%preprocess(i)%add_config(preprocess)
645 return
646 end if
647 end do find_similar
648
649 ! Similar preprocessor config not found: add a new one
650 allocate(new_preprocess(n+1))
651 new_preprocess(1:n) = dep%preprocess
652 new_preprocess(n+1) = preprocess
653 call move_alloc(from=new_preprocess,to=dep%preprocess)
654
655 end if
656 else
657
658 ! Copy configuration
659 allocate(dep%preprocess(1),source=preprocess)
660
661 end if
662
663 end subroutine add_preprocess
664
665
666 end module fpm_manifest_dependency