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 9.4KB

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