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

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