unofficial mirror of guix-patches@gnu.org 
 help / color / mirror / code / Atom feed
blob e32503c9c33c5f891a803936fda04076e2477582 8169 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
 
;;; 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_E is not set\" -> '(\"CONFIG_E\" . #f)
\"CONFIG_C=\"             -> '(\"CONFIG_C\" . #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.  For a "CONFIG_A=" we like
     ;; to get #f, which later emits "# … is not set".
     "^ *(#[\\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_F\"
  \"# CONFIG_G is not set\")

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 e32503c9c3 ...
found e32503c9c3 in https://yhetil.org/guix-patches/125F42BC-E90F-47A5-9997-7E0089AF07BF@vodafonemail.de/

applying [1/1] https://yhetil.org/guix-patches/125F42BC-E90F-47A5-9997-7E0089AF07BF@vodafonemail.de/
diff --git a/guix/build/kconfig.scm b/guix/build/kconfig.scm
new file mode 100644
index 0000000000..e32503c9c3

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

index at:
100644 e32503c9c33c5f891a803936fda04076e2477582	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 public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).