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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. define_list = subprocess.check_output(cmd, shell=True).splitlines()
  37. preprocessor_cache[filename] = define_list
  38. return define_list
  39. ################################################################################
  40. #
  41. # Find a compiler, considering the OS
  42. #
  43. def search_compiler(env):
  44. ENV_BUILD_PATH = os.path.join(env['PROJECT_BUILD_DIR'], env['PIOENV'])
  45. GCC_PATH_CACHE = os.path.join(ENV_BUILD_PATH, ".gcc_path")
  46. try:
  47. filepath = env.GetProjectOption('custom_gcc')
  48. blab("Getting compiler from env")
  49. return filepath
  50. except:
  51. pass
  52. # Warning: The cached .gcc_path will obscure a newly-installed toolkit
  53. if not nocache and os.path.exists(GCC_PATH_CACHE):
  54. blab("Getting g++ path from cache")
  55. with open(GCC_PATH_CACHE, 'r') as f:
  56. return f.read()
  57. # Find a platform compiler by searching $PATH items
  58. # A native target will search all PATH bin folders.
  59. # Others look only within $HOME/.platformio.
  60. path_regex = "" if env.GetProjectOption('platform') == 'native' else re.escape(env['PROJECT_PACKAGES_DIR'])
  61. gcc = "g++"
  62. if env['PLATFORM'] == 'win32':
  63. path_separator = ';'
  64. path_regex += r'.*\\bin'
  65. gcc += ".exe"
  66. else:
  67. path_separator = ':'
  68. path_regex += r'/.+/bin'
  69. # Search for the compiler
  70. for pathdir in env['ENV']['PATH'].split(path_separator):
  71. if not re.search(path_regex, pathdir, re.IGNORECASE):
  72. continue
  73. for filepath in os.listdir(pathdir):
  74. if not filepath.endswith(gcc):
  75. continue
  76. # Use entire path to not rely on env PATH
  77. filepath = os.path.sep.join([pathdir, filepath])
  78. # Cache the g++ path to no search always
  79. if not nocache and os.path.exists(ENV_BUILD_PATH):
  80. blab("Caching g++ for current env")
  81. with open(GCC_PATH_CACHE, 'w+') as f:
  82. f.write(filepath)
  83. return filepath
  84. filepath = env.get('CXX')
  85. blab("Couldn't find a compiler! Fallback to %s" % filepath)
  86. return filepath