From 83be2dd1235f310c2ea43458b83c2ac80117be95 Mon Sep 17 00:00:00 2001 From: Jose Martins Date: Fri, 29 Sep 2023 00:36:29 +0100 Subject: [PATCH] feat(util): add make functions for listing files Signed-off-by: Jose Martins --- util.mk | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/util.mk b/util.mk index 6ec5bfe..424826f 100644 --- a/util.mk +++ b/util.mk @@ -8,3 +8,44 @@ define check_variable_defined $(if $($(strip $1)),,$(error $(strip $1) not defined: $(strip $2))) endef + +# Check if a variable is a valid directory path. +# Returns nothing if false, some unknown value if true. +# @param directory path (can be absolute or relative) +# @example $(call is_dir, src/core) +define is_dir +$(strip $(wildcard $1/.)) +endef + +# Check if a variable is a valid file path (not a directory). +# Returns nothing if false, some unknown value if true. +# @param file path (absolute of relative) +# @example $(call is_dir, src/core/init.c) +define is_file +$(if $(strip $(wildcard $1/.)),,$1,) +endef + +# List files within a directory that match a wildcard pattern +# @param directory path (can be absolte of relative) +# @param a make wildcard pattern for the files' name (no relative paths) +# example $(call list_dir, src, *.c) +define list_dir_files +$(strip \ + $(foreach node, $(wildcard $(strip $1)/$(strip $2)), \ + $(if $(call is_file, $(node)),$(node))) \ +) +endef + +# List files within a directory and all of its subdirectories which names +# matches a wildcard pattern +# @param directory path (can be absolute of relative) +# @param a make wildcard pattern for files' name (no relative paths) +# example $(call list_dir_files_recursive, src, *.c) +define list_dir_files_recursive +$(strip \ + $(call list_dir_files, $1, $2) \ + $(foreach node, $(wildcard $(strip $1)/*), \ + $(if $(call is_dir, $(node)), + $(call list_dir_files_recursive, $(node), $(strip $2)))) \ +) +endef