common-dependencies.py 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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', 'build_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. if not 'lib_deps' in feat: feat['lib_deps'] = {}
  62. feat['lib_deps'] = list(filter(lib_re.match, feat['lib_deps'])) + [dep]
  63. blab("[%s] lib_deps = %s" % (feature, dep), 3)
  64. def load_features():
  65. blab("========== Gather [features] entries...")
  66. for key in ProjectConfig().items('features'):
  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. for n in env.GetProjectOptions():
  74. key = n[0]
  75. mat = re.match(r'custom_marlin\.(.+)', key)
  76. if mat:
  77. try:
  78. val = env.GetProjectOption(key)
  79. except:
  80. val = None
  81. if val:
  82. opt = mat[1].upper()
  83. blab("%s.custom_marlin.%s = '%s'" % ( env['PIOENV'], opt, val ))
  84. add_to_feat_cnf(opt, val)
  85. def get_all_known_libs():
  86. known_libs = []
  87. for feature in FEATURE_CONFIG:
  88. feat = FEATURE_CONFIG[feature]
  89. if not 'lib_deps' in feat:
  90. continue
  91. for dep in feat['lib_deps']:
  92. known_libs.append(PackageSpec(dep).name)
  93. return known_libs
  94. def get_all_env_libs():
  95. env_libs = []
  96. lib_deps = env.GetProjectOption('lib_deps')
  97. for dep in lib_deps:
  98. env_libs.append(PackageSpec(dep).name)
  99. return env_libs
  100. def set_env_field(field, value):
  101. proj = env.GetProjectConfig()
  102. proj.set("env:" + env['PIOENV'], field, value)
  103. # All unused libs should be ignored so that if a library
  104. # exists in .pio/lib_deps it will not break compilation.
  105. def force_ignore_unused_libs():
  106. env_libs = get_all_env_libs()
  107. known_libs = get_all_known_libs()
  108. diff = (list(set(known_libs) - set(env_libs)))
  109. lib_ignore = env.GetProjectOption('lib_ignore') + diff
  110. blab("Ignore libraries: %s" % lib_ignore)
  111. set_env_field('lib_ignore', lib_ignore)
  112. def apply_features_config():
  113. load_features()
  114. blab("========== Apply enabled features...")
  115. for feature in FEATURE_CONFIG:
  116. if not env.MarlinHas(feature):
  117. continue
  118. feat = FEATURE_CONFIG[feature]
  119. if 'lib_deps' in feat and len(feat['lib_deps']):
  120. blab("========== Adding lib_deps for %s... " % feature, 2)
  121. # feat to add
  122. deps_to_add = {}
  123. for dep in feat['lib_deps']:
  124. deps_to_add[PackageSpec(dep).name] = dep
  125. blab("==================== %s... " % dep, 2)
  126. # Does the env already have the dependency?
  127. deps = env.GetProjectOption('lib_deps')
  128. for dep in deps:
  129. name = PackageSpec(dep).name
  130. if name in deps_to_add:
  131. del deps_to_add[name]
  132. # Are there any libraries that should be ignored?
  133. lib_ignore = env.GetProjectOption('lib_ignore')
  134. for dep in deps:
  135. name = PackageSpec(dep).name
  136. if name in deps_to_add:
  137. del deps_to_add[name]
  138. # Is there anything left?
  139. if len(deps_to_add) > 0:
  140. # Only add the missing dependencies
  141. set_env_field('lib_deps', deps + list(deps_to_add.values()))
  142. if 'build_flags' in feat:
  143. f = feat['build_flags']
  144. blab("========== Adding build_flags for %s: %s" % (feature, f), 2)
  145. new_flags = env.GetProjectOption('build_flags') + [ f ]
  146. env.Replace(BUILD_FLAGS=new_flags)
  147. if 'extra_scripts' in feat:
  148. blab("Running extra_scripts for %s... " % feature, 2)
  149. env.SConscript(feat['extra_scripts'], exports="env")
  150. if 'build_src_filter' in feat:
  151. blab("========== Adding build_src_filter for %s... " % feature, 2)
  152. build_src_filter = ' '.join(env.GetProjectOption('build_src_filter'))
  153. # first we need to remove the references to the same folder
  154. my_srcs = re.findall(r'[+-](<.*?>)', feat['build_src_filter'])
  155. cur_srcs = re.findall(r'[+-](<.*?>)', build_src_filter)
  156. for d in my_srcs:
  157. if d in cur_srcs:
  158. build_src_filter = re.sub(r'[+-]' + d, '', build_src_filter)
  159. build_src_filter = feat['build_src_filter'] + ' ' + build_src_filter
  160. set_env_field('build_src_filter', [build_src_filter])
  161. env.Replace(SRC_FILTER=build_src_filter)
  162. if 'lib_ignore' in feat:
  163. blab("========== Adding lib_ignore for %s... " % feature, 2)
  164. lib_ignore = env.GetProjectOption('lib_ignore') + [feat['lib_ignore']]
  165. set_env_field('lib_ignore', lib_ignore)
  166. #
  167. # Use the compiler to get a list of all enabled features
  168. #
  169. def load_marlin_features():
  170. if 'MARLIN_FEATURES' in env:
  171. return
  172. # Process defines
  173. from preprocessor import run_preprocessor
  174. define_list = run_preprocessor(env)
  175. marlin_features = {}
  176. for define in define_list:
  177. feature = define[8:].strip().decode().split(' ')
  178. feature, definition = feature[0], ' '.join(feature[1:])
  179. marlin_features[feature] = definition
  180. env['MARLIN_FEATURES'] = marlin_features
  181. #
  182. # Return True if a matching feature is enabled
  183. #
  184. def MarlinHas(env, feature):
  185. load_marlin_features()
  186. r = re.compile('^' + feature + '$')
  187. found = list(filter(r.match, env['MARLIN_FEATURES']))
  188. # Defines could still be 'false' or '0', so check
  189. some_on = False
  190. if len(found):
  191. for f in found:
  192. val = env['MARLIN_FEATURES'][f]
  193. if val in [ '', '1', 'true' ]:
  194. some_on = True
  195. elif val in env['MARLIN_FEATURES']:
  196. some_on = env.MarlinHas(val)
  197. return some_on
  198. validate_pio()
  199. try:
  200. verbose = int(env.GetProjectOption('custom_verbose'))
  201. except:
  202. pass
  203. #
  204. # Add a method for other PIO scripts to query enabled features
  205. #
  206. env.AddMethod(MarlinHas)
  207. #
  208. # Add dependencies for enabled Marlin features
  209. #
  210. apply_features_config()
  211. force_ignore_unused_libs()
  212. #print(env.Dump())
  213. from signature import compute_build_signature
  214. compute_build_signature(env)