mk_utils.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. # -*- coding: UTF-8 -*-
  2. import os
  3. import sys
  4. import re
  5. from . import file_utils
  6. class MkUtils:
  7. #成员变量
  8. srcContent = ''
  9. includeContent = ''
  10. exportIncludeContent = ''
  11. rootPath = ''
  12. mkPath = ''
  13. relativePath = ''
  14. def __init__(self):
  15. pass
  16. def init(self, rootPath, mkPath):
  17. self.rootPath = rootPath
  18. self.mkPath = mkPath
  19. def replaceContent(self, mkPath):
  20. with open(mkPath) as f:
  21. content = f.read()
  22. content = re.sub(r'#___AUTO_SRC_START___[\s\S]*#___AUTO_SRC_END___', '#___AUTO_SRC_START___\n' + 'LOCAL_SRC_FILES := ' + self.srcContent + '#___AUTO_SRC_END___\n', content)
  23. content = re.sub(r'#___AUTO_EXPORT_C_INCLUDES_START___[\s\S]*#___AUTO_EXPORT_C_INCLUDES_END___', '#___AUTO_EXPORT_C_INCLUDES_START___\n' + 'LOCAL_EXPORT_C_INCLUDES :=' + self.includeContent + '#___AUTO_EXPORT_C_INCLUDES_END___\n', content)
  24. content = re.sub(r'#___AUTO_C_INCLUDES_START___[\s\S]*#___AUTO_C_INCLUDES_END___', '#___AUTO_C_INCLUDES_START___\n' + 'LOCAL_C_INCLUDES :=' + self.includeContent + '#___AUTO_C_INCLUDES_END___\n', content)
  25. file_utils.writeToFile(content, mkPath)
  26. pass
  27. '''
  28. 遍历目录中的c++文件加入Android.mk
  29. @param dirctory:文件路径
  30. @param suffixList:拓展名数组
  31. @param recursive:是否递归
  32. '''
  33. def addToMk(self, directory, suffixList, recursive):
  34. #遍历文件夹下的所有cpp文件
  35. self.travelsalDir(directory, suffixList, recursive)
  36. pass
  37. def addToInclude(self, path):
  38. relativePath = self.getRelativePath(path)
  39. self.includeContent += ' $(LOCAL_PATH)/' + relativePath + ' \\' + '\n'
  40. pass
  41. def addToSrc(self, path):
  42. relativePath = self.getRelativePath(path)
  43. self.srcContent += ' ' + relativePath + ' \\' + '\n'
  44. pass
  45. def getRelativePath(self, path):
  46. mkLen = len(self.mkPath)
  47. rootLen = len(self.rootPath)
  48. splitPath = self.mkPath[rootLen + 1 : mkLen]
  49. mkPathArr = []
  50. pathArr = []
  51. if len(splitPath) > 0 :
  52. mkPathArr = splitPath.split('/')
  53. if len(path) > 0 :
  54. pathArr = path.split('/')
  55. relativePath = ''
  56. for dir in mkPathArr:
  57. if(dir in pathArr):
  58. pathArr.remove(dir)
  59. else:
  60. relativePath += '../'
  61. num = len(pathArr)
  62. for i in range(num):
  63. if(i == num - 1):
  64. relativePath += pathArr[i]
  65. else:
  66. relativePath += pathArr[i] + '/'
  67. return relativePath
  68. #遍历目录中的所有文件和目录
  69. def travelsalDir(self, directory, suffixList, recursive):
  70. dirPath = self.rootPath + '/' + directory
  71. if(file_utils.isFile(dirPath)):
  72. print("不是文件夹:" + dirPath);
  73. return;
  74. self.addToInclude(directory)
  75. dirs = os.listdir(dirPath)
  76. dirs.sort()#排序
  77. for file in dirs:
  78. if(file_utils.isFile(dirPath+ '/'+ file)):
  79. ext = os.path.splitext(dirPath+ '/'+ file)[1]
  80. if(ext in suffixList):
  81. if(directory == ''):
  82. self.addToSrc(file)
  83. print(file);
  84. else:
  85. self.addToSrc(directory+ '/'+ file)
  86. print(directory+ '/'+ file);
  87. else:
  88. if recursive == True:
  89. self.travelsalDir(directory +'/'+ file, suffixList, True)
  90. pass