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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. deps = re.sub(',\\s*', '\n', key[1]).strip().split('\n')
  21. if not key[0].upper() in FEATURE_DEPENDENCIES:
  22. FEATURE_DEPENDENCIES[key[0].upper()] = {
  23. 'lib_deps': []
  24. }
  25. for dep in deps:
  26. parts = dep.split('=')
  27. name = parts.pop(0)
  28. rest = '='.join(parts)
  29. if name == 'extra_scripts':
  30. FEATURE_DEPENDENCIES[key[0].upper()]['extra_scripts'] = rest
  31. elif name == 'src_filter':
  32. FEATURE_DEPENDENCIES[key[0].upper()]['src_filter'] = rest
  33. else:
  34. FEATURE_DEPENDENCIES[key[0].upper()]['lib_deps'] += [dep]
  35. def install_features_dependencies():
  36. load_config()
  37. for feature in FEATURE_DEPENDENCIES:
  38. if not env.MarlinFeatureIsEnabled(feature):
  39. continue
  40. if 'lib_deps' in FEATURE_DEPENDENCIES[feature]:
  41. print("Adding lib_deps for %s... " % feature)
  42. # deps to add
  43. deps_to_add = {}
  44. for dep in FEATURE_DEPENDENCIES[feature]['lib_deps']:
  45. name, _, _ = PackageManager.parse_pkg_uri(dep)
  46. deps_to_add[name] = dep
  47. # first check if the env already have the dep
  48. deps = env.GetProjectOption("lib_deps")
  49. for dep in deps:
  50. name, _, _ = PackageManager.parse_pkg_uri(dep)
  51. if name in deps_to_add:
  52. del deps_to_add[name]
  53. # check if we need ignore any lib
  54. lib_ignore = env.GetProjectOption("lib_ignore")
  55. for dep in deps:
  56. name, _, _ = PackageManager.parse_pkg_uri(dep)
  57. if name in deps_to_add:
  58. del deps_to_add[name]
  59. # any left?
  60. if len(deps_to_add) <= 0:
  61. continue
  62. # add only the missing deps
  63. proj = env.GetProjectConfig()
  64. proj.set("env:" + env["PIOENV"], "lib_deps", deps + list(deps_to_add.values()))
  65. if 'extra_scripts' in FEATURE_DEPENDENCIES[feature]:
  66. print("Executing extra_scripts for %s... " % feature)
  67. env.SConscript(FEATURE_DEPENDENCIES[feature]['extra_scripts'], exports="env")
  68. if 'src_filter' in FEATURE_DEPENDENCIES[feature]:
  69. print("Adding src_filter for %s... " % feature)
  70. proj = env.GetProjectConfig()
  71. src_filter = env.GetProjectOption("src_filter")
  72. # first we need to remove the references to the same folder
  73. my_srcs = re.findall( r'[+-](<.*?>)', FEATURE_DEPENDENCIES[feature]['src_filter'])
  74. cur_srcs = re.findall( r'[+-](<.*?>)', src_filter[0])
  75. for d in my_srcs:
  76. if d in cur_srcs:
  77. src_filter[0] = re.sub(r'[+-]' + d, '', src_filter[0])
  78. src_filter[0] = FEATURE_DEPENDENCIES[feature]['src_filter'] + ' ' + src_filter[0]
  79. proj.set("env:" + env["PIOENV"], "src_filter", src_filter)
  80. env.Replace(SRC_FILTER=src_filter)
  81. # load marlin features
  82. def load_marlin_features():
  83. if "MARLIN_FEATURES" in env:
  84. return
  85. # procces defines
  86. # print(env.Dump())
  87. build_flags = env.get('BUILD_FLAGS')
  88. build_flags = env.ParseFlagsExtended(build_flags)
  89. cmd = []
  90. # build flags from board.json
  91. # if 'BOARD' in env:
  92. # cmd += [env.BoardConfig().get("build.extra_flags")]
  93. for s in build_flags['CPPDEFINES']:
  94. if isinstance(s, tuple):
  95. cmd += ['-D' + s[0] + '=' + str(s[1])]
  96. else:
  97. cmd += ['-D' + s]
  98. # cmd += ['-w -dM -E -x c++ Marlin/src/inc/MarlinConfigPre.h']
  99. cmd += ['-w -dM -E -x c++ buildroot/share/PlatformIO/scripts/common-features-dependencies.h']
  100. cmd = [env.get('CXX')] + cmd
  101. cmd = ' '.join(cmd)
  102. print(cmd)
  103. define_list = subprocess.check_output(cmd, shell=True).splitlines()
  104. marlin_features = {}
  105. for define in define_list:
  106. feature = define[8:].strip().decode().split(' ')
  107. feature, definition = feature[0], ' '.join(feature[1:])
  108. marlin_features[feature] = definition
  109. env["MARLIN_FEATURES"] = marlin_features
  110. def MarlinFeatureIsEnabled(env, feature):
  111. load_marlin_features()
  112. return feature in env["MARLIN_FEATURES"]
  113. # add a method for others scripts to check if a feature is enabled
  114. env.AddMethod(MarlinFeatureIsEnabled)
  115. # install all dependencies for features enabled in Configuration.h
  116. install_features_dependencies()