๐Ÿ“ฆ apache / nuttx

๐Ÿ“„ checkkconfig.py ยท 308 lines
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308#!/usr/bin/env python3
# tools/checkkconfig.py
#
# SPDX-License-Identifier: Apache-2.0
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.  The
# ASF licenses this file to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance with the
# License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
# License for the specific language governing permissions and limitations
# under the License.
#

#
# [checkkconfig.py] is a tool that simulates the effects of modifying a CONFIG item,
# Can be used to check whether my config changes are what I expected.
#
# usage: checkkconfig.py [-h] -f FILE (-s CONFIG VALUE | -d DIFF)
#
# optional arguments:
#  -h, --help            show this help message and exit
#  -f FILE, --file FILE  Path to the input defconfig file
#  -s CONFIG_XXX VALUE, --single CONFIG VALUE
#                        Analyze single change: CONFIG_NAME y/m/n
#  -d DIFF, --diff DIFF  Analyze changes from diff file
#
# example: ./tools/checkkconfig.py -f defconfig -s ELF n
#
# outputs:
# Change report for ELF=n
# Config Option                            Old                  New
# ----------------------------------------------------------------------
# BINFMT_LOADABLE                          y                    n
# ELF                                      y                    n
# ELF_STACKSIZE                            8192                 <unset>
# LIBC_ARCH_ELF                            y                    n
# LIBC_MODLIB                              y                    n
# MODLIB_ALIGN_LOG2                        2                    <unset>
# MODLIB_BUFFERINCR                        32                   <unset>
# MODLIB_BUFFERSIZE                        32                   <unset>
# MODLIB_MAXDEPEND                         2                    <unset>
# MODLIB_RELOCATION_BUFFERCOUNT            256                  <unset>
# MODLIB_SYMBOL_CACHECOUNT                 256                  <unset>
#
# As we can see, we can clearly know that
# if I turn off ELF in defconfig at this time,
# it will bring about the following configuration linkage changes
#
# It can also parse diff files, which can be used to check the changes of multiple configs.
# diff file example:
#  diff --git a/boards/demo/configs/nsh/defconfig b/boards/demo/configs/nsh/defconfig
#  index cf7d07c..5de20d4 100644
#  --- a/boards/demo/configs/nsh/defconfig
#  +++ b/boards/demo/configs/nsh/defconfig
#  @@ -51,7 +51,6 @@ CONFIG_ARMV7A_STRING_FUNCTION=y
#   CONFIG_ARM_PSCI=y
#   CONFIG_ARM_SEMIHOSTING_HOSTFS=y
#   CONFIG_ARM_THUMB=y
#  -CONFIG_AUDIO=y
#   CONFIG_BCH=y
#   CONFIG_BINFMT_ELF_EXECUTABLE=y
#   CONFIG_BLUETOOTH=y
#  @@ -104,7 +103,6 @@ CONFIG_DRIVERS_VIRTIO_SERIAL=y
#   CONFIG_DRIVERS_VIRTIO_SOUND=y
#   CONFIG_DRIVERS_WIFI_SIM=y
#   CONFIG_DRIVERS_WIRELESS=y
#  -CONFIG_ELF=y
#   CONFIG_ETC_ROMFS=y
#   CONFIG_EVENT_FD=y
#   CONFIG_EXAMPLES_FB=y
#
# example: ./tools/checkkconfig.py -f defconfig -d changes.diff
#
# outputs:
# Change report for diff: changes.diff
# Config Option                            Old                  New
# ----------------------------------------------------------------------
# AUDIO                                    y                    n
# AUDIO_BUFFER_NUMBYTES                    8192                 <unset>
# AUDIO_EXCLUDE_EQUALIZER                  y                    n
# AUDIO_EXCLUDE_REWIND                     y                    n
# AUDIO_FORMAT_AMR                         y                    n
# AUDIO_FORMAT_MP3                         y                    n
# AUDIO_FORMAT_OPUS                        y                    n
# AUDIO_FORMAT_PCM                         y                    n
# AUDIO_FORMAT_SBC                         y                    n
# AUDIO_NUM_BUFFERS                        2                    <unset>
# BINFMT_LOADABLE                          y                    n
# ELF                                      y                    n
# ELF_STACKSIZE                            8192                 <unset>
# LIBC_ARCH_ELF                            y                    n
# LIBC_MODLIB                              y                    n
# MODLIB_ALIGN_LOG2                        2                    <unset>
# MODLIB_BUFFERINCR                        32                   <unset>
# MODLIB_BUFFERSIZE                        32                   <unset>
# MODLIB_MAXDEPEND                         2                    <unset>
# MODLIB_RELOCATION_BUFFERCOUNT            256                  <unset>
# MODLIB_SYMBOL_CACHECOUNT                 256                  <unset>
# NXPLAYER_COMMAND_LINE                    y                    n
# NXPLAYER_DEFAULT_MEDIADIR                /music               <unset>
# NXPLAYER_FMT_FROM_EXT                    y                    n
# NXPLAYER_INCLUDE_DEVICE_SEARCH           y                    n
# NXPLAYER_INCLUDE_HELP                    y                    n
# NXPLAYER_INCLUDE_MEDIADIR                y                    n
# NXPLAYER_INCLUDE_PREFERRED_DEVICE        y                    n
# NXPLAYER_MAINTHREAD_STACKSIZE            8192                 <unset>
# NXPLAYER_PLAYTHREAD_STACKSIZE            8192                 <unset>
# NXRECORDER_COMMAND_LINE                  y                    n
# NXRECORDER_FMT_FROM_EXT                  y                    n
# NXRECORDER_INCLUDE_HELP                  y                    n
# NXRECORDER_MAINTHREAD_STACKSIZE          8192                 <unset>
# NXRECORDER_RECORDTHREAD_STACKSIZE        8192                 <unset>
# SYSTEM_NXPLAYER                          y                    n
# SYSTEM_NXRECORDER                        y                    n
#
#
# RECAUTION:
# Because NuttX apps Kconfig of menu is generated by build system,
# and arch/board bridge Kconfig is symlink to real arch board dir.
# So it is best to check the defconfig that has been configured.
# If the environment does not generate Kconfig menu, etc.
# the tool will execute `configure.sh` and distclean at the end.
#

