🧑‍💻 Optimize PlatformIO source filtering (#25332)

Co-authored-by: Scott Lahteine <thinkyhead@users.noreply.github.com>
This commit is contained in:
Martin Turski 2023-04-27 14:05:24 +02:00 committed by GitHub
parent 774861b15d
commit 9a7d9e6995
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
45 changed files with 650 additions and 297 deletions

View file

@ -5,7 +5,9 @@
import pioutil
if pioutil.is_pio_build():
import subprocess,os,re
import subprocess,os,re,fnmatch,glob
srcfilepattern = re.compile(r".*[.](cpp|c)$")
marlinbasedir = os.path.join(os.getcwd(), "Marlin/")
Import("env")
from platformio.package.meta import PackageSpec
@ -128,6 +130,7 @@ if pioutil.is_pio_build():
def apply_features_config():
load_features()
blab("========== Apply enabled features...")
build_filters = ' '.join(env.GetProjectOption('src_filter'))
for feature in FEATURE_CONFIG:
if not env.MarlinHas(feature):
continue
@ -174,23 +177,87 @@ if pioutil.is_pio_build():
if 'src_filter' in feat:
blab("========== Adding build_src_filter for %s... " % feature, 2)
src_filter = ' '.join(env.GetProjectOption('src_filter'))
# first we need to remove the references to the same folder
my_srcs = re.findall(r'[+-](<.*?>)', feat['src_filter'])
cur_srcs = re.findall(r'[+-](<.*?>)', src_filter)
for d in my_srcs:
if d in cur_srcs:
src_filter = re.sub(r'[+-]' + d, '', src_filter)
src_filter = feat['src_filter'] + ' ' + src_filter
set_env_field('build_src_filter', [src_filter])
env.Replace(SRC_FILTER=src_filter)
build_filters = build_filters + ' ' + feat['src_filter']
# Just append the filter in the order that the build environment specifies.
# Important here is the order of entries in the "features.ini" file.
if 'lib_ignore' in feat:
blab("========== Adding lib_ignore for %s... " % feature, 2)
lib_ignore = env.GetProjectOption('lib_ignore') + [feat['lib_ignore']]
set_env_field('lib_ignore', lib_ignore)
src_filter = ""
if True:
# Build the actual equivalent build_src_filter list based on the inclusions by the features.
# PlatformIO doesn't do it this way, but maybe in the future....
cur_srcs = set()
# Remove the references to the same folder
my_srcs = re.findall(r'([+-]<.*?>)', build_filters)
for d in my_srcs:
# Assume normalized relative paths
plain = d[2:-1]
if d[0] == '+':
def addentry(fullpath, info=None):
relp = os.path.relpath(fullpath, marlinbasedir)
if srcfilepattern.match(relp):
if info:
blab("Added src file %s (%s)" % (relp, str(info)))
else:
blab("Added src file %s " % relp)
cur_srcs.add(relp)
# Special rule: If a direct folder is specified add all files within.
fullplain = os.path.join(marlinbasedir, plain)
if os.path.isdir(fullplain):
blab("Directory content addition for %s " % plain)
gpattern = os.path.join(fullplain, "**")
for fname in glob.glob(gpattern, recursive=True):
addentry(fname, "dca")
else:
# Add all the things from the pattern by GLOB.
def srepl(matchi):
g0 = matchi.group(0)
return r"**" + g0[1:]
gpattern = re.sub(r'[*]($|[^*])', srepl, plain)
gpattern = os.path.join(marlinbasedir, gpattern)
for fname in glob.glob(gpattern, recursive=True):
addentry(fname)
else:
# Special rule: If a direct folder is specified then remove all files within.
def onremove(relp, info=None):
if info:
blab("Removed src file %s (%s)" % (relp, str(info)))
else:
blab("Removed src file %s " % relp)
fullplain = os.path.join(marlinbasedir, plain)
if os.path.isdir(fullplain):
blab("Directory content removal for %s " % plain)
def filt(x):
common = os.path.commonpath([plain, x])
if not common == os.path.normpath(plain): return True
onremove(x, "dcr")
return False
cur_srcs = set(filter(filt, cur_srcs))
else:
# Remove matching source entries.
def filt(x):
if not fnmatch.fnmatch(x, plain): return True
onremove(x)
return False
cur_srcs = set(filter(filt, cur_srcs))
# Transform the resulting set into a string.
for x in cur_srcs:
if len(src_filter) > 0: src_filter += ' '
src_filter += "+<" + x + ">"
#blab("Final src_filter: " + src_filter)
else:
src_filter = build_filters
# Update in PlatformIO
set_env_field('build_src_filter', [src_filter])
env.Replace(SRC_FILTER=src_filter)
#
# Use the compiler to get a list of all enabled features
#
@ -226,6 +293,8 @@ if pioutil.is_pio_build():
elif val in env['MARLIN_FEATURES']:
some_on = env.MarlinHas(val)
#blab("%s is %s" % (feature, str(some_on)))
return some_on
validate_pio()