Skip to content

Commit

Permalink
#96: Simplify traverse
Browse files Browse the repository at this point in the history
  • Loading branch information
abusalimov committed Sep 26, 2010
1 parent e6c47e8 commit 59fc084
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 30 deletions.
41 changes: 11 additions & 30 deletions mk/traverse.mk
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@
# SUCH DAMAGE.
#

ifndef _traverse_mk_
_traverse_mk_ := 1
ifndef $(already_included)

include $(MK_DIR)/util.mk
include util/file.mk

#
# Usage:
Expand Down Expand Up @@ -98,7 +97,7 @@ __traverse_invoke = $1 \
$(foreach subdir,$(__traverse_process),$(call $0,$1/$(subdir),$2))

__traverse_process = \
$(eval $(value __traverse_process_mk))$(__traverse_process_result)
${eval $(value __traverse_process_mk)}$(__traverse_process_result)

#
# The main routine used for recursive processing of tree nodes.
Expand All @@ -107,12 +106,14 @@ define __traverse_process_mk
# Provide the node location.
SELFDIR := $1
# Sometimes it is useful to define variables as recursively expanded.
$_SELFDIR := $(SELFDIR)
$_SELFDIR := $(SELFDIR)# TODO try to avoid it

# Default SUBDIRS to expansion of *.
SUBDIRS := $(call __traverse_subdirs_wildcard,$1,*)
__traverse_subdirs := $(call d-wildcard_relative,$1,*)
SUBDIRS := $(__traverse_subdirs)

__traverse_include := $(__traverse_node_file_wildcard)
__traverse_include := \
$(call f-wildcard_first,$(addprefix $1/,$2 Makefile makefile))
ifneq ($(__traverse_include),)
# Go!
include $(__traverse_include)
Expand All @@ -129,35 +130,15 @@ define __traverse_process_mk
endif

# Prepare return value.
__traverse_process_result := $(call __traverse_subdirs_wildcard,$1,$(SUBDIRS))
__traverse_process_result := \
$(filter $(__traverse_subdirs),$(call d-wildcard_relative,$1,$(SUBDIRS)))

endef

#
# Params:
# 1. Base directory
# 2. Subdirectories list relative to the base dir possibly containing wildcard
# expressions.
#
# In a nutshell:
# Expand d-wildcards for sub-dirs within the base dir.
# Get back to sub-dirs relative names and remove duplicates.
#
__traverse_subdirs_wildcard = \
$(sort $(patsubst $1/%,%,$(call d-wildcard,$(2:%=$1/%))))

__traverse_node_file_wildcard = $(strip \
$(or $ \
$(wildcard $(2:%=$1/%)),$ \
$(wildcard $1/Makefile),$ \
$(wildcard $1/makefile),$ \
) \
)

__traverse_parent_node_file = $(strip \
$(patsubst $(abspath $(__traverse_root))/%,$(__traverse_root)/%, \
$(abspath $(call __traverse_node_file_wildcard,$1/..,$2)) \
) \
)

endif # _traverse_mk_
endif # $(already_included)
12 changes: 12 additions & 0 deletions mk/util/file.mk
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ d-wildcard = $(patsubst %/,%,$(filter %/,$(wildcard $(1:%=%/))))
#
f-wildcard = $(patsubst %/,%,$(filter-out %/,$(wildcard $(1:%=%/))))

#
# XXX docs were copy-pasted from traverse.mk -- Eldar
#
# Params:
# 1. Base directory
# 2. Subdirectories list relative to the base dir possibly containing wildcard
# expressions.
#
# In a nutshell:
# Expand d-wildcards for sub-dirs within the base dir.
# Get back to sub-dirs relative names and remove duplicates.
#
d-wildcard_relative = $(call __wildcard_relative,d-wildcard,$1,$2)
f-wildcard_relative = $(call __wildcard_relative,f-wildcard,$1,$2)
wildcard_relative = $(call __wildcard_relative, wildcard,$1,$2)
Expand Down

0 comments on commit 59fc084

Please sign in to comment.