import os,sys,re
import shutil,string
# [OK]
# global var for app
# calc the baseLen, we should add the divator char '/' or '\'
gBaseLen = len(os.getcwd()) + 1
gSrcList = "SOURCES += \\" + "\n"
gHeadList = "HEADERS += \\" + "\n"
gUiList = "FORMS += \\" + "\n"
gTarget = "Test"
argLen = len(sys.argv)
if argLen >= 2:
gTarget = sys.argv[1]
#[OK]
# walk the dirname,
# if type is file,then call pFunc to process it.
# if type is dir,then recurse to walk the dir.
def getList(dirname,pFunc):
try:
ls=os.listdir(dirname)
except:
print dirname,'is access deny'
else:
for file in ls:
temp = os.path.join(dirname,file)
if(os.path.isdir(temp)):
getList(temp,pFunc)
else:
pFunc(dirname,file)
# [OK]
# this function is used to process the certain file && dirname.
def printFile(dirname,file):
global gSrcList,gHeadList,gUiList
newPath = ""
if len(dirname) == gBaseLen:
newPath = file
else:
newPath = dirname[gBaseLen:] + "/" + file
#filter
if newPath[0] == '/':
newPath = newPath[1:]
#print newPath
#newPath = convertDivChar(newPath)
if re.search("\.h$",newPath):
gHeadList = gHeadList + "\t" + newPath + " \\" + "\n"
if re.search("\.hpp$",newPath):
gHeadList = gHeadList + "\t" + newPath + " \\" + "\n"
if re.search("\.cpp$",newPath):
gSrcList = gSrcList + "\t" + newPath + " \\" + "\n"
if re.search("\.c$",newPath):
gSrcList = gSrcList + "\t" + newPath + " \\" + "\n"
if re.search("\.ui$",newPath):
gUiList = gUiList + "\t" + newPath + " \\" + "\n"
def printProFile(target):
global gSrcList,gHeadList,gUiList
print "QT += core gui"
print "TARGET = %s" % (target)
print "TEMPLATE = app"
print "\n"
print "#LIBS += -L/usr/lib/mysql -lmysqlclient\n"
print "#INCLUDEPATH += /usr/include/mysql\n"
print "\n"
print gSrcList
print gHeadList
print gUiList
# this function is the main point.
getList(os.getcwd(),printFile)
printProFile(gTarget)
</pre><pre name="code" class="python">genQtProFile.py [projectName]
应用:(组织编译复杂的项目、生成静态库、生成动态库都很方便)
1、直接编译报错。
gcc main_c.c -o main<span style="white-space:pre"> </span>//缺少函数add、sub的实现
2、实现的c文件一起加入编译成功。
gcc main_c.c my_math.c -o main
3、借助于genProFile.py生成pro/makefile的方式管理编译代码。
topro main > main.pro
qmake
make
成功
脚本自动生成pro文件(人工再微调一下):
QT += core gui
TARGET = mathLib
TEMPLATE = lib
config += static
#LIBS += -L/usr/lib/mysql -lmysqlclient
#INCLUDEPATH += /usr/include/mysql
SOURCES += \
my_math.c \
main_c.c \
main_cpp.cpp \
HEADERS += \
my_math.h \
FORMS += \