Debian Codegraph @

src/fpm/manifest/executable.f90

raw
1 !> Implementation of the meta data for an executables.
2 !>
3 !> An executable table can currently have the following fields
4 !>
5 !>```toml
6 !>[[ executable ]]
7 !>name = "string"
8 !>source-dir = "path"
9 !>main = "file"
10 !>link = ["lib"]
11 !>[executable.dependencies]
12 !>```
13 module fpm_manifest_executable
14 use fpm_manifest_dependency, only : dependency_config_t, new_dependencies, resize
15 use fpm_error, only : error_t, syntax_error, bad_name_error, fatal_error
16 use fpm_strings, only : string_t, operator(==)
17 use tomlf, only : toml_table, toml_key, toml_stat
18 use fpm_toml, only : get_value, get_list, serializable_t, add_table, &
19 set_string, set_list
20 implicit none
21 private
22
23 public :: executable_config_t, new_executable
24
25
26 !> Configuation meta data for an executable
27 type, extends(serializable_t) :: executable_config_t
28
29 !> Name of the resulting executable
30 character(len=:), allocatable :: name
31
32 !> Source directory for collecting the executable
33 character(len=:), allocatable :: source_dir
34
35 !> Name of the source file declaring the main program
36 character(len=:), allocatable :: main
37
38 !> Dependency meta data for this executable
39 type(dependency_config_t), allocatable :: dependency(:)
40
41 !> Libraries to link against
42 type(string_t), allocatable :: link(:)
43
44 contains
45
46 !> Print information on this instance
47 procedure :: info
48
49 !> Serialization interface
50 procedure :: serializable_is_same => exe_is_same
51 procedure :: dump_to_toml
52 procedure :: load_from_toml
53
54 end type executable_config_t
55
56 character(*), parameter, private :: class_name = 'executable_config_t'
57
58
59 contains
60
61
62 !> Construct a new executable configuration from a TOML data structure
63 subroutine new_executable(self, table, error)
64
65 !> Instance of the executable configuration
66 type(executable_config_t), intent(out) :: self
67
68 !> Instance of the TOML data structure
69 type(toml_table), intent(inout) :: table
70
71 !> Error handling
72 type(error_t), allocatable, intent(out) :: error
73
74 type(toml_table), pointer :: child
75
76 call check(table, error)
77 if (allocated(error)) return
78
79 call get_value(table, "name", self%name)
80 if (.not.allocated(self%name)) then
81 call syntax_error(error, "Could not retrieve executable name")
82 return
83 end if
84 if (bad_name_error(error,'executable',self%name))then
85 return
86 endif
87 call get_value(table, "source-dir", self%source_dir, "app")
88 call get_value(table, "main", self%main, "main.f90")
89
90 call get_value(table, "dependencies", child, requested=.false.)
91 if (associated(child)) then
92 call new_dependencies(self%dependency, child, error=error)
93 if (allocated(error)) return
94 end if
95
96 call get_list(table, "link", self%link, error)
97 if (allocated(error)) return
98
99 end subroutine new_executable
100
101
102 !> Check local schema for allowed entries
103 subroutine check(table, error)
104
105 !> Instance of the TOML data structure
106 type(toml_table), intent(inout) :: table
107
108 !> Error handling
109 type(error_t), allocatable, intent(out) :: error
110
111 type(toml_key), allocatable :: list(:)
112 logical :: name_present
113 integer :: ikey
114
115 name_present = .false.
116
117 call table%get_keys(list)
118
119 if (size(list) < 1) then
120 call syntax_error(error, "Executable section does not provide sufficient entries")
121 return
122 end if
123
124 do ikey = 1, size(list)
125 select case(list(ikey)%key)
126 case default
127 call syntax_error(error, "Key "//list(ikey)%key//" is not allowed as executable entry")
128 exit
129
130 case("name")
131 name_present = .true.
132
133 case("source-dir", "main", "dependencies", "link")
134 continue
135
136 end select
137 end do
138 if (allocated(error)) return
139
140 if (.not.name_present) then
141 call syntax_error(error, "Executable name is not provided, please add a name entry")
142 end if
143
144 end subroutine check
145
146
147 !> Write information on instance
148 subroutine info(self, unit, verbosity)
149
150 !> Instance of the executable configuration
151 class(executable_config_t), intent(in) :: self
152
153 !> Unit for IO
154 integer, intent(in) :: unit
155
156 !> Verbosity of the printout
157 integer, intent(in), optional :: verbosity
158
159 integer :: pr, ii
160 character(len=*), parameter :: fmt = '("#", 1x, a, t30, a)', &
161 & fmti = '("#", 1x, a, t30, i0)'
162
163 if (present(verbosity)) then
164 pr = verbosity
165 else
166 pr = 1
167 end if
168
169 if (pr < 1) return
170
171 write(unit, fmt) "Executable target"
172 if (allocated(self%name)) then
173 write(unit, fmt) "- name", self%name
174 end if
175 if (allocated(self%source_dir)) then
176 if (self%source_dir /= "app" .or. pr > 2) then
177 write(unit, fmt) "- source directory", self%source_dir
178 end if
179 end if
180 if (allocated(self%main)) then
181 if (self%main /= "main.f90" .or. pr > 2) then
182 write(unit, fmt) "- program source", self%main
183 end if
184 end if
185
186 if (allocated(self%dependency)) then
187 if (size(self%dependency) > 1 .or. pr > 2) then
188 write(unit, fmti) "- dependencies", size(self%dependency)
189 end if
190 do ii = 1, size(self%dependency)
191 call self%dependency(ii)%info(unit, pr - 1)
192 end do
193 end if
194
195 end subroutine info
196
197
198 logical function exe_is_same(this,that)
199 class(executable_config_t), intent(in) :: this
200 class(serializable_t), intent(in) :: that
201
202 integer :: ii
203
204 exe_is_same = .false.
205
206 select type (other=>that)
207 type is (executable_config_t)
208 if (.not.this%link==other%link) return
209 if (allocated(this%name).neqv.allocated(other%name)) return
210 if (allocated(this%name)) then
211 if (.not.this%name==other%name) return
212 end if
213 if (allocated(this%source_dir).neqv.allocated(other%source_dir)) return
214 if (allocated(this%source_dir)) then
215 if (.not.this%source_dir==other%source_dir) return
216 end if
217 if (allocated(this%main).neqv.allocated(other%main)) return
218 if (allocated(this%main)) then
219 if (.not.this%main==other%main) return
220 end if
221 if (allocated(this%dependency).neqv.allocated(other%dependency)) return
222 if (allocated(this%dependency)) then
223 if (.not.(size(this%dependency)==size(other%dependency))) return
224 do ii = 1, size(this%dependency)
225 if (.not.(this%dependency(ii)==other%dependency(ii))) return
226 end do
227 end if
228 class default
229 ! Not the same type
230 return
231 end select
232
233 !> All checks passed!
234 exe_is_same = .true.
235
236 end function exe_is_same
237
238 !> Dump install config to toml table
239 subroutine dump_to_toml(self, table, error)
240
241 !> Instance of the serializable object
242 class(executable_config_t), intent(inout) :: self
243
244 !> Data structure
245 type(toml_table), intent(inout) :: table
246
247 !> Error handling
248 type(error_t), allocatable, intent(out) :: error
249
250 !> Local variables
251 integer :: ierr, ii
252 type(toml_table), pointer :: ptr_deps,ptr
253 character(27) :: unnamed
254
255 call set_string(table, "name", self%name, error)
256 if (allocated(error)) return
257 call set_string(table, "source-dir", self%source_dir, error)
258 if (allocated(error)) return
259 call set_string(table, "main", self%main, error)
260 if (allocated(error)) return
261
262 if (allocated(self%dependency)) then
263
264 ! Create dependency table
265 call add_table(table, "dependencies", ptr_deps)
266 if (.not. associated(ptr_deps)) then
267 call fatal_error(error, class_name//" cannot create dependency table ")
268 return
269 end if
270
271 do ii = 1, size(self%dependency)
272 associate (dep => self%dependency(ii))
273
274 !> Because dependencies are named, fallback if this has no name
275 !> So, serialization will work regardless of size(self%dep) == self%ndep
276 if (len_trim(dep%name)==0) then
277 write(unnamed,1) ii
278 call add_table(ptr_deps, trim(unnamed), ptr)
279 else
280 call add_table(ptr_deps, dep%name, ptr)
281 end if
282 if (.not. associated(ptr)) then
283 call fatal_error(error, class_name//" cannot create entry for dependency "//dep%name)
284 return
285 end if
286 call dep%dump_to_toml(ptr, error)
287 if (allocated(error)) return
288 end associate
289 end do
290
291 endif
292
293 call set_list(table, "link", self%link, error)
294 if (allocated(error)) return
295
296 1 format('UNNAMED_DEPENDENCY_',i0)
297
298 end subroutine dump_to_toml
299
300 !> Read install config from toml table (no checks made at this stage)
301 subroutine load_from_toml(self, table, error)
302
303 !> Instance of the serializable object
304 class(executable_config_t), intent(inout) :: self
305
306 !> Data structure
307 type(toml_table), intent(inout) :: table
308
309 !> Error handling
310 type(error_t), allocatable, intent(out) :: error
311
312 !> Local variables
313 type(toml_key), allocatable :: keys(:),dep_keys(:)
314 type(toml_table), pointer :: ptr_deps,ptr
315 integer :: ii, jj, ierr
316
317 call table%get_keys(keys)
318
319 call get_value(table, "name", self%name)
320 if (allocated(error)) return
321 call get_value(table, "source-dir", self%source_dir)
322 if (allocated(error)) return
323 call get_value(table, "main", self%main)
324 if (allocated(error)) return
325 call get_list(table, "link", self%link, error)
326
327 find_deps_table: do ii = 1, size(keys)
328 if (keys(ii)%key=="dependencies") then
329
330 call get_value(table, keys(ii), ptr_deps)
331 if (.not.associated(ptr_deps)) then
332 call fatal_error(error,class_name//': error retrieving dependency table from TOML table')
333 return
334 end if
335
336 !> Read all dependencies
337 call ptr_deps%get_keys(dep_keys)
338 call resize(self%dependency, size(dep_keys))
339
340 do jj = 1, size(dep_keys)
341
342 call get_value(ptr_deps, dep_keys(jj), ptr)
343 call self%dependency(jj)%load_from_toml(ptr, error)
344 if (allocated(error)) return
345
346 end do
347
348 exit find_deps_table
349
350 endif
351 end do find_deps_table
352
353 end subroutine load_from_toml
354
355
356 end module fpm_manifest_executable