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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 == 'extra_scripts':
  31. FEATURE_DEPENDENCIES[ukey]['extra_scripts'] = rest
  32. elif name == 'src_filter':
  33. FEATURE_DEPENDENCIES[ukey]['src_filter'] = rest
  34. elif name == 'lib_ignore':
  35. FEATURE_DEPENDENCIES[ukey]['lib_ignore'] = rest
  36. else:
  37. FEATURE_DEPENDENCIES[ukey]['lib_deps'] += [dep]
  38. def get_all_known_libs():
  39. known_libs = []
  40. for feature in FEATURE_DEPENDENCIES:
  41. if not 'lib_deps' in FEATURE_DEPENDENCIES[feature]:
  42. continue
  43. for dep in FEATURE_DEPENDENCIES[feature]['lib_deps']:
  44. name, _, _ = PackageManager.parse_pkg_uri(dep)
  45. known_libs.append(name)
  46. return known_libs
  47. def get_all_env_libs():
  48. env_libs = []
  49. lib_deps = env.GetProjectOption("lib_deps")
  50. for dep in lib_deps:
  51. name, _, _ = PackageManager.parse_pkg_uri(dep)
  52. env_libs.append(name)
  53. return env_libs
  54. # We need to ignore all non-used libs,
  55. # so if a lib folder lay forgotten in .pio/lib_deps, it
  56. # will not break compiling
  57. def force_ignore_unused_libs():
  58. env_libs = get_all_env_libs()
  59. known_libs = get_all_known_libs()
  60. diff = (list(set(known_libs) - set(env_libs)))
  61. lib_ignore = env.GetProjectOption("lib_ignore") + diff
  62. print("Ignoring libs: ", lib_ignore)
  63. proj = env.GetProjectConfig()
  64. proj.set("env:" + env["PIOENV"], "lib_ignore", lib_ignore)
  65. def install_features_dependencies():
  66. load_config()
  67. for feature in FEATURE_DEPENDENCIES:
  68. if not env.MarlinFeatureIsEnabled(feature):
  69. continue
  70. if 'lib_deps' in FEATURE_DEPENDENCIES[feature]:
  71. print("Adding lib_deps for %s... " % feature)
  72. # deps to add
  73. deps_to_add = {}
  74. for dep in FEATURE_DEPENDENCIES[feature]['lib_deps']:
  75. name, _, _ = PackageManager.parse_pkg_uri(dep)
  76. deps_to_add[name] = dep
  77. # first check if the env already have the dep
  78. deps = env.GetProjectOption("lib_deps")
  79. for dep in deps:
  80. name, _, _ = PackageManager.parse_pkg_uri(dep)
  81. if name in deps_to_add:
  82. del deps_to_add[name]
  83. # check if we need ignore any lib
  84. lib_ignore = env.GetProjectOption("lib_ignore")
  85. for dep in deps:
  86. name, _, _ = PackageManager.parse_pkg_uri(dep)
  87. if name in deps_to_add:
  88. del deps_to_add[name]
  89. # any left?
  90. if len(deps_to_add) > 0:
  91. # add only the missing deps
  92. proj = env.GetProjectConfig()
  93. proj.set("env:" + env["PIOENV"], "lib_deps", deps + list(deps_to_add.values()))
  94. if 'extra_scripts' in FEATURE_DEPENDENCIES[feature]:
  95. print("Executing extra_scripts for %s... " % feature)
  96. env.SConscript(FEATURE_DEPENDENCIES[feature]['extra_scripts'], exports="env")
  97. if 'src_filter' in FEATURE_DEPENDENCIES[feature]:
  98. print("Adding src_filter for %s... " % feature)
  99. proj = env.GetProjectConfig()
  100. src_filter = ' '.join(env.GetProjectOption("src_filter"))
  101. # first we need to remove the references to the same folder
  102. my_srcs = re.findall( r'[+-](<.*?>)', FEATURE_DEPENDENCIES[feature]['src_filter'])
  103. cur_srcs = re.findall( r'[+-](<.*?>)', src_filter)
  104. for d in my_srcs:
  105. if d in cur_srcs:
  106. src_filter = re.sub(r'[+-]' + d, '', src_filter)
  107. src_filter = FEATURE_DEPENDENCIES[feature]['src_filter'] + ' ' + src_filter
  108. proj.set("env:" + env["PIOENV"], "src_filter", [src_filter])
  109. env.Replace(SRC_FILTER=src_filter)
  110. # search the current compiler, considering the OS
  111. def search_compiler():
  112. if env['PLATFORM'] == 'win32':
  113. # the first path have the compiler
  114. for path in env['ENV']['PATH'].split(';'):
  115. if not re.search(r'platformio\\packages.*\\bin', path):
  116. continue
  117. #print(path)
  118. for file in os.listdir(path):
  119. if file.endswith("g++.exe"):
  120. return file
  121. print("Could not find the g++")
  122. return None
  123. else:
  124. return env.get('CXX')
  125. # load marlin features
  126. def load_marlin_features():
  127. if "MARLIN_FEATURES" in env:
  128. return
  129. # procces defines
  130. # print(env.Dump())
  131. build_flags = env.get('BUILD_FLAGS')
  132. build_flags = env.ParseFlagsExtended(build_flags)
  133. cxx = search_compiler()
  134. cmd = [cxx]
  135. # build flags from board.json
  136. # if 'BOARD' in env:
  137. # cmd += [env.BoardConfig().get("build.extra_flags")]
  138. for s in build_flags['CPPDEFINES']:
  139. if isinstance(s, tuple):
  140. cmd += ['-D' + s[0] + '=' + str(s[1])]
  141. else:
  142. cmd += ['-D' + s]
  143. # cmd += ['-w -dM -E -x c++ Marlin/src/inc/MarlinConfigPre.h']
  144. cmd += ['-w -dM -E -x c++ buildroot/share/PlatformIO/scripts/common-features-dependencies.h']
  145. cmd = ' '.join(cmd)
  146. print(cmd)
  147. define_list = subprocess.check_output(cmd, shell=True).splitlines()
  148. marlin_features = {}
  149. for define in define_list:
  150. feature = define[8:].strip().decode().split(' ')
  151. feature, definition = feature[0], ' '.join(feature[1:])
  152. marlin_features[feature] = definition
  153. env["MARLIN_FEATURES"] = marlin_features
  154. def MarlinFeatureIsEnabled(env, feature):
  155. load_marlin_features()
  156. r = re.compile(feature)
  157. matches = list(filter(r.match, env["MARLIN_FEATURES"]))
  158. return len(matches) > 0
  159. # add a method for others scripts to check if a feature is enabled
  160. env.AddMethod(MarlinFeatureIsEnabled)
  161. # install all dependencies for features enabled in Configuration.h
  162. install_features_dependencies()
  163. force_ignore_unused_libs()