Debian Codegraph @

src/fpm/manifest/build.f90

raw
1 !> Implementation of the build configuration data.
2 !>
3 !> A build table can currently have the following fields
4 !>
5 !>```toml
6 !>[build]
7 !>auto-executables = bool
8 !>auto-examples = bool
9 !>auto-tests = bool
10 !>link = ["lib"]
11 !>```
12 module fpm_manifest_build
13 use fpm_error, only : error_t, syntax_error, fatal_error
14 use fpm_strings, only : string_t, len_trim, is_valid_module_prefix, operator(==)
15 use tomlf, only: toml_table, toml_key, toml_stat
16 use fpm_toml, only : serializable_t, get_value, get_list, set_value, set_string, set_list
17
18 implicit none
19 private
20
21 public :: build_config_t, new_build_config
22
23 !> Configuration data for build
24 type, extends(serializable_t) :: build_config_t
25
26 !> Automatic discovery of executables
27 logical :: auto_executables = .true.
28
29 !> Automatic discovery of examples
30 logical :: auto_examples = .true.
31
32 !> Automatic discovery of tests
33 logical :: auto_tests = .true.
34
35 !> Enforcing of package module names
36 logical :: module_naming = .false.
37 type(string_t) :: module_prefix
38
39 !> Libraries to link against
40 type(string_t), allocatable :: link(:)
41
42 !> External modules to use
43 type(string_t), allocatable :: external_modules(:)
44
45 contains
46
47 !> Print information on this instance
48 procedure :: info
49
50 !> Serialization interface
51 procedure :: serializable_is_same => build_conf_is_same
52 procedure :: dump_to_toml
53 procedure :: load_from_toml
54
55 end type build_config_t
56
57 character(*), parameter, private :: class_name = 'build_config_t'
58
59
60 contains
61
62
63 !> Construct a new build configuration from a TOML data structure
64 subroutine new_build_config(self, table, package_name, error)
65
66 !> Instance of the build configuration
67 type(build_config_t), intent(out) :: self
68
69 !> Instance of the TOML data structure
70 type(toml_table), intent(inout) :: table
71
72 !> Package name
73 character(len=*), intent(in) :: package_name
74
75 !> Error handling
76 type(error_t), allocatable, intent(out) :: error
77
78 integer :: stat
79
80 call check(table, package_name, error)
81 if (allocated(error)) return
82
83 call get_value(table, "auto-executables", self%auto_executables, .true., stat=stat)
84
85 if (stat /= toml_stat%success) then
86 call fatal_error(error,"Error while reading value for 'auto-executables' in fpm.toml, expecting logical")
87 return
88 end if
89
90 call get_value(table, "auto-tests", self%auto_tests, .true., stat=stat)
91
92 if (stat /= toml_stat%success) then
93 call fatal_error(error,"Error while reading value for 'auto-tests' in fpm.toml, expecting logical")
94 return
95 end if
96
97 call get_value(table, "auto-examples", self%auto_examples, .true., stat=stat)
98
99 if (stat /= toml_stat%success) then
100 call fatal_error(error,"Error while reading value for 'auto-examples' in fpm.toml, expecting logical")
101 return
102 end if
103
104 !> Module naming: fist, attempt boolean value first
105 call get_value(table, "module-naming", self%module_naming, .false., stat=stat)
106
107 if (stat == toml_stat%success) then
108
109 ! Boolean value found. Set no custom prefix. This also falls back to key not provided
110 if (allocated(self%module_prefix%s)) deallocate(self%module_prefix%s)
111
112 else
113
114 !> Value found, but not a boolean. Attempt to read a prefix string
115 call get_value(table, "module-naming", self%module_prefix%s)
116
117 if (.not.allocated(self%module_prefix%s)) then
118 call syntax_error(error,"Could not read value for 'module-naming' in fpm.toml, expecting logical or a string")
119 return
120 end if
121
122 if (.not.is_valid_module_prefix(self%module_prefix)) then
123 call syntax_error(error,"Invalid custom module name prefix for in fpm.toml: <"//self%module_prefix%s// &
124 ">, expecting a valid alphanumeric string")
125 return
126 end if
127
128 ! Set module naming to ON
129 self%module_naming = .true.
130
131 end if
132
133 call get_list(table, "link", self%link, error)
134 if (allocated(error)) return
135
136 call get_list(table, "external-modules", self%external_modules, error)
137 if (allocated(error)) return
138
139 end subroutine new_build_config
140
141 !> Check local schema for allowed entries
142 subroutine check(table, package_name, error)
143
144 !> Instance of the TOML data structure
145 type(toml_table), intent(inout) :: table
146
147 !> Package name
148 character(len=*), intent(in) :: package_name
149
150 !> Error handling
151 type(error_t), allocatable, intent(out) :: error
152
153 type(toml_key), allocatable :: list(:)
154 integer :: ikey
155
156 call table%get_keys(list)
157
158 ! table can be empty
159 if (size(list) < 1) return
160
161 do ikey = 1, size(list)
162 select case(list(ikey)%key)
163
164 case("auto-executables", "auto-examples", "auto-tests", "link", "external-modules", "module-naming")
165 continue
166
167 case default
168
169 call syntax_error(error, 'Manifest file syntax error: key "'//list(ikey)%key//'" found in the [build] '//&
170 'section of package/dependency "'//package_name//'" fpm.toml is not allowed')
171 exit
172
173 end select
174 end do
175
176 end subroutine check
177
178
179 !> Write information on build configuration instance
180 subroutine info(self, unit, verbosity)
181
182 !> Instance of the build configuration
183 class(build_config_t), intent(in) :: self
184
185 !> Unit for IO
186 integer, intent(in) :: unit
187
188 !> Verbosity of the printout
189 integer, intent(in), optional :: verbosity
190
191 integer :: pr, ilink, imod
192 character(len=*), parameter :: fmt = '("#", 1x, a, t30, a)'
193
194 if (present(verbosity)) then
195 pr = verbosity
196 else
197 pr = 1
198 end if
199
200 if (pr < 1) return
201
202 write(unit, fmt) "Build configuration"
203 write(unit, fmt) " - auto-discovery (apps) ", merge("enabled ", "disabled", self%auto_executables)
204 write(unit, fmt) " - auto-discovery (examples) ", merge("enabled ", "disabled", self%auto_examples)
205 write(unit, fmt) " - auto-discovery (tests) ", merge("enabled ", "disabled", self%auto_tests)
206 write(unit, fmt) " - enforce module naming ", merge("enabled ", "disabled", self%module_naming)
207 if (allocated(self%link)) then
208 write(unit, fmt) " - link against"
209 do ilink = 1, size(self%link)
210 write(unit, fmt) " - " // self%link(ilink)%s
211 end do
212 end if
213 if (allocated(self%external_modules)) then
214 write(unit, fmt) " - external modules"
215 do imod = 1, size(self%external_modules)
216 write(unit, fmt) " - " // self%external_modules(imod)%s
217 end do
218 end if
219
220 end subroutine info
221
222 !> Check that two dependency trees are equal
223 logical function build_conf_is_same(this,that)
224 class(build_config_t), intent(in) :: this
225 class(serializable_t), intent(in) :: that
226
227 build_conf_is_same = .false.
228
229 select type (other=>that)
230 type is (build_config_t)
231
232 if (this%auto_executables.neqv.other%auto_executables) return
233 if (this%auto_examples.neqv.other%auto_examples) return
234 if (this%auto_tests.neqv.other%auto_tests) return
235 if (this%module_naming.neqv.other%module_naming) return
236 if (.not.this%module_prefix==other%module_prefix) return
237 if (.not.this%link==other%link) return
238 if (.not.this%external_modules==other%external_modules) return
239
240 class default
241 ! Not the same type
242 return
243 end select
244
245 !> All checks passed!
246 build_conf_is_same = .true.
247
248 end function build_conf_is_same
249
250 !> Dump build config to toml table
251 subroutine dump_to_toml(self, table, error)
252
253 !> Instance of the serializable object
254 class(build_config_t), intent(inout) :: self
255
256 !> Data structure
257 type(toml_table), intent(inout) :: table
258
259 !> Error handling
260 type(error_t), allocatable, intent(out) :: error
261
262 call set_value(table, "auto-executables", self%auto_executables, error, class_name)
263 if (allocated(error)) return
264 call set_value(table, "auto-tests", self%auto_tests, error, class_name)
265 if (allocated(error)) return
266 call set_value(table, "auto-examples", self%auto_examples, error, class_name)
267 if (allocated(error)) return
268
269 ! Module naming can either contain a boolean value, or the prefix
270 has_prefix: if (self%module_naming .and. len_trim(self%module_prefix)>0) then
271 call set_string(table, "module-naming", self%module_prefix, error, class_name)
272 else
273 call set_value (table, "module-naming", self%module_naming, error, class_name)
274 end if has_prefix
275 if (allocated(error)) return
276
277 call set_list(table, "link", self%link, error)
278 if (allocated(error)) return
279 call set_list(table, "external-modules", self%external_modules, error)
280 if (allocated(error)) return
281
282 end subroutine dump_to_toml
283
284 !> Read build config from toml table (no checks made at this stage)
285 subroutine load_from_toml(self, table, error)
286
287 !> Instance of the serializable object
288 class(build_config_t), intent(inout) :: self
289
290 !> Data structure
291 type(toml_table), intent(inout) :: table
292
293 !> Error handling
294 type(error_t), allocatable, intent(out) :: error
295
296 integer :: stat
297
298 call get_value(table, "auto-executables", self%auto_executables, error, class_name)
299 if (allocated(error)) return
300 call get_value(table, "auto-tests", self%auto_tests, error, class_name)
301 if (allocated(error)) return
302 call get_value(table, "auto-examples", self%auto_examples, error, class_name)
303 if (allocated(error)) return
304
305 !> Module naming: fist, attempt boolean value first
306 call get_value(table, "module-naming", self%module_naming, .false., stat=stat)
307 if (stat == toml_stat%success) then
308 ! Boolean value found. Set no custom prefix. This also falls back to key not provided
309 if (allocated(self%module_prefix%s)) deallocate(self%module_prefix%s)
310 else
311 !> Value found, but not a boolean. Attempt to read a prefix string
312 call get_value(table, "module-naming", self%module_prefix%s)
313 if (.not.allocated(self%module_prefix%s)) then
314 call syntax_error(error,"Could not read value for 'module-naming' in fpm.toml, expecting logical or a string")
315 return
316 end if
317 self%module_naming = .true.
318 end if
319
320 call get_list(table, "link", self%link, error)
321 if (allocated(error)) return
322 call get_list(table, "external-modules", self%external_modules, error)
323 if (allocated(error)) return
324
325 end subroutine load_from_toml
326
327
328 end module fpm_manifest_build