2020-07-19 21:42:30 -05:00
|
|
|
#
|
2020-07-25 21:40:44 -05:00
|
|
|
# common-dependencies.py
|
2020-07-19 21:42:30 -05:00
|
|
|
# Convenience script to check dependencies and add libs and sources for Marlin Enabled Features
|
|
|
|
#
|
|
|
|
import subprocess
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
try:
|
2020-08-06 08:14:00 -05:00
|
|
|
import configparser
|
2020-07-19 21:42:30 -05:00
|
|
|
except ImportError:
|
2020-08-06 08:14:00 -05:00
|
|
|
import ConfigParser as configparser
|
2020-07-19 21:42:30 -05:00
|
|
|
from platformio.managers.package import PackageManager
|
|
|
|
|
|
|
|
Import("env")
|
|
|
|
|
2020-08-06 08:14:00 -05:00
|
|
|
FEATURE_CONFIG = {}
|
|
|
|
|
|
|
|
def add_to_feat_cnf(feature, flines):
|
|
|
|
feat = FEATURE_CONFIG[feature]
|
|
|
|
atoms = re.sub(',\\s*', '\n', flines).strip().split('\n')
|
|
|
|
for dep in atoms:
|
|
|
|
parts = dep.split('=')
|
|
|
|
name = parts.pop(0)
|
|
|
|
rest = '='.join(parts)
|
|
|
|
if name in ['extra_scripts', 'src_filter', 'lib_ignore']:
|
|
|
|
feat[name] = rest
|
|
|
|
else:
|
|
|
|
feat['lib_deps'] += [dep]
|
2020-07-19 21:42:30 -05:00
|
|
|
|
|
|
|
def load_config():
|
|
|
|
config = configparser.ConfigParser()
|
|
|
|
config.read("platformio.ini")
|
|
|
|
items = config.items('features')
|
|
|
|
for key in items:
|
2020-08-06 08:14:00 -05: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 17:33:42 -05:00
|
|
|
# Add options matching custom_marlin.MY_OPTION to the pile
|
2020-08-06 08:14:00 -05:00
|
|
|
all_opts = env.GetProjectOptions()
|
|
|
|
for n in all_opts:
|
2020-08-06 17:33:42 -05:00
|
|
|
mat = re.match(r'custom_marlin\.(.+)', n[0])
|
2020-08-06 08:14:00 -05:00
|
|
|
if mat:
|
|
|
|
try:
|
|
|
|
val = env.GetProjectOption(n[0])
|
|
|
|
except:
|
|
|
|
val = None
|
|
|
|
if val:
|
|
|
|
add_to_feat_cnf(mat.group(1).upper(), val)
|
2020-07-19 21:42:30 -05:00
|
|
|
|
2020-07-21 02:33:58 -05:00
|
|
|
def get_all_known_libs():
|
|
|
|
known_libs = []
|
2020-08-06 08:14:00 -05:00
|
|
|
for feature in FEATURE_CONFIG:
|
|
|
|
feat = FEATURE_CONFIG[feature]
|
|
|
|
if not 'lib_deps' in feat:
|
2020-07-21 02:33:58 -05:00
|
|
|
continue
|
2020-08-06 08:14:00 -05:00
|
|
|
for dep in feat['lib_deps']:
|
2020-07-21 02:33:58 -05:00
|
|
|
name, _, _ = PackageManager.parse_pkg_uri(dep)
|
|
|
|
known_libs.append(name)
|
|
|
|
return known_libs
|
|
|
|
|
|
|
|
def get_all_env_libs():
|
|
|
|
env_libs = []
|
2020-07-25 22:57:00 -05:00
|
|
|
lib_deps = env.GetProjectOption('lib_deps')
|
2020-07-21 02:33:58 -05:00
|
|
|
for dep in lib_deps:
|
|
|
|
name, _, _ = PackageManager.parse_pkg_uri(dep)
|
|
|
|
env_libs.append(name)
|
|
|
|
return env_libs
|
|
|
|
|
2020-07-25 22:57:00 -05:00
|
|
|
def set_env_field(field, value):
|
|
|
|
proj = env.GetProjectConfig()
|
|
|
|
proj.set("env:" + env['PIOENV'], field, value)
|
|
|
|
|
2020-07-25 20:52:44 -05: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 02:33:58 -05: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-25 22:57:00 -05:00
|
|
|
lib_ignore = env.GetProjectOption('lib_ignore') + diff
|
2020-08-06 08:14:00 -05:00
|
|
|
print("Ignore libraries:", lib_ignore)
|
2020-07-25 22:57:00 -05:00
|
|
|
set_env_field('lib_ignore', lib_ignore)
|
2020-07-21 02:33:58 -05:00
|
|
|
|
2020-08-06 08:14:00 -05:00
|
|
|
def apply_features_config():
|
2020-07-19 21:42:30 -05:00
|
|
|
load_config()
|
2020-08-06 08:14:00 -05:00
|
|
|
for feature in FEATURE_CONFIG:
|
2020-07-19 21:42:30 -05:00
|
|
|
if not env.MarlinFeatureIsEnabled(feature):
|
|
|
|
continue
|
|
|
|
|
2020-08-06 08:14:00 -05:00
|
|
|
feat = FEATURE_CONFIG[feature]
|
|
|
|
|
|
|
|
if 'lib_deps' in feat and len(feat['lib_deps']):
|
2020-07-19 21:42:30 -05:00
|
|
|
print("Adding lib_deps for %s... " % feature)
|
|
|
|
|
2020-08-06 08:14:00 -05:00
|
|
|
# feat to add
|
2020-07-19 21:42:30 -05:00
|
|
|
deps_to_add = {}
|
2020-08-06 08:14:00 -05:00
|
|
|
for dep in feat['lib_deps']:
|
2020-07-19 21:42:30 -05:00
|
|
|
name, _, _ = PackageManager.parse_pkg_uri(dep)
|
|
|
|
deps_to_add[name] = dep
|
|
|
|
|
2020-07-25 20:52:44 -05:00
|
|
|
# Does the env already have the dependency?
|
2020-07-25 22:57:00 -05:00
|
|
|
deps = env.GetProjectOption('lib_deps')
|
2020-07-19 21:42:30 -05:00
|
|
|
for dep in deps:
|
|
|
|
name, _, _ = PackageManager.parse_pkg_uri(dep)
|
|
|
|
if name in deps_to_add:
|
|
|
|
del deps_to_add[name]
|
|
|
|
|
2020-07-25 20:52:44 -05:00
|
|
|
# Are there any libraries that should be ignored?
|
2020-07-25 22:57:00 -05:00
|
|
|
lib_ignore = env.GetProjectOption('lib_ignore')
|
2020-07-19 21:42:30 -05:00
|
|
|
for dep in deps:
|
|
|
|
name, _, _ = PackageManager.parse_pkg_uri(dep)
|
|
|
|
if name in deps_to_add:
|
|
|
|
del deps_to_add[name]
|
|
|
|
|
2020-07-25 20:52:44 -05:00
|
|
|
# Is there anything left?
|
2020-07-21 03:15:20 -05:00
|
|
|
if len(deps_to_add) > 0:
|
2020-07-25 20:52:44 -05:00
|
|
|
# Only add the missing dependencies
|
2020-07-25 22:57:00 -05:00
|
|
|
set_env_field('lib_deps', deps + list(deps_to_add.values()))
|
2020-07-19 21:42:30 -05:00
|
|
|
|
2020-08-06 08:14:00 -05:00
|
|
|
if 'extra_scripts' in feat:
|
|
|
|
print("Running extra_scripts for %s... " % feature)
|
|
|
|
env.SConscript(feat['extra_scripts'], exports="env")
|
2020-07-19 21:42:30 -05:00
|
|
|
|
2020-08-06 08:14:00 -05:00
|
|
|
if 'src_filter' in feat:
|
2020-07-19 21:42:30 -05:00
|
|
|
print("Adding src_filter for %s... " % feature)
|
2020-07-25 22:57:00 -05:00
|
|
|
src_filter = ' '.join(env.GetProjectOption('src_filter'))
|
2020-07-19 21:42:30 -05:00
|
|
|
# first we need to remove the references to the same folder
|
2020-08-06 08:14:00 -05:00
|
|
|
my_srcs = re.findall( r'[+-](<.*?>)', feat['src_filter'])
|
2020-07-21 03:15:20 -05:00
|
|
|
cur_srcs = re.findall( r'[+-](<.*?>)', src_filter)
|
2020-07-19 21:42:30 -05:00
|
|
|
for d in my_srcs:
|
|
|
|
if d in cur_srcs:
|
2020-07-21 03:15:20 -05:00
|
|
|
src_filter = re.sub(r'[+-]' + d, '', src_filter)
|
2020-07-19 21:42:30 -05:00
|
|
|
|
2020-08-06 08:14:00 -05:00
|
|
|
src_filter = feat['src_filter'] + ' ' + src_filter
|
2020-07-25 22:57:00 -05:00
|
|
|
set_env_field('src_filter', [src_filter])
|
2020-07-19 21:42:30 -05:00
|
|
|
env.Replace(SRC_FILTER=src_filter)
|
|
|
|
|
2020-08-06 08:14:00 -05:00
|
|
|
if 'lib_ignore' in feat:
|
|
|
|
print("Adding lib_ignore for %s... " % feature)
|
|
|
|
lib_ignore = env.GetProjectOption('lib_ignore') + [feat['lib_ignore']]
|
2020-07-25 22:57:00 -05:00
|
|
|
set_env_field('lib_ignore', lib_ignore)
|
2020-07-23 23:47:01 -05:00
|
|
|
|
2020-07-25 20:52:44 -05:00
|
|
|
#
|
|
|
|
# Find a compiler, considering the OS
|
|
|
|
#
|
2020-07-25 22:57:00 -05:00
|
|
|
ENV_BUILD_PATH = os.path.join(env.Dictionary('PROJECT_BUILD_DIR'), env['PIOENV'])
|
2020-07-25 20:52:44 -05:00
|
|
|
GCC_PATH_CACHE = os.path.join(ENV_BUILD_PATH, ".gcc_path")
|
2020-07-20 04:52:15 -05:00
|
|
|
def search_compiler():
|
2020-07-25 20:52:44 -05:00
|
|
|
if os.path.exists(GCC_PATH_CACHE):
|
|
|
|
print('Getting g++ path from cache')
|
|
|
|
with open(GCC_PATH_CACHE, 'r') as f:
|
|
|
|
return f.read()
|
|
|
|
|
|
|
|
# PlatformIO inserts the toolchain bin folder on the front of the $PATH
|
|
|
|
# Find the current platform compiler by searching the $PATH
|
2020-07-20 04:52:15 -05:00
|
|
|
if env['PLATFORM'] == 'win32':
|
2020-07-25 20:52:44 -05:00
|
|
|
path_separator = ';'
|
|
|
|
path_regex = r'platformio\\packages.*\\bin'
|
|
|
|
gcc = "g++.exe"
|
2020-07-20 04:52:15 -05:00
|
|
|
else:
|
2020-07-25 20:52:44 -05:00
|
|
|
path_separator = ':'
|
|
|
|
path_regex = r'platformio/packages.*/bin'
|
|
|
|
gcc = "g++"
|
|
|
|
|
|
|
|
# Search for the compiler
|
|
|
|
for path in env['ENV']['PATH'].split(path_separator):
|
|
|
|
if not re.search(path_regex, path):
|
|
|
|
continue
|
|
|
|
for file in os.listdir(path):
|
|
|
|
if not file.endswith(gcc):
|
|
|
|
continue
|
|
|
|
|
|
|
|
# Cache the g++ path to no search always
|
|
|
|
if os.path.exists(ENV_BUILD_PATH):
|
|
|
|
print('Caching g++ for current env')
|
|
|
|
with open(GCC_PATH_CACHE, 'w+') as f:
|
|
|
|
f.write(file)
|
|
|
|
|
|
|
|
return file
|
2020-07-20 04:52:15 -05:00
|
|
|
|
2020-07-25 20:52:44 -05:00
|
|
|
file = env.get('CXX')
|
|
|
|
print("Couldn't find a compiler! Fallback to", file)
|
|
|
|
return file
|
2020-07-20 04:52:15 -05:00
|
|
|
|
2020-07-25 20:52:44 -05:00
|
|
|
#
|
|
|
|
# Use the compiler to get a list of all enabled features
|
|
|
|
#
|
2020-07-19 21:42:30 -05:00
|
|
|
def load_marlin_features():
|
2020-08-06 08:14:00 -05:00
|
|
|
if 'MARLIN_FEATURES' in env:
|
2020-07-19 21:42:30 -05:00
|
|
|
return
|
|
|
|
|
2020-07-25 20:52:44 -05:00
|
|
|
# Process defines
|
|
|
|
#print(env.Dump())
|
2020-07-19 21:42:30 -05:00
|
|
|
build_flags = env.get('BUILD_FLAGS')
|
|
|
|
build_flags = env.ParseFlagsExtended(build_flags)
|
2020-07-20 04:52:15 -05:00
|
|
|
|
|
|
|
cxx = search_compiler()
|
|
|
|
cmd = [cxx]
|
|
|
|
|
2020-07-25 20:52:44 -05:00
|
|
|
# Build flags from board.json
|
|
|
|
#if 'BOARD' in env:
|
|
|
|
# cmd += [env.BoardConfig().get("build.extra_flags")]
|
2020-07-19 21:42:30 -05:00
|
|
|
for s in build_flags['CPPDEFINES']:
|
|
|
|
if isinstance(s, tuple):
|
|
|
|
cmd += ['-D' + s[0] + '=' + str(s[1])]
|
|
|
|
else:
|
|
|
|
cmd += ['-D' + s]
|
2020-07-25 20:52:44 -05:00
|
|
|
|
2020-07-25 21:40:44 -05:00
|
|
|
cmd += ['-w -dM -E -x c++ buildroot/share/PlatformIO/scripts/common-dependencies.h']
|
2020-07-19 21:42:30 -05:00
|
|
|
cmd = ' '.join(cmd)
|
|
|
|
print(cmd)
|
|
|
|
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 08:14:00 -05:00
|
|
|
env['MARLIN_FEATURES'] = marlin_features
|
2020-07-19 21:42:30 -05:00
|
|
|
|
2020-07-25 20:52:44 -05:00
|
|
|
#
|
|
|
|
# Return True if a matching feature is enabled
|
|
|
|
#
|
2020-07-19 21:42:30 -05:00
|
|
|
def MarlinFeatureIsEnabled(env, feature):
|
|
|
|
load_marlin_features()
|
2020-08-06 08:14:00 -05: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-19 21:42:30 -05:00
|
|
|
|
2020-07-25 20:52:44 -05:00
|
|
|
#
|
|
|
|
# Add a method for other PIO scripts to query enabled features
|
|
|
|
#
|
2020-07-19 21:42:30 -05:00
|
|
|
env.AddMethod(MarlinFeatureIsEnabled)
|
|
|
|
|
2020-07-25 20:52:44 -05:00
|
|
|
#
|
|
|
|
# Add dependencies for enabled Marlin features
|
|
|
|
#
|
2020-08-06 08:14:00 -05:00
|
|
|
apply_features_config()
|
2020-07-21 02:33:58 -05:00
|
|
|
force_ignore_unused_libs()
|