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.6KB

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