154568cb3Sopenharmony_ci#!/usr/bin/env python3
254568cb3Sopenharmony_ci# coding=utf-8
354568cb3Sopenharmony_ci# Transfer kconfig configuration file to buildef/config file.
454568cb3Sopenharmony_ci# Copyright © Huawei Technologies Co., Ltd. 2010-2020. All rights reserved.
554568cb3Sopenharmony_ci
654568cb3Sopenharmony_ciimport os
754568cb3Sopenharmony_ciimport sys
854568cb3Sopenharmony_ciimport time
954568cb3Sopenharmony_ciimport getopt
1054568cb3Sopenharmony_ciimport re
1154568cb3Sopenharmony_ciimport logging
1254568cb3Sopenharmony_ciimport codecs
1354568cb3Sopenharmony_ci
1454568cb3Sopenharmony_ci
1554568cb3Sopenharmony_cilogging.basicConfig(level=logging.NOTSET)
1654568cb3Sopenharmony_ci
1754568cb3Sopenharmony_ciHEADER_TYPE_BUILD = 0
1854568cb3Sopenharmony_ciHEADER_TYPE_CONFIG = 1
1954568cb3Sopenharmony_ciHEADER_TYPE_INVALID = 2
2054568cb3Sopenharmony_ci
2154568cb3Sopenharmony_cidef cmd_help():
2254568cb3Sopenharmony_ci    logging.info("Interpret the configuration file which was generated by Kconfig tool, and")
2354568cb3Sopenharmony_ci    logging.info("Transfer it to header file which will be used in C code")
2454568cb3Sopenharmony_ci    logging.info("Command format:")
2554568cb3Sopenharmony_ci    logging.info("   Kconfig2macro.py [-e][-f configFileName] [-o headerFileName]")
2654568cb3Sopenharmony_ci    logging.info("The default configFileName is .config")
2754568cb3Sopenharmony_ci    logging.info("The default headerFileName is config.h")
2854568cb3Sopenharmony_ci
2954568cb3Sopenharmony_cidef headerType(in_file):
3054568cb3Sopenharmony_ci    with open(in_file, 'r') as fd_in:
3154568cb3Sopenharmony_ci        lines = fd_in.readlines()
3254568cb3Sopenharmony_ci        for line in lines:
3354568cb3Sopenharmony_ci
3454568cb3Sopenharmony_ci            if line.find("CONFIG_OS_HARDWARE_PLATFORM=") >= 0:
3554568cb3Sopenharmony_ci                return HEADER_TYPE_BUILD
3654568cb3Sopenharmony_ci    return HEADER_TYPE_CONFIG
3754568cb3Sopenharmony_ci
3854568cb3Sopenharmony_ci
3954568cb3Sopenharmony_cidef write_header(out_file):
4054568cb3Sopenharmony_ci    with codecs.open(out_file,
4154568cb3Sopenharmony_ci                     'w',
4254568cb3Sopenharmony_ci                     encoding='gbk',
4354568cb3Sopenharmony_ci                     errors='ignore') as fd_out:
4454568cb3Sopenharmony_ci        file_path, file_name = os.path.split(out_file)
4554568cb3Sopenharmony_ci        name = ((str(file_name)).split('.'))[0]
4654568cb3Sopenharmony_ci        header_macro = '%s_H' % (str.upper(name))
4754568cb3Sopenharmony_ci        text = r'''/*
4854568cb3Sopenharmony_ci * Copyright (c) 2009-2022 Huawei Technologies Co., Ltd. All rights reserved.
4954568cb3Sopenharmony_ci *
5054568cb3Sopenharmony_ci * UniProton is licensed under Mulan PSL v2.
5154568cb3Sopenharmony_ci * You can use this software according to the terms and conditions of the Mulan PSL v2.
5254568cb3Sopenharmony_ci * You may obtain a copy of Mulan PSL v2 at:
5354568cb3Sopenharmony_ci *          http://license.coscl.org.cn/MulanPSL2
5454568cb3Sopenharmony_ci * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
5554568cb3Sopenharmony_ci * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
5654568cb3Sopenharmony_ci * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
5754568cb3Sopenharmony_ci * See the Mulan PSL v2 for more details.
5854568cb3Sopenharmony_ci * Create: 2009-12-22
5954568cb3Sopenharmony_ci * Description: the common part buildef.h
6054568cb3Sopenharmony_ci */
6154568cb3Sopenharmony_ci'''
6254568cb3Sopenharmony_ci
6354568cb3Sopenharmony_ci        text = '%s#ifndef %s\n' % (text, header_macro)
6454568cb3Sopenharmony_ci        text = '%s#define %s\n' % (text, header_macro)
6554568cb3Sopenharmony_ci        text = '%s\n' % (text)
6654568cb3Sopenharmony_ci        text = '%s#ifdef __cplusplus\n#if __cplusplus\nextern "C" {\n#endif\n#endif\n' % (text)
6754568cb3Sopenharmony_ci
6854568cb3Sopenharmony_ci        fd_out.write(text)
6954568cb3Sopenharmony_ci    return 0
7054568cb3Sopenharmony_ci
7154568cb3Sopenharmony_ci
7254568cb3Sopenharmony_cidef get_macro_fix(macro):
7354568cb3Sopenharmony_ci    prefix = ""
7454568cb3Sopenharmony_ci    suffix = "\n"
7554568cb3Sopenharmony_ci    if macro == "OS_HARDWARE_PLATFORM" or macro == "OS_CPU_TYPE" or macro == "OS_BYTE_ORDER":
7654568cb3Sopenharmony_ci        prefix = '#ifndef %s\n' % (macro)
7754568cb3Sopenharmony_ci        suffix = "#endif\n\n"
7854568cb3Sopenharmony_ci
7954568cb3Sopenharmony_ci    return prefix, suffix
8054568cb3Sopenharmony_ci
8154568cb3Sopenharmony_cidef write_macro(fd_out, line, flag):
8254568cb3Sopenharmony_ci    export_flag = False
8354568cb3Sopenharmony_ci    match_s = re.search(r'\w+', line)
8454568cb3Sopenharmony_ci    if match_s is None:
8554568cb3Sopenharmony_ci        return
8654568cb3Sopenharmony_ci
8754568cb3Sopenharmony_ci    macro = match_s.group()
8854568cb3Sopenharmony_ci    if not macro.startswith("CONFIG_"):
8954568cb3Sopenharmony_ci        return
9054568cb3Sopenharmony_ci
9154568cb3Sopenharmony_ci    macro = macro[len("CONFIG_"):]
9254568cb3Sopenharmony_ci    if macro.startswith("INTERNAL_"):
9354568cb3Sopenharmony_ci        return
9454568cb3Sopenharmony_ci
9554568cb3Sopenharmony_ci    if macro.startswith("EXPORT_"):
9654568cb3Sopenharmony_ci        macro = macro[len("EXPORT_"):]
9754568cb3Sopenharmony_ci        export_flag = flag
9854568cb3Sopenharmony_ci
9954568cb3Sopenharmony_ci    if line.find("is not set") >= 0:
10054568cb3Sopenharmony_ci        value = "NO"
10154568cb3Sopenharmony_ci    else:
10254568cb3Sopenharmony_ci        expression = line.split("=", 1)
10354568cb3Sopenharmony_ci        if len(expression) < 2:
10454568cb3Sopenharmony_ci            return
10554568cb3Sopenharmony_ci        value = expression[1].strip()
10654568cb3Sopenharmony_ci        if value == 'y':
10754568cb3Sopenharmony_ci            value = ""
10854568cb3Sopenharmony_ci    prefix, suffix = get_macro_fix(macro)
10954568cb3Sopenharmony_ci    text = '%s#define %s %s\n%s' %(prefix ,macro, value.strip('"'), suffix)
11054568cb3Sopenharmony_ci    if export_flag == True or flag == False:
11154568cb3Sopenharmony_ci        fd_out.write(text.rstrip() + "\n\n")
11254568cb3Sopenharmony_cidef write_config(in_file, out_file, flag):
11354568cb3Sopenharmony_ci    with codecs.open(out_file, 'a+') as fd_out, open(in_file) as fd_in:
11454568cb3Sopenharmony_ci        fd_out.write("\n")
11554568cb3Sopenharmony_ci        lines = fd_in.readlines()
11654568cb3Sopenharmony_ci        for line in lines:
11754568cb3Sopenharmony_ci            if line.startswith('##'):
11854568cb3Sopenharmony_ci                fd_out(line.replace("##", "/*") + "*/")
11954568cb3Sopenharmony_ci            elif line.startswith("CONFIG_"):
12054568cb3Sopenharmony_ci                write_macro(fd_out, line, flag)
12154568cb3Sopenharmony_ci
12254568cb3Sopenharmony_ci    return 0
12354568cb3Sopenharmony_ci
12454568cb3Sopenharmony_cidef write_def_config(filename, string):
12554568cb3Sopenharmony_ci    with open(filename, 'r', errors='ignore') as fp:
12654568cb3Sopenharmony_ci        for line in fp:
12754568cb3Sopenharmony_ci            if string in line:
12854568cb3Sopenharmony_ci                return line
12954568cb3Sopenharmony_ci
13054568cb3Sopenharmony_ci    with codecs.open(file, 'a+') as fd_in:
13154568cb3Sopenharmony_ci        text = '#define %-35s NO \n\n' %(str)
13254568cb3Sopenharmony_ci
13354568cb3Sopenharmony_ci        fd_in.write(text)
13454568cb3Sopenharmony_ci
13554568cb3Sopenharmony_ci    return 0
13654568cb3Sopenharmony_ci
13754568cb3Sopenharmony_cidef write_external_macro(out_file):
13854568cb3Sopenharmony_ci    with codecs.open(out_file, 'a+') as fd_out:
13954568cb3Sopenharmony_ci
14054568cb3Sopenharmony_ci        text = '#define OS_LITTLE_ENDIAN 0x1234\n\n#define OS_BIG_ENDIAN 0x4321\n\n#define OS_CORTEX_M4 0x1\n\n#define OS_STM32F407 0x1\n\n'
14154568cb3Sopenharmony_ci
14254568cb3Sopenharmony_ci        fd_out.write(text)
14354568cb3Sopenharmony_ci
14454568cb3Sopenharmony_ci    return 0
14554568cb3Sopenharmony_ci
14654568cb3Sopenharmony_cidef write_c_external_tail(out_file):
14754568cb3Sopenharmony_ci    with codecs.open(out_file, 'a+') as fd_out:
14854568cb3Sopenharmony_ci
14954568cb3Sopenharmony_ci
15054568cb3Sopenharmony_ci        text = '#ifdef __cplusplus\n#if __cplusplus\n}\n#endif\n#endif\n'
15154568cb3Sopenharmony_ci
15254568cb3Sopenharmony_ci        fd_out.write(text)
15354568cb3Sopenharmony_ci
15454568cb3Sopenharmony_ci    return 0
15554568cb3Sopenharmony_ci
15654568cb3Sopenharmony_cidef write_tail(out_file):
15754568cb3Sopenharmony_ci    with codecs.open(out_file, 'a+') as fd_out:
15854568cb3Sopenharmony_ci
15954568cb3Sopenharmony_ci        text = '\n#endif\n'
16054568cb3Sopenharmony_ci
16154568cb3Sopenharmony_ci        fd_out.write(text)
16254568cb3Sopenharmony_ci
16354568cb3Sopenharmony_ci    return 0
16454568cb3Sopenharmony_ci
16554568cb3Sopenharmony_cidef kconfig2macro(in_file, out_file, flag):
16654568cb3Sopenharmony_ci    header_type = headerType(in_file)
16754568cb3Sopenharmony_ci    if header_type == HEADER_TYPE_INVALID:
16854568cb3Sopenharmony_ci        return -10
16954568cb3Sopenharmony_ci
17054568cb3Sopenharmony_ci    ret = write_header(out_file)
17154568cb3Sopenharmony_ci    if ret != 0:
17254568cb3Sopenharmony_ci        return ret
17354568cb3Sopenharmony_ci
17454568cb3Sopenharmony_ci    ret = write_config(in_file, out_file, flag)
17554568cb3Sopenharmony_ci    if ret != 0:
17654568cb3Sopenharmony_ci        return ret
17754568cb3Sopenharmony_ci
17854568cb3Sopenharmony_ci    ret = write_external_macro(out_file)
17954568cb3Sopenharmony_ci    if ret != 0:
18054568cb3Sopenharmony_ci        return ret
18154568cb3Sopenharmony_ci
18254568cb3Sopenharmony_ci    ret = write_c_external_tail(out_file)
18354568cb3Sopenharmony_ci    if ret != 0:
18454568cb3Sopenharmony_ci        return ret
18554568cb3Sopenharmony_ci
18654568cb3Sopenharmony_ci    ret = write_tail(out_file)
18754568cb3Sopenharmony_ci    if ret != 0:
18854568cb3Sopenharmony_ci        return ret
18954568cb3Sopenharmony_ci
19054568cb3Sopenharmony_ci    return 0
19154568cb3Sopenharmony_cidef do_cmd(argv, flag):
19254568cb3Sopenharmony_ci    in_file = ".config"
19354568cb3Sopenharmony_ci    out_file = "config.h"
19454568cb3Sopenharmony_ci
19554568cb3Sopenharmony_ci    try:
19654568cb3Sopenharmony_ci        opts, args = getopt.getopt(argv,"ehf:o:",["help", "inFile=", "outFile="])
19754568cb3Sopenharmony_ci    except getopt.GetoptError:
19854568cb3Sopenharmony_ci        cmd_help()
19954568cb3Sopenharmony_ci        return -1
20054568cb3Sopenharmony_ci
20154568cb3Sopenharmony_ci    if len(args) > 0:
20254568cb3Sopenharmony_ci        cmd_help()
20354568cb3Sopenharmony_ci        return -2
20454568cb3Sopenharmony_ci
20554568cb3Sopenharmony_ci    for opt, arg in opts:
20654568cb3Sopenharmony_ci        if opt in ('-h', '--help'):
20754568cb3Sopenharmony_ci            cmd_help()
20854568cb3Sopenharmony_ci            return 0
20954568cb3Sopenharmony_ci        elif opt in ('-e'):
21054568cb3Sopenharmony_ci            flag = True
21154568cb3Sopenharmony_ci        elif opt in ('-f', '--inFile'):
21254568cb3Sopenharmony_ci            in_file = arg
21354568cb3Sopenharmony_ci        elif opt in ('-o', '--outFile'):
21454568cb3Sopenharmony_ci            out_file = arg
21554568cb3Sopenharmony_ci        else:
21654568cb3Sopenharmony_ci            logging.error("ERROR: Invalid Input %s", opt)
21754568cb3Sopenharmony_ci            cmd_help()
21854568cb3Sopenharmony_ci            return -3
21954568cb3Sopenharmony_ci
22054568cb3Sopenharmony_ci    if os.path.exists(in_file) == False:
22154568cb3Sopenharmony_ci        logging.info("config input file <%s> doesn't exist", in_file)
22254568cb3Sopenharmony_ci        return -4
22354568cb3Sopenharmony_ci
22454568cb3Sopenharmony_ci    return kconfig2macro(in_file, out_file, flag)
22554568cb3Sopenharmony_ci
22654568cb3Sopenharmony_ci
22754568cb3Sopenharmony_ciif __name__ == "__main__":
22854568cb3Sopenharmony_ci    res = do_cmd(sys.argv[1:], False)
22954568cb3Sopenharmony_ci    sys.exit(res)
230