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-features-dependencies.py 6.6KB

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