import os
import re
from string import Template
import rtconfig
import shutil
import time
# version
MODULE_VER_NUM = 1
cproject_temp = """
"""
project_temp = """
__project_name_flag__
org.eclipse.cdt.managedbuilder.core.genmakebuilder
clean,full,incremental,
org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder
full,incremental,
org.eclipse.cdt.core.cnature
org.rt-thread.studio.rttnature
org.eclipse.cdt.managedbuilder.core.managedBuildNature
org.eclipse.cdt.managedbuilder.core.ScannerConfigNature
"""
projcfg_ini_temp = """#RT-Thread Studio Project Configuration
# $time
cfg_version=v3.0
board_name=
bsp_version=
bsp_path=
chip_name=
project_base_rtt_bsp=true
is_use_scons_build=true
hardware_adapter=
selected_rtt_version=latest
board_base_nano_proj=false
is_base_example_project=false
example_name=
project_type=rt-thread
os_branch=master
os_version=latest
project_name=$project_name
output_project_path=$output_project_path"""
eclipse_core_runtime_temp = """content-types/enabled=true
content-types/org.eclipse.cdt.core.asmSource/file-extensions=s
eclipse.preferences.version=1"""
makefile_targets_temp = """clean2:
\t-$(RM) $(CC_DEPS)$(C++_DEPS)$(C_UPPER_DEPS)$(CXX_DEPS)$(SECONDARY_FLASH)$(SECONDARY_SIZE)$(ASM_DEPS)$(S_UPPER_DEPS)$(C_DEPS)$(CPP_DEPS)
\t-$(RM) $(OBJS) *.elf
\t-@echo ' '
*.elf: $(wildcard ../linkscripts/*/*.lds) $(wildcard ../linkscripts/*/*/*.lds)"""
def get_mcu_info(uvproj_file_path):
if os.path.exists(uvproj_file_path):
with open(uvproj_file_path, mode='r') as f:
data = f.read()
result = re.search("(.*)", data)
if result:
return result.group(1)
else:
return "unknown"
else:
return "unknown"
def gen_makefile_targets(output_file_path):
try:
w_str = makefile_targets_temp
dir_name = os.path.dirname(output_file_path)
if not os.path.exists(dir_name):
os.makedirs(dir_name)
with open(output_file_path, 'w') as f:
f.write(w_str)
return True
except Exception as e:
print(e)
return False
def gen_org_eclipse_core_runtime_prefs(output_file_path):
try:
w_str = eclipse_core_runtime_temp
dir_name = os.path.dirname(output_file_path)
if not os.path.exists(dir_name):
os.makedirs(dir_name)
with open(output_file_path, 'w') as f:
f.write(w_str)
return True
except Exception as e:
print(e)
return False
def gen_cproject_file(output_file_path):
template_file_path = os.path.join(os.path.dirname(output_file_path), "template.cproject")
if os.path.exists(template_file_path):
try:
shutil.copy(template_file_path, output_file_path)
except Exception as e:
print(e)
return True
else:
CFLAGS = rtconfig.CFLAGS
AFLAGS = rtconfig.AFLAGS
LFLAGS = rtconfig.LFLAGS
if 'CXXFLAGS' in dir(rtconfig):
CXXFLAGS = rtconfig.CXXFLAGS
else:
CXXFLAGS = ""
if "-T" in LFLAGS:
items = str(LFLAGS).split()
t_index = items.index("-T")
items[t_index] = ""
items[t_index + 1] = ""
LFLAGS = " ".join(items)
try:
w_str = cproject_temp
if "a_misc_flag" in w_str:
w_str = w_str.replace("a_misc_flag", AFLAGS)
if "c_misc_flag" in w_str:
w_str = w_str.replace("c_misc_flag", CFLAGS)
if "cpp_misc_flag" in w_str:
w_str = w_str.replace("cpp_misc_flag", CXXFLAGS)
if "c_link_misc_flag" in w_str:
w_str = w_str.replace("c_link_misc_flag", LFLAGS)
if "cpp_link_misc_flag" in w_str:
w_str = w_str.replace("cpp_link_misc_flag", LFLAGS)
dir_name = os.path.dirname(output_file_path)
if not os.path.exists(dir_name):
os.makedirs(dir_name)
with open(output_file_path, 'w') as f:
f.write(w_str)
return True
except Exception as e:
return False
def gen_project_file(output_file_path):
try:
w_str = project_temp
dir_name = os.path.dirname(output_file_path)
if not os.path.exists(dir_name):
os.makedirs(dir_name)
with open(output_file_path, 'w') as f:
f.write(w_str)
return True
except Exception as e:
return False
def gen_projcfg_ini_file(chip_name, project_name, output_file_path):
try:
projcfg_file_tmp = Template(projcfg_ini_temp)
w_str = projcfg_file_tmp.substitute(time=time.strftime("%a %b %d %H:%M:%S %Y", time.localtime()),
project_name=project_name,
output_project_path=os.path.abspath(""))
dir_name = os.path.dirname(output_file_path)
if not os.path.exists(dir_name):
os.makedirs(dir_name)
with open(output_file_path, 'w') as f:
f.write(w_str)
return True
except Exception as e:
return False