all messages for Guix-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
blob 0cfaffe0566e7d7c636937496b575c0707b55389 8246 bytes (raw)
name: guix/build/kconfig.scm 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
 
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2020 Stefan <stefan-guix@vodafonemail.de>
;;;
;;; This file is part of GNU Guix.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

(define-module (guix build kconfig)
  #:use-module  (ice-9 rdelim)
  #:use-module  (ice-9 regex)
  #:use-module  (srfi srfi-1)
  #:use-module  (srfi srfi-26)
  #:export (modify-defconfig
            verify-config))

;; Commentary:
;;
;; Builder-side code to modify configurations for the Kconfig build system as
;; used by Linux and U-Boot.
;;
;; Code:

(define (config-string->pair config-string)
  "Parse a configuration string like \"CONFIG_EXAMPLE=m\" into a key-value pair.
An error is thrown for invalid configurations.

\"CONFIG_A=y\"            -> '(\"CONFIG_A\" . \"y\")
\"CONFIG_B=\\\"\\\"\"         -> '(\"CONFIG_B\" . \"\\\"\\\"\")
\"CONFIG_C=\"             -> '(\"CONFIG_C\" . \"\")
\"# CONFIG_E is not set\" -> '(\"CONFIG_E\" . #f)
\"CONFIG_D\"              -> '(\"CONFIG_D\" . #f)
\"# Any comment\"         -> '(#f . \"# Any comment\")
\"\"                      -> '(#f . \"\")
\"# CONFIG_E=y\"          -> (error \"Invalid configuration\")
\"CONFIG_E is not set\"   -> (error \"Invalid configuration\")
\"Anything else\"         -> (error \"Invalid configuration\")"
  (define config-regexp
    (make-regexp
     ;; (match:substring (string-match "=(.*)" "=") 1) returns "", but the
     ;; pattern "=(.+)?" makes it return #f instead.  From a "CONFIG_A=" we like
     ;; to get "", which later emits "CONFIG_A=" again.
     "^ *(#[\\t ]*)?(CONFIG_[a-zA-Z0-9_]+)([\\t ]*=[\\t ]*(.*)|([\\t ]+is[\\t ]+not[\\t ]+set))?$"))

  (define config-comment-regexp
    (make-regexp "^([\\t ]*(#.*)?)$"))

  (let ((match (regexp-exec config-regexp (string-trim-right config-string))))
    (if match
        (let* ((comment (match:substring match 1))
               (key (match:substring match 2))
               (unset (match:substring match 5))
               (value (and (not comment)
                           (not unset)
                           (match:substring match 4))))
          (if (eq? (not comment) (not unset))
              ;; The key is uncommented and set or commented and unset.
              (cons key value)
              ;; The key is set or unset ambigiously.
              (error (format #f "Invalid configuration, did you mean \"~a\"?"
                             (pair->config-string (cons key #f)))
                     config-string)))
        ;; This is not a valid or ambigious config-string, but mayby a comment.
        (if (regexp-exec config-comment-regexp config-string)
            ;; We keep valid comments.
            (cons #f config-string)
            (error "Invalid configuration" config-string)))))

(define (pair->config-string pair)
  "Convert a PAIR back to a config-string."
  (let* ((key (first pair))
         (value (cdr pair)))
    (if (string? key)
        (if (string? value)
            (string-append key "=" value)
            (string-append "# " key " is not set"))
        value)))

(define (defconfig->alist defconfig)
  "Convert the content of a DEFCONFIG (or .config) file into an alist."
  (with-input-from-file defconfig
    (lambda ()
      (let loop ((alist '())
                 (line (read-line)))
        (if (eof-object? line)
            ;; Building the alist is done, now check for duplicates.
            (let loop ((keys (map first (filter first alist)))
                       (duplicates '()))
              (if (null? keys)
                  ;; The search for duplicates is done.
                  ;; Return the alist or throw an error on duplicates.
                  (if (null? duplicates)
                      alist
                      (error
                       (format #f "Duplicate configurations in ~a" defconfig)
                       duplicates))
                  ;; Continue the search for duplicates.
                  (loop (cdr keys)
                        (if (member (first keys) (cdr keys) equal?)
                            (cons (first keys) duplicates)
                            duplicates))))
            ;; Build the alist.
            (loop (cons (config-string->pair line) alist)
                  (read-line)))))))

(define (modify-defconfig defconfig configs)
  "This function can modify a given DEFCONFIG (or .config) file by adding,
changing or removing the list of strings in CONFIGS.  This allows customization
of Kconfig based projects like the kernel Linux or the bootloader 'Das U-Boot'.

These are examples for CONFIGS to add, change or remove configurations to/from
DEFCONFIG:

'(\"CONFIG_A=\\\"a\\\"\"
  \"CONFIG_B=0\"
  \"CONFIG_C=y\"
  \"CONFIG_D=m\"
  \"CONFIG_E=\"
  \"# CONFIG_G is not set\"
  ;; For convinience this abbrevation can be used for not set configurations.
  \"CONFIG_F\")

Instead of a list, CONFGIS can be a string with one configuration per line."
  (let* (;; Split the configs into a list of single configuations.
         ;; To minimize mistakes, we support a string and a list of strings,
         ;; each with newlines to separate configurations.
         (config-pairs (map config-string->pair
                            (append-map (cut string-split <>  #\newline)
                                        (if (string? configs)
                                            (list configs)
                                            configs))))
         ;; Generate a blocklist from all valid keys in config-pairs.
         (blocklist (delete #f (map first config-pairs)))
         ;; Generate an alist from the defconifg without the keys in blocklist.
         (filtered-defconfig-pairs (remove (lambda (pair)
                                             (member (first pair) blocklist))
                                           (defconfig->alist defconfig))))
    (with-output-to-file defconfig
      (lambda ()
        (for-each
           (lambda (pair)
             (display (pair->config-string pair))
             (newline))
           (append filtered-defconfig-pairs config-pairs))))))

(define (verify-config config defconfig)
  "Verify that the CONFIG file contains all configurations from the DEFCONFIG
file and return #t in this case. Otherwise throw an error with the mismatching
keys and their values."
  (let* ((config-pairs (defconfig->alist config))
         (defconfig-pairs (defconfig->alist defconfig))
         (mismatching-pairs
          (remove (lambda (pair)
                    ;; Remove all configurations, whose values are #f and whose
                    ;; keys are not in config-pairs, as not in config-pairs
                    ;; means unset, …
                    (and (not (cdr pair))
                         (not (assoc-ref config-pairs (first pair)))))
                  ;; … from the defconfig-pairs different to config-pairs.
                  (lset-difference equal?
                                   ;; Remove comments by filtering with first.
                                   (filter first defconfig-pairs)
                                   config-pairs))))
    (if (null? mismatching-pairs)
        #t
        (error (format #f
                       "Mismatching configurations in ~a and ~a"
                       config
                       defconfig)
               (map (lambda (mismatching-pair)
                      (let* ((key (first mismatching-pair))
                             (defconfig-value (cdr mismatching-pair))
                             (config-value (assoc-ref config-pairs key)))
                        (cons key (list (list config-value defconfig-value)))))
                    mismatching-pairs)))))

debug log:

solving 0cfaffe056 ...
found 0cfaffe056 in https://yhetil.org/guix/204332DD-AA02-4A31-9B48-FB3FAB9BD8F3@vodafonemail.de/

applying [1/1] https://yhetil.org/guix/204332DD-AA02-4A31-9B48-FB3FAB9BD8F3@vodafonemail.de/
diff --git a/guix/build/kconfig.scm b/guix/build/kconfig.scm
new file mode 100644
index 0000000000..0cfaffe056

Checking patch guix/build/kconfig.scm...
Applied patch guix/build/kconfig.scm cleanly.

index at:
100644 0cfaffe0566e7d7c636937496b575c0707b55389	guix/build/kconfig.scm

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/guix.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.