Skip to content

Commit

Permalink
bug 756443: Add mkdir_stem() to complement mkdir_deps. Strip extraneo…
Browse files Browse the repository at this point in the history
…us slashes from given paths.
  • Loading branch information
Joey Armstrong committed Jun 1, 2012
1 parent 5915c6a commit 2686a51
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 17 deletions.
42 changes: 35 additions & 7 deletions config/makefiles/autotargets.mk
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,54 @@ ifndef INCLUDED_AUTOTARGETS_MK #{
MKDIR ?= mkdir -p
TOUCH ?= touch

# declare for local use, rules.mk may not have been loaded
space = $(NULL) $(NULL)

# Deps will be considered intermediate when used as a pre-requisite for
# wildcard targets. Inhibit their removal, mkdir -p is a standalone op.
.PRECIOUS: %/.mkdir.done

###########################################################################
# Threadsafe directory creation
# GENERATED_DIRS - Automated creation of these directories.
# Squeeze '//' from the path, easily created by $(dir $(path))
###########################################################################
mkdir_deps =$(subst //,/,$(foreach dir,$(getargv),$(dir)/.mkdir.done))
#########################
##---] FUNCTIONS [---##
#########################

# Squeeze can be overzealous, restore root for abspath
getPathPrefix =$(if $(filter /%,$(1)),/)

# Squeeze '//' from the path, easily created by string functions
_slashSqueeze =$(foreach val,$(getargv),$(call getPathPrefix,$(val))$(subst $(space),/,$(strip $(subst /,$(space),$(val)))))

# Squeeze extraneous directory slashes from the path
# o protect embedded spaces within the path
# o replace //+ sequences with /
slash_strip =\
$(strip \
$(subst <--[**]-->,$(space),\
$(call _slashSqueeze,\
$(subst $(space),<--[**]-->,$(1))\
)))

# Extract directory path from a dependency file.
mkdir_stem =$(foreach val,$(getargv),$(subst /.mkdir.done,$(NULL),$(val)))

## Generate timestamp file for threadsafe directory creation
mkdir_deps =$(foreach dir,$(getargv),$(call slash_strip,$(dir)/.mkdir.done))

#######################
##---] TARGETS [---##
#######################

%/.mkdir.done: # mkdir -p -p => mkdir -p
$(subst $(SPACE)-p,$(null),$(MKDIR)) -p $(dir $@)
$(subst $(space)-p,$(null),$(MKDIR)) -p $(dir $@)
@$(TOUCH) $@

# A handful of makefiles are attempting "mkdir dot". Likely not intended
# or stale logic so add a stub target to handle the request and warn for now.
.mkdir.done:
ifndef NOWARN_AUTOTARGETS # {
@echo "WARNING: $(MKDIR) -dot- requested by $(MAKE) -C $(CURDIR) $(MAKECMDGOALS)"
@$(TOUCH) $@
endif #}

INCLUDED_AUTOTARGETS_MK = 1
endif #}
Expand Down
1 change: 1 addition & 0 deletions config/makefiles/test/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ VPATH = @srcdir@

include $(DEPTH)/config/autoconf.mk

USE_AUTOTARGETS_MK = 1
MAKEUTILS_UNIT_TEST = 1
include $(topsrcdir)/config/makefiles/makeutils.mk

Expand Down
54 changes: 51 additions & 3 deletions config/makefiles/test/check-autotargets.mk
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ ifdef VERBOSE
$(warning loading test)
endif

space=$(null) $(null)
GENERATED_DIRS = bogus # test data

NOWARN_AUTOTARGETS = 1 # Unit test includes makefile twice.

undefine USE_AUTOTARGETS_MK
undefine INCLUDED_AUTOTARGETS_MK
include $(topsrcdir)/config/makefiles/autotargets.mk
Expand All @@ -30,9 +33,54 @@ ifneq (bogus,$(findstring bogus,$(AUTO_DEPS)))
$(error AUTO_DEPS=[$(AUTO_DEPS)] is not set correctly)
endif

path = foo/bar.c
exp = foo/.mkdir.done
found = $(call mkdir_deps,$(dir $(path)))

# relpath
path := foo/bar.c
exp := foo/.mkdir.done
found := $(call mkdir_deps,$(dir $(path)))
ifneq ($(exp),$(found))
$(error mkdir_deps($(path))=$(exp) not set correctly [$(found)])
endif

# abspath
path := /foo//bar/
exp := /foo/bar/.mkdir.done
found := $(call mkdir_deps,$(path))
ifneq ($(exp),$(found))
$(error mkdir_deps($(path))=$(exp) not set correctly [$(found)])
endif


## verify strip_slash
#####################

path := a/b//c///d////e/////
exp := a/b/c/d/e/.mkdir.done
found := $(call mkdir_deps,$(path))
ifneq ($(exp),$(found))
$(error mkdir_deps($(path))=$(exp) not set correctly [$(found)])
endif


## verify mkdir_stem()
######################
path := verify/mkdir_stem
pathD = $(call mkdir_deps,$(path))
pathS = $(call mkdir_stem,$(pathD))
exp := $(path)

ifeq ($(pathD),$(pathS))
$(error mkdir_deps and mkdir_stem should not match [$(pathD)])
endif
ifneq ($(pathS),$(exp))
$(error mkdir_stem=[$(pathS)] != exp=[$(exp)])
endif


## Verify embedded whitespace has been protected
path := a/b$(space)c//d
exp := a/b$(space)c/d
found := $(call slash_strip,$(path))
ifneq ($(exp),$(found))
$(error slash_strip($(path))=$(exp) not set correctly [$(found)])
endif
42 changes: 35 additions & 7 deletions js/src/config/makefiles/autotargets.mk
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,54 @@ ifndef INCLUDED_AUTOTARGETS_MK #{
MKDIR ?= mkdir -p
TOUCH ?= touch

# declare for local use, rules.mk may not have been loaded
space = $(NULL) $(NULL)

# Deps will be considered intermediate when used as a pre-requisite for
# wildcard targets. Inhibit their removal, mkdir -p is a standalone op.
.PRECIOUS: %/.mkdir.done

###########################################################################
# Threadsafe directory creation
# GENERATED_DIRS - Automated creation of these directories.
# Squeeze '//' from the path, easily created by $(dir $(path))
###########################################################################
mkdir_deps =$(subst //,/,$(foreach dir,$(getargv),$(dir)/.mkdir.done))
#########################
##---] FUNCTIONS [---##
#########################

# Squeeze can be overzealous, restore root for abspath
getPathPrefix =$(if $(filter /%,$(1)),/)

# Squeeze '//' from the path, easily created by string functions
_slashSqueeze =$(foreach val,$(getargv),$(call getPathPrefix,$(val))$(subst $(space),/,$(strip $(subst /,$(space),$(val)))))

# Squeeze extraneous directory slashes from the path
# o protect embedded spaces within the path
# o replace //+ sequences with /
slash_strip =\
$(strip \
$(subst <--[**]-->,$(space),\
$(call _slashSqueeze,\
$(subst $(space),<--[**]-->,$(1))\
)))

# Extract directory path from a dependency file.
mkdir_stem =$(foreach val,$(getargv),$(subst /.mkdir.done,$(NULL),$(val)))

## Generate timestamp file for threadsafe directory creation
mkdir_deps =$(foreach dir,$(getargv),$(call slash_strip,$(dir)/.mkdir.done))

#######################
##---] TARGETS [---##
#######################

%/.mkdir.done: # mkdir -p -p => mkdir -p
$(subst $(SPACE)-p,$(null),$(MKDIR)) -p $(dir $@)
$(subst $(space)-p,$(null),$(MKDIR)) -p $(dir $@)
@$(TOUCH) $@

# A handful of makefiles are attempting "mkdir dot". Likely not intended
# or stale logic so add a stub target to handle the request and warn for now.
.mkdir.done:
ifndef NOWARN_AUTOTARGETS # {
@echo "WARNING: $(MKDIR) -dot- requested by $(MAKE) -C $(CURDIR) $(MAKECMDGOALS)"
@$(TOUCH) $@
endif #}

INCLUDED_AUTOTARGETS_MK = 1
endif #}
Expand Down

0 comments on commit 2686a51

Please sign in to comment.