My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

common-dependencies.py 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. #
  2. # common-dependencies.py
  3. # Convenience script to check dependencies and add libs and sources for Marlin Enabled Features
  4. #
  5. import subprocess
  6. import os
  7. import re
  8. try:
  9. import configparser
  10. except ImportError:
  11. import ConfigParser as configparser
  12. try:
  13. # PIO < 4.4
  14. from platformio.managers.package import PackageManager
  15. except ImportError:
  16. # PIO >= 4.4
  17. from platformio.package.meta import PackageSpec as PackageManager
  18. Import("env")
  19. FEATURE_CONFIG = {}
  20. def parse_pkg_uri(spec):
  21. if PackageManager.__name__ == 'PackageSpec':
  22. return PackageManager(spec).name
  23. else:
  24. name, _, _ = PackageManager.parse_pkg_uri(spec)
  25. return name
  26. def add_to_feat_cnf(feature, flines):
  27. feat = FEATURE_CONFIG[feature]
  28. atoms = re.sub(',\\s*', '\n', flines).strip().split('\n')
  29. for dep in atoms:
  30. parts = dep.split('=')
  31. name = parts.pop(0)
  32. rest = '='.join(parts)
  33. if name in ['extra_scripts', 'src_filter', 'lib_ignore']:
  34. feat[name] = rest
  35. else:
  36. feat['lib_deps'] += [dep]
  37. def load_config():
  38. config = configparser.ConfigParser()
  39. config.read("platformio.ini")
  40. items = config.items('features')
  41. for key in items:
  42. feature = key[0].upper()
  43. if not feature in FEATURE_CONFIG:
  44. FEATURE_CONFIG[feature] = { 'lib_deps': [] }
  45. add_to_feat_cnf(feature, key[1])
  46. # Add options matching custom_marlin.MY_OPTION to the pile
  47. all_opts = env.GetProjectOptions()
  48. for n in all_opts:
  49. mat = re.match(r'custom_marlin\.(.+)', n[0])
  50. if mat:
  51. try:
  52. val = env.GetProjectOption(n[0])
  53. except:
  54. val = None
  55. if val:
  56. add_to_feat_cnf(mat.group(1).upper(), val)
  57. def get_all_known_libs():
  58. known_libs = []
  59. for feature in FEATURE_CONFIG:
  60. feat = FEATURE_CONFIG[feature]
  61. if not 'lib_deps' in feat:
  62. continue
  63. for dep in feat['lib_deps']:
  64. name = parse_pkg_uri(dep)
  65. known_libs.append(name)
  66. return known_libs
  67. def get_all_env_libs():
  68. env_libs = []
  69. lib_deps = env.GetProjectOption('lib_deps')
  70. for dep in lib_deps:
  71. name = parse_pkg_uri(dep)
  72. env_libs.append(name)
  73. return env_libs
  74. def set_env_field(field, value):
  75. proj = env.GetProjectConfig()
  76. proj.set("env:" + env['PIOENV'], field, value)
  77. # All unused libs should be ignored so that if a library
  78. # exists in .pio/lib_deps it will not break compilation.
  79. def force_ignore_unused_libs():
  80. env_libs = get_all_env_libs()
  81. known_libs = get_all_known_libs()
  82. diff = (list(set(known_libs) - set(env_libs)))
  83. lib_ignore = env.GetProjectOption('lib_ignore') + diff
  84. print("Ignore libraries:", lib_ignore)
  85. set_env_field('lib_ignore', lib_ignore)
  86. def apply_features_config():
  87. load_config()
  88. for feature in FEATURE_CONFIG:
  89. if not env.MarlinFeatureIsEnabled(feature):
  90. continue
  91. feat = FEATURE_CONFIG[feature]
  92. if 'lib_deps' in feat and len(feat['lib_deps']):
  93. print("Adding lib_deps for %s... " % feature)
  94. # feat to add
  95. deps_to_add = {}
  96. for dep in feat['lib_deps']:
  97. name = parse_pkg_uri(dep)
  98. deps_to_add[name] = dep
  99. # Does the env already have the dependency?
  100. deps = env.GetProjectOption('lib_deps')
  101. for dep in deps:
  102. name = parse_pkg_uri(dep)
  103. if name in deps_to_add:
  104. del deps_to_add[name]
  105. # Are there any libraries that should be ignored?
  106. lib_ignore = env.GetProjectOption('lib_ignore')
  107. for dep in deps:
  108. name = parse_pkg_uri(dep)
  109. if name in deps_to_add:
  110. del deps_to_add[name]
  111. # Is there anything left?
  112. if len(deps_to_add) > 0:
  113. # Only add the missing dependencies
  114. set_env_field('lib_deps', deps + list(deps_to_add.values()))
  115. if 'extra_scripts' in feat:
  116. print("Running extra_scripts for %s... " % feature)
  117. env.SConscript(feat['extra_scripts'], exports="env")
  118. if 'src_filter' in feat:
  119. print("Adding src_filter for %s... " % feature)
  120. src_filter = ' '.join(env.GetProjectOption('src_filter'))
  121. # first we need to remove the references to the same folder
  122. my_srcs = re.findall( r'[+-](<.*?>)', feat['src_filter'])
  123. cur_srcs = re.findall( r'[+-](<.*?>)', src_filter)
  124. for d in my_srcs:
  125. if d in cur_srcs:
  126. src_filter = re.sub(r'[+-]' + d, '', src_filter)
  127. src_filter = feat['src_filter'] + ' ' + src_filter
  128. set_env_field('src_filter', [src_filter])
  129. env.Replace(SRC_FILTER=src_filter)
  130. if 'lib_ignore' in feat:
  131. print("Adding lib_ignore for %s... " % feature)
  132. lib_ignore = env.GetProjectOption('lib_ignore') + [feat['lib_ignore']]
  133. set_env_field('lib_ignore', lib_ignore)
  134. #
  135. # Find a compiler, considering the OS
  136. #
  137. ENV_BUILD_PATH = os.path.join(env.Dictionary('PROJECT_BUILD_DIR'), env['PIOENV'])
  138. GCC_PATH_CACHE = os.path.join(ENV_BUILD_PATH, ".gcc_path")
  139. def search_compiler():
  140. if os.path.exists(GCC_PATH_CACHE):
  141. print('Getting g++ path from cache')
  142. with open(GCC_PATH_CACHE, 'r') as f:
  143. return f.read()
  144. # PlatformIO inserts the toolchain bin folder on the front of the $PATH
  145. # Find the current platform compiler by searching the $PATH
  146. if env['PLATFORM'] == 'win32':
  147. path_separator = ';'
  148. path_regex = re.escape(env['PROJECT_PACKAGES_DIR']) + r'.*\\bin'
  149. gcc = "g++.exe"
  150. else:
  151. path_separator = ':'
  152. path_regex = re.escape(env['PROJECT_PACKAGES_DIR']) + r'.*/bin'
  153. gcc = "g++"
  154. # Search for the compiler
  155. for path in env['ENV']['PATH'].split(path_separator):
  156. if not re.search(path_regex, path, re.IGNORECASE):
  157. continue
  158. for file in os.listdir(path):
  159. if not file.endswith(gcc):
  160. continue
  161. # Cache the g++ path to no search always
  162. if os.path.exists(ENV_BUILD_PATH):
  163. print('Caching g++ for current env')
  164. with open(GCC_PATH_CACHE, 'w+') as f:
  165. f.write(file)
  166. return file
  167. file = env.get('CXX')
  168. print("Couldn't find a compiler! Fallback to", file)
  169. return file
  170. #
  171. # Use the compiler to get a list of all enabled features
  172. #
  173. def load_marlin_features():
  174. if 'MARLIN_FEATURES' in env:
  175. return
  176. # Process defines
  177. #print(env.Dump())
  178. build_flags = env.get('BUILD_FLAGS')
  179. build_flags = env.ParseFlagsExtended(build_flags)
  180. cxx = search_compiler()
  181. cmd = [cxx]
  182. # Build flags from board.json
  183. #if 'BOARD' in env:
  184. # cmd += [env.BoardConfig().get("build.extra_flags")]
  185. for s in build_flags['CPPDEFINES']:
  186. if isinstance(s, tuple):
  187. cmd += ['-D' + s[0] + '=' + str(s[1])]
  188. else:
  189. cmd += ['-D' + s]
  190. cmd += ['-w -dM -E -x c++ buildroot/share/PlatformIO/scripts/common-dependencies.h']
  191. cmd = ' '.join(cmd)
  192. print(cmd)
  193. define_list = subprocess.check_output(cmd, shell=True).splitlines()
  194. marlin_features = {}
  195. for define in define_list:
  196. feature = define[8:].strip().decode().split(' ')
  197. feature, definition = feature[0], ' '.join(feature[1:])
  198. marlin_features[feature] = definition
  199. env['MARLIN_FEATURES'] = marlin_features
  200. #
  201. # Return True if a matching feature is enabled
  202. #
  203. def MarlinFeatureIsEnabled(env, feature):
  204. load_marlin_features()
  205. r = re.compile('^' + feature + '$')
  206. found = list(filter(r.match, env['MARLIN_FEATURES']))
  207. # Defines could still be 'false' or '0', so check
  208. some_on = False
  209. if len(found):
  210. for f in found:
  211. val = env['MARLIN_FEATURES'][f]
  212. if val in [ '', '1', 'true' ]:
  213. some_on = True
  214. elif val in env['MARLIN_FEATURES']:
  215. some_on = env.MarlinFeatureIsEnabled(val)
  216. return some_on
  217. #
  218. # Add a method for other PIO scripts to query enabled features
  219. #
  220. env.AddMethod(MarlinFeatureIsEnabled)
  221. #
  222. # Add dependencies for enabled Marlin features
  223. #
  224. apply_features_config()
  225. force_ignore_unused_libs()