2018-01-12 02:50:19 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# Command line tool to take in ESP-IDF sdkconfig files with project
|
|
|
|
# settings and output data in multiple formats (update config, generate
|
|
|
|
# header file, generate .cmake include file, documentation, etc).
|
|
|
|
#
|
|
|
|
# Used internally by the ESP-IDF build system. But designed to be
|
|
|
|
# non-IDF-specific.
|
|
|
|
#
|
2021-11-04 14:28:07 +00:00
|
|
|
# SPDX-FileCopyrightText: 2018-2021 Espressif Systems (Shanghai) CO LTD
|
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
2021-01-26 02:49:01 +00:00
|
|
|
|
2018-01-12 02:50:19 +00:00
|
|
|
import argparse
|
2019-03-14 08:54:04 +00:00
|
|
|
import json
|
2018-01-12 02:50:19 +00:00
|
|
|
import os
|
|
|
|
import os.path
|
2019-01-25 04:16:51 +00:00
|
|
|
import re
|
2019-03-14 08:54:04 +00:00
|
|
|
import sys
|
|
|
|
import tempfile
|
2021-12-14 14:31:08 +00:00
|
|
|
import textwrap
|
2022-01-31 02:56:14 +00:00
|
|
|
from collections import defaultdict
|
2018-01-12 02:50:19 +00:00
|
|
|
|
|
|
|
import gen_kconfig_doc
|
2021-01-26 02:49:01 +00:00
|
|
|
import kconfiglib
|
|
|
|
from future.utils import iteritems
|
2019-09-11 09:18:18 +00:00
|
|
|
|
2021-01-26 02:49:01 +00:00
|
|
|
__version__ = '0.1'
|
2018-01-12 02:50:19 +00:00
|
|
|
|
2018-12-04 12:46:48 +00:00
|
|
|
|
2019-03-14 08:54:04 +00:00
|
|
|
class DeprecatedOptions(object):
|
|
|
|
_REN_FILE = 'sdkconfig.rename'
|
|
|
|
_DEP_OP_BEGIN = '# Deprecated options for backward compatibility'
|
|
|
|
_DEP_OP_END = '# End of deprecated options'
|
|
|
|
_RE_DEP_OP_BEGIN = re.compile(_DEP_OP_BEGIN)
|
|
|
|
_RE_DEP_OP_END = re.compile(_DEP_OP_END)
|
|
|
|
|
2019-07-16 14:39:12 +00:00
|
|
|
def __init__(self, config_prefix, path_rename_files=[]):
|
2019-03-14 08:54:04 +00:00
|
|
|
self.config_prefix = config_prefix
|
|
|
|
# r_dic maps deprecated options to new options; rev_r_dic maps in the opposite direction
|
2019-07-16 14:39:12 +00:00
|
|
|
self.r_dic, self.rev_r_dic = self._parse_replacements(path_rename_files)
|
2019-03-14 08:54:04 +00:00
|
|
|
|
|
|
|
# note the '=' at the end of regex for not getting partial match of configs
|
|
|
|
self._RE_CONFIG = re.compile(r'{}(\w+)='.format(self.config_prefix))
|
|
|
|
|
2019-07-16 14:39:12 +00:00
|
|
|
def _parse_replacements(self, repl_paths):
|
2019-03-14 08:54:04 +00:00
|
|
|
rep_dic = {}
|
2022-01-31 02:56:14 +00:00
|
|
|
rev_rep_dic = defaultdict(list)
|
2019-03-14 08:54:04 +00:00
|
|
|
|
2019-04-26 10:50:20 +00:00
|
|
|
def remove_config_prefix(string):
|
|
|
|
if string.startswith(self.config_prefix):
|
|
|
|
return string[len(self.config_prefix):]
|
|
|
|
raise RuntimeError('Error in {} (line {}): Config {} is not prefixed with {}'
|
|
|
|
''.format(rep_path, line_number, string, self.config_prefix))
|
|
|
|
|
2019-07-16 14:39:12 +00:00
|
|
|
for rep_path in repl_paths:
|
|
|
|
with open(rep_path) as f_rep:
|
|
|
|
for line_number, line in enumerate(f_rep, start=1):
|
|
|
|
sp_line = line.split()
|
|
|
|
if len(sp_line) == 0 or sp_line[0].startswith('#'):
|
|
|
|
# empty line or comment
|
|
|
|
continue
|
|
|
|
if len(sp_line) != 2 or not all(x.startswith(self.config_prefix) for x in sp_line):
|
|
|
|
raise RuntimeError('Syntax error in {} (line {})'.format(rep_path, line_number))
|
|
|
|
if sp_line[0] in rep_dic:
|
|
|
|
raise RuntimeError('Error in {} (line {}): Replacement {} exist for {} and new '
|
|
|
|
'replacement {} is defined'.format(rep_path, line_number,
|
|
|
|
rep_dic[sp_line[0]], sp_line[0],
|
|
|
|
sp_line[1]))
|
|
|
|
|
|
|
|
(dep_opt, new_opt) = (remove_config_prefix(x) for x in sp_line)
|
|
|
|
rep_dic[dep_opt] = new_opt
|
2022-01-31 02:56:14 +00:00
|
|
|
rev_rep_dic[new_opt].append(dep_opt)
|
2019-03-14 08:54:04 +00:00
|
|
|
return rep_dic, rev_rep_dic
|
|
|
|
|
|
|
|
def get_deprecated_option(self, new_option):
|
2022-01-31 02:56:14 +00:00
|
|
|
return self.rev_r_dic.get(new_option, [])
|
2019-03-14 08:54:04 +00:00
|
|
|
|
|
|
|
def get_new_option(self, deprecated_option):
|
|
|
|
return self.r_dic.get(deprecated_option, None)
|
|
|
|
|
|
|
|
def replace(self, sdkconfig_in, sdkconfig_out):
|
|
|
|
replace_enabled = True
|
|
|
|
with open(sdkconfig_in, 'r') as f_in, open(sdkconfig_out, 'w') as f_out:
|
|
|
|
for line_num, line in enumerate(f_in, start=1):
|
|
|
|
if self._RE_DEP_OP_BEGIN.search(line):
|
|
|
|
replace_enabled = False
|
|
|
|
elif self._RE_DEP_OP_END.search(line):
|
|
|
|
replace_enabled = True
|
|
|
|
elif replace_enabled:
|
|
|
|
m = self._RE_CONFIG.search(line)
|
|
|
|
if m and m.group(1) in self.r_dic:
|
|
|
|
depr_opt = self.config_prefix + m.group(1)
|
|
|
|
new_opt = self.config_prefix + self.r_dic[m.group(1)]
|
|
|
|
line = line.replace(depr_opt, new_opt)
|
|
|
|
print('{}:{} {} was replaced with {}'.format(sdkconfig_in, line_num, depr_opt, new_opt))
|
|
|
|
f_out.write(line)
|
|
|
|
|
2019-12-12 15:02:36 +00:00
|
|
|
def append_doc(self, config, visibility, path_output):
|
2019-04-26 13:43:40 +00:00
|
|
|
|
|
|
|
def option_was_written(opt):
|
2020-01-31 07:48:56 +00:00
|
|
|
# named choices were written if any of the symbols in the choice were visible
|
|
|
|
if new_opt in config.named_choices:
|
|
|
|
syms = config.named_choices[new_opt].syms
|
|
|
|
for s in syms:
|
|
|
|
if any(visibility.visible(node) for node in s.nodes):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
else:
|
2021-03-30 07:24:40 +00:00
|
|
|
try:
|
|
|
|
# otherwise if any of the nodes associated with the option was visible
|
|
|
|
return any(visibility.visible(node) for node in config.syms[opt].nodes)
|
|
|
|
except KeyError:
|
|
|
|
return False
|
2019-04-26 13:43:40 +00:00
|
|
|
|
2019-03-14 08:54:04 +00:00
|
|
|
if len(self.r_dic) > 0:
|
|
|
|
with open(path_output, 'a') as f_o:
|
|
|
|
header = 'Deprecated options and their replacements'
|
2019-05-22 11:01:19 +00:00
|
|
|
f_o.write('.. _configuration-deprecated-options:\n\n{}\n{}\n\n'.format(header, '-' * len(header)))
|
2019-04-26 13:43:40 +00:00
|
|
|
for dep_opt in sorted(self.r_dic):
|
|
|
|
new_opt = self.r_dic[dep_opt]
|
2020-01-31 07:48:56 +00:00
|
|
|
if option_was_written(new_opt) and (new_opt not in config.syms or config.syms[new_opt].choice is None):
|
2019-04-26 13:43:40 +00:00
|
|
|
# everything except config for a choice (no link reference for those in the docs)
|
|
|
|
f_o.write('- {}{} (:ref:`{}{}`)\n'.format(config.config_prefix, dep_opt,
|
|
|
|
config.config_prefix, new_opt))
|
|
|
|
|
|
|
|
if new_opt in config.named_choices:
|
|
|
|
# here are printed config options which were filtered out
|
|
|
|
syms = config.named_choices[new_opt].syms
|
|
|
|
for sym in syms:
|
|
|
|
if sym.name in self.rev_r_dic:
|
|
|
|
# only if the symbol has been renamed
|
2022-01-31 02:56:14 +00:00
|
|
|
dep_names = self.rev_r_dic[sym.name]
|
|
|
|
dep_names = [config.config_prefix + name for name in dep_names]
|
2019-04-26 13:43:40 +00:00
|
|
|
# config options doesn't have references
|
2022-01-31 02:56:14 +00:00
|
|
|
f_o.write(' - {}\n'.format(', '.join(dep_names)))
|
2019-03-14 08:54:04 +00:00
|
|
|
|
|
|
|
def append_config(self, config, path_output):
|
|
|
|
tmp_list = []
|
|
|
|
|
|
|
|
def append_config_node_process(node):
|
|
|
|
item = node.item
|
|
|
|
if isinstance(item, kconfiglib.Symbol) and item.env_var is None:
|
|
|
|
if item.name in self.rev_r_dic:
|
|
|
|
c_string = item.config_string
|
|
|
|
if c_string:
|
2022-01-31 02:56:14 +00:00
|
|
|
for dep_name in self.rev_r_dic[item.name]:
|
|
|
|
tmp_list.append(c_string.replace(self.config_prefix + item.name,
|
|
|
|
self.config_prefix + dep_name))
|
2019-03-14 08:54:04 +00:00
|
|
|
|
2019-09-10 07:58:52 +00:00
|
|
|
for n in config.node_iter():
|
|
|
|
append_config_node_process(n)
|
2019-03-14 08:54:04 +00:00
|
|
|
|
|
|
|
if len(tmp_list) > 0:
|
|
|
|
with open(path_output, 'a') as f_o:
|
|
|
|
f_o.write('\n{}\n'.format(self._DEP_OP_BEGIN))
|
|
|
|
f_o.writelines(tmp_list)
|
|
|
|
f_o.write('{}\n'.format(self._DEP_OP_END))
|
|
|
|
|
|
|
|
def append_header(self, config, path_output):
|
2019-06-07 11:57:47 +00:00
|
|
|
def _opt_defined(opt):
|
2022-03-02 10:01:08 +00:00
|
|
|
if opt.orig_type in (kconfiglib.BOOL, kconfiglib.TRISTATE) and opt.str_value != 'n':
|
|
|
|
opt_defined = True
|
|
|
|
elif opt.orig_type in (kconfiglib.INT, kconfiglib.STRING, kconfiglib.HEX) and opt.str_value != '':
|
|
|
|
opt_defined = True
|
|
|
|
else:
|
|
|
|
opt_defined = False
|
|
|
|
return opt_defined
|
2019-06-07 11:57:47 +00:00
|
|
|
|
2019-03-14 08:54:04 +00:00
|
|
|
if len(self.r_dic) > 0:
|
|
|
|
with open(path_output, 'a') as f_o:
|
|
|
|
f_o.write('\n/* List of deprecated options */\n')
|
|
|
|
for dep_opt in sorted(self.r_dic):
|
|
|
|
new_opt = self.r_dic[dep_opt]
|
2019-06-07 11:57:47 +00:00
|
|
|
if new_opt in config.syms and _opt_defined(config.syms[new_opt]):
|
|
|
|
f_o.write('#define {}{} {}{}\n'.format(self.config_prefix, dep_opt, self.config_prefix, new_opt))
|
2019-03-14 08:54:04 +00:00
|
|
|
|
|
|
|
|
2019-09-10 07:58:52 +00:00
|
|
|
def dict_enc_for_env(dic, encoding=sys.getfilesystemencoding() or 'utf-8'):
|
|
|
|
"""
|
|
|
|
This function can be deleted after dropping support for Python 2.
|
|
|
|
There is no rule for it that environment variables cannot be Unicode but usually people try to avoid it.
|
|
|
|
The upstream kconfiglib cannot detect strings properly if the environment variables are "unicode". This is problem
|
|
|
|
only in Python 2.
|
|
|
|
"""
|
|
|
|
if sys.version_info[0] >= 3:
|
|
|
|
return dic
|
|
|
|
ret = dict()
|
|
|
|
for (key, value) in iteritems(dic):
|
|
|
|
ret[key.encode(encoding)] = value.encode(encoding)
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
2018-01-12 02:50:19 +00:00
|
|
|
def main():
|
2018-01-22 03:32:02 +00:00
|
|
|
parser = argparse.ArgumentParser(description='confgen.py v%s - Config Generation Tool' % __version__, prog=os.path.basename(sys.argv[0]))
|
2018-01-12 02:50:19 +00:00
|
|
|
|
|
|
|
parser.add_argument('--config',
|
|
|
|
help='Project configuration settings',
|
|
|
|
nargs='?',
|
|
|
|
default=None)
|
|
|
|
|
2018-01-22 03:32:02 +00:00
|
|
|
parser.add_argument('--defaults',
|
2018-12-04 12:46:48 +00:00
|
|
|
help='Optional project defaults file, used if --config file doesn\'t exist. '
|
|
|
|
'Multiple files can be specified using multiple --defaults arguments.',
|
2018-01-22 03:32:02 +00:00
|
|
|
nargs='?',
|
2018-10-19 03:53:57 +00:00
|
|
|
default=[],
|
|
|
|
action='append')
|
2018-01-22 03:32:02 +00:00
|
|
|
|
2018-01-12 02:50:19 +00:00
|
|
|
parser.add_argument('--kconfig',
|
|
|
|
help='KConfig file with config item definitions',
|
|
|
|
required=True)
|
|
|
|
|
2019-07-16 14:39:12 +00:00
|
|
|
parser.add_argument('--sdkconfig-rename',
|
|
|
|
help='File with deprecated Kconfig options',
|
|
|
|
required=False)
|
|
|
|
|
2019-11-08 13:40:28 +00:00
|
|
|
parser.add_argument('--dont-write-deprecated',
|
|
|
|
help='Do not write compatibility statements for deprecated values',
|
|
|
|
action='store_true')
|
|
|
|
|
2018-01-12 02:50:19 +00:00
|
|
|
parser.add_argument('--output', nargs=2, action='append',
|
|
|
|
help='Write output file (format and output filename)',
|
2018-01-14 22:55:00 +00:00
|
|
|
metavar=('FORMAT', 'FILENAME'),
|
|
|
|
default=[])
|
2018-01-12 02:50:19 +00:00
|
|
|
|
|
|
|
parser.add_argument('--env', action='append', default=[],
|
|
|
|
help='Environment to set when evaluating the config file', metavar='NAME=VAL')
|
|
|
|
|
2019-06-26 05:56:47 +00:00
|
|
|
parser.add_argument('--env-file', type=argparse.FileType('r'),
|
|
|
|
help='Optional file to load environment variables from. Contents '
|
|
|
|
'should be a JSON object where each key/value pair is a variable.')
|
|
|
|
|
2020-10-05 22:13:55 +00:00
|
|
|
parser.add_argument('--list-separator', choices=['space', 'semicolon'],
|
|
|
|
default='space',
|
|
|
|
help='Separator used in environment list variables (COMPONENT_SDKCONFIG_RENAMES)')
|
|
|
|
|
2018-01-12 02:50:19 +00:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
for fmt, filename in args.output:
|
2018-12-04 12:46:48 +00:00
|
|
|
if fmt not in OUTPUT_FORMATS.keys():
|
2018-05-16 06:54:22 +00:00
|
|
|
print("Format '%s' not recognised. Known formats: %s" % (fmt, OUTPUT_FORMATS.keys()))
|
2018-01-12 02:50:19 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
try:
|
2021-01-26 02:49:01 +00:00
|
|
|
args.env = [(name,value) for (name,value) in (e.split('=',1) for e in args.env)]
|
2018-01-12 02:50:19 +00:00
|
|
|
except ValueError:
|
2018-12-04 12:46:48 +00:00
|
|
|
print("--env arguments must each contain =. To unset an environment variable, use 'ENV='")
|
|
|
|
sys.exit(1)
|
2018-01-12 02:50:19 +00:00
|
|
|
|
|
|
|
for name, value in args.env:
|
|
|
|
os.environ[name] = value
|
|
|
|
|
2019-06-26 05:56:47 +00:00
|
|
|
if args.env_file is not None:
|
|
|
|
env = json.load(args.env_file)
|
2019-09-10 07:58:52 +00:00
|
|
|
os.environ.update(dict_enc_for_env(env))
|
2019-06-26 05:56:47 +00:00
|
|
|
|
2018-01-12 02:50:19 +00:00
|
|
|
config = kconfiglib.Kconfig(args.kconfig)
|
2019-09-10 07:58:52 +00:00
|
|
|
config.warn_assign_redun = False
|
|
|
|
config.warn_assign_override = False
|
|
|
|
|
2020-10-05 22:13:55 +00:00
|
|
|
sdkconfig_renames_sep = ';' if args.list_separator == 'semicolon' else ' '
|
|
|
|
|
2019-09-10 07:58:52 +00:00
|
|
|
sdkconfig_renames = [args.sdkconfig_rename] if args.sdkconfig_rename else []
|
2020-10-05 22:13:55 +00:00
|
|
|
sdkconfig_renames_from_env = os.environ.get('COMPONENT_SDKCONFIG_RENAMES')
|
|
|
|
if sdkconfig_renames_from_env:
|
|
|
|
sdkconfig_renames += sdkconfig_renames_from_env.split(sdkconfig_renames_sep)
|
2019-09-10 07:58:52 +00:00
|
|
|
deprecated_options = DeprecatedOptions(config.config_prefix, path_rename_files=sdkconfig_renames)
|
2018-01-12 02:50:19 +00:00
|
|
|
|
2018-10-19 03:53:57 +00:00
|
|
|
if len(args.defaults) > 0:
|
2019-09-10 07:58:52 +00:00
|
|
|
def _replace_empty_assignments(path_in, path_out):
|
|
|
|
with open(path_in, 'r') as f_in, open(path_out, 'w') as f_out:
|
|
|
|
for line_num, line in enumerate(f_in, start=1):
|
|
|
|
line = line.strip()
|
|
|
|
if line.endswith('='):
|
|
|
|
line += 'n'
|
|
|
|
print('{}:{} line was updated to {}'.format(path_out, line_num, line))
|
|
|
|
f_out.write(line)
|
|
|
|
f_out.write('\n')
|
|
|
|
|
2018-01-22 03:32:02 +00:00
|
|
|
# always load defaults first, so any items which are not defined in that config
|
|
|
|
# will have the default defined in the defaults file
|
2018-10-19 03:53:57 +00:00
|
|
|
for name in args.defaults:
|
2021-01-26 02:49:01 +00:00
|
|
|
print('Loading defaults file %s...' % name)
|
2018-10-19 03:53:57 +00:00
|
|
|
if not os.path.exists(name):
|
2021-01-26 02:49:01 +00:00
|
|
|
raise RuntimeError('Defaults file not found: %s' % name)
|
2019-09-25 13:58:02 +00:00
|
|
|
try:
|
2021-01-26 02:49:01 +00:00
|
|
|
with tempfile.NamedTemporaryFile(prefix='confgen_tmp', delete=False) as f:
|
2019-09-10 07:58:52 +00:00
|
|
|
temp_file1 = f.name
|
2021-01-26 02:49:01 +00:00
|
|
|
with tempfile.NamedTemporaryFile(prefix='confgen_tmp', delete=False) as f:
|
2019-09-10 07:58:52 +00:00
|
|
|
temp_file2 = f.name
|
|
|
|
deprecated_options.replace(sdkconfig_in=name, sdkconfig_out=temp_file1)
|
|
|
|
_replace_empty_assignments(temp_file1, temp_file2)
|
|
|
|
config.load_config(temp_file2, replace=False)
|
2019-09-25 13:58:02 +00:00
|
|
|
finally:
|
|
|
|
try:
|
2019-09-10 07:58:52 +00:00
|
|
|
os.remove(temp_file1)
|
|
|
|
os.remove(temp_file2)
|
2019-09-25 13:58:02 +00:00
|
|
|
except OSError:
|
|
|
|
pass
|
2019-03-14 08:54:04 +00:00
|
|
|
|
2019-01-15 15:06:50 +00:00
|
|
|
# If config file previously exists, load it
|
|
|
|
if args.config and os.path.exists(args.config):
|
2019-03-14 08:54:04 +00:00
|
|
|
# ... but replace deprecated options before that
|
2021-01-26 02:49:01 +00:00
|
|
|
with tempfile.NamedTemporaryFile(prefix='confgen_tmp', delete=False) as f:
|
2019-03-14 08:54:04 +00:00
|
|
|
temp_file = f.name
|
|
|
|
try:
|
|
|
|
deprecated_options.replace(sdkconfig_in=args.config, sdkconfig_out=temp_file)
|
|
|
|
config.load_config(temp_file, replace=False)
|
|
|
|
update_if_changed(temp_file, args.config)
|
|
|
|
finally:
|
|
|
|
try:
|
|
|
|
os.remove(temp_file)
|
|
|
|
except OSError:
|
|
|
|
pass
|
2019-01-15 15:06:50 +00:00
|
|
|
|
2019-11-08 13:40:28 +00:00
|
|
|
if args.dont_write_deprecated:
|
|
|
|
# The deprecated object was useful until now for replacements. Now it will be redefined with no configurations
|
|
|
|
# and as the consequence, it won't generate output with deprecated statements.
|
|
|
|
deprecated_options = DeprecatedOptions('', path_rename_files=[])
|
|
|
|
|
2019-01-15 15:06:50 +00:00
|
|
|
# Output the files specified in the arguments
|
|
|
|
for output_type, filename in args.output:
|
2021-01-26 02:49:01 +00:00
|
|
|
with tempfile.NamedTemporaryFile(prefix='confgen_tmp', delete=False) as f:
|
2019-03-14 08:54:04 +00:00
|
|
|
temp_file = f.name
|
2019-01-15 15:06:50 +00:00
|
|
|
try:
|
|
|
|
output_function = OUTPUT_FORMATS[output_type]
|
2019-03-14 08:54:04 +00:00
|
|
|
output_function(deprecated_options, config, temp_file)
|
2019-01-15 15:06:50 +00:00
|
|
|
update_if_changed(temp_file, filename)
|
|
|
|
finally:
|
2018-01-12 02:50:19 +00:00
|
|
|
try:
|
2019-01-15 15:06:50 +00:00
|
|
|
os.remove(temp_file)
|
|
|
|
except OSError:
|
|
|
|
pass
|
2018-01-12 02:50:19 +00:00
|
|
|
|
|
|
|
|
2019-03-14 08:54:04 +00:00
|
|
|
def write_config(deprecated_options, config, filename):
|
2018-01-12 02:50:19 +00:00
|
|
|
CONFIG_HEADING = """#
|
|
|
|
# Automatically generated file. DO NOT EDIT.
|
|
|
|
# Espressif IoT Development Framework (ESP-IDF) Project Configuration
|
|
|
|
#
|
|
|
|
"""
|
|
|
|
config.write_config(filename, header=CONFIG_HEADING)
|
2019-03-14 08:54:04 +00:00
|
|
|
deprecated_options.append_config(config, filename)
|
2018-01-12 02:50:19 +00:00
|
|
|
|
2018-12-04 12:46:48 +00:00
|
|
|
|
2021-12-14 14:31:08 +00:00
|
|
|
def write_min_config(deprecated_options, config, filename):
|
|
|
|
target_symbol = config.syms['IDF_TARGET']
|
|
|
|
# 'esp32` is harcoded here because the default value of IDF_TARGET is set on the first run from the environment
|
|
|
|
# variable. I.E. `esp32 is not defined as default value.
|
|
|
|
write_target = target_symbol.str_value != 'esp32'
|
|
|
|
|
|
|
|
CONFIG_HEADING = textwrap.dedent('''\
|
|
|
|
# This file was generated using idf.py save-defconfig. It can be edited manually.
|
|
|
|
# Espressif IoT Development Framework (ESP-IDF) Project Minimal Configuration
|
|
|
|
#
|
|
|
|
{}\
|
|
|
|
'''.format(target_symbol.config_string if write_target else ''))
|
|
|
|
config.write_min_config(filename, header=CONFIG_HEADING)
|
|
|
|
|
|
|
|
|
2019-03-14 08:54:04 +00:00
|
|
|
def write_header(deprecated_options, config, filename):
|
2018-01-12 02:50:19 +00:00
|
|
|
CONFIG_HEADING = """/*
|
|
|
|
* Automatically generated file. DO NOT EDIT.
|
|
|
|
* Espressif IoT Development Framework (ESP-IDF) Configuration Header
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
"""
|
|
|
|
config.write_autoconf(filename, header=CONFIG_HEADING)
|
2019-03-14 08:54:04 +00:00
|
|
|
deprecated_options.append_header(config, filename)
|
2018-01-12 02:50:19 +00:00
|
|
|
|
2018-12-04 12:46:48 +00:00
|
|
|
|
2019-03-14 08:54:04 +00:00
|
|
|
def write_cmake(deprecated_options, config, filename):
|
2021-01-26 02:49:01 +00:00
|
|
|
with open(filename, 'w') as f:
|
2019-03-14 08:54:04 +00:00
|
|
|
tmp_dep_list = []
|
2018-01-12 02:50:19 +00:00
|
|
|
write = f.write
|
|
|
|
prefix = config.config_prefix
|
|
|
|
|
|
|
|
write("""#
|
|
|
|
# Automatically generated file. DO NOT EDIT.
|
|
|
|
# Espressif IoT Development Framework (ESP-IDF) Configuration cmake include file
|
|
|
|
#
|
|
|
|
""")
|
2018-12-04 12:46:48 +00:00
|
|
|
|
2019-04-26 05:42:10 +00:00
|
|
|
configs_list = list()
|
|
|
|
|
2018-01-12 02:50:19 +00:00
|
|
|
def write_node(node):
|
|
|
|
sym = node.item
|
|
|
|
if not isinstance(sym, kconfiglib.Symbol):
|
|
|
|
return
|
|
|
|
|
2019-07-01 15:03:55 +00:00
|
|
|
if sym.config_string:
|
|
|
|
val = sym.str_value
|
2021-01-26 02:49:01 +00:00
|
|
|
if sym.orig_type in (kconfiglib.BOOL, kconfiglib.TRISTATE) and val == 'n':
|
|
|
|
val = '' # write unset values as empty variables
|
2020-02-12 13:25:15 +00:00
|
|
|
elif sym.orig_type == kconfiglib.STRING:
|
|
|
|
val = kconfiglib.escape(val)
|
2020-04-16 03:59:16 +00:00
|
|
|
elif sym.orig_type == kconfiglib.HEX:
|
|
|
|
val = hex(int(val, 16)) # ensure 0x prefix
|
|
|
|
write('set({}{} "{}")\n'.format(prefix, sym.name, val))
|
2019-04-26 05:42:10 +00:00
|
|
|
|
|
|
|
configs_list.append(prefix + sym.name)
|
2022-01-31 02:56:14 +00:00
|
|
|
dep_opts = deprecated_options.get_deprecated_option(sym.name)
|
|
|
|
for opt in dep_opts:
|
|
|
|
tmp_dep_list.append('set({}{} "{}")\n'.format(prefix, opt, val))
|
|
|
|
configs_list.append(prefix + opt)
|
2019-04-26 05:42:10 +00:00
|
|
|
|
2019-09-10 07:58:52 +00:00
|
|
|
for n in config.node_iter():
|
|
|
|
write_node(n)
|
2021-01-26 02:49:01 +00:00
|
|
|
write('set(CONFIGS_LIST {})'.format(';'.join(configs_list)))
|
2018-01-12 02:50:19 +00:00
|
|
|
|
2019-03-14 08:54:04 +00:00
|
|
|
if len(tmp_dep_list) > 0:
|
|
|
|
write('\n# List of deprecated options for backward compatibility\n')
|
|
|
|
f.writelines(tmp_dep_list)
|
|
|
|
|
2018-12-04 12:46:48 +00:00
|
|
|
|
2018-05-16 06:54:22 +00:00
|
|
|
def get_json_values(config):
|
2018-01-23 06:08:28 +00:00
|
|
|
config_dict = {}
|
2018-12-04 12:46:48 +00:00
|
|
|
|
2018-01-23 06:08:28 +00:00
|
|
|
def write_node(node):
|
|
|
|
sym = node.item
|
|
|
|
if not isinstance(sym, kconfiglib.Symbol):
|
|
|
|
return
|
|
|
|
|
2019-07-01 15:03:55 +00:00
|
|
|
if sym.config_string:
|
|
|
|
val = sym.str_value
|
2018-12-04 12:46:48 +00:00
|
|
|
if sym.type in [kconfiglib.BOOL, kconfiglib.TRISTATE]:
|
2021-01-26 02:49:01 +00:00
|
|
|
val = (val != 'n')
|
2018-01-23 06:08:28 +00:00
|
|
|
elif sym.type == kconfiglib.HEX:
|
|
|
|
val = int(val, 16)
|
|
|
|
elif sym.type == kconfiglib.INT:
|
|
|
|
val = int(val)
|
|
|
|
config_dict[sym.name] = val
|
2019-09-10 07:58:52 +00:00
|
|
|
for n in config.node_iter(False):
|
|
|
|
write_node(n)
|
2018-05-16 06:54:22 +00:00
|
|
|
return config_dict
|
|
|
|
|
2018-12-04 12:46:48 +00:00
|
|
|
|
2019-03-14 08:54:04 +00:00
|
|
|
def write_json(deprecated_options, config, filename):
|
2018-05-16 06:54:22 +00:00
|
|
|
config_dict = get_json_values(config)
|
2021-01-26 02:49:01 +00:00
|
|
|
with open(filename, 'w') as f:
|
2018-01-23 06:08:28 +00:00
|
|
|
json.dump(config_dict, f, indent=4, sort_keys=True)
|
2018-01-12 02:50:19 +00:00
|
|
|
|
2018-12-04 12:46:48 +00:00
|
|
|
|
2019-01-25 04:16:51 +00:00
|
|
|
def get_menu_node_id(node):
|
|
|
|
""" Given a menu node, return a unique id
|
|
|
|
which can be used to identify it in the menu structure
|
|
|
|
|
|
|
|
Will either be the config symbol name, or a menu identifier
|
|
|
|
'slug'
|
|
|
|
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
if not isinstance(node.item, kconfiglib.Choice):
|
|
|
|
return node.item.name
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
result = []
|
|
|
|
while node.parent is not None:
|
|
|
|
slug = re.sub(r'\W+', '-', node.prompt[0]).lower()
|
|
|
|
result.append(slug)
|
|
|
|
node = node.parent
|
|
|
|
|
2021-01-26 02:49:01 +00:00
|
|
|
result = '-'.join(reversed(result))
|
2019-01-25 04:16:51 +00:00
|
|
|
return result
|
|
|
|
|
|
|
|
|
2019-03-14 08:54:04 +00:00
|
|
|
def write_json_menus(deprecated_options, config, filename):
|
2019-01-25 04:16:51 +00:00
|
|
|
existing_ids = set()
|
2018-05-16 06:54:22 +00:00
|
|
|
result = [] # root level items
|
|
|
|
node_lookup = {} # lookup from MenuNode to an item in result
|
|
|
|
|
|
|
|
def write_node(node):
|
|
|
|
try:
|
2021-01-26 02:49:01 +00:00
|
|
|
json_parent = node_lookup[node.parent]['children']
|
2018-05-16 06:54:22 +00:00
|
|
|
except KeyError:
|
2018-12-04 12:46:48 +00:00
|
|
|
assert node.parent not in node_lookup # if fails, we have a parent node with no "children" entity (ie a bug)
|
2018-05-16 06:54:22 +00:00
|
|
|
json_parent = result # root level node
|
|
|
|
|
|
|
|
# node.kconfig.y means node has no dependency,
|
|
|
|
if node.dep is node.kconfig.y:
|
|
|
|
depends = None
|
|
|
|
else:
|
|
|
|
depends = kconfiglib.expr_str(node.dep)
|
|
|
|
|
|
|
|
try:
|
2019-09-10 07:58:52 +00:00
|
|
|
# node.is_menuconfig is True in newer kconfiglibs for menus and choices as well
|
|
|
|
is_menuconfig = node.is_menuconfig and isinstance(node.item, kconfiglib.Symbol)
|
2018-05-16 06:54:22 +00:00
|
|
|
except AttributeError:
|
|
|
|
is_menuconfig = False
|
|
|
|
|
|
|
|
new_json = None
|
|
|
|
if node.item == kconfiglib.MENU or is_menuconfig:
|
2021-01-26 02:49:01 +00:00
|
|
|
new_json = {'type': 'menu',
|
|
|
|
'title': node.prompt[0],
|
|
|
|
'depends_on': depends,
|
|
|
|
'children': [],
|
2018-12-04 12:46:48 +00:00
|
|
|
}
|
2018-05-16 06:54:22 +00:00
|
|
|
if is_menuconfig:
|
|
|
|
sym = node.item
|
2021-01-26 02:49:01 +00:00
|
|
|
new_json['name'] = sym.name
|
|
|
|
new_json['help'] = node.help
|
|
|
|
new_json['is_menuconfig'] = is_menuconfig
|
2018-05-16 06:54:22 +00:00
|
|
|
greatest_range = None
|
|
|
|
if len(sym.ranges) > 0:
|
|
|
|
# Note: Evaluating the condition using kconfiglib's expr_value
|
2019-03-01 04:38:23 +00:00
|
|
|
# should have one condition which is true
|
2018-05-16 06:54:22 +00:00
|
|
|
for min_range, max_range, cond_expr in sym.ranges:
|
2019-03-01 04:38:23 +00:00
|
|
|
if kconfiglib.expr_value(cond_expr):
|
2018-05-16 06:54:22 +00:00
|
|
|
greatest_range = [min_range, max_range]
|
2021-01-26 02:49:01 +00:00
|
|
|
new_json['range'] = greatest_range
|
2018-05-16 06:54:22 +00:00
|
|
|
|
|
|
|
elif isinstance(node.item, kconfiglib.Symbol):
|
|
|
|
sym = node.item
|
|
|
|
greatest_range = None
|
|
|
|
if len(sym.ranges) > 0:
|
|
|
|
# Note: Evaluating the condition using kconfiglib's expr_value
|
2019-03-01 04:38:23 +00:00
|
|
|
# should have one condition which is true
|
2018-05-16 06:54:22 +00:00
|
|
|
for min_range, max_range, cond_expr in sym.ranges:
|
2019-03-01 04:38:23 +00:00
|
|
|
if kconfiglib.expr_value(cond_expr):
|
2019-07-03 11:51:53 +00:00
|
|
|
base = 16 if sym.type == kconfiglib.HEX else 10
|
|
|
|
greatest_range = [int(min_range.str_value, base), int(max_range.str_value, base)]
|
2019-07-06 07:17:31 +00:00
|
|
|
break
|
2018-05-16 06:54:22 +00:00
|
|
|
|
|
|
|
new_json = {
|
2021-01-26 02:49:01 +00:00
|
|
|
'type': kconfiglib.TYPE_TO_STR[sym.type],
|
|
|
|
'name': sym.name,
|
|
|
|
'title': node.prompt[0] if node.prompt else None,
|
|
|
|
'depends_on': depends,
|
|
|
|
'help': node.help,
|
|
|
|
'range': greatest_range,
|
|
|
|
'children': [],
|
2018-05-16 06:54:22 +00:00
|
|
|
}
|
|
|
|
elif isinstance(node.item, kconfiglib.Choice):
|
|
|
|
choice = node.item
|
|
|
|
new_json = {
|
2021-01-26 02:49:01 +00:00
|
|
|
'type': 'choice',
|
|
|
|
'title': node.prompt[0],
|
|
|
|
'name': choice.name,
|
|
|
|
'depends_on': depends,
|
|
|
|
'help': node.help,
|
|
|
|
'children': []
|
2018-05-16 06:54:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if new_json:
|
2019-01-25 04:16:51 +00:00
|
|
|
node_id = get_menu_node_id(node)
|
|
|
|
if node_id in existing_ids:
|
2021-01-26 02:49:01 +00:00
|
|
|
raise RuntimeError('Config file contains two items with the same id: %s (%s). ' +
|
|
|
|
'Please rename one of these items to avoid ambiguity.' % (node_id, node.prompt[0]))
|
|
|
|
new_json['id'] = node_id
|
2019-01-25 04:16:51 +00:00
|
|
|
|
2018-05-16 06:54:22 +00:00
|
|
|
json_parent.append(new_json)
|
|
|
|
node_lookup[node] = new_json
|
|
|
|
|
2019-09-10 07:58:52 +00:00
|
|
|
for n in config.node_iter():
|
|
|
|
write_node(n)
|
2021-01-26 02:49:01 +00:00
|
|
|
with open(filename, 'w') as f:
|
2018-05-16 06:54:22 +00:00
|
|
|
f.write(json.dumps(result, sort_keys=True, indent=4))
|
|
|
|
|
2018-12-04 12:46:48 +00:00
|
|
|
|
2019-03-14 08:54:04 +00:00
|
|
|
def write_docs(deprecated_options, config, filename):
|
2019-12-12 15:02:36 +00:00
|
|
|
try:
|
|
|
|
target = os.environ['IDF_TARGET']
|
|
|
|
except KeyError:
|
|
|
|
print('IDF_TARGET environment variable must be defined!')
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
visibility = gen_kconfig_doc.ConfigTargetVisibility(config, target)
|
|
|
|
gen_kconfig_doc.write_docs(config, visibility, filename)
|
|
|
|
deprecated_options.append_doc(config, visibility, filename)
|
2019-03-14 08:54:04 +00:00
|
|
|
|
|
|
|
|
2018-01-12 02:50:19 +00:00
|
|
|
def update_if_changed(source, destination):
|
2021-01-26 02:49:01 +00:00
|
|
|
with open(source, 'r') as f:
|
2018-01-12 02:50:19 +00:00
|
|
|
source_contents = f.read()
|
2019-01-15 15:06:50 +00:00
|
|
|
|
2018-01-12 02:50:19 +00:00
|
|
|
if os.path.exists(destination):
|
2021-01-26 02:49:01 +00:00
|
|
|
with open(destination, 'r') as f:
|
2018-01-12 02:50:19 +00:00
|
|
|
dest_contents = f.read()
|
|
|
|
if source_contents == dest_contents:
|
|
|
|
return # nothing to update
|
|
|
|
|
2021-01-26 02:49:01 +00:00
|
|
|
with open(destination, 'w') as f:
|
2018-01-12 02:50:19 +00:00
|
|
|
f.write(source_contents)
|
|
|
|
|
|
|
|
|
2021-01-26 02:49:01 +00:00
|
|
|
OUTPUT_FORMATS = {'config': write_config,
|
|
|
|
'header': write_header,
|
|
|
|
'cmake': write_cmake,
|
|
|
|
'docs': write_docs,
|
|
|
|
'json': write_json,
|
|
|
|
'json_menus': write_json_menus,
|
2021-12-14 14:31:08 +00:00
|
|
|
'savedefconfig': write_min_config,
|
2018-12-04 12:46:48 +00:00
|
|
|
}
|
|
|
|
|
2018-01-12 02:50:19 +00:00
|
|
|
|
2018-01-14 22:55:00 +00:00
|
|
|
class FatalError(RuntimeError):
|
|
|
|
"""
|
|
|
|
Class for runtime errors (not caused by bugs but by user input).
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
2018-12-04 12:46:48 +00:00
|
|
|
|
2018-01-12 02:50:19 +00:00
|
|
|
if __name__ == '__main__':
|
2018-01-14 22:55:00 +00:00
|
|
|
try:
|
|
|
|
main()
|
|
|
|
except FatalError as e:
|
2021-01-26 02:49:01 +00:00
|
|
|
print('A fatal error occurred: %s' % e)
|
2018-01-14 22:55:00 +00:00
|
|
|
sys.exit(2)
|