| 1 |
!> Define the package data containing the meta data from the configuration file. |
| 2 |
!> |
| 3 |
!> The package data defines a Fortran type corresponding to the respective |
| 4 |
!> TOML document, after creating it from a package file no more interaction |
| 5 |
!> with the TOML document is required. |
| 6 |
!> |
| 7 |
!> Every configuration type provides it custom constructor (prefixed with `new_`) |
| 8 |
!> and knows how to deserialize itself from a TOML document. |
| 9 |
!> To ensure we find no untracked content in the package file all keywords are |
| 10 |
!> checked and possible entries have to be explicitly allowed in the `check` |
| 11 |
!> function. |
| 12 |
!> If entries are mutally exclusive or interdependent inside the current table |
| 13 |
!> the `check` function is required to enforce this schema on the data structure. |
| 14 |
!> |
| 15 |
!> The package file root allows the following keywords |
| 16 |
!> |
| 17 |
!>```toml |
| 18 |
!>name = "string" |
| 19 |
!>version = "string" |
| 20 |
!>license = "string" |
| 21 |
!>author = "string" |
| 22 |
!>maintainer = "string" |
| 23 |
!>copyright = "string" |
| 24 |
!>[library] |
| 25 |
!>[dependencies] |
| 26 |
!>[dev-dependencies] |
| 27 |
!>[profiles] |
| 28 |
!>[build] |
| 29 |
!>[install] |
| 30 |
!>[fortran] |
| 31 |
!>[[ executable ]] |
| 32 |
!>[[ example ]] |
| 33 |
!>[[ test ]] |
| 34 |
!>[extra] |
| 35 |
!>``` |
| 36 |
module fpm_manifest_package |
| 37 |
use fpm_manifest_build, only: build_config_t, new_build_config |
| 38 |
use fpm_manifest_dependency, only : dependency_config_t, new_dependencies |
| 39 |
use fpm_manifest_profile, only : profile_config_t, new_profiles, add_default_profiles |
| 40 |
use fpm_manifest_example, only : example_config_t, new_example |
| 41 |
use fpm_manifest_executable, only : executable_config_t, new_executable |
| 42 |
use fpm_manifest_fortran, only : fortran_config_t, new_fortran_config |
| 43 |
use fpm_manifest_library, only : library_config_t, new_library |
| 44 |
use fpm_manifest_install, only: install_config_t, new_install_config |
| 45 |
use fpm_manifest_test, only : test_config_t, new_test |
| 46 |
use fpm_manifest_preprocess, only : preprocess_config_t, new_preprocessors |
| 47 |
use fpm_manifest_feature, only: feature_config_t, init_feature_components |
| 48 |
use fpm_manifest_feature_collection, only: feature_collection_t, new_collections, add_default_features |
| 49 |
use fpm_manifest_platform, only: platform_config_t |
| 50 |
use fpm_strings, only: string_t |
| 51 |
use fpm_filesystem, only : exists, getline, join_path |
| 52 |
use fpm_error, only : error_t, fatal_error, syntax_error, bad_name_error |
| 53 |
use tomlf, only : toml_table, toml_array, toml_key, toml_stat |
| 54 |
use fpm_toml, only : get_value, len, serializable_t, set_value, set_string, set_list, add_table |
| 55 |
use fpm_versioning, only : version_t, new_version |
| 56 |
|
| 57 |
implicit none |
| 58 |
private |
| 59 |
|
| 60 |
public :: package_config_t, new_package |
| 61 |
|
| 62 |
!> Package meta data |
| 63 |
!> Package configuration data - extends a `feature_config_t` to represent the "default" |
| 64 |
!> package feature. The following are now inherited from feature_config_t: name (but for package |
| 65 |
!> it's the package name), description, compiler, os_type (defaults to id_all, OS_ALL for packages) |
| 66 |
!> library, executable(:), dependency(:), dev_dependency(:), example(:), test(:), preprocess(:) |
| 67 |
!> flags, c_flags, cxx_flags, link_time_flags, requires_features(:) |
| 68 |
|
| 69 |
type, extends(feature_config_t) :: package_config_t |
| 70 |
|
| 71 |
!> Package version (name is inherited from feature_config_t%name) |
| 72 |
type(version_t) :: version |
| 73 |
|
| 74 |
!> Package metadata (package-specific) |
| 75 |
character(len=:), allocatable :: license |
| 76 |
character(len=:), allocatable :: author |
| 77 |
character(len=:), allocatable :: maintainer |
| 78 |
character(len=:), allocatable :: copyright |
| 79 |
|
| 80 |
!> Additional feature collections beyond the default package feature |
| 81 |
type(feature_collection_t), allocatable :: features(:) |
| 82 |
|
| 83 |
!> Profiles (collections of features) |
| 84 |
type(profile_config_t), allocatable :: profiles(:) |
| 85 |
|
| 86 |
contains |
| 87 |
|
| 88 |
!> Print information on this instance |
| 89 |
procedure :: info |
| 90 |
|
| 91 |
!> Serialization interface |
| 92 |
procedure :: serializable_is_same => manifest_is_same |
| 93 |
procedure :: dump_to_toml |
| 94 |
procedure :: load_from_toml |
| 95 |
|
| 96 |
!> Check if any features has a cpp configuration |
| 97 |
procedure :: has_cpp |
| 98 |
|
| 99 |
!> Export package configuration with features applied |
| 100 |
procedure :: export_config |
| 101 |
|
| 102 |
!> Find feature by name, returns index or 0 if not found |
| 103 |
procedure :: find_feature |
| 104 |
|
| 105 |
!> Find profile by name, returns index or 0 if not found |
| 106 |
procedure :: find_profile |
| 107 |
|
| 108 |
end type package_config_t |
| 109 |
|
| 110 |
character(len=*), parameter, private :: class_name = 'package_config_t' |
| 111 |
|
| 112 |
|
| 113 |
contains |
| 114 |
|
| 115 |
|
| 116 |
!> Construct a new package configuration from a TOML data structure |
| 117 |
subroutine new_package(self, table, root, error) |
| 118 |
|
| 119 |
!> Instance of the package configuration |
| 120 |
type(package_config_t), intent(out) :: self |
| 121 |
|
| 122 |
!> Instance of the TOML data structure |
| 123 |
type(toml_table), intent(inout) :: table |
| 124 |
|
| 125 |
!> Root directory of the manifest |
| 126 |
character(len=*), intent(in), optional :: root |
| 127 |
|
| 128 |
!> Error handling |
| 129 |
type(error_t), allocatable, intent(out) :: error |
| 130 |
|
| 131 |
! Backspace (8), tabulator (9), newline (10), formfeed (12) and carriage |
| 132 |
! return (13) are invalid in package names |
| 133 |
character(len=*), parameter :: invalid_chars = & |
| 134 |
achar(8) // achar(9) // achar(10) // achar(12) // achar(13) |
| 135 |
type(toml_table), pointer :: child, node |
| 136 |
type(toml_array), pointer :: children |
| 137 |
character(len=:), allocatable :: version, version_file |
| 138 |
integer :: ii, nn, stat, io |
| 139 |
|
| 140 |
! Ensure metapackage data is initialized although off |
| 141 |
call self%meta%reset() |
| 142 |
|
| 143 |
call check(table, error) |
| 144 |
if (allocated(error)) return |
| 145 |
|
| 146 |
! Get package name and perform validation |
| 147 |
call get_value(table, "name", self%name) |
| 148 |
if (.not.allocated(self%name)) then |
| 149 |
call syntax_error(error, "Could not retrieve package name") |
| 150 |
return |
| 151 |
end if |
| 152 |
if (bad_name_error(error,'package',self%name)) return |
| 153 |
|
| 154 |
if (len(self%name) <= 0) then |
| 155 |
call syntax_error(error, "Package name must be a non-empty string") |
| 156 |
return |
| 157 |
end if |
| 158 |
|
| 159 |
ii = scan(self%name, invalid_chars) |
| 160 |
if (ii > 0) then |
| 161 |
call syntax_error(error, "Package name contains invalid characters") |
| 162 |
return |
| 163 |
end if |
| 164 |
|
| 165 |
! Get package-specific metadata |
| 166 |
call get_value(table, "license", self%license) |
| 167 |
call get_value(table, "author", self%author) |
| 168 |
call get_value(table, "maintainer", self%maintainer) |
| 169 |
call get_value(table, "copyright", self%copyright) |
| 170 |
|
| 171 |
! Initialize shared feature components |
| 172 |
call init_feature_components(self%feature_config_t, table, root=root, error=error) |
| 173 |
if (allocated(error)) return |
| 174 |
|
| 175 |
call get_value(table, "version", version, "0") |
| 176 |
call new_version(self%version, version, error) |
| 177 |
if (allocated(error) .and. present(root)) then |
| 178 |
version_file = join_path(root, version) |
| 179 |
if (exists(version_file)) then |
| 180 |
deallocate(error) |
| 181 |
open(file=version_file, newunit=io, iostat=stat) |
| 182 |
if (stat == 0) then |
| 183 |
call getline(io, version, iostat=stat) |
| 184 |
end if |
| 185 |
if (stat == 0) then |
| 186 |
close(io, iostat=stat) |
| 187 |
end if |
| 188 |
if (stat == 0) then |
| 189 |
call new_version(self%version, version, error) |
| 190 |
else |
| 191 |
call fatal_error(error, "Reading version number from file '" & |
| 192 |
& //version_file//"' failed") |
| 193 |
end if |
| 194 |
end if |
| 195 |
end if |
| 196 |
if (allocated(error)) return |
| 197 |
|
| 198 |
call get_value(table, "profiles", child, requested=.false.) |
| 199 |
if (associated(child)) then |
| 200 |
call new_profiles(self%profiles, child, error) |
| 201 |
if (allocated(error)) return |
| 202 |
else |
| 203 |
! No profiles defined - start with empty array |
| 204 |
allocate(self%profiles(0)) |
| 205 |
end if |
| 206 |
|
| 207 |
call get_value(table, "features", child, requested=.false.) |
| 208 |
if (associated(child)) then |
| 209 |
! Parse features from manifest using new_collections |
| 210 |
call new_collections(self%features, child, error) |
| 211 |
if (allocated(error)) return |
| 212 |
else |
| 213 |
! No features defined - start with empty array |
| 214 |
allocate(self%features(0)) |
| 215 |
end if |
| 216 |
|
| 217 |
! Add default features and profiles if they don't already exist |
| 218 |
call add_default_features(self%features, error) |
| 219 |
if (allocated(error)) return |
| 220 |
|
| 221 |
call add_default_profiles(self%profiles, error) |
| 222 |
if (allocated(error)) return |
| 223 |
|
| 224 |
! Validate profiles after all features and profiles have been loaded |
| 225 |
call validate_profiles(self, error) |
| 226 |
if (allocated(error)) return |
| 227 |
|
| 228 |
end subroutine new_package |
| 229 |
|
| 230 |
|
| 231 |
!> Check local schema for allowed entries |
| 232 |
subroutine check(table, error) |
| 233 |
|
| 234 |
!> Instance of the TOML data structure |
| 235 |
type(toml_table), intent(inout) :: table |
| 236 |
|
| 237 |
!> Error handling |
| 238 |
type(error_t), allocatable, intent(out) :: error |
| 239 |
|
| 240 |
type(toml_key), allocatable :: list(:) |
| 241 |
logical :: name_present |
| 242 |
integer :: ikey |
| 243 |
|
| 244 |
name_present = .false. |
| 245 |
|
| 246 |
call table%get_keys(list) |
| 247 |
|
| 248 |
if (size(list) < 1) then |
| 249 |
call syntax_error(error, "Package file is empty") |
| 250 |
return |
| 251 |
end if |
| 252 |
|
| 253 |
do ikey = 1, size(list) |
| 254 |
select case(list(ikey)%key) |
| 255 |
case default |
| 256 |
call syntax_error(error, "Key "//list(ikey)%key//" is not allowed in package file") |
| 257 |
exit |
| 258 |
|
| 259 |
case("name") |
| 260 |
name_present = .true. |
| 261 |
|
| 262 |
case("version", "license", "author", "maintainer", "copyright", & |
| 263 |
& "description", "keywords", "categories", "homepage", "build", & |
| 264 |
& "dependencies", "dev-dependencies", "profiles", "features", "test", "executable", & |
| 265 |
& "example", "library", "install", "extra", "preprocess", "fortran") |
| 266 |
continue |
| 267 |
|
| 268 |
end select |
| 269 |
end do |
| 270 |
if (allocated(error)) return |
| 271 |
|
| 272 |
if (.not.name_present) then |
| 273 |
call syntax_error(error, "Package name is not provided, please add a name entry") |
| 274 |
end if |
| 275 |
|
| 276 |
end subroutine check |
| 277 |
|
| 278 |
|
| 279 |
!> Write information on instance |
| 280 |
subroutine info(self, unit, verbosity) |
| 281 |
|
| 282 |
!> Instance of the package configuration |
| 283 |
class(package_config_t), intent(in) :: self |
| 284 |
|
| 285 |
!> Unit for IO |
| 286 |
integer, intent(in) :: unit |
| 287 |
|
| 288 |
!> Verbosity of the printout |
| 289 |
integer, intent(in), optional :: verbosity |
| 290 |
|
| 291 |
integer :: pr, ii |
| 292 |
character(len=*), parameter :: fmt = '("#", 1x, a, t30, a)', & |
| 293 |
& fmti = '("#", 1x, a, t30, i0)' |
| 294 |
|
| 295 |
if (present(verbosity)) then |
| 296 |
pr = verbosity |
| 297 |
else |
| 298 |
pr = 1 |
| 299 |
end if |
| 300 |
|
| 301 |
if (pr < 1) return |
| 302 |
|
| 303 |
write(unit, fmt) "Package" |
| 304 |
if (allocated(self%name)) then |
| 305 |
write(unit, fmt) "- name", self%name |
| 306 |
end if |
| 307 |
|
| 308 |
call self%build%info(unit, pr - 1) |
| 309 |
|
| 310 |
call self%install%info(unit, pr - 1) |
| 311 |
|
| 312 |
if (allocated(self%library)) then |
| 313 |
write(unit, fmt) "- target", "archive" |
| 314 |
call self%library%info(unit, pr - 1) |
| 315 |
end if |
| 316 |
|
| 317 |
if (allocated(self%executable)) then |
| 318 |
if (size(self%executable) > 1 .or. pr > 2) then |
| 319 |
write(unit, fmti) "- executables", size(self%executable) |
| 320 |
end if |
| 321 |
do ii = 1, size(self%executable) |
| 322 |
call self%executable(ii)%info(unit, pr - 1) |
| 323 |
end do |
| 324 |
end if |
| 325 |
|
| 326 |
if (allocated(self%dependency)) then |
| 327 |
if (size(self%dependency) > 1 .or. pr > 2) then |
| 328 |
write(unit, fmti) "- dependencies", size(self%dependency) |
| 329 |
end if |
| 330 |
do ii = 1, size(self%dependency) |
| 331 |
call self%dependency(ii)%info(unit, pr - 1) |
| 332 |
end do |
| 333 |
end if |
| 334 |
|
| 335 |
if (allocated(self%example)) then |
| 336 |
if (size(self%example) > 1 .or. pr > 2) then |
| 337 |
write(unit, fmti) "- examples", size(self%example) |
| 338 |
end if |
| 339 |
do ii = 1, size(self%example) |
| 340 |
call self%example(ii)%info(unit, pr - 1) |
| 341 |
end do |
| 342 |
end if |
| 343 |
|
| 344 |
if (allocated(self%test)) then |
| 345 |
if (size(self%test) > 1 .or. pr > 2) then |
| 346 |
write(unit, fmti) "- tests", size(self%test) |
| 347 |
end if |
| 348 |
do ii = 1, size(self%test) |
| 349 |
call self%test(ii)%info(unit, pr - 1) |
| 350 |
end do |
| 351 |
end if |
| 352 |
|
| 353 |
if (allocated(self%dev_dependency)) then |
| 354 |
if (size(self%dev_dependency) > 1 .or. pr > 2) then |
| 355 |
write(unit, fmti) "- development deps.", size(self%dev_dependency) |
| 356 |
end if |
| 357 |
do ii = 1, size(self%dev_dependency) |
| 358 |
call self%dev_dependency(ii)%info(unit, pr - 1) |
| 359 |
end do |
| 360 |
end if |
| 361 |
|
| 362 |
if (allocated(self%profiles)) then |
| 363 |
if (size(self%profiles) > 1 .or. pr > 2) then |
| 364 |
write(unit, fmti) "- profiles", size(self%profiles) |
| 365 |
end if |
| 366 |
do ii = 1, size(self%profiles) |
| 367 |
call self%profiles(ii)%info(unit, pr - 1) |
| 368 |
end do |
| 369 |
end if |
| 370 |
|
| 371 |
end subroutine info |
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
logical function manifest_is_same(this,that) |
| 376 |
class(package_config_t), intent(in) :: this |
| 377 |
class(serializable_t), intent(in) :: that |
| 378 |
|
| 379 |
integer :: ii |
| 380 |
|
| 381 |
manifest_is_same = .false. |
| 382 |
|
| 383 |
select type (other=>that) |
| 384 |
type is (package_config_t) |
| 385 |
|
| 386 |
! Compare base fields |
| 387 |
if (.not.this%feature_config_t==other%feature_config_t) return |
| 388 |
|
| 389 |
! Manifest-specific fields |
| 390 |
if (.not.this%version==other%version) return |
| 391 |
if (allocated(this%license).neqv.allocated(other%license)) return |
| 392 |
if (allocated(this%license)) then |
| 393 |
if (.not.this%license==other%license) return |
| 394 |
end if |
| 395 |
if (allocated(this%author).neqv.allocated(other%author)) return |
| 396 |
if (allocated(this%author)) then |
| 397 |
if (.not.this%author==other%author) return |
| 398 |
end if |
| 399 |
if (allocated(this%maintainer).neqv.allocated(other%maintainer)) return |
| 400 |
if (allocated(this%maintainer)) then |
| 401 |
if (.not.this%maintainer==other%maintainer) return |
| 402 |
end if |
| 403 |
if (allocated(this%copyright).neqv.allocated(other%copyright)) return |
| 404 |
if (allocated(this%copyright)) then |
| 405 |
if (.not.this%copyright==other%copyright) return |
| 406 |
end if |
| 407 |
if (allocated(this%profiles).neqv.allocated(other%profiles)) return |
| 408 |
if (allocated(this%profiles)) then |
| 409 |
if (.not.size(this%profiles)==size(other%profiles)) return |
| 410 |
do ii=1,size(this%profiles) |
| 411 |
if (.not.this%profiles(ii)==other%profiles(ii)) return |
| 412 |
end do |
| 413 |
end if |
| 414 |
|
| 415 |
class default |
| 416 |
! Not the same type |
| 417 |
return |
| 418 |
end select |
| 419 |
|
| 420 |
!> All checks passed! |
| 421 |
manifest_is_same = .true. |
| 422 |
|
| 423 |
end function manifest_is_same |
| 424 |
|
| 425 |
!> Dump manifest to toml table |
| 426 |
subroutine dump_to_toml(self, table, error) |
| 427 |
|
| 428 |
!> Instance of the serializable object |
| 429 |
class(package_config_t), intent(inout) :: self |
| 430 |
|
| 431 |
!> Data structure |
| 432 |
type(toml_table), intent(inout) :: table |
| 433 |
|
| 434 |
!> Error handling |
| 435 |
type(error_t), allocatable, intent(out) :: error |
| 436 |
|
| 437 |
integer :: ii |
| 438 |
type(toml_table), pointer :: ptr,ptr_pkg |
| 439 |
character(30) :: unnamed |
| 440 |
character(128) :: profile_name |
| 441 |
|
| 442 |
! Dump feature first |
| 443 |
call self%feature_config_t%dump_to_toml(table, error) |
| 444 |
if (allocated(error)) return |
| 445 |
|
| 446 |
! Package-specific fields |
| 447 |
call set_string(table, "version", self%version%s(), error, class_name) |
| 448 |
if (allocated(error)) return |
| 449 |
call set_string(table, "license", self%license, error, class_name) |
| 450 |
if (allocated(error)) return |
| 451 |
call set_string(table, "author", self%author, error, class_name) |
| 452 |
if (allocated(error)) return |
| 453 |
call set_string(table, "maintainer", self%maintainer, error, class_name) |
| 454 |
if (allocated(error)) return |
| 455 |
call set_string(table, "copyright", self%copyright, error, class_name) |
| 456 |
if (allocated(error)) return |
| 457 |
|
| 458 |
if (allocated(self%profiles)) then |
| 459 |
|
| 460 |
call add_table(table, "profiles", ptr_pkg) |
| 461 |
if (.not. associated(ptr_pkg)) then |
| 462 |
call fatal_error(error, class_name//" cannot create 'profiles' table ") |
| 463 |
return |
| 464 |
end if |
| 465 |
|
| 466 |
do ii = 1, size(self%profiles) |
| 467 |
|
| 468 |
associate (pkg => self%profiles(ii)) |
| 469 |
|
| 470 |
!> Duplicate profile names are possible, as multiple profiles are possible with the |
| 471 |
!> same name, same compiler, etc. So, use a unique name here |
| 472 |
write(profile_name,2) ii |
| 473 |
call add_table(ptr_pkg, trim(profile_name), ptr, error, class_name//'(profiles)') |
| 474 |
if (allocated(error)) return |
| 475 |
call pkg%dump_to_toml(ptr, error) |
| 476 |
if (allocated(error)) return |
| 477 |
|
| 478 |
end associate |
| 479 |
|
| 480 |
end do |
| 481 |
end if |
| 482 |
|
| 483 |
2 format('PROFILE_',i0) |
| 484 |
|
| 485 |
end subroutine dump_to_toml |
| 486 |
|
| 487 |
!> Read manifest from toml table (no checks made at this stage) |
| 488 |
subroutine load_from_toml(self, table, error) |
| 489 |
|
| 490 |
!> Instance of the serializable object |
| 491 |
class(package_config_t), intent(inout) :: self |
| 492 |
|
| 493 |
!> Data structure |
| 494 |
type(toml_table), intent(inout) :: table |
| 495 |
|
| 496 |
!> Error handling |
| 497 |
type(error_t), allocatable, intent(out) :: error |
| 498 |
|
| 499 |
type(toml_key), allocatable :: keys(:),pkg_keys(:) |
| 500 |
integer :: ii, jj |
| 501 |
character(len=:), allocatable :: flag |
| 502 |
type(toml_table), pointer :: ptr,ptr_pkg |
| 503 |
|
| 504 |
! Clean state |
| 505 |
if (allocated(self%library)) deallocate(self%library) |
| 506 |
if (allocated(self%executable)) deallocate(self%executable) |
| 507 |
if (allocated(self%dependency)) deallocate(self%dependency) |
| 508 |
if (allocated(self%dev_dependency)) deallocate(self%dev_dependency) |
| 509 |
if (allocated(self%profiles)) deallocate(self%profiles) |
| 510 |
if (allocated(self%example)) deallocate(self%example) |
| 511 |
if (allocated(self%test)) deallocate(self%test) |
| 512 |
if (allocated(self%preprocess)) deallocate(self%preprocess) |
| 513 |
|
| 514 |
!> Load base fields |
| 515 |
call self%feature_config_t%load_from_toml(table, error) |
| 516 |
if (allocated(error)) return |
| 517 |
|
| 518 |
call table%get_keys(keys) |
| 519 |
|
| 520 |
call get_value(table, "license", self%license) |
| 521 |
call get_value(table, "author", self%author) |
| 522 |
call get_value(table, "maintainer", self%maintainer) |
| 523 |
call get_value(table, "copyright", self%copyright) |
| 524 |
call get_value(table, "version", flag) |
| 525 |
call new_version(self%version, flag, error) |
| 526 |
if (allocated(error)) then |
| 527 |
error%message = class_name//': version error from TOML table - '//error%message |
| 528 |
return |
| 529 |
endif |
| 530 |
|
| 531 |
sub_deps: do ii = 1, size(keys) |
| 532 |
|
| 533 |
select case (keys(ii)%key) |
| 534 |
|
| 535 |
case ("profiles") |
| 536 |
|
| 537 |
call get_value(table, keys(ii), ptr) |
| 538 |
if (.not.associated(ptr)) then |
| 539 |
call fatal_error(error,class_name//': error retrieving profiles table') |
| 540 |
return |
| 541 |
end if |
| 542 |
|
| 543 |
!> Read all packages |
| 544 |
call ptr%get_keys(pkg_keys) |
| 545 |
allocate(self%profiles(size(pkg_keys))) |
| 546 |
|
| 547 |
do jj = 1, size(pkg_keys) |
| 548 |
call get_value(ptr, pkg_keys(jj), ptr_pkg) |
| 549 |
call self%profiles(jj)%load_from_toml(ptr_pkg, error) |
| 550 |
if (allocated(error)) return |
| 551 |
end do |
| 552 |
|
| 553 |
case default |
| 554 |
cycle sub_deps |
| 555 |
end select |
| 556 |
|
| 557 |
end do sub_deps |
| 558 |
|
| 559 |
end subroutine load_from_toml |
| 560 |
|
| 561 |
!> Export package configuration for a given (OS+compiler) platform |
| 562 |
type(package_config_t) function export_config(self, platform, features, profile, verbose, error) result(cfg) |
| 563 |
|
| 564 |
!> Instance of the package configuration |
| 565 |
class(package_config_t), intent(in), target :: self |
| 566 |
|
| 567 |
!> Target platform |
| 568 |
type(platform_config_t), intent(in) :: platform |
| 569 |
|
| 570 |
!> Optional list of features to apply (cannot be used with profile) |
| 571 |
type(string_t), optional, intent(in), target :: features(:) |
| 572 |
|
| 573 |
!> Optional profile name to apply (cannot be used with features) |
| 574 |
character(len=*), optional, intent(in) :: profile |
| 575 |
|
| 576 |
!> Verbose output flag |
| 577 |
logical, optional, intent(in) :: verbose |
| 578 |
|
| 579 |
!> Error handling |
| 580 |
type(error_t), allocatable, intent(out) :: error |
| 581 |
|
| 582 |
integer :: i, idx |
| 583 |
type(string_t), pointer :: want_features(:) |
| 584 |
logical :: apply_default |
| 585 |
|
| 586 |
! Validate that both profile and features are not specified simultaneously |
| 587 |
if (present(profile) .and. present(features)) then |
| 588 |
call syntax_error(error, "Cannot specify both 'profile' and 'features' parameters simultaneously") |
| 589 |
return |
| 590 |
end if |
| 591 |
|
| 592 |
! Copy the entire package configuration |
| 593 |
cfg = self |
| 594 |
|
| 595 |
! Determine which features to apply and whether to include the "default" profile. |
| 596 |
! Default profile features are applied when: |
| 597 |
! - No explicit profile/features are specified (implicit debug build) |
| 598 |
! - The "debug" or "release" profile is requested |
| 599 |
! Default profile features are NOT applied when: |
| 600 |
! - An explicit features list is specified |
| 601 |
! - A custom (non-debug/release) profile is requested |
| 602 |
apply_default = .false. |
| 603 |
|
| 604 |
if (present(features)) then |
| 605 |
want_features => features |
| 606 |
elseif (present(profile)) then |
| 607 |
idx = find_profile(self, profile) |
| 608 |
if (idx<=0) then |
| 609 |
call fatal_error(error, "Cannot find profile "//profile//" in package "//self%name) |
| 610 |
return |
| 611 |
end if |
| 612 |
want_features => self%profiles(idx)%features |
| 613 |
apply_default = (profile == "debug" .or. profile == "release") |
| 614 |
else |
| 615 |
nullify(want_features) |
| 616 |
apply_default = .true. |
| 617 |
endif |
| 618 |
|
| 619 |
! Apply "default" profile features first (baseline configuration) |
| 620 |
if (apply_default) then |
| 621 |
idx = find_profile(self, "default") |
| 622 |
if (idx > 0) then |
| 623 |
call apply_default_features(self, self%profiles(idx), cfg, platform, verbose, error) |
| 624 |
if (allocated(error)) return |
| 625 |
end if |
| 626 |
end if |
| 627 |
|
| 628 |
! Then apply the requested features on top |
| 629 |
apply_features: if (associated(want_features)) then |
| 630 |
do i=1,size(want_features) |
| 631 |
|
| 632 |
! Find feature |
| 633 |
idx = self%find_feature(want_features(i)%s) |
| 634 |
if (idx<=0) then |
| 635 |
call fatal_error(error, "Cannot find feature "//want_features(i)%s//& |
| 636 |
" in package "//self%name) |
| 637 |
return |
| 638 |
end if |
| 639 |
|
| 640 |
! Print feature collection info if verbose |
| 641 |
if (present(verbose)) then |
| 642 |
if (verbose) then |
| 643 |
call print_feature_collection(self%features(idx), platform) |
| 644 |
end if |
| 645 |
end if |
| 646 |
|
| 647 |
! Add it to the current configuration |
| 648 |
call self%features(idx)%merge_into_package(cfg, platform, error) |
| 649 |
if (allocated(error)) return |
| 650 |
|
| 651 |
end do |
| 652 |
end if apply_features |
| 653 |
|
| 654 |
! Ensure allocatable fields are always allocated with default values if not already set |
| 655 |
if (.not. allocated(cfg%build)) then |
| 656 |
allocate(cfg%build) |
| 657 |
cfg%build%auto_executables = .true. |
| 658 |
cfg%build%auto_examples = .true. |
| 659 |
cfg%build%auto_tests = .true. |
| 660 |
cfg%build%module_naming = .false. |
| 661 |
end if |
| 662 |
|
| 663 |
if (.not. allocated(cfg%install)) then |
| 664 |
allocate(cfg%install) |
| 665 |
cfg%install%library = .false. |
| 666 |
cfg%install%test = .false. |
| 667 |
end if |
| 668 |
|
| 669 |
if (.not. allocated(cfg%fortran)) then |
| 670 |
allocate(cfg%fortran) |
| 671 |
cfg%fortran%implicit_typing = .false. |
| 672 |
cfg%fortran%implicit_external = .false. |
| 673 |
cfg%fortran%source_form = 'free' |
| 674 |
end if |
| 675 |
|
| 676 |
end function export_config |
| 677 |
|
| 678 |
!> Find profile by name, returns index or 0 if not found |
| 679 |
function find_profile(self, profile_name) result(idx) |
| 680 |
|
| 681 |
!> Instance of the package configuration |
| 682 |
class(package_config_t), intent(in) :: self |
| 683 |
|
| 684 |
!> Name of the feature to find |
| 685 |
character(len=*), intent(in) :: profile_name |
| 686 |
|
| 687 |
!> Index of the feature (0 if not found) |
| 688 |
integer :: idx |
| 689 |
|
| 690 |
integer :: i |
| 691 |
|
| 692 |
idx = 0 |
| 693 |
|
| 694 |
! Check if features are allocated |
| 695 |
if (.not. allocated(self%profiles)) return |
| 696 |
|
| 697 |
! Search through features array |
| 698 |
do i = 1, size(self%profiles) |
| 699 |
if (allocated(self%profiles(i)%name)) then |
| 700 |
if (self%profiles(i)%name == profile_name) then |
| 701 |
idx = i |
| 702 |
return |
| 703 |
end if |
| 704 |
end if |
| 705 |
end do |
| 706 |
|
| 707 |
end function find_profile |
| 708 |
|
| 709 |
!> Apply all features from a profile into the package configuration |
| 710 |
subroutine apply_default_features(self, prof, cfg, platform, verbose, error) |
| 711 |
|
| 712 |
!> Instance of the package configuration |
| 713 |
class(package_config_t), intent(in) :: self |
| 714 |
|
| 715 |
!> Profile whose features to apply |
| 716 |
type(profile_config_t), intent(in) :: prof |
| 717 |
|
| 718 |
!> Package configuration to merge into |
| 719 |
type(package_config_t), intent(inout) :: cfg |
| 720 |
|
| 721 |
!> Target platform |
| 722 |
type(platform_config_t), intent(in) :: platform |
| 723 |
|
| 724 |
!> Verbose output flag |
| 725 |
logical, optional, intent(in) :: verbose |
| 726 |
|
| 727 |
!> Error handling |
| 728 |
type(error_t), allocatable, intent(out) :: error |
| 729 |
|
| 730 |
integer :: i, idx |
| 731 |
|
| 732 |
if (.not. allocated(prof%features)) return |
| 733 |
|
| 734 |
do i = 1, size(prof%features) |
| 735 |
|
| 736 |
idx = self%find_feature(prof%features(i)%s) |
| 737 |
if (idx <= 0) then |
| 738 |
call fatal_error(error, "Cannot find feature "//prof%features(i)%s//& |
| 739 |
" in package "//self%name) |
| 740 |
return |
| 741 |
end if |
| 742 |
|
| 743 |
if (present(verbose)) then |
| 744 |
if (verbose) then |
| 745 |
call print_feature_collection(self%features(idx), platform) |
| 746 |
end if |
| 747 |
end if |
| 748 |
|
| 749 |
call self%features(idx)%merge_into_package(cfg, platform, error) |
| 750 |
if (allocated(error)) return |
| 751 |
|
| 752 |
end do |
| 753 |
|
| 754 |
end subroutine apply_default_features |
| 755 |
|
| 756 |
!> Find feature by name, returns index or 0 if not found |
| 757 |
function find_feature(self, feature_name) result(idx) |
| 758 |
|
| 759 |
!> Instance of the package configuration |
| 760 |
class(package_config_t), intent(in) :: self |
| 761 |
|
| 762 |
!> Name of the feature to find |
| 763 |
character(len=*), intent(in) :: feature_name |
| 764 |
|
| 765 |
!> Index of the feature (0 if not found) |
| 766 |
integer :: idx |
| 767 |
|
| 768 |
integer :: i |
| 769 |
|
| 770 |
idx = 0 |
| 771 |
|
| 772 |
! Check if features are allocated |
| 773 |
if (.not. allocated(self%features)) return |
| 774 |
|
| 775 |
! Search through features array |
| 776 |
do i = 1, size(self%features) |
| 777 |
if (allocated(self%features(i)%base%name)) then |
| 778 |
if (self%features(i)%base%name == feature_name) then |
| 779 |
idx = i |
| 780 |
return |
| 781 |
end if |
| 782 |
end if |
| 783 |
end do |
| 784 |
|
| 785 |
end function find_feature |
| 786 |
|
| 787 |
|
| 788 |
!> Validate profiles - check for duplicate names and valid feature references |
| 789 |
subroutine validate_profiles(self, error) |
| 790 |
|
| 791 |
!> Instance of the package configuration |
| 792 |
class(package_config_t), intent(in) :: self |
| 793 |
|
| 794 |
!> Error handling |
| 795 |
type(error_t), allocatable, intent(out) :: error |
| 796 |
|
| 797 |
integer :: i, j |
| 798 |
|
| 799 |
! Check if profiles are allocated |
| 800 |
if (.not. allocated(self%profiles)) return |
| 801 |
|
| 802 |
! Check for duplicate profile names |
| 803 |
do i = 1, size(self%profiles) |
| 804 |
do j = i + 1, size(self%profiles) |
| 805 |
if (allocated(self%profiles(i)%name) .and. allocated(self%profiles(j)%name)) then |
| 806 |
if (self%profiles(i)%name == self%profiles(j)%name) then |
| 807 |
call syntax_error(error, "Duplicate profile name '" // self%profiles(i)%name // "'") |
| 808 |
return |
| 809 |
end if |
| 810 |
end if |
| 811 |
end do |
| 812 |
end do |
| 813 |
|
| 814 |
! Check that all profile features reference valid features |
| 815 |
do i = 1, size(self%profiles) |
| 816 |
if (allocated(self%profiles(i)%features)) then |
| 817 |
do j = 1, size(self%profiles(i)%features) |
| 818 |
! Check if feature exists (case sensitive) |
| 819 |
if (self%find_feature(self%profiles(i)%features(j)%s) == 0) then |
| 820 |
call syntax_error(error, "Profile '" // self%profiles(i)%name // & |
| 821 |
"' references undefined feature '" // self%profiles(i)%features(j)%s // "'") |
| 822 |
return |
| 823 |
end if |
| 824 |
end do |
| 825 |
end if |
| 826 |
end do |
| 827 |
|
| 828 |
end subroutine validate_profiles |
| 829 |
|
| 830 |
!> Check if there is a CPP preprocessor configuration |
| 831 |
elemental logical function has_cpp(self) |
| 832 |
class(package_config_t), intent(in) :: self |
| 833 |
|
| 834 |
integer :: i |
| 835 |
|
| 836 |
has_cpp = self%feature_config_t%has_cpp() |
| 837 |
if (has_cpp) return |
| 838 |
if (.not.allocated(self%features)) return |
| 839 |
|
| 840 |
do i=1,size(self%features) |
| 841 |
has_cpp = self%features(i)%has_cpp() |
| 842 |
if (has_cpp) return |
| 843 |
end do |
| 844 |
|
| 845 |
end function has_cpp |
| 846 |
|
| 847 |
!> Print feature collection information in verbose mode |
| 848 |
subroutine print_feature_collection(collection, platform) |
| 849 |
use, intrinsic :: iso_fortran_env, only: stdout => output_unit |
| 850 |
use fpm_compiler, only: compiler_name |
| 851 |
use fpm_environment, only: os_name |
| 852 |
use fpm_strings, only: string_cat |
| 853 |
type(feature_collection_t), intent(in) :: collection |
| 854 |
type(platform_config_t), intent(in) :: platform |
| 855 |
|
| 856 |
type(feature_config_t) :: extracted |
| 857 |
type(error_t), allocatable :: error_tmp |
| 858 |
integer :: i, j, n_macros |
| 859 |
|
| 860 |
! Extract the feature configuration for the target platform |
| 861 |
extracted = collection%extract_for_target(platform, error_tmp) |
| 862 |
if (allocated(error_tmp)) return |
| 863 |
|
| 864 |
! Print header with feature name |
| 865 |
if (allocated(extracted%name)) then |
| 866 |
print *, '+ feature collection: ', trim(extracted%name) |
| 867 |
end if |
| 868 |
|
| 869 |
! Print platform information |
| 870 |
print *, '+ platform: ', platform%compiler_name(), ' on ', platform%os_name() |
| 871 |
|
| 872 |
! Print flags |
| 873 |
if (allocated(extracted%flags)) then |
| 874 |
print *, '+ flags: ', trim(extracted%flags) |
| 875 |
end if |
| 876 |
if (allocated(extracted%c_flags)) then |
| 877 |
print *, '+ c-flags: ', trim(extracted%c_flags) |
| 878 |
end if |
| 879 |
if (allocated(extracted%cxx_flags)) then |
| 880 |
print *, '+ cxx-flags: ', trim(extracted%cxx_flags) |
| 881 |
end if |
| 882 |
if (allocated(extracted%link_time_flags)) then |
| 883 |
print *, '+ link-flags: ', trim(extracted%link_time_flags) |
| 884 |
end if |
| 885 |
|
| 886 |
! Print preprocessor macros |
| 887 |
if (allocated(extracted%preprocess)) then |
| 888 |
n_macros = 0 |
| 889 |
do i = 1, size(extracted%preprocess) |
| 890 |
if (allocated(extracted%preprocess(i)%macros)) then |
| 891 |
n_macros = n_macros + size(extracted%preprocess(i)%macros) |
| 892 |
end if |
| 893 |
end do |
| 894 |
|
| 895 |
if (n_macros > 0) then |
| 896 |
print *, '+ cpp-macros: yes (', n_macros, ' defined)' |
| 897 |
do i = 1, size(extracted%preprocess) |
| 898 |
if (allocated(extracted%preprocess(i)%macros)) then |
| 899 |
do j = 1, size(extracted%preprocess(i)%macros) |
| 900 |
print *, '+ - ', trim(extracted%preprocess(i)%macros(j)%s) |
| 901 |
end do |
| 902 |
end if |
| 903 |
end do |
| 904 |
else |
| 905 |
print *, '+ cpp-macros: no' |
| 906 |
end if |
| 907 |
else |
| 908 |
print *, '+ cpp-macros: no' |
| 909 |
end if |
| 910 |
|
| 911 |
! Print description if available |
| 912 |
if (allocated(extracted%description)) then |
| 913 |
print *, '+ description: ', trim(extracted%description) |
| 914 |
end if |
| 915 |
|
| 916 |
! Print number of variants in collection |
| 917 |
if (allocated(collection%variants)) then |
| 918 |
print *, '+ variants: ', size(collection%variants) |
| 919 |
end if |
| 920 |
|
| 921 |
end subroutine print_feature_collection |
| 922 |
|
| 923 |
end module fpm_manifest_package |