import argparse
import os
import re
import subprocess
import sys
from pathlib import Path

try:
    from kconfiglib import Kconfig
except ImportError:
    print(
        "ERROR: checkkconfig tool depends on kconfiglib for parse Kconfig tree.\nplease install it.\npip install kconfiglib",
        file=sys.stderr,
    )
    sys.exit(1)

TOPDIR = Path(__file__).resolve().parent.parent
DPATH = Path("defconfig")
NEED_RESET = False


# Prepare environment for Kconfig
def prepare_env():
    global NEED_RESET
    # check if we are in the configured Kconfig environment
    full_config_file = TOPDIR / Path(".config")
    if not full_config_file.exists():
        print("apps preconfig do not generate yet \nrun configure.sh first")
        result = subprocess.run(
            [f"{TOPDIR}/tools/configure.sh", "-e", f"{str(DPATH.parent)}"],
            capture_output=True,
            text=True,
        )
        if result.returncode != 0:
            print(
                f"ERROR: {TOPDIR}/tools/configure.sh run fail\n configure path: {str(DPATH.parent)}",
                file=sys.stderr,
            )
            print(f"STDERROR:{result.stderr}", file=sys.stderr)
            sys.exit(1)
        NEED_RESET = True
    os.environ["APPSDIR"] = "../apps"
    os.environ["APPSBINDIR"] = "../apps"
    os.environ["EXTERNALDIR"] = "dummy"
    os.environ["BINDIR"] = str(TOPDIR)
    os.environ["KCONFIG_CONFIG"] = str(DPATH)


# Reset environment to previous
def reset_env():
    os.environ.pop("APPSDIR", None)
    os.environ.pop("APPSBINDIR", None)
    os.environ.pop("EXTERNALDIR", None)
    os.environ.pop("BINDIR", None)
    os.environ.pop("KCONFIG_CONFIG", None)
    if NEED_RESET:
        result = subprocess.run(
            ["make", "distclean"], cwd=TOPDIR, capture_output=True, text=True
        )
        print(result.stdout)
        if result.returncode != 0:
            print(
                "ERROR: distclean error please clean up your workspace manually",
                file=sys.stderr,
            )
            print(f"STDERROR:{result.stderr}", file=sys.stderr)


# Parse a diff file and return a dict of changes
def parse_diff(diff_path):
    changes = {}
    diff_pattern = re.compile(r"^([+-])CONFIG_(\w+)=([ymn])$")
    with open(diff_path, "r") as f:
        for line in f:
            line = line.strip()

            match = diff_pattern.match(line)
            if not match:
                continue

            op, name, value = match.groups()
            full_name = f"{name}"

            if op == "-":
                changes[full_name] = "n"
            elif op == "+":
                changes[full_name] = value.lower()
    return changes


# Apply a set of changes to a Kconfig tree and return a list of changed symbols
def apply_changes(kconf, changes):

    # step 1: keep track of original values
    orig_state = {sym.name: sym.str_value for sym in kconf.defined_syms}

    # step 2: apply the config settings
    value_map = {"n": 0, "m": 1, "y": 2}
    for target, value in changes.items():
        sym = kconf.syms.get(target)
        if not sym:
            print(f"Warning: {target} not found, skipped")
            continue
        if value not in value_map:
            print(f"Invalid value {value} for {target}, skipped")
            continue
        sym.set_value(value_map[value])

    # step 3: check for changes
    changed = []
    for sym in kconf.defined_syms:
        orig = orig_state.get(sym.name, "")
        curr = sym.str_value
        if orig != curr:
            changed.append((sym.name, orig, curr))

    return changed


def track_single_change(target, value):
    kconf = Kconfig()
    kconf.load_config()
    return apply_changes(kconf, {target: value})


def track_diff_changes(diff_path):
    kconf = Kconfig()
    kconf.load_config()
    changes = parse_diff(diff_path)
    return apply_changes(kconf, changes)


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "-f", "--file", required=True, help="Path to the input defconfig file"
    )
    group = parser.add_mutually_exclusive_group(required=True)
    group.add_argument(
        "-s",
        "--single",
        nargs=2,
        metavar=("CONFIG", "VALUE"),
        help="Analyze single change: CONFIG_NAME y/m/n",
    )
    group.add_argument("-d", "--diff", help="Analyze changes from diff file")

    args = parser.parse_args()

    DPATH = Path(args.file)

    if not DPATH.is_absolute():
        DPATH = TOPDIR / DPATH

    if not DPATH.exists:
        print("ERROR: defconfig file DO NOT exist", file=sys.stderr)
        sys.exit(1)

    prepare_env()

    if args.single:
        target, value = args.single
        changes = track_single_change(target, value.lower())
        title = f"Change report for {target}={value}"
    elif args.diff:
        changes = track_diff_changes(args.diff)
        title = f"Change report for diff: {args.diff}"

    reset_env()

    print(f"\n{title}")
    print(f"{'Config Option':<40} {'Old':<20} {'New':<20}")
    print("-" * 70)
    for name, old, new in sorted(changes, key=lambda x: x[0]):
        print(f"{name:<40} {old or '<unset>':<20} {new or '<unset>':<20}")