1#!/usr/bin/env python 2# 3# Copyright 2019 Espressif Systems (Shanghai) PTE LTD 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http:#www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16 17from __future__ import print_function, unicode_literals 18 19import argparse 20import json 21import sys 22from io import open 23 24 25def _prepare_source_files(env_dict): 26 """ 27 Prepares source files which are sourced from the main Kconfig because upstream kconfiglib doesn't support sourcing 28 a file list. The inputs are the same environment variables which are used by kconfiglib: 29 - COMPONENT_KCONFIGS, 30 - COMPONENT_KCONFIGS_SOURCE_FILE, 31 - COMPONENT_KCONFIGS_PROJBUILD, 32 - COMPONENT_KCONFIGS_PROJBUILD_SOURCE_FILE. 33 34 The outputs are written into files pointed by the value of 35 - COMPONENT_KCONFIGS_SOURCE_FILE, 36 - COMPONENT_KCONFIGS_PROJBUILD_SOURCE_FILE, 37 38 After running this function, COMPONENT_KCONFIGS_SOURCE_FILE and COMPONENT_KCONFIGS_PROJBUILD_SOURCE_FILE will 39 contain a list of source statements based on the content of COMPONENT_KCONFIGS and COMPONENT_KCONFIGS_PROJBUILD, 40 respectively. For example, if COMPONENT_KCONFIGS="var1 var2 var3" and 41 COMPONENT_KCONFIGS_SOURCE_FILE="/path/file.txt" then the content of file /path/file.txt will be: 42 source "var1" 43 source "var2" 44 source "var3" 45 """ 46 47 def _dequote(var): 48 return var[1:-1] if len(var) > 0 and (var[0], var[-1]) == ('"',) * 2 else var 49 50 def _write_source_file(config_var, config_file): 51 new_content = '\n'.join(['source "{}"'.format(path) for path in _dequote(config_var).split()]) 52 try: 53 with open(config_file, 'r', encoding='utf-8') as f: 54 old_content = f.read() 55 except Exception: 56 # File doesn't exist or other issue 57 old_content = None 58 # "None" ensures that it won't be equal to new_content when it is empty string because files need to be 59 # created for empty environment variables as well 60 61 if new_content != old_content: 62 # write or rewrite file only if it is necessary 63 with open(config_file, 'w', encoding='utf-8') as f: 64 f.write(new_content) 65 66 try: 67 _write_source_file(env_dict['COMPONENT_KCONFIGS'], env_dict['COMPONENT_KCONFIGS_SOURCE_FILE']) 68 _write_source_file(env_dict['COMPONENT_KCONFIGS_PROJBUILD'], env_dict['COMPONENT_KCONFIGS_PROJBUILD_SOURCE_FILE']) 69 except KeyError as e: 70 print('Error:', e, 'is not defined!') 71 sys.exit(1) 72 73 74if __name__ == '__main__': 75 76 parser = argparse.ArgumentParser(description='Kconfig Source File Generator') 77 78 parser.add_argument('--env', action='append', default=[], 79 help='Environment value', metavar='NAME=VAL') 80 81 parser.add_argument('--env-file', type=argparse.FileType('r'), 82 help='Optional file to load environment variables from. Contents ' 83 'should be a JSON object where each key/value pair is a variable.') 84 85 args = parser.parse_args() 86 87 try: 88 env = dict([(name, value) for (name, value) in (e.split('=', 1) for e in args.env)]) 89 except ValueError: 90 print('--env arguments must each contain =.') 91 sys.exit(1) 92 93 if args.env_file is not None: 94 env.update(json.load(args.env_file)) 95 96 _prepare_source_files(env) 97