2018-01-11 21:50:19 -05: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.
|
|
|
|
#
|
|
|
|
# Copyright 2018 Espressif Systems (Shanghai) PTE LTD
|
|
|
|
#
|
|
|
|
# Licensed 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.
|
2018-09-18 08:43:23 -04:00
|
|
|
from __future__ import print_function
|
2018-01-11 21:50:19 -05:00
|
|
|
import argparse
|
2019-03-14 04:54:04 -04:00
|
|
|
import json
|
2018-01-11 21:50:19 -05:00
|
|
|
import os
|
|
|
|
import os.path
|
2019-01-24 23:16:51 -05:00
|
|
|
import re
|
2019-03-14 04:54:04 -04:00
|
|
|
import sys
|
|
|
|
import tempfile
|
2018-01-11 21:50:19 -05:00
|
|
|
|
|
|
|
import gen_kconfig_doc
|
|
|
|
import kconfiglib
|
|
|
|
|
|
|
|
__version__ = "0.1"
|
|
|
|
|
2018-12-04 07:46:48 -05:00
|
|
|
if "IDF_CMAKE" not in os.environ:
|
2018-08-08 03:58:00 -04:00
|
|
|
os.environ["IDF_CMAKE"] = ""
|
|
|
|
|
2018-12-04 07:46:48 -05:00
|
|
|
|
2019-03-14 04:54:04 -04: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 10:39:12 -04:00
|
|
|
def __init__(self, config_prefix, path_rename_files=[]):
|
2019-03-14 04:54:04 -04: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 10:39:12 -04:00
|
|
|
self.r_dic, self.rev_r_dic = self._parse_replacements(path_rename_files)
|
2019-03-14 04:54:04 -04: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 10:39:12 -04:00
|
|
|
def _parse_replacements(self, repl_paths):
|
2019-03-14 04:54:04 -04:00
|
|
|
rep_dic = {}
|
|
|
|
rev_rep_dic = {}
|
|
|
|
|
2019-04-26 06:50:20 -04: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 10:39:12 -04: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
|
|
|
|
rev_rep_dic[new_opt] = dep_opt
|
2019-03-14 04:54:04 -04:00
|
|
|
return rep_dic, rev_rep_dic
|
|
|
|
|
|
|
|
def get_deprecated_option(self, new_option):
|
|
|
|
return self.rev_r_dic.get(new_option, None)
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
def append_doc(self, config, path_output):
|
2019-04-26 09:43:40 -04:00
|
|
|
|
|
|
|
def option_was_written(opt):
|
|
|
|
return any(gen_kconfig_doc.node_should_write(node) for node in config.syms[opt].nodes)
|
|
|
|
|
2019-03-14 04:54:04 -04:00
|
|
|
if len(self.r_dic) > 0:
|
|
|
|
with open(path_output, 'a') as f_o:
|
|
|
|
header = 'Deprecated options and their replacements'
|
2019-05-22 07:01:19 -04:00
|
|
|
f_o.write('.. _configuration-deprecated-options:\n\n{}\n{}\n\n'.format(header, '-' * len(header)))
|
2019-04-26 09:43:40 -04:00
|
|
|
for dep_opt in sorted(self.r_dic):
|
|
|
|
new_opt = self.r_dic[dep_opt]
|
|
|
|
if new_opt not in config.syms or (config.syms[new_opt].choice is None and option_was_written(new_opt)):
|
|
|
|
# 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
|
|
|
|
dep_name = self.rev_r_dic[sym.name]
|
|
|
|
|
|
|
|
# config options doesn't have references
|
|
|
|
f_o.write(' - {}{}\n'.format(config.config_prefix, dep_name))
|
2019-03-14 04:54:04 -04: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:
|
|
|
|
tmp_list.append(c_string.replace(self.config_prefix + item.name,
|
|
|
|
self.config_prefix + self.rev_r_dic[item.name]))
|
|
|
|
|
|
|
|
config.walk_menu(append_config_node_process)
|
|
|
|
|
|
|
|
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 07:57:47 -04:00
|
|
|
def _opt_defined(opt):
|
|
|
|
if not opt.visibility:
|
|
|
|
return False
|
|
|
|
return not (opt.orig_type in (kconfiglib.BOOL, kconfiglib.TRISTATE) and opt.str_value == "n")
|
|
|
|
|
2019-03-14 04:54:04 -04: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 07:57:47 -04: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 04:54:04 -04:00
|
|
|
|
|
|
|
|
2018-01-11 21:50:19 -05:00
|
|
|
def main():
|
2018-01-21 22:32:02 -05:00
|
|
|
parser = argparse.ArgumentParser(description='confgen.py v%s - Config Generation Tool' % __version__, prog=os.path.basename(sys.argv[0]))
|
2018-01-11 21:50:19 -05:00
|
|
|
|
|
|
|
parser.add_argument('--config',
|
|
|
|
help='Project configuration settings',
|
|
|
|
nargs='?',
|
|
|
|
default=None)
|
|
|
|
|
2018-01-21 22:32:02 -05:00
|
|
|
parser.add_argument('--defaults',
|
2018-12-04 07:46:48 -05:00
|
|
|
help='Optional project defaults file, used if --config file doesn\'t exist. '
|
|
|
|
'Multiple files can be specified using multiple --defaults arguments.',
|
2018-01-21 22:32:02 -05:00
|
|
|
nargs='?',
|
2018-10-18 23:53:57 -04:00
|
|
|
default=[],
|
|
|
|
action='append')
|
2018-01-21 22:32:02 -05:00
|
|
|
|
2018-01-11 21:50:19 -05:00
|
|
|
parser.add_argument('--kconfig',
|
|
|
|
help='KConfig file with config item definitions',
|
|
|
|
required=True)
|
|
|
|
|
2019-07-16 10:39:12 -04:00
|
|
|
parser.add_argument('--sdkconfig-rename',
|
|
|
|
help='File with deprecated Kconfig options',
|
|
|
|
required=False)
|
|
|
|
|
2018-01-11 21:50:19 -05:00
|
|
|
parser.add_argument('--output', nargs=2, action='append',
|
|
|
|
help='Write output file (format and output filename)',
|
2018-01-14 17:55:00 -05:00
|
|
|
metavar=('FORMAT', 'FILENAME'),
|
|
|
|
default=[])
|
2018-01-11 21:50:19 -05:00
|
|
|
|
|
|
|
parser.add_argument('--env', action='append', default=[],
|
|
|
|
help='Environment to set when evaluating the config file', metavar='NAME=VAL')
|
|
|
|
|
2019-06-26 01:56:47 -04: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.')
|
|
|
|
|
2018-01-11 21:50:19 -05:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
for fmt, filename in args.output:
|
2018-12-04 07:46:48 -05:00
|
|
|
if fmt not in OUTPUT_FORMATS.keys():
|
2018-05-16 02:54:22 -04:00
|
|
|
print("Format '%s' not recognised. Known formats: %s" % (fmt, OUTPUT_FORMATS.keys()))
|
2018-01-11 21:50:19 -05:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
try:
|
2018-12-04 07:46:48 -05:00
|
|
|
args.env = [(name,value) for (name,value) in (e.split("=",1) for e in args.env)]
|
2018-01-11 21:50:19 -05:00
|
|
|
except ValueError:
|
2018-12-04 07:46:48 -05:00
|
|
|
print("--env arguments must each contain =. To unset an environment variable, use 'ENV='")
|
|
|
|
sys.exit(1)
|
2018-01-11 21:50:19 -05:00
|
|
|
|
|
|
|
for name, value in args.env:
|
|
|
|
os.environ[name] = value
|
|
|
|
|
2019-06-26 01:56:47 -04:00
|
|
|
if args.env_file is not None:
|
|
|
|
env = json.load(args.env_file)
|
|
|
|
os.environ.update(env)
|
|
|
|
|
2018-01-11 21:50:19 -05:00
|
|
|
config = kconfiglib.Kconfig(args.kconfig)
|
2019-01-25 05:24:53 -05:00
|
|
|
config.disable_redun_warnings()
|
2019-01-28 22:17:02 -05:00
|
|
|
config.disable_override_warnings()
|
2018-01-11 21:50:19 -05:00
|
|
|
|
2018-10-18 23:53:57 -04:00
|
|
|
if len(args.defaults) > 0:
|
2018-01-21 22:32:02 -05: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-18 23:53:57 -04:00
|
|
|
for name in args.defaults:
|
|
|
|
print("Loading defaults file %s..." % name)
|
|
|
|
if not os.path.exists(name):
|
|
|
|
raise RuntimeError("Defaults file not found: %s" % name)
|
|
|
|
config.load_config(name, replace=False)
|
2018-01-21 22:32:02 -05:00
|
|
|
|
2019-07-16 10:39:12 -04:00
|
|
|
sdkconfig_renames = [args.sdkconfig_rename] if args.sdkconfig_rename else []
|
|
|
|
sdkconfig_renames += os.environ.get("COMPONENT_SDKCONFIG_RENAMES", "").split()
|
|
|
|
deprecated_options = DeprecatedOptions(config.config_prefix, path_rename_files=sdkconfig_renames)
|
2019-03-14 04:54:04 -04:00
|
|
|
|
2019-01-15 10:06:50 -05:00
|
|
|
# If config file previously exists, load it
|
|
|
|
if args.config and os.path.exists(args.config):
|
2019-03-14 04:54:04 -04:00
|
|
|
# ... but replace deprecated options before that
|
|
|
|
with tempfile.NamedTemporaryFile(prefix="confgen_tmp", delete=False) as f:
|
|
|
|
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 10:06:50 -05:00
|
|
|
|
|
|
|
# Output the files specified in the arguments
|
|
|
|
for output_type, filename in args.output:
|
2019-03-14 04:54:04 -04:00
|
|
|
with tempfile.NamedTemporaryFile(prefix="confgen_tmp", delete=False) as f:
|
|
|
|
temp_file = f.name
|
2019-01-15 10:06:50 -05:00
|
|
|
try:
|
|
|
|
output_function = OUTPUT_FORMATS[output_type]
|
2019-03-14 04:54:04 -04:00
|
|
|
output_function(deprecated_options, config, temp_file)
|
2019-01-15 10:06:50 -05:00
|
|
|
update_if_changed(temp_file, filename)
|
|
|
|
finally:
|
2018-01-11 21:50:19 -05:00
|
|
|
try:
|
2019-01-15 10:06:50 -05:00
|
|
|
os.remove(temp_file)
|
|
|
|
except OSError:
|
|
|
|
pass
|
2018-01-11 21:50:19 -05:00
|
|
|
|
|
|
|
|
2019-03-14 04:54:04 -04:00
|
|
|
def write_config(deprecated_options, config, filename):
|
2018-01-11 21:50:19 -05: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 04:54:04 -04:00
|
|
|
deprecated_options.append_config(config, filename)
|
2018-01-11 21:50:19 -05:00
|
|
|
|
2018-12-04 07:46:48 -05:00
|
|
|
|
2019-03-14 04:54:04 -04:00
|
|
|
def write_makefile(deprecated_options, config, filename):
|
|
|
|
CONFIG_HEADING = """#
|
|
|
|
# Automatically generated file. DO NOT EDIT.
|
|
|
|
# Espressif IoT Development Framework (ESP-IDF) Project Makefile Configuration
|
|
|
|
#
|
|
|
|
"""
|
|
|
|
with open(filename, "w") as f:
|
|
|
|
tmp_dep_lines = []
|
|
|
|
f.write(CONFIG_HEADING)
|
|
|
|
|
|
|
|
def get_makefile_config_string(name, value, orig_type):
|
|
|
|
if orig_type in (kconfiglib.BOOL, kconfiglib.TRISTATE):
|
|
|
|
return "{}{}={}\n".format(config.config_prefix, name, '' if value == 'n' else value)
|
|
|
|
elif orig_type in (kconfiglib.INT, kconfiglib.HEX):
|
|
|
|
return "{}{}={}\n".format(config.config_prefix, name, value)
|
|
|
|
elif orig_type == kconfiglib.STRING:
|
|
|
|
return '{}{}="{}"\n'.format(config.config_prefix, name, kconfiglib.escape(value))
|
|
|
|
else:
|
|
|
|
raise RuntimeError('{}{}: unknown type {}'.format(config.config_prefix, name, orig_type))
|
|
|
|
|
|
|
|
def write_makefile_node(node):
|
|
|
|
item = node.item
|
|
|
|
if isinstance(item, kconfiglib.Symbol) and item.env_var is None:
|
|
|
|
# item.config_string cannot be used because it ignores hidden config items
|
|
|
|
val = item.str_value
|
|
|
|
f.write(get_makefile_config_string(item.name, val, item.orig_type))
|
|
|
|
|
|
|
|
dep_opt = deprecated_options.get_deprecated_option(item.name)
|
|
|
|
if dep_opt:
|
|
|
|
# the same string but with the deprecated name
|
|
|
|
tmp_dep_lines.append(get_makefile_config_string(dep_opt, val, item.orig_type))
|
|
|
|
|
|
|
|
config.walk_menu(write_makefile_node, True)
|
|
|
|
|
|
|
|
if len(tmp_dep_lines) > 0:
|
|
|
|
f.write('\n# List of deprecated options\n')
|
|
|
|
f.writelines(tmp_dep_lines)
|
|
|
|
|
|
|
|
|
|
|
|
def write_header(deprecated_options, config, filename):
|
2018-01-11 21:50:19 -05: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 04:54:04 -04:00
|
|
|
deprecated_options.append_header(config, filename)
|
2018-01-11 21:50:19 -05:00
|
|
|
|
2018-12-04 07:46:48 -05:00
|
|
|
|
2019-03-14 04:54:04 -04:00
|
|
|
def write_cmake(deprecated_options, config, filename):
|
2018-01-11 21:50:19 -05:00
|
|
|
with open(filename, "w") as f:
|
2019-03-14 04:54:04 -04:00
|
|
|
tmp_dep_list = []
|
2018-01-11 21:50:19 -05: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 07:46:48 -05:00
|
|
|
|
2019-04-26 01:42:10 -04:00
|
|
|
configs_list = list()
|
|
|
|
|
2018-01-11 21:50:19 -05:00
|
|
|
def write_node(node):
|
|
|
|
sym = node.item
|
|
|
|
if not isinstance(sym, kconfiglib.Symbol):
|
|
|
|
return
|
|
|
|
|
2019-07-01 11:03:55 -04:00
|
|
|
if sym.config_string:
|
|
|
|
val = sym.str_value
|
2018-01-11 21:50:19 -05:00
|
|
|
if sym.orig_type in (kconfiglib.BOOL, kconfiglib.TRISTATE) and val == "n":
|
|
|
|
val = "" # write unset values as empty variables
|
|
|
|
write("set({}{} \"{}\")\n".format(
|
|
|
|
prefix, sym.name, val))
|
2019-04-26 01:42:10 -04:00
|
|
|
|
|
|
|
configs_list.append(prefix + sym.name)
|
2019-03-14 04:54:04 -04:00
|
|
|
dep_opt = deprecated_options.get_deprecated_option(sym.name)
|
|
|
|
if dep_opt:
|
|
|
|
tmp_dep_list.append("set({}{} \"{}\")\n".format(prefix, dep_opt, val))
|
2019-04-26 01:42:10 -04:00
|
|
|
configs_list.append(prefix + dep_opt)
|
|
|
|
|
2018-01-11 21:50:19 -05:00
|
|
|
config.walk_menu(write_node)
|
2019-04-26 01:42:10 -04:00
|
|
|
write("set(CONFIGS_LIST {})".format(";".join(configs_list)))
|
2018-01-11 21:50:19 -05:00
|
|
|
|
2019-03-14 04:54:04 -04: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 07:46:48 -05:00
|
|
|
|
2018-05-16 02:54:22 -04:00
|
|
|
def get_json_values(config):
|
2018-01-23 01:08:28 -05:00
|
|
|
config_dict = {}
|
2018-12-04 07:46:48 -05:00
|
|
|
|
2018-01-23 01:08:28 -05:00
|
|
|
def write_node(node):
|
|
|
|
sym = node.item
|
|
|
|
if not isinstance(sym, kconfiglib.Symbol):
|
|
|
|
return
|
|
|
|
|
2019-07-01 11:03:55 -04:00
|
|
|
if sym.config_string:
|
|
|
|
val = sym.str_value
|
2018-12-04 07:46:48 -05:00
|
|
|
if sym.type in [kconfiglib.BOOL, kconfiglib.TRISTATE]:
|
2018-01-23 01:08:28 -05:00
|
|
|
val = (val != "n")
|
|
|
|
elif sym.type == kconfiglib.HEX:
|
|
|
|
val = int(val, 16)
|
|
|
|
elif sym.type == kconfiglib.INT:
|
|
|
|
val = int(val)
|
|
|
|
config_dict[sym.name] = val
|
|
|
|
config.walk_menu(write_node)
|
2018-05-16 02:54:22 -04:00
|
|
|
return config_dict
|
|
|
|
|
2018-12-04 07:46:48 -05:00
|
|
|
|
2019-03-14 04:54:04 -04:00
|
|
|
def write_json(deprecated_options, config, filename):
|
2018-05-16 02:54:22 -04:00
|
|
|
config_dict = get_json_values(config)
|
2018-01-23 01:08:28 -05:00
|
|
|
with open(filename, "w") as f:
|
|
|
|
json.dump(config_dict, f, indent=4, sort_keys=True)
|
2018-01-11 21:50:19 -05:00
|
|
|
|
2018-12-04 07:46:48 -05:00
|
|
|
|
2019-01-24 23:16:51 -05: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
|
|
|
|
|
|
|
|
result = "-".join(reversed(result))
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
2019-03-14 04:54:04 -04:00
|
|
|
def write_json_menus(deprecated_options, config, filename):
|
2019-01-24 23:16:51 -05:00
|
|
|
existing_ids = set()
|
2018-05-16 02:54:22 -04:00
|
|
|
result = [] # root level items
|
|
|
|
node_lookup = {} # lookup from MenuNode to an item in result
|
|
|
|
|
|
|
|
def write_node(node):
|
|
|
|
try:
|
|
|
|
json_parent = node_lookup[node.parent]["children"]
|
|
|
|
except KeyError:
|
2018-12-04 07:46:48 -05: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 02:54:22 -04: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:
|
|
|
|
is_menuconfig = node.is_menuconfig
|
|
|
|
except AttributeError:
|
|
|
|
is_menuconfig = False
|
|
|
|
|
|
|
|
new_json = None
|
|
|
|
if node.item == kconfiglib.MENU or is_menuconfig:
|
2018-12-04 07:46:48 -05:00
|
|
|
new_json = {"type": "menu",
|
|
|
|
"title": node.prompt[0],
|
|
|
|
"depends_on": depends,
|
2019-01-24 23:16:51 -05:00
|
|
|
"children": [],
|
2018-12-04 07:46:48 -05:00
|
|
|
}
|
2018-05-16 02:54:22 -04:00
|
|
|
if is_menuconfig:
|
|
|
|
sym = node.item
|
|
|
|
new_json["name"] = sym.name
|
|
|
|
new_json["help"] = node.help
|
|
|
|
new_json["is_menuconfig"] = is_menuconfig
|
|
|
|
greatest_range = None
|
|
|
|
if len(sym.ranges) > 0:
|
|
|
|
# Note: Evaluating the condition using kconfiglib's expr_value
|
2019-02-28 23:38:23 -05:00
|
|
|
# should have one condition which is true
|
2018-05-16 02:54:22 -04:00
|
|
|
for min_range, max_range, cond_expr in sym.ranges:
|
2019-02-28 23:38:23 -05:00
|
|
|
if kconfiglib.expr_value(cond_expr):
|
2018-05-16 02:54:22 -04:00
|
|
|
greatest_range = [min_range, max_range]
|
|
|
|
new_json["range"] = greatest_range
|
|
|
|
|
|
|
|
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-02-28 23:38:23 -05:00
|
|
|
# should have one condition which is true
|
2018-05-16 02:54:22 -04:00
|
|
|
for min_range, max_range, cond_expr in sym.ranges:
|
2019-02-28 23:38:23 -05:00
|
|
|
if kconfiglib.expr_value(cond_expr):
|
2019-07-03 07:51:53 -04: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 03:17:31 -04:00
|
|
|
break
|
2018-05-16 02:54:22 -04:00
|
|
|
|
|
|
|
new_json = {
|
2018-12-04 07:46:48 -05:00
|
|
|
"type": kconfiglib.TYPE_TO_STR[sym.type],
|
|
|
|
"name": sym.name,
|
2018-05-16 02:54:22 -04:00
|
|
|
"title": node.prompt[0] if node.prompt else None,
|
2018-12-04 07:46:48 -05:00
|
|
|
"depends_on": depends,
|
2018-05-16 02:54:22 -04:00
|
|
|
"help": node.help,
|
2018-12-04 07:46:48 -05:00
|
|
|
"range": greatest_range,
|
2018-05-16 02:54:22 -04:00
|
|
|
"children": [],
|
|
|
|
}
|
|
|
|
elif isinstance(node.item, kconfiglib.Choice):
|
|
|
|
choice = node.item
|
|
|
|
new_json = {
|
|
|
|
"type": "choice",
|
|
|
|
"title": node.prompt[0],
|
|
|
|
"name": choice.name,
|
2018-12-04 07:46:48 -05:00
|
|
|
"depends_on": depends,
|
2018-05-16 02:54:22 -04:00
|
|
|
"help": node.help,
|
|
|
|
"children": []
|
|
|
|
}
|
|
|
|
|
|
|
|
if new_json:
|
2019-01-24 23:16:51 -05:00
|
|
|
node_id = get_menu_node_id(node)
|
|
|
|
if node_id in existing_ids:
|
|
|
|
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
|
|
|
|
|
2018-05-16 02:54:22 -04:00
|
|
|
json_parent.append(new_json)
|
|
|
|
node_lookup[node] = new_json
|
|
|
|
|
|
|
|
config.walk_menu(write_node)
|
|
|
|
with open(filename, "w") as f:
|
|
|
|
f.write(json.dumps(result, sort_keys=True, indent=4))
|
|
|
|
|
2018-12-04 07:46:48 -05:00
|
|
|
|
2019-03-14 04:54:04 -04:00
|
|
|
def write_docs(deprecated_options, config, filename):
|
|
|
|
gen_kconfig_doc.write_docs(config, filename)
|
|
|
|
deprecated_options.append_doc(config, filename)
|
|
|
|
|
|
|
|
|
2018-01-11 21:50:19 -05:00
|
|
|
def update_if_changed(source, destination):
|
|
|
|
with open(source, "r") as f:
|
|
|
|
source_contents = f.read()
|
2019-01-15 10:06:50 -05:00
|
|
|
|
2018-01-11 21:50:19 -05:00
|
|
|
if os.path.exists(destination):
|
|
|
|
with open(destination, "r") as f:
|
|
|
|
dest_contents = f.read()
|
|
|
|
if source_contents == dest_contents:
|
|
|
|
return # nothing to update
|
|
|
|
|
|
|
|
with open(destination, "w") as f:
|
|
|
|
f.write(source_contents)
|
|
|
|
|
|
|
|
|
2018-12-04 07:46:48 -05:00
|
|
|
OUTPUT_FORMATS = {"config": write_config,
|
2019-03-14 04:54:04 -04:00
|
|
|
"makefile": write_makefile, # only used with make in order to generate auto.conf
|
2018-12-04 07:46:48 -05:00
|
|
|
"header": write_header,
|
|
|
|
"cmake": write_cmake,
|
2019-03-14 04:54:04 -04:00
|
|
|
"docs": write_docs,
|
2018-12-04 07:46:48 -05:00
|
|
|
"json": write_json,
|
|
|
|
"json_menus": write_json_menus,
|
|
|
|
}
|
|
|
|
|
2018-01-11 21:50:19 -05:00
|
|
|
|
2018-01-14 17:55:00 -05:00
|
|
|
class FatalError(RuntimeError):
|
|
|
|
"""
|
|
|
|
Class for runtime errors (not caused by bugs but by user input).
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
2018-12-04 07:46:48 -05:00
|
|
|
|
2018-01-11 21:50:19 -05:00
|
|
|
if __name__ == '__main__':
|
2018-01-14 17:55:00 -05:00
|
|
|
try:
|
|
|
|
main()
|
|
|
|
except FatalError as e:
|
|
|
|
print("A fatal error occurred: %s" % e)
|
|
|
|
sys.exit(2)
|