1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
|
!> Define tests for the `fpm_sources` module (module dependency checking)
module test_module_dependencies
use testsuite, only : new_unittest, unittest_t, error_t, test_failed
use fpm_targets, only: targets_from_sources, resolve_module_dependencies, &
resolve_target_linking, build_target_t, build_target_ptr, &
FPM_TARGET_EXECUTABLE, FPM_TARGET_OBJECT, FPM_TARGET_ARCHIVE
use fpm_model, only: fpm_model_t, srcfile_t, &
FPM_UNIT_UNKNOWN, FPM_UNIT_PROGRAM, FPM_UNIT_MODULE, &
FPM_UNIT_SUBMODULE, FPM_UNIT_SUBPROGRAM, FPM_UNIT_CSOURCE, &
FPM_UNIT_CHEADER, FPM_SCOPE_UNKNOWN, FPM_SCOPE_LIB, &
FPM_SCOPE_DEP, FPM_SCOPE_APP, FPM_SCOPE_TEST
use fpm_strings, only: string_t, operator(.in.)
use fpm, only: check_modules_for_duplicates
implicit none
private
public :: collect_module_dependencies, operator(.in.)
interface operator(.in.)
module procedure target_in
end interface
contains
!> Collect all exported unit tests
subroutine collect_module_dependencies(testsuite)
!> Collection of tests
type(unittest_t), allocatable, intent(out) :: testsuite(:)
testsuite = [ &
& new_unittest("library-module-use", test_library_module_use), &
& new_unittest("program-module-use", test_program_module_use), &
& new_unittest("program-with-module", test_program_with_module), &
& new_unittest("program-own-module-use", test_program_own_module_use), &
& new_unittest("missing-library-use", &
test_missing_library_use, should_fail=.true.), &
& new_unittest("missing-program-use", &
test_missing_program_use, should_fail=.true.), &
& new_unittest("invalid-library-use", &
test_invalid_library_use, should_fail=.true.), &
& new_unittest("package-with-no-duplicates", &
test_package_with_no_module_duplicates), &
& new_unittest("package-with-duplicates-in-same-source", &
test_package_module_duplicates_same_source, should_fail=.true.), &
& new_unittest("package-with-duplicates-in-one-package", &
test_package_module_duplicates_one_package, should_fail=.true.), &
& new_unittest("package-with-duplicates-in-two-packages", &
test_package_module_duplicates_two_packages, should_fail=.true.), &
& new_unittest("subdirectory-module-use", &
test_subdirectory_module_use), &
& new_unittest("invalid-subdirectory-module-use", &
test_invalid_subdirectory_module_use, should_fail=.true.) &
]
end subroutine collect_module_dependencies
!> Check library module using another library module
subroutine test_library_module_use(error)
!> Error handling
type(error_t), allocatable, intent(out) :: error
type(fpm_model_t) :: model
type(build_target_ptr), allocatable :: targets(:)
!model%output_directory = ''
allocate(model%external_modules(0))
allocate(model%packages(1))
allocate(model%packages(1)%sources(2))
model%packages(1)%sources(1) = new_test_source(FPM_UNIT_MODULE,file_name="src/my_mod_1.f90", &
scope = FPM_SCOPE_LIB, &
provides=[string_t('my_mod_1')])
model%packages(1)%sources(2) = new_test_source(FPM_UNIT_MODULE,file_name="src/my_mod_2.f90", &
scope = FPM_SCOPE_LIB, &
provides=[string_t('my_mod_2')], &
uses=[string_t('my_mod_1')])
call targets_from_sources(targets,model,error)
if (allocated(error)) return
if (allocated(error)) then
return
end if
if (size(targets) /= 3) then
call test_failed(error,'Incorrect number of targets - expecting three')
return
end if
call check_target(targets(1)%ptr,type=FPM_TARGET_ARCHIVE,n_depends=2, &
deps = [targets(2),targets(3)], &
links = targets(2:3), error=error)
if (allocated(error)) return
call check_target(targets(2)%ptr,type=FPM_TARGET_OBJECT,n_depends=0, &
source=model%packages(1)%sources(1),error=error)
if (allocated(error)) return
call check_target(targets(3)%ptr,type=FPM_TARGET_OBJECT,n_depends=1, &
deps=[targets(2)],source=model%packages(1)%sources(2),error=error)
if (allocated(error)) return
end subroutine test_library_module_use
!> Check a program using a library module
!> Each program generates two targets: object file and executable
!>
subroutine test_program_module_use(error)
!> Error handling
type(error_t), allocatable, intent(out) :: error
call test_scope(FPM_SCOPE_APP,error)
if (allocated(error)) return
call test_scope(FPM_SCOPE_TEST,error)
if (allocated(error)) return
contains
subroutine test_scope(exe_scope,error)
integer, intent(in) :: exe_scope
type(error_t), allocatable, intent(out) :: error
type(fpm_model_t) :: model
type(build_target_ptr), allocatable :: targets(:)
character(:), allocatable :: scope_str
!model%output_directory = ''
allocate(model%external_modules(0))
allocate(model%packages(1))
allocate(model%packages(1)%sources(2))
scope_str = merge('FPM_SCOPE_APP ','FPM_SCOPE_TEST',exe_scope==FPM_SCOPE_APP)//' - '
model%packages(1)%sources(1) = new_test_source(FPM_UNIT_MODULE,file_name="src/my_mod_1.f90", &
scope = FPM_SCOPE_LIB, &
provides=[string_t('my_mod_1')])
model%packages(1)%sources(2) = new_test_source(FPM_UNIT_PROGRAM,file_name="app/my_program.f90", &
scope=exe_scope, &
uses=[string_t('my_mod_1')])
call targets_from_sources(targets,model,error)
if (allocated(error)) return
if (size(targets) /= 4) then
call test_failed(error,scope_str//'Incorrect number of targets - expecting three')
return
end if
call check_target(targets(1)%ptr,type=FPM_TARGET_ARCHIVE,n_depends=1, &
deps=[targets(2)],links=[targets(2)],error=error)
if (allocated(error)) return
call check_target(targets(2)%ptr,type=FPM_TARGET_OBJECT,n_depends=0, &
source=model%packages(1)%sources(1),error=error)
if (allocated(error)) return
call check_target(targets(3)%ptr,type=FPM_TARGET_OBJECT,n_depends=1, &
deps=[targets(2)],source=model%packages(1)%sources(2),error=error)
if (allocated(error)) return
call check_target(targets(4)%ptr,type=FPM_TARGET_EXECUTABLE,n_depends=2, &
deps=[targets(1),targets(3)], &
links=[targets(3)], error=error)
if (allocated(error)) return
end subroutine test_scope
end subroutine test_program_module_use
!> Check program with module in single source file
!> (Resulting target should not include itself as a dependency)
subroutine test_program_with_module(error)
!> Error handling
type(error_t), allocatable, intent(out) :: error
type(fpm_model_t) :: model
type(build_target_ptr), allocatable :: targets(:)
!model%output_directory = ''
allocate(model%external_modules(0))
allocate(model%packages(1))
allocate(model%packages(1)%sources(1))
model%packages(1)%sources(1) = new_test_source(FPM_UNIT_PROGRAM,file_name="app/my_program.f90", &
scope = FPM_SCOPE_APP, &
provides=[string_t('app_mod')], &
uses=[string_t('app_mod')])
call targets_from_sources(targets,model,error)
if (allocated(error)) return
if (size(targets) /= 2) then
write(*,*) size(targets)
call test_failed(error,'Incorrect number of targets - expecting two')
return
end if
call check_target(targets(1)%ptr,type=FPM_TARGET_OBJECT,n_depends=0, &
source=model%packages(1)%sources(1),error=error)
if (allocated(error)) return
call check_target(targets(2)%ptr,type=FPM_TARGET_EXECUTABLE,n_depends=1, &
deps=[targets(1)],links=[targets(1)],error=error)
if (allocated(error)) return
end subroutine test_program_with_module
!> Check program using modules in same directory
subroutine test_program_own_module_use(error)
!> Error handling
type(error_t), allocatable, intent(out) :: error
call test_scope(FPM_SCOPE_APP,error)
if (allocated(error)) return
call test_scope(FPM_SCOPE_TEST,error)
if (allocated(error)) return
contains
subroutine test_scope(exe_scope,error)
integer, intent(in) :: exe_scope
type(error_t), allocatable, intent(out) :: error
type(fpm_model_t) :: model
type(build_target_ptr), allocatable :: targets(:)
character(:), allocatable :: scope_str
!model%output_directory = ''
allocate(model%external_modules(0))
allocate(model%packages(1))
allocate(model%packages(1)%sources(3))
scope_str = merge('FPM_SCOPE_APP ','FPM_SCOPE_TEST',exe_scope==FPM_SCOPE_APP)//' - '
model%packages(1)%sources(1) = new_test_source(FPM_UNIT_MODULE,file_name="app/app_mod1.f90", &
scope = exe_scope, &
provides=[string_t('app_mod1')])
model%packages(1)%sources(2) = new_test_source(FPM_UNIT_MODULE,file_name="app/app_mod2.f90", &
scope = exe_scope, &
provides=[string_t('app_mod2')],uses=[string_t('app_mod1')])
model%packages(1)%sources(3) = new_test_source(FPM_UNIT_PROGRAM,file_name="app/my_program.f90", &
scope=exe_scope, &
uses=[string_t('app_mod2')])
call targets_from_sources(targets,model,error)
if (allocated(error)) return
if (size(targets) /= 4) then
call test_failed(error,scope_str//'Incorrect number of targets - expecting three')
return
end if
call check_target(targets(1)%ptr,type=FPM_TARGET_OBJECT,n_depends=0, &
source=model%packages(1)%sources(1),error=error)
if (allocated(error)) return
call check_target(targets(2)%ptr,type=FPM_TARGET_OBJECT,n_depends=1, &
source=model%packages(1)%sources(2),deps=[targets(1)],error=error)
if (allocated(error)) return
call check_target(targets(3)%ptr,type=FPM_TARGET_OBJECT,n_depends=1, &
source=model%packages(1)%sources(3),deps=[targets(2)],error=error)
if (allocated(error)) return
call check_target(targets(4)%ptr,type=FPM_TARGET_EXECUTABLE,n_depends=1, &
deps=[targets(3)],links=targets(1:3), error=error)
if (allocated(error)) return
end subroutine test_scope
end subroutine test_program_own_module_use
!> Check missing library module dependency
subroutine test_missing_library_use(error)
!> Error handling
type(error_t), allocatable, intent(out) :: error
type(fpm_model_t) :: model
type(build_target_ptr), allocatable :: targets(:)
!model%output_directory = ''
allocate(model%external_modules(0))
allocate(model%packages(1))
allocate(model%packages(1)%sources(2))
model%packages(1)%sources(1) = new_test_source(FPM_UNIT_MODULE,file_name="src/my_mod_1.f90", &
scope = FPM_SCOPE_LIB, &
provides=[string_t('my_mod_1')])
model%packages(1)%sources(2) = new_test_source(FPM_UNIT_MODULE,file_name="src/my_mod_2.f90", &
scope = FPM_SCOPE_LIB, &
provides=[string_t('my_mod_2')], &
uses=[string_t('my_mod_3')])
call targets_from_sources(targets,model,error)
end subroutine test_missing_library_use
!> Check missing program module dependency
subroutine test_missing_program_use(error)
!> Error handling
type(error_t), allocatable, intent(out) :: error
type(fpm_model_t) :: model
type(build_target_ptr), allocatable :: targets(:)
!model%output_directory = ''
allocate(model%external_modules(0))
allocate(model%packages(1))
allocate(model%packages(1)%sources(2))
model%packages(1)%sources(1) = new_test_source(FPM_UNIT_MODULE,file_name="src/my_mod_1.f90", &
scope = FPM_SCOPE_LIB, &
provides=[string_t('my_mod_1')])
model%packages(1)%sources(2) = new_test_source(FPM_UNIT_PROGRAM,file_name="app/my_program.f90", &
scope=FPM_SCOPE_APP, &
uses=[string_t('my_mod_2')])
call targets_from_sources(targets,model,error)
end subroutine test_missing_program_use
!> Check library module using a non-library module
subroutine test_invalid_library_use(error)
!> Error handling
type(error_t), allocatable, intent(out) :: error
type(fpm_model_t) :: model
type(build_target_ptr), allocatable :: targets(:)
!model%output_directory = ''
allocate(model%external_modules(0))
allocate(model%packages(1))
allocate(model%packages(1)%sources(2))
model%packages(1)%sources(1) = new_test_source(FPM_UNIT_MODULE,file_name="app/app_mod.f90", &
scope = FPM_SCOPE_APP, &
provides=[string_t('app_mod')])
model%packages(1)%sources(2) = new_test_source(FPM_UNIT_MODULE,file_name="src/my_mod.f90", &
scope = FPM_SCOPE_LIB, &
provides=[string_t('my_mod')], &
uses=[string_t('app_mod')])
call targets_from_sources(targets,model,error)
end subroutine test_invalid_library_use
!> Check program using a non-library module in a sub-directory
subroutine test_subdirectory_module_use(error)
!> Error handling
type(error_t), allocatable, intent(out) :: error
type(fpm_model_t) :: model
type(build_target_ptr), allocatable :: targets(:)
!model%output_directory = ''
allocate(model%external_modules(0))
allocate(model%packages(1))
allocate(model%packages(1)%sources(2))
model%packages(1)%sources(1) = new_test_source(FPM_UNIT_MODULE,file_name="app/subdir/app_mod.f90", &
scope = FPM_SCOPE_APP, &
provides=[string_t('app_mod')])
model%packages(1)%sources(2) = new_test_source(FPM_UNIT_PROGRAM,file_name="app/my_program.f90", &
scope=FPM_SCOPE_APP, &
uses=[string_t('app_mod')])
call targets_from_sources(targets,model,error)
end subroutine test_subdirectory_module_use
!> Check program with no duplicate modules
subroutine test_package_with_no_module_duplicates(error)
type(error_t), allocatable, intent(out) :: error
type(fpm_model_t) :: model
logical :: duplicates_found = .false.
allocate(model%packages(1))
allocate(model%packages(1)%sources(2))
model%packages(1)%sources(1) = new_test_source(FPM_UNIT_MODULE,file_name="src/my_mod_1.f90", &
scope = FPM_SCOPE_LIB, provides=[string_t('my_mod_1')])
model%packages(1)%sources(2) = new_test_source(FPM_UNIT_MODULE,file_name="src/my_mod_2.f90", &
scope = FPM_SCOPE_LIB, provides=[string_t('my_mod_2')])
call check_modules_for_duplicates(model, duplicates_found)
if (duplicates_found) then
call test_failed(error,'Duplicate modules found')
return
end if
end subroutine test_package_with_no_module_duplicates
!> Check program with duplicate modules in same source file
subroutine test_package_module_duplicates_same_source(error)
type(error_t), allocatable, intent(out) :: error
type(fpm_model_t) :: model
logical :: duplicates_found
allocate(model%packages(1))
allocate(model%packages(1)%sources(1))
model%packages(1)%sources(1) = new_test_source(FPM_UNIT_MODULE,file_name="src/my_mod_1.f90", &
scope = FPM_SCOPE_LIB, provides=[string_t('my_mod_1'), string_t('my_mod_1')])
call check_modules_for_duplicates(model, duplicates_found)
if (duplicates_found) then
call test_failed(error,'Duplicate modules found')
return
end if
end subroutine test_package_module_duplicates_same_source
!> Check program with duplicate modules in two different source files in one package
subroutine test_package_module_duplicates_one_package(error)
type(error_t), allocatable, intent(out) :: error
type(fpm_model_t) :: model
logical :: duplicates_found
allocate(model%packages(1))
allocate(model%packages(1)%sources(2))
model%packages(1)%sources(1) = new_test_source(FPM_UNIT_MODULE,file_name="src/my_mod_1_a.f90", &
scope = FPM_SCOPE_LIB, provides=[string_t('my_mod_1')])
model%packages(1)%sources(2) = new_test_source(FPM_UNIT_MODULE,file_name="src/my_mod_1_b.f90", &
scope = FPM_SCOPE_LIB, provides=[string_t('my_mod_1')])
call check_modules_for_duplicates(model, duplicates_found)
if (duplicates_found) then
call test_failed(error,'Duplicate modules found')
return
end if
end subroutine test_package_module_duplicates_one_package
!> Check program with duplicate modules in two different packages
subroutine test_package_module_duplicates_two_packages(error)
type(error_t), allocatable, intent(out) :: error
type(fpm_model_t) :: model
logical :: duplicates_found
allocate(model%packages(2))
allocate(model%packages(1)%sources(1))
allocate(model%packages(2)%sources(1))
model%packages(1)%sources(1) = new_test_source(FPM_UNIT_MODULE,file_name="src/subdir1/my_mod_1.f90", &
scope = FPM_SCOPE_LIB, provides=[string_t('my_mod_1')])
model%packages(2)%sources(1) = new_test_source(FPM_UNIT_MODULE,file_name="src/subdir2/my_mod_1.f90", &
scope = FPM_SCOPE_LIB, provides=[string_t('my_mod_1')])
call check_modules_for_duplicates(model, duplicates_found)
if (duplicates_found) then
call test_failed(error,'Duplicate modules found')
return
end if
end subroutine test_package_module_duplicates_two_packages
!> Check program using a non-library module in a differente sub-directory
subroutine test_invalid_subdirectory_module_use(error)
!> Error handling
type(error_t), allocatable, intent(out) :: error
type(fpm_model_t) :: model
type(build_target_ptr), allocatable :: targets(:)
!model%output_directory = ''
allocate(model%external_modules(0))
allocate(model%packages(1))
allocate(model%packages(1)%sources(2))
model%packages(1)%sources(1) = new_test_source(FPM_UNIT_MODULE,file_name="app/diff_dir/app_mod.f90", &
scope = FPM_SCOPE_APP, &
provides=[string_t('app_mod')])
model%packages(1)%sources(2) = new_test_source(FPM_UNIT_PROGRAM,file_name="app/prog_dir/my_program.f90", &
scope=FPM_SCOPE_APP, &
uses=[string_t('app_mod')])
call targets_from_sources(targets,model,error)
end subroutine test_invalid_subdirectory_module_use
!> Helper to create a new srcfile_t
function new_test_source(type,file_name, scope, uses, provides) result(src)
integer, intent(in) :: type
character(*), intent(in) :: file_name
integer, intent(in) :: scope
type(string_t), intent(in), optional :: uses(:)
type(string_t), intent(in), optional :: provides(:)
type(srcfile_t) :: src
src%file_name = file_name
src%unit_scope = scope
src%unit_type = type
if (present(provides)) then
src%modules_provided = provides
else
allocate(src%modules_provided(0))
end if
if (present(uses)) then
src%modules_used = uses
else
allocate(src%modules_used(0))
end if
allocate(src%include_dependencies(0))
end function new_test_source
!> Helper to check an expected output target
subroutine check_target(target,type,n_depends,deps,links,source,error)
type(build_target_t), intent(in) :: target
integer, intent(in) :: type
integer, intent(in) :: n_depends
type(srcfile_t), intent(in), optional :: source
type(build_target_ptr), intent(in), optional :: deps(:)
type(build_target_ptr), intent(in), optional :: links(:)
type(error_t), intent(out), allocatable :: error
integer :: i
if (target%target_type /= type) then
call test_failed(error,'Unexpected target_type for target "'//target%output_file//'"')
return
end if
if (size(target%dependencies) /= n_depends) then
call test_failed(error,'Wrong number of dependencies for target "'//target%output_file//'"')
return
end if
if (present(deps)) then
do i=1,size(deps)
if (.not.(deps(i)%ptr .in. target%dependencies)) then
call test_failed(error,'Missing dependency ('//deps(i)%ptr%output_file//&
') for target "'//target%output_file//'"')
return
end if
end do
end if
if (present(links)) then
do i=1,size(links)
if (.not.(links(i)%ptr%output_file .in. target%link_objects)) then
call test_failed(error,'Missing object ('//links(i)%ptr%output_file//&
') for executable "'//target%output_file//'"')
return
end if
end do
if (size(links) > size(target%link_objects)) then
call test_failed(error,'There are missing link objects for target "'&
//target%output_file//'"')
return
elseif (size(links) < size(target%link_objects)) then
call test_failed(error,'There are more link objects than expected for target "'&
//target%output_file//'"')
return
end if
end if
if (present(source)) then
if (allocated(target%source)) then
if (target%source%file_name /= source%file_name) then
call test_failed(error,'Incorrect source ('//target%source%file_name//') for target "'//&
target%output_file//'"'//new_line('a')//' expected "'//source%file_name//'"')
return
end if
else
call test_failed(error,'Expecting source for target "'//target%output_file//'" but none found')
return
end if
else
if (allocated(target%source)) then
call test_failed(error,'Found source ('//target%source%file_name//') for target "'//&
target%output_file//'" but none expected')
return
end if
end if
end subroutine check_target
!> Helper to check if a build target is in a list of build_target_ptr
logical function target_in(needle,haystack)
type(build_target_t), intent(in), target :: needle
type(build_target_ptr), intent(in) :: haystack(:)
integer :: i
target_in = .false.
do i=1,size(haystack)
if (associated(haystack(i)%ptr,needle)) then
target_in = .true.
return
end if
end do
end function target_in
end module test_module_dependencies
|