论坛首页 编程语言技术论坛

用python写的linux编译工具,直接读取vcproj文件

浏览 3294 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2011-09-30  
很多时候,我们写代码,是在wndows下面用vc写的,但是又需要到linux系统或者其他unix系统上编译,还需要另外写makefile文件,很麻烦,下面这个工具,可以直接从vcproj中读取文件,生成编译命令进行编译。。。。不用再写烦人的makefile了。。。工具还不完善,后面有时间继续完善它。。。。
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
from xml.dom import minidom
import sys
import re
import os

class TOptions:
    def __init__(self):
        incDirs = ''
        libDirs = ''
        libs = ''
        preDefines = ''
        outFile = ''
        outDir = ''
        srcfiles = []


if __name__ == '__main__':
    #print sys.argc
    print sys.argv
    oFile = file('tclient.vcproj', 'r')
    data = ''
    lines = oFile.readlines()
    lines[0] = lines[0].replace('"gb2312"', '"utf-8"')
    data = data.join(lines)
    oFile.close()
    data = data.decode('gb2312').encode('utf-8');
    lstEnv = []
    lstEnv = re.findall(r'\$\(\w*\)'.encode('utf-8'), data)
    for env in lstEnv:
        print env[2:-1]
        data = data.replace(env, os.getenv(env[2:-1], env))
    data = data.replace('\\', '/');
    option = TOptions()
    xmldoc = minidom.parseString(data)
    xmlConf = xmldoc.getElementsByTagName('Configuration')
    for conf in xmlConf:
        print conf.attributes['Name'].value
        print conf.attributes['OutputDirectory'].value
        if conf.attributes['Name'].value.find('Debug') >= 0:
            print 'Debug do nothing...'
            continue
        option.outDir = conf.attributes['OutputDirectory'].value
        for tool in conf.childNodes:
            if 1 == tool.nodeType:
                if 'VCCLCompilerTool' == tool.attributes['Name'].value:
                    print '--compile...'
                    print tool.attributes['AdditionalIncludeDirectories'].value
                    print tool.attributes['PreprocessorDefinitions'].value
                    option.incDirs = tool.attributes['AdditionalIncludeDirectories'].value
                    option.preDefines = tool.attributes['PreprocessorDefinitions'].value
                    
                if 'VCLinkerTool' == tool.attributes['Name'].value:
                    print '--link...'
                    print tool.attributes['AdditionalDependencies'].value
                    print tool.attributes['AdditionalLibraryDirectories'].value
                    print tool.attributes['GenerateDebugInformation'].value
                    print tool.attributes['OutputFile'].value
                    option.libs = tool.attributes['AdditionalDependencies'].value
                    option.libDirs = tool.attributes['AdditionalLibraryDirectories'].value
                    option.outFile = tool.attributes['OutputFile'].value.rstrip('.exe')
    option.srcfiles = []
    files = xmldoc.getElementsByTagName('File')
    for file in files:
        srcfile = file.attributes['RelativePath'].value
        option.srcfiles.append(srcfile)
    
    ds = option.preDefines.split(';')
    ds1 = []
    for d in ds:
        if 'WIN32' == d:
            d = 'LINUX'
        d = '-D' + d
        ds1.append(d)
    option.preDefines = ' '.join(ds1)
    print option.preDefines
    
    incDir = option.incDirs.split(';')
    incDir1 = []
    for inc in incDir:
        inc = '-I' + inc
        incDir1.append(inc)
    option.incDirs = ' '.join(incDir1)
    print option.incDirs
    
    libDir = option.libDirs.split(';')
    libDir1 = []
    for dir in libDir:
        dir = '-L' + dir
        libDir1.append(dir)
    option.libDirs = ' '.join(libDir1)
    print option.libDirs
    
    libs = option.libs.split(' ')
    libs1 = []
    for lib in libs:
        lib = '-l' + lib.rstrip('.lib')
        libs1.append(lib)
    libs1.append('-lc')   #linux下两个默认库
    libs1.append('-ldl')
    option.libs = ' '.join(libs1)
    print option.libs
    print '..................'
    
    compileLine = option.preDefines + ' -c -g -pthread -fPIC ' + option.incDirs
    print compileLine
    
    #创建目标目录
    print option.srcfiles
    cppfiles = []
    objfiles = []
    if False == os.access('obj', os.F_OK):
        os.mkdir('obj')
    else:
        #判断cpp文件的时间戳与.o文件的时间戳
        for srcfile in option.srcfiles:
            objfile = 'obj/' + srcfile.rstrip('.cpp').lstrip('./') + '.o'
            if os.stat(srcfile).st_mtime >= os.stat(objfile).st_mtime:
                cppfiles.append(srcfile)
            else:
                objfiles.append(objfile)
        
    
    print cppfiles
    for srcfile in cppfiles:
        objfile = 'obj/' + srcfile.rstrip('.cpp').lstrip('./') + '.o'
        target = objfile + ' ' + srcfile
        objfiles.append(objfile)
        cl = 'g++ -o ' + target + ' ' + compileLine
        print cl
        os.system(cl)
        
    objs = ' '.join(objfiles)
    link = 'g++ -g -pthread -fPIC -o ' + option.outFile + ' ' + objs + ' ' + option.libDirs + ' ' + option.libs
    print link
    os.system(link)
   发表时间:2011-10-22  
好东西,Python写这样的小工具最合适了。
0 请登录后投票
   发表时间:2011-10-26  
超级爱好这东西,楼主,辛苦了。
0 请登录后投票
   发表时间:2011-10-27  
好东西!我之前也有过这个想法的
0 请登录后投票
论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics