@@ -51,9 +51,11 @@ subroutine collect_module_dependencies(testsuite)
51
51
& new_unittest(" subdirectory-module-use" , &
52
52
test_subdirectory_module_use), &
53
53
& new_unittest(" invalid-subdirectory-module-use" , &
54
- test_invalid_subdirectory_module_use, should_fail= .true. ) &
54
+ test_invalid_subdirectory_module_use, should_fail= .true. ), &
55
+ & new_unittest(" tree-shake-module" , &
56
+ test_tree_shake_module, should_fail= .false. ) &
55
57
]
56
-
58
+
57
59
end subroutine collect_module_dependencies
58
60
59
61
@@ -495,6 +497,77 @@ subroutine test_package_module_duplicates_two_packages(error)
495
497
end if
496
498
end subroutine test_package_module_duplicates_two_packages
497
499
500
+
501
+ ! > Check tree-shaking of unused modules
502
+ ! > Unused module should not be included in targets
503
+ subroutine test_tree_shake_module (error )
504
+
505
+ ! > Error handling
506
+ type (error_t), allocatable , intent (out ) :: error
507
+
508
+ type (fpm_model_t) :: model
509
+ type (build_target_ptr), allocatable :: targets(:)
510
+ character (:), allocatable :: scope_str
511
+
512
+ allocate (model% external_modules(0 ))
513
+ allocate (model% packages(1 ))
514
+ allocate (model% packages(1 )% sources(4 ))
515
+
516
+ model% packages(1 )% sources(1 ) = new_test_source(FPM_UNIT_MODULE,file_name= " src/my_mod_1.f90" , &
517
+ scope = FPM_SCOPE_LIB, &
518
+ provides= [string_t(' my_mod_1' )]) ! indirectly used
519
+
520
+ model% packages(1 )% sources(2 ) = new_test_source(FPM_UNIT_MODULE,file_name= " src/my_mod_2.f90" , &
521
+ scope = FPM_SCOPE_LIB, &
522
+ provides= [string_t(' my_mod_2' )], &
523
+ uses= [string_t(' my_mod_1' )]) ! directly used
524
+
525
+ model% packages(1 )% sources(3 ) = new_test_source(FPM_UNIT_MODULE,file_name= " src/my_mod_3.f90" , &
526
+ scope = FPM_SCOPE_LIB, &
527
+ provides= [string_t(' my_mod_3' )]) ! unused module
528
+
529
+ model% packages(1 )% sources(4 ) = new_test_source(FPM_UNIT_PROGRAM,file_name= " app/my_program.f90" , &
530
+ scope= FPM_SCOPE_APP, &
531
+ uses= [string_t(' my_mod_2' )])
532
+
533
+ call targets_from_sources(targets,model,error)
534
+ if (allocated (error)) return
535
+
536
+ if (size (targets) /= 5 ) then
537
+ call test_failed(error,scope_str// ' Incorrect number of targets - expecting three' )
538
+ return
539
+ end if
540
+
541
+ call check_target(targets(1 )% ptr,type= FPM_TARGET_ARCHIVE,n_depends= 2 , &
542
+ deps= [targets(2 ),targets(3 )], &
543
+ links= [targets(2 ),targets(3 )],error= error)
544
+
545
+ if (allocated (error)) return
546
+
547
+ call check_target(targets(2 )% ptr,type= FPM_TARGET_OBJECT,n_depends= 0 , &
548
+ source= model% packages(1 )% sources(1 ),error= error)
549
+
550
+ if (allocated (error)) return
551
+
552
+ call check_target(targets(3 )% ptr,type= FPM_TARGET_OBJECT,n_depends= 1 , &
553
+ deps= [targets(2 )],source= model% packages(1 )% sources(2 ),error= error)
554
+
555
+ if (allocated (error)) return
556
+
557
+ call check_target(targets(4 )% ptr,type= FPM_TARGET_OBJECT,n_depends= 1 , &
558
+ deps= [targets(3 )],source= model% packages(1 )% sources(4 ),error= error)
559
+
560
+ if (allocated (error)) return
561
+
562
+ call check_target(targets(5 )% ptr,type= FPM_TARGET_EXECUTABLE,n_depends= 2 , &
563
+ deps= [targets(1 ),targets(4 )], &
564
+ links= [targets(4 )], error= error)
565
+
566
+ if (allocated (error)) return
567
+
568
+ end subroutine test_tree_shake_module
569
+
570
+
498
571
! > Check program using a non-library module in a differente sub-directory
499
572
subroutine test_invalid_subdirectory_module_use (error )
500
573
0 commit comments