From 285925cb9f0f285acee02f2fe79c0e0e2dacc27d Mon Sep 17 00:00:00 2001 From: kubajj Date: Wed, 24 Mar 2021 20:24:36 +0100 Subject: Create method check_modules to check for duplicate modules --- fpm/src/fpm.f90 | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/fpm/src/fpm.f90 b/fpm/src/fpm.f90 index 68385cd..ee07100 100644 --- a/fpm/src/fpm.f90 +++ b/fpm/src/fpm.f90 @@ -154,8 +154,45 @@ subroutine build_model(model, settings, package, error) end do if (allocated(error)) return + ! Check for duplicate modules + call check_modules(model) end subroutine build_model +! Check for duplicate modules +subroutine check_modules(model) + type(fpm_model_t), intent(in) :: model + integer :: maxsize + integer :: i,j,k,modi + type(string_t), allocatable :: modules(:) + ! Initialise the size of array + maxsize = 0 + ! Get number of modules provided by each source file + do i=1,size(model%packages(1)%sources) + if (allocated(model%packages(1)%sources(j)%modules_provided)) then + maxsize = maxsize + size(model%packages(1)%sources(i)%modules_provided) + end if + end do + ! Allocate array to contain distinct names of modules + allocate(modules(1:maxsize)) + + ! Initialise index to point at start of the newly allocated array + modi = 1 + + ! Loop through modules provided by each source file + ! Add it to the array if it is not already there + ! Otherwise print out warning about duplicates + do j=1,size(model%packages(1)%sources) + if (allocated(model%packages(1)%sources(j)%modules_provided)) then + do k=1,size(model%packages(1)%sources(j)%modules_provided) + if (model%packages(1)%sources(j)%modules_provided(k)%s.in.modules) then + print *,"Warning: Module ",model%packages(1)%sources(j)%modules_provided(k)%s," is duplicate" + else + modules(modi) = model%packages(1)%sources(j)%modules_provided(k) + end if + end do + end if + end do +end subroutine check_modules subroutine cmd_build(settings) type(fpm_build_settings), intent(in) :: settings -- cgit v1.2.3 From 8a91181c07abd428a5c6ad0f27aaa366daa015db Mon Sep 17 00:00:00 2001 From: kubajj Date: Wed, 24 Mar 2021 23:43:24 +0100 Subject: Update check modules subroutine so that it checks all packages for duplicates --- fpm/src/fpm.f90 | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/fpm/src/fpm.f90 b/fpm/src/fpm.f90 index ee07100..5b6736c 100644 --- a/fpm/src/fpm.f90 +++ b/fpm/src/fpm.f90 @@ -162,15 +162,17 @@ end subroutine build_model subroutine check_modules(model) type(fpm_model_t), intent(in) :: model integer :: maxsize - integer :: i,j,k,modi + integer :: i,j,k,l,m,modi type(string_t), allocatable :: modules(:) ! Initialise the size of array maxsize = 0 - ! Get number of modules provided by each source file - do i=1,size(model%packages(1)%sources) - if (allocated(model%packages(1)%sources(j)%modules_provided)) then - maxsize = maxsize + size(model%packages(1)%sources(i)%modules_provided) - end if + ! Get number of modules provided by each source file of every package + do i=1,size(model%packages) + do j=1,size(model%packages(i)%sources) + if (allocated(model%packages(i)%sources(j)%modules_provided)) then + maxsize = maxsize + size(model%packages(i)%sources(j)%modules_provided) + end if + end do end do ! Allocate array to contain distinct names of modules allocate(modules(1:maxsize)) @@ -178,19 +180,21 @@ subroutine check_modules(model) ! Initialise index to point at start of the newly allocated array modi = 1 - ! Loop through modules provided by each source file + ! Loop through modules provided by each source file of every package ! Add it to the array if it is not already there ! Otherwise print out warning about duplicates - do j=1,size(model%packages(1)%sources) - if (allocated(model%packages(1)%sources(j)%modules_provided)) then - do k=1,size(model%packages(1)%sources(j)%modules_provided) - if (model%packages(1)%sources(j)%modules_provided(k)%s.in.modules) then - print *,"Warning: Module ",model%packages(1)%sources(j)%modules_provided(k)%s," is duplicate" - else - modules(modi) = model%packages(1)%sources(j)%modules_provided(k) - end if - end do - end if + do k=1,size(model%packages) + do l=1,size(model%packages(k)%sources) + if (allocated(model%packages(k)%sources(l)%modules_provided)) then + do m=1,size(model%packages(k)%sources(l)%modules_provided) + if (model%packages(k)%sources(l)%modules_provided(m)%s.in.modules) then + print *,"Warning: Module ",model%packages(k)%sources(l)%modules_provided(m)%s," is duplicate" + else + modules(modi) = model%packages(k)%sources(l)%modules_provided(m) + end if + end do + end if + end do end do end subroutine check_modules -- cgit v1.2.3 From 9a14cd1e10a26f527fde7fedb94109c6b46a262a Mon Sep 17 00:00:00 2001 From: kubajj Date: Fri, 26 Mar 2021 23:26:15 +0100 Subject: Implement changes requested in the feedback for PR #412 --- fpm/src/fpm.f90 | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/fpm/src/fpm.f90 b/fpm/src/fpm.f90 index 5b6736c..33e70f9 100644 --- a/fpm/src/fpm.f90 +++ b/fpm/src/fpm.f90 @@ -23,6 +23,7 @@ use,intrinsic :: iso_fortran_env, only : stdin=>input_unit, & & stdout=>output_unit, & & stderr=>error_unit use fpm_manifest_dependency, only: dependency_config_t +use, intrinsic :: iso_fortran_env, only: error_unit implicit none private public :: cmd_build, cmd_run @@ -43,6 +44,8 @@ subroutine build_model(model, settings, package, error) type(package_config_t) :: dependency character(len=:), allocatable :: manifest, lib_dir + logical :: duplicates_found = .false. + if(settings%verbose)then write(*,*)'BUILD_NAME:',settings%build_name write(*,*)'COMPILER: ',settings%compiler @@ -155,15 +158,19 @@ subroutine build_model(model, settings, package, error) if (allocated(error)) return ! Check for duplicate modules - call check_modules(model) + call check_modules_for_duplicates(model, duplicates_found) + if (duplicates_found) then + error stop + end if end subroutine build_model ! Check for duplicate modules -subroutine check_modules(model) +subroutine check_modules_for_duplicates(model, duplicates_found) type(fpm_model_t), intent(in) :: model integer :: maxsize integer :: i,j,k,l,m,modi type(string_t), allocatable :: modules(:) + logical :: duplicates_found ! Initialise the size of array maxsize = 0 ! Get number of modules provided by each source file of every package @@ -175,7 +182,7 @@ subroutine check_modules(model) end do end do ! Allocate array to contain distinct names of modules - allocate(modules(1:maxsize)) + allocate(modules(maxsize)) ! Initialise index to point at start of the newly allocated array modi = 1 @@ -187,16 +194,18 @@ subroutine check_modules(model) do l=1,size(model%packages(k)%sources) if (allocated(model%packages(k)%sources(l)%modules_provided)) then do m=1,size(model%packages(k)%sources(l)%modules_provided) - if (model%packages(k)%sources(l)%modules_provided(m)%s.in.modules) then - print *,"Warning: Module ",model%packages(k)%sources(l)%modules_provided(m)%s," is duplicate" + if (model%packages(k)%sources(l)%modules_provided(m)%s.in.modules(:modi-1)) then + write(error_unit, *) "Warning: Module ",model%packages(k)%sources(l)%modules_provided(m)%s," is duplicate" + duplicates_found = .true. else modules(modi) = model%packages(k)%sources(l)%modules_provided(m) + modi = modi + 1 end if end do end if end do end do -end subroutine check_modules +end subroutine check_modules_for_duplicates subroutine cmd_build(settings) type(fpm_build_settings), intent(in) :: settings -- cgit v1.2.3 From eb3846236e7d2b4af59aafd66b115e612ab5f679 Mon Sep 17 00:00:00 2001 From: kubajj Date: Fri, 26 Mar 2021 23:46:00 +0100 Subject: Resolve merge conflict --- fpm/src/fpm.f90 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fpm/src/fpm.f90 b/fpm/src/fpm.f90 index ac306b4..c75b50a 100644 --- a/fpm/src/fpm.f90 +++ b/fpm/src/fpm.f90 @@ -45,12 +45,12 @@ subroutine build_model(model, settings, package, error) character(len=:), allocatable :: manifest, lib_dir logical :: duplicates_found = .false. + type(string_t) :: include_dir if(settings%verbose)then write(*,*)'BUILD_NAME:',settings%build_name write(*,*)'COMPILER: ',settings%compiler endif - type(string_t) :: include_dir model%package_name = package%name -- cgit v1.2.3 From 6bebb81f3723c8d3a74a4d434bd0f5cccf31619e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Jel=C3=ADnek?= <33724536+kubajj@users.noreply.github.com> Date: Sat, 27 Mar 2021 17:04:31 +0100 Subject: Update fpm/src/fpm.f90 Co-authored-by: Laurence Kedward --- fpm/src/fpm.f90 | 5 ----- 1 file changed, 5 deletions(-) diff --git a/fpm/src/fpm.f90 b/fpm/src/fpm.f90 index c75b50a..d58bd17 100644 --- a/fpm/src/fpm.f90 +++ b/fpm/src/fpm.f90 @@ -47,11 +47,6 @@ subroutine build_model(model, settings, package, error) logical :: duplicates_found = .false. type(string_t) :: include_dir - if(settings%verbose)then - write(*,*)'BUILD_NAME:',settings%build_name - write(*,*)'COMPILER: ',settings%compiler - endif - model%package_name = package%name allocate(model%include_dirs(0)) -- cgit v1.2.3 From aa7eca7b9bb2344fbc60567c102e3cf413a77453 Mon Sep 17 00:00:00 2001 From: kubajj Date: Tue, 30 Mar 2021 20:52:09 +0200 Subject: Implement suggestions bu milancurcic - add a n article to duplicates Warning, add a message to error stop --- fpm/src/fpm.f90 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fpm/src/fpm.f90 b/fpm/src/fpm.f90 index c75b50a..0c8dcd9 100644 --- a/fpm/src/fpm.f90 +++ b/fpm/src/fpm.f90 @@ -190,7 +190,7 @@ subroutine build_model(model, settings, package, error) ! Check for duplicate modules call check_modules_for_duplicates(model, duplicates_found) if (duplicates_found) then - error stop + error stop 'Error: One or more duplicate module names found.' end if end subroutine build_model @@ -225,7 +225,7 @@ subroutine check_modules_for_duplicates(model, duplicates_found) if (allocated(model%packages(k)%sources(l)%modules_provided)) then do m=1,size(model%packages(k)%sources(l)%modules_provided) if (model%packages(k)%sources(l)%modules_provided(m)%s.in.modules(:modi-1)) then - write(error_unit, *) "Warning: Module ",model%packages(k)%sources(l)%modules_provided(m)%s," is duplicate" + write(error_unit, *) "Warning: Module ",model%packages(k)%sources(l)%modules_provided(m)%s," is a duplicate" duplicates_found = .true. else modules(modi) = model%packages(k)%sources(l)%modules_provided(m) -- cgit v1.2.3 From 12d1bcc7e305a736f3ec7ffd70d722da4355131b Mon Sep 17 00:00:00 2001 From: kubajj Date: Tue, 30 Mar 2021 21:30:39 +0200 Subject: Add simple tests for the module duplicates --- fpm/src/fpm.f90 | 2 +- fpm/test/fpm_test/test_module_dependencies.f90 | 106 ++++++++++++++++++++++++- 2 files changed, 106 insertions(+), 2 deletions(-) diff --git a/fpm/src/fpm.f90 b/fpm/src/fpm.f90 index 0c8dcd9..e201648 100644 --- a/fpm/src/fpm.f90 +++ b/fpm/src/fpm.f90 @@ -27,7 +27,7 @@ use, intrinsic :: iso_fortran_env, only: error_unit implicit none private public :: cmd_build, cmd_run -public :: build_model +public :: build_model, check_modules_for_duplicates contains diff --git a/fpm/test/fpm_test/test_module_dependencies.f90 b/fpm/test/fpm_test/test_module_dependencies.f90 index 7f6c0be..fc580bc 100644 --- a/fpm/test/fpm_test/test_module_dependencies.f90 +++ b/fpm/test/fpm_test/test_module_dependencies.f90 @@ -10,6 +10,7 @@ module test_module_dependencies 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 @@ -40,7 +41,17 @@ contains & new_unittest("invalid-library-use", & test_invalid_library_use, should_fail=.true.), & & new_unittest("invalid-own-module-use", & - test_invalid_own_module_use, should_fail=.true.) & + test_invalid_own_module_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-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.) & ] end subroutine collect_module_dependencies @@ -392,6 +403,99 @@ contains end subroutine test_invalid_own_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 + + 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 !> Helper to create a new srcfile_t function new_test_source(type,file_name, scope, uses, provides) result(src) -- cgit v1.2.3 From 4aff40d0b7bb882cdb0baf784c01552bfb84a843 Mon Sep 17 00:00:00 2001 From: kubajj Date: Tue, 30 Mar 2021 22:04:13 +0200 Subject: Fix issues created by the merge conflict in test module dependencies --- fpm/test/fpm_test/test_module_dependencies.f90 | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/fpm/test/fpm_test/test_module_dependencies.f90 b/fpm/test/fpm_test/test_module_dependencies.f90 index fe92aab..021375b 100644 --- a/fpm/test/fpm_test/test_module_dependencies.f90 +++ b/fpm/test/fpm_test/test_module_dependencies.f90 @@ -40,8 +40,6 @@ contains test_missing_program_use, should_fail=.true.), & & new_unittest("invalid-library-use", & test_invalid_library_use, should_fail=.true.), & - & new_unittest("invalid-own-module-use", & - test_invalid_own_module_use, should_fail=.true.), & & new_unittest("package-with-no-duplicates", & test_package_with_no_module_duplicates), & & new_unittest("package-with-duplicates-in-same-source", & @@ -404,6 +402,8 @@ contains 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) @@ -411,7 +411,7 @@ contains type(error_t), allocatable, intent(out) :: error type(fpm_model_t) :: model - logical :: duplicates_found + logical :: duplicates_found = .false. allocate(model%packages(1)) allocate(model%packages(1)%sources(2)) @@ -498,8 +498,6 @@ contains return end if end subroutine test_package_module_duplicates_two_packages - - end subroutine test_subdirectory_module_use !> Check program using a non-library module in a differente sub-directory subroutine test_invalid_subdirectory_module_use(error) -- cgit v1.2.3 From e0d336ce1aea71693c467367e19bf102f662ec43 Mon Sep 17 00:00:00 2001 From: kubajj Date: Wed, 31 Mar 2021 10:54:32 +0200 Subject: Print filename when duplicate found and remove duplicate test call --- fpm/src/fpm.f90 | 3 ++- fpm/test/fpm_test/test_module_dependencies.f90 | 2 -- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/fpm/src/fpm.f90 b/fpm/src/fpm.f90 index 1d2b0ae..31b68ff 100644 --- a/fpm/src/fpm.f90 +++ b/fpm/src/fpm.f90 @@ -220,7 +220,8 @@ subroutine check_modules_for_duplicates(model, duplicates_found) if (allocated(model%packages(k)%sources(l)%modules_provided)) then do m=1,size(model%packages(k)%sources(l)%modules_provided) if (model%packages(k)%sources(l)%modules_provided(m)%s.in.modules(:modi-1)) then - write(error_unit, *) "Warning: Module ",model%packages(k)%sources(l)%modules_provided(m)%s," is a duplicate" + write(error_unit, *) "Warning: Module ",model%packages(k)%sources(l)%modules_provided(m)%s, & + " in ",model%packages(k)%sources(l)%file_name," is a duplicate" duplicates_found = .true. else modules(modi) = model%packages(k)%sources(l)%modules_provided(m) diff --git a/fpm/test/fpm_test/test_module_dependencies.f90 b/fpm/test/fpm_test/test_module_dependencies.f90 index 021375b..f193646 100644 --- a/fpm/test/fpm_test/test_module_dependencies.f90 +++ b/fpm/test/fpm_test/test_module_dependencies.f90 @@ -44,8 +44,6 @@ contains 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-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", & -- cgit v1.2.3