2020-07-20 02:42:30 +00:00
|
|
|
#
|
2020-07-26 02:40:44 +00:00
|
|
|
# common-dependencies.py
|
2020-07-20 02:42:30 +00:00
|
|
|
# Convenience script to check dependencies and add libs and sources for Marlin Enabled Features
|
|
|
|
#
|
2021-02-28 04:38:57 +00:00
|
|
|
import subprocess,os,re
|
2020-07-20 02:42:30 +00:00
|
|
|
|
2020-12-03 17:23:48 +00:00
|
|
|
PIO_VERSION_MIN = (5, 0, 3)
|
|
|
|
try:
|
|
|
|
from platformio import VERSION as PIO_VERSION
|
|
|
|
weights = (1000, 100, 1)
|
|
|
|
version_min = sum([x[0] * float(re.sub(r'[^0-9]', '.', str(x[1]))) for x in zip(weights, PIO_VERSION_MIN)])
|
|
|
|
version_cur = sum([x[0] * float(re.sub(r'[^0-9]', '.', str(x[1]))) for x in zip(weights, PIO_VERSION)])
|
|
|
|
if version_cur < version_min:
|
|
|
|
print()
|
|
|
|
print("**************************************************")
|
|
|
|
print("****** An update to PlatformIO is ******")
|
|
|
|
print("****** required to build Marlin Firmware. ******")
|
|
|
|
print("****** ******")
|
|
|
|
print("****** Minimum version: ", PIO_VERSION_MIN, " ******")
|
|
|
|
print("****** Current Version: ", PIO_VERSION, " ******")
|
|
|
|
print("****** ******")
|
|
|
|
print("****** Update PlatformIO and try again. ******")
|
|
|
|
print("**************************************************")
|
|
|
|
print()
|
|
|
|
exit(1)
|
|
|
|
except SystemExit:
|
|
|
|
exit(1)
|
|
|
|
except:
|
|
|
|
print("Can't detect PlatformIO Version")
|
|
|
|
|
2021-04-02 02:53:19 +00:00
|
|
|
from platformio.package.meta import PackageSpec
|
|
|
|
from platformio.project.config import ProjectConfig
|
|
|
|
|
2021-04-19 03:46:43 +00:00
|
|
|
Import("env")
|
|
|
|
|
|
|
|
#print(env.Dump())
|
|
|
|
|
2020-08-20 07:47:16 +00:00
|
|
|
try:
|
|
|
|
verbose = int(env.GetProjectOption('custom_verbose'))
|
|
|
|
except:
|
|
|
|
verbose = 0
|
|
|
|
|
2021-04-20 10:11:43 +00:00
|
|
|
def blab(str,level=1):
|
|
|
|
if verbose >= level:
|
|
|
|
print("[deps] %s" % str)
|
2020-08-06 13:14:00 +00:00
|
|
|
|
2020-08-20 07:47:16 +00:00
|
|
|
FEATURE_CONFIG = {}
|
|
|
|
|
2020-08-06 13:14:00 +00:00
|
|
|
def add_to_feat_cnf(feature, flines):
|
2020-10-09 21:42:23 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
feat = FEATURE_CONFIG[feature]
|
|
|
|
except:
|
|
|
|
FEATURE_CONFIG[feature] = {}
|
|
|
|
|
2021-01-23 02:51:58 +00:00
|
|
|
# Get a reference to the FEATURE_CONFIG under construction
|
2020-08-06 13:14:00 +00:00
|
|
|
feat = FEATURE_CONFIG[feature]
|
2021-01-23 02:51:58 +00:00
|
|
|
|
|
|
|
# Split up passed lines on commas or newlines and iterate
|
|
|
|
# Add common options to the features config under construction
|
|
|
|
# For lib_deps replace a previous instance of the same library
|
|
|
|
atoms = re.sub(r',\\s*', '\n', flines).strip().split('\n')
|
|
|
|
for line in atoms:
|
|
|
|
parts = line.split('=')
|
2020-08-06 13:14:00 +00:00
|
|
|
name = parts.pop(0)
|
2021-01-06 03:03:13 +00:00
|
|
|
if name in ['build_flags', 'extra_scripts', 'src_filter', 'lib_ignore']:
|
2021-01-23 02:51:58 +00:00
|
|
|
feat[name] = '='.join(parts)
|
2021-04-20 10:11:43 +00:00
|
|
|
blab("[%s] %s=%s" % (feature, name, feat[name]), 3)
|
2020-08-06 13:14:00 +00:00
|
|
|
else:
|
2021-04-20 10:11:43 +00:00
|
|
|
for dep in re.split(r",\s*", line):
|
2021-01-25 03:20:51 +00:00
|
|
|
lib_name = re.sub(r'@([~^]|[<>]=?)?[\d.]+', '', dep.strip()).split('=').pop(0)
|
2021-01-23 02:51:58 +00:00
|
|
|
lib_re = re.compile('(?!^' + lib_name + '\\b)')
|
|
|
|
feat['lib_deps'] = list(filter(lib_re.match, feat['lib_deps'])) + [dep]
|
2021-04-20 10:11:43 +00:00
|
|
|
blab("[%s] lib_deps = %s" % (feature, dep), 3)
|
2020-07-20 02:42:30 +00:00
|
|
|
|
|
|
|
def load_config():
|
2021-04-20 10:11:43 +00:00
|
|
|
blab("========== Gather [features] entries...")
|
2021-04-02 02:53:19 +00:00
|
|
|
items = ProjectConfig().items('features')
|
2020-07-20 02:42:30 +00:00
|
|
|
for key in items:
|
2020-08-06 13:14:00 +00:00
|
|
|
feature = key[0].upper()
|
|
|
|
if not feature in FEATURE_CONFIG:
|
|
|
|
FEATURE_CONFIG[feature] = { 'lib_deps': [] }
|
|
|
|
add_to_feat_cnf(feature, key[1])
|
|
|
|
|
2020-08-06 22:33:42 +00:00
|
|
|
# Add options matching custom_marlin.MY_OPTION to the pile
|
2021-04-20 10:11:43 +00:00
|
|
|
blab("========== Gather custom_marlin entries...")
|
2020-08-06 13:14:00 +00:00
|
|
|
all_opts = env.GetProjectOptions()
|
|
|
|
for n in all_opts:
|
2021-04-20 10:11:43 +00:00
|
|
|
key = n[0]
|
|
|
|
mat = re.match(r'custom_marlin\.(.+)', key)
|
2020-08-06 13:14:00 +00:00
|
|
|
if mat:
|
|
|
|
try:
|
2021-04-20 10:11:43 +00:00
|
|
|
val = env.GetProjectOption(key)
|
2020-08-06 13:14:00 +00:00
|
|
|
except:
|
|
|
|
val = None
|
|
|
|
if val:
|
2021-04-20 10:11:43 +00:00
|
|
|
opt = mat.group(1).upper()
|
|
|
|
blab("%s.custom_marlin.%s = '%s'" % ( env['PIOENV'], opt, val ))
|
|
|
|
add_to_feat_cnf(opt, val)
|
2020-07-20 02:42:30 +00:00
|
|
|
|
2020-07-21 07:33:58 +00:00
|
|
|
def get_all_known_libs():
|
|
|
|
known_libs = []
|
2020-08-06 13:14:00 +00:00
|
|
|
for feature in FEATURE_CONFIG:
|
|
|
|
feat = FEATURE_CONFIG[feature]
|
|
|
|
if not 'lib_deps' in feat:
|
2020-07-21 07:33:58 +00:00
|
|
|
continue
|
2020-08-06 13:14:00 +00:00
|
|
|
for dep in feat['lib_deps']:
|
2021-04-02 02:53:19 +00:00
|
|
|
known_libs.append(PackageSpec(dep).name)
|
2020-07-21 07:33:58 +00:00
|
|
|
return known_libs
|
|
|
|
|
|
|
|
def get_all_env_libs():
|
|
|
|
env_libs = []
|
2020-07-26 03:57:00 +00:00
|
|
|
lib_deps = env.GetProjectOption('lib_deps')
|
2020-07-21 07:33:58 +00:00
|
|
|
for dep in lib_deps:
|
2021-04-02 02:53:19 +00:00
|
|
|
env_libs.append(PackageSpec(dep).name)
|
2020-07-21 07:33:58 +00:00
|
|
|
return env_libs
|
|
|
|
|
2020-07-26 03:57:00 +00:00
|
|
|
def set_env_field(field, value):
|
|
|
|
proj = env.GetProjectConfig()
|
|
|
|
proj.set("env:" + env['PIOENV'], field, value)
|
|
|
|
|
2020-07-26 01:52:44 +00:00
|
|
|
# All unused libs should be ignored so that if a library
|
|
|
|
# exists in .pio/lib_deps it will not break compilation.
|
2020-07-21 07:33:58 +00:00
|
|
|
def force_ignore_unused_libs():
|
|
|
|
env_libs = get_all_env_libs()
|
|
|
|
known_libs = get_all_known_libs()
|
|
|
|
diff = (list(set(known_libs) - set(env_libs)))
|
2020-07-26 03:57:00 +00:00
|
|
|
lib_ignore = env.GetProjectOption('lib_ignore') + diff
|
2021-01-09 00:51:54 +00:00
|
|
|
blab("Ignore libraries: %s" % lib_ignore)
|
2020-07-26 03:57:00 +00:00
|
|
|
set_env_field('lib_ignore', lib_ignore)
|
2020-07-21 07:33:58 +00:00
|
|
|
|
2020-08-06 13:14:00 +00:00
|
|
|
def apply_features_config():
|
2020-07-20 02:42:30 +00:00
|
|
|
load_config()
|
2021-04-20 10:11:43 +00:00
|
|
|
blab("========== Apply enabled features...")
|
2020-08-06 13:14:00 +00:00
|
|
|
for feature in FEATURE_CONFIG:
|
2020-07-20 02:42:30 +00:00
|
|
|
if not env.MarlinFeatureIsEnabled(feature):
|
|
|
|
continue
|
|
|
|
|
2020-08-06 13:14:00 +00:00
|
|
|
feat = FEATURE_CONFIG[feature]
|
|
|
|
|
|
|
|
if 'lib_deps' in feat and len(feat['lib_deps']):
|
2021-04-20 10:11:43 +00:00
|
|
|
blab("========== Adding lib_deps for %s... " % feature, 2)
|
2020-07-20 02:42:30 +00:00
|
|
|
|
2020-08-06 13:14:00 +00:00
|
|
|
# feat to add
|
2020-07-20 02:42:30 +00:00
|
|
|
deps_to_add = {}
|
2020-08-06 13:14:00 +00:00
|
|
|
for dep in feat['lib_deps']:
|
2021-04-02 02:53:19 +00:00
|
|
|
deps_to_add[PackageSpec(dep).name] = dep
|
2021-04-20 10:11:43 +00:00
|
|
|
blab("==================== %s... " % dep, 2)
|
2020-07-20 02:42:30 +00:00
|
|
|
|
2020-07-26 01:52:44 +00:00
|
|
|
# Does the env already have the dependency?
|
2020-07-26 03:57:00 +00:00
|
|
|
deps = env.GetProjectOption('lib_deps')
|
2020-07-20 02:42:30 +00:00
|
|
|
for dep in deps:
|
2021-04-02 02:53:19 +00:00
|
|
|
name = PackageSpec(dep).name
|
2020-07-20 02:42:30 +00:00
|
|
|
if name in deps_to_add:
|
|
|
|
del deps_to_add[name]
|
|
|
|
|
2020-07-26 01:52:44 +00:00
|
|
|
# Are there any libraries that should be ignored?
|
2020-07-26 03:57:00 +00:00
|
|
|
lib_ignore = env.GetProjectOption('lib_ignore')
|
2020-07-20 02:42:30 +00:00
|
|
|
for dep in deps:
|
2021-04-02 02:53:19 +00:00
|
|
|
name = PackageSpec(dep).name
|
2020-07-20 02:42:30 +00:00
|
|
|
if name in deps_to_add:
|
|
|
|
del deps_to_add[name]
|
|
|
|
|
2020-07-26 01:52:44 +00:00
|
|
|
# Is there anything left?
|
2020-07-21 08:15:20 +00:00
|
|
|
if len(deps_to_add) > 0:
|
2020-07-26 01:52:44 +00:00
|
|
|
# Only add the missing dependencies
|
2020-07-26 03:57:00 +00:00
|
|
|
set_env_field('lib_deps', deps + list(deps_to_add.values()))
|
2020-07-20 02:42:30 +00:00
|
|
|
|
2021-01-06 03:03:13 +00:00
|
|
|
if 'build_flags' in feat:
|
|
|
|
f = feat['build_flags']
|
2021-04-20 10:11:43 +00:00
|
|
|
blab("========== Adding build_flags for %s: %s" % (feature, f), 2)
|
2021-01-06 03:03:13 +00:00
|
|
|
new_flags = env.GetProjectOption('build_flags') + [ f ]
|
|
|
|
env.Replace(BUILD_FLAGS=new_flags)
|
|
|
|
|
2020-08-06 13:14:00 +00:00
|
|
|
if 'extra_scripts' in feat:
|
2021-04-20 10:11:43 +00:00
|
|
|
blab("Running extra_scripts for %s... " % feature, 2)
|
2020-08-06 13:14:00 +00:00
|
|
|
env.SConscript(feat['extra_scripts'], exports="env")
|
2020-07-20 02:42:30 +00:00
|
|
|
|
2020-08-06 13:14:00 +00:00
|
|
|
if 'src_filter' in feat:
|
2021-04-20 10:11:43 +00:00
|
|
|
blab("========== Adding src_filter for %s... " % feature, 2)
|
2020-07-26 03:57:00 +00:00
|
|
|
src_filter = ' '.join(env.GetProjectOption('src_filter'))
|
2020-07-20 02:42:30 +00:00
|
|
|
# first we need to remove the references to the same folder
|
2021-01-23 02:51:58 +00:00
|
|
|
my_srcs = re.findall(r'[+-](<.*?>)', feat['src_filter'])
|
|
|
|
cur_srcs = re.findall(r'[+-](<.*?>)', src_filter)
|
2020-07-20 02:42:30 +00:00
|
|
|
for d in my_srcs:
|
|
|
|
if d in cur_srcs:
|
2020-07-21 08:15:20 +00:00
|
|
|
src_filter = re.sub(r'[+-]' + d, '', src_filter)
|
2020-07-20 02:42:30 +00:00
|
|
|
|
2020-08-06 13:14:00 +00:00
|
|
|
src_filter = feat['src_filter'] + ' ' + src_filter
|
2020-07-26 03:57:00 +00:00
|
|
|
set_env_field('src_filter', [src_filter])
|
2020-07-20 02:42:30 +00:00
|
|
|
env.Replace(SRC_FILTER=src_filter)
|
|
|
|
|
2020-08-06 13:14:00 +00:00
|
|
|
if 'lib_ignore' in feat:
|
2021-04-20 10:11:43 +00:00
|
|
|
blab("========== Adding lib_ignore for %s... " % feature, 2)
|
2020-08-06 13:14:00 +00:00
|
|
|
lib_ignore = env.GetProjectOption('lib_ignore') + [feat['lib_ignore']]
|
2020-07-26 03:57:00 +00:00
|
|
|
set_env_field('lib_ignore', lib_ignore)
|
2020-07-24 04:47:01 +00:00
|
|
|
|
2020-07-26 01:52:44 +00:00
|
|
|
#
|
|
|
|
# Find a compiler, considering the OS
|
|
|
|
#
|
2020-07-26 03:57:00 +00:00
|
|
|
ENV_BUILD_PATH = os.path.join(env.Dictionary('PROJECT_BUILD_DIR'), env['PIOENV'])
|
2020-07-26 01:52:44 +00:00
|
|
|
GCC_PATH_CACHE = os.path.join(ENV_BUILD_PATH, ".gcc_path")
|
2020-07-20 09:52:15 +00:00
|
|
|
def search_compiler():
|
2020-08-20 07:47:16 +00:00
|
|
|
try:
|
|
|
|
filepath = env.GetProjectOption('custom_gcc')
|
2021-01-09 00:51:54 +00:00
|
|
|
blab("Getting compiler from env")
|
2020-08-20 07:47:16 +00:00
|
|
|
return filepath
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
2020-07-26 01:52:44 +00:00
|
|
|
if os.path.exists(GCC_PATH_CACHE):
|
|
|
|
with open(GCC_PATH_CACHE, 'r') as f:
|
|
|
|
return f.read()
|
|
|
|
|
|
|
|
# Find the current platform compiler by searching the $PATH
|
2020-08-20 07:47:16 +00:00
|
|
|
# which will be in a platformio toolchain bin folder
|
|
|
|
path_regex = re.escape(env['PROJECT_PACKAGES_DIR'])
|
2021-07-22 00:01:23 +00:00
|
|
|
|
|
|
|
# See if the environment provides a default compiler
|
|
|
|
try:
|
|
|
|
gcc = env.GetProjectOption('custom_deps_gcc')
|
|
|
|
except:
|
|
|
|
gcc = "g++"
|
|
|
|
|
2020-07-20 09:52:15 +00:00
|
|
|
if env['PLATFORM'] == 'win32':
|
2020-07-26 01:52:44 +00:00
|
|
|
path_separator = ';'
|
2020-08-20 07:47:16 +00:00
|
|
|
path_regex += r'.*\\bin'
|
|
|
|
gcc += ".exe"
|
2020-07-20 09:52:15 +00:00
|
|
|
else:
|
2020-07-26 01:52:44 +00:00
|
|
|
path_separator = ':'
|
2020-08-20 07:47:16 +00:00
|
|
|
path_regex += r'/.+/bin'
|
2020-07-26 01:52:44 +00:00
|
|
|
|
|
|
|
# Search for the compiler
|
2020-08-20 07:47:16 +00:00
|
|
|
for pathdir in env['ENV']['PATH'].split(path_separator):
|
|
|
|
if not re.search(path_regex, pathdir, re.IGNORECASE):
|
2020-07-26 01:52:44 +00:00
|
|
|
continue
|
2020-08-20 07:47:16 +00:00
|
|
|
for filepath in os.listdir(pathdir):
|
|
|
|
if not filepath.endswith(gcc):
|
2020-07-26 01:52:44 +00:00
|
|
|
continue
|
2020-10-24 21:25:14 +00:00
|
|
|
# Use entire path to not rely on env PATH
|
|
|
|
filepath = os.path.sep.join([pathdir, filepath])
|
2020-07-26 01:52:44 +00:00
|
|
|
# Cache the g++ path to no search always
|
|
|
|
if os.path.exists(ENV_BUILD_PATH):
|
|
|
|
with open(GCC_PATH_CACHE, 'w+') as f:
|
2020-08-20 07:47:16 +00:00
|
|
|
f.write(filepath)
|
2020-07-26 01:52:44 +00:00
|
|
|
|
2020-08-20 07:47:16 +00:00
|
|
|
return filepath
|
2020-07-20 09:52:15 +00:00
|
|
|
|
2020-08-20 07:47:16 +00:00
|
|
|
filepath = env.get('CXX')
|
2021-07-22 00:01:23 +00:00
|
|
|
if filepath == 'CC':
|
|
|
|
filepath = gcc
|
2021-01-09 00:51:54 +00:00
|
|
|
blab("Couldn't find a compiler! Fallback to %s" % filepath)
|
2020-08-20 07:47:16 +00:00
|
|
|
return filepath
|
2020-07-20 09:52:15 +00:00
|
|
|
|
2020-07-26 01:52:44 +00:00
|
|
|
#
|
|
|
|
# Use the compiler to get a list of all enabled features
|
|
|
|
#
|
2020-07-20 02:42:30 +00:00
|
|
|
def load_marlin_features():
|
2020-08-06 13:14:00 +00:00
|
|
|
if 'MARLIN_FEATURES' in env:
|
2020-07-20 02:42:30 +00:00
|
|
|
return
|
|
|
|
|
2020-07-26 01:52:44 +00:00
|
|
|
# Process defines
|
2020-07-20 02:42:30 +00:00
|
|
|
build_flags = env.get('BUILD_FLAGS')
|
|
|
|
build_flags = env.ParseFlagsExtended(build_flags)
|
2020-07-20 09:52:15 +00:00
|
|
|
|
|
|
|
cxx = search_compiler()
|
2020-10-29 04:45:10 +00:00
|
|
|
cmd = ['"' + cxx + '"']
|
2020-07-20 09:52:15 +00:00
|
|
|
|
2020-07-26 01:52:44 +00:00
|
|
|
# Build flags from board.json
|
|
|
|
#if 'BOARD' in env:
|
|
|
|
# cmd += [env.BoardConfig().get("build.extra_flags")]
|
2020-07-20 02:42:30 +00:00
|
|
|
for s in build_flags['CPPDEFINES']:
|
|
|
|
if isinstance(s, tuple):
|
|
|
|
cmd += ['-D' + s[0] + '=' + str(s[1])]
|
|
|
|
else:
|
|
|
|
cmd += ['-D' + s]
|
2020-07-26 01:52:44 +00:00
|
|
|
|
2020-10-09 21:42:23 +00:00
|
|
|
cmd += ['-D__MARLIN_DEPS__ -w -dM -E -x c++ buildroot/share/PlatformIO/scripts/common-dependencies.h']
|
2020-07-20 02:42:30 +00:00
|
|
|
cmd = ' '.join(cmd)
|
2021-04-20 10:11:43 +00:00
|
|
|
blab(cmd, 4)
|
2020-07-20 02:42:30 +00:00
|
|
|
define_list = subprocess.check_output(cmd, shell=True).splitlines()
|
|
|
|
marlin_features = {}
|
|
|
|
for define in define_list:
|
|
|
|
feature = define[8:].strip().decode().split(' ')
|
|
|
|
feature, definition = feature[0], ' '.join(feature[1:])
|
|
|
|
marlin_features[feature] = definition
|
2020-08-06 13:14:00 +00:00
|
|
|
env['MARLIN_FEATURES'] = marlin_features
|
2020-07-20 02:42:30 +00:00
|
|
|
|
2020-07-26 01:52:44 +00:00
|
|
|
#
|
|
|
|
# Return True if a matching feature is enabled
|
|
|
|
#
|
2020-07-20 02:42:30 +00:00
|
|
|
def MarlinFeatureIsEnabled(env, feature):
|
|
|
|
load_marlin_features()
|
2020-08-06 13:14:00 +00:00
|
|
|
r = re.compile('^' + feature + '$')
|
|
|
|
found = list(filter(r.match, env['MARLIN_FEATURES']))
|
|
|
|
|
|
|
|
# Defines could still be 'false' or '0', so check
|
|
|
|
some_on = False
|
|
|
|
if len(found):
|
|
|
|
for f in found:
|
|
|
|
val = env['MARLIN_FEATURES'][f]
|
|
|
|
if val in [ '', '1', 'true' ]:
|
|
|
|
some_on = True
|
|
|
|
elif val in env['MARLIN_FEATURES']:
|
|
|
|
some_on = env.MarlinFeatureIsEnabled(val)
|
|
|
|
|
|
|
|
return some_on
|
2020-07-20 02:42:30 +00:00
|
|
|
|
2020-07-26 01:52:44 +00:00
|
|
|
#
|
|
|
|
# Add a method for other PIO scripts to query enabled features
|
|
|
|
#
|
2020-07-20 02:42:30 +00:00
|
|
|
env.AddMethod(MarlinFeatureIsEnabled)
|
|
|
|
|
2020-07-26 01:52:44 +00:00
|
|
|
#
|
|
|
|
# Add dependencies for enabled Marlin features
|
|
|
|
#
|
2020-08-06 13:14:00 +00:00
|
|
|
apply_features_config()
|
2020-07-21 07:33:58 +00:00
|
|
|
force_ignore_unused_libs()
|