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.

common-dependencies.py 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 = (6, 0, 1)
  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 build_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('build_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. # Use the compiler to get a list of all enabled features
  169. #
  170. def load_marlin_features():
  171. if 'MARLIN_FEATURES' in env:
  172. return
  173. # Process defines
  174. from preprocessor import run_preprocessor
  175. define_list = run_preprocessor(env)
  176. marlin_features = {}
  177. for define in define_list:
  178. feature = define[8:].strip().decode().split(' ')
  179. feature, definition = feature[0], ' '.join(feature[1:])
  180. marlin_features[feature] = definition
  181. env['MARLIN_FEATURES'] = marlin_features
  182. #
  183. # Return True if a matching feature is enabled
  184. #
  185. def MarlinFeatureIsEnabled(env, feature):
  186. load_marlin_features()
  187. r = re.compile('^' + feature + '$')
  188. found = list(filter(r.match, env['MARLIN_FEATURES']))
  189. # Defines could still be 'false' or '0', so check
  190. some_on = False
  191. if len(found):
  192. for f in found:
  193. val = env['MARLIN_FEATURES'][f]
  194. if val in [ '', '1', 'true' ]:
  195. some_on = True
  196. elif val in env['MARLIN_FEATURES']:
  197. some_on = env.MarlinFeatureIsEnabled(val)
  198. return some_on
  199. validate_pio()
  200. try:
  201. verbose = int(env.GetProjectOption('custom_verbose'))
  202. except:
  203. pass
  204. #
  205. # Add a method for other PIO scripts to query enabled features
  206. #
  207. env.AddMethod(MarlinFeatureIsEnabled)
  208. #
  209. # Add dependencies for enabled Marlin features
  210. #
  211. apply_features_config()
  212. force_ignore_unused_libs()
  213. #print(env.Dump())
  214. from signature import compute_build_signature
  215. compute_build_signature(env)