|
| 1 | +!> Implementation of the meta data for preprocessing. |
| 2 | +!> |
| 3 | +!> A preprocess table can currently have the following fields |
| 4 | +!> |
| 5 | +!> ```toml |
| 6 | +!> [preprocess] |
| 7 | +!> [preprocess.cpp] |
| 8 | +!> suffixes = ["F90", "f90"] |
| 9 | +!> directories = ["src/feature1", "src/models"] |
| 10 | +!> macros = [] |
| 11 | +!> ``` |
| 12 | + |
| 13 | +module fpm_mainfest_preprocess |
| 14 | + use fpm_error, only : error_t, syntax_error |
| 15 | + use fpm_strings, only : string_t |
| 16 | + use fpm_toml, only : toml_table, toml_key, toml_stat, get_value, get_list |
| 17 | + implicit none |
| 18 | + private |
| 19 | + |
| 20 | + public :: preprocess_config_t, new_preprocess_config, new_preprocessors |
| 21 | + |
| 22 | + !> Configuration meta data for a preprocessor |
| 23 | + type :: preprocess_config_t |
| 24 | + |
| 25 | + !> Name of the preprocessor |
| 26 | + character(len=:), allocatable :: name |
| 27 | + |
| 28 | + !> Suffixes of the files to be preprocessed |
| 29 | + type(string_t), allocatable :: suffixes(:) |
| 30 | + |
| 31 | + !> Directories to search for files to be preprocessed |
| 32 | + type(string_t), allocatable :: directories(:) |
| 33 | + |
| 34 | + !> Macros to be defined for the preprocessor |
| 35 | + type(string_t), allocatable :: macros(:) |
| 36 | + |
| 37 | + contains |
| 38 | + |
| 39 | + !> Print information on this instance |
| 40 | + procedure :: info |
| 41 | + |
| 42 | + end type preprocess_config_t |
| 43 | + |
| 44 | +contains |
| 45 | + |
| 46 | + !> Construct a new preprocess configuration from TOML data structure |
| 47 | + subroutine new_preprocess_config(self, table, error) |
| 48 | + |
| 49 | + !> Instance of the preprocess configuration |
| 50 | + type(preprocess_config_t), intent(out) :: self |
| 51 | + |
| 52 | + !> Instance of the TOML data structure. |
| 53 | + type(toml_table), intent(inout) :: table |
| 54 | + |
| 55 | + !> Error handling |
| 56 | + type(error_t), allocatable, intent(inout) :: error |
| 57 | + |
| 58 | + call check(table, error) |
| 59 | + if (allocated(error)) return |
| 60 | + |
| 61 | + call table%get_key(self%name) |
| 62 | + |
| 63 | + call get_list(table, "suffixes", self%suffixes, error) |
| 64 | + if (allocated(error)) return |
| 65 | + |
| 66 | + call get_list(table, "directories", self%directories, error) |
| 67 | + if (allocated(error)) return |
| 68 | + |
| 69 | + call get_list(table, "macros", self%macros, error) |
| 70 | + if (allocated(error)) return |
| 71 | + |
| 72 | + end subroutine new_preprocess_config |
| 73 | + |
| 74 | + !> Check local schema for allowed entries |
| 75 | + subroutine check(table, error) |
| 76 | + |
| 77 | + !> Instance of the TOML data structure. |
| 78 | + type(toml_table), intent(inout) :: table |
| 79 | + |
| 80 | + !> Error handling |
| 81 | + type(error_t), allocatable, intent(inout) :: error |
| 82 | + |
| 83 | + character(len=:), allocatable :: name |
| 84 | + type(toml_key), allocatable :: list(:) |
| 85 | + logical :: suffixes_present, directories_present, macros_present |
| 86 | + integer :: ikey |
| 87 | + |
| 88 | + suffixes_present = .false. |
| 89 | + directories_present = .false. |
| 90 | + macros_present = .false. |
| 91 | + |
| 92 | + call table%get_key(name) |
| 93 | + call table%get_keys(list) |
| 94 | + |
| 95 | + do ikey = 1, size(list) |
| 96 | + select case(list(ikey)%key) |
| 97 | + case default |
| 98 | + call syntax_error(error, "Key " // list(ikey)%key // "is not allowed in preprocessor"//name) |
| 99 | + exit |
| 100 | + case("suffixes") |
| 101 | + suffixes_present = .true. |
| 102 | + case("directories") |
| 103 | + directories_present = .true. |
| 104 | + case("macros") |
| 105 | + macros_present = .true. |
| 106 | + end select |
| 107 | + end do |
| 108 | + end subroutine check |
| 109 | + |
| 110 | + !> Construct new preprocess array from a TOML data structure. |
| 111 | + subroutine new_preprocessors(preprocessors, table, error) |
| 112 | + |
| 113 | + !> Instance of the preprocess configuration |
| 114 | + type(preprocess_config_t), allocatable, intent(out) :: preprocessors(:) |
| 115 | + |
| 116 | + !> Instance of the TOML data structure |
| 117 | + type(toml_table), intent(inout) :: table |
| 118 | + |
| 119 | + !> Error handling |
| 120 | + type(error_t), allocatable, intent(out) :: error |
| 121 | + |
| 122 | + type(toml_table), pointer :: node |
| 123 | + type(toml_key), allocatable :: list(:) |
| 124 | + integer :: iprep, stat |
| 125 | + |
| 126 | + call table%get_keys(list) |
| 127 | + |
| 128 | + ! An empty table is not allowed |
| 129 | + if (size(list) == 0) then |
| 130 | + call syntax_error(error, "No preprocessors defined") |
| 131 | + end if |
| 132 | + |
| 133 | + allocate(preprocessors(size(list))) |
| 134 | + do iprep = 1, size(list) |
| 135 | + call get_value(table, list(iprep)%key, node, stat=stat) |
| 136 | + if (stat /= toml_stat%success) then |
| 137 | + call syntax_error(error, "Preprocessor "//list(iprep)%key//" must be a table entry") |
| 138 | + exit |
| 139 | + end if |
| 140 | + call new_preprocess_config(preprocessors(iprep), node, error) |
| 141 | + if (allocated(error)) exit |
| 142 | + end do |
| 143 | + |
| 144 | + end subroutine new_preprocessors |
| 145 | + |
| 146 | + !> Write information on this instance |
| 147 | + subroutine info(self, unit, verbosity) |
| 148 | + |
| 149 | + !> Instance of the preprocess configuration |
| 150 | + class(preprocess_config_t), intent(in) :: self |
| 151 | + |
| 152 | + !> Unit for IO |
| 153 | + integer, intent(in) :: unit |
| 154 | + |
| 155 | + !> Verbosity of the printout |
| 156 | + integer, intent(in), optional :: verbosity |
| 157 | + |
| 158 | + integer :: pr, ilink |
| 159 | + character(len=*), parameter :: fmt = '("#", 1x, a, t30, a)' |
| 160 | + |
| 161 | + if (present(verbosity)) then |
| 162 | + pr = verbosity |
| 163 | + else |
| 164 | + pr = 1 |
| 165 | + end if |
| 166 | + |
| 167 | + if (pr < 1) return |
| 168 | + |
| 169 | + write(unit, fmt) "Preprocessor" |
| 170 | + if (allocated(self%name)) then |
| 171 | + write(unit, fmt) "- name", self%name |
| 172 | + end if |
| 173 | + if (allocated(self%suffixes)) then |
| 174 | + write(unit, fmt) " - suffixes" |
| 175 | + do ilink = 1, size(self%suffixes) |
| 176 | + write(unit, fmt) " - " // self%suffixes(ilink)%s |
| 177 | + end do |
| 178 | + end if |
| 179 | + if (allocated(self%directories)) then |
| 180 | + write(unit, fmt) " - directories" |
| 181 | + do ilink = 1, size(self%directories) |
| 182 | + write(unit, fmt) " - " // self%directories(ilink)%s |
| 183 | + end do |
| 184 | + end if |
| 185 | + if (allocated(self%macros)) then |
| 186 | + write(unit, fmt) " - macros" |
| 187 | + do ilink = 1, size(self%macros) |
| 188 | + write(unit, fmt) " - " // self%macros(ilink)%s |
| 189 | + end do |
| 190 | + end if |
| 191 | + |
| 192 | + end subroutine info |
| 193 | + |
| 194 | +end module fpm_mainfest_preprocess |
0 commit comments