Debian Codegraph @

src/fpm/manifest/preprocess.f90

raw
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_manifest_preprocess
14 use fpm_error, only : error_t, syntax_error
15 use fpm_strings, only : string_t, operator(==), add_strings
16 use tomlf, only : toml_table, toml_key, toml_stat
17 use fpm_toml, only : get_value, get_list, serializable_t, set_value, set_list, &
18 set_string
19 use,intrinsic :: iso_fortran_env, only : stderr=>error_unit
20 implicit none
21 private
22
23 public :: preprocess_config_t, new_preprocessors, operator(==)
24
25 !> Configuration meta data for a preprocessor
26 type, extends(serializable_t) :: preprocess_config_t
27
28 !> Name of the preprocessor
29 character(len=:), allocatable :: name
30
31 !> Suffixes of the files to be preprocessed
32 type(string_t), allocatable :: suffixes(:)
33
34 !> Directories to search for files to be preprocessed
35 type(string_t), allocatable :: directories(:)
36
37 !> Macros to be defined for the preprocessor
38 type(string_t), allocatable :: macros(:)
39
40 contains
41
42 !> Print information on this instance
43 procedure :: info
44
45 !> Initialization
46 procedure, private :: new_cpp_config_with_macros
47 procedure, private :: new_preprocess_config
48 generic :: new => new_cpp_config_with_macros, new_preprocess_config
49
50 !> Serialization interface
51 procedure :: serializable_is_same => preprocess_is_same
52 procedure :: dump_to_toml
53 procedure :: load_from_toml
54
55 !> Operations
56 procedure :: destroy
57 procedure :: add_config
58
59 !> Properties
60 procedure :: is_cpp
61 procedure :: is_fypp
62
63 end type preprocess_config_t
64
65 character(*), parameter, private :: class_name = 'preprocess_config_t'
66
67 contains
68
69 !> Construct a new cpp preprocessor configuration with a list of macros
70 subroutine new_cpp_config_with_macros(self, macros)
71
72 !> Instance of the preprocess configuration
73 class(preprocess_config_t), intent(out) :: self
74
75 !> List of macros
76 type(string_t), intent(in) :: macros(:)
77
78 call self%destroy()
79
80 !> Set cpp
81 self%name = "cpp"
82
83 !> Set macros
84 if (size(macros)<=0) then
85 allocate(self%macros(0))
86 else
87 allocate(self%macros, source=macros)
88 end if
89
90 end subroutine new_cpp_config_with_macros
91
92 !> Construct a new preprocess configuration from TOML data structure
93 subroutine new_preprocess_config(self, table, error)
94
95 !> Instance of the preprocess configuration
96 class(preprocess_config_t), intent(out) :: self
97
98 !> Instance of the TOML data structure.
99 type(toml_table), intent(inout) :: table
100
101 !> Error handling
102 type(error_t), allocatable, intent(out) :: error
103
104 call check(table, error)
105 if (allocated(error)) return
106
107 call table%get_key(self%name)
108
109 call get_list(table, "suffixes", self%suffixes, error)
110 if (allocated(error)) return
111
112 call get_list(table, "directories", self%directories, error)
113 if (allocated(error)) return
114
115 call get_list(table, "macros", self%macros, error)
116 if (allocated(error)) return
117
118 end subroutine new_preprocess_config
119
120 !> Check local schema for allowed entries
121 subroutine check(table, error)
122
123 !> Instance of the TOML data structure.
124 type(toml_table), intent(inout) :: table
125
126 !> Error handling
127 type(error_t), allocatable, intent(inout) :: error
128
129 character(len=:), allocatable :: name
130 type(toml_key), allocatable :: list(:)
131 integer :: ikey
132
133 call table%get_key(name)
134 call table%get_keys(list)
135
136 do ikey = 1, size(list)
137 select case(list(ikey)%key)
138 !> Valid keys.
139 case("suffixes", "directories", "macros")
140 case default
141 call syntax_error(error, "Key '"//list(ikey)%key//"' not allowed in preprocessor '"//name//"'."); exit
142 end select
143 end do
144 end subroutine check
145
146 !> Construct new preprocess array from a TOML data structure.
147 subroutine new_preprocessors(preprocessors, table, error)
148
149 !> Instance of the preprocess configuration
150 type(preprocess_config_t), allocatable, intent(out) :: preprocessors(:)
151
152 !> Instance of the TOML data structure
153 type(toml_table), intent(inout) :: table
154
155 !> Error handling
156 type(error_t), allocatable, intent(out) :: error
157
158 type(toml_table), pointer :: node
159 type(toml_key), allocatable :: list(:)
160 integer :: iprep, stat
161
162 call table%get_keys(list)
163
164 ! An empty table is not allowed
165 if (size(list) == 0) then
166 call syntax_error(error, "No preprocessors defined")
167 end if
168
169 allocate(preprocessors(size(list)))
170 do iprep = 1, size(list)
171 call get_value(table, list(iprep)%key, node, stat=stat)
172 if (stat /= toml_stat%success) then
173 call syntax_error(error, "Preprocessor "//list(iprep)%key//" must be a table entry")
174 exit
175 end if
176 call preprocessors(iprep)%new(node, error)
177 if (allocated(error)) exit
178 end do
179
180 end subroutine new_preprocessors
181
182 !> Write information on this instance
183 subroutine info(self, unit, verbosity)
184
185 !> Instance of the preprocess configuration
186 class(preprocess_config_t), intent(in) :: self
187
188 !> Unit for IO
189 integer, intent(in) :: unit
190
191 !> Verbosity of the printout
192 integer, intent(in), optional :: verbosity
193
194 integer :: pr, ilink
195 character(len=*), parameter :: fmt = '("#", 1x, a, t30, a)'
196
197 if (present(verbosity)) then
198 pr = verbosity
199 else
200 pr = 1
201 end if
202
203 if (pr < 1) return
204
205 write(unit, fmt) "Preprocessor"
206 if (allocated(self%name)) then
207 write(unit, fmt) "- name", self%name
208 end if
209 if (allocated(self%suffixes)) then
210 write(unit, fmt) " - suffixes"
211 do ilink = 1, size(self%suffixes)
212 write(unit, fmt) " - " // self%suffixes(ilink)%s
213 end do
214 end if
215 if (allocated(self%directories)) then
216 write(unit, fmt) " - directories"
217 do ilink = 1, size(self%directories)
218 write(unit, fmt) " - " // self%directories(ilink)%s
219 end do
220 end if
221 if (allocated(self%macros)) then
222 write(unit, fmt) " - macros"
223 do ilink = 1, size(self%macros)
224 write(unit, fmt) " - " // self%macros(ilink)%s
225 end do
226 end if
227
228 end subroutine info
229
230 logical function preprocess_is_same(this,that)
231 class(preprocess_config_t), intent(in) :: this
232 class(serializable_t), intent(in) :: that
233
234 integer :: istr
235
236 preprocess_is_same = .false.
237
238 select type (other=>that)
239 type is (preprocess_config_t)
240 if (allocated(this%name).neqv.allocated(other%name)) return
241 if (allocated(this%name)) then
242 if (.not.(this%name==other%name)) return
243 endif
244
245 if (.not.(this%suffixes==other%suffixes)) return
246 if (.not.(this%directories==other%directories)) return
247 if (.not.(this%macros==other%macros)) return
248
249 class default
250 ! Not the same type
251 return
252 end select
253
254 !> All checks passed!
255 preprocess_is_same = .true.
256
257 end function preprocess_is_same
258
259 !> Dump install config to toml table
260 subroutine dump_to_toml(self, table, error)
261
262 !> Instance of the serializable object
263 class(preprocess_config_t), intent(inout) :: self
264
265 !> Data structure
266 type(toml_table), intent(inout) :: table
267
268 !> Error handling
269 type(error_t), allocatable, intent(out) :: error
270
271 call set_string(table, "name", self%name, error)
272 if (allocated(error)) return
273 call set_list(table, "suffixes", self%suffixes, error)
274 if (allocated(error)) return
275 call set_list(table, "directories", self%directories, error)
276 if (allocated(error)) return
277 call set_list(table, "macros", self%macros, error)
278 if (allocated(error)) return
279
280 end subroutine dump_to_toml
281
282 !> Read install config from toml table (no checks made at this stage)
283 subroutine load_from_toml(self, table, error)
284
285 !> Instance of the serializable object
286 class(preprocess_config_t), intent(inout) :: self
287
288 !> Data structure
289 type(toml_table), intent(inout) :: table
290
291 !> Error handling
292 type(error_t), allocatable, intent(out) :: error
293
294 call get_value(table, "name", self%name)
295 call get_list(table, "suffixes", self%suffixes, error)
296 if (allocated(error)) return
297 call get_list(table, "directories", self%directories, error)
298 if (allocated(error)) return
299 call get_list(table, "macros", self%macros, error)
300 if (allocated(error)) return
301
302 end subroutine load_from_toml
303
304 !> Clean preprocessor structure
305 elemental subroutine destroy(this)
306 class(preprocess_config_t), intent(inout) :: this
307
308 if (allocated(this%name))deallocate(this%name)
309 if (allocated(this%suffixes))deallocate(this%suffixes)
310 if (allocated(this%directories))deallocate(this%directories)
311 if (allocated(this%macros))deallocate(this%macros)
312
313 end subroutine destroy
314
315 !> Add preprocessor settings
316 subroutine add_config(this,that)
317 class(preprocess_config_t), intent(inout) :: this
318 type(preprocess_config_t), intent(in) :: that
319
320 if (.not.that%is_cpp()) then
321 write(stderr, '(a)') 'Warning: Preprocessor ' // that%name // &
322 ' is not supported; will ignore it'
323 return
324 end if
325
326 if (.not.allocated(this%name)) this%name = that%name
327
328 ! Add macros
329 if (allocated(that%macros)) &
330 call add_strings(this%macros, that%macros)
331
332 ! Add suffixes
333 if (allocated(that%suffixes)) &
334 call add_strings(this%suffixes, that%suffixes)
335
336 ! Add directories
337 if (allocated(that%directories)) &
338 call add_strings(this%directories, that%directories)
339
340 end subroutine add_config
341
342 ! Check cpp
343 elemental logical function is_cpp(this)
344 class(preprocess_config_t), intent(in) :: this
345 is_cpp = .false.
346 if (allocated(this%name)) is_cpp = this%name == "cpp"
347 end function is_cpp
348
349 ! Check cpp
350 logical function is_fypp(this)
351 class(preprocess_config_t), intent(in) :: this
352 is_fypp = .false.
353 if (allocated(this%name)) is_fypp = this%name == "fypp"
354 end function is_fypp
355
356 end module fpm_manifest_preprocess