preprocessor.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #
  2. # preprocessor.py
  3. #
  4. import subprocess
  5. nocache = 1
  6. verbose = 0
  7. def blab(str):
  8. if verbose:
  9. print(str)
  10. ################################################################################
  11. #
  12. # Invoke GCC to run the preprocessor and extract enabled features
  13. #
  14. preprocessor_cache = {}
  15. def run_preprocessor(env, fn=None):
  16. filename = fn or 'buildroot/share/PlatformIO/scripts/common-dependencies.h'
  17. if filename in preprocessor_cache:
  18. return preprocessor_cache[filename]
  19. # Process defines
  20. build_flags = env.get('BUILD_FLAGS')
  21. build_flags = env.ParseFlagsExtended(build_flags)
  22. cxx = search_compiler(env)
  23. cmd = ['"' + cxx + '"']
  24. # Build flags from board.json
  25. #if 'BOARD' in env:
  26. # cmd += [env.BoardConfig().get("build.extra_flags")]
  27. for s in build_flags['CPPDEFINES']:
  28. if isinstance(s, tuple):
  29. cmd += ['-D' + s[0] + '=' + str(s[1])]
  30. else:
  31. cmd += ['-D' + s]
  32. cmd += ['-D__MARLIN_DEPS__ -w -dM -E -x c++']
  33. depcmd = cmd + [ filename ]
  34. cmd = ' '.join(depcmd)
  35. blab(cmd)
  36. try:
  37. define_list = subprocess.check_output(cmd, shell=True).splitlines()
  38. except:
  39. define_list = {}
  40. preprocessor_cache[filename] = define_list
  41. return define_list
  42. ################################################################################
  43. #
  44. # Find a compiler, considering the OS
  45. #
  46. def search_compiler(env):
  47. from pathlib import Path, PurePath
  48. ENV_BUILD_PATH = Path(env['PROJECT_BUILD_DIR'], env['PIOENV'])
  49. GCC_PATH_CACHE = ENV_BUILD_PATH / ".gcc_path"
  50. try:
  51. gccpath = env.GetProjectOption('custom_gcc')
  52. blab("Getting compiler from env")
  53. return gccpath
  54. except:
  55. pass
  56. # Warning: The cached .gcc_path will obscure a newly-installed toolkit
  57. if not nocache and GCC_PATH_CACHE.exists():
  58. blab("Getting g++ path from cache")
  59. return GCC_PATH_CACHE.read_text()
  60. # Use any item in $PATH corresponding to a platformio toolchain bin folder
  61. path_separator = ':'
  62. gcc_exe = '*g++'
  63. if env['PLATFORM'] == 'win32':
  64. path_separator = ';'
  65. gcc_exe += ".exe"
  66. # Search for the compiler in PATH
  67. for ppath in map(Path, env['ENV']['PATH'].split(path_separator)):
  68. if ppath.match(env['PROJECT_PACKAGES_DIR'] + "/**/bin"):
  69. for gpath in ppath.glob(gcc_exe):
  70. gccpath = str(gpath.resolve())
  71. # Cache the g++ path to no search always
  72. if not nocache and ENV_BUILD_PATH.exists():
  73. blab("Caching g++ for current env")
  74. GCC_PATH_CACHE.write_text(gccpath)
  75. return gccpath
  76. gccpath = env.get('CXX')
  77. blab("Couldn't find a compiler! Fallback to %s" % gccpath)
  78. return gccpath