| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx
LueOsburn
9年前发布

基于Python的cmake多目录工程的自动配置实现

compile_env_config.py  

#!/usr/bin/python    """  2015.06.30  Modified some functions.  Now this configration can be run on both Windows and Linux.  *************************************************************************************  2015.06.29  This is a configration function code which can be only run on Win Sys  Written by Grey 2015.06.29  """  #coding:utf-8  import os,shutil,re    # Function DeleteCmakefilesDirectory can delete the directory generated by cmake.  def DeleteCmakefilesDirectory():   try:    shutil.rmtree("./CMakeFiles")   except:    print "cannot find the object!"    # Function RemoveEmptyFolder can remove all the empty folders in pwd  def RemoveEmptyFolder():   for root,dirs,files in os.walk("./"):    if dirs == [] and files == []:     os.rmdir(root)    # Function SubDirectoryCmakelistsGen can generate a CMakeLists.txt for a subdirectory  def SubDirectoryCmakelistsGen():   pwd = os.getcwd()   directory_name = os.path.basename(pwd)   directory_source_name = directory_name + '_DIR_SRCS'   directory_lib_name    = directory_name + '_lib'   cmakelists_obj        = open('CMakeLists.txt','w')   cmakelists_obj.write("aux_source_directory(. %s)\n" % directory_source_name)   cmakelists_obj.write("add_library(%s ${%s})\n\n" % (directory_lib_name,directory_source_name))   cmakelists_obj.close()    def MainCmakelistsGen():   cmakelists_obj = open('CMakeLists.txt','w')   pwd = os.getcwd()   directory_name = os.path.basename(pwd)   directory_source_name = directory_name + '_DIR_SRCS'   directory_lib_name = directory_name + '_lib'   cmakelists_obj = open('CMakeLists.txt','w')   cmakelists_obj.write("cmake_minimum_required(VERSION 3.3)\n")   cmakelists_obj.write("project(%s_prj)\n" % directory_name)   cmakelists_obj.write("aux_source_directory(. %s)\n" % directory_source_name)   cmakelists_obj.write("add_subdirectory_for_replace\n")   cmakelists_obj.write("add_executable(%s_bin ${%s})\n" % (directory_name,directory_source_name))   cmakelists_obj.write("target_link_libraries(%s_bin all_libs_for_replace)\n" % directory_name)   cmakelists_obj.close()    def MainCmakelistsUpdate():   main_text = open('CMakeLists.txt','r').read()   include_text = open('include.txt','r').read()   subdirectory_text = open('subdirectory.txt','r').read()   lib_text = open('lib_list.txt','r').read()   subdirectory_text = re.sub(r'add_subdirectory\(./\)\n','',subdirectory_text)   main_text = re.sub('add_subdirectory_for_replace','%s',main_text) % (include_text + '\n' + subdirectory_text)   main_text = re.sub('all_libs_for_replace','%s',main_text) % lib_text   main_text = re.sub(r'\\','/',main_text)   main_text = re.sub(r'\n\n','\n',main_text)   main_write_fid = open('CMakeLists.txt','w')   main_write_fid.write(main_text)   main_write_fid.close()    def MainConfig():   # step1 : delete all the empty code folders   RemoveEmptyFolder()   # step2 : delete 'CMakeFiles' in main folder   DeleteCmakefilesDirectory()   # step3 : process for subdirectories   subdirectories = []   sub_libs = []   pwd = os.getcwd()   for root,dirs,files in os.walk("./"):    os.chdir(root)    DeleteCmakefilesDirectory()    SubDirectoryCmakelistsGen()    subdirectories.append(root)    subdirectory_name = os.path.basename(root)    sub_libs.append(subdirectory_name + "_lib")    os.chdir(pwd)   include_obj = open('include.txt','w')   subdir_obj = open('subdirectory.txt','w')   include_obj.write("include_directories(")   for subdirectory in subdirectories:    include_obj.write("%s " % subdirectory)    subdir_obj.write("add_subdirectory(%s)\n" % subdirectory)   include_obj.write(")")   include_obj.close()   subdir_obj.close()   # step4 : try to generate main CMakeLists.txt   MainCmakelistsGen()   lib_flag = 0   lib_list_obj = open('lib_list.txt','w')   for each_sub_lib in sub_libs:    if lib_flag != 0:     lib_list_obj.write("%s " % each_sub_lib)    lib_flag += 1   lib_list_obj.close()   MainCmakelistsUpdate()    MainConfig()