Skip to content

Commit 24d9da0

Browse files
committed
serialize file_scope_flag and test
1 parent 819e0eb commit 24d9da0

File tree

2 files changed

+116
-23
lines changed

2 files changed

+116
-23
lines changed

src/fpm/manifest/profiles.f90

Lines changed: 97 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
!>
4444
module fpm_manifest_profile
4545
use fpm_error, only : error_t, syntax_error, fatal_error, fpm_stop
46-
use fpm_toml, only : toml_table, toml_key, toml_stat, get_value
46+
use fpm_toml, only : toml_table, toml_key, toml_stat, get_value, serializable_t, set_value, set_string
4747
use fpm_strings, only: lower
4848
use fpm_environment, only: get_os_type, OS_UNKNOWN, OS_LINUX, OS_MACOS, OS_WINDOWS, &
4949
OS_CYGWIN, OS_SOLARIS, OS_FREEBSD, OS_OPENBSD
@@ -53,19 +53,26 @@ module fpm_manifest_profile
5353
& info_profile, find_profile, DEFAULT_COMPILER
5454

5555
!> Name of the default compiler
56-
character(len=*), parameter :: DEFAULT_COMPILER = 'gfortran'
56+
character(len=*), parameter :: DEFAULT_COMPILER = 'gfortran'
5757
integer, parameter :: OS_ALL = -1
5858
character(len=:), allocatable :: path
5959

6060
!> Type storing file name - file scope compiler flags pairs
61-
type :: file_scope_flag
61+
type, extends(serializable_t) :: file_scope_flag
6262

6363
!> Name of the file
6464
character(len=:), allocatable :: file_name
6565

6666
!> File scope flags
6767
character(len=:), allocatable :: flags
6868

69+
contains
70+
71+
!> Serialization interface
72+
procedure :: serializable_is_same => file_scope_same
73+
procedure :: dump_to_toml => file_scope_dump
74+
procedure :: load_from_toml => file_scope_load
75+
6976
end type file_scope_flag
7077

7178
!> Configuration meta data for a profile
@@ -78,7 +85,7 @@ module fpm_manifest_profile
7885

7986
!> Value repesenting OS
8087
integer :: os_type
81-
88+
8289
!> Fortran compiler flags
8390
character(len=:), allocatable :: flags
8491

@@ -110,16 +117,16 @@ module fpm_manifest_profile
110117
function new_profile(profile_name, compiler, os_type, flags, c_flags, cxx_flags, &
111118
link_time_flags, file_scope_flags, is_built_in) &
112119
& result(profile)
113-
120+
114121
!> Name of the profile
115122
character(len=*), intent(in) :: profile_name
116-
123+
117124
!> Name of the compiler
118125
character(len=*), intent(in) :: compiler
119-
126+
120127
!> Type of the OS
121128
integer, intent(in) :: os_type
122-
129+
123130
!> Fortran compiler flags
124131
character(len=*), optional, intent(in) :: flags
125132

@@ -190,7 +197,7 @@ subroutine validate_compiler_name(compiler_name, is_valid)
190197
is_valid = .false.
191198
end select
192199
end subroutine validate_compiler_name
193-
200+
194201
!> Check if os_name is a valid name of a supported OS
195202
subroutine validate_os_name(os_name, is_valid)
196203

@@ -373,10 +380,10 @@ subroutine get_flags(profile_name, compiler_name, os_type, key_list, table, prof
373380
& flags, c_flags, cxx_flags, link_time_flags, file_scope_flags)
374381
profindex = profindex + 1
375382
end subroutine get_flags
376-
383+
377384
!> Traverse operating system tables to obtain number of profiles
378385
subroutine traverse_oss_for_size(profile_name, compiler_name, os_list, table, profiles_size, error)
379-
386+
380387
!> Name of profile
381388
character(len=:), allocatable, intent(in) :: profile_name
382389

@@ -447,7 +454,7 @@ end subroutine traverse_oss_for_size
447454

