xref: /optee_os/scripts/kconfig/kconfiglib/setconfig.py (revision c689edbb2550c76ae81dcecab717d82a564b2d7b)
1#!/usr/bin/env python3
2
3# Copyright (c) 2019, Ulf Magnusson
4# SPDX-License-Identifier: ISC
5
6"""
7Simple utility for setting configuration values from the command line.
8
9Sample usage:
10
11  $ setconfig FOO_SUPPORT=y BAR_BITS=8
12
13Note: Symbol names should not be prefixed with 'CONFIG_'.
14
15The exit status on errors is 1.
16
17The default input/output configuration file is '.config'. A different filename
18can be passed in the KCONFIG_CONFIG environment variable.
19
20When overwriting a configuration file, the old version is saved to
21<filename>.old (e.g. .config.old).
22"""
23import argparse
24import sys
25
26import kconfiglib
27
28
29def main():
30    parser = argparse.ArgumentParser(
31        formatter_class=argparse.RawDescriptionHelpFormatter,
32        description=__doc__)
33
34    parser.add_argument(
35        "--kconfig",
36        default="Kconfig",
37        help="Top-level Kconfig file (default: Kconfig)")
38
39    parser.add_argument(
40        "--no-check-exists",
41        dest="check_exists",
42        action="store_false",
43        help="Ignore assignments to non-existent symbols instead of erroring "
44             "out")
45
46    parser.add_argument(
47        "--no-check-value",
48        dest="check_value",
49        action="store_false",
50        help="Ignore assignments that didn't \"take\" (where the symbol got a "
51             "different value, e.g. due to unsatisfied dependencies) instead "
52             "of erroring out")
53
54    parser.add_argument(
55        "assignments",
56        metavar="ASSIGNMENT",
57        nargs="*",
58        help="A 'NAME=value' assignment")
59
60    args = parser.parse_args()
61
62    kconf = kconfiglib.Kconfig(args.kconfig, suppress_traceback=True)
63    print(kconf.load_config())
64
65    for arg in args.assignments:
66        if "=" not in arg:
67            sys.exit("error: no '=' in assignment: '{}'".format(arg))
68        name, value = arg.split("=", 1)
69
70        if name not in kconf.syms:
71            if not args.check_exists:
72                continue
73            sys.exit("error: no symbol '{}' in configuration".format(name))
74
75        sym = kconf.syms[name]
76
77        if not sym.set_value(value):
78            sys.exit("error: '{}' is an invalid value for the {} symbol {}"
79                     .format(value, kconfiglib.TYPE_TO_STR[sym.orig_type],
80                             name))
81
82        if args.check_value and sym.str_value != value:
83            sys.exit("error: {} was assigned the value '{}', but got the "
84                     "value '{}'. Check the symbol's dependencies, and make "
85                     "sure that it has a prompt."
86                     .format(name, value, sym.str_value))
87
88    print(kconf.write_config())
89
90
91if __name__ == "__main__":
92    main()
93