My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

preprocessor.py 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #
  2. # preprocessor.py
  3. #
  4. import subprocess,os,re
  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. ENV_BUILD_PATH = os.path.join(env['PROJECT_BUILD_DIR'], env['PIOENV'])
  48. GCC_PATH_CACHE = os.path.join(ENV_BUILD_PATH, ".gcc_path")
  49. try:
  50. filepath = env.GetProjectOption('custom_gcc')
  51. blab("Getting compiler from env")
  52. return filepath
  53. except:
  54. pass
  55. # Warning: The cached .gcc_path will obscure a newly-installed toolkit
  56. if not nocache and os.path.exists(GCC_PATH_CACHE):
  57. blab("Getting g++ path from cache")
  58. with open(GCC_PATH_CACHE, 'r') as f:
  59. return f.read()
  60. # Find the current platform compiler by searching the $PATH
  61. # which will be in a platformio toolchain bin folder
  62. path_regex = re.escape(env['PROJECT_PACKAGES_DIR'])
  63. gcc = "g++"
  64. if env['PLATFORM'] == 'win32':
  65. path_separator = ';'
  66. path_regex += r'.*\\bin'
  67. gcc += ".exe"
  68. else:
  69. path_separator = ':'
  70. path_regex += r'/.+/bin'
  71. # Search for the compiler
  72. for pathdir in env['ENV']['PATH'].split(path_separator):
  73. if not re.search(path_regex, pathdir, re.IGNORECASE):
  74. continue
  75. for filepath in os.listdir(pathdir):
  76. if not filepath.endswith(gcc):
  77. continue
  78. # Use entire path to not rely on env PATH
  79. filepath = os.path.sep.join([pathdir, filepath])
  80. # Cache the g++ path to no search always
  81. if not nocache and os.path.exists(ENV_BUILD_PATH):
  82. blab("Caching g++ for current env")
  83. with open(GCC_PATH_CACHE, 'w+') as f:
  84. f.write(filepath)
  85. return filepath
  86. filepath = env.get('CXX')
  87. blab("Couldn't find a compiler! Fallback to %s" % filepath)
  88. return filepath