448455
!> Traverse operating system tables to obtain profiles
449456
subroutine traverse_oss(profile_name, compiler_name, os_list, table, profiles, profindex, error)
450-
457+
451458
!> Name of profile
452459
character(len=:), allocatable, intent(in) :: profile_name
453460

@@ -468,7 +475,7 @@ subroutine traverse_oss(profile_name, compiler_name, os_list, table, profiles, p
468475

469476
!> Index in the list of profiles
470477
integer, intent(inout) :: profindex
471-
478+
472479
type(toml_key), allocatable :: key_list(:)
473480
character(len=:), allocatable :: os_name, l_os_name
474481
type(toml_table), pointer :: os_node
@@ -513,7 +520,7 @@ end subroutine traverse_oss
513520

514521
!> Traverse compiler tables
515522
subroutine traverse_compilers(profile_name, comp_list, table, error, profiles_size, profiles, profindex)
516-
523+
517524
!> Name of profile
518525
character(len=:), allocatable, intent(in) :: profile_name
519526

@@ -522,10 +529,10 @@ subroutine traverse_compilers(profile_name, comp_list, table, error, profiles_si
522529

523530
!> Table containing compiler tables
524531
type(toml_table), pointer, intent(in) :: table
525-
532+
526533
!> Error handling
527534
type(error_t), allocatable, intent(out) :: error
528-
535+
529536
!> Number of profiles in list of profiles
530537
integer, intent(inout), optional :: profiles_size
531538

@@ -534,8 +541,8 @@ subroutine traverse_compilers(profile_name, comp_list, table, error, profiles_si
534541

535542
!> Index in the list of profiles
536543
integer, intent(inout), optional :: profindex
537-
538-
character(len=:), allocatable :: compiler_name
544+
545+
character(len=:), allocatable :: compiler_name
539546
type(toml_table), pointer :: comp_node
540547
type(toml_key), allocatable :: os_list(:)
541548
integer :: icomp, stat
@@ -544,7 +551,7 @@ subroutine traverse_compilers(profile_name, comp_list, table, error, profiles_si
544551
if (size(comp_list)<1) return
545552
do icomp = 1, size(comp_list)
546553
call validate_compiler_name(comp_list(icomp)%key, is_valid)
547-
if (is_valid) then
554+
if (is_valid) then
548555
compiler_name = comp_list(icomp)%key
549556
call get_value(table, compiler_name, comp_node, stat=stat)
550557
if (stat /= toml_stat%success) then
@@ -567,7 +574,7 @@ subroutine traverse_compilers(profile_name, comp_list, table, error, profiles_si
567574
else
568575
call fatal_error(error,'*traverse_compilers*:Error: Compiler name not specified or invalid.')
569576
end if
570-
end do
577+
end do
571578
end subroutine traverse_compilers
572579

573580
!> Construct new profiles array from a TOML data structure
@@ -596,9 +603,9 @@ subroutine new_profiles(profiles, table, error)
596603
default_profiles = get_default_profiles(error)
597604
if (allocated(error)) return
598605
call table%get_keys(prof_list)
599-
606+
600607
if (size(prof_list) < 1) return
601-
608+
602609
profiles_size = 0
603610

604611
do iprof = 1, size(prof_list)
@@ -633,7 +640,7 @@ subroutine new_profiles(profiles, table, error)
633640

634641
profiles_size = profiles_size + size(default_profiles)
635642
allocate(profiles(profiles_size))
636-
643+
637644
do profindex=1, size(default_profiles)
638645
profiles(profindex) = default_profiles(profindex)
639646
end do
@@ -954,4 +961,71 @@ subroutine find_profile(profiles, profile_name, compiler, os_type, found_matchin
954961
end do
955962
end if
956963
end subroutine find_profile
964+
965+
966+
logical function file_scope_same(this,that)
967+
class(file_scope_flag), intent(in) :: this
968+
class(serializable_t), intent(in) :: that
969+
970+
file_scope_same = .false.
971+
972+
select type (other=>that)
973+
type is (file_scope_flag)
974+
if (allocated(this%file_name).neqv.allocated(other%file_name)) return
975+
if (allocated(this%file_name)) then
976+
if (.not.(this%file_name==other%file_name)) return
977+
endif
978+
if (allocated(this%flags).neqv.allocated(other%flags)) return
979+
if (allocated(this%flags)) then
980+
if (.not.(this%flags==other%flags)) return
981+
endif
982+
983+
class default
984+
! Not the same type
985+
return
986+
end select
987+
988+
!> All checks passed!
989+
file_scope_same = .true.
990+
991+
end function file_scope_same
992+
993+
!> Dump to toml table
994+
subroutine file_scope_dump(self, table, error)
995+
996+
!> Instance of the serializable object
997+
class(file_scope_flag), intent(inout) :: self
998+
999+
!> Data structure
1000+
type(toml_table), intent(inout) :: table
1001+
1002+
!> Error handling
1003+
type(error_t), allocatable, intent(out) :: error
1004+
1005+
call set_string(table, "file-name", self%file_name, error)
1006+
if (allocated(error)) return
1007+
call set_string(table, "flags", self%flags, error)
1008+
if (allocated(error)) return
1009+
1010+
end subroutine file_scope_dump
1011+
1012+
!> Read from toml table (no checks made at this stage)
1013+
subroutine file_scope_load(self, table, error)
1014+
1015+
!> Instance of the serializable object
1016+
class(file_scope_flag), intent(inout) :: self
1017+
1018+
!> Data structure
1019+
type(toml_table), intent(inout) :: table
1020+
1021+
!> Error handling
1022+
type(error_t), allocatable, intent(out) :: error
1023+
1024+
call get_value(table, "file-name", self%file_name)
1025+
call get_value(table, "flags", self%flags)
1026+
1027+
end subroutine file_scope_load
1028+
1029+
1030+
9571031
end module fpm_manifest_profile

test/fpm_test/test_toml.f90

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ module test_toml
1212
use fpm_manifest_library
1313
use fpm_manifest_executable
1414
use fpm_manifest_preprocess
15+
use fpm_manifest_profile
1516
use fpm_versioning, only: new_version
1617
use fpm_strings, only: string_t, operator(==), split
1718
use fpm_model, only: fortran_features_t, package_t, FPM_SCOPE_LIB, FPM_UNIT_MODULE, fpm_model_t, &
@@ -53,6 +54,7 @@ subroutine collect_toml(testsuite)
5354
& new_unittest("serialize-library-config", library_config_roundtrip), &
5455
& new_unittest("serialize-executable-config", executable_config_roundtrip), &
5556
& new_unittest("serialize-preprocess-config", preprocess_config_roundtrip), &
57+
& new_unittest("serialize-file-scope-flag", file_scope_flag_roundtrip), &
5658
& new_unittest("serialize-string-array", string_array_roundtrip), &
5759
& new_unittest("serialize-fortran-features", fft_roundtrip), &
5860
& new_unittest("serialize-fortran-invalid", fft_invalid, should_fail=.true.), &
@@ -1271,4 +1273,21 @@ subroutine preprocess_config_roundtrip(error)
12711273

12721274
end subroutine preprocess_config_roundtrip
12731275

1276+
subroutine file_scope_flag_roundtrip(error)
1277+
1278+
!> Error handling
1279+
type(error_t), allocatable, intent(out) :: error
1280+
1281+
type(file_scope_flag) :: ff
1282+
1283+
call ff%test_serialization('file_scope_flag: empty', error)
1284+
if (allocated(error)) return
1285+
1286+
ff%file_name = "preprocessor config"
1287+
ff%flags = "-1 -f -2 -g"
1288+
1289+
call ff%test_serialization('file_scope_flag: non-empty', error)
1290+
1291+
end subroutine file_scope_flag_roundtrip
1292+
12741293
end module test_toml

0 commit comments

Comments
 (0)