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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. from platformio.managers.package import PackageManager
  13. Import("env")
  14. FEATURE_CONFIG = {}
  15. def add_to_feat_cnf(feature, flines):
  16. feat = FEATURE_CONFIG[feature]
  17. atoms = re.sub(',\\s*', '\n', flines).strip().split('\n')
  18. for dep in atoms:
  19. parts = dep.split('=')
  20. name = parts.pop(0)
  21. rest = '='.join(parts)
  22. if name in ['extra_scripts', 'src_filter', 'lib_ignore']:
  23. feat[name] = rest
  24. else:
  25. feat['lib_deps'] += [dep]
  26. def load_config():
  27. config = configparser.ConfigParser()
  28. config.read("platformio.ini")
  29. items = config.items('features')
  30. for key in items:
  31. feature = key[0].upper()
  32. if not feature in FEATURE_CONFIG:
  33. FEATURE_CONFIG[feature] = { 'lib_deps': [] }
  34. add_to_feat_cnf(feature, key[1])
  35. # Add options matching custom_marlin.MY_OPTION to the pile
  36. all_opts = env.GetProjectOptions()
  37. for n in all_opts:
  38. mat = re.match(r'custom_marlin\.(.+)', n[0])
  39. if mat:
  40. try:
  41. val = env.GetProjectOption(n[0])
  42. except:
  43. val = None
  44. if val:
  45. add_to_feat_cnf(mat.group(1).upper(), val)
  46. def get_all_known_libs():
  47. known_libs = []
  48. for feature in FEATURE_CONFIG:
  49. feat = FEATURE_CONFIG[feature]
  50. if not 'lib_deps' in feat:
  51. continue
  52. for dep in feat['lib_deps']:
  53. name, _, _ = PackageManager.parse_pkg_uri(dep)
  54. known_libs.append(name)
  55. return known_libs
  56. def get_all_env_libs():
  57. env_libs = []
  58. lib_deps = env.GetProjectOption('lib_deps')
  59. for dep in lib_deps:
  60. name, _, _ = PackageManager.parse_pkg_uri(dep)
  61. env_libs.append(name)
  62. return env_libs
  63. def set_env_field(field, value):
  64. proj = env.GetProjectConfig()
  65. proj.set("env:" + env['PIOENV'], field, value)
  66. # All unused libs should be ignored so that if a library
  67. # exists in .pio/lib_deps it will not break compilation.
  68. def force_ignore_unused_libs():
  69. env_libs = get_all_env_libs()
  70. known_libs = get_all_known_libs()
  71. diff = (list(set(known_libs) - set(env_libs)))
  72. lib_ignore = env.GetProjectOption('lib_ignore') + diff
  73. print("Ignore libraries:", lib_ignore)
  74. set_env_field('lib_ignore', lib_ignore)
  75. def apply_features_config():
  76. load_config()
  77. for feature in FEATURE_CONFIG:
  78. if not env.MarlinFeatureIsEnabled(feature):
  79. continue
  80. feat = FEATURE_CONFIG[feature]
  81. if 'lib_deps' in feat and len(feat['lib_deps']):
  82. print("Adding lib_deps for %s... " % feature)
  83. # feat to add
  84. deps_to_add = {}
  85. for dep in feat['lib_deps']:
  86. name, _, _ = PackageManager.parse_pkg_uri(dep)
  87. deps_to_add[name] = dep
  88. # Does the env already have the dependency?
  89. deps = env.GetProjectOption('lib_deps')
  90. for dep in deps:
  91. name, _, _ = PackageManager.parse_pkg_uri(dep)
  92. if name in deps_to_add:
  93. del deps_to_add[name]
  94. # Are there any libraries that should be ignored?
  95. lib_ignore = env.GetProjectOption('lib_ignore')
  96. for dep in deps:
  97. name, _, _ = PackageManager.parse_pkg_uri(dep)
  98. if name in deps_to_add:
  99. del deps_to_add[name]
  100. # Is there anything left?
  101. if len(deps_to_add) > 0:
  102. # Only add the missing dependencies
  103. set_env_field('lib_deps', deps + list(deps_to_add.values()))
  104. if 'extra_scripts' in feat:
  105. print("Running extra_scripts for %s... " % feature)
  106. env.SConscript(feat['extra_scripts'], exports="env")
  107. if 'src_filter' in feat:
  108. print("Adding src_filter for %s... " % feature)
  109. src_filter = ' '.join(env.GetProjectOption('src_filter'))
  110. # first we need to remove the references to the same folder
  111. my_srcs = re.findall( r'[+-](<.*?>)', feat['src_filter'])
  112. cur_srcs = re.findall( r'[+-](<.*?>)', src_filter)
  113. for d in my_srcs:
  114. if d in cur_srcs:
  115. src_filter = re.sub(r'[+-]' + d, '', src_filter)
  116. src_filter = feat['src_filter'] + ' ' + src_filter
  117. set_env_field('src_filter', [src_filter])
  118. env.Replace(SRC_FILTER=src_filter)
  119. if 'lib_ignore' in feat:
  120. print("Adding lib_ignore for %s... " % feature)
  121. lib_ignore = env.GetProjectOption('lib_ignore') + [feat['lib_ignore']]
  122. set_env_field('lib_ignore', lib_ignore)
  123. #
  124. # Find a compiler, considering the OS
  125. #
  126. ENV_BUILD_PATH = os.path.join(env.Dictionary('PROJECT_BUILD_DIR'), env['PIOENV'])
  127. GCC_PATH_CACHE = os.path.join(ENV_BUILD_PATH, ".gcc_path")
  128. def search_compiler():
  129. if os.path.exists(GCC_PATH_CACHE):
  130. print('Getting g++ path from cache')
  131. with open(GCC_PATH_CACHE, 'r') as f:
  132. return f.read()
  133. # PlatformIO inserts the toolchain bin folder on the front of the $PATH
  134. # Find the current platform compiler by searching the $PATH
  135. if env['PLATFORM'] == 'win32':
  136. path_separator = ';'
  137. path_regex = re.escape(env['PROJECT_PACKAGES_DIR']) + r'.*\\bin'
  138. gcc = "g++.exe"
  139. else:
  140. path_separator = ':'
  141. path_regex = re.escape(env['PROJECT_PACKAGES_DIR']) + r'.*/bin'
  142. gcc = "g++"
  143. # Search for the compiler
  144. for path in env['ENV']['PATH'].split(path_separator):
  145. if not re.search(path_regex, path, re.IGNORECASE):
  146. continue
  147. for file in os.listdir(path):
  148. if not file.endswith(gcc):
  149. continue
  150. # Cache the g++ path to no search always
  151. if os.path.exists(ENV_BUILD_PATH):
  152. print('Caching g++ for current env')
  153. with open(GCC_PATH_CACHE, 'w+') as f:
  154. f.write(file)
  155. return file
  156. file = env.get('CXX')
  157. print("Couldn't find a compiler! Fallback to", file)
  158. return file
  159. #
  160. # Use the compiler to get a list of all enabled features
  161. #
  162. def load_marlin_features():
  163. if 'MARLIN_FEATURES' in env:
  164. return
  165. # Process defines
  166. #print(env.Dump())
  167. build_flags = env.get('BUILD_FLAGS')
  168. build_flags = env.ParseFlagsExtended(build_flags)
  169. cxx = search_compiler()
  170. cmd = [cxx]
  171. # Build flags from board.json
  172. #if 'BOARD' in env:
  173. # cmd += [env.BoardConfig().get("build.extra_flags")]
  174. for s in build_flags['CPPDEFINES']:
  175. if isinstance(s, tuple):
  176. cmd += ['-D' + s[0] + '=' + str(s[1])]
  177. else:
  178. cmd += ['-D' + s]
  179. cmd += ['-w -dM -E -x c++ buildroot/share/PlatformIO/scripts/common-dependencies.h']
  180. cmd = ' '.join(cmd)
  181. print(cmd)
  182. define_list = subprocess.check_output(cmd, shell=True).splitlines()
  183. marlin_features = {}
  184. for define in define_list:
  185. feature = define[8:].strip().decode().split(' ')
  186. feature, definition = feature[0], ' '.join(feature[1:])
  187. marlin_features[feature] = definition
  188. env['MARLIN_FEATURES'] = marlin_features
  189. #
  190. # Return True if a matching feature is enabled
  191. #
  192. def MarlinFeatureIsEnabled(env, feature):
  193. load_marlin_features()
  194. r = re.compile('^' + feature + '$')
  195. found = list(filter(r.match, env['MARLIN_FEATURES']))
  196. # Defines could still be 'false' or '0', so check
  197. some_on = False
  198. if len(found):
  199. for f in found:
  200. val = env['MARLIN_FEATURES'][f]
  201. if val in [ '', '1', 'true' ]:
  202. some_on = True
  203. elif val in env['MARLIN_FEATURES']:
  204. some_on = env.MarlinFeatureIsEnabled(val)
  205. return some_on
  206. #
  207. # Add a method for other PIO scripts to query enabled features
  208. #
  209. env.AddMethod(MarlinFeatureIsEnabled)
  210. #
  211. # Add dependencies for enabled Marlin features
  212. #
  213. apply_features_config()
  214. force_ignore_unused_libs()