Debian Codegraph @

src/fpm/manifest/platform.f90

raw
1 !> Platform configuration type.
2 !>
3 !> This type captures only the target compiler and operating-system
4 !> selector, and implements the standard serialization interface
5 !> (serializable_t) used across FPM manifest classes.
6 !>
7 !> TOML representation:
8 !> compiler = "<name>" # e.g., "gfortran", "ifx", "all"
9 !> os = "<name>" # e.g., "linux", "macos", "windows", "all"
10 module fpm_manifest_platform
11 use fpm_error, only : error_t, fatal_error
12 use tomlf, only : toml_table
13 use fpm_toml, only : serializable_t, set_string, get_value
14 use fpm_environment,only : OS_ALL, OS_NAME, match_os_type, OS_UNKNOWN, validate_os_name, &
15 OS_WINDOWS, OS_LINUX, OS_MACOS
16 use fpm_compiler, only : compiler_enum, compiler_id_name, match_compiler_type, id_all, &
17 id_unknown, validate_compiler_name, id_intel_classic_nix, id_intel_classic_mac, &
18 id_intel_classic_windows, id_intel_llvm_nix, id_intel_llvm_windows, id_intel_llvm_unknown
19 use fpm_strings, only : lower
20 implicit none
21 private
22
23 public :: platform_config_t
24 public :: is_platform_key
25
26 !> Shortcuts for the Intel OS variants
27 integer(compiler_enum), parameter :: &
28 id_intel_classic(*) = [id_intel_classic_mac,id_intel_classic_nix,id_intel_classic_windows], &
29 id_intel_llvm (*) = [id_intel_llvm_nix,id_intel_llvm_windows,id_intel_llvm_unknown]
30
31 !> Serializable platform configuration (compiler + OS only)
32 type, extends(serializable_t) :: platform_config_t
33
34 integer(compiler_enum) :: compiler = id_all
35 integer :: os_type = OS_ALL
36
37 contains
38
39 procedure :: serializable_is_same => platform_is_same
40 procedure :: dump_to_toml
41 procedure :: load_from_toml
42
43 !> Print information
44 procedure :: info
45
46 !> Return .true. if THIS platform selector is compatible with CURRENT (wildcards allowed)
47 procedure :: matches => platform_is_suitable
48
49 !> Get compiler name as string
50 procedure :: compiler_name => platform_compiler_name
51
52 !> Get OS name as string
53 procedure :: os_name => platform_os_name
54
55 !> Get configuration name as it appears in the manifest
56 procedure :: name => platform_config_name
57
58 !> Validation
59 procedure :: is_valid => platform_is_valid
60
61 !> Properties
62 procedure, non_overridable :: any_compiler
63 procedure, non_overridable :: any_os
64 procedure, non_overridable :: any_platform
65
66 end type platform_config_t
67
68 ! Overloaded initializer
69 interface platform_config_t
70 module procedure new_platform
71 module procedure new_platform_id
72 end interface
73
74 character(len=*), parameter, private :: class_name = 'platform_config_t'
75
76 contains
77
78 !> Initialize a new platform config from compiler name
79 !> Automatically selects correct Intel compiler version based on OS
80 type(platform_config_t) function new_platform(compiler, os_type)
81 character(*), intent(in) :: compiler
82 integer, intent(in) :: os_type
83
84 new_platform%compiler = match_compiler_type(compiler)
85 new_platform%os_type = os_type
86
87 ! Correct Intel compiler ID based on OS (fallback to unix version for OS_ALL)
88 new_platform%compiler = correct_compiler_for_os(new_platform%compiler, os_type)
89
90 end function new_platform
91
92 !> Initialize a new platform config from compiler enum ID
93 !> Automatically selects correct Intel compiler version based on OS
94 type(platform_config_t) function new_platform_id(compiler_id, os_type)
95 integer(compiler_enum), intent(in) :: compiler_id
96 integer, intent(in) :: os_type
97
98 new_platform_id%compiler = compiler_id
99 new_platform_id%os_type = os_type
100
101 ! Correct Intel compiler ID based on OS (fallback to unix version for OS_ALL)
102 new_platform_id%compiler = correct_compiler_for_os(new_platform_id%compiler, os_type)
103
104 end function new_platform_id
105
106 !> Correct Intel compiler ID to match the target OS
107 !> Returns the appropriate OS-specific Intel compiler variant
108 function correct_compiler_for_os(compiler_id, os_type) result(corrected_id)
109 integer(compiler_enum), intent(in) :: compiler_id
110 integer, intent(in) :: os_type
111 integer(compiler_enum) :: corrected_id
112
113 corrected_id = compiler_id ! Default: no change
114
115 ! Intel classic compilers: map to OS-specific version
116 select case (compiler_id)
117 case (id_intel_classic_mac,id_intel_classic_nix,id_intel_classic_windows)
118 select case (os_type)
119 case (OS_WINDOWS)
120 corrected_id = id_intel_classic_windows
121 case (OS_MACOS)
122 corrected_id = id_intel_classic_mac
123 case default
124 corrected_id = id_intel_classic_nix ! Fallback to unix version
125 end select
126
127 case (id_intel_llvm_nix,id_intel_llvm_windows)
128 select case (os_type)
129 case (OS_WINDOWS)
130 corrected_id = id_intel_llvm_windows
131 case default
132 corrected_id = id_intel_llvm_nix ! Fallback to unix version
133 end select
134 end select
135
136 end function correct_compiler_for_os
137
138 !> Check if a compiler ID is suitable for a target platform
139 !> Handles special cases like Intel compiler variants
140 logical function compiler_is_suitable(compiler_id, target) result(suitable)
141 integer(compiler_enum), intent(in) :: compiler_id
142 type(platform_config_t), intent(in) :: target
143
144 ! Default case: exact match or compiler_id is id_all
145 suitable = (compiler_id == id_all .or. compiler_id == target%compiler)
146
147 if (suitable) return
148
149 ! Intel classic compilers: all variants are equivalent
150 if (any(compiler_id == id_intel_classic) .and. any(target%compiler == id_intel_classic)) then
151 suitable = .true.
152 return
153 end if
154
155 ! Intel LLVM compilers: all variants are equivalent
156 if (any(compiler_id == id_intel_llvm) .and. any(target%compiler == id_intel_llvm)) then
157 suitable = .true.
158 return
159 end if
160
161 ! Future extensions can be added here for other compiler families
162
163 end function compiler_is_suitable
164
165 !> Compare two platform_config_t (semantic equality)
166 logical function platform_is_same(this, that)
167 class(platform_config_t), intent(in) :: this
168 class(serializable_t), intent(in) :: that
169
170 platform_is_same = .false.
171 select type (other => that)
172 type is (platform_config_t)
173 if (this%compiler /= other%compiler) return
174 if (this%os_type /= other%os_type ) return
175 class default
176 return
177 end select
178 platform_is_same = .true.
179 end function platform_is_same
180
181 !> Dump to TOML table
182 subroutine dump_to_toml(self, table, error)
183 class(platform_config_t), intent(inout) :: self
184 type(toml_table), intent(inout) :: table
185 type(error_t), allocatable, intent(out) :: error
186
187 ! Compiler as canonical name (e.g., "gfortran", "ifx", "all")
188 call set_string(table, "compiler", compiler_id_name(self%compiler), error, class_name)
189 if (allocated(error)) return
190
191 ! OS as canonical name (e.g., "linux", "macos", "windows", "all")
192 call set_string(table, "os", OS_NAME(self%os_type), error, class_name)
193 if (allocated(error)) return
194 end subroutine dump_to_toml
195
196
197 !> Load from TOML table
198 subroutine load_from_toml(self, table, error)
199 class(platform_config_t), intent(inout) :: self
200 type(toml_table), intent(inout) :: table
201 type(error_t), allocatable, intent(out) :: error
202
203 character(len=:), allocatable :: s
204
205 ! Compiler (default "all")
206 call get_value(table, "compiler", s, "all")
207 self%compiler = match_compiler_type(s)
208 if (self%compiler == id_unknown) then
209 call fatal_error(error, class_name//": unsupported compiler '"//s//"'")
210 return
211 end if
212
213 ! OS (default "all")
214 call get_value(table, "os", s, "all")
215 self%os_type = match_os_type(s)
216 ! match_os_type should map unknowns to a sentinel; rely on it for validation.
217 ! If you prefer a hard failure on unknown here, uncomment the next block:
218 ! if (self%os_type == OS_UNKNOWN) then
219 ! call fatal_error(error, class_name//": unsupported os '"//s//"'")
220 ! return
221 ! end if
222 end subroutine load_from_toml
223
224 !> Write information on instance (similar style to profile_config_t%info)
225 subroutine info(self, unit, verbosity)
226 class(platform_config_t), intent(in) :: self
227 integer, intent(in) :: unit
228 integer, optional, intent(in) :: verbosity
229
230 integer :: pr
231 character(len=*), parameter :: fmt = '("#", 1x, a, t30, a)'
232
233 pr = merge(verbosity, 1, present(verbosity))
234
235 write(unit, fmt) "Platform"
236 write(unit, fmt) "- compiler", compiler_id_name(self%compiler)
237 write(unit, fmt) "- os", OS_NAME(self%os_type)
238
239 ! Currently 'verbosity' does not expand output; reserved for future fields.
240 end subroutine info
241
242 !> Return .true. if SELF is suitable for a given target platform
243 !>
244 !> Rules:
245 !> - compiler matches if SELF%compiler == id_all OR == target%compiler
246 !> - os matches if SELF%os_type == OS_ALL OR == target%os_type
247 !> - id_unknown / OS_UNKNOWN in SELF are treated as "no match" (conservative)
248 !> - Intel compilers must match OS (ifort unix/windows versions use different flags)
249 logical function platform_is_suitable(self, target) result(ok)
250 class(platform_config_t), intent(in) :: self
251 type(platform_config_t), intent(in) :: target
252
253 logical :: compiler_ok, os_ok
254
255
256
257 ! Check that both platforms are valid
258 if (.not. self%is_valid() .or. .not. target%is_valid()) then
259 ok = .false.
260 return
261 end if
262
263 compiler_ok = compiler_is_suitable(self%compiler, target)
264 os_ok = any(self%os_type == [OS_ALL,target%os_type])
265
266 ! Basic matching
267 ok = compiler_ok .and. os_ok
268
269 if (.not. ok) return
270
271 ! Additional validation: Intel compilers must have compatible OS
272 ! ifort on Unix/Mac should not match ifort on Windows and vice versa
273 if (self%compiler /= id_all .and. self%os_type /= OS_ALL) then
274 ok = compiler_os_compatible(self%compiler, self%os_type) .and. &
275 compiler_os_compatible(target%compiler, target%os_type)
276 end if
277
278 end function platform_is_suitable
279
280 !> Check if a platform configuration is valid (no unknowns, compatible compiler+OS)
281 logical function platform_is_valid(self) result(valid)
282 class(platform_config_t), intent(in) :: self
283
284 ! Check compiler+OS compatibility
285 valid = compiler_os_compatible(self%compiler, self%os_type)
286
287 end function platform_is_valid
288
289 !> Check if a compiler ID is compatible with an OS type
290 elemental logical function compiler_os_compatible(compiler_id, os_type) result(compatible)
291 integer(compiler_enum), intent(in) :: compiler_id
292 integer, intent(in) :: os_type
293
294 ! Check for unknowns
295 if (compiler_id == id_unknown .or. os_type == OS_UNKNOWN) then
296 compatible = .false.
297 return
298 end if
299
300 ! Intel classic compilers: OS-specific variants
301 select case (compiler_id)
302 case (id_intel_classic_windows)
303 compatible = any(os_type == [OS_ALL,OS_WINDOWS])
304 case (id_intel_classic_nix)
305 compatible = any(os_type == [OS_ALL,OS_LINUX])
306 case (id_intel_classic_mac)
307 compatible = any(os_type == [OS_ALL,OS_MACOS])
308 case (id_intel_llvm_windows)
309 compatible = any(os_type == [OS_ALL,OS_WINDOWS])
310 case (id_intel_llvm_nix)
311 compatible = any(os_type == [OS_ALL,OS_LINUX,OS_MACOS])
312 case default
313 ! Other compilers are compatible with any OS
314 compatible = os_type/=OS_UNKNOWN .and. compiler_id/=id_unknown
315 end select
316
317 end function compiler_os_compatible
318
319 !> Check if a key (os or compiler) can be used for platform setting
320 elemental logical function is_platform_key(key)
321 character(*), intent(in) :: key
322
323 call validate_compiler_name(key, is_platform_key)
324 if (is_platform_key) return
325
326 call validate_os_name(key, is_platform_key)
327 if (is_platform_key) return
328
329 end function is_platform_key
330
331 !> Get compiler name as string
332 function platform_compiler_name(self) result(name)
333 class(platform_config_t), intent(in) :: self
334 character(len=:), allocatable :: name
335
336 name = compiler_id_name(self%compiler)
337 end function platform_compiler_name
338
339 !> Get OS name as string
340 function platform_os_name(self) result(name)
341 class(platform_config_t), intent(in) :: self
342 character(len=:), allocatable :: name
343
344 name = OS_NAME(self%os_type)
345 end function platform_os_name
346
347 !> Get configuration name
348 function platform_config_name(self) result(name)
349 class(platform_config_t), intent(in) :: self
350 character(len=:), allocatable :: name
351
352 if (self%os_type==OS_ALL .and. self%compiler==id_all) then
353 name = ""
354 elseif (self%os_type==OS_ALL) then
355 name = self%compiler_name()
356 elseif (self%compiler==id_all) then
357 name = self%os_name()
358 else
359 name = self%os_name()//'.'//self%compiler_name()
360 end if
361
362 end function platform_config_name
363
364 !> Whether the configuration is generic
365 elemental logical function any_compiler(self)
366 class(platform_config_t), intent(in) :: self
367 any_compiler = self%compiler == id_all
368 end function any_compiler
369 elemental logical function any_os(self)
370 class(platform_config_t), intent(in) :: self
371 any_os = self%os_type == OS_ALL
372 end function any_os
373 elemental logical function any_platform(self)
374 class(platform_config_t), intent(in) :: self
375 any_platform = any_os(self) .and. any_compiler(self)
376 end function any_platform
377
378 end module fpm_manifest_platform