手动构造 CMakeLists.txt ,自己构造项目名,CMAKE 版本,只做了添加c,cpp和h 文件添加到cmakelists中
import os
import re
m = {"cpp": 1, "c": 1, "h": 1}
def read_file():
file_list = []
current_directory = os.getcwd()
for root, dirs, files in os.walk(current_directory):
# print(root,dirs,files)
for file in files:
suffixIndex = file.rfind(".")
if suffixIndex == -1:
continue
if ("cmake-build-debug" not in root) and (m.get(file[suffixIndex + 1:].lower()) == 1):
file_path = os.path.join(root, file)
file_list.append(file_path.replace(current_directory + "/", ""))
return file_list
def read_cmakelists():
fp = open("CMakeLists.txt", "r", encoding="utf-8")
line = fp.read()
res = re.compile('add_executable\(([\s\S]*?)\)')
result = res.search(line).group(1)
result_list = [i.strip() for i in result.split("\n")]
add_list = []
for i in result_list:
add_list.extend([j.strip() for j in i.split(" ") if j != ""])
return add_list
def merge(list_a, list_b):
new_list = list_a
for i in list_b:
if i not in list_a:
new_list.append(i)
return new_list
def rewrite(write_list):
fp = open("CMakeLists.txt", "r", encoding="utf-8")
text_list = []
write_text = ""
while 1:
line = fp.readline()
if "add_executable" in line:
break
text_list.append(line)
fp.seek(0)
fp.close()
fps = open("CMakeLists.txt", "w", encoding="utf-8")
for text in text_list:
write_text += text
write_text+="add_executable("
write_text += " ".join(write_list)
write_text += "\n)"
fps.write(write_text)
# print(write_text)
if __name__ == '__main__':
added_list = read_file()
add_executable = read_cmakelists()
# print(added_list)
# print(add_executable)
new_add_list = merge(add_executable, added_list)
rewrite(new_add_list)