platform.scm 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2021 Mathieu Othacehe <othacehe@gnu.org>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  6. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Guix is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (guix platform)
  19. #:use-module (guix discovery)
  20. #:use-module (guix memoization)
  21. #:use-module (guix records)
  22. #:use-module (guix ui)
  23. #:use-module (srfi srfi-1)
  24. #:export (platform
  25. platform?
  26. platform-target
  27. platform-system
  28. platform-linux-architecture
  29. platform-glibc-dynamic-linker
  30. platform-modules
  31. platforms
  32. lookup-platform-by-system
  33. lookup-platform-by-target
  34. lookup-platform-by-target-or-system
  35. platform-system->target
  36. platform-target->system
  37. systems
  38. targets))
  39. ;;;
  40. ;;; Platform record.
  41. ;;;
  42. ;; Description of a platform supported by GNU Guix.
  43. ;;
  44. ;; The 'target' field must be a valid GNU triplet as defined here:
  45. ;; https://www.gnu.org/software/autoconf/manual/autoconf-2.68/html_node/Specifying-Target-Triplets.html.
  46. ;; It is used for cross-compilation purposes.
  47. ;;
  48. ;; The 'system' field is the name of the corresponding system as defined in
  49. ;; the (gnu packages bootstrap) module. It can be for instance
  50. ;; "aarch64-linux" or "armhf-linux". It is used to emulate a different host
  51. ;; architecture, for instance i686-linux on x86_64-linux-gnu, or armhf-linux
  52. ;; on x86_64-linux, using the QEMU binfmt transparent emulation mechanism.
  53. ;;
  54. ;; The 'linux-architecture' is only relevant if the kernel is Linux. In that
  55. ;; case, it corresponds to the ARCH variable used when building Linux.
  56. ;;
  57. ;; The 'glibc-dynamic-linker' field is the name of Glibc's dynamic linker for
  58. ;; the corresponding system.
  59. (define-record-type* <platform> platform make-platform
  60. platform?
  61. (target platform-target)
  62. (system platform-system)
  63. (linux-architecture platform-linux-architecture
  64. (default #false))
  65. (glibc-dynamic-linker platform-glibc-dynamic-linker))
  66. ;;;
  67. ;;; Platforms.
  68. ;;;
  69. (define (platform-modules)
  70. "Return the list of platform modules."
  71. (all-modules (map (lambda (entry)
  72. `(,entry . "guix/platforms"))
  73. %load-path)
  74. #:warn warn-about-load-error))
  75. (define platforms
  76. ;; The list of publically-known platforms.
  77. (memoize
  78. (lambda ()
  79. (fold-module-public-variables (lambda (obj result)
  80. (if (platform? obj)
  81. (cons obj result)
  82. result))
  83. '()
  84. (platform-modules)))))
  85. (define (lookup-platform-by-system system)
  86. "Return the platform corresponding to the given SYSTEM."
  87. (find (lambda (platform)
  88. (let ((s (platform-system platform)))
  89. (and (string? s) (string=? s system))))
  90. (platforms)))
  91. (define (lookup-platform-by-target target)
  92. "Return the platform corresponding to the given TARGET."
  93. (find (lambda (platform)
  94. (let ((t (platform-target platform)))
  95. (and (string? t) (string=? t target))))
  96. (platforms)))
  97. (define (lookup-platform-by-target-or-system target-or-system)
  98. "Return the platform corresponding to the given TARGET or SYSTEM."
  99. (or (lookup-platform-by-target target-or-system)
  100. (lookup-platform-by-system target-or-system)))
  101. (define (platform-system->target system)
  102. "Return the target matching the given SYSTEM if it exists or false
  103. otherwise."
  104. (let ((platform (lookup-platform-by-system system)))
  105. (and=> platform platform-target)))
  106. (define (platform-target->system target)
  107. "Return the system matching the given TARGET if it exists or false
  108. otherwise."
  109. (let ((platform (lookup-platform-by-target target)))
  110. (and=> platform platform-system)))
  111. ;;;
  112. ;;; Systems & Targets.
  113. ;;;
  114. (define (systems)
  115. "Return the list of supported systems."
  116. (delete-duplicates
  117. (filter-map platform-system (platforms))))
  118. (define (targets)
  119. "Return the list of supported targets."
  120. (map platform-target (platforms)))