# 自动生成 CMakeLists.txt,把该文件放到项目根目录中
import os
project_name = input("请输入项目名称: ") # TestAlgorithm
cur_abs_path = os.getcwd()
input_dir_name = input("请输入要遍历的项目根目录中的文件目录名(之后会遍历该目录中所有 c 和 cpp 文件: ")
input_dir_abs_path = os.path.join(cur_abs_path, input_dir_name)
main_file_name = input("请输入根目录 main 函数所在的文件名: ")
cmakeFileAbsPath = os.path.join(cur_abs_path, "CMakeLists.txt")
target_dir_list_result = []
def listTargetDir(path):
print("listTargetDir path = " + path)
if not (os.path.isdir(path)): # 是文件
if os.path.basename(path).find(".c") != -1:
print("filePathList.append path=" + path)
target_dir_list_result.append(path)
else: # 是目录
for childDir in os.listdir(path):
listTargetDir(os.path.join(path,childDir))
listTargetDir(input_dir_abs_path)
# 添加根目录
target_dir_list_result.append(os.path.join(cur_abs_path,main_file_name))
# 添加遍历后的 .c / .cpp 文件到 cmake_file_list_result 中
cmake_file_list_result = []
for absFilePath in target_dir_list_result: # 遍历文件夹
result = absFilePath.split(project_name + "/")[1]
cmake_file_list_result.append(result)
cmake_content = '''cmake_minimum_required(VERSION 3.5.1)
project(''' + project_name + ''')
set(CMAKE_CXX_STANDARD 14)
add_executable( ''' + project_name + " "
for filePath in cmake_file_list_result:
print("filePath = " + filePath)
cmake_content += filePath
cmake_content += " "
cmake_content += ")"
with open(cmakeFileAbsPath, 'w') as f:
print("cmake_content = " + cmake_content)
f.write(cmake_content)
test:
~/work/masterfit_pose/upload$ python3 autogenmakelist.py
请输入项目名称: upload
请输入要遍历的项目根目录中的文件目录名(之后会遍历该目录中所有 c 和 cpp 文件: /home/masterfit/work/masterfit_pose/upload
请输入根目录 main 函数所在的文件名: main.cpp
listTargetDir path = /home/masterfit/work/masterfit_pose/upload
listTargetDir path = /home/masterfit/work/masterfit_pose/upload/RecvUpload.cpp
filePathList.append path=/home/masterfit/work/masterfit_pose/upload/RecvUpload.cpp
listTargetDir path = /home/masterfit/work/masterfit_pose/upload/upload.vcxproj.filters
listTargetDir path = /home/masterfit/work/masterfit_pose/upload/XmlUpdatePaser.cpp
filePathList.append path=/home/masterfit/work/masterfit_pose/upload/XmlUpdatePaser.cpp
listTargetDir path = /home/masterfit/work/masterfit_pose/upload/CppSQLite3.h
listTargetDir path = /home/masterfit/work/masterfit_pose/upload/RecvUpload.h
listTargetDir path = /home/masterfit/work/masterfit_pose/upload/HttpCommont.h
listTargetDir path = /home/masterfit/work/masterfit_pose/upload/HttpCommont.cpp
filePathList.append path=/home/masterfit/work/masterfit_pose/upload/HttpCommont.cpp
listTargetDir path = /home/masterfit/work/masterfit_pose/upload/HttpClient.h
listTargetDir path = /home/masterfit/work/masterfit_pose/upload/DataServer.cpp
filePathList.append path=/home/masterfit/work/masterfit_pose/upload/DataServer.cpp
listTargetDir path = /home/masterfit/work/masterfit_pose/upload/CMakeLists.txt
listTargetDir path = /home/masterfit/work/masterfit_pose/upload/sqlite
listTargetDir path = /home/masterfit/work/masterfit_pose/upload/sqlite/shell.c
filePathList.append path=/home/masterfit/work/masterfit_pose/upload/sqlite/shell.c
listTargetDir path = /home/masterfit/work/masterfit_pose/upload/sqlite/sqlite3.h
listTargetDir path = /home/masterfit/work/masterfit_pose/upload/sqlite/sqlite3.c
filePathList.append path=/home/masterfit/work/masterfit_pose/upload/sqlite/sqlite3.c
listTargetDir path = /home/masterfit/work/masterfit_pose/upload/sqlite/sqlite3ext.h
listTargetDir path = /home/masterfit/work/masterfit_pose/upload/DataCache.h
listTargetDir path = /home/masterfit/work/masterfit_pose/upload/upload.vcxproj.user
listTargetDir path = /home/masterfit/work/masterfit_pose/upload/Ftp.cpp
filePathList.append path=/home/masterfit/work/masterfit_pose/upload/Ftp.cpp
listTargetDir path = /home/masterfit/work/masterfit_pose/upload/HttpClient.cpp
filePathList.append path=/home/masterfit/work/masterfit_pose/upload/HttpClient.cpp
listTargetDir path = /home/masterfit/work/masterfit_pose/upload/XmlUpdatePaser.h
listTargetDir path = /home/masterfit/work/masterfit_pose/upload/wingetopt.h
listTargetDir path = /home/masterfit/work/masterfit_pose/upload/PushMsg.cpp
filePathList.append path=/home/masterfit/work/masterfit_pose/upload/PushMsg.cpp
listTargetDir path = /home/masterfit/work/masterfit_pose/upload/PushMsg.h
listTargetDir path = /home/masterfit/work/masterfit_pose/upload/DataDefine.h
listTargetDir path = /home/masterfit/work/masterfit_pose/upload/CppSQLite3.cpp
filePathList.append path=/home/masterfit/work/masterfit_pose/upload/CppSQLite3.cpp
listTargetDir path = /home/masterfit/work/masterfit_pose/upload/DataCache.cpp
filePathList.append path=/home/masterfit/work/masterfit_pose/upload/DataCache.cpp
listTargetDir path = /home/masterfit/work/masterfit_pose/upload/autogenmakelist.py
listTargetDir path = /home/masterfit/work/masterfit_pose/upload/VERSION
listTargetDir path = /home/masterfit/work/masterfit_pose/upload/DataServer.h
listTargetDir path = /home/masterfit/work/masterfit_pose/upload/Ftp.h
listTargetDir path = /home/masterfit/work/masterfit_pose/upload/main.cpp
filePathList.append path=/home/masterfit/work/masterfit_pose/upload/main.cpp
listTargetDir path = /home/masterfit/work/masterfit_pose/upload/wingetopt.cpp
filePathList.append path=/home/masterfit/work/masterfit_pose/upload/wingetopt.cpp
filePath = RecvUpload.cpp
filePath = XmlUpdatePaser.cpp
filePath = HttpCommont.cpp
filePath = DataServer.cpp
filePath = sqlite/shell.c
filePath = sqlite/sqlite3.c
filePath = Ftp.cpp
filePath = HttpClient.cpp
filePath = PushMsg.cpp
filePath = CppSQLite3.cpp
filePath = DataCache.cpp
filePath = main.cpp
filePath = wingetopt.cpp
filePath = main.cpp
cmake_content = cmake_minimum_required(VERSION 3.5.1)
project(upload)
set(CMAKE_CXX_STANDARD 14)
add_executable( upload RecvUpload.cpp XmlUpdatePaser.cpp HttpCommont.cpp DataServer.cpp sqlite/shell.c sqlite/sqlite3.c Ftp.cpp HttpClient.cpp PushMsg.cpp CppSQLite3.cpp DataCache.cpp main.cpp wingetopt.cpp main.cpp )
masterfit@masterfit01:~/work/masterfit_pose/upload$ la
autogenmakelist.py CppSQLite3.cpp DataCache.cpp DataDefine.h DataServer.h Ftp.h HttpClient.h HttpCommont.h PushMsg.cpp RecvUpload.cpp sqlite upload.vcxproj.user wingetopt.cpp XmlUpdatePaser.cpp
CMakeLists.txt CppSQLite3.h DataCache.h DataServer.cpp Ftp.cpp HttpClient.cpp HttpCommont.cpp main.cpp PushMsg.h RecvUpload.h upload.vcxproj.filters VERSION wingetopt.h XmlUpdatePaser.h
masterfit@masterfit01:~/work/masterfit_pose/upload$ vi CMakeLists.txt
masterfit@masterfit01:~/work/masterfit_pose/upload$ mkdir build
masterfit@masterfit01:~/work/masterfit_pose/upload$ cd build/
masterfit@masterfit01:~/work/masterfit_pose/upload/build$ la
masterfit@masterfit01:~/work/masterfit_pose/upload/build$ cmake ..
-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/masterfit/work/masterfit_pose/upload/build
masterfit@masterfit01:~/work/masterfit_pose/upload/build$ la
CMakeCache.txt CMakeFiles cmake_install.cmake Makefile
masterfit@masterfit01:~/work/masterfit_pose/upload/build$ make