1#
2# Copyright (c) 2006-2023, RT-Thread Development Team
3#
4# SPDX-License-Identifier: Apache-2.0
5#
6# Change Logs:
7# Date           Author       Notes
8# 2023-05-10     GuEe-GUI     the first version
9#
10
11import os, re
12
13from building import *
14
15__dtc_install_tip = """
16You should install dtc (devicetree compiler) in your system:
17    Linux:
18        Debian/Ubuntu: apt-get install device-tree-compiler
19        Arch/Manjaro: pacman -Sy dtc
20
21    MacOS:
22        brew install dtc
23
24    Windows (MinGW):
25        msys2: pacman -S dtc
26"""
27
28def __check_dtc(value):
29    if value != 0 and os.system("dtc -v") != 0:
30        print(__dtc_install_tip)
31
32def dts_to_dtb(RTT_ROOT, dts_list, options = "", include_paths = [], ignore_warning = []):
33    path = GetCurrentDir() + '/'
34    warning_ops = ""
35    for warning in ignore_warning:
36        warning_ops += " -W no-" + warning
37    for dts in dts_list:
38        dtb = dts.replace('.dts', '.dtb')
39        if not os.path.exists(path + dtb) or os.path.getmtime(path + dtb) < os.path.getmtime(path + dts):
40            tmp_dts = dts + '.tmp'
41            Preprocessing(dts, None, output = tmp_dts, CPPPATH=[RTT_ROOT + '/components/drivers/include'] + include_paths)
42            ret = os.system("dtc -I dts -O dtb -@ -A {} {} {} -o {}".format(warning_ops, options, path + tmp_dts, path + dtb))
43            __check_dtc(ret)
44            if os.path.exists(path + tmp_dts):
45                os.remove(path + tmp_dts)
46
47def dtb_to_dts(RTT_ROOT, dtb_name, dts_name = None, options = ""):
48    path = GetCurrentDir() + '/'
49    if dts_name == None:
50        dts_name = re.sub(r'\.dtb[o]*$', '.dts', dtb_name)
51    ret = os.system("dtc -I dtb -O dts {} {} -o {}".format(options, path + dtb_name, path + dts_name))
52    __check_dtc(ret)
53