My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

common-dependencies.py 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. #
  2. # common-dependencies.py
  3. # Convenience script to check dependencies and add libs and sources for Marlin Enabled Features
  4. #
  5. import pioutil
  6. if pioutil.is_pio_build():
  7. import subprocess,os,re
  8. Import("env")
  9. from platformio.package.meta import PackageSpec
  10. from platformio.project.config import ProjectConfig
  11. verbose = 0
  12. FEATURE_CONFIG = {}
  13. def validate_pio():
  14. PIO_VERSION_MIN = (5, 0, 3)
  15. try:
  16. from platformio import VERSION as PIO_VERSION
  17. weights = (1000, 100, 1)
  18. version_min = sum([x[0] * float(re.sub(r'[^0-9]', '.', str(x[1]))) for x in zip(weights, PIO_VERSION_MIN)])
  19. version_cur = sum([x[0] * float(re.sub(r'[^0-9]', '.', str(x[1]))) for x in zip(weights, PIO_VERSION)])
  20. if version_cur < version_min:
  21. print()
  22. print("**************************************************")
  23. print("****** An update to PlatformIO is ******")
  24. print("****** required to build Marlin Firmware. ******")
  25. print("****** ******")
  26. print("****** Minimum version: ", PIO_VERSION_MIN, " ******")
  27. print("****** Current Version: ", PIO_VERSION, " ******")
  28. print("****** ******")
  29. print("****** Update PlatformIO and try again. ******")
  30. print("**************************************************")
  31. print()
  32. exit(1)
  33. except SystemExit:
  34. exit(1)
  35. except:
  36. print("Can't detect PlatformIO Version")
  37. def blab(str,level=1):
  38. if verbose >= level:
  39. print("[deps] %s" % str)
  40. def add_to_feat_cnf(feature, flines):
  41. try:
  42. feat = FEATURE_CONFIG[feature]
  43. except:
  44. FEATURE_CONFIG[feature] = {}
  45. # Get a reference to the FEATURE_CONFIG under construction
  46. feat = FEATURE_CONFIG[feature]
  47. # Split up passed lines on commas or newlines and iterate
  48. # Add common options to the features config under construction
  49. # For lib_deps replace a previous instance of the same library
  50. atoms = re.sub(r',\\s*', '\n', flines).strip().split('\n')
  51. for line in atoms:
  52. parts = line.split('=')
  53. name = parts.pop(0)
  54. if name in ['build_flags', 'extra_scripts', 'src_filter', 'lib_ignore']:
  55. feat[name] = '='.join(parts)
  56. blab("[%s] %s=%s" % (feature, name, feat[name]), 3)
  57. else:
  58. for dep in re.split(r",\s*", line):
  59. lib_name = re.sub(r'@([~^]|[<>]=?)?[\d.]+', '', dep.strip()).split('=').pop(0)
  60. lib_re = re.compile('(?!^' + lib_name + '\\b)')
  61. feat['lib_deps'] = list(filter(lib_re.match, feat['lib_deps'])) + [dep]
  62. blab("[%s] lib_deps = %s" % (feature, dep), 3)
  63. def load_config():
  64. blab("========== Gather [features] entries...")
  65. items = ProjectConfig().items('features')
  66. for key in items:
  67. feature = key[0].upper()
  68. if not feature in FEATURE_CONFIG:
  69. FEATURE_CONFIG[feature] = { 'lib_deps': [] }
  70. add_to_feat_cnf(feature, key[1])
  71. # Add options matching custom_marlin.MY_OPTION to the pile
  72. blab("========== Gather custom_marlin entries...")
  73. all_opts = env.GetProjectOptions()
  74. for n in all_opts:
  75. key = n[0]
  76. mat = re.match(r'custom_marlin\.(.+)', key)
  77. if mat:
  78. try:
  79. val = env.GetProjectOption(key)
  80. except:
  81. val = None
  82. if val:
  83. opt = mat.group(1).upper()
  84. blab("%s.custom_marlin.%s = '%s'" % ( env['PIOENV'], opt, val ))
  85. add_to_feat_cnf(opt, val)
  86. def get_all_known_libs():
  87. known_libs = []
  88. for feature in FEATURE_CONFIG:
  89. feat = FEATURE_CONFIG[feature]
  90. if not 'lib_deps' in feat:
  91. continue
  92. for dep in feat['lib_deps']:
  93. known_libs.append(PackageSpec(dep).name)
  94. return known_libs
  95. def get_all_env_libs():
  96. env_libs = []
  97. lib_deps = env.GetProjectOption('lib_deps')
  98. for dep in lib_deps:
  99. env_libs.append(PackageSpec(dep).name)
  100. return env_libs
  101. def set_env_field(field, value):
  102. proj = env.GetProjectConfig()
  103. proj.set("env:" + env['PIOENV'], field, value)
  104. # All unused libs should be ignored so that if a library
  105. # exists in .pio/lib_deps it will not break compilation.
  106. def force_ignore_unused_libs():
  107. env_libs = get_all_env_libs()
  108. known_libs = get_all_known_libs()
  109. diff = (list(set(known_libs) - set(env_libs)))
  110. lib_ignore = env.GetProjectOption('lib_ignore') + diff
  111. blab("Ignore libraries: %s" % lib_ignore)
  112. set_env_field('lib_ignore', lib_ignore)
  113. def apply_features_config():
  114. load_config()
  115. blab("========== Apply enabled features...")
  116. for feature in FEATURE_CONFIG:
  117. if not env.MarlinFeatureIsEnabled(feature):
  118. continue
  119. feat = FEATURE_CONFIG[feature]
  120. if 'lib_deps' in feat and len(feat['lib_deps']):
  121. blab("========== Adding lib_deps for %s... " % feature, 2)
  122. # feat to add
  123. deps_to_add = {}
  124. for dep in feat['lib_deps']:
  125. deps_to_add[PackageSpec(dep).name] = dep
  126. blab("==================== %s... " % dep, 2)
  127. # Does the env already have the dependency?
  128. deps = env.GetProjectOption('lib_deps')
  129. for dep in deps:
  130. name = PackageSpec(dep).name
  131. if name in deps_to_add:
  132. del deps_to_add[name]
  133. # Are there any libraries that should be ignored?
  134. lib_ignore = env.GetProjectOption('lib_ignore')
  135. for dep in deps:
  136. name = PackageSpec(dep).name
  137. if name in deps_to_add:
  138. del deps_to_add[name]
  139. # Is there anything left?
  140. if len(deps_to_add) > 0:
  141. # Only add the missing dependencies
  142. set_env_field('lib_deps', deps + list(deps_to_add.values()))
  143. if 'build_flags' in feat:
  144. f = feat['build_flags']
  145. blab("========== Adding build_flags for %s: %s" % (feature, f), 2)
  146. new_flags = env.GetProjectOption('build_flags') + [ f ]
  147. env.Replace(BUILD_FLAGS=new_flags)
  148. if 'extra_scripts' in feat:
  149. blab("Running extra_scripts for %s... " % feature, 2)
  150. env.SConscript(feat['extra_scripts'], exports="env")
  151. if 'src_filter' in feat:
  152. blab("========== Adding src_filter for %s... " % feature, 2)
  153. src_filter = ' '.join(env.GetProjectOption('src_filter'))
  154. # first we need to remove the references to the same folder
  155. my_srcs = re.findall(r'[+-](<.*?>)', feat['src_filter'])
  156. cur_srcs = re.findall(r'[+-](<.*?>)', src_filter)
  157. for d in my_srcs:
  158. if d in cur_srcs:
  159. src_filter = re.sub(r'[+-]' + d, '', src_filter)
  160. src_filter = feat['src_filter'] + ' ' + src_filter
  161. set_env_field('src_filter', [src_filter])
  162. env.Replace(SRC_FILTER=src_filter)
  163. if 'lib_ignore' in feat:
  164. blab("========== Adding lib_ignore for %s... " % feature, 2)
  165. lib_ignore = env.GetProjectOption('lib_ignore') + [feat['lib_ignore']]
  166. set_env_field('lib_ignore', lib_ignore)
  167. #
  168. # Find a compiler, considering the OS
  169. #
  170. ENV_BUILD_PATH = os.path.join(env.Dictionary('PROJECT_BUILD_DIR'), env['PIOENV'])
  171. GCC_PATH_CACHE = os.path.join(ENV_BUILD_PATH, ".gcc_path")
  172. def search_compiler():
  173. try:
  174. filepath = env.GetProjectOption('custom_gcc')
  175. blab("Getting compiler from env")
  176. return filepath
  177. except:
  178. pass
  179. if os.path.exists(GCC_PATH_CACHE):
  180. with open(GCC_PATH_CACHE, 'r') as f:
  181. return f.read()
  182. # Find the current platform compiler by searching the $PATH
  183. # which will be in a platformio toolchain bin folder
  184. path_regex = re.escape(env['PROJECT_PACKAGES_DIR'])
  185. # See if the environment provides a default compiler
  186. try:
  187. gcc = env.GetProjectOption('custom_deps_gcc')
  188. except:
  189. gcc = "g++"
  190. if env['PLATFORM'] == 'win32':
  191. path_separator = ';'
  192. path_regex += r'.*\\bin'
  193. gcc += ".exe"
  194. else:
  195. path_separator = ':'
  196. path_regex += r'/.+/bin'
  197. # Search for the compiler
  198. for pathdir in env['ENV']['PATH'].split(path_separator):
  199. if not re.search(path_regex, pathdir, re.IGNORECASE):
  200. continue
  201. for filepath in os.listdir(pathdir):
  202. if not filepath.endswith(gcc):
  203. continue
  204. # Use entire path to not rely on env PATH
  205. filepath = os.path.sep.join([pathdir, filepath])
  206. # Cache the g++ path to no search always
  207. if os.path.exists(ENV_BUILD_PATH):
  208. with open(GCC_PATH_CACHE, 'w+') as f:
  209. f.write(filepath)
  210. return filepath
  211. filepath = env.get('CXX')
  212. if filepath == 'CC':
  213. filepath = gcc
  214. blab("Couldn't find a compiler! Fallback to %s" % filepath)
  215. return filepath
  216. #
  217. # Use the compiler to get a list of all enabled features
  218. #
  219. def load_marlin_features():
  220. if 'MARLIN_FEATURES' in env:
  221. return
  222. # Process defines
  223. build_flags = env.get('BUILD_FLAGS')
  224. build_flags = env.ParseFlagsExtended(build_flags)
  225. cxx = search_compiler()
  226. cmd = ['"' + cxx + '"']
  227. # Build flags from board.json
  228. #if 'BOARD' in env:
  229. # cmd += [env.BoardConfig().get("build.extra_flags")]
  230. for s in build_flags['CPPDEFINES']:
  231. if isinstance(s, tuple):
  232. cmd += ['-D' + s[0] + '=' + str(s[1])]
  233. else:
  234. cmd += ['-D' + s]
  235. cmd += ['-D__MARLIN_DEPS__ -w -dM -E -x c++ buildroot/share/PlatformIO/scripts/common-dependencies.h']
  236. cmd = ' '.join(cmd)
  237. blab(cmd, 4)
  238. define_list = subprocess.check_output(cmd, shell=True).splitlines()
  239. marlin_features = {}
  240. for define in define_list:
  241. feature = define[8:].strip().decode().split(' ')
  242. feature, definition = feature[0], ' '.join(feature[1:])
  243. marlin_features[feature] = definition
  244. env['MARLIN_FEATURES'] = marlin_features
  245. #
  246. # Return True if a matching feature is enabled
  247. #
  248. def MarlinFeatureIsEnabled(env, feature):
  249. load_marlin_features()
  250. r = re.compile('^' + feature + '$')
  251. found = list(filter(r.match, env['MARLIN_FEATURES']))
  252. # Defines could still be 'false' or '0', so check
  253. some_on = False
  254. if len(found):
  255. for f in found:
  256. val = env['MARLIN_FEATURES'][f]
  257. if val in [ '', '1', 'true' ]:
  258. some_on = True
  259. elif val in env['MARLIN_FEATURES']:
  260. some_on = env.MarlinFeatureIsEnabled(val)
  261. return some_on
  262. validate_pio()
  263. try:
  264. verbose = int(env.GetProjectOption('custom_verbose'))
  265. except:
  266. pass
  267. # Add a method for other PIO scripts to query enabled features
  268. env.AddMethod(MarlinFeatureIsEnabled)
  269. # Add dependencies for enabled Marlin features
  270. apply_features_config()
  271. force_ignore_unused_libs()