1#!/usr/bin/env python3
2
3# Copyright 2023 NXP
4# All rights reserved.
5#
6# SPDX-License-Identifier: BSD-3-Clause
7
8import os
9
10def write_file(filename, content, mode="w"):
11    with open(filename, mode, encoding='utf-8') as f:
12        f.write(content)
13
14    if not os.path.exists(filename):
15        return "NA"
16    else:
17        return os.path.abspath(filename)
18
19def read_file(filename):
20    content = ""
21    with open(filename, "r", encoding='utf-8') as f:
22        content = f.read()
23
24    return content
25
26def make_board_name(config_data):
27
28    board = ""
29    core = ""
30    for line in config_data.splitlines():
31        if line and not line.startswith("#"):
32            name, val = line[4:-1].split(" ", 1)
33            if name == "CONFIG_BOARD":
34                board = val
35            elif name == "CONFIG_CORE_ID":
36                core = val
37
38    board_name = board.upper()
39    if core:
40        board_name = "%s_%s"%(board_name,core.upper())
41
42    return board_name
43
44def mcux_create_config_cmake(config_out,kconfig_namespace='CONFIG_MCUX'):
45    newlines = "set(CONFIG_COMPILER gcc)\nset(CONFIG_TOOLCHAIN armgcc)\n#config.cmake generated from .config".splitlines()
46
47    config_data = read_file(config_out)
48    config_cmake = os.path.join(os.path.dirname(config_out), "config.cmake")
49    for line in config_data.splitlines():
50        # if line and line[:7] == "CONFIG_" and "MCUX_Family" not in line:
51         if line and line[:7] == "CONFIG_":
52            name, val = line.split("=")
53
54            if val == 'y':
55                if "%s_HAS"%kconfig_namespace not in name:
56                    newlines.append('set({} true)'.format(name.replace("%s_"%kconfig_namespace, "CONFIG_")))
57            else:
58                newlines.append('set({} {})'.format(name.replace("%s_"%kconfig_namespace, "CONFIG_"), val))
59
60    if os.path.exists(config_cmake):
61        config_cmake_old = config_cmake.replace("config.cmake","config.cmake.old")
62        if os.path.exists(config_cmake_old):
63            os.remove(config_cmake_old)
64        os.rename(config_cmake, config_cmake_old)
65
66    write_file(config_cmake,'\n'.join(newlines))
67
68def mcux_create_config_from_cmake(config_cmake,kconfig_namespace='CONFIG_MCUX'):
69    newlines = []
70    config_data = read_file(config_cmake)
71    config_dot = os.path.join(os.path.dirname(config_cmake), ".config")
72    for line in config_data.splitlines():
73        if line and not line.startswith("#"):
74            # print (line)
75            # print (line[4:-1].split(" "))
76            name, val = line[4:-1].split(" ", 1)
77            if val == "true":
78                newlines.append('{}={}'.format(name.replace("CONFIG_","%s_"%kconfig_namespace),'y'))
79            else:
80                newlines.append('{}="{}"'.format(name.replace("CONFIG_","%s_"%kconfig_namespace),val))
81
82    if "CONFIG_USE_BOARD" not in config_data:
83        board_name = make_board_name(config_data)
84        newlines.append("%s_USE_BOARD_%s=y"%(kconfig_namespace, board_name))
85
86    if os.path.exists(config_dot):
87        os.remove(config_dot)
88
89    write_file(config_dot,'\n'.join(newlines))
90
91    return config_dot
92