Debian Codegraph @

src/fpm/manifest/feature.f90

raw
1 !> Implementation of the meta data for features.
2 !>
3 !> A feature is a configurable set of package properties that can be
4 !> conditionally enabled. Features allow fine-grained control over
5 !> dependencies, compiler flags, preprocessor definitions, and other
6 !> package components based on the target compiler and operating system.
7 !>
8 !> Features are similar to Rust Cargo features but adapted for Fortran
9 !> package management. Each feature can specify:
10 !> - Compiler-specific flags and settings
11 !> - Additional dependencies
12 !> - Preprocessor definitions
13 !> - Source files and build configurations
14 !>
15 !> A feature table can currently have the following fields:
16 !>
17 !>```toml
18 !>[features.mpi]
19 !>description = "Enable MPI parallel support"
20 !>compiler = "gfortran"
21 !>os = "linux"
22 !>flags = "-fopenmp"
23 !>preprocessor = ["WITH_MPI"]
24 !>[features.mpi.dependencies]
25 !>mpi = { git = "https://github.com/fortran-lang/mpi" }
26 !>```
27 !>
28 module fpm_manifest_feature
29 use fpm_manifest_build, only: build_config_t, new_build_config
30 use fpm_manifest_dependency, only: dependency_config_t, new_dependencies
31 use fpm_manifest_example, only: example_config_t, new_example
32 use fpm_manifest_executable, only: executable_config_t, new_executable
33 use fpm_manifest_fortran, only: fortran_config_t, new_fortran_config
34 use fpm_manifest_library, only: library_config_t, new_library
35 use fpm_manifest_install, only: install_config_t, new_install_config
36 use fpm_manifest_test, only: test_config_t, new_test
37 use fpm_manifest_preprocess, only: preprocess_config_t, new_preprocessors
38 use fpm_manifest_metapackages, only: metapackage_config_t, new_meta_config
39 use fpm_manifest_platform, only: platform_config_t
40 use fpm_error, only: error_t, fatal_error, syntax_error
41 use fpm_environment, only: OS_UNKNOWN, OS_LINUX, OS_MACOS, OS_WINDOWS, OS_CYGWIN, OS_SOLARIS, &
42 OS_FREEBSD, OS_OPENBSD, OS_ALL, OS_NAME, match_os_type
43 use fpm_compiler, only: compiler_enum, compiler_id_name, match_compiler_type, id_all
44 use fpm_strings, only: string_t, lower, operator(==)
45 use tomlf, only: toml_table, toml_array, toml_key, toml_stat
46 use fpm_toml, only: get_value, len, serializable_t, set_value, set_string, set_list, add_table, &
47 get_list
48 implicit none
49 private
50
51 public :: feature_config_t, new_feature, new_features, find_feature, init_feature_components, &
52 unique_programs
53
54 !> Feature configuration data
55 type, extends(serializable_t) :: feature_config_t
56
57 !> Feature identity
58 character(len=:), allocatable :: name
59 character(len=:), allocatable :: description
60
61 !> Compiler/OS targeting (consistent with profile_config_t pattern)
62 type(platform_config_t) :: platform
63
64 !> Build configuration
65 type(build_config_t), allocatable :: build
66
67 !> Installation configuration
68 type(install_config_t), allocatable :: install
69
70 !> Fortran configuration
71 type(fortran_config_t), allocatable :: fortran
72
73 !> Library configuration
74 type(library_config_t), allocatable :: library
75
76 !> Executable configurations
77 type(executable_config_t), allocatable :: executable(:)
78
79 !> Dependencies
80 type(dependency_config_t), allocatable :: dependency(:)
81
82 !> Development dependencies
83 type(dependency_config_t), allocatable :: dev_dependency(:)
84
85 !> Examples
86 type(example_config_t), allocatable :: example(:)
87
88 !> Tests
89 type(test_config_t), allocatable :: test(:)
90
91 !> Preprocessor configuration
92 type(preprocess_config_t), allocatable :: preprocess(:)
93
94 !> Metapackage data
95 type(metapackage_config_t) :: meta
96
97 !> Compiler flags
98 character(len=:), allocatable :: flags
99 character(len=:), allocatable :: c_flags
100 character(len=:), allocatable :: cxx_flags
101 character(len=:), allocatable :: link_time_flags
102
103 !> Feature dependencies (not active yet)
104 type(string_t), allocatable :: requires_features(:)
105
106 contains
107
108 !> Print information on this instance
109 procedure :: info
110
111 !> Check validity of the TOML table
112 procedure, nopass :: check
113
114 !> Get manifest name
115 procedure :: manifest_name
116
117 !> Check if there is a cpp configuration
118 procedure :: has_cpp
119
120 !> Serialization interface
121 procedure :: serializable_is_same => feature_is_same
122 procedure :: dump_to_toml
123 procedure :: load_from_toml
124
125 end type feature_config_t
126
127 character(len=*), parameter, private :: class_name = 'feature_config_t'
128
129 interface unique_programs
130 module procedure :: unique_programs1
131 module procedure :: unique_programs2
132 end interface unique_programs
133
134 contains
135
136 !> Construct a new feature configuration from a TOML data structure
137 subroutine new_feature(self, table, root, error, name)
138
139 !> Instance of the feature configuration
140 type(feature_config_t), intent(out) :: self
141
142 !> Instance of the TOML data structure
143 type(toml_table), intent(inout) :: table
144
145 !> Root directory of the manifest
146 character(len=*), intent(in), optional :: root
147
148 !> Error handling
149 type(error_t), allocatable, intent(out) :: error
150
151 !> Optional name override (if not provided, gets from table key)
152 character(len=*), intent(in), optional :: name
153
154 type(toml_table), pointer :: child, node
155 type(toml_array), pointer :: children
156 character(len=:), allocatable :: compiler_name, os_name
157 integer :: ii, nn, stat
158
159 ! Only check schema for pure features (not when called from package)
160 if (.not. present(name)) then
161 call check(table, error)
162 if (allocated(error)) return
163 end if
164
165 ! Get feature name from parameter or table key
166 if (present(name)) then
167 self%name = name
168 else
169 call table%get_key(self%name)
170 end if
171
172 ! Initialize common components
173 call init_feature_components(self, table, root=root, error=error)
174 if (allocated(error)) return
175
176 ! For features, get platform configuration (optional for packages)
177 if (.not. present(name)) then
178 call get_value(table, "platform", child, requested=.false., stat=stat)
179 if (stat == toml_stat%success .and. associated(child)) then
180 call self%platform%load_from_toml(child, error)
181 if (allocated(error)) return
182 end if
183 end if
184
185 end subroutine new_feature
186
187 !> Check local schema for allowed entries
188 subroutine check(table, error)
189
190 !> Instance of the TOML data structure
191 type(toml_table), intent(inout) :: table
192
193 !> Error handling
194 type(error_t), allocatable, intent(out) :: error
195
196 type(toml_key), allocatable :: list(:)
197 integer :: ikey
198
199 call table%get_keys(list)
200
201 if (size(list) < 1) then
202 call syntax_error(error, "Feature table is empty")
203 return
204 end if
205
206 do ikey = 1, size(list)
207 select case(list(ikey)%key)
208 case default
209 call syntax_error(error, "Key "//list(ikey)%key//" is not allowed in feature table")
210 exit
211
212 ! Keys
213 case("description", "platform", "flags", "c-flags", &
214 "cxx-flags", "link-time-flags", "preprocessor", "requires", &
215 "build", "install", "fortran", "library", "dependencies", &
216 "dev-dependencies", "executable", "example", "test", "preprocess")
217
218 continue
219
220 ! OS names (lowercase)
221 case("linux", "macos", "windows", "cygwin", "solaris", "freebsd", "openbsd")
222
223 continue
224
225 ! Compiler names
226 case ("gfortran", "f95", "caf", "ifort", "ifx", "pgfortran", "nvfortran", "nagfor", &
227 "flang", "flang-new", "f18", "xlf90", "lfortran")
228
229 continue
230
231 ! Standard feature configuration names
232 case("debug", "release")
233
234 continue
235
236 end select
237 end do
238
239 end subroutine check
240
241 !> Construct new feature array from a TOML data structure
242 subroutine new_features(features, table, root, error)
243
244 !> Instance of the feature configuration array
245 type(feature_config_t), allocatable, intent(out) :: features(:)
246
247 !> Instance of the TOML data structure
248 type(toml_table), intent(inout) :: table
249
250 !> Root directory of the manifest
251 character(len=*), intent(in), optional :: root
252
253 !> Error handling
254 type(error_t), allocatable, intent(out) :: error
255
256 type(toml_table), pointer :: node
257 type(toml_key), allocatable :: list(:)
258 integer :: ifeature, stat
259
260 call table%get_keys(list)
261
262 if (size(list) < 1) then
263 allocate(features(0))
264 return
265 end if
266
267 allocate(features(size(list)))
268
269 do ifeature = 1, size(list)
270 call get_value(table, list(ifeature)%key, node, stat=stat)
271 if (stat /= toml_stat%success) then
272 call fatal_error(error, "Feature "//list(ifeature)%key//" must be a table entry")
273 exit
274 end if
275 call new_feature(features(ifeature), node, root, error)
276 if (allocated(error)) exit
277 end do
278
279 end subroutine new_features
280
281 !> Find matching feature configuration (similar to find_profile)
282 subroutine find_feature(features, feature_name, current_platform, found, chosen_feature)
283 type(feature_config_t), allocatable, intent(in) :: features(:)
284 character(*), intent(in) :: feature_name
285 type(platform_config_t), intent(in) :: current_platform
286 logical, intent(out) :: found
287 type(feature_config_t), intent(out) :: chosen_feature
288
289 integer :: i
290
291 found = .false.
292 if (size(features) < 1) return
293
294 ! Try to find exact match (feature + compiler + OS)
295 do i = 1, size(features)
296 if (features(i)%name == feature_name .and. &
297 features(i)%platform%matches(current_platform)) then
298 chosen_feature = features(i)
299 found = .true.
300 return
301 end if
302 end do
303
304 ! Try to find compiler match with OS_ALL
305 do i = 1, size(features)
306 if (features(i)%name == feature_name .and. &
307 features(i)%platform%matches(current_platform)) then
308 chosen_feature = features(i)
309 found = .true.
310 return
311 end if
312 end do
313
314 ! Try to find COMPILER_ALL match
315 do i = 1, size(features)
316 if (features(i)%name == feature_name .and. &
317 features(i)%platform%matches(current_platform)) then
318 chosen_feature = features(i)
319 found = .true.
320 return
321 end if
322 end do
323 end subroutine find_feature
324
325 !> Write information on instance
326 subroutine info(self, unit, verbosity)
327
328 !> Instance of the feature configuration
329 class(feature_config_t), intent(in) :: self
330
331 !> Unit for IO
332 integer, intent(in) :: unit
333
334 !> Verbosity of the printout
335 integer, intent(in), optional :: verbosity
336
337 integer :: pr, ii
338 character(len=*), parameter :: fmt = '("#", 1x, a, t30, a)', &
339 & fmti = '("#", 1x, a, t30, i0)'
340
341 if (present(verbosity)) then
342 pr = verbosity
343 else
344 pr = 1
345 end if
346
347 if (pr < 1) return
348
349 write(unit, fmt) "Feature"
350 if (allocated(self%name)) then
351 write(unit, fmt) "- name", self%name
352 end if
353 if (allocated(self%description)) then
354 write(unit, fmt) "- description", self%description
355 end if
356
357 call self%platform%info(unit, verbosity)
358
359 if (allocated(self%flags)) then
360 write(unit, fmt) "- flags", self%flags
361 end if
362 if (allocated(self%c_flags)) then
363 write(unit, fmt) "- c-flags", self%c_flags
364 end if
365 if (allocated(self%cxx_flags)) then
366 write(unit, fmt) "- cxx-flags", self%cxx_flags
367 end if
368 if (allocated(self%link_time_flags)) then
369 write(unit, fmt) "- link-time-flags", self%link_time_flags
370 end if
371
372 if (allocated(self%build)) then
373 call self%build%info(unit, pr - 1)
374 end if
375 if (allocated(self%install)) then
376 call self%install%info(unit, pr - 1)
377 end if
378
379 if (allocated(self%library)) then
380 write(unit, fmt) "- target", "archive"
381 call self%library%info(unit, pr - 1)
382 end if
383
384 if (allocated(self%executable)) then
385 if (size(self%executable) > 1 .or. pr > 2) then
386 write(unit, fmti) "- executables", size(self%executable)
387 end if
388 do ii = 1, size(self%executable)
389 call self%executable(ii)%info(unit, pr - 1)
390 end do
391 end if
392
393 if (allocated(self%dependency)) then
394 if (size(self%dependency) > 1 .or. pr > 2) then
395 write(unit, fmti) "- dependencies", size(self%dependency)
396 end if
397 do ii = 1, size(self%dependency)
398 call self%dependency(ii)%info(unit, pr - 1)
399 end do
400 end if
401
402 end subroutine info
403
404 !> Check that two feature configs are equal
405 logical function feature_is_same(this, that)
406 class(feature_config_t), intent(in) :: this
407 class(serializable_t), intent(in) :: that
408
409 integer :: ii
410
411 feature_is_same = .false.
412
413 select type (other=>that)
414 type is (feature_config_t)
415
416 if (allocated(this%name).neqv.allocated(other%name)) return
417 if (allocated(this%name)) then
418 if (.not.(this%name==other%name)) return
419 end if
420
421 if (allocated(this%description).neqv.allocated(other%description)) return
422 if (allocated(this%description)) then
423 if (.not.(this%description==other%description)) return
424 end if
425
426 if (.not.this%platform == other%platform) return
427
428 if (allocated(this%build).neqv.allocated(other%build)) return
429 if (allocated(this%build)) then
430 if (.not.(this%build==other%build)) return
431 end if
432
433 if (allocated(this%install).neqv.allocated(other%install)) return
434 if (allocated(this%install)) then
435 if (.not.(this%install==other%install)) return
436 end if
437
438 if (allocated(this%fortran).neqv.allocated(other%fortran)) return
439 if (allocated(this%fortran)) then
440 if (.not.(this%fortran==other%fortran)) return
441 end if
442
443 if (allocated(this%library).neqv.allocated(other%library)) return
444 if (allocated(this%library)) then
445 if (.not.(this%library==other%library)) return
446 end if
447
448 if (allocated(this%executable).neqv.allocated(other%executable)) return
449 if (allocated(this%executable)) then
450 if (.not.(size(this%executable)==size(other%executable))) return
451 do ii = 1, size(this%executable)
452 if (.not.(this%executable(ii)==other%executable(ii))) return
453 end do
454 end if
455
456 if (allocated(this%dependency).neqv.allocated(other%dependency)) return
457 if (allocated(this%dependency)) then
458 if (.not.(size(this%dependency)==size(other%dependency))) return
459 do ii = 1, size(this%dependency)
460 if (.not.(this%dependency(ii)==other%dependency(ii))) return
461 end do
462 end if
463
464 if (allocated(this%dev_dependency).neqv.allocated(other%dev_dependency)) return
465 if (allocated(this%dev_dependency)) then
466 if (.not.(size(this%dev_dependency)==size(other%dev_dependency))) return
467 do ii = 1, size(this%dev_dependency)
468 if (.not.(this%dev_dependency(ii)==other%dev_dependency(ii))) return
469 end do
470 end if
471
472 if (allocated(this%example).neqv.allocated(other%example)) return
473 if (allocated(this%example)) then
474 if (.not.(size(this%example)==size(other%example))) return
475 do ii = 1, size(this%example)
476 if (.not.(this%example(ii)==other%example(ii))) return
477 end do
478 end if
479
480 if (allocated(this%test).neqv.allocated(other%test)) return
481 if (allocated(this%test)) then
482 if (.not.(size(this%test)==size(other%test))) return
483 do ii = 1, size(this%test)
484 if (.not.(this%test(ii)==other%test(ii))) return
485 end do
486 end if
487
488 if (allocated(this%preprocess).neqv.allocated(other%preprocess)) return
489 if (allocated(this%preprocess)) then
490 if (.not.(size(this%preprocess)==size(other%preprocess))) return
491 do ii = 1, size(this%preprocess)
492 if (.not.(this%preprocess(ii)==other%preprocess(ii))) return
493 end do
494 end if
495
496 if (allocated(this%flags).neqv.allocated(other%flags)) return
497 if (allocated(this%flags)) then
498 if (.not.(this%flags==other%flags)) return
499 end if
500
501 if (allocated(this%c_flags).neqv.allocated(other%c_flags)) return
502 if (allocated(this%c_flags)) then
503 if (.not.(this%c_flags==other%c_flags)) return
504 end if
505
506 if (allocated(this%cxx_flags).neqv.allocated(other%cxx_flags)) return
507 if (allocated(this%cxx_flags)) then
508 if (.not.(this%cxx_flags==other%cxx_flags)) return
509 end if
510
511 if (allocated(this%link_time_flags).neqv.allocated(other%link_time_flags)) return
512 if (allocated(this%link_time_flags)) then
513 if (.not.(this%link_time_flags==other%link_time_flags)) return
514 end if
515
516 if (allocated(this%requires_features).neqv.allocated(other%requires_features)) return
517 if (allocated(this%requires_features)) then
518 if (.not.(size(this%requires_features)==size(other%requires_features))) return
519 do ii = 1, size(this%requires_features)
520 if (.not.(this%requires_features(ii)==other%requires_features(ii))) return
521 end do
522 end if
523
524 if (.not.this%meta==other%meta) return
525
526 class default
527 return
528 end select
529
530 feature_is_same = .true.
531
532 end function feature_is_same
533
534 !> Dump feature to toml table
535 subroutine dump_to_toml(self, table, error)
536
537 !> Instance of the serializable object
538 class(feature_config_t), intent(inout) :: self
539
540 !> Data structure
541 type(toml_table), intent(inout) :: table
542
543 !> Error handling
544 type(error_t), allocatable, intent(out) :: error
545
546 integer :: ii
547 type(toml_table), pointer :: ptr, ptr_pkg
548 character(30) :: unnamed
549
550 call set_string(table, "name", self%name, error, class_name)
551 if (allocated(error)) return
552 call set_string(table, "description", self%description, error, class_name)
553 if (allocated(error)) return
554
555 call add_table(table, "platform", ptr, error, class_name)
556 if (allocated(error)) return
557 call self%platform%dump_to_toml(ptr, error)
558 if (allocated(error)) return
559
560 call set_string(table, "flags", self%flags, error, class_name)
561 if (allocated(error)) return
562 call set_string(table, "c-flags", self%c_flags, error, class_name)
563 if (allocated(error)) return
564 call set_string(table, "cxx-flags", self%cxx_flags, error, class_name)
565 if (allocated(error)) return
566 call set_string(table, "link-time-flags", self%link_time_flags, error, class_name)
567 if (allocated(error)) return
568
569 call set_list(table, "requires", self%requires_features, error)
570 if (allocated(error)) return
571
572 if (allocated(self%build)) then
573 call add_table(table, "build", ptr, error, class_name)
574 if (allocated(error)) return
575 call self%build%dump_to_toml(ptr, error)
576 if (allocated(error)) return
577 end if
578
579 if (allocated(self%install)) then
580 call add_table(table, "install", ptr, error, class_name)
581 if (allocated(error)) return
582 call self%install%dump_to_toml(ptr, error)
583 if (allocated(error)) return
584 end if
585
586 if (allocated(self%fortran)) then
587 call add_table(table, "fortran", ptr, error, class_name)
588 if (allocated(error)) return
589 call self%fortran%dump_to_toml(ptr, error)
590 if (allocated(error)) return
591 end if
592
593 if (allocated(self%library)) then
594 call add_table(table, "library", ptr, error, class_name)
595 if (allocated(error)) return
596 call self%library%dump_to_toml(ptr, error)
597 if (allocated(error)) return
598 end if
599
600 if (allocated(self%executable)) then
601 call add_table(table, "executable", ptr_pkg)
602 if (.not. associated(ptr_pkg)) then
603 call fatal_error(error, class_name//" cannot create 'executable' table ")
604 return
605 end if
606
607 do ii = 1, size(self%executable)
608 associate (pkg => self%executable(ii))
609
610 !> Because dependencies are named, fallback if this has no name
611 !> So, serialization will work regardless of size(self%dep) == self%ndep
612
613 if (len_trim(pkg%name)==0) then
614 write(unnamed,1) 'EXECUTABLE',ii
615 call add_table(ptr_pkg, trim(unnamed), ptr, error, class_name//'(executable)')
616 else
617 call add_table(ptr_pkg, pkg%name, ptr, error, class_name//'(executable)')
618 end if
619 if (allocated(error)) return
620 call pkg%dump_to_toml(ptr, error)
621 if (allocated(error)) return
622 end associate
623 end do
624 end if
625
626 if (allocated(self%dependency)) then
627 call add_table(table, "dependencies", ptr_pkg)
628 if (.not. associated(ptr_pkg)) then
629 call fatal_error(error, class_name//" cannot create 'dependencies' table ")
630 return
631 end if
632
633 do ii = 1, size(self%dependency)
634 associate (pkg => self%dependency(ii))
635 if (len_trim(pkg%name)==0) then
636 write(unnamed,1) 'DEPENDENCY',ii
637 call add_table(ptr_pkg, trim(unnamed), ptr, error, class_name//'(dependencies)')
638 else
639 call add_table(ptr_pkg, pkg%name, ptr, error, class_name//'(dependencies)')
640 end if
641 if (allocated(error)) return
642 call pkg%dump_to_toml(ptr, error)
643 if (allocated(error)) return
644 end associate
645 end do
646 end if
647
648 if (allocated(self%example)) then
649
650 call add_table(table, "example", ptr_pkg)
651 if (.not. associated(ptr_pkg)) then
652 call fatal_error(error, class_name//" cannot create 'example' table ")
653 return
654 end if
655
656 do ii = 1, size(self%example)
657
658 associate (pkg => self%example(ii))
659
660 !> Because dependencies are named, fallback if this has no name
661 !> So, serialization will work regardless of size(self%dep) == self%ndep
662 if (len_trim(pkg%name)==0) then
663 write(unnamed,1) 'EXAMPLE',ii
664 call add_table(ptr_pkg, trim(unnamed), ptr, error, class_name//'(example)')
665 else
666 call add_table(ptr_pkg, pkg%name, ptr, error, class_name//'(example)')
667 end if
668 if (allocated(error)) return
669 call pkg%dump_to_toml(ptr, error)
670 if (allocated(error)) return
671
672 end associate
673
674 end do
675 end if
676
677 if (allocated(self%test)) then
678
679 call add_table(table, "test", ptr_pkg)
680 if (.not. associated(ptr_pkg)) then
681 call fatal_error(error, class_name//" cannot create 'test' table ")
682 return
683 end if
684
685 do ii = 1, size(self%test)
686
687 associate (pkg => self%test(ii))
688
689 !> Because dependencies are named, fallback if this has no name
690 !> So, serialization will work regardless of size(self%dep) == self%ndep
691 if (len_trim(pkg%name)==0) then
692 write(unnamed,1) 'TEST',ii
693 call add_table(ptr_pkg, trim(unnamed), ptr, error, class_name//'(test)')
694 else
695 call add_table(ptr_pkg, pkg%name, ptr, error, class_name//'(test)')
696 end if
697 if (allocated(error)) return
698 call pkg%dump_to_toml(ptr, error)
699 if (allocated(error)) return
700
701 end associate
702
703 end do
704 end if
705
706 if (allocated(self%preprocess)) then
707
708 call add_table(table, "preprocess", ptr_pkg)
709 if (.not. associated(ptr_pkg)) then
710 call fatal_error(error, class_name//" cannot create 'preprocess' table ")
711 return
712 end if
713
714 do ii = 1, size(self%preprocess)
715
716 associate (pkg => self%preprocess(ii))
717
718 !> Because dependencies are named, fallback if this has no name
719 !> So, serialization will work regardless of size(self%dep) == self%ndep
720 if (len_trim(pkg%name)==0) then
721 write(unnamed,1) 'PREPROCESS',ii
722 call add_table(ptr_pkg, trim(unnamed), ptr, error, class_name//'(preprocess)')
723 else
724 call add_table(ptr_pkg, pkg%name, ptr, error, class_name//'(preprocess)')
725 end if
726 if (allocated(error)) return
727 call pkg%dump_to_toml(ptr, error)
728 if (allocated(error)) return
729
730 end associate
731
732 end do
733 end if
734
735 call add_table(table, "metapackages", ptr, error, class_name)
736 if (allocated(error)) return
737 call self%meta%dump_to_toml(ptr, error)
738 if (allocated(error)) return
739
740 1 format('UNNAMED_',a,'_',i0)
741
742 end subroutine dump_to_toml
743
744 !> Read feature from toml table (no checks made at this stage)
745 subroutine load_from_toml(self, table, error)
746
747 !> Instance of the serializable object
748 class(feature_config_t), intent(inout) :: self
749
750 !> Data structure
751 type(toml_table), intent(inout) :: table
752
753 !> Error handling
754 type(error_t), allocatable, intent(out) :: error
755
756 type(toml_key), allocatable :: keys(:), pkg_keys(:)
757 integer :: ii, jj
758 type(toml_table), pointer :: ptr, ptr_pkg
759
760 call table%get_keys(keys)
761
762 call get_value(table, "name", self%name)
763 call get_value(table, "description", self%description)
764
765 call get_value(table, "flags", self%flags)
766 call get_value(table, "c-flags", self%c_flags)
767 call get_value(table, "cxx-flags", self%cxx_flags)
768 call get_value(table, "link-time-flags", self%link_time_flags)
769
770 call get_list(table, "requires", self%requires_features, error)
771 if (allocated(error)) return
772
773 if (allocated(self%executable)) deallocate(self%executable)
774 if (allocated(self%dependency)) deallocate(self%dependency)
775 if (allocated(self%dev_dependency)) deallocate(self%dev_dependency)
776 if (allocated(self%example)) deallocate(self%example)
777 if (allocated(self%test)) deallocate(self%test)
778 if (allocated(self%preprocess)) deallocate(self%preprocess)
779
780 do ii = 1, size(keys)
781 select case (keys(ii)%key)
782 case ("platform")
783
784 call get_value(table, keys(ii), ptr)
785 if (.not.associated(ptr)) then
786 call fatal_error(error,class_name//': error retrieving '//keys(ii)%key//' table')
787 return
788 end if
789 call self%platform%load_from_toml(ptr, error)
790 if (allocated(error)) return
791
792 case ("build")
793 allocate(self%build)
794 call get_value(table, keys(ii), ptr)
795 if (.not.associated(ptr)) then
796 call fatal_error(error,class_name//': error retrieving '//keys(ii)%key//' table')
797 return
798 end if
799 call self%build%load_from_toml(ptr, error)
800 if (allocated(error)) return
801
802 case ("install")
803 allocate(self%install)
804 call get_value(table, keys(ii), ptr)
805 if (.not.associated(ptr)) then
806 call fatal_error(error,class_name//': error retrieving '//keys(ii)%key//' table')
807 return
808 end if
809 call self%install%load_from_toml(ptr, error)
810
811 case ("fortran")
812 allocate(self%fortran)
813 call get_value(table, keys(ii), ptr)
814 if (.not.associated(ptr)) then
815 call fatal_error(error,class_name//': error retrieving '//keys(ii)%key//' table')
816 return
817 end if
818 call self%fortran%load_from_toml(ptr, error)
819
820 case ("library")
821 allocate(self%library)
822 call get_value(table, keys(ii), ptr)
823 if (.not.associated(ptr)) then
824 call fatal_error(error,class_name//': error retrieving '//keys(ii)%key//' table')
825 return
826 end if
827 call self%library%load_from_toml(ptr, error)
828
829 case ("executable")
830
831 call get_value(table, keys(ii), ptr)
832 if (.not.associated(ptr)) then
833 call fatal_error(error,class_name//': error retrieving executable table')
834 return
835 end if
836
837 !> Read all packages
838 call ptr%get_keys(pkg_keys)
839 allocate(self%executable(size(pkg_keys)))
840
841 do jj = 1, size(pkg_keys)
842 call get_value(ptr, pkg_keys(jj), ptr_pkg)
843 call self%executable(jj)%load_from_toml(ptr_pkg, error)
844 if (allocated(error)) return
845 end do
846
847 case ("dependencies")
848
849 call get_value(table, keys(ii), ptr)
850 if (.not.associated(ptr)) then
851 call fatal_error(error,class_name//': error retrieving dependency table')
852 return
853 end if
854
855 !> Read all packages
856 call ptr%get_keys(pkg_keys)
857 allocate(self%dependency(size(pkg_keys)))
858
859 do jj = 1, size(pkg_keys)
860 call get_value(ptr, pkg_keys(jj), ptr_pkg)
861 call self%dependency(jj)%load_from_toml(ptr_pkg, error)
862 if (allocated(error)) return
863 end do
864
865 case ("dev-dependencies")
866
867 call get_value(table, keys(ii), ptr)
868 if (.not.associated(ptr)) then
869 call fatal_error(error,class_name//': error retrieving dev-dependencies table')
870 return
871 end if
872
873 !> Read all packages
874 call ptr%get_keys(pkg_keys)
875 allocate(self%dev_dependency(size(pkg_keys)))
876
877 do jj = 1, size(pkg_keys)
878 call get_value(ptr, pkg_keys(jj), ptr_pkg)
879 call self%dev_dependency(jj)%load_from_toml(ptr_pkg, error)
880 if (allocated(error)) return
881 end do
882
883 case ("example")
884
885 call get_value(table, keys(ii), ptr)
886 if (.not.associated(ptr)) then
887 call fatal_error(error,class_name//': error retrieving example table')
888 return
889 end if
890
891 !> Read all packages
892 call ptr%get_keys(pkg_keys)
893 allocate(self%example(size(pkg_keys)))
894
895 do jj = 1, size(pkg_keys)
896 call get_value(ptr, pkg_keys(jj), ptr_pkg)
897 call self%example(jj)%load_from_toml(ptr_pkg, error)
898 if (allocated(error)) return
899 end do
900
901 case ("test")
902
903 call get_value(table, keys(ii), ptr)
904 if (.not.associated(ptr)) then
905 call fatal_error(error,class_name//': error retrieving test table')
906 return
907 end if
908
909 !> Read all packages
910 call ptr%get_keys(pkg_keys)
911 allocate(self%test(size(pkg_keys)))
912
913 do jj = 1, size(pkg_keys)
914 call get_value(ptr, pkg_keys(jj), ptr_pkg)
915 call self%test(jj)%load_from_toml(ptr_pkg, error)
916 if (allocated(error)) return
917 end do
918
919 case ("preprocess")
920
921 call get_value(table, keys(ii), ptr)
922 if (.not.associated(ptr)) then
923 call fatal_error(error,class_name//': error retrieving preprocess table')
924 return
925 end if
926
927 !> Read all packages
928 call ptr%get_keys(pkg_keys)
929 allocate(self%preprocess(size(pkg_keys)))
930
931 do jj = 1, size(pkg_keys)
932 call get_value(ptr, pkg_keys(jj), ptr_pkg)
933 call self%preprocess(jj)%load_from_toml(ptr_pkg, error)
934 if (allocated(error)) return
935 end do
936
937 case ("metapackages")
938
939 call get_value(table, keys(ii), ptr)
940 if (.not.associated(ptr)) then
941 call fatal_error(error,class_name//': error retrieving '//keys(ii)%key//' table')
942 return
943 end if
944 call self%meta%load_from_toml(ptr, error)
945
946 case default
947 cycle
948 end select
949 end do
950
951 end subroutine load_from_toml
952
953
954 !> Initialize the feature components (shared between new_feature and new_package)
955 subroutine init_feature_components(self, table, platform, root, error)
956 type(feature_config_t), intent(inout) :: self
957 type(toml_table), intent(inout) :: table
958 type(platform_config_t), optional, intent(in) :: platform
959 character(len=*), intent(in), optional :: root
960 type(error_t), allocatable, intent(out) :: error
961
962 type(toml_table), pointer :: child, node
963 type(toml_array), pointer :: children
964 integer :: ii, nn, stat
965
966 ! Initialize platform with defaults
967 if (present(platform)) then
968 self%platform = platform
969 else
970 self%platform = platform_config_t(id_all,OS_ALL)
971 end if
972
973 ! Get description
974 call get_value(table, "description", self%description)
975
976 ! Get compiler flags
977 call get_value(table, "flags", self%flags)
978 call get_value(table, "c-flags", self%c_flags)
979 call get_value(table, "cxx-flags", self%cxx_flags)
980 call get_value(table, "link-time-flags", self%link_time_flags)
981
982 ! Get feature dependencies
983 call get_list(table, "requires", self%requires_features, error)
984 if (allocated(error)) return
985
986 ! Get build configuration
987 call get_value(table, "build", child, requested=.false., stat=stat)
988 if (stat == toml_stat%success .and. associated(child)) then
989 allocate(self%build)
990 call new_build_config(self%build, child, self%name, error)
991 if (allocated(error)) return
992 end if
993
994 ! Get install configuration
995 call get_value(table, "install", child, requested=.false., stat=stat)
996 if (stat == toml_stat%success .and. associated(child)) then
997 allocate(self%install)
998 call new_install_config(self%install, child, error)
999 if (allocated(error)) return
1000 end if
1001
1002 ! Get Fortran configuration
1003 call get_value(table, "fortran", child, requested=.false., stat=stat)
1004 if (stat == toml_stat%success .and. associated(child)) then
1005 allocate(self%fortran)
1006 call new_fortran_config(self%fortran, child, error)
1007 if (allocated(error)) return
1008 end if
1009
1010 ! Get library configuration
1011 call get_value(table, "library", child, requested=.false.)
1012 if (associated(child)) then
1013 allocate(self%library)
1014 call new_library(self%library, child, error)
1015 if (allocated(error)) return
1016 end if
1017
1018 ! Get dependencies and metapackage dependencies
1019 call get_value(table, "dependencies", child, requested=.false.)
1020 if (associated(child)) then
1021 call new_dependencies(self%dependency, child, root, self%meta, error=error)
1022 if (allocated(error)) return
1023 end if
1024
1025 ! Get development dependencies
1026 call get_value(table, "dev-dependencies", child, requested=.false.)
1027 if (associated(child)) then
1028 call new_dependencies(self%dev_dependency, child, root, error=error)
1029 if (allocated(error)) return
1030 end if
1031
1032 ! Get executables
1033 call get_value(table, "executable", children, requested=.false.)
1034 if (associated(children)) then
1035 nn = len(children)
1036 allocate(self%executable(nn))
1037 do ii = 1, nn
1038 call get_value(children, ii, node, stat=stat)
1039 if (stat /= toml_stat%success) then
1040 call fatal_error(error, "Could not retrieve executable from array entry")
1041 exit
1042 end if
1043 call new_executable(self%executable(ii), node, error)
1044 if (allocated(error)) exit
1045 end do
1046 if (allocated(error)) return
1047 end if
1048
1049 ! Get examples
1050 call get_value(table, "example", children, requested=.false.)
1051 if (associated(children)) then
1052 nn = len(children)
1053 allocate(self%example(nn))
1054 do ii = 1, nn
1055 call get_value(children, ii, node, stat=stat)
1056 if (stat /= toml_stat%success) then
1057 call fatal_error(error, "Could not retrieve example from array entry")
1058 exit
1059 end if
1060 call new_example(self%example(ii), node, error)
1061 if (allocated(error)) exit
1062 end do
1063 if (allocated(error)) return
1064 end if
1065
1066 ! Get tests
1067 call get_value(table, "test", children, requested=.false.)
1068 if (associated(children)) then
1069 nn = len(children)
1070 allocate(self%test(nn))
1071 do ii = 1, nn
1072 call get_value(children, ii, node, stat=stat)
1073 if (stat /= toml_stat%success) then
1074 call fatal_error(error, "Could not retrieve test from array entry")
1075 exit
1076 end if
1077 call new_test(self%test(ii), node, error)
1078 if (allocated(error)) exit
1079 end do
1080 if (allocated(error)) return
1081 end if
1082
1083 ! Get preprocessors
1084 call get_value(table, "preprocess", child, requested=.false.)
1085 if (associated(child)) then
1086 call new_preprocessors(self%preprocess, child, error)
1087 if (allocated(error)) return
1088 end if
1089
1090 ! Validate unique program names
1091 if (allocated(self%executable)) then
1092 call unique_programs(self%executable, error)
1093 if (allocated(error)) return
1094 end if
1095
1096 if (allocated(self%example)) then
1097 call unique_programs(self%example, error)
1098 if (allocated(error)) return
1099
1100 if (allocated(self%executable)) then
1101 call unique_programs(self%executable, self%example, error)
1102 if (allocated(error)) return
1103 end if
1104 end if
1105
1106 if (allocated(self%test)) then
1107 call unique_programs(self%test, error)
1108 if (allocated(error)) return
1109 end if
1110
1111 end subroutine init_feature_components
1112
1113 !> Check whether or not the names in a set of executables are unique
1114 subroutine unique_programs1(executable, error)
1115
1116 !> Array of executables
1117 class(executable_config_t), intent(in) :: executable(:)
1118
1119 !> Error handling
1120 type(error_t), allocatable, intent(out) :: error
1121
1122 integer :: i, j
1123
1124 do i = 1, size(executable)
1125 do j = 1, i - 1
1126 if (executable(i)%name == executable(j)%name) then
1127 call fatal_error(error, "The program named '"//&
1128 executable(j)%name//"' is duplicated. "//&
1129 "Unique program names are required.")
1130 exit
1131 end if
1132 end do
1133 end do
1134 if (allocated(error)) return
1135
1136 end subroutine unique_programs1
1137
1138
1139 !> Check whether or not the names in a set of executables are unique
1140 subroutine unique_programs2(executable_i, executable_j, error)
1141
1142 !> Array of executables
1143 class(executable_config_t), intent(in) :: executable_i(:)
1144
1145 !> Array of executables
1146 class(executable_config_t), intent(in) :: executable_j(:)
1147
1148 !> Error handling
1149 type(error_t), allocatable, intent(out) :: error
1150
1151 integer :: i, j
1152
1153 do i = 1, size(executable_i)
1154 do j = 1, size(executable_j)
1155 if (executable_i(i)%name == executable_j(j)%name) then
1156 call fatal_error(error, "The program named '"//&
1157 executable_j(j)%name//"' is duplicated. "//&
1158 "Unique program names are required.")
1159 exit
1160 end if
1161 end do
1162 end do
1163 if (allocated(error)) return
1164
1165 end subroutine unique_programs2
1166
1167 !> Return a name string as it would appear in the TOML manifest
1168 function manifest_name(self) result(name)
1169 class(feature_config_t), intent(in) :: self
1170 character(:), allocatable :: name
1171
1172 character(:), allocatable :: platform
1173
1174 platform = self%platform%name()
1175
1176 if (len(platform)>0) then
1177 name = self%name//'.'//platform
1178 else
1179 name = self%name
1180 end if
1181
1182 end function manifest_name
1183
1184 !> Check if there is a CPP preprocessor configuration
1185 elemental logical function has_cpp(self)
1186 class(feature_config_t), intent(in) :: self
1187
1188 integer :: i
1189
1190 has_cpp = .false.
1191 if (.not.allocated(self%preprocess)) return
1192
1193 do i=1,size(self%preprocess)
1194 has_cpp = self%preprocess(i)%is_cpp()
1195 if (has_cpp) return
1196 end do
1197
1198 end function has_cpp
1199
1200 end module fpm_manifest_feature