Debian Codegraph @

src/fpm/manifest/profiles.f90

raw
1 !> Implementation of the profiles configuration.
2 !>
3 !> A profile is a named collection of features that can be applied together.
4 !> Profiles provide a convenient way to group features for different use cases,
5 !> such as debug builds, release builds, or specific target configurations.
6 !>
7 !> A profile table has the following structure:
8 !>```toml
9 !>[profiles.debug]
10 !>features = ["debug-flags", "development-tools"]
11 !>
12 !>[profiles.release]
13 !>features = ["optimized", "strip-symbols"]
14 !>```
15 module fpm_manifest_profile
16 use fpm_error, only: error_t, fatal_error, syntax_error
17 use fpm_strings, only: string_t, operator(==)
18 use tomlf, only: toml_table, toml_array, toml_key, toml_stat, len
19 use fpm_toml, only: get_value, serializable_t, set_string, set_list, get_list, add_table
20
21 implicit none
22 private
23
24 public :: profile_config_t, new_profile, new_profiles, get_default_profiles, add_default_profiles
25
26 !> Configuration data for a profile
27 type, extends(serializable_t) :: profile_config_t
28
29 !> Profile name
30 character(len=:), allocatable :: name
31
32 !> List of features to apply
33 type(string_t), allocatable :: features(:)
34
35 contains
36
37 !> Print information on this instance
38 procedure :: info
39
40 !> Serialization interface
41 procedure :: serializable_is_same => profile_is_same
42 procedure :: dump_to_toml
43 procedure :: load_from_toml
44
45 end type profile_config_t
46
47 character(len=*), parameter, private :: class_name = 'profile_config_t'
48
49 contains
50
51 !> Construct a new profile configuration from a TOML array
52 subroutine new_profile(self, features_array, profile_name, error)
53
54 !> Instance of the profile configuration
55 type(profile_config_t), intent(out) :: self
56
57 !> TOML array containing the feature names
58 type(toml_array), intent(inout) :: features_array
59
60 !> Name of the profile
61 character(len=*), intent(in) :: profile_name
62
63 !> Error handling
64 type(error_t), allocatable, intent(out) :: error
65
66 integer :: i, stat
67 character(len=:), allocatable :: feature_name
68
69 ! Set profile name
70 self%name = profile_name
71
72 ! Get feature names from array
73 if (len(features_array) > 0) then
74 allocate(self%features(len(features_array)))
75 do i = 1, len(features_array)
76 call get_value(features_array, i, feature_name, stat=stat)
77 if (stat /= toml_stat%success) then
78 call fatal_error(error, "Failed to read feature name from profile " // profile_name)
79 return
80 end if
81 self%features(i)%s = feature_name
82 end do
83 else
84 allocate(self%features(0))
85 end if
86
87 end subroutine new_profile
88
89
90 !> Construct new profiles array from a TOML data structure
91 subroutine new_profiles(profiles, table, error)
92
93 !> Instance of the profile configuration array
94 type(profile_config_t), allocatable, intent(out) :: profiles(:)
95
96 !> Instance of the TOML data structure
97 type(toml_table), intent(inout) :: table
98
99 !> Error handling
100 type(error_t), allocatable, intent(out) :: error
101
102 type(toml_array), pointer :: array_node
103 type(toml_key), allocatable :: list(:)
104 integer :: iprofile, stat
105
106 call table%get_keys(list)
107
108 if (size(list) < 1) then
109 allocate(profiles(0))
110 return
111 end if
112
113 allocate(profiles(size(list)))
114
115 do iprofile = 1, size(list)
116 call get_value(table, list(iprofile)%key, array_node, stat=stat)
117 if (stat /= toml_stat%success) then
118 call fatal_error(error, "Profile "//list(iprofile)%key//" must be an array of feature names")
119 exit
120 end if
121 call new_profile(profiles(iprofile), array_node, list(iprofile)%key, error)
122 if (allocated(error)) exit
123 end do
124
125 end subroutine new_profiles
126
127 !> Write information on instance
128 subroutine info(self, unit, verbosity)
129
130 !> Instance of the profile configuration
131 class(profile_config_t), intent(in) :: self
132
133 !> Unit for IO
134 integer, intent(in) :: unit
135
136 !> Verbosity of the printout
137 integer, intent(in), optional :: verbosity
138
139 integer :: pr, ii
140 character(len=*), parameter :: fmt = '("#", 1x, a, t30, a)', &
141 & fmti = '("#", 1x, a, t30, i0)'
142
143 if (present(verbosity)) then
144 pr = verbosity
145 else
146 pr = 1
147 end if
148
149 if (pr < 1) return
150
151 write(unit, fmt) "Profile"
152 if (allocated(self%name)) then
153 write(unit, fmt) "- name", self%name
154 end if
155
156 if (allocated(self%features)) then
157 if (size(self%features) > 0) then
158 write(unit, fmti) "- features", size(self%features)
159 if (pr > 1) then
160 do ii = 1, size(self%features)
161 write(unit, fmt) " - feature", self%features(ii)%s
162 end do
163 end if
164 end if
165 end if
166
167 end subroutine info
168
169 !> Check that two profile configs are equal
170 logical function profile_is_same(this, that)
171 class(profile_config_t), intent(in) :: this
172 class(serializable_t), intent(in) :: that
173
174 integer :: ii
175
176 profile_is_same = .false.
177
178 select type (other=>that)
179 type is (profile_config_t)
180
181 if (allocated(this%name).neqv.allocated(other%name)) return
182 if (allocated(this%name)) then
183 if (.not.(this%name==other%name)) return
184 end if
185
186 if (allocated(this%features).neqv.allocated(other%features)) return
187 if (allocated(this%features)) then
188 if (.not.(size(this%features)==size(other%features))) return
189 do ii = 1, size(this%features)
190 if (.not.(this%features(ii)==other%features(ii))) return
191 end do
192 end if
193
194 class default
195 return
196 end select
197
198 profile_is_same = .true.
199
200 end function profile_is_same
201
202 !> Dump profile to toml table
203 subroutine dump_to_toml(self, table, error)
204
205 !> Instance of the serializable object
206 class(profile_config_t), intent(inout) :: self
207
208 !> Data structure
209 type(toml_table), intent(inout) :: table
210
211 !> Error handling
212 type(error_t), allocatable, intent(out) :: error
213
214 call set_string(table, "name", self%name, error, class_name)
215 if (allocated(error)) return
216
217 call set_list(table, "features", self%features, error)
218 if (allocated(error)) return
219
220 end subroutine dump_to_toml
221
222 !> Read profile from toml table (no checks made at this stage)
223 subroutine load_from_toml(self, table, error)
224
225 !> Instance of the serializable object
226 class(profile_config_t), intent(inout) :: self
227
228 !> Data structure
229 type(toml_table), intent(inout) :: table
230
231 !> Error handling
232 type(error_t), allocatable, intent(out) :: error
233
234 call get_value(table, "name", self%name)
235
236 call get_list(table, "features", self%features, error)
237 if (allocated(error)) return
238
239 end subroutine load_from_toml
240
241
242 !> Create default profiles with standard features
243 subroutine get_default_profiles(profiles, error)
244
245 !> Instance of the profile configuration array
246 type(profile_config_t), allocatable, intent(out) :: profiles(:)
247
248 !> Error handling
249 type(error_t), allocatable, intent(out) :: error
250
251 ! Create two default profiles: debug and release
252 allocate(profiles(2))
253
254 ! Debug profile with "debug" feature
255 profiles(1)%name = "debug"
256 allocate(profiles(1)%features(1))
257 profiles(1)%features(1)%s = "debug"
258
259 ! Release profile with "release" feature
260 profiles(2)%name = "release"
261 allocate(profiles(2)%features(1))
262 profiles(2)%features(1)%s = "release"
263
264 end subroutine get_default_profiles
265
266
267 !> Add default profiles to existing profiles array if they don't already exist
268 subroutine add_default_profiles(profiles, error)
269
270 !> Instance of the profile configuration array (will be resized)
271 type(profile_config_t), allocatable, intent(inout) :: profiles(:)
272
273 !> Error handling
274 type(error_t), allocatable, intent(out) :: error
275
276 type(profile_config_t), allocatable :: temp_profiles(:)
277 type(profile_config_t), allocatable :: default_profiles(:)
278 logical :: debug_exists, release_exists
279 integer :: i, current_size, new_size
280
281 ! Get default profiles
282 call get_default_profiles(default_profiles, error)
283 if (allocated(error)) return
284
285 ! Check if debug and release profiles already exist
286 debug_exists = .false.
287 release_exists = .false.
288
289 if (allocated(profiles)) then
290 do i = 1, size(profiles)
291 if (allocated(profiles(i)%name)) then
292 if (profiles(i)%name == "debug") debug_exists = .true.
293 if (profiles(i)%name == "release") release_exists = .true.
294 end if
295 end do
296 current_size = size(profiles)
297 else
298 current_size = 0
299 end if
300
301 ! Calculate how many profiles to add
302 new_size = current_size
303 if (.not. debug_exists) new_size = new_size + 1
304 if (.not. release_exists) new_size = new_size + 1
305
306 ! If nothing to add, return
307 if (new_size == current_size) return
308
309 ! Create new array with existing + missing defaults
310 allocate(temp_profiles(new_size))
311
312 ! Copy existing profiles
313 if (current_size > 0) then
314 temp_profiles(1:current_size) = profiles(1:current_size)
315 end if
316
317 ! Add missing defaults
318 i = current_size
319 if (.not. debug_exists) then
320 i = i + 1
321 temp_profiles(i) = default_profiles(1) ! debug profile
322 end if
323 if (.not. release_exists) then
324 i = i + 1
325 temp_profiles(i) = default_profiles(2) ! release profile
326 end if
327
328 ! Replace the profiles array
329 call move_alloc(temp_profiles, profiles)
330
331 end subroutine add_default_profiles
332
333 end module fpm_manifest_profile