platform.scm 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. #:use-module (srfi srfi-34)
  25. #:use-module (srfi srfi-35)
  26. #:export (platform
  27. platform?
  28. platform-target
  29. platform-system
  30. platform-linux-architecture
  31. platform-glibc-dynamic-linker
  32. &platform-not-found-error
  33. platform-not-found-error?
  34. false-if-platform-not-found
  35. platform-modules
  36. platforms
  37. lookup-platform-by-system
  38. lookup-platform-by-target
  39. lookup-platform-by-target-or-system
  40. platform-system->target
  41. platform-target->system
  42. systems
  43. targets))
  44. ;;;
  45. ;;; Platform record.
  46. ;;;
  47. ;; Description of a platform supported by GNU Guix.
  48. ;;
  49. ;; The 'target' field must be a valid GNU triplet as defined here:
  50. ;; https://www.gnu.org/software/autoconf/manual/autoconf-2.68/html_node/Specifying-Target-Triplets.html.
  51. ;; It is used for cross-compilation purposes.
  52. ;;
  53. ;; The 'system' field is the name of the corresponding system as defined in
  54. ;; the (gnu packages bootstrap) module. It can be for instance
  55. ;; "aarch64-linux" or "armhf-linux". It is used to emulate a different host
  56. ;; architecture, for instance i686-linux on x86_64-linux-gnu, or armhf-linux
  57. ;; on x86_64-linux, using the QEMU binfmt transparent emulation mechanism.
  58. ;;
  59. ;; The 'linux-architecture' is only relevant if the kernel is Linux. In that
  60. ;; case, it corresponds to the ARCH variable used when building Linux.
  61. ;;
  62. ;; The 'glibc-dynamic-linker' field is the name of Glibc's dynamic linker for
  63. ;; the corresponding system.
  64. (define-record-type* <platform> platform make-platform
  65. platform?
  66. (target platform-target)
  67. (system platform-system)
  68. (linux-architecture platform-linux-architecture
  69. (default #false))
  70. (glibc-dynamic-linker platform-glibc-dynamic-linker))
  71. ;;;
  72. ;;; Exceptions.
  73. ;;;
  74. (define-condition-type &platform-not-found-error &error
  75. platform-not-found-error?
  76. (target-or-system platform-not-found-error-target-or-system))
  77. (define-syntax-rule (false-if-platform-not-found exp)
  78. "Evaluate EXP but return #f if it raises a platform-not-found-error?
  79. exception."
  80. (guard (ex ((platform-not-found-error? ex) #f))
  81. exp))
  82. ;;;
  83. ;;; Platforms.
  84. ;;;
  85. (define (platform-modules)
  86. "Return the list of platform modules."
  87. (all-modules (map (lambda (entry)
  88. `(,entry . "guix/platforms"))
  89. %load-path)
  90. #:warn warn-about-load-error))
  91. (define platforms
  92. ;; The list of publically-known platforms.
  93. (memoize
  94. (lambda ()
  95. (fold-module-public-variables (lambda (obj result)
  96. (if (platform? obj)
  97. (cons obj result)
  98. result))
  99. '()
  100. (platform-modules)))))
  101. (define (lookup-platform-by-system system)
  102. "Return the platform corresponding to the given SYSTEM. Raise
  103. &PLATFORM-NOT-FOUND-ERROR when no platform could be found."
  104. (or (find (lambda (platform)
  105. (let ((s (platform-system platform)))
  106. (and (string? s) (string=? s system))))
  107. (platforms))
  108. (raise-exception (condition (&platform-not-found-error
  109. (target-or-system system))))))
  110. (define (lookup-platform-by-target target)
  111. "Return the platform corresponding to the given TARGET. Raise
  112. &PLATFORM-NOT-FOUND-ERROR when no platform could be found."
  113. (or (find (lambda (platform)
  114. (let ((t (platform-target platform)))
  115. (and (string? t) (string=? t target))))
  116. (platforms))
  117. (raise-exception (condition (&platform-not-found-error
  118. (target-or-system target))))))
  119. (define (lookup-platform-by-target-or-system target-or-system)
  120. "Return the platform corresponding to the given TARGET or SYSTEM. Raise
  121. &PLATFORM-NOT-FOUND-ERROR when no platform could be found."
  122. (or (false-if-platform-not-found (lookup-platform-by-target target-or-system))
  123. (false-if-platform-not-found (lookup-platform-by-system target-or-system))
  124. (raise-exception (condition (&platform-not-found-error
  125. (target-or-system target-or-system))))))
  126. (define (platform-system->target system)
  127. "Return the target matching the given SYSTEM if it exists or false
  128. otherwise."
  129. (let ((platform (lookup-platform-by-system system)))
  130. (and=> platform platform-target)))
  131. (define (platform-target->system target)
  132. "Return the system matching the given TARGET if it exists or false
  133. otherwise."
  134. (let ((platform (lookup-platform-by-target target)))
  135. (and=> platform platform-system)))
  136. ;;;
  137. ;;; Systems & Targets.
  138. ;;;
  139. (define (systems)
  140. "Return the list of supported systems."
  141. (delete-duplicates
  142. (filter-map platform-system (platforms))))
  143. (define (targets)
  144. "Return the list of supported targets."
  145. (map platform-target (platforms)))