refactor(tool): 配置脚本重构
body python脚本结构重构
This commit is contained in:
26
kconfig.py
26
kconfig.py
@@ -9,28 +9,10 @@
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
import re
|
import re
|
||||||
|
import logging
|
||||||
|
from kconfiglib import Kconfig
|
||||||
|
|
||||||
try:
|
logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s')
|
||||||
from tool import install_package
|
|
||||||
except ImportError:
|
|
||||||
exit(1)
|
|
||||||
|
|
||||||
try:
|
|
||||||
from kconfiglib import Kconfig
|
|
||||||
except ImportError:
|
|
||||||
install_package('kconfiglib')
|
|
||||||
from kconfiglib import Kconfig
|
|
||||||
|
|
||||||
try:
|
|
||||||
import curses
|
|
||||||
except ImportError:
|
|
||||||
install_package('windows-curses')
|
|
||||||
import curses
|
|
||||||
|
|
||||||
try:
|
|
||||||
from tool import log_print
|
|
||||||
except ImportError:
|
|
||||||
exit(1)
|
|
||||||
|
|
||||||
|
|
||||||
def generate_config_file(kconfig_file, config_in, config_out, header_out):
|
def generate_config_file(kconfig_file, config_in, config_out, header_out):
|
||||||
@@ -68,7 +50,7 @@ def generate_config_file(kconfig_file, config_in, config_out, header_out):
|
|||||||
header_file.write("#endif /* _MR_CONFIG_H_ */\n")
|
header_file.write("#endif /* _MR_CONFIG_H_ */\n")
|
||||||
|
|
||||||
header_file.close()
|
header_file.close()
|
||||||
log_print('success', "mr-library config file make success")
|
logging.info("Build mr-library config file successfully")
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|||||||
389
tool.py
389
tool.py
@@ -9,109 +9,72 @@
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import pip
|
import logging
|
||||||
import argparse
|
import argparse
|
||||||
import subprocess
|
import subprocess
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
def log_print(level, text):
|
|
||||||
# Log level colors
|
|
||||||
LEVEL_COLORS = {
|
|
||||||
'error': '\033[31m',
|
|
||||||
'success': '\033[32m',
|
|
||||||
'warning': '\033[33m',
|
|
||||||
'info': '\033[34m',
|
|
||||||
}
|
|
||||||
RESET_COLOR = '\033[0m'
|
|
||||||
# Log level name
|
|
||||||
LEVEL_NAME = {
|
|
||||||
'error': 'ERROR',
|
|
||||||
'success': 'SUCCESS',
|
|
||||||
'warning': 'WARNING',
|
|
||||||
'info': 'INFO',
|
|
||||||
}
|
|
||||||
print(LEVEL_COLORS[level] + LEVEL_NAME[level] + ': ' + text + RESET_COLOR)
|
|
||||||
|
|
||||||
|
|
||||||
def install_package(package):
|
|
||||||
log_print('info', "%s package installing..." % package)
|
|
||||||
pip.main(['install', package])
|
|
||||||
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from lxml import etree
|
from lxml import etree
|
||||||
except ImportError:
|
except ImportError:
|
||||||
install_package('lxml')
|
subprocess.run(['pip', 'install', 'lxml'], check=True)
|
||||||
from lxml import etree
|
from lxml import etree
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from kconfiglib import Kconfig
|
from kconfiglib import Kconfig
|
||||||
except ImportError:
|
except ImportError:
|
||||||
install_package('kconfiglib')
|
subprocess.run(['pip', 'install', 'kconfiglib'], check=True)
|
||||||
from kconfiglib import Kconfig
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import curses
|
import curses
|
||||||
except ImportError:
|
except ImportError:
|
||||||
install_package('windows-curses')
|
subprocess.run(['pip', 'install', 'windows-curses'], check=True)
|
||||||
|
|
||||||
|
logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s')
|
||||||
|
|
||||||
|
|
||||||
class MDK5:
|
class MDK5:
|
||||||
|
|
||||||
def __init__(self, mdk_path):
|
def __init__(self, mdk_path):
|
||||||
# Get MDK project file
|
self.path, self.file = self._find_uvprojx_file(mdk_path)
|
||||||
mdk_file = []
|
if self.file:
|
||||||
for root, dirs, fs in os.walk(mdk_path):
|
self.exist = True
|
||||||
for f in fs:
|
self.tree = etree.parse(self.file)
|
||||||
if f.endswith('.uvprojx'):
|
|
||||||
mdk_file = os.path.join(root, f)
|
|
||||||
break
|
|
||||||
if mdk_file:
|
|
||||||
break
|
|
||||||
# Check mdk file, init self
|
|
||||||
if mdk_file:
|
|
||||||
self.state = "ok"
|
|
||||||
self.path = os.path.dirname(mdk_file)
|
|
||||||
self.file = mdk_file
|
|
||||||
self.tree = etree.parse(mdk_file)
|
|
||||||
self.root = self.tree.getroot()
|
self.root = self.tree.getroot()
|
||||||
else:
|
else:
|
||||||
self.state = "not found"
|
self.exist = False
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _find_uvprojx_file(mdk_path):
|
||||||
|
for root, dirs, files in os.walk(mdk_path):
|
||||||
|
for file in files:
|
||||||
|
if file.endswith('.uvprojx'):
|
||||||
|
return Path(root), Path(root) / file
|
||||||
|
return None, None
|
||||||
|
|
||||||
def add_include_path(self, path):
|
def add_include_path(self, path):
|
||||||
# Fix path
|
path = Path(path).relative_to(self.path).as_posix()
|
||||||
path = os.path.relpath(path, self.path).replace('\\', '/')
|
|
||||||
# Add path
|
|
||||||
inc_path = self.tree.xpath("//Cads/VariousControls/IncludePath")[0]
|
inc_path = self.tree.xpath("//Cads/VariousControls/IncludePath")[0]
|
||||||
exist_paths = inc_path.text.split(';')
|
exist_paths = inc_path.text.split(';')
|
||||||
if path not in exist_paths:
|
if path not in exist_paths:
|
||||||
inc_path.text += f";{path}"
|
inc_path.text += f";{path}"
|
||||||
log_print('info', "include %s" % path)
|
logging.info(f"include {path}")
|
||||||
|
|
||||||
def add_include_paths(self, paths):
|
|
||||||
for path in paths:
|
|
||||||
self.add_include_path(path)
|
|
||||||
|
|
||||||
def add_file_to_group(self, name, file):
|
def add_file_to_group(self, name, file):
|
||||||
# Fix name and files
|
file = Path(file).relative_to(self.path).as_posix()
|
||||||
name = name.replace('\\', '/')
|
|
||||||
file = os.path.relpath(file, self.path).replace('\\', '/')
|
|
||||||
# Check name and file
|
|
||||||
if name is None or file is None:
|
|
||||||
return
|
|
||||||
# Add group
|
|
||||||
groups_node = self.tree.find('//Groups')
|
groups_node = self.tree.find('//Groups')
|
||||||
group_node = groups_node.find(f"./Group[GroupName='{name}']")
|
group_node = groups_node.find(f"./Group[GroupName='{name}']")
|
||||||
if group_node is None:
|
if group_node is None:
|
||||||
group_node = etree.SubElement(groups_node, "Group")
|
group_node = etree.SubElement(groups_node, "Group")
|
||||||
group_name_node = etree.SubElement(group_node, "GroupName")
|
group_name_node = etree.SubElement(group_node, "GroupName")
|
||||||
group_name_node.text = name
|
group_name_node.text = name
|
||||||
# Add files
|
|
||||||
files_node = group_node.find("Files")
|
files_node = group_node.find("Files")
|
||||||
if files_node is None:
|
if files_node is None:
|
||||||
files_node = etree.SubElement(group_node, "Files")
|
files_node = etree.SubElement(group_node, "Files")
|
||||||
# Add file
|
# Add file
|
||||||
file_node = files_node.find(f"./File[FileName='{os.path.basename(file)}']")
|
file_node = files_node.find(
|
||||||
|
f"./File[FileName='{os.path.basename(file)}']")
|
||||||
if file_node is None:
|
if file_node is None:
|
||||||
file_node = etree.SubElement(files_node, "File")
|
file_node = etree.SubElement(files_node, "File")
|
||||||
file_name_node = file_node.find(f"./FileName")
|
file_name_node = file_node.find(f"./FileName")
|
||||||
@@ -136,166 +99,125 @@ class MDK5:
|
|||||||
file_extension = os.path.splitext(file_name_node.text)[1]
|
file_extension = os.path.splitext(file_name_node.text)[1]
|
||||||
file_type = file_type_map.get(file_extension, '9')
|
file_type = file_type_map.get(file_extension, '9')
|
||||||
file_type_node.text = file_type
|
file_type_node.text = file_type
|
||||||
log_print('info', "add %s" % file)
|
logging.info(f"=> {file}")
|
||||||
|
|
||||||
def add_file(self, file):
|
def add_file(self, file):
|
||||||
name = os.path.dirname(os.path.relpath(file, self.path).replace('\\', '/')).replace('../', '')
|
group_name = Path(file).relative_to(
|
||||||
self.add_file_to_group(name, file)
|
self.path).parent.as_posix().replace('../', '')
|
||||||
|
file = Path(file).relative_to(self.path).as_posix()
|
||||||
|
self.add_file_to_group(group_name, file)
|
||||||
|
|
||||||
def add_files(self, files):
|
def add_files(self, files):
|
||||||
for file in files:
|
for file in files:
|
||||||
self.add_file(file)
|
self.add_file(file)
|
||||||
|
|
||||||
def use_gnu(self, enable=True):
|
def use_gnu(self, enable=True):
|
||||||
# Check uAC6
|
|
||||||
ac6_node = self.tree.find('//Target/uAC6')
|
ac6_node = self.tree.find('//Target/uAC6')
|
||||||
if ac6_node is None:
|
if ac6_node is None:
|
||||||
log_print('error', "GNU use failed")
|
logging.error("Config GNU failed")
|
||||||
return
|
return
|
||||||
# Use GNU
|
|
||||||
if ac6_node.text == '0':
|
if ac6_node.text == '0':
|
||||||
# Get uGnu
|
|
||||||
gnu_node = self.tree.find('//Cads/uGnu')
|
gnu_node = self.tree.find('//Cads/uGnu')
|
||||||
gnu_text = '1' if enable else '0'
|
gnu_text = '1' if enable else '0'
|
||||||
else:
|
else:
|
||||||
# Get gnu-c99
|
|
||||||
gnu_node = self.tree.find('//Cads/v6Lang')
|
gnu_node = self.tree.find('//Cads/v6Lang')
|
||||||
gnu_text = '4' if enable else '3'
|
gnu_text = '4' if enable else '3'
|
||||||
# Set gnu
|
|
||||||
if gnu_node is not None:
|
if gnu_node is not None:
|
||||||
if gnu_node.text != gnu_text:
|
if gnu_node.text != gnu_text:
|
||||||
gnu_node.text = gnu_text
|
gnu_node.text = gnu_text
|
||||||
log_print('info', "use GNU")
|
logging.info("Config GNU successfully")
|
||||||
|
|
||||||
def save(self):
|
def save(self):
|
||||||
self.tree.write(self.file, pretty_print=True, encoding="utf-8", xml_declaration=True)
|
self.tree.write(self.file, pretty_print=True, encoding="utf-8",
|
||||||
log_print('success', "project build success")
|
xml_declaration=True)
|
||||||
|
logging.info("Build project successfully")
|
||||||
|
|
||||||
|
|
||||||
class Eclipse:
|
class Eclipse:
|
||||||
def __init__(self, eclipse_path):
|
def __init__(self, eclipse_path):
|
||||||
# Get eclipse project file
|
self.path, self.file = self._find_cproject_file(eclipse_path)
|
||||||
eclipse_file = []
|
if self.file:
|
||||||
for root, dirs, fs in os.walk(eclipse_path):
|
self.exist = True
|
||||||
for f in fs:
|
self.tree = etree.parse(self.file)
|
||||||
if f.endswith('.cproject'):
|
|
||||||
eclipse_file = os.path.join(root, f)
|
|
||||||
break
|
|
||||||
if eclipse_file:
|
|
||||||
break
|
|
||||||
# Check eclipse file, init self
|
|
||||||
if eclipse_file:
|
|
||||||
self.state = "ok"
|
|
||||||
self.path = os.path.dirname(eclipse_file)
|
|
||||||
self.file = eclipse_file
|
|
||||||
self.tree = etree.parse(eclipse_file)
|
|
||||||
self.root = self.tree.getroot()
|
self.root = self.tree.getroot()
|
||||||
else:
|
else:
|
||||||
self.state = "not found"
|
self.exist = False
|
||||||
|
|
||||||
def add_include_path(self, path):
|
@staticmethod
|
||||||
# Fix path
|
def _find_cproject_file(eclipse_path):
|
||||||
path = os.path.relpath(path, self.file).replace('\\', '/')
|
for root, dirs, files in os.walk(eclipse_path):
|
||||||
# Find all include path node
|
for file in files:
|
||||||
inc_path_nodes = self.tree.findall(".//option[@valueType='includePath']")
|
if file.endswith('.cproject'):
|
||||||
for inc_path_node in inc_path_nodes:
|
return Path(root), Path(root) / file
|
||||||
inc_path_node_id = inc_path_node.get('id')
|
return None, None
|
||||||
# Check path node
|
|
||||||
if 'c.compiler' in inc_path_node_id and 'include' in inc_path_node_id:
|
|
||||||
# Add path
|
|
||||||
list_option = inc_path_node.find(f".//listOptionValue[@value='{path}']")
|
|
||||||
if list_option is None:
|
|
||||||
list_option = etree.SubElement(inc_path_node, "listOptionValue")
|
|
||||||
list_option.set('builtIn', "false")
|
|
||||||
list_option.set('value', path)
|
|
||||||
log_print('info', "include %s" % path)
|
|
||||||
break
|
|
||||||
|
|
||||||
def use_auto_init(self):
|
def use_auto_init(self):
|
||||||
# Find ld file
|
ld_file = self._find_ld_file(self.path)
|
||||||
ld_file = []
|
|
||||||
for root, dirs, files in os.walk(self.path):
|
|
||||||
for file in files:
|
|
||||||
if file.endswith(".ld"):
|
|
||||||
ld_file = os.path.join(root, file)
|
|
||||||
break
|
|
||||||
if ld_file:
|
|
||||||
break
|
|
||||||
# Check ld file
|
|
||||||
if ld_file:
|
if ld_file:
|
||||||
# Use auto init
|
with ld_file.open() as fr:
|
||||||
with open(ld_file) as fr:
|
|
||||||
content = fr.read()
|
content = fr.read()
|
||||||
pos = content.find('.text :')
|
pos = content.find('.text :')
|
||||||
# Check pos
|
|
||||||
if pos == -1:
|
if pos == -1:
|
||||||
log_print('warning', "use auto init failed, '.text' not found")
|
|
||||||
return
|
return
|
||||||
# Check auto init is existed
|
if '/* mr-library */' not in content:
|
||||||
if content.find('/* mr-library */') == -1:
|
|
||||||
# Find pos offset
|
|
||||||
pos_offset = content[pos:].find('}')
|
pos_offset = content[pos:].find('}')
|
||||||
# Check pos offset
|
|
||||||
if pos_offset == -1:
|
if pos_offset == -1:
|
||||||
log_print('warning', "use auto init failed, '.text' not found")
|
|
||||||
return
|
return
|
||||||
pos = pos + pos_offset
|
pos += pos_offset
|
||||||
# Use auto init
|
link_config = """
|
||||||
with open(ld_file, 'w') as fw:
|
/* mr-library */
|
||||||
front = content[:pos]
|
. = ALIGN(4);
|
||||||
link_config = """
|
KEEP(*(SORT(mr_auto_init.*)))
|
||||||
/* mr-library */
|
KEEP(*(SORT(mr_msh_cmd.*)))
|
||||||
. = ALIGN(4);
|
. = ALIGN(4);
|
||||||
KEEP(*(SORT(mr_auto_init.*)))
|
"""
|
||||||
KEEP(*(SORT(mr_msh_cmd.*)))
|
with ld_file.open('w') as fw:
|
||||||
. = ALIGN(4);
|
fw.write(content[:pos] + link_config + content[pos:])
|
||||||
"""
|
logging.info("Config auto-init successfully")
|
||||||
back = content[pos:]
|
|
||||||
fw.write(front + link_config + back)
|
|
||||||
fw.close()
|
|
||||||
fr.close()
|
|
||||||
log_print('info', "use auto init")
|
|
||||||
else:
|
else:
|
||||||
log_print('warning', "use auto init failed, '.ld' not found")
|
logging.warning("Config auto-init failed, '.ld' not found")
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _find_ld_file(path):
|
||||||
|
for root, dirs, files in os.walk(path):
|
||||||
|
for file in files:
|
||||||
|
if file.endswith(".ld"):
|
||||||
|
return Path(root) / file
|
||||||
|
return None
|
||||||
|
|
||||||
|
def add_include_path(self, path):
|
||||||
|
path = Path(path).relative_to(self.path).as_posix()
|
||||||
|
path_nodes = self.tree.findall(".//option[@valueType='includePath']")
|
||||||
|
for path_node in path_nodes:
|
||||||
|
path_node_id = path_node.get('id')
|
||||||
|
if 'c.compiler' in path_node_id and 'include' in path_node_id:
|
||||||
|
option = path_node.find(f".//listOptionValue[@value='{path}']")
|
||||||
|
if option is None:
|
||||||
|
etree.SubElement(path_node, "listOptionValue",
|
||||||
|
builtIn="false", value=path)
|
||||||
|
logging.info(f"=> {path}")
|
||||||
|
break
|
||||||
|
|
||||||
def save(self):
|
def save(self):
|
||||||
self.tree.write(self.file, pretty_print=True, encoding="utf-8", xml_declaration=True)
|
self.tree.write(self.file, pretty_print=True, encoding="utf-8",
|
||||||
log_print('success', "project build success")
|
xml_declaration=True)
|
||||||
|
logging.info("Build project successfully")
|
||||||
|
|
||||||
|
|
||||||
class MrLib:
|
class MrLib:
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.path = os.getcwd()
|
self.path = Path.cwd()
|
||||||
self.c_files = []
|
self.c_files = list(self.path.rglob('*.c'))
|
||||||
self.h_files = []
|
self.h_files = list(self.path.rglob('*.h'))
|
||||||
for root, dirs, files in os.walk(self.path):
|
self.proj_path = self.path.parent
|
||||||
for f in files:
|
|
||||||
if f.endswith(".c") or f.endswith(".cpp"):
|
|
||||||
self.c_files.append(os.path.join(root, f))
|
|
||||||
elif f.endswith(".h"):
|
|
||||||
self.h_files.append(os.path.join(root, f))
|
|
||||||
self.proj_path = os.path.dirname(self.path)
|
|
||||||
|
|
||||||
def generate_include_file(self):
|
def generate_include_file(self):
|
||||||
header_out = os.path.join(self.path, "include/mr_lib.h").replace('\\', '/')
|
header_out = self.path / "include/mr_lib.h"
|
||||||
include_path = []
|
include_path = next((p for p in self.path.glob('**/include')), None)
|
||||||
for root, dirs, files in os.walk(self.path):
|
header_files = [f.relative_to(include_path).as_posix() for f in
|
||||||
for d in dirs:
|
include_path.rglob('*.h')] if include_path else []
|
||||||
if d == "include":
|
|
||||||
include_path = os.path.join(root, d)
|
|
||||||
break
|
|
||||||
if include_path:
|
|
||||||
break
|
|
||||||
|
|
||||||
header_files = []
|
with open(header_out, "w+") as header_file:
|
||||||
for root, dirs, files in os.walk(include_path):
|
|
||||||
for file in files:
|
|
||||||
if file.endswith('.h'):
|
|
||||||
f = os.path.relpath(os.path.join(root, file), include_path).replace('\\', '/')
|
|
||||||
header_files.append(f)
|
|
||||||
|
|
||||||
with open(header_out, 'r+') as header_file:
|
|
||||||
header_file.truncate(0)
|
header_file.truncate(0)
|
||||||
header_file.seek(0)
|
header_file.seek(0)
|
||||||
|
|
||||||
@@ -310,7 +232,7 @@ class MrLib:
|
|||||||
# Link include
|
# Link include
|
||||||
for hf in header_files:
|
for hf in header_files:
|
||||||
if hf != os.path.basename(header_out):
|
if hf != os.path.basename(header_out):
|
||||||
header_file.write('#include "../mr-library/include/' + hf + '"\n')
|
header_file.write('#include <include/' + hf + '>\n')
|
||||||
|
|
||||||
# Add the micro
|
# Add the micro
|
||||||
header_file.write("\n#ifdef __cplusplus\n")
|
header_file.write("\n#ifdef __cplusplus\n")
|
||||||
@@ -318,121 +240,98 @@ class MrLib:
|
|||||||
header_file.write("#endif /* __cplusplus */\n\n")
|
header_file.write("#endif /* __cplusplus */\n\n")
|
||||||
header_file.write("#endif /* _MR_LIB_H_ */\n")
|
header_file.write("#endif /* _MR_LIB_H_ */\n")
|
||||||
|
|
||||||
header_file.close()
|
logging.info(f"Generate include file successfully")
|
||||||
log_print('success', "mr-library include file make success")
|
|
||||||
|
|
||||||
|
|
||||||
def show_logo():
|
def show_logo():
|
||||||
print(" __ __ _ _ _ ")
|
print("__ __ _ _ _ "
|
||||||
print("| \\/ | _ __ | | (_) | |__ _ __ __ _ _ __ _ _")
|
" ")
|
||||||
print("| |\\/| | | '__| _____ | | | | | '_ \\ | '__| / _` | | '__| | | | |")
|
print(
|
||||||
print("| | | | | | |_____| | | | | | |_) | | | | (_| | | | | |_| |")
|
"| \\/ | _ __ | | (_) | |__ _ __ __ _ _ __ _ _")
|
||||||
print("|_| |_| |_| |_| |_| |_.__/ |_| \\__,_| |_| \\__, |")
|
print(
|
||||||
print(" |___/")
|
"| |\\/| | | '__| _____ | | | | | '_ \\ | '__| / _` | | '__| | | | |")
|
||||||
|
print(
|
||||||
|
"| | | | | | |_____| | | | | | |_) | | | | (_| | | | | |_| |")
|
||||||
|
print(
|
||||||
|
"|_| |_| |_| |_| |_| |_.__/ |_| \\__,_| |_| \\__, |")
|
||||||
|
print(
|
||||||
|
" |___/")
|
||||||
|
|
||||||
|
|
||||||
def show_license():
|
def show_license():
|
||||||
license_file = os.path.join(os.path.dirname(__file__), "LICENSE")
|
license_file = Path(__file__).parent / "LICENSE"
|
||||||
try:
|
with license_file.open() as f:
|
||||||
with open(license_file) as fr:
|
print(f.read())
|
||||||
print(fr.read())
|
|
||||||
except OSError:
|
|
||||||
log_print('warning',
|
|
||||||
"This software is provided subject to the terms of the Apache License 2.0, the full text of which "
|
|
||||||
"is not currently available due to missing license documentation. By continuing to use the "
|
|
||||||
"Software, you agree to be bound by the terms of the Apache License 2.0. The full license text is "
|
|
||||||
"available at https://www.apache.org/licenses/LICENSE-2.0. We advise you to review the license terms "
|
|
||||||
"in full before use to ensure you understand and agree to be bound by all provisions contained "
|
|
||||||
"therein.")
|
|
||||||
log_print('warning',
|
|
||||||
"本软件根据Apache许可证2.0版本条款提供,由于许可证文件缺失,当前无法获取完整许可内容。继续使用本软件, "
|
|
||||||
"代表您同意接受并遵守Apache许可证2.0版本的所有条款。完整许可证可在https://www.apache.org/licenses/LICENSE-2.0"
|
|
||||||
"查看。建议您在使用前全面复核许可证内容, 以确保完全理解并同意接受其中的所有规定。")
|
|
||||||
|
|
||||||
|
|
||||||
def build_mdk(mr_proj_path, include_path, c_files):
|
def build_mdk(proj_path, include_path, c_files):
|
||||||
# MDK project
|
mdk_proj = MDK5(proj_path)
|
||||||
mdk_proj = MDK5(mr_proj_path)
|
|
||||||
# Include path
|
|
||||||
mdk_proj.add_include_path(include_path)
|
mdk_proj.add_include_path(include_path)
|
||||||
# Add all c files
|
|
||||||
mdk_proj.add_files(c_files)
|
mdk_proj.add_files(c_files)
|
||||||
# Use gnu
|
|
||||||
mdk_proj.use_gnu(True)
|
mdk_proj.use_gnu(True)
|
||||||
# Save
|
|
||||||
mdk_proj.save()
|
mdk_proj.save()
|
||||||
|
|
||||||
|
|
||||||
def build_eclipse(mr_proj_path, path):
|
def build_eclipse(proj_path, path):
|
||||||
# Eclipse project
|
eclipse_proj = Eclipse(proj_path)
|
||||||
eclipse_proj = Eclipse(mr_proj_path)
|
|
||||||
# Include path
|
|
||||||
eclipse_proj.add_include_path(path)
|
eclipse_proj.add_include_path(path)
|
||||||
# Use auto init
|
|
||||||
eclipse_proj.use_auto_init()
|
eclipse_proj.use_auto_init()
|
||||||
# Save
|
|
||||||
eclipse_proj.save()
|
eclipse_proj.save()
|
||||||
|
|
||||||
|
|
||||||
def menuconfig():
|
def menuconfig():
|
||||||
|
devnull_path = os.devnull if os.name == 'nt' else '/dev/null'
|
||||||
try:
|
try:
|
||||||
if os.name == 'nt':
|
with open(devnull_path, 'w') as devnull:
|
||||||
devnull = open(os.devnull, 'w')
|
subprocess.run(["menuconfig"], stdout=devnull, stderr=devnull,
|
||||||
else:
|
check=True)
|
||||||
devnull = open('/dev/null', 'w')
|
|
||||||
subprocess.run(["menuconfig"], stdout=devnull, stderr=devnull, check=True)
|
|
||||||
devnull.close()
|
|
||||||
except subprocess.CalledProcessError:
|
except subprocess.CalledProcessError:
|
||||||
log_print('error', "menuconfig failed")
|
logging.error("Config menuconfig failed")
|
||||||
exit(1)
|
exit(1)
|
||||||
try:
|
try:
|
||||||
subprocess.run(["python", "kconfig.py"], check=True)
|
subprocess.run(["python", "kconfig.py"], check=True)
|
||||||
except subprocess.CalledProcessError:
|
except subprocess.CalledProcessError:
|
||||||
log_print('error', "menuconfig failed, 'kconfig.py' not found")
|
logging.error("Config menuconfig failed, 'kconfig.py' not found")
|
||||||
exit(1)
|
exit(1)
|
||||||
|
|
||||||
|
|
||||||
def build():
|
def build():
|
||||||
mr_lib = MrLib()
|
mr_lib = MrLib()
|
||||||
|
|
||||||
# Build project
|
|
||||||
mdk5 = MDK5(mr_lib.proj_path)
|
|
||||||
eclipse = Eclipse(mr_lib.proj_path)
|
|
||||||
if mdk5.state == "ok":
|
|
||||||
build_mdk(mr_lib.proj_path, mr_lib.path, mr_lib.c_files)
|
|
||||||
elif eclipse.state == "ok":
|
|
||||||
build_eclipse(mr_lib.proj_path, mr_lib.path)
|
|
||||||
else:
|
|
||||||
log_print('error', "Project not found(MDK5 or Eclipse)")
|
|
||||||
exit(1)
|
|
||||||
|
|
||||||
# Generate include file
|
|
||||||
mr_lib.generate_include_file()
|
mr_lib.generate_include_file()
|
||||||
|
|
||||||
|
mdk5 = MDK5(mr_lib.proj_path)
|
||||||
|
if mdk5.exist:
|
||||||
|
build_mdk(mr_lib.proj_path, mr_lib.path, mr_lib.c_files)
|
||||||
|
else:
|
||||||
|
eclipse = Eclipse(mr_lib.proj_path)
|
||||||
|
if eclipse.exist:
|
||||||
|
build_eclipse(mr_lib.proj_path, mr_lib.path)
|
||||||
|
else:
|
||||||
|
logging.error("Found project(MDK5 or Eclipse) failed")
|
||||||
|
exit(1)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
# Show logo
|
|
||||||
show_logo()
|
show_logo()
|
||||||
|
|
||||||
# Parse arguments
|
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument("-m", "--menuconfig", action="store_true", help="Run menuconfig")
|
parser.add_argument("-m", "--menuconfig", action="store_true",
|
||||||
parser.add_argument("-b", "--build", action="store_true", help="Build project")
|
help="Run menuconfig")
|
||||||
parser.add_argument("-lic", "--license", action="store_true", help="Show license")
|
parser.add_argument("-b", "--build", action="store_true",
|
||||||
parser.add_argument("-gli", "--generate_lib_include_file", action="store_true",
|
help="Build project")
|
||||||
|
parser.add_argument("-l", "--license", action="store_true",
|
||||||
|
help="Show license")
|
||||||
|
parser.add_argument("-g", "--generate",
|
||||||
|
action="store_true",
|
||||||
help="Generate library include file")
|
help="Generate library include file")
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
# Build
|
|
||||||
if args.build:
|
if args.build:
|
||||||
build()
|
build()
|
||||||
# Menuconfig
|
|
||||||
if args.menuconfig:
|
if args.menuconfig:
|
||||||
menuconfig()
|
menuconfig()
|
||||||
# Show license
|
|
||||||
if args.license:
|
if args.license:
|
||||||
show_license()
|
show_license()
|
||||||
# Generate library include file
|
if args.generate:
|
||||||
if args.generate_lib_include_file:
|
|
||||||
mr = MrLib()
|
mr = MrLib()
|
||||||
mr.generate_include_file()
|
mr.generate_include_file()
|
||||||
|
|||||||
Reference in New Issue
Block a user