Skip to content

Commit

Permalink
feat(util): add make functions for listing files
Browse files Browse the repository at this point in the history
Signed-off-by: Jose Martins <[email protected]>
  • Loading branch information
josecm committed Sep 28, 2023
1 parent 40238c5 commit 2ed2a7d
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions util.mk
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,43 @@
define check_variable_defined
$(if $($(strip $1)),,$(error $(strip $1) not defined: $(strip $2)))
endef

# Check if a variable is a valid directory path (can be absolute or relative).
# Returns nothing if false, some unknown value if true.
# @param directory name
# @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
# @param directory path (can be absolte of relative)
# @param a make pattern for the local file 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
# @param directory path (can be absolte of relative)
# @param a make pattern for the local file 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

0 comments on commit 2ed2a7d

Please sign in to comment.