eclipse_make_wrapper.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #!/usr/bin/env python
  2. #
  3. # Wrapper to run make and preprocess any paths in the output from MSYS Unix-style paths
  4. # to Windows paths, for Eclipse
  5. from __future__ import print_function, division
  6. import sys
  7. import subprocess
  8. import os.path
  9. import os
  10. import re
  11. import glob
  12. from test import test_cmd_line
  13. #UNIX_PATH_RE = re.compile(r'(([a-zA-Z]{1}[:]{1}){0,1}[/\\][^\s\'\"\t\[\(]+)+')
  14. UNIX_PATH_RE = re.compile(r'(([a-zA-Z]{1}[:]{1}){0,1}[/\\][^\s\'\"\t\[\(]+(?![^\\/\n]*$)[/\\]?)')
  15. INCLUDE_PATH_RE = re.compile(r'-I[\s"]{0,}(.+?)["]{0,}(?=\s-\S)')
  16. INCLUDE_PATH_ADJ_RE = re.compile(r'^([/]opt[/]esp-idf[/]){1}(.*)')
  17. INCLUDE_PATH_ADJ2_RE = re.compile(r'^([/]c[/]){1}(.*)')
  18. paths = {}
  19. names = []
  20. idf_path= os.environ.get('IDF_PATH').replace("/", "\\")
  21. cwd_path= os.environ.get('CWD')
  22. pwd_path= os.environ.get('PWD')
  23. def check_path(path):
  24. try:
  25. return paths[path]
  26. except KeyError:
  27. pass
  28. paths[path] = path
  29. winpath =path
  30. if not os.path.exists(winpath):
  31. # cache as failed, replace with success if it works
  32. if re.match(INCLUDE_PATH_ADJ2_RE, path) is not None:
  33. winpath = INCLUDE_PATH_ADJ2_RE.sub(r'c:/\2',path) #replace /c/
  34. try:
  35. winpath = subprocess.check_output(["cygpath", "-w", winpath]).strip()
  36. except subprocess.CalledProcessError:
  37. return path # something went wrong running cygpath, assume this is not a path!
  38. if not os.path.exists(winpath):
  39. if not os.path.exists(winpath):
  40. winpath=idf_path + '\\' + re.sub(r'^[/\\]opt[/\\](esp-idf[/\\]){0,}', '', path, 1)
  41. try:
  42. winpath = subprocess.check_output(["cygpath", "-w", winpath]).strip()
  43. except subprocess.CalledProcessError:
  44. return path # something went wrong running cygpath, assume this is not a path!
  45. if not os.path.exists(winpath):
  46. return path # not actually a valid path
  47. winpath = winpath.replace("/", "\\") # make consistent with forward-slashes used elsewhere
  48. paths[path] = winpath
  49. #print("In path: {0}, out path: {1}".format(path,winpath) )
  50. return winpath
  51. def fix_paths(filename):
  52. if re.match(r'.*[\\](.*$)',filename) is not None:
  53. filename = re.findall(r'.*[\\](.*$)',filename)[0].replace("\\", "/")
  54. return filename.rstrip()
  55. def print_paths(path_list, file_name, source_file):
  56. new_path_list = list(set(path_list))
  57. new_path_list.sort()
  58. last_n = ''
  59. cmd_line='xtensa-esp32-elf-gcc '
  60. for n in new_path_list:
  61. if re.match(INCLUDE_PATH_ADJ_RE, n) is not None:
  62. n = INCLUDE_PATH_ADJ_RE.sub(idf_path+r"\2",n )
  63. if re.match(INCLUDE_PATH_ADJ2_RE, n) is not None:
  64. n = INCLUDE_PATH_ADJ2_RE.sub(r'c:/\2',n)
  65. if last_n != n:
  66. cmd_line = cmd_line + ' -I ' + n.rstrip()
  67. last_n = n
  68. if source_file:
  69. cmd_line = cmd_line + ' -c ' + fix_paths(source_file)
  70. cmd_line = cmd_line + ' -o ' + fix_paths(file_name)
  71. print(cmd_line)
  72. def extract_includes():
  73. for filename in [y for x in os.walk('build') for y in glob.glob(os.path.join(x[0], '*.d'))]:
  74. lines = []
  75. source=''
  76. with open(filename) as file_in:
  77. for line in file_in:
  78. if re.match(r'\S*(?=/[^/]*\.[h][p]?)',line) is not None:
  79. lines.extend(re.findall(r'\S*(?=/[^/]*\.[h][p]?)/',line))
  80. if re.match(r'\S*(?=\.[cC][pP]{0,})[^\\\s]*',line) is not None:
  81. source = re.findall(r'\S*(?=\.[cC][pP]{0,})[^\\\s]*',line)[0]
  82. print_paths(lines,filename,source )
  83. def main():
  84. cwd_path=check_path(os.getcwd())
  85. os.environ['CWD']= cwd_path
  86. os.environ['PWD']= cwd_path
  87. idf_path= os.environ.get('IDF_PATH').replace("/", "\\")
  88. cwd_path= os.environ.get('CWD')
  89. pwd_path= os.environ.get('PWD')
  90. print('Running custom script make in {}, IDF_PATH={}, CWD={}, PWD={}'.format(cwd_path,idf_path,cwd_path,pwd_path))
  91. make = subprocess.Popen(["make"] + sys.argv[1:] + ["BATCH_BUILD=1"], stdout=subprocess.PIPE)
  92. for line in iter(make.stdout.readline, ''):
  93. line = re.sub(UNIX_PATH_RE, lambda m: check_path(m.group(0)), line)
  94. names.extend(INCLUDE_PATH_RE.findall(line))
  95. print(line.rstrip())
  96. sys.exit(make.wait())
  97. if __name__ == "__main__":
  98. main()