kconfig.scm 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2020 Stefan <stefan-guix@vodafonemail.de>
  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 build kconfig)
  19. #:use-module (ice-9 rdelim)
  20. #:use-module (ice-9 regex)
  21. #:use-module (srfi srfi-1)
  22. #:use-module (srfi srfi-26)
  23. #:export (modify-defconfig
  24. verify-config))
  25. ;; Commentary:
  26. ;;
  27. ;; Builder-side code to modify configurations for the Kconfig build system as
  28. ;; used by Linux and U-Boot.
  29. ;;
  30. ;; Code:
  31. (define (pair->config-string pair)
  32. "Convert a PAIR back to a config-string."
  33. (let* ((key (first pair))
  34. (value (cdr pair)))
  35. (if (string? key)
  36. (if (string? value)
  37. (string-append key "=" value)
  38. (string-append "# " key " is not set"))
  39. value)))
  40. (define (config-string->pair config-string)
  41. "Parse a configuration string like \"CONFIG_EXAMPLE=m\" into a key-value pair.
  42. An error is thrown for invalid configurations.
  43. \"CONFIG_A=y\" -> '(\"CONFIG_A\" . \"y\")
  44. \"CONFIG_B=\\\"\\\"\" -> '(\"CONFIG_B\" . \"\\\"\\\"\")
  45. \"CONFIG_C=\" -> '(\"CONFIG_C\" . \"\")
  46. \"# CONFIG_E is not set\" -> '(\"CONFIG_E\" . #f)
  47. \"CONFIG_D\" -> '(\"CONFIG_D\" . #f)
  48. \"# Any comment\" -> '(#f . \"# Any comment\")
  49. \"\" -> '(#f . \"\")
  50. \"# CONFIG_E=y\" -> (error \"Invalid configuration\")
  51. \"CONFIG_E is not set\" -> (error \"Invalid configuration\")
  52. \"Anything else\" -> (error \"Invalid configuration\")"
  53. (define config-regexp
  54. (make-regexp
  55. ;; (match:substring (string-match "=(.*)" "=") 1) returns "", but the
  56. ;; pattern "=(.+)?" makes it return #f instead. From a "CONFIG_A=" we like
  57. ;; to get "", which later emits "CONFIG_A=" again.
  58. (string-append "^ *(#[\\t ]*)?(CONFIG_[a-zA-Z0-9_]+)([\\t ]*="
  59. "[\\t ]*(.*)|([\\t ]+is[\\t ]+not[\\t ]+set))?$")))
  60. (define config-comment-regexp
  61. (make-regexp "^([\\t ]*(#.*)?)$"))
  62. (let ((match (regexp-exec config-regexp (string-trim-right config-string))))
  63. (if match
  64. (let* ((comment (match:substring match 1))
  65. (key (match:substring match 2))
  66. (unset (match:substring match 5))
  67. (value (and (not comment)
  68. (not unset)
  69. (match:substring match 4))))
  70. (if (eq? (not comment) (not unset))
  71. ;; The key is uncommented and set or commented and unset.
  72. (cons key value)
  73. ;; The key is set or unset ambigiously.
  74. (error (format #f "invalid configuration, did you mean \"~a\"?"
  75. (pair->config-string (cons key #f)))
  76. config-string)))
  77. ;; This is not a valid or ambigious config-string, but maybe a
  78. ;; comment.
  79. (if (regexp-exec config-comment-regexp config-string)
  80. (cons #f config-string) ;keep valid comments
  81. (error "Invalid configuration" config-string)))))
  82. (define (defconfig->alist defconfig)
  83. "Convert the content of a DEFCONFIG (or .config) file into an alist."
  84. (with-input-from-file defconfig
  85. (lambda ()
  86. (let loop ((alist '())
  87. (line (read-line)))
  88. (if (eof-object? line)
  89. ;; Building the alist is done, now check for duplicates.
  90. ;; Note: the filter invocation is used to remove comments.
  91. (let loop ((keys (map first (filter first alist)))
  92. (duplicates '()))
  93. (if (null? keys)
  94. ;; The search for duplicates is done.
  95. ;; Return the alist or throw an error on duplicates.
  96. (if (null? duplicates)
  97. (reverse alist)
  98. (error
  99. (format #f "duplicate configurations in ~a" defconfig)
  100. (reverse duplicates)))
  101. ;; Continue the search for duplicates.
  102. (loop (cdr keys)
  103. (if (member (first keys) (cdr keys))
  104. (cons (first keys) duplicates)
  105. duplicates))))
  106. ;; Build the alist.
  107. (loop (cons (config-string->pair line) alist)
  108. (read-line)))))))
  109. (define (modify-defconfig defconfig configs)
  110. "This function can modify a given DEFCONFIG (or .config) file by adding,
  111. changing or removing the list of strings in CONFIGS. This allows customization
  112. of Kconfig based projects like the kernel Linux or the bootloader 'Das U-Boot'.
  113. These are examples for CONFIGS to add, change or remove configurations to/from
  114. DEFCONFIG:
  115. '(\"CONFIG_A=\\\"a\\\"\"
  116. \"CONFIG_B=0\"
  117. \"CONFIG_C=y\"
  118. \"CONFIG_D=m\"
  119. \"CONFIG_E=\"
  120. \"# CONFIG_G is not set\"
  121. ;; For convenience this abbrevation can be used for not set configurations.
  122. \"CONFIG_F\")
  123. Instead of a list, CONFIGS can be a string with one configuration per line."
  124. ;; Normalize CONFIGS to a list of configuration pairs.
  125. (let* ((config-pairs (map config-string->pair
  126. (append-map (cut string-split <> #\newline)
  127. (if (string? configs)
  128. (list configs)
  129. configs))))
  130. ;; Generate a blocklist from all valid keys in config-pairs.
  131. (blocklist (delete #f (map first config-pairs)))
  132. ;; Generate an alist from the defconfig without the keys in blocklist.
  133. (filtered-defconfig-pairs (remove (lambda (pair)
  134. (member (first pair) blocklist))
  135. (defconfig->alist defconfig))))
  136. (with-output-to-file defconfig
  137. (lambda ()
  138. (for-each (lambda (pair)
  139. (display (pair->config-string pair))
  140. (newline))
  141. (append filtered-defconfig-pairs config-pairs))))))
  142. (define (verify-config config defconfig)
  143. "Verify that the CONFIG file contains all configurations from the DEFCONFIG
  144. file. When the verification fails, raise an error with the mismatching keys
  145. and their values."
  146. (let* ((config-pairs (defconfig->alist config))
  147. (defconfig-pairs (defconfig->alist defconfig))
  148. (mismatching-pairs
  149. (remove (lambda (pair)
  150. ;; Remove all configurations, whose values are #f and
  151. ;; whose keys are not in config-pairs, as not in
  152. ;; config-pairs means unset, ...
  153. (and (not (cdr pair))
  154. (not (assoc-ref config-pairs (first pair)))))
  155. ;; ... from the defconfig-pairs different to config-pairs.
  156. (lset-difference equal?
  157. ;; Remove comments by filtering with first.
  158. (filter first defconfig-pairs)
  159. config-pairs))))
  160. (unless (null? mismatching-pairs)
  161. (error (format #f "Mismatching configurations in ~a and ~a"
  162. config defconfig)
  163. (map (lambda (mismatching-pair)
  164. (let* ((key (first mismatching-pair))
  165. (defconfig-value (cdr mismatching-pair))
  166. (config-value (assoc-ref config-pairs key)))
  167. (cons key (list (list config-value defconfig-value)))))
  168. mismatching-pairs)))))