2023-11-01 04:21:31 +00:00
|
|
|
import itertools
|
|
|
|
|
2022-06-26 12:00:03 +00:00
|
|
|
import SCons
|
2023-02-08 09:35:38 +00:00
|
|
|
from fbt.util import GLOB_FILE_EXCLUSION
|
2023-10-06 07:11:02 +00:00
|
|
|
from SCons.Node.FS import has_glob_magic
|
2023-11-01 04:21:31 +00:00
|
|
|
from SCons.Script import Flatten
|
2022-06-26 12:00:03 +00:00
|
|
|
|
|
|
|
|
2023-02-08 09:35:38 +00:00
|
|
|
def GlobRecursive(env, pattern, node=".", exclude=[]):
|
|
|
|
exclude = list(set(Flatten(exclude) + GLOB_FILE_EXCLUSION))
|
|
|
|
# print(f"Starting glob for {pattern} from {node} (exclude: {exclude})")
|
2022-06-26 12:00:03 +00:00
|
|
|
results = []
|
|
|
|
if isinstance(node, str):
|
|
|
|
node = env.Dir(node)
|
2023-10-06 07:11:02 +00:00
|
|
|
# Only initiate actual recursion if special symbols can be found in 'pattern'
|
|
|
|
if has_glob_magic(pattern):
|
|
|
|
for f in node.glob("*", source=True, exclude=exclude):
|
|
|
|
if isinstance(f, SCons.Node.FS.Dir):
|
|
|
|
results += env.GlobRecursive(pattern, f, exclude)
|
|
|
|
results += node.glob(
|
|
|
|
pattern,
|
|
|
|
source=True,
|
|
|
|
exclude=exclude,
|
|
|
|
)
|
2023-10-23 09:55:36 +00:00
|
|
|
# Otherwise, just assume that file at path exists
|
|
|
|
else:
|
|
|
|
results.append(node.File(pattern))
|
2023-11-01 04:21:31 +00:00
|
|
|
## Debug
|
2023-02-08 09:35:38 +00:00
|
|
|
# print(f"Glob result for {pattern} from {node}: {results}")
|
2022-06-26 12:00:03 +00:00
|
|
|
return results
|
|
|
|
|
|
|
|
|
2023-11-01 04:21:31 +00:00
|
|
|
def GatherSources(env, sources_list, node="."):
|
|
|
|
sources_list = list(set(Flatten(sources_list)))
|
|
|
|
include_sources = list(filter(lambda x: not x.startswith("!"), sources_list))
|
|
|
|
exclude_sources = list(x[1:] for x in sources_list if x.startswith("!"))
|
|
|
|
gathered_sources = list(
|
|
|
|
itertools.chain.from_iterable(
|
|
|
|
env.GlobRecursive(
|
|
|
|
source_type,
|
|
|
|
node,
|
|
|
|
exclude=exclude_sources,
|
|
|
|
)
|
|
|
|
for source_type in include_sources
|
|
|
|
)
|
|
|
|
)
|
|
|
|
## Debug
|
|
|
|
# print(
|
|
|
|
# f"Gathered sources for {sources_list} from {node}: {list(f.path for f in gathered_sources)}"
|
|
|
|
# )
|
|
|
|
return gathered_sources
|
|
|
|
|
|
|
|
|
2022-06-26 12:00:03 +00:00
|
|
|
def generate(env):
|
|
|
|
env.AddMethod(GlobRecursive)
|
2023-11-01 04:21:31 +00:00
|
|
|
env.AddMethod(GatherSources)
|
2022-06-26 12:00:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
def exists(env):
|
|
|
|
return True
|