Debian Codegraph @

src/fpm/manifest/feature_collection.f90

raw ◀ ▶
1 Debian patch touch this file
1
2 module fpm_manifest_feature_collection
3 use fpm_manifest_feature, only: feature_config_t, new_feature, init_feature_components
4 use fpm_manifest_platform, only: platform_config_t, is_platform_key
5 use fpm_manifest_dependency, only: dependency_config_t
6 use fpm_manifest_example, only: example_config_t
7 use fpm_manifest_executable, only: executable_config_t
8 use fpm_manifest_metapackages, only: metapackage_config_t, metapackage_request_t
9 use fpm_manifest_test, only: test_config_t
10 use fpm_manifest_preprocess, only: preprocess_config_t
11 use fpm_error, only: error_t, fatal_error, syntax_error
12 use fpm_environment, only: OS_UNKNOWN, OS_LINUX, OS_MACOS, OS_WINDOWS, OS_CYGWIN, OS_SOLARIS, &
13 OS_FREEBSD, OS_OPENBSD, OS_ALL, match_os_type, OS_NAME
14 use fpm_compiler, only: compiler_enum, compiler_id_name, match_compiler_type, &
15 id_unknown, id_gcc, id_f95, id_caf, &
16 id_intel_classic_nix, id_intel_classic_mac, id_intel_classic_windows, &
17 id_intel_llvm_nix, id_intel_llvm_windows, id_intel_llvm_unknown, &
18 id_pgi, id_nvhpc, id_nag, id_flang, id_lahey, id_lfortran, id_all
19 use fpm_strings, only: string_t, lower, operator(==), split, str
20 use tomlf, only: toml_table, toml_array, toml_key, toml_stat
21 use fpm_toml, only: get_value, len, serializable_t, set_value, set_string, set_list, add_table, &
22 get_list
23 implicit none
24 private
25
26 public :: new_collections, get_default_features, &
27 default_debug_feature, default_release_feature, &
28 add_default_features, collection_from_feature
29
30 !> Feature configuration data
31 type, public, extends(serializable_t) :: feature_collection_t
32
33 ! Features shared by all platforms, all compilers
34 type(feature_config_t) :: base
35
36 ! Features shared by specific platform/compiler configurations
37 type(feature_config_t), allocatable :: variants(:)
38
39 contains
40
41 procedure :: serializable_is_same => feature_collection_same
42 procedure :: dump_to_toml => feature_collection_dump
43 procedure :: load_from_toml => feature_collection_load
44
45 procedure :: push_variant
46 procedure :: extract_for_target
47 procedure :: check => check_collection
48 procedure :: merge_into_package
49 procedure :: has_cpp
50
51 end type feature_collection_t
52
53 !> Interface for feature_collection_t constructor
54 interface feature_collection_t
55 module procedure collection_from_feature
56 end interface feature_collection_t
57
58 contains
59
60 !> Equality (semantic): base and variants (size + element-wise)
61 logical function feature_collection_same(this, that)
62 class(feature_collection_t), intent(in) :: this
63 class(serializable_t), intent(in) :: that
64 integer :: i
65
66 feature_collection_same = .false.
67 select type (other => that)
68 type is (feature_collection_t)
69 if (.not.(this%base == other%base)) return
70 if (allocated(this%variants) .neqv. allocated(other%variants)) return
71 if (allocated(this%variants)) then
72 if (size(this%variants) /= size(other%variants)) return
73 do i = 1, size(this%variants)
74 if (.not.(this%variants(i) == other%variants(i))) return
75 end do
76 end if
77 class default
78 return
79 end select
80 feature_collection_same = .true.
81 end function feature_collection_same
82
83 !> Serialize: base as a subtable; variants under a "variants" table
84 subroutine feature_collection_dump(self, table, error)
85 class(feature_collection_t), intent(inout) :: self
86 type(toml_table), intent(inout) :: table
87 type(error_t), allocatable, intent(out) :: error
88
89 type(toml_table), pointer :: ptr_base, ptr_vars, ptr
90 integer :: i
91 character(len=32) :: key
92
93 ! base
94 call add_table(table, "base", ptr_base)
95 if (.not. associated(ptr_base)) then
96 call fatal_error(error, "feature_collection_t: cannot create 'base' table"); return
97 end if
98 call self%base%dump_to_toml(ptr_base, error); if (allocated(error)) return
99
100 ! variants (optional)
101 if (allocated(self%variants)) then
102 call add_table(table, "variants", ptr_vars)
103 if (.not. associated(ptr_vars)) then
104 call fatal_error(error, "feature_collection_t: cannot create 'variants' table"); return
105 end if
106 do i = 1, size(self%variants)
107 write(key, '("variant_", i0)') i
108 call add_table(ptr_vars, trim(key), ptr)
109 if (.not. associated(ptr)) then
110 call fatal_error(error, "feature_collection_t: cannot create entry for "//trim(key)); return
111 end if
112 call self%variants(i)%dump_to_toml(ptr, error); if (allocated(error)) return
113 end do
114 end if
115 end subroutine feature_collection_dump
116
117 !> Deserialize: read base; then any number of variants under "variants"
118 subroutine feature_collection_load(self, table, error)
119 class(feature_collection_t), intent(inout) :: self
120 type(toml_table), intent(inout) :: table
121 type(error_t), allocatable, intent(out) :: error
122
123 type(toml_table), pointer :: ptr_base, ptr_vars, ptr
124 type(toml_key), allocatable :: keys(:)
125 integer :: i
126
127 ! base (required)
128 call get_value(table, "base", ptr_base)
129 if (.not. associated(ptr_base)) then
130 call fatal_error(error, "feature_collection_t: missing 'base' table"); return
131 end if
132 call self%base%load_from_toml(ptr_base, error); if (allocated(error)) return
133
134 ! variants (optional)
135 call get_value(table, "variants", ptr_vars)
136 if (.not. associated(ptr_vars)) then
137 if (allocated(self%variants)) deallocate(self%variants)
138 return
139 end if
140
141 call ptr_vars%get_keys(keys)
142 if (allocated(self%variants)) deallocate(self%variants)
143 allocate(self%variants(size(keys)))
144
145 do i = 1, size(keys)
146 call get_value(ptr_vars, keys(i), ptr)
147 if (.not. associated(ptr)) then
148 call fatal_error(error, "feature_collection_t: invalid variant entry '" &
149 //keys(i)%key//"'"); return
150 end if
151 call self%variants(i)%load_from_toml(ptr, error); if (allocated(error)) return
152 end do
153 end subroutine feature_collection_load
154
155 ! helper: append a variant to self%variants
156 elemental subroutine push_variant(self,variant)
157 class(feature_collection_t), intent(inout) :: self
158 type(feature_config_t), intent(in) :: variant
159
160
161 type(feature_config_t), allocatable :: tmp(:)
162
163 integer :: n
164
165 if (.not. allocated(self%variants)) then
166 allocate(self%variants(1), source=variant)
167 else
168 n = size(self%variants)
169 allocate(tmp(n+1))
170 if (n>0) tmp(1:n) = self%variants
171 tmp(n+1) = variant
172 call move_alloc(tmp, self%variants)
173 end if
174 end subroutine push_variant
175
176 logical function is_compiler_key(s)
177 character(*), intent(in) :: s
178 is_compiler_key = match_compiler_type(s) /= id_unknown
179 end function is_compiler_key
180
181 logical function is_os_key(s)
182 character(*), intent(in) :: s
183 is_os_key = match_os_type(s) /= OS_UNKNOWN
184 end function is_os_key
185
186 !> Initialize multiple feature collections from manifest features table
187 subroutine new_collections(collections, table, error)
188 type(feature_collection_t), allocatable, intent(out) :: collections(:)
189 type(toml_table), intent(inout) :: table
190 type(error_t), allocatable, intent(out) :: error
191
192 type(toml_key), allocatable :: keys(:)
193 type(toml_table), pointer :: feature_table
194 integer :: i, stat
195
196 ! Get all top-level feature names from the features table
197 call table%get_keys(keys)
198
199 if (size(keys) == 0) then
200 ! No features defined, return default collections
201 call get_default_features(collections, error)
202 return
203 end if
204
205 ! Create one collection per top-level feature name
206 allocate(collections(size(keys)))
207
208 do i = 1, size(keys)
209 ! Get the subtable for this feature
210 call get_value(table, keys(i)%key, feature_table, stat=stat)
211 if (stat /= toml_stat%success) then
212 call fatal_error(error, "Could not retrieve feature table for '"//keys(i)%key//"'")
213 return
214 end if
215
216 ! Create collection from this feature's subtable
217 call new_collection_from_subtable(collections(i), feature_table, keys(i)%key, error)
218 if (allocated(error)) return
219
220 end do
221
222 end subroutine new_collections
223
224 !> Create a feature collection from a TOML subtable by traversing the hierarchy
225 !> Supports flexible configurations like:
226 !> [features]
227 !> name.os.compiler.* = ... # specific compiler + OS
228 !> name.compiler.* = ... # all OS, specific compiler
229 !> name.os.* = ... # specific OS, all compilers
230 !> name.* = ... # base feature (all OS, all compilers)
231 subroutine new_collection_from_subtable(self, table, name, error)
232 type(feature_collection_t), intent(out) :: self
233 type(toml_table), intent(inout) :: table
234 character(*), intent(in) :: name
235 type(error_t), allocatable, intent(out) :: error
236
237 integer :: i
238 type(platform_config_t) :: default_platform
239 type(toml_key), allocatable :: keys(:)
240
241 default_platform = platform_config_t(id_all,OS_ALL)
242
243 ! Initialize base feature
244 self%base%name = name
245 self%base%platform = default_platform
246
247 ! Traverse the table hierarchy to find variants
248 call traverse_feature_table(self, table, name, default_platform, error)
249 if (allocated(error)) return
250
251 ! Check collection
252 call self%check(error)
253 if (allocated(error)) return
254
255 end subroutine new_collection_from_subtable
256
257 !> Recursively traverse a feature table to find variants
258 recursive subroutine traverse_feature_table(collection, table, feature_name, &
259 constraint, error)
260 type(feature_collection_t), intent(inout) :: collection
261 type(toml_table), intent(inout) :: table
262 character(*), intent(in) :: feature_name
263 type(platform_config_t), intent(in) :: constraint
264 type(error_t), allocatable, intent(out) :: error
265
266 type(toml_key), allocatable :: keys(:)
267 type(toml_table), pointer :: subtable
268 character(len=:), allocatable :: value_str
269 integer :: i, stat, os_type, compiler_type
270 type(feature_config_t) :: feature_variant
271 type(platform_config_t) :: platform
272 logical :: has_platform_keys, has_feature_data
273
274 call table%get_keys(keys)
275 has_platform_keys = .false.
276 has_feature_data = .false.
277
278 ! First pass: check what types of keys we have
279 do i = 1, size(keys)
280
281 ! Check if this key is a valid OS name
282 os_type = match_os_type(keys(i)%key)
283 if (os_type /= OS_UNKNOWN) then
284 has_platform_keys = .true.
285 cycle
286 end if
287
288 ! Check if this key is a valid compiler name
289 compiler_type = match_compiler_type(keys(i)%key)
290 if (compiler_type /= id_unknown) then
291 has_platform_keys = .true.
292 cycle
293 end if
294
295 ! This is a feature specification (like "flags" or "preprocess")
296 has_feature_data = .true.
297
298 ! No compiler/OS flags can appear in feature specification branches
299 if (is_platform_key(keys(i)%key)) then
300 call fatal_error(error, "Key '"//keys(i)%key//"' is not allowed in feature table")
301 return
302 end if
303
304 end do
305
306 ! If we have platform keys, traverse them
307 if (has_platform_keys) then
308 do i = 1, size(keys)
309 ! Check if this key is an OS name
310 os_type = match_os_type(keys(i)%key)
311 if (os_type /= OS_UNKNOWN) then
312 ! This is an OS constraint
313
314 ! Check for chained OS commands (e.g., feature.windows.linux)
315 if (constraint%os_type /= OS_ALL) then
316 call fatal_error(error, "Cannot chain OS constraints: '" // &
317 constraint%os_name() // "." // keys(i)%key // &
318 "' - OS was already specified")
319 return
320 end if
321
322 ! Get subtable and recurse
323 call get_value(table, keys(i)%key, subtable, stat=stat)
324 if (stat == toml_stat%success) then
325 platform = platform_config_t(constraint%compiler,os_type)
326 call traverse_feature_table(collection, subtable, feature_name, &
327 platform, error)
328 if (allocated(error)) return
329 end if
330 cycle
331 end if
332
333 ! Check if this key is a compiler name
334 compiler_type = match_compiler_type(keys(i)%key)
335 if (compiler_type /= id_unknown) then
336
337 ! Check for chained compiler commands (e.g., feature.gfortran.ifort)
338 if (constraint%compiler /= id_all) then
339 call fatal_error(error, "Cannot chain compiler constraints: '" // &
340 constraint%compiler_name() // "." // keys(i)%key // &
341 "' - compiler was already specified")
342 return
343 end if
344
345 ! This is a compiler constraint - get subtable and recurse
346 call get_value(table, keys(i)%key, subtable, stat=stat)
347 if (stat == toml_stat%success) then
348 platform = platform_config_t(compiler_type,constraint%os_type)
349 call traverse_feature_table(collection, subtable, feature_name, &
350 platform, error)
351 if (allocated(error)) return
352 end if
353 cycle
354 end if
355 end do
356 end if
357
358 ! If we found feature data at this level (no more platform keys), initialize feature components
359 if (has_feature_data) then
360
361 ! Initialize a new feature variant
362 feature_variant%name = feature_name
363
364 ! Check that the table is right
365 call feature_variant%check(table, error)
366 if (allocated(error)) return
367
368 call init_feature_components(feature_variant, table, constraint, error=error)
369 if (allocated(error)) return
370
371 if (constraint%any_platform()) then
372 ! This is a base feature specification
373 call merge_feature_configs(collection%base, feature_variant, error)
374 if (allocated(error)) return
375 else
376 ! This is a constrained variant
377 call collection%push_variant(feature_variant)
378 end if
379 end if
380
381 ! If this is the root table and we haven't processed any feature data yet,
382 ! call init_feature_components on the base feature (may be empty)
383 if (constraint%any_platform() .and. .not.(has_platform_keys.or.has_feature_data)) then
384
385 ! Check that the table is right
386 call feature_variant%check(table, error)
387 if (allocated(error)) return
388
389 ! Initialize base feature components from empty or root table
390 call init_feature_components(collection%base, table, error=error)
391 if (allocated(error)) return
392 end if
393
394 end subroutine traverse_feature_table
395
396 !> Merge two feature configurations using standardized rules:
397 !> - String properties (flags): concatenate with spaces (additive)
398 !> - Array properties: append arrays (additive)
399 !> - Allocatable properties: source overwrites target if target not allocated (conflict if both allocated)
400 !> - Metapackages: OR logic - turn on any that are requested (additive)
401 subroutine merge_feature_configs(target, source, error)
402 type(feature_config_t), intent(inout) :: target
403 type(feature_config_t), intent(in) :: source
404 type(error_t), allocatable, intent(out) :: error
405
406 ! Check for allocatable property conflicts (should be caught by validation, but double-check)
407 if (allocated(target%build) .and. allocated(source%build)) then
408 call fatal_error(error, "build configuration can only be specified in one feature variant")
409 return
410 end if
411
412 if (allocated(target%install) .and. allocated(source%install)) then
413 call fatal_error(error, "install configuration can only be specified in one feature variant")
414 return
415 end if
416
417 if (allocated(target%fortran) .and. allocated(source%fortran)) then
418 call fatal_error(error, "fortran configuration can only be specified in one feature variant")
419 return
420 end if
421
422 if (allocated(target%library) .and. allocated(source%library)) then
423 call fatal_error(error, "library configuration can only be specified in one feature variant")
424 return
425 end if
426
427 ! Merge simple string fields - source takes precedence if target doesn't have one
428 if (allocated(source%description) .and. .not. allocated(target%description)) then
429 target%description = source%description
430 end if
431
432 ! ADDITIVE: String properties (flags) - concatenate with spaces
433 call merge_string_additive(target%flags, source%flags)
434 call merge_string_additive(target%c_flags, source%c_flags)
435 call merge_string_additive(target%cxx_flags, source%cxx_flags)
436 call merge_string_additive(target%link_time_flags, source%link_time_flags)
437
438 ! ALLOCATABLE: Only set if target doesn't have it (conflicts checked above)
439 if (allocated(source%build) .and. .not. allocated(target%build)) then
440 allocate(target%build)
441 target%build = source%build
442 end if
443
444 if (allocated(source%install) .and. .not. allocated(target%install)) then
445 allocate(target%install)
446 target%install = source%install
447 end if
448
449 if (allocated(source%fortran) .and. .not. allocated(target%fortran)) then
450 allocate(target%fortran)
451 target%fortran = source%fortran
452 end if
453
454 if (allocated(source%library) .and. .not. allocated(target%library)) then
455 allocate(target%library)
456 target%library = source%library
457 end if
458
459 ! ADDITIVE: Array properties - append source to target
460 call merge_executable_arrays(target%executable, source%executable, error)
461 if (allocated(error)) return
462
463 call merge_dependency_arrays(target%dependency, source%dependency, error)
464 if (allocated(error)) return
465
466 call merge_dependency_arrays(target%dev_dependency, source%dev_dependency, error)
467 if (allocated(error)) return
468
469 call merge_example_arrays(target%example, source%example, error)
470 if (allocated(error)) return
471
472 call merge_test_arrays(target%test, source%test, error)
473 if (allocated(error)) return
474
475 call merge_preprocess_arrays(target%preprocess, source%preprocess)
476 call merge_string_arrays(target%requires_features, source%requires_features)
477
478 ! ADDITIVE: Metapackages - OR logic (if either requests it, turn it on)
479 call merge_metapackages(target%meta, source%meta)
480
481 end subroutine merge_feature_configs
482
483 !> Merge executable arrays by appending source to target, checking for duplicates
484 subroutine merge_executable_arrays(target, source, error)
485 type(executable_config_t), allocatable, intent(inout) :: target(:)
486 type(executable_config_t), allocatable, intent(in) :: source(:)
487 type(error_t), allocatable, intent(out) :: error
488
489 type(executable_config_t), allocatable :: temp(:)
490 integer :: target_size, source_size, i, j
491
492 if (.not. allocated(source)) return
493
494 source_size = size(source)
495 if (source_size == 0) return
496
497 ! Check for duplicates between source and target
498 if (allocated(target)) then
499 target_size = size(target)
500 do i = 1, source_size
501 do j = 1, target_size
502 if (allocated(source(i)%name) .and. allocated(target(j)%name)) then
503 if (source(i)%name == target(j)%name) then
504 call fatal_error(error, "Duplicate executable '"//source(i)%name//"' found. " // &
505 "Multiple definitions of the same executable are not currently allowed.")
506 return
507 end if
508 end if
509 end do
510 end do
511 end if
512
513 if (.not. allocated(target)) then
514 allocate(target(source_size), source=source)
515 else
516 target_size = size(target)
517 allocate(temp(target_size + source_size))
518 temp(1:target_size) = target
519 temp(target_size+1:target_size+source_size) = source
520 call move_alloc(temp, target)
521 end if
522
523 end subroutine merge_executable_arrays
524
525 !> Merge dependency arrays by appending source to target, checking for duplicates
526 subroutine merge_dependency_arrays(target, source, error)
527 type(dependency_config_t), allocatable, intent(inout) :: target(:)
528 type(dependency_config_t), allocatable, intent(in) :: source(:)
529 type(error_t), allocatable, intent(out) :: error
530
531 type(dependency_config_t), allocatable :: temp(:)
532 integer :: target_size, source_size, i, j
533
534 if (.not. allocated(source)) return
535
536 source_size = size(source)
537 if (source_size == 0) return
538
539 ! Check for duplicates between source and target
540 if (allocated(target)) then
541 target_size = size(target)
542 do i = 1, source_size
543 do j = 1, target_size
544 if (allocated(source(i)%name) .and. allocated(target(j)%name)) then
545 if (source(i)%name == target(j)%name) then
546 call fatal_error(error, "Duplicate dependency '"//source(i)%name//"' found. " // &
547 "Multiple definitions of the same dependency are not currently allowed.")
548 return
549 end if
550 end if
551 end do
552 end do
553 end if
554
555 if (.not. allocated(target)) then
556 allocate(target(source_size), source=source)
557 else
558 target_size = size(target)
559 allocate(temp(target_size + source_size))
560 temp(1:target_size) = target
561 temp(target_size+1:target_size+source_size) = source
562 call move_alloc(temp, target)
563 end if
564
565 end subroutine merge_dependency_arrays
566
567 !> Merge string properties additively by concatenating with space
568 subroutine merge_string_additive(target, source)
569 character(len=:), allocatable, intent(inout) :: target
570 character(len=:), allocatable, intent(in) :: source
571
572 if (allocated(source)) then
573 if (allocated(target)) then
574 target = trim(target) // " " // trim(source)
575 else
576 target = source
577 end if
578 end if
579 end subroutine merge_string_additive
580
581 !> Merge example arrays by appending source to target, checking for duplicates
582 subroutine merge_example_arrays(target, source, error)
583 type(example_config_t), allocatable, intent(inout) :: target(:)
584 type(example_config_t), allocatable, intent(in) :: source(:)
585 type(error_t), allocatable, intent(out) :: error
586
587 type(example_config_t), allocatable :: temp(:)
588 integer :: target_size, source_size, i, j
589
590 if (.not. allocated(source)) return
591
592 source_size = size(source)
593 if (source_size == 0) return
594
595 ! Check for duplicates between source and target
596 if (allocated(target)) then
597 target_size = size(target)
598 do i = 1, source_size
599 do j = 1, target_size
600 if (allocated(source(i)%name) .and. allocated(target(j)%name)) then
601 if (source(i)%name == target(j)%name) then
602 call fatal_error(error, "Duplicate example '"//source(i)%name//"' found. " // &
603 "Multiple definitions of the same example are not currently allowed.")
604 return
605 end if
606 end if
607 end do
608 end do
609 end if
610
611 if (.not. allocated(target)) then
612 allocate(target(source_size), source=source)
613 else
614 target_size = size(target)
615 allocate(temp(target_size + source_size))
616 temp(1:target_size) = target
617 temp(target_size+1:target_size+source_size) = source
618 call move_alloc(temp, target)
619 end if
620 end subroutine merge_example_arrays
621
622 !> Merge test arrays by appending source to target, checking for duplicates
623 subroutine merge_test_arrays(target, source, error)
624 type(test_config_t), allocatable, intent(inout) :: target(:)
625 type(test_config_t), allocatable, intent(in) :: source(:)
626 type(error_t), allocatable, intent(out) :: error
627
628 type(test_config_t), allocatable :: temp(:)
629 integer :: target_size, source_size, i, j
630
631 if (.not. allocated(source)) return
632
633 source_size = size(source)
634 if (source_size == 0) return
635
636 ! Check for duplicates between source and target
637 if (allocated(target)) then
638 target_size = size(target)
639 do i = 1, source_size
640 do j = 1, target_size
641 if (allocated(source(i)%name) .and. allocated(target(j)%name)) then
642 if (source(i)%name == target(j)%name) then
643 call fatal_error(error, "Duplicate test '"//source(i)%name//"' found. " // &
644 "Multiple definitions of the same test are not currently allowed.")
645 return
646 end if
647 end if
648 end do
649 end do
650 end if
651
652 if (.not. allocated(target)) then
653 allocate(target(source_size))
654 target = source
655 else
656 target_size = size(target)
657 allocate(temp(target_size + source_size))
658 temp(1:target_size) = target
659 temp(target_size+1:target_size+source_size) = source
660 call move_alloc(temp, target)
661 end if
662 end subroutine merge_test_arrays
663
664 !> Merge preprocess arrays by merging configurations for same preprocessor names
665 !> and appending new ones
666 subroutine merge_preprocess_arrays(target, source)
667 type(preprocess_config_t), allocatable, intent(inout) :: target(:)
668 type(preprocess_config_t), allocatable, intent(in) :: source(:)
669
670 type(preprocess_config_t), allocatable :: temp(:)
671 integer :: target_size, source_size, i, j, new_count
672 integer, allocatable :: source_to_target_map(:) ! Maps source index to target index (0 = new)
673
674 if (.not. allocated(source)) return
675
676 source_size = size(source)
677 if (source_size == 0) return
678
679 if (.not. allocated(target)) then
680 allocate(target(source_size), source=source)
681 return
682 end if
683
684 target_size = size(target)
685
686 ! Create mapping arrays in a single pass
687 allocate(source_to_target_map(source_size), source=0)
688
689 ! Single loop to build the mapping
690 do i = 1, source_size
691 if (allocated(source(i)%name)) then
692 do j = 1, target_size
693 if (allocated(target(j)%name)) then
694 if (target(j)%name == source(i)%name) then
695 source_to_target_map(i) = j
696 exit
697 end if
698 end if
699 end do
700 end if
701 end do
702
703 ! Merge overlapping configurations
704 do i = 1, source_size
705 j = source_to_target_map(i)
706 if (j==0) cycle ! new config
707 call merge_preprocessor_config(target(j), source(i))
708 end do
709
710 ! Count and add new preprocessors
711 new_count = count(source_to_target_map==0)
712 if (new_count > 0) then
713 allocate(temp(target_size + new_count))
714 temp(1:target_size) = target
715
716 ! Add new preprocessors in a single pass
717 j = target_size
718 do i = 1, source_size
719 if (source_to_target_map(i)==0) then
720 j = j + 1
721 temp(j) = source(i)
722 end if
723 end do
724
725 call move_alloc(temp, target)
726 end if
727 end subroutine merge_preprocess_arrays
728
729 !> Helper to merge two preprocessor configurations with the same name
730 subroutine merge_preprocessor_config(target, source)
731 type(preprocess_config_t), intent(inout) :: target
732 type(preprocess_config_t), intent(in) :: source
733
734 ! Merge suffixes arrays
735 call merge_string_arrays(target%suffixes, source%suffixes)
736
737 ! Merge directories arrays
738 call merge_string_arrays(target%directories, source%directories)
739
740 ! Merge macros arrays
741 call merge_string_arrays(target%macros, source%macros)
742
743 end subroutine merge_preprocessor_config
744
745 !> Merge string arrays by appending source to target
746 subroutine merge_string_arrays(target, source)
747 type(string_t), allocatable, intent(inout) :: target(:)
748 type(string_t), allocatable, intent(in) :: source(:)
749
750 type(string_t), allocatable :: temp(:)
751 integer :: target_size, source_size
752
753 if (.not. allocated(source)) return
754
755 source_size = size(source)
756 if (source_size == 0) return
757
758 if (.not. allocated(target)) then
759 allocate(target(source_size))
760 target = source
761 else
762 target_size = size(target)
763 allocate(temp(target_size + source_size))
764 temp(1:target_size) = target
765 temp(target_size+1:target_size+source_size) = source
766 call move_alloc(temp, target)
767 end if
768 end subroutine merge_string_arrays
769
770 !> Merge metapackages using OR logic - if either requests it, turn it on
771 subroutine merge_metapackages_additive(target, source)
772 type(metapackage_request_t), intent(inout) :: target
773 type(metapackage_request_t), intent(in) :: source
774
775 ! OR logic: if either requests a metapackage, turn it on
776 if (source%on) then
777 target%on = .true.
778 target%name = source%name
779 ! Use source version if target doesn't have one
780 if (allocated(source%version) .and. .not. allocated(target%version)) then
781 target%version = source%version
782 end if
783 end if
784
785 end subroutine merge_metapackages_additive
786
787 !> Merge whole metapackage config
788 subroutine merge_metapackages(target, source)
789 type(metapackage_config_t), intent(inout) :: target
790 type(metapackage_config_t), intent(in) :: source
791
792 call merge_metapackages_additive(target%openmp,source%openmp)
793 call merge_metapackages_additive(target%stdlib,source%stdlib)
794 call merge_metapackages_additive(target%minpack,source%minpack)
795 call merge_metapackages_additive(target%mpi,source%mpi)
796 call merge_metapackages_additive(target%hdf5,source%hdf5)
797 call merge_metapackages_additive(target%netcdf,source%netcdf)
798 call merge_metapackages_additive(target%blas,source%blas)
799
800 end subroutine merge_metapackages
801
802 !> Create default debug feature collection
803 function default_debug_feature() result(collection)
804 type(feature_collection_t) :: collection
805
806 ! Initialize base feature with debug settings
807 collection%base%name = 'debug'
808 collection%base%platform%compiler = id_all
809 collection%base%platform%os_type = OS_ALL
810
811 ! Add debug variants for different compilers
812 call collection%push_variant(default_variant('debug', id_caf, OS_ALL, &
813 ' -Wall -Wextra -Wimplicit-interface -Wno-external-argument-mismatch&
814 & -fPIC -fmax-errors=1 -g -fcheck=bounds&
815 & -fcheck=array-temps -fbacktrace', &
816 ' -fPIC', &
817 ' -fPIC'))
818
819 call collection%push_variant(default_variant('debug', id_gcc, OS_ALL, &
820 ' -Wall -Wextra -Wimplicit-interface -Wno-external-argument-mismatch&
821 & -fPIC -fmax-errors=1 -g -fcheck=bounds&
822 & -fcheck=array-temps -fbacktrace -fcoarray=single', &
823 ' -Wall -Wextra -fPIC -fmax-errors=1 -g', &
824 ' -Wall -Wextra -fPIC -fmax-errors=1 -g'))
825
826 call collection%push_variant(default_variant('debug', id_f95, OS_ALL, &
827 ' -Wall -Wextra -Wimplicit-interface -Wno-external-argument-mismatch&
828 & -fPIC -fmax-errors=1 -g -fcheck=bounds&
829 & -fcheck=array-temps -Wno-maybe-uninitialized -Wno-uninitialized -fbacktrace', &
830 ' -fPIC', &
831 ' -fPIC'))
832
833 call collection%push_variant(default_variant('debug', id_nvhpc, OS_ALL, &
834 ' -Minform=inform -Mbackslash -g -Mbounds -Mchkptr -Mchkstk -traceback', &
835 ' -fPIC', &
836 ' -fPIC'))
837
838 call collection%push_variant(default_variant('debug', id_intel_classic_nix, OS_ALL, &
839 ' -warn all -check all -error-limit 1 -O0 -g -assume byterecl -traceback', &
840 ' -fPIC', &
841 ' -fPIC'))
842
843 call collection%push_variant(default_variant('debug', id_intel_classic_nix, OS_WINDOWS, &
844 ' /warn:all /check:all /error-limit:1&
845 & /Od /Z7 /assume:byterecl /traceback'))
846
847 call collection%push_variant(default_variant('debug', id_intel_llvm_nix, OS_ALL, &
848 ' -warn all -check all -error-limit 1 -O0 -g -assume byterecl -traceback', &
849 ' -fPIC', &
850 ' -fPIC'))
851
852 call collection%push_variant(default_variant('debug', id_intel_llvm_nix, OS_WINDOWS, &
853 ' /warn:all /check:all /error-limit:1 /Od /Z7 /assume:byterecl'))
854
855 call collection%push_variant(default_variant('debug', id_lfortran, OS_ALL, '', ' -fPIC', ' -fPIC'))
856
857 end function default_debug_feature
858
859 !> Create default release feature collection
860 function default_release_feature() result(collection)
861 type(feature_collection_t) :: collection
862
863 ! Initialize base feature with release settings
864 collection%base%name = 'release'
865 collection%base%platform%compiler = id_all
866 collection%base%platform%os_type = OS_ALL
867
868 ! Add release variants for different compilers
869 call collection%push_variant(default_variant('release', id_caf, OS_ALL, &
870 ' -O3 -Wimplicit-interface -fPIC -fmax-errors=1 -funroll-loops', &
871 ' -fPIC', &
872 ' -fPIC'))
873
874 call collection%push_variant(default_variant('release', id_gcc, OS_ALL, &
875 ' -O3 -Wimplicit-interface -fPIC -fmax-errors=1 -funroll-loops -fcoarray=single', &
876 ' -O3 -fPIC -fmax-errors=1 -funroll-loops', &
877 ' -O3 -fPIC -fmax-errors=1 -funroll-loops'))
878
879 call collection%push_variant(default_variant('release', id_f95, OS_ALL, &
880 ' -O3 -Wimplicit-interface -fPIC -fmax-errors=1 -ffast-math -funroll-loops', &
881 ' -fPIC', &
882 ' -fPIC'))
883
884 call collection%push_variant(default_variant('release', id_nvhpc, OS_ALL, &
885 ' -Mbackslash', &
886 ' -fPIC', &
887 ' -fPIC'))
888
889 call collection%push_variant(default_variant('release', id_intel_classic_nix, OS_LINUX, &
890 ' -fp-model precise -pc64 -align all -error-limit 1 -reentrancy&
891 & threaded -nogen-interfaces -assume byterecl', &
892 ' -fPIC', &
893 ' -fPIC'))
894
895 call collection%push_variant(default_variant('release', id_intel_classic_windows, &
896 OS_WINDOWS, ' /fp:precise /align:all /error-limit:1 /reentrancy:threaded&
897 & /nogen-interfaces /assume:byterecl'))
898
899 call collection%push_variant(default_variant('release', id_intel_llvm_nix, &
900 OS_LINUX, ' -fp-model=precise -pc64 -align all -error-limit 1 -reentrancy threaded&
901 & -nogen-interfaces -assume byterecl', &
902 ' -fPIC', &
903 ' -fPIC'))
904
905 call collection%push_variant(default_variant('release', id_intel_llvm_nix, &
906 OS_WINDOWS, ' /fp:precise /align:all /error-limit:1 /reentrancy:threaded&
907 & /nogen-interfaces /assume:byterecl'))
908
909 call collection%push_variant(default_variant('release', id_nag, OS_ALL, &
910 ' -O4 -coarray=single -PIC', &
911 ' -fPIC', &
912 ' -fPIC'))
913
914 call collection%push_variant(default_variant('release', id_lfortran, OS_ALL, &
•915 ' --fast', &
916 ' -fPIC', &
917 ' -fPIC'))
918
919 end function default_release_feature
920
921 !> Get default feature collections (debug and release)
922 subroutine get_default_features(collections, error)
923
924 !> Feature collections array to populate (debug and release)
925 type(feature_collection_t), allocatable, intent(out) :: collections(:)
926
927 !> Error handling
928 type(error_t), allocatable, intent(out) :: error
929
930 ! Allocate array for debug and release collections
931 allocate(collections(2))
932
933 ! Create debug and release collections
934 collections(1) = default_debug_feature()
935 collections(2) = default_release_feature()
936
937 end subroutine get_default_features
938
939 !> Helper to create a feature variant
940 function default_variant(name, compiler_id, os_type, flags, c_flags, cxx_flags) result(feature)
941 character(len=*), intent(in) :: name
942 integer(compiler_enum), intent(in) :: compiler_id
943 integer, intent(in) :: os_type
944 character(len=*), optional, intent(in) :: flags
945 character(len=*), optional, intent(in) :: c_flags
946 character(len=*), optional, intent(in) :: cxx_flags
947 type(feature_config_t) :: feature
948
949 feature%name = name
950 feature%platform%compiler = compiler_id
951 feature%platform%os_type = os_type
952 feature%flags = ""
953 feature%c_flags = ""
954 feature%cxx_flags = ""
955
956 if (present(flags)) then
957 feature%flags = flags
958 end if
959 if (present(c_flags)) then
960 feature%c_flags = c_flags
961 end if
962 if (present(cxx_flags)) then
963 feature%cxx_flags = cxx_flags
964 end if
965
966 end function default_variant
967
968
969 !> Check that the collection has valid OS/compiler logic and can be merged safely
970 !> Implements standardized feature hierarchy validation:
971 !> 1. OS_all+id_all (base) → id_compiler+OS_all → id_all+OS_current → id_compiler+OS_current
972 !> 2. Additive properties (flags) can be concatenated
973 !> 3. Allocatable properties can only exist in one variant per merge path
974 subroutine check_collection(self, error)
975 class(feature_collection_t), intent(in) :: self
976 type(error_t), allocatable, intent(out) :: error
977
978 integer :: i, j
979
980 ! Check base feature has valid platform settings
981 if (self%base%platform%os_type == OS_UNKNOWN) then
982 call fatal_error(error, "Base feature '"//self%base%name//"' has invalid OS type")
983 return
984 end if
985
986 if (self%base%platform%compiler == id_unknown) then
987 call fatal_error(error, "Base feature '"//self%base%name//"' has invalid compiler type")
988 return
989 end if
990
991 ! Base feature must be OS_ALL + id_all for proper hierarchy
992 if (self%base%platform%os_type /= OS_ALL .or. self%base%platform%compiler /= id_all) then
993 call fatal_error(error, "Base feature '"//self%base%name// &
994 "' must have OS_ALL and id_all platform settings")
995 return
996 end if
997
998 ! Check all variants have valid platform settings and hierarchy rules
999 if (allocated(self%variants)) then
1000 do i = 1, size(self%variants)
1001 ! Validate OS and compiler settings
1002 if (self%variants(i)%platform%os_type == OS_UNKNOWN) then
1003 call fatal_error(error, "Variant "//trim(str(i))//" of feature '" &
1004 //self%base%name//"' has invalid OS type")
1005 return
1006 end if
1007
1008 if (self%variants(i)%platform%compiler == id_unknown) then
1009 call fatal_error(error, "Variant "//trim(str(i))//" of feature '" &
1010 //self%base%name//"' has invalid compiler type")
1011 return
1012 end if
1013
1014 ! Check that variant name matches base name
1015 if (allocated(self%variants(i)%name) .and. allocated(self%base%name)) then
1016 if (self%variants(i)%name /= self%base%name) then
1017 call fatal_error(error, "Variant "//trim(str(i))//" name '" &
1018 //self%variants(i)%name// &
1019 "' does not match base name '"//self%base%name//"'")
1020 return
1021 end if
1022 end if
1023
1024 ! Validate feature hierarchy rules
1025 call validate_variant_hierarchy(self%variants(i), i, error)
1026 if (allocated(error)) return
1027
1028 ! Check for exact duplicate platforms
1029 do j = i + 1, size(self%variants)
1030 if (self%variants(i)%platform == self%variants(j)%platform) then
1031 call fatal_error(error, "Duplicate platform configurations: "// &
1032 self%variants(i)%manifest_name()// &
1033 " and "//self%variants(j)%manifest_name())
1034 return
1035 end if
1036 end do
1037 end do
1038
1039 ! Check for conflicts between variants that could be applied together
1040 call check_merge_conflicts(self, error)
1041 if (allocated(error)) return
1042 end if
1043
1044 end subroutine check_collection
1045
1046 !> Validate that a variant follows the feature hierarchy rules
1047 !> Valid combinations:
1048 !> - id_compiler + OS_all (compiler-specific, all OS)
1049 !> - id_all + OS_specific (OS-specific, all compilers)
1050 !> - id_compiler + OS_specific (target-specific)
1051 !> Note: id_all + OS_all variants are allowed during parsing but should be merged into base
1052 subroutine validate_variant_hierarchy(variant, index, error)
1053 type(feature_config_t), intent(in) :: variant
1054 integer, intent(in) :: index
1055 type(error_t), allocatable, intent(out) :: error
1056
1057 ! For now, allow all combinations - the merge logic handles id_all + OS_ALL -> base
1058 ! The validation was too strict for current parsing logic
1059 ! TODO: Implement stricter validation after parsing is fixed
1060
1061 ! All combinations are valid in the hierarchy:
1062 ! - id_all + OS_all (should be merged to base, but parsing might create these temporarily)
1063 ! - id_compiler + OS_all (compiler-specific)
1064 ! - id_all + OS_specific (OS-specific)
1065 ! - id_compiler + OS_specific (target-specific)
1066
1067 end subroutine validate_variant_hierarchy
1068
1069 !> Check for merge conflicts between variants that could be applied together
1070 !> This validates that no two applicable variants have conflicting allocatable properties
1071 subroutine check_merge_conflicts(collection, error)
1072 class(feature_collection_t), intent(in) :: collection
1073 type(error_t), allocatable, intent(out) :: error
1074
1075 integer :: i, j
1076 type(feature_config_t) :: merged_feature
1077 integer, parameter :: test_compilers(3) = [id_gcc, id_flang, id_intel_classic_nix]
1078 integer, parameter :: test_oses(3) = [OS_LINUX, OS_WINDOWS, OS_MACOS]
1079 integer :: comp_idx, os_idx
1080
1081 ! Test merge conflicts for common compiler/OS combinations
1082 do comp_idx = 1, size(test_compilers)
1083 do os_idx = 1, size(test_oses)
1084
1085 ! Start with base feature
1086 merged_feature = collection%base
1087
1088 ! Try to merge all applicable variants for this target
1089 if (allocated(collection%variants)) then
1090 do i = 1, size(collection%variants)
1091 if (variant_applies_to_target(collection%variants(i), &
1092 test_compilers(comp_idx), test_oses(os_idx))) then
1093
1094 ! Check for allocatable property conflicts before merging
1095 call check_single_merge_conflict(merged_feature, collection%variants(i), &
1096 test_compilers(comp_idx), test_oses(os_idx), error)
1097 if (allocated(error)) return
1098
1099 ! Simulate the merge (without error checking since we just checked)
1100 call simulate_merge(merged_feature, collection%variants(i))
1101 end if
1102 end do
1103 end if
1104 end do
1105 end do
1106 end subroutine check_merge_conflicts
1107
1108 !> Check if a variant applies to the given target compiler and OS
1109 logical function variant_applies_to_target(variant, target_compiler, target_os)
1110 type(feature_config_t), intent(in) :: variant
1111 integer, intent(in) :: target_compiler, target_os
1112
1113 ! A variant applies if:
1114 ! - Compiler matches or is id_all
1115 ! - OS matches or is OS_ALL
1116 variant_applies_to_target = &
1117 (variant%platform%compiler == target_compiler .or. variant%platform%compiler == id_all) .and. &
1118 (variant%platform%os_type == target_os .or. variant%platform%os_type == OS_ALL)
1119 end function variant_applies_to_target
1120
1121 !> Check for conflicts between two features that would be merged
1122 subroutine check_single_merge_conflict(target, source, compiler_id, os_id, error)
1123 type(feature_config_t), intent(in) :: target, source
1124 integer, intent(in) :: compiler_id, os_id
1125 type(error_t), allocatable, intent(out) :: error
1126
1127 character(len=:), allocatable :: compiler, os
1128
1129 compiler = compiler_id_name(compiler_id)
1130 os = OS_NAME(os_id)
1131
1132 if (allocated(target%build) .and. allocated(source%build)) then
1133 call fatal_error(error, "Feature '"//target%name//"' has conflicting build configurations for "// &
1134 compiler//"+"//os//": both base/variants specify build settings")
1135 return
1136 end if
1137
1138 if (allocated(target%install) .and. allocated(source%install)) then
1139 call fatal_error(error, "Feature '"//target%name//"' has conflicting install configurations for "// &
1140 compiler//"+"//os//": both base/variants specify install settings")
1141 return
1142 end if
1143
1144 if (allocated(target%fortran) .and. allocated(source%fortran)) then
1145 call fatal_error(error, "Feature '"//target%name//"' has conflicting fortran configurations for "// &
1146 compiler//"+"//os//": both base/variants specify fortran settings")
1147 return
1148 end if
1149
1150 if (allocated(target%library) .and. allocated(source%library)) then
1151 call fatal_error(error, "Feature '"//target%name//"' has conflicting library configurations for "// &
1152 compiler//"+"//os//": both base/variants specify library settings")
1153 return
1154 end if
1155 end subroutine check_single_merge_conflict
1156
1157 !> Simulate merging source into target for conflict checking (without error handling)
1158 subroutine simulate_merge(target, source)
1159 type(feature_config_t), intent(inout) :: target
1160 type(feature_config_t), intent(in) :: source
1161
1162 ! Only merge allocatable properties if target doesn't have them
1163 ! (conflicts should have been caught by check_single_merge_conflict)
1164
1165 if (allocated(source%build) .and. .not. allocated(target%build)) then
1166 allocate(target%build)
1167 target%build = source%build
1168 end if
1169
1170 if (allocated(source%install) .and. .not. allocated(target%install)) then
1171 allocate(target%install)
1172 target%install = source%install
1173 end if
1174
1175 if (allocated(source%fortran) .and. .not. allocated(target%fortran)) then
1176 allocate(target%fortran)
1177 target%fortran = source%fortran
1178 end if
1179
1180 if (allocated(source%library) .and. .not. allocated(target%library)) then
1181 allocate(target%library)
1182 target%library = source%library
1183 end if
1184 end subroutine simulate_merge
1185
1186 !> Merge a feature configuration into an existing global package
1187 subroutine merge_into_package(self, package, target, error)
1188 class(feature_collection_t), intent(in) :: self
1189
1190 class(feature_config_t), intent(inout) :: package
1191
1192 type(platform_config_t), intent(in) :: target
1193
1194 type(error_t), allocatable, intent(out) :: error
1195
1196 type(feature_config_t) :: feature
1197
1198 ! Extract the feature configuration for the target platform
1199 feature = self%extract_for_target(target, error)
1200 if (allocated(error)) return
1201
1202 ! Merge the extracted feature into the package
1203 call merge_feature_configs(package, feature, error)
1204 if (allocated(error)) return
1205
1206 end subroutine merge_into_package
1207
1208 !> Extract a merged feature configuration for the given target platform
1209 type(feature_config_t) function extract_for_target(self, target, error) result(feature)
1210 class(feature_collection_t), intent(in) :: self
1211 type(platform_config_t), intent(in) :: target
1212 type(error_t), allocatable, intent(out) :: error
1213
1214 integer :: i
1215
1216 ! Start with base feature as foundation
1217 feature = self%base
1218
1219 ! Apply matching variants on top of base
1220 if (allocated(self%variants)) then
1221 do i = 1, size(self%variants)
1222 if (self%variants(i)%platform%matches(target)) then
1223 ! Merge this variant into the feature
1224 call merge_feature_configs(feature, self%variants(i), error)
1225 if (allocated(error)) return
1226 end if
1227 end do
1228 end if
1229
1230 end function extract_for_target
1231
1232 !> Add default features to existing features array if they don't already exist
1233 subroutine add_default_features(features, error)
1234
1235 !> Instance of the feature collections array (will be resized)
1236 type(feature_collection_t), allocatable, intent(inout) :: features(:)
1237
1238 !> Error handling
1239 type(error_t), allocatable, intent(out) :: error
1240
1241 type(feature_collection_t), allocatable :: temp_features(:)
1242 type(feature_collection_t), allocatable :: default_features(:)
1243 logical :: debug_exists, release_exists
1244 integer :: i, current_size, new_size
1245
1246 ! Get default features
1247 call get_default_features(default_features, error)
1248 if (allocated(error)) return
1249
1250 ! Check if debug and release features already exist
1251 debug_exists = .false.
1252 release_exists = .false.
1253
1254 if (allocated(features)) then
1255 do i = 1, size(features)
1256 if (allocated(features(i)%base%name)) then
1257 if (features(i)%base%name == "debug") debug_exists = .true.
1258 if (features(i)%base%name == "release") release_exists = .true.
1259 end if
1260 end do
1261 current_size = size(features)
1262 else
1263 current_size = 0
1264 end if
1265
1266 ! Calculate how many features to add
1267 new_size = current_size
1268 if (.not. debug_exists) new_size = new_size + 1
1269 if (.not. release_exists) new_size = new_size + 1
1270
1271 ! If nothing to add, return
1272 if (new_size == current_size) return
1273
1274 ! Create new array with existing + missing defaults
1275 allocate(temp_features(new_size))
1276
1277 ! Copy existing features
1278 if (current_size > 0) then
1279 temp_features(1:current_size) = features(1:current_size)
1280 end if
1281
1282 ! Add missing defaults
1283 i = current_size
1284 if (.not. debug_exists) then
1285 i = i + 1
1286 temp_features(i) = default_features(1) ! debug feature
1287 end if
1288 if (.not. release_exists) then
1289 i = i + 1
1290 temp_features(i) = default_features(2) ! release feature
1291 end if
1292
1293 ! Replace the features array
1294 call move_alloc(temp_features, features)
1295
1296 end subroutine add_default_features
1297
1298
1299 !> Create a feature collection from a single feature_config_t
1300 !> The feature becomes the base configuration for all OS/compiler combinations
1301 type(feature_collection_t) function collection_from_feature(self) result(collection)
1302
1303 !> Feature configuration to convert
1304 class(feature_config_t), intent(in) :: self
1305
1306 ! Copy the feature into the base configuration
1307 collection%base = self
1308
1309 ! Set platform to all OS and all compilers for the base
1310 collection%base%platform%os_type = OS_ALL
1311 collection%base%platform%compiler = id_all
1312
1313 ! Copy the name if available
1314 if (allocated(self%name)) collection%base%name = self%name
1315
1316 ! No variants initially - just the base configuration
1317 ! (variants can be added later if needed)
1318
1319 end function collection_from_feature
1320
1321
1322 !> Check if there is a CPP preprocessor configuration
1323 elemental logical function has_cpp(self)
1324 class(feature_collection_t), intent(in) :: self
1325
1326 integer :: i
1327
1328 has_cpp = self%base%has_cpp()
1329 if (has_cpp) return
1330 if (.not.allocated(self%variants)) return
1331
1332 do i=1,size(self%variants)
1333 has_cpp = self%variants(i)%has_cpp()
1334 if (has_cpp) return
1335 end do
1336
1337 end function has_cpp
1338
1339 end module fpm_manifest_feature_collection