From 766ec17a90f67e8cda78394e58a7fffb00f5a4b7 Mon Sep 17 00:00:00 2001
From: Bruno Haible <bruno@clisp.org>
Date: Wed, 15 Dec 2021 19:18:20 +0100
Subject: [PATCH] Fix creation of build directories with
 non-recursive-gnulib-prefix-hack.

This is necessary for executing "make" in a VPATH build of a package
that uses prefix-gnulib-mk and a module such as alloca-opt.

* modules/alloca-opt (Makefile.am): Before generating the .h-t file,
ensure that the directory in which we want to create it exists.
(configure.ac): Invoke AC_PROG_MKDIR_P.
* modules/argz: Likewise.
* modules/arpa_inet: Likewise.
* modules/assert-h: Likewise.
* modules/byteswap: Likewise.
* modules/configmake: Likewise.
* modules/ctype: Likewise.
* modules/dirent: Likewise.
* modules/dynarray: Likewise.
* modules/errno: Likewise.
* modules/execinfo: Likewise.
* modules/fcntl-h: Likewise.
* modules/float: Likewise.
* modules/fnmatch-h: Likewise.
* modules/getopt-posix: Likewise.
* modules/glob-h: Likewise.
* modules/iconv-h: Likewise.
* modules/ieee754-h: Likewise.
* modules/inttypes-incomplete: Likewise.
* modules/langinfo: Likewise.
* modules/libgmp: Likewise.
* modules/libtextstyle-optional: Likewise.
* modules/limits-h: Likewise.
* modules/locale: Likewise.
* modules/malloc-h: Likewise.
* modules/math: Likewise.
* modules/monetary: Likewise.
* modules/net_if: Likewise.
* modules/netdb: Likewise.
* modules/netinet_in: Likewise.
* modules/openmp-init: Likewise.
* modules/poll-h: Likewise.
* modules/posix-shell: Likewise.
* modules/pthread-h: Likewise.
* modules/pty: Likewise.
* modules/sched: Likewise.
* modules/scratch_buffer: Likewise.
* modules/search: Likewise.
* modules/selinux-h: Likewise.
* modules/signal-h: Likewise.
* modules/sigsegv: Likewise.
* modules/snippet/link-warning: Likewise.
* modules/spawn: Likewise.
* modules/stdalign: Likewise.
* modules/stdarg: Likewise.
* modules/stdbool: Likewise.
* modules/stddef: Likewise.
* modules/stdint: Likewise.
* modules/stdio: Likewise.
* modules/stdlib: Likewise.
* modules/stdnoreturn: Likewise.
* modules/string: Likewise.
* modules/strings: Likewise.
* modules/sys_file: Likewise.
* modules/sys_ioctl: Likewise.
* modules/sys_random: Likewise.
* modules/sys_resource: Likewise.
* modules/sys_select: Likewise.
* modules/sys_socket: Likewise.
* modules/sys_stat: Likewise.
* modules/sys_time: Likewise.
* modules/sys_times: Likewise.
* modules/sys_types: Likewise.
* modules/sys_uio: Likewise.
* modules/sys_utsname: Likewise.
* modules/sys_wait: Likewise.
* modules/sysexits: Likewise.
* modules/termios: Likewise.
* modules/threads-h: Likewise.
* modules/time: Likewise.
* modules/uchar: Likewise.
* modules/unicase/base: Likewise.
* modules/unicase/special-casing: Likewise.
* modules/uniconv/base: Likewise.
* modules/unictype/base: Likewise.
* modules/unigbrk/base: Likewise.
* modules/unilbrk/base: Likewise.
* modules/uniname/base: Likewise.
* modules/uninorm/base: Likewise.
* modules/unistd: Likewise.
* modules/unistdio/base: Likewise.
* modules/unistr/base: Likewise.
* modules/unitypes: Likewise.
* modules/uniwbrk/base: Likewise.
* modules/uniwidth/base: Likewise.
* modules/utime-h: Likewise.
* modules/wchar: Likewise.
* modules/wctype-h: Likewise.
* gnulib-tool (func_emit_lib_Makefile_am): When producing output for
GNU make, eliminate %reldir% tokens.
---
 ChangeLog                      | 98 ++++++++++++++++++++++++++++++++++
 gnulib-tool                    | 24 +++++++--
 modules/alloca-opt             |  2 +
 modules/argz                   |  2 +
 modules/arpa_inet              |  2 +-
 modules/assert-h               |  2 +
 modules/byteswap               |  2 +
 modules/configmake             |  2 +
 modules/ctype                  |  2 +
 modules/dirent                 |  2 +
 modules/dynarray               |  4 +-
 modules/errno                  |  2 +
 modules/execinfo               |  2 +
 modules/fcntl-h                |  2 +
 modules/float                  |  2 +
 modules/fnmatch-h              |  2 +
 modules/getopt-posix           |  2 +
 modules/glob-h                 |  2 +
 modules/iconv-h                |  2 +
 modules/ieee754-h              |  2 +
 modules/inttypes-incomplete    |  2 +
 modules/langinfo               |  2 +
 modules/libgmp                 |  7 ++-
 modules/libtextstyle-optional  |  2 +
 modules/limits-h               |  2 +
 modules/locale                 |  2 +
 modules/malloc-h               |  2 +
 modules/math                   |  2 +
 modules/monetary               |  2 +
 modules/net_if                 |  2 +-
 modules/netdb                  |  2 +
 modules/netinet_in             |  2 +-
 modules/openmp-init            |  2 +
 modules/poll-h                 |  2 +
 modules/posix-shell            |  4 +-
 modules/pthread-h              |  2 +
 modules/pty                    |  2 +
 modules/sched                  |  2 +
 modules/scratch_buffer         |  2 +-
 modules/search                 |  2 +
 modules/selinux-h              |  7 +--
 modules/signal-h               |  2 +
 modules/sigsegv                |  2 +
 modules/snippet/link-warning   |  2 +
 modules/spawn                  |  2 +
 modules/stdalign               |  2 +
 modules/stdarg                 |  2 +
 modules/stdbool                |  2 +
 modules/stddef                 |  2 +
 modules/stdint                 |  2 +
 modules/stdio                  |  2 +
 modules/stdlib                 |  2 +
 modules/stdnoreturn            |  2 +
 modules/string                 |  2 +
 modules/strings                |  2 +
 modules/sys_file               |  2 +-
 modules/sys_ioctl              |  2 +-
 modules/sys_random             |  2 +-
 modules/sys_resource           |  2 +-
 modules/sys_select             |  2 +-
 modules/sys_socket             |  2 +-
 modules/sys_stat               |  2 +-
 modules/sys_time               |  2 +-
 modules/sys_times              |  2 +-
 modules/sys_types              |  2 +-
 modules/sys_uio                |  2 +-
 modules/sys_utsname            |  2 +-
 modules/sys_wait               |  2 +-
 modules/sysexits               |  2 +
 modules/termios                |  2 +
 modules/threads-h              |  2 +
 modules/time                   |  2 +
 modules/uchar                  |  2 +
 modules/unicase/base           |  2 +
 modules/unicase/special-casing |  3 +-
 modules/uniconv/base           |  2 +
 modules/unictype/base          |  2 +
 modules/unigbrk/base           |  2 +
 modules/unilbrk/base           |  2 +
 modules/uniname/base           |  2 +
 modules/uninorm/base           |  2 +
 modules/unistd                 |  2 +
 modules/unistdio/base          |  2 +
 modules/unistr/base            |  2 +
 modules/unitypes               |  2 +
 modules/uniwbrk/base           |  2 +
 modules/uniwidth/base          |  2 +
 modules/utime-h                |  2 +
 modules/wchar                  |  2 +
 modules/wctype-h               |  2 +
 90 files changed, 282 insertions(+), 31 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index cbb66c2bbe..29296575b1 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,101 @@
+2021-12-15  Bruno Haible  <bruno@clisp.org>
+
+	Fix creation of build directories with non-recursive-gnulib-prefix-hack.
+	This is necessary for executing "make" in a VPATH build of a package
+	that uses prefix-gnulib-mk and a module such as alloca-opt.
+	* modules/alloca-opt (Makefile.am): Before generating the .h-t file,
+	ensure that the directory in which we want to create it exists.
+	(configure.ac): Invoke AC_PROG_MKDIR_P.
+	* modules/argz: Likewise.
+	* modules/arpa_inet: Likewise.
+	* modules/assert-h: Likewise.
+	* modules/byteswap: Likewise.
+	* modules/configmake: Likewise.
+	* modules/ctype: Likewise.
+	* modules/dirent: Likewise.
+	* modules/dynarray: Likewise.
+	* modules/errno: Likewise.
+	* modules/execinfo: Likewise.
+	* modules/fcntl-h: Likewise.
+	* modules/float: Likewise.
+	* modules/fnmatch-h: Likewise.
+	* modules/getopt-posix: Likewise.
+	* modules/glob-h: Likewise.
+	* modules/iconv-h: Likewise.
+	* modules/ieee754-h: Likewise.
+	* modules/inttypes-incomplete: Likewise.
+	* modules/langinfo: Likewise.
+	* modules/libgmp: Likewise.
+	* modules/libtextstyle-optional: Likewise.
+	* modules/limits-h: Likewise.
+	* modules/locale: Likewise.
+	* modules/malloc-h: Likewise.
+	* modules/math: Likewise.
+	* modules/monetary: Likewise.
+	* modules/net_if: Likewise.
+	* modules/netdb: Likewise.
+	* modules/netinet_in: Likewise.
+	* modules/openmp-init: Likewise.
+	* modules/poll-h: Likewise.
+	* modules/posix-shell: Likewise.
+	* modules/pthread-h: Likewise.
+	* modules/pty: Likewise.
+	* modules/sched: Likewise.
+	* modules/scratch_buffer: Likewise.
+	* modules/search: Likewise.
+	* modules/selinux-h: Likewise.
+	* modules/signal-h: Likewise.
+	* modules/sigsegv: Likewise.
+	* modules/snippet/link-warning: Likewise.
+	* modules/spawn: Likewise.
+	* modules/stdalign: Likewise.
+	* modules/stdarg: Likewise.
+	* modules/stdbool: Likewise.
+	* modules/stddef: Likewise.
+	* modules/stdint: Likewise.
+	* modules/stdio: Likewise.
+	* modules/stdlib: Likewise.
+	* modules/stdnoreturn: Likewise.
+	* modules/string: Likewise.
+	* modules/strings: Likewise.
+	* modules/sys_file: Likewise.
+	* modules/sys_ioctl: Likewise.
+	* modules/sys_random: Likewise.
+	* modules/sys_resource: Likewise.
+	* modules/sys_select: Likewise.
+	* modules/sys_socket: Likewise.
+	* modules/sys_stat: Likewise.
+	* modules/sys_time: Likewise.
+	* modules/sys_times: Likewise.
+	* modules/sys_types: Likewise.
+	* modules/sys_uio: Likewise.
+	* modules/sys_utsname: Likewise.
+	* modules/sys_wait: Likewise.
+	* modules/sysexits: Likewise.
+	* modules/termios: Likewise.
+	* modules/threads-h: Likewise.
+	* modules/time: Likewise.
+	* modules/uchar: Likewise.
+	* modules/unicase/base: Likewise.
+	* modules/unicase/special-casing: Likewise.
+	* modules/uniconv/base: Likewise.
+	* modules/unictype/base: Likewise.
+	* modules/unigbrk/base: Likewise.
+	* modules/unilbrk/base: Likewise.
+	* modules/uniname/base: Likewise.
+	* modules/uninorm/base: Likewise.
+	* modules/unistd: Likewise.
+	* modules/unistdio/base: Likewise.
+	* modules/unistr/base: Likewise.
+	* modules/unitypes: Likewise.
+	* modules/uniwbrk/base: Likewise.
+	* modules/uniwidth/base: Likewise.
+	* modules/utime-h: Likewise.
+	* modules/wchar: Likewise.
+	* modules/wctype-h: Likewise.
+	* gnulib-tool (func_emit_lib_Makefile_am): When producing output for
+	GNU make, eliminate %reldir% tokens.
+
 2021-12-15  Bruno Haible  <bruno@clisp.org>
 
 	non-recursive-gnulib-prefix-hack: Remove obsolete code.
diff --git a/gnulib-tool b/gnulib-tool
index 58e1859c54..25df157dca 100755
--- a/gnulib-tool
+++ b/gnulib-tool
@@ -3737,9 +3737,9 @@ func_emit_lib_Makefile_am ()
           echo "## begin gnulib module $module"
           if $gnu_make; then
             echo "ifeq (,\$(OMIT_GNULIB_MODULE_$module))"
-            convert_to_gnu_make='s/^if \(.*\)/ifneq (,$(\1))/'
-          else
-            convert_to_gnu_make=
+            convert_to_gnu_make_1='s/^if \(.*\)/ifneq (,$(\1))/'
+            convert_to_gnu_make_2='s|%reldir%/||g'
+            convert_to_gnu_make_3='s|%reldir%|.|g'
           fi
           echo
           if test "$cond_dependencies" = true; then
@@ -3752,13 +3752,27 @@ func_emit_lib_Makefile_am ()
               fi
             fi
           fi
-          sed "$convert_to_gnu_make" "$tmp"/amsnippet1
+          if $gnu_make; then
+            sed -e "$convert_to_gnu_make_1" \
+                -e "$convert_to_gnu_make_2" \
+                -e "$convert_to_gnu_make_3" \
+                "$tmp"/amsnippet1
+          else
+            cat "$tmp"/amsnippet1
+          fi
           if test "$cond_dependencies" = true; then
             if func_cond_module_p "$module"; then
               echo "endif"
             fi
           fi
-          sed "$convert_to_gnu_make" "$tmp"/amsnippet2
+          if $gnu_make; then
+            sed -e "$convert_to_gnu_make_1" \
+                -e "$convert_to_gnu_make_2" \
+                -e "$convert_to_gnu_make_3" \
+                "$tmp"/amsnippet2
+          else
+            cat "$tmp"/amsnippet2
+          fi
           if $gnu_make; then
             echo "endif"
           fi
diff --git a/modules/alloca-opt b/modules/alloca-opt
index 53bb28dba8..ec3250dc4e 100644
--- a/modules/alloca-opt
+++ b/modules/alloca-opt
@@ -11,6 +11,7 @@ Depends-on:
 
 configure.ac:
 gl_FUNC_ALLOCA
+AC_PROG_MKDIR_P
 
 Makefile.am:
 BUILT_SOURCES += $(ALLOCA_H)
@@ -20,6 +21,7 @@ BUILT_SOURCES += $(ALLOCA_H)
 if GL_GENERATE_ALLOCA_H
 alloca.h: alloca.in.h $(top_builddir)/config.status
 	$(AM_V_GEN)rm -f $@-t $@ && \
+	$(MKDIR_P) '%reldir%' && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
 	  sed -e 's|@''HAVE_ALLOCA_H''@|$(HAVE_ALLOCA_H)|g' < $(srcdir)/alloca.in.h; \
 	} > $@-t && \
diff --git a/modules/argz b/modules/argz
index a8eb55e944..a20a214d48 100644
--- a/modules/argz
+++ b/modules/argz
@@ -16,6 +16,7 @@ strstr          [test -n "$ARGZ_H"]
 
 configure.ac:
 gl_FUNC_ARGZ
+AC_PROG_MKDIR_P
 if test -n "$ARGZ_H"; then
   AC_LIBOBJ([argz])
 fi
@@ -28,6 +29,7 @@ BUILT_SOURCES += $(ARGZ_H)
 if GL_GENERATE_ARGZ_H
 argz.h: argz.in.h $(top_builddir)/config.status
 	$(AM_V_GEN)rm -f $@-t $@ && \
+	$(MKDIR_P) '%reldir%' && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
 	  cat $(srcdir)/argz.in.h; \
 	} > $@-t && \
diff --git a/modules/arpa_inet b/modules/arpa_inet
index 7e630427eb..4655998c81 100644
--- a/modules/arpa_inet
+++ b/modules/arpa_inet
@@ -25,7 +25,7 @@ BUILT_SOURCES += arpa/inet.h
 # We need the following in order to create <arpa/inet.h> when the system
 # doesn't have one.
 arpa/inet.h: arpa_inet.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(WARN_ON_USE_H) $(ARG_NONNULL_H)
-	$(AM_V_at)$(MKDIR_P) arpa
+	$(AM_V_at)$(MKDIR_P) '%reldir%/arpa'
 	$(AM_V_GEN)rm -f $@-t $@ && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
 	  sed -e 's|@''GUARD_PREFIX''@|${gl_include_guard_prefix}|g' \
diff --git a/modules/assert-h b/modules/assert-h
index 402586f2f7..aec3aae7d1 100644
--- a/modules/assert-h
+++ b/modules/assert-h
@@ -11,6 +11,7 @@ include_next
 
 configure.ac:
 gl_ASSERT_H
+AC_PROG_MKDIR_P
 
 Makefile.am:
 BUILT_SOURCES += $(ASSERT_H)
@@ -20,6 +21,7 @@ BUILT_SOURCES += $(ASSERT_H)
 if GL_GENERATE_ASSERT_H
 assert.h: assert.in.h verify.h $(top_builddir)/config.status
 	$(AM_V_GEN)rm -f $@-t $@ && \
+	$(MKDIR_P) '%reldir%' && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */' && \
 	  sed -e 's|@''INCLUDE_NEXT''@|$(INCLUDE_NEXT)|g' \
 	      -e 's|@''PRAGMA_SYSTEM_HEADER''@|@PRAGMA_SYSTEM_HEADER@|g' \
diff --git a/modules/byteswap b/modules/byteswap
index c1da40074c..43391572f2 100644
--- a/modules/byteswap
+++ b/modules/byteswap
@@ -9,6 +9,7 @@ Depends-on:
 
 configure.ac:
 gl_BYTESWAP
+AC_PROG_MKDIR_P
 
 Makefile.am:
 BUILT_SOURCES += $(BYTESWAP_H)
@@ -18,6 +19,7 @@ BUILT_SOURCES += $(BYTESWAP_H)
 if GL_GENERATE_BYTESWAP_H
 byteswap.h: byteswap.in.h $(top_builddir)/config.status
 	$(AM_V_GEN)rm -f $@-t $@ && \
+	$(MKDIR_P) '%reldir%' && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
 	  cat $(srcdir)/byteswap.in.h; \
 	} > $@-t && \
diff --git a/modules/configmake b/modules/configmake
index f2bbed654f..05a0f4a7ef 100644
--- a/modules/configmake
+++ b/modules/configmake
@@ -8,6 +8,7 @@ Depends-on:
 
 configure.ac:
 gl_CONFIGMAKE_PREP
+AC_PROG_MKDIR_P
 
 Makefile.am:
 # Listed in the same order as the GNU makefile conventions, and
@@ -16,6 +17,7 @@ Makefile.am:
 # listed in the Automake 1.10a+ documentation.
 configmake.h: Makefile
 	$(AM_V_GEN)rm -f $@-t && \
+	$(MKDIR_P) '%reldir%' && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
 	  echo '#if HAVE_WINSOCK2_H'; \
 	  echo '# include <winsock2.h> /* avoid mingw pollution on DATADIR */'; \
diff --git a/modules/ctype b/modules/ctype
index 60d7d5fb46..4d35c8ae02 100644
--- a/modules/ctype
+++ b/modules/ctype
@@ -14,6 +14,7 @@ snippet/warn-on-use
 configure.ac:
 gl_CTYPE_H
 gl_CTYPE_H_REQUIRE_DEFAULTS
+AC_PROG_MKDIR_P
 
 Makefile.am:
 BUILT_SOURCES += ctype.h
@@ -22,6 +23,7 @@ BUILT_SOURCES += ctype.h
 # doesn't have one that works with the given compiler.
 ctype.h: ctype.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(WARN_ON_USE_H)
 	$(AM_V_GEN)rm -f $@-t $@ && \
+	$(MKDIR_P) '%reldir%' && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
 	  sed -e 's|@''GUARD_PREFIX''@|${gl_include_guard_prefix}|g' \
 	      -e 's|@''INCLUDE_NEXT''@|$(INCLUDE_NEXT)|g' \
diff --git a/modules/dirent b/modules/dirent
index 80e3843ce4..3d56548c4b 100644
--- a/modules/dirent
+++ b/modules/dirent
@@ -16,6 +16,7 @@ snippet/warn-on-use
 configure.ac:
 gl_DIRENT_H
 gl_DIRENT_H_REQUIRE_DEFAULTS
+AC_PROG_MKDIR_P
 
 Makefile.am:
 BUILT_SOURCES += dirent.h
@@ -24,6 +25,7 @@ BUILT_SOURCES += dirent.h
 # doesn't have one that works with the given compiler.
 dirent.h: dirent.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H) $(WARN_ON_USE_H)
 	$(AM_V_GEN)rm -f $@-t $@ && \
+	$(MKDIR_P) '%reldir%' && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
 	  sed -e 's|@''GUARD_PREFIX''@|${gl_include_guard_prefix}|g' \
 	      -e 's|@''HAVE_DIRENT_H''@|$(HAVE_DIRENT_H)|g' \
diff --git a/modules/dynarray b/modules/dynarray
index fa608ded0e..37d1f6c753 100644
--- a/modules/dynarray
+++ b/modules/dynarray
@@ -26,7 +26,7 @@ Makefile.am:
 BUILT_SOURCES += malloc/dynarray.gl.h malloc/dynarray-skeleton.gl.h
 
 malloc/dynarray.gl.h: malloc/dynarray.h
-	$(AM_V_at)$(MKDIR_P) malloc
+	$(AM_V_at)$(MKDIR_P) '%reldir%/malloc'
 	$(AM_V_GEN)rm -f $@-t $@ && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
 	  sed -e '/libc_hidden_proto/d' < $(srcdir)/malloc/dynarray.h; \
@@ -35,7 +35,7 @@ malloc/dynarray.gl.h: malloc/dynarray.h
 MOSTLYCLEANFILES += malloc/dynarray.gl.h malloc/dynarray.gl.h-t
 
 malloc/dynarray-skeleton.gl.h: malloc/dynarray-skeleton.c
-	$(AM_V_at)$(MKDIR_P) malloc
+	$(AM_V_at)$(MKDIR_P) '%reldir%/malloc'
 	$(AM_V_GEN)rm -f $@-t $@ && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
 	  sed -e 's|<malloc/dynarray\.h>|<malloc/dynarray.gl.h>|g' \
diff --git a/modules/errno b/modules/errno
index 27810f159e..35fe9ada9c 100644
--- a/modules/errno
+++ b/modules/errno
@@ -10,6 +10,7 @@ include_next
 
 configure.ac:
 gl_HEADER_ERRNO_H
+AC_PROG_MKDIR_P
 
 Makefile.am:
 BUILT_SOURCES += $(ERRNO_H)
@@ -19,6 +20,7 @@ BUILT_SOURCES += $(ERRNO_H)
 if GL_GENERATE_ERRNO_H
 errno.h: errno.in.h $(top_builddir)/config.status
 	$(AM_V_GEN)rm -f $@-t $@ && \
+	$(MKDIR_P) '%reldir%' && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */' && \
 	  sed -e 's|@''GUARD_PREFIX''@|${gl_include_guard_prefix}|g' \
 	      -e 's|@''INCLUDE_NEXT''@|$(INCLUDE_NEXT)|g' \
diff --git a/modules/execinfo b/modules/execinfo
index 2a49f88f61..89426bb55d 100644
--- a/modules/execinfo
+++ b/modules/execinfo
@@ -11,6 +11,7 @@ extern-inline
 
 configure.ac:
 gl_EXECINFO_H
+AC_PROG_MKDIR_P
 
 Makefile.am:
 BUILT_SOURCES += $(EXECINFO_H)
@@ -20,6 +21,7 @@ BUILT_SOURCES += $(EXECINFO_H)
 if GL_GENERATE_EXECINFO_H
 execinfo.h: execinfo.in.h $(top_builddir)/config.status
 	$(AM_V_GEN)rm -f $@-t $@ && \
+	$(MKDIR_P) '%reldir%' && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
 	  cat $(srcdir)/execinfo.in.h; \
 	} > $@-t && \
diff --git a/modules/fcntl-h b/modules/fcntl-h
index 2277789d93..41dc983087 100644
--- a/modules/fcntl-h
+++ b/modules/fcntl-h
@@ -19,6 +19,7 @@ unistd
 configure.ac:
 gl_FCNTL_H
 gl_FCNTL_H_REQUIRE_DEFAULTS
+AC_PROG_MKDIR_P
 
 Makefile.am:
 BUILT_SOURCES += fcntl.h
@@ -27,6 +28,7 @@ BUILT_SOURCES += fcntl.h
 # doesn't have one that works with the given compiler.
 fcntl.h: fcntl.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H) $(WARN_ON_USE_H)
 	$(AM_V_GEN)rm -f $@-t $@ && \
+	$(MKDIR_P) '%reldir%' && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
 	  sed -e 's|@''GUARD_PREFIX''@|${gl_include_guard_prefix}|g' \
 	      -e 's|@''INCLUDE_NEXT''@|$(INCLUDE_NEXT)|g' \
diff --git a/modules/float b/modules/float
index a24e80d05c..ff68615a2f 100644
--- a/modules/float
+++ b/modules/float
@@ -12,6 +12,7 @@ include_next
 
 configure.ac:
 gl_FLOAT_H
+AC_PROG_MKDIR_P
 if test $REPLACE_FLOAT_LDBL = 1; then
   AC_LIBOBJ([float])
 fi
@@ -27,6 +28,7 @@ BUILT_SOURCES += $(FLOAT_H)
 if GL_GENERATE_FLOAT_H
 float.h: float.in.h $(top_builddir)/config.status
 	$(AM_V_GEN)rm -f $@-t $@ && \
+	$(MKDIR_P) '%reldir%' && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */' && \
 	  sed -e 's|@''GUARD_PREFIX''@|${gl_include_guard_prefix}|g' \
 	      -e 's|@''INCLUDE_NEXT''@|$(INCLUDE_NEXT)|g' \
diff --git a/modules/fnmatch-h b/modules/fnmatch-h
index 43800764c5..cfabc8cd51 100644
--- a/modules/fnmatch-h
+++ b/modules/fnmatch-h
@@ -15,6 +15,7 @@ snippet/warn-on-use
 configure.ac:
 gl_FNMATCH_H
 gl_FNMATCH_H_REQUIRE_DEFAULTS
+AC_PROG_MKDIR_P
 
 Makefile.am:
 BUILT_SOURCES += $(FNMATCH_H)
@@ -23,6 +24,7 @@ BUILT_SOURCES += $(FNMATCH_H)
 if GL_GENERATE_FNMATCH_H
 fnmatch.h: fnmatch.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H) $(WARN_ON_USE_H)
 	$(AM_V_GEN)rm -f $@-t $@ && \
+	$(MKDIR_P) '%reldir%' && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */' && \
 	  sed -e 's|@''GUARD_PREFIX''@|${gl_include_guard_prefix}|g' \
 	      -e 's|@''HAVE_FNMATCH_H''@|$(HAVE_FNMATCH_H)|g' \
diff --git a/modules/getopt-posix b/modules/getopt-posix
index 1a665e13b4..0e8e667d5a 100644
--- a/modules/getopt-posix
+++ b/modules/getopt-posix
@@ -22,6 +22,7 @@ snippet/arg-nonnull
 
 configure.ac:
 gl_FUNC_GETOPT_POSIX
+AC_PROG_MKDIR_P
 if test $REPLACE_GETOPT = 1; then
   AC_LIBOBJ([getopt])
   AC_LIBOBJ([getopt1])
@@ -38,6 +39,7 @@ BUILT_SOURCES += $(GETOPT_H) $(GETOPT_CDEFS_H)
 # doesn't have one that works with the given compiler.
 getopt.h: getopt.in.h $(top_builddir)/config.status $(ARG_NONNULL_H)
 	$(AM_V_GEN)rm -f $@-t $@ && \
+	$(MKDIR_P) '%reldir%' && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
 	  sed -e 's|@''GUARD_PREFIX''@|${gl_include_guard_prefix}|g' \
 	      -e 's|@''HAVE_GETOPT_H''@|$(HAVE_GETOPT_H)|g' \
diff --git a/modules/glob-h b/modules/glob-h
index c48c91c486..891db9d10c 100644
--- a/modules/glob-h
+++ b/modules/glob-h
@@ -19,6 +19,7 @@ sys_stat
 configure.ac:
 gl_GLOB_H
 gl_GLOB_H_REQUIRE_DEFAULTS
+AC_PROG_MKDIR_P
 
 Makefile.am:
 BUILT_SOURCES += $(GLOB_H)
@@ -27,6 +28,7 @@ BUILT_SOURCES += $(GLOB_H)
 if GL_GENERATE_GLOB_H
 glob.h: glob.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H) $(WARN_ON_USE_H)
 	$(AM_V_GEN)rm -f $@-t $@ && \
+	$(MKDIR_P) '%reldir%' && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */' && \
 	  sed -e 's|@''GUARD_PREFIX''@|${gl_include_guard_prefix}|g' \
 	      -e 's|@''HAVE_GLOB_H''@|$(HAVE_GLOB_H)|g' \
diff --git a/modules/iconv-h b/modules/iconv-h
index b7a6045270..66faaed5dd 100644
--- a/modules/iconv-h
+++ b/modules/iconv-h
@@ -14,6 +14,7 @@ snippet/warn-on-use
 configure.ac:
 gl_ICONV_H
 gl_ICONV_H_REQUIRE_DEFAULTS
+AC_PROG_MKDIR_P
 
 Makefile.am:
 BUILT_SOURCES += $(ICONV_H)
@@ -23,6 +24,7 @@ BUILT_SOURCES += $(ICONV_H)
 if GL_GENERATE_ICONV_H
 iconv.h: iconv.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H) $(WARN_ON_USE_H)
 	$(AM_V_GEN)rm -f $@-t $@ && \
+	$(MKDIR_P) '%reldir%' && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */' && \
 	  sed -e 's|@''GUARD_PREFIX''@|${gl_include_guard_prefix}|g' \
 	      -e 's|@''INCLUDE_NEXT''@|$(INCLUDE_NEXT)|g' \
diff --git a/modules/ieee754-h b/modules/ieee754-h
index e830023aa6..4ccf4208fa 100644
--- a/modules/ieee754-h
+++ b/modules/ieee754-h
@@ -9,6 +9,7 @@ Depends-on:
 
 configure.ac:
 gl_IEEE754_H
+AC_PROG_MKDIR_P
 
 Makefile.am:
 BUILT_SOURCES += $(IEEE754_H)
@@ -18,6 +19,7 @@ BUILT_SOURCES += $(IEEE754_H)
 if GL_GENERATE_IEEE754_H
 ieee754.h: ieee754.in.h $(top_builddir)/config.status
 	$(AM_V_GEN)rm -f $@-t && \
+	$(MKDIR_P) '%reldir%' && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
 	  sed -e 's/ifndef _GL_GNULIB_HEADER/if 0/g' \
 	    $(srcdir)/ieee754.in.h; \
diff --git a/modules/inttypes-incomplete b/modules/inttypes-incomplete
index 0e73c90020..af49908147 100644
--- a/modules/inttypes-incomplete
+++ b/modules/inttypes-incomplete
@@ -16,6 +16,7 @@ stdint
 configure.ac:
 gl_INTTYPES_INCOMPLETE
 gl_INTTYPES_H_REQUIRE_DEFAULTS
+AC_PROG_MKDIR_P
 
 Makefile.am:
 BUILT_SOURCES += inttypes.h
@@ -24,6 +25,7 @@ BUILT_SOURCES += inttypes.h
 # doesn't have one that works with the given compiler.
 inttypes.h: inttypes.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(WARN_ON_USE_H) $(ARG_NONNULL_H)
 	$(AM_V_GEN)rm -f $@-t $@ && \
+	$(MKDIR_P) '%reldir%' && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
 	  sed -e 's/@''HAVE_INTTYPES_H''@/$(HAVE_INTTYPES_H)/g' \
 	      -e 's|@''INCLUDE_NEXT''@|$(INCLUDE_NEXT)|g' \
diff --git a/modules/langinfo b/modules/langinfo
index 0e4aa964ad..9c1665da89 100644
--- a/modules/langinfo
+++ b/modules/langinfo
@@ -14,6 +14,7 @@ snippet/warn-on-use
 configure.ac:
 gl_LANGINFO_H
 gl_LANGINFO_H_REQUIRE_DEFAULTS
+AC_PROG_MKDIR_P
 
 Makefile.am:
 BUILT_SOURCES += langinfo.h
@@ -22,6 +23,7 @@ BUILT_SOURCES += langinfo.h
 # <langinfo.h> when the system doesn't have one.
 langinfo.h: langinfo.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(WARN_ON_USE_H)
 	$(AM_V_GEN)rm -f $@-t $@ && \
+	$(MKDIR_P) '%reldir%' && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
 	  sed -e 's|@''GUARD_PREFIX''@|${gl_include_guard_prefix}|g' \
 	      -e 's|@''HAVE_LANGINFO_H''@|$(HAVE_LANGINFO_H)|g' \
diff --git a/modules/libgmp b/modules/libgmp
index bfc62f8d86..1c347617bb 100644
--- a/modules/libgmp
+++ b/modules/libgmp
@@ -11,6 +11,7 @@ Depends-on:
 
 configure.ac:
 gl_LIBGMP
+AC_PROG_MKDIR_P
 if test $HAVE_LIBGMP != yes; then
   AC_LIBOBJ([mini-gmp-gnulib])
 fi
@@ -21,13 +22,15 @@ BUILT_SOURCES += $(GMP_H)
 if GL_GENERATE_MINI_GMP_H
 # Build gmp.h as a wrapper for mini-gmp.h when using mini-gmp.
 gmp.h: $(top_builddir)/config.status
-	echo '#include "mini-gmp.h"' >$@-t
+	$(MKDIR_P) '%reldir%'
+	echo '#include "mini-gmp.h"' > $@-t
 	mv $@-t $@
 else
 if GL_GENERATE_GMP_GMP_H
 # Build gmp.h as a wrapper for gmp/gmp.h.
 gmp.h: $(top_builddir)/config.status
-	echo '#include <gmp/gmp.h>' >$@-t
+	$(MKDIR_P) '%reldir%'
+	echo '#include <gmp/gmp.h>' > $@-t
 	mv $@-t $@
 else
 gmp.h: $(top_builddir)/config.status
diff --git a/modules/libtextstyle-optional b/modules/libtextstyle-optional
index c0935e1f56..342d718890 100644
--- a/modules/libtextstyle-optional
+++ b/modules/libtextstyle-optional
@@ -15,6 +15,7 @@ vasprintf-posix
 
 configure.ac:
 # You need to invoke gl_LIBTEXTSTYLE_OPTIONAL yourself, possibly with arguments.
+AC_PROG_MKDIR_P
 
 Makefile.am:
 BUILT_SOURCES += $(TEXTSTYLE_H)
@@ -24,6 +25,7 @@ BUILT_SOURCES += $(TEXTSTYLE_H)
 if GL_GENERATE_TEXTSTYLE_H
 textstyle.h: textstyle.in.h $(top_builddir)/config.status
 	$(AM_V_GEN)rm -f $@-t $@ && \
+	$(MKDIR_P) '%reldir%' && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
 	  cat $(srcdir)/textstyle.in.h; \
 	} > $@-t && \
diff --git a/modules/limits-h b/modules/limits-h
index 9c60ec9c04..3f5dcf9e30 100644
--- a/modules/limits-h
+++ b/modules/limits-h
@@ -10,6 +10,7 @@ include_next
 
 configure.ac:
 gl_LIMITS_H
+AC_PROG_MKDIR_P
 
 Makefile.am:
 BUILT_SOURCES += $(LIMITS_H)
@@ -19,6 +20,7 @@ BUILT_SOURCES += $(LIMITS_H)
 if GL_GENERATE_LIMITS_H
 limits.h: limits.in.h $(top_builddir)/config.status
 	$(AM_V_GEN)rm -f $@-t $@ && \
+	$(MKDIR_P) '%reldir%' && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */' && \
 	  sed -e 's|@''GUARD_PREFIX''@|${gl_include_guard_prefix}|g' \
 	      -e 's|@''INCLUDE_NEXT''@|$(INCLUDE_NEXT)|g' \
diff --git a/modules/locale b/modules/locale
index b2773c8a19..7bb7cfc98b 100644
--- a/modules/locale
+++ b/modules/locale
@@ -16,6 +16,7 @@ stddef
 configure.ac:
 gl_LOCALE_H
 gl_LOCALE_H_REQUIRE_DEFAULTS
+AC_PROG_MKDIR_P
 
 Makefile.am:
 BUILT_SOURCES += locale.h
@@ -24,6 +25,7 @@ BUILT_SOURCES += locale.h
 # doesn't have one that provides all definitions.
 locale.h: locale.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H) $(WARN_ON_USE_H)
 	$(AM_V_GEN)rm -f $@-t $@ && \
+	$(MKDIR_P) '%reldir%' && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */' && \
 	  sed -e 's|@''GUARD_PREFIX''@|${gl_include_guard_prefix}|g' \
 	      -e 's|@''INCLUDE_NEXT''@|$(INCLUDE_NEXT)|g' \
diff --git a/modules/malloc-h b/modules/malloc-h
index f79fa3ed82..9a902e02c8 100644
--- a/modules/malloc-h
+++ b/modules/malloc-h
@@ -13,6 +13,7 @@ snippet/warn-on-use
 configure.ac:
 gl_MALLOC_H
 gl_MALLOC_H_REQUIRE_DEFAULTS
+AC_PROG_MKDIR_P
 
 Makefile.am:
 BUILT_SOURCES += malloc.h
@@ -20,6 +21,7 @@ BUILT_SOURCES += malloc.h
 # We need the following in order to create <malloc.h> when we add workarounds.
 malloc.h: malloc.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(WARN_ON_USE_H)
 	$(AM_V_GEN)rm -f $@-t $@ && \
+	$(MKDIR_P) '%reldir%' && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
 	  sed -e 's|@''GUARD_PREFIX''@|${gl_include_guard_prefix}|g' \
 	      -e 's|@''HAVE_MALLOC_H''@|$(HAVE_MALLOC_H)|g' \
diff --git a/modules/math b/modules/math
index 3041609125..da84455247 100644
--- a/modules/math
+++ b/modules/math
@@ -17,6 +17,7 @@ float
 configure.ac:
 gl_MATH_H
 gl_MATH_H_REQUIRE_DEFAULTS
+AC_PROG_MKDIR_P
 
 Makefile.am:
 BUILT_SOURCES += math.h
@@ -26,6 +27,7 @@ lib_SOURCES += math.c
 # doesn't have one that works with the given compiler.
 math.h: math.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H) $(WARN_ON_USE_H)
 	$(AM_V_GEN)rm -f $@-t $@ && \
+	$(MKDIR_P) '%reldir%' && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */' && \
 	  sed -e 's|@''GUARD_PREFIX''@|${gl_include_guard_prefix}|g' \
 	      -e 's|@''INCLUDE_NEXT_AS_FIRST_DIRECTIVE''@|$(INCLUDE_NEXT_AS_FIRST_DIRECTIVE)|g' \
diff --git a/modules/monetary b/modules/monetary
index 451323b6ac..645062c68d 100644
--- a/modules/monetary
+++ b/modules/monetary
@@ -14,6 +14,7 @@ snippet/warn-on-use
 configure.ac:
 gl_MONETARY_H
 gl_MONETARY_H_REQUIRE_DEFAULTS
+AC_PROG_MKDIR_P
 
 Makefile.am:
 BUILT_SOURCES += $(MONETARY_H)
@@ -23,6 +24,7 @@ BUILT_SOURCES += $(MONETARY_H)
 if GL_GENERATE_MONETARY_H
 monetary.h: monetary.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(WARN_ON_USE_H) $(ARG_NONNULL_H)
 	$(AM_V_GEN)rm -f $@-t $@ && \
+	$(MKDIR_P) '%reldir%' && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */' && \
 	  sed -e 's|@''GUARD_PREFIX''@|${gl_include_guard_prefix}|g' \
 	      -e 's|@''HAVE_MONETARY_H''@|$(HAVE_MONETARY_H)|g' \
diff --git a/modules/net_if b/modules/net_if
index 9e30f01512..467544e4d0 100644
--- a/modules/net_if
+++ b/modules/net_if
@@ -20,7 +20,7 @@ BUILT_SOURCES += $(NET_IF_H)
 # doesn't have one.
 if GL_GENERATE_NET_IF_H
 net/if.h: net_if.in.h $(top_builddir)/config.status
-	$(AM_V_at)$(MKDIR_P) net
+	$(AM_V_at)$(MKDIR_P) '%reldir%/net'
 	$(AM_V_GEN)rm -f $@-t $@ && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
 	  sed -e 's|@''GUARD_PREFIX''@|${gl_include_guard_prefix}|g' \
diff --git a/modules/netdb b/modules/netdb
index 8c589df669..9ad12eb93c 100644
--- a/modules/netdb
+++ b/modules/netdb
@@ -15,6 +15,7 @@ sys_socket
 configure.ac:
 gl_NETDB_H
 gl_NETDB_H_REQUIRE_DEFAULTS
+AC_PROG_MKDIR_P
 
 Makefile.am:
 BUILT_SOURCES += netdb.h
@@ -23,6 +24,7 @@ BUILT_SOURCES += netdb.h
 # doesn't have one that works with the given compiler.
 netdb.h: netdb.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H) $(WARN_ON_USE_H)
 	$(AM_V_GEN)rm -f $@-t $@ && \
+	$(MKDIR_P) '%reldir%' && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
 	  sed -e 's|@''GUARD_PREFIX''@|${gl_include_guard_prefix}|g' \
 	      -e 's|@''INCLUDE_NEXT''@|$(INCLUDE_NEXT)|g' \
diff --git a/modules/netinet_in b/modules/netinet_in
index 7d09a3761b..da21e0d6bd 100644
--- a/modules/netinet_in
+++ b/modules/netinet_in
@@ -20,7 +20,7 @@ BUILT_SOURCES += $(NETINET_IN_H)
 # doesn't have one.
 if GL_GENERATE_NETINET_IN_H
 netinet/in.h: netinet_in.in.h $(top_builddir)/config.status
-	$(AM_V_at)$(MKDIR_P) netinet
+	$(AM_V_at)$(MKDIR_P) '%reldir%/netinet'
 	$(AM_V_GEN)rm -f $@-t $@ && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
 	  sed -e 's|@''GUARD_PREFIX''@|${gl_include_guard_prefix}|g' \
diff --git a/modules/openmp-init b/modules/openmp-init
index e3975ca473..f3fd23e774 100644
--- a/modules/openmp-init
+++ b/modules/openmp-init
@@ -13,6 +13,7 @@ setenv
 
 configure.ac:
 gl_OMP_H
+AC_PROG_MKDIR_P
 
 Makefile.am:
 BUILT_SOURCES += omp.h
@@ -22,6 +23,7 @@ BUILT_SOURCES += omp.h
 omp.h: omp.in.h $(top_builddir)/config.status $(CXXDEFS_H) \
   $(_NORETURN_H) $(ARG_NONNULL_H) $(WARN_ON_USE_H)
 	$(AM_V_GEN)rm -f $@-t $@ && \
+	$(MKDIR_P) '%reldir%' && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */' && \
 	  sed -e 's|@''GUARD_PREFIX''@|${gl_include_guard_prefix}|g' \
 	      -e 's|@''INCLUDE_NEXT''@|$(INCLUDE_NEXT)|g' \
diff --git a/modules/poll-h b/modules/poll-h
index 998607dc38..4287299e54 100644
--- a/modules/poll-h
+++ b/modules/poll-h
@@ -15,6 +15,7 @@ snippet/warn-on-use
 configure.ac:
 gl_POLL_H
 gl_POLL_H_REQUIRE_DEFAULTS
+AC_PROG_MKDIR_P
 
 Makefile.am:
 BUILT_SOURCES += poll.h
@@ -23,6 +24,7 @@ BUILT_SOURCES += poll.h
 # doesn't have one.
 poll.h: poll.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(WARN_ON_USE_H)
 	$(AM_V_GEN)rm -f $@-t $@ && \
+	$(MKDIR_P) '%reldir%' && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
 	  sed -e 's|@''GUARD_PREFIX''@|${gl_include_guard_prefix}|g' \
 	      -e 's|@''HAVE_POLL_H''@|$(HAVE_POLL_H)|g' \
diff --git a/modules/posix-shell b/modules/posix-shell
index 8f7b864e88..0773691671 100644
--- a/modules/posix-shell
+++ b/modules/posix-shell
@@ -8,14 +8,16 @@ Depends-on:
 
 configure.ac:
 gl_POSIX_SHELL
+AC_PROG_MKDIR_P
 
 Makefile.am:
 ##Sample usage of posix-shell module:
 #script: script.in
 #	rm -f $@-t $@
+#	$(MKDIR_P) '%reldir%'
 #	sed -e 's#@''PREFERABLY_POSIX_SHELL''@#$(PREFERABLY_POSIX_SHELL)#g' \
 #	    -e 's#@''POSIX_SHELL''@#$(POSIX_SHELL)#g' \
-#	    -e $(srcdir)/$@.in >$@-t
+#	    -e $(srcdir)/$@.in > $@-t
 #	chmod a+x $@-t
 #	mv $@-t $@
 #EXTRA_DIST += script.in
diff --git a/modules/pthread-h b/modules/pthread-h
index dec17389e6..cecf43966a 100644
--- a/modules/pthread-h
+++ b/modules/pthread-h
@@ -22,6 +22,7 @@ gl_ANYTHREADLIB_EARLY
 configure.ac:
 gl_PTHREAD_H
 gl_PTHREAD_H_REQUIRE_DEFAULTS
+AC_PROG_MKDIR_P
 
 Makefile.am:
 BUILT_SOURCES += pthread.h
@@ -30,6 +31,7 @@ BUILT_SOURCES += pthread.h
 # doesn't have one that works with the given compiler.
 pthread.h: pthread.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(_NORETURN_H) $(ARG_NONNULL_H) $(WARN_ON_USE_H)
 	$(AM_V_GEN)rm -f $@-t $@ && \
+	$(MKDIR_P) '%reldir%' && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
 	  sed -e 's|@''GUARD_PREFIX''@|${gl_include_guard_prefix}|g' \
 	      -e 's|@''HAVE_PTHREAD_H''@|$(HAVE_PTHREAD_H)|g' \
diff --git a/modules/pty b/modules/pty
index ae0790089c..2814d3ca58 100644
--- a/modules/pty
+++ b/modules/pty
@@ -13,6 +13,7 @@ snippet/warn-on-use
 configure.ac:
 gl_PTY_H
 gl_PTY_H_REQUIRE_DEFAULTS
+AC_PROG_MKDIR_P
 
 Makefile.am:
 BUILT_SOURCES += pty.h
@@ -21,6 +22,7 @@ BUILT_SOURCES += pty.h
 # doesn't have one that works with the given compiler.
 pty.h: pty.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(WARN_ON_USE_H)
 	$(AM_V_GEN)rm -f $@-t $@ && \
+	$(MKDIR_P) '%reldir%' && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
 	  sed -e 's|@''GUARD_PREFIX''@|${gl_include_guard_prefix}|g' \
 	      -e 's|@''HAVE_PTY_H''@|$(HAVE_PTY_H)|g' \
diff --git a/modules/sched b/modules/sched
index 995dfb95e2..ea706e8fc7 100644
--- a/modules/sched
+++ b/modules/sched
@@ -14,6 +14,7 @@ snippet/warn-on-use
 configure.ac:
 gl_SCHED_H
 gl_SCHED_H_REQUIRE_DEFAULTS
+AC_PROG_MKDIR_P
 
 Makefile.am:
 BUILT_SOURCES += sched.h
@@ -22,6 +23,7 @@ BUILT_SOURCES += sched.h
 # the system doesn't have one.
 sched.h: sched.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(WARN_ON_USE_H)
 	$(AM_V_GEN)rm -f $@-t $@ && \
+	$(MKDIR_P) '%reldir%' && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
 	  sed -e 's|@''GUARD_PREFIX''@|${gl_include_guard_prefix}|g' \
 	      -e 's|@''HAVE_SCHED_H''@|$(HAVE_SCHED_H)|g' \
diff --git a/modules/scratch_buffer b/modules/scratch_buffer
index 184f0b6bf1..336a534725 100644
--- a/modules/scratch_buffer
+++ b/modules/scratch_buffer
@@ -26,7 +26,7 @@ Makefile.am:
 BUILT_SOURCES += malloc/scratch_buffer.gl.h
 
 malloc/scratch_buffer.gl.h: malloc/scratch_buffer.h
-	$(AM_V_at)$(MKDIR_P) malloc
+	$(AM_V_at)$(MKDIR_P) '%reldir%/malloc'
 	$(AM_V_GEN)rm -f $@-t $@ && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
 	  sed -e 's|__always_inline|inline _GL_ATTRIBUTE_ALWAYS_INLINE|g' \
diff --git a/modules/search b/modules/search
index 364107485b..d24c37b16a 100644
--- a/modules/search
+++ b/modules/search
@@ -14,6 +14,7 @@ snippet/warn-on-use
 configure.ac:
 gl_SEARCH_H
 gl_SEARCH_H_REQUIRE_DEFAULTS
+AC_PROG_MKDIR_P
 
 Makefile.am:
 BUILT_SOURCES += search.h
@@ -22,6 +23,7 @@ BUILT_SOURCES += search.h
 # doesn't have one that works with the given compiler.
 search.h: search.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H) $(WARN_ON_USE_H)
 	$(AM_V_GEN)rm -f $@-t $@ && \
+	$(MKDIR_P) '%reldir%' && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */' && \
 	  sed -e 's|@''GUARD_PREFIX''@|${gl_include_guard_prefix}|g' \
 	      -e 's|@''HAVE_SEARCH_H''@|$(HAVE_SEARCH_H)|g' \
diff --git a/modules/selinux-h b/modules/selinux-h
index 00328bf20f..d0e3b29dbb 100644
--- a/modules/selinux-h
+++ b/modules/selinux-h
@@ -21,6 +21,7 @@ configure.ac:
 gl_HEADERS_SELINUX_SELINUX_H
 gl_HEADERS_SELINUX_CONTEXT_H
 gl_HEADERS_SELINUX_LABEL_H
+AC_PROG_MKDIR_P
 if test "$with_selinux" != no && test "$ac_cv_header_selinux_selinux_h" = yes; then
   AC_LIBOBJ([getfilecon])
 fi
@@ -31,7 +32,7 @@ lib_SOURCES += se-context.in.h se-label.in.h se-selinux.in.h \
 
 BUILT_SOURCES += selinux/selinux.h
 selinux/selinux.h: se-selinux.in.h $(top_builddir)/config.status
-	$(AM_V_at)$(MKDIR_P) selinux
+	$(AM_V_at)$(MKDIR_P) '%reldir%/selinux'
 	$(AM_V_GEN)rm -f $@-t $@ && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */' && \
 	  sed -e 's|@''GUARD_PREFIX''@|${gl_include_guard_prefix}|g' \
@@ -47,7 +48,7 @@ MOSTLYCLEANFILES += selinux/selinux.h selinux/selinux.h-t
 BUILT_SOURCES += $(SELINUX_CONTEXT_H)
 if GL_GENERATE_SELINUX_CONTEXT_H
 selinux/context.h: se-context.in.h $(top_builddir)/config.status
-	$(AM_V_at)$(MKDIR_P) selinux
+	$(AM_V_at)$(MKDIR_P) '%reldir%/selinux'
 	$(AM_V_GEN)rm -f $@-t $@ && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */' && \
 	  cat $(srcdir)/se-context.in.h; \
@@ -62,7 +63,7 @@ MOSTLYCLEANFILES += selinux/context.h selinux/context.h-t
 BUILT_SOURCES += $(SELINUX_LABEL_H)
 if GL_GENERATE_SELINUX_LABEL_H
 selinux/label.h: se-label.in.h $(top_builddir)/config.status
-	$(AM_V_at)$(MKDIR_P) selinux
+	$(AM_V_at)$(MKDIR_P) '%reldir%/selinux'
 	$(AM_V_GEN)rm -f $@-t $@ && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */' && \
 	  cat $(srcdir)/se-label.in.h; \
diff --git a/modules/signal-h b/modules/signal-h
index cca349d7c1..a0285f2925 100644
--- a/modules/signal-h
+++ b/modules/signal-h
@@ -16,6 +16,7 @@ extensions
 configure.ac:
 gl_SIGNAL_H
 gl_SIGNAL_H_REQUIRE_DEFAULTS
+AC_PROG_MKDIR_P
 
 Makefile.am:
 BUILT_SOURCES += signal.h
@@ -24,6 +25,7 @@ BUILT_SOURCES += signal.h
 # doesn't have a complete one.
 signal.h: signal.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H) $(WARN_ON_USE_H)
 	$(AM_V_GEN)rm -f $@-t $@ && \
+	$(MKDIR_P) '%reldir%' && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */' && \
 	  sed -e 's|@''GUARD_PREFIX''@|${gl_include_guard_prefix}|g' \
 	      -e 's|@''INCLUDE_NEXT''@|$(INCLUDE_NEXT)|g' \
diff --git a/modules/sigsegv b/modules/sigsegv
index fdd815c370..fa0a1d5263 100644
--- a/modules/sigsegv
+++ b/modules/sigsegv
@@ -37,6 +37,7 @@ else
 fi
 AC_SUBST([SIGSEGV_H])
 AM_CONDITIONAL([GL_GENERATE_SIGSEGV_H], [test -n "$SIGSEGV_H"])
+AC_PROG_MKDIR_P
 if test -n "$SIGSEGV_H"; then
   dnl Persuade glibc <sys/ucontext.h> to declare macros designating register
   dnl indices: REG_RSP on x86_64, REG_ESP on i386.
@@ -71,6 +72,7 @@ BUILT_SOURCES += $(SIGSEGV_H)
 if GL_GENERATE_SIGSEGV_H
 sigsegv.h: sigsegv.in.h $(top_builddir)/config.status
 	$(AM_V_GEN)rm -f $@-t $@ && \
+	$(MKDIR_P) '%reldir%' && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
 	  cat $(srcdir)/sigsegv.in.h; \
 	} > $@-t && \
diff --git a/modules/snippet/link-warning b/modules/snippet/link-warning
index 8c1b16b0be..6727eedcae 100644
--- a/modules/snippet/link-warning
+++ b/modules/snippet/link-warning
@@ -11,6 +11,7 @@ Depends-on:
 
 configure.ac:
 AC_REQUIRE([gl_FEATURES_H])
+AC_PROG_MKDIR_P
 
 Makefile.am:
 # The BUILT_SOURCES created by this Makefile snippet are not used via #include
@@ -25,6 +26,7 @@ Makefile.am:
 BUILT_SOURCES += link-warning.h
 link-warning.h: $(top_srcdir)/build-aux/snippet/link-warning.h
 	$(AM_V_GEN)rm -f $@-t $@ && \
+	$(MKDIR_P) '%reldir%' && \
 	sed -n -e '/HAVE_FEATURES_H/,$$p' \
 	  < $(top_srcdir)/build-aux/snippet/link-warning.h \
 	| sed -e 's|@''HAVE_FEATURES_H''@|$(HAVE_FEATURES_H)|g' \
diff --git a/modules/spawn b/modules/spawn
index 312c5ec9bd..8ad028c10e 100644
--- a/modules/spawn
+++ b/modules/spawn
@@ -17,6 +17,7 @@ snippet/warn-on-use
 configure.ac:
 gl_SPAWN_H
 gl_SPAWN_H_REQUIRE_DEFAULTS
+AC_PROG_MKDIR_P
 
 Makefile.am:
 BUILT_SOURCES += spawn.h
@@ -25,6 +26,7 @@ BUILT_SOURCES += spawn.h
 # the system doesn't have one.
 spawn.h: spawn.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H) $(WARN_ON_USE_H)
 	$(AM_V_GEN)rm -f $@-t $@ && \
+	$(MKDIR_P) '%reldir%' && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
 	  sed -e 's|@''GUARD_PREFIX''@|${gl_include_guard_prefix}|g' \
 	      -e 's|@''HAVE_SPAWN_H''@|$(HAVE_SPAWN_H)|g' \
diff --git a/modules/stdalign b/modules/stdalign
index a2c6125218..af25b34b88 100644
--- a/modules/stdalign
+++ b/modules/stdalign
@@ -9,6 +9,7 @@ Depends-on:
 
 configure.ac:
 gl_STDALIGN_H
+AC_PROG_MKDIR_P
 
 Makefile.am:
 BUILT_SOURCES += $(STDALIGN_H)
@@ -18,6 +19,7 @@ BUILT_SOURCES += $(STDALIGN_H)
 if GL_GENERATE_STDALIGN_H
 stdalign.h: stdalign.in.h $(top_builddir)/config.status
 	$(AM_V_GEN)rm -f $@-t $@ && \
+	$(MKDIR_P) '%reldir%' && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
 	  cat $(srcdir)/stdalign.in.h; \
 	} > $@-t && \
diff --git a/modules/stdarg b/modules/stdarg
index 0a2f27a6c3..f4f37abac5 100644
--- a/modules/stdarg
+++ b/modules/stdarg
@@ -15,6 +15,7 @@ gl_PROG_CC_C99
 
 configure.ac:
 gl_STDARG_H
+AC_PROG_MKDIR_P
 
 Makefile.am:
 BUILT_SOURCES += $(STDARG_H)
@@ -24,6 +25,7 @@ BUILT_SOURCES += $(STDARG_H)
 if GL_GENERATE_STDARG_H
 stdarg.h: stdarg.in.h $(top_builddir)/config.status
 	$(AM_V_GEN)rm -f $@-t $@ && \
+	$(MKDIR_P) '%reldir%' && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */' && \
 	  sed -e 's|@''GUARD_PREFIX''@|${gl_include_guard_prefix}|g' \
 	      -e 's|@''INCLUDE_NEXT''@|$(INCLUDE_NEXT)|g' \
diff --git a/modules/stdbool b/modules/stdbool
index b1fcdda456..d3f434cc55 100644
--- a/modules/stdbool
+++ b/modules/stdbool
@@ -10,6 +10,7 @@ Depends-on:
 
 configure.ac:
 AM_STDBOOL_H
+AC_PROG_MKDIR_P
 
 Makefile.am:
 BUILT_SOURCES += $(STDBOOL_H)
@@ -19,6 +20,7 @@ BUILT_SOURCES += $(STDBOOL_H)
 if GL_GENERATE_STDBOOL_H
 stdbool.h: stdbool.in.h $(top_builddir)/config.status
 	$(AM_V_GEN)rm -f $@-t $@ && \
+	$(MKDIR_P) '%reldir%' && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
 	  sed -e 's/@''HAVE__BOOL''@/$(HAVE__BOOL)/g' < $(srcdir)/stdbool.in.h; \
 	} > $@-t && \
diff --git a/modules/stddef b/modules/stddef
index c17523cac8..d69b861f6b 100644
--- a/modules/stddef
+++ b/modules/stddef
@@ -13,6 +13,7 @@ extensions
 configure.ac:
 gl_STDDEF_H
 gl_STDDEF_H_REQUIRE_DEFAULTS
+AC_PROG_MKDIR_P
 
 Makefile.am:
 BUILT_SOURCES += $(STDDEF_H)
@@ -22,6 +23,7 @@ BUILT_SOURCES += $(STDDEF_H)
 if GL_GENERATE_STDDEF_H
 stddef.h: stddef.in.h $(top_builddir)/config.status
 	$(AM_V_GEN)rm -f $@-t $@ && \
+	$(MKDIR_P) '%reldir%' && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */' && \
 	  sed -e 's|@''GUARD_PREFIX''@|${gl_include_guard_prefix}|g' \
 	      -e 's|@''INCLUDE_NEXT''@|$(INCLUDE_NEXT)|g' \
diff --git a/modules/stdint b/modules/stdint
index 0c7e878e45..ea38736bfa 100644
--- a/modules/stdint
+++ b/modules/stdint
@@ -20,6 +20,7 @@ sys_types
 
 configure.ac:
 gl_STDINT_H
+AC_PROG_MKDIR_P
 
 Makefile.am:
 BUILT_SOURCES += $(STDINT_H)
@@ -29,6 +30,7 @@ BUILT_SOURCES += $(STDINT_H)
 if GL_GENERATE_STDINT_H
 stdint.h: stdint.in.h $(top_builddir)/config.status
 	$(AM_V_GEN)rm -f $@-t $@ && \
+	$(MKDIR_P) '%reldir%' && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
 	  sed -e 's|@''GUARD_PREFIX''@|${gl_include_guard_prefix}|g' \
 	      -e 's/@''HAVE_STDINT_H''@/$(HAVE_STDINT_H)/g' \
diff --git a/modules/stdio b/modules/stdio
index e13b689375..f286f4b445 100644
--- a/modules/stdio
+++ b/modules/stdio
@@ -18,6 +18,7 @@ sys_types
 configure.ac:
 gl_STDIO_H
 gl_STDIO_H_REQUIRE_DEFAULTS
+AC_PROG_MKDIR_P
 
 dnl No need to create extra modules for these functions. Everyone who uses
 dnl <stdio.h> likely needs them.
@@ -51,6 +52,7 @@ BUILT_SOURCES += stdio.h
 # doesn't have one that works with the given compiler.
 stdio.h: stdio.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H) $(WARN_ON_USE_H)
 	$(AM_V_GEN)rm -f $@-t $@ && \
+	$(MKDIR_P) '%reldir%' && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */' && \
 	  sed -e 's|@''GUARD_PREFIX''@|${gl_include_guard_prefix}|g' \
 	      -e 's|@''INCLUDE_NEXT''@|$(INCLUDE_NEXT)|g' \
diff --git a/modules/stdlib b/modules/stdlib
index da5629dc9a..f4a3388726 100644
--- a/modules/stdlib
+++ b/modules/stdlib
@@ -17,6 +17,7 @@ unistd
 configure.ac:
 gl_STDLIB_H
 gl_STDLIB_H_REQUIRE_DEFAULTS
+AC_PROG_MKDIR_P
 
 Makefile.am:
 BUILT_SOURCES += stdlib.h
@@ -26,6 +27,7 @@ BUILT_SOURCES += stdlib.h
 stdlib.h: stdlib.in.h $(top_builddir)/config.status $(CXXDEFS_H) \
   $(_NORETURN_H) $(ARG_NONNULL_H) $(WARN_ON_USE_H)
 	$(AM_V_GEN)rm -f $@-t $@ && \
+	$(MKDIR_P) '%reldir%' && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */' && \
 	  sed -e 's|@''GUARD_PREFIX''@|${gl_include_guard_prefix}|g' \
 	      -e 's|@''INCLUDE_NEXT''@|$(INCLUDE_NEXT)|g' \
diff --git a/modules/stdnoreturn b/modules/stdnoreturn
index acd661b282..d2b9566255 100644
--- a/modules/stdnoreturn
+++ b/modules/stdnoreturn
@@ -10,6 +10,7 @@ snippet/_Noreturn
 
 configure.ac:
 gl_STDNORETURN_H
+AC_PROG_MKDIR_P
 
 Makefile.am:
 BUILT_SOURCES += $(STDNORETURN_H)
@@ -19,6 +20,7 @@ BUILT_SOURCES += $(STDNORETURN_H)
 if GL_GENERATE_STDNORETURN_H
 stdnoreturn.h: stdnoreturn.in.h $(top_builddir)/config.status $(_NORETURN_H)
 	$(AM_V_GEN)rm -f $@-t $@ && \
+	$(MKDIR_P) '%reldir%' && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */' && \
 	  sed -e '/definition of _Noreturn/r $(_NORETURN_H)' \
               < $(srcdir)/stdnoreturn.in.h; \
diff --git a/modules/string b/modules/string
index e9c3153997..64f543e082 100644
--- a/modules/string
+++ b/modules/string
@@ -18,6 +18,7 @@ stdlib
 configure.ac:
 gl_STRING_H
 gl_STRING_H_REQUIRE_DEFAULTS
+AC_PROG_MKDIR_P
 
 Makefile.am:
 BUILT_SOURCES += string.h
@@ -26,6 +27,7 @@ BUILT_SOURCES += string.h
 # doesn't have one that works with the given compiler.
 string.h: string.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H) $(WARN_ON_USE_H)
 	$(AM_V_GEN)rm -f $@-t $@ && \
+	$(MKDIR_P) '%reldir%' && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */' && \
 	  sed -e 's|@''GUARD_PREFIX''@|${gl_include_guard_prefix}|g' \
 	      -e 's|@''INCLUDE_NEXT''@|$(INCLUDE_NEXT)|g' \
diff --git a/modules/strings b/modules/strings
index d705ac9e5f..e3b7b28850 100644
--- a/modules/strings
+++ b/modules/strings
@@ -15,6 +15,7 @@ sys_types
 configure.ac:
 gl_STRINGS_H
 gl_STRINGS_H_REQUIRE_DEFAULTS
+AC_PROG_MKDIR_P
 
 Makefile.am:
 BUILT_SOURCES += strings.h
@@ -23,6 +24,7 @@ BUILT_SOURCES += strings.h
 # doesn't have one that works with the given compiler.
 strings.h: strings.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(WARN_ON_USE_H) $(ARG_NONNULL_H)
 	$(AM_V_GEN)rm -f $@-t $@ && \
+	$(MKDIR_P) '%reldir%' && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */' && \
 	  sed -e 's|@''GUARD_PREFIX''@|${gl_include_guard_prefix}|g' \
 	      -e 's|@''HAVE_STRINGS_H''@|$(HAVE_STRINGS_H)|g' \
diff --git a/modules/sys_file b/modules/sys_file
index a5d98013e4..753f6e1e8e 100644
--- a/modules/sys_file
+++ b/modules/sys_file
@@ -20,7 +20,7 @@ BUILT_SOURCES += sys/file.h
 # We need the following in order to create <sys/file.h> when the system
 # has one that is incomplete.
 sys/file.h: sys_file.in.h $(top_builddir)/config.status $(WARN_ON_USE_H)
-	$(AM_V_at)$(MKDIR_P) sys
+	$(AM_V_at)$(MKDIR_P) '%reldir%/sys'
 	$(AM_V_GEN)rm -f $@-t $@ && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
 	  sed -e 's|@''GUARD_PREFIX''@|${gl_include_guard_prefix}|g' \
diff --git a/modules/sys_ioctl b/modules/sys_ioctl
index 06b85c7f47..571dc822b7 100644
--- a/modules/sys_ioctl
+++ b/modules/sys_ioctl
@@ -22,7 +22,7 @@ BUILT_SOURCES += sys/ioctl.h
 # We need the following in order to create <sys/ioctl.h> when the system
 # does not have a complete one.
 sys/ioctl.h: sys_ioctl.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(WARN_ON_USE_H)
-	$(AM_V_at)$(MKDIR_P) sys
+	$(AM_V_at)$(MKDIR_P) '%reldir%/sys'
 	$(AM_V_GEN)rm -f $@-t $@ && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
 	  sed -e 's|@''GUARD_PREFIX''@|${gl_include_guard_prefix}|g' \
diff --git a/modules/sys_random b/modules/sys_random
index 5044baa4ec..331103ae6b 100644
--- a/modules/sys_random
+++ b/modules/sys_random
@@ -22,7 +22,7 @@ BUILT_SOURCES += sys/random.h
 # We need the following in order to create <sys/random.h> when the system
 # doesn't have one.
 sys/random.h: sys_random.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H) $(WARN_ON_USE_H)
-	$(AM_V_at)$(MKDIR_P) sys
+	$(AM_V_at)$(MKDIR_P) '%reldir%/sys'
 	$(AM_V_GEN)rm -f $@-t $@ && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
 	  sed -e 's|@''GUARD_PREFIX''@|${gl_include_guard_prefix}|g' \
diff --git a/modules/sys_resource b/modules/sys_resource
index 83caef5028..5c68b981b0 100644
--- a/modules/sys_resource
+++ b/modules/sys_resource
@@ -23,7 +23,7 @@ BUILT_SOURCES += sys/resource.h
 # We need the following in order to create <sys/resource.h> when the system
 # doesn't have one.
 sys/resource.h: sys_resource.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H) $(WARN_ON_USE_H)
-	$(AM_V_at)$(MKDIR_P) sys
+	$(AM_V_at)$(MKDIR_P) '%reldir%/sys'
 	$(AM_V_GEN)rm -f $@-t $@ && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
 	  sed -e 's|@''GUARD_PREFIX''@|${gl_include_guard_prefix}|g' \
diff --git a/modules/sys_select b/modules/sys_select
index 89f07b26ee..61e78f8270 100644
--- a/modules/sys_select
+++ b/modules/sys_select
@@ -24,7 +24,7 @@ BUILT_SOURCES += sys/select.h
 # We need the following in order to create <sys/select.h> when the system
 # doesn't have one that works with the given compiler.
 sys/select.h: sys_select.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(WARN_ON_USE_H)
-	$(AM_V_at)$(MKDIR_P) sys
+	$(AM_V_at)$(MKDIR_P) '%reldir%/sys'
 	$(AM_V_GEN)rm -f $@-t $@ && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
 	  sed -e 's|@''GUARD_PREFIX''@|${gl_include_guard_prefix}|g' \
diff --git a/modules/sys_socket b/modules/sys_socket
index 72a6a4a7ee..dae62f250e 100644
--- a/modules/sys_socket
+++ b/modules/sys_socket
@@ -31,7 +31,7 @@ lib_SOURCES += sys_socket.c
 # We need the following in order to create <sys/socket.h> when the system
 # doesn't have one that works with the given compiler.
 sys/socket.h: sys_socket.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(WARN_ON_USE_H) $(ARG_NONNULL_H)
-	$(AM_V_at)$(MKDIR_P) sys
+	$(AM_V_at)$(MKDIR_P) '%reldir%/sys'
 	$(AM_V_GEN)rm -f $@-t $@ && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
 	  sed -e 's|@''GUARD_PREFIX''@|${gl_include_guard_prefix}|g' \
diff --git a/modules/sys_stat b/modules/sys_stat
index 36bc9567f8..b778038c7c 100644
--- a/modules/sys_stat
+++ b/modules/sys_stat
@@ -26,7 +26,7 @@ BUILT_SOURCES += sys/stat.h
 # We need the following in order to create <sys/stat.h> when the system
 # has one that is incomplete.
 sys/stat.h: sys_stat.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H) $(WARN_ON_USE_H)
-	$(AM_V_at)$(MKDIR_P) sys
+	$(AM_V_at)$(MKDIR_P) '%reldir%/sys'
 	$(AM_V_GEN)rm -f $@-t $@ && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
 	  sed -e 's|@''GUARD_PREFIX''@|${gl_include_guard_prefix}|g' \
diff --git a/modules/sys_time b/modules/sys_time
index b24e68e6c5..07838d2cd1 100644
--- a/modules/sys_time
+++ b/modules/sys_time
@@ -23,7 +23,7 @@ BUILT_SOURCES += sys/time.h
 # We need the following in order to create <sys/time.h> when the system
 # doesn't have one that works with the given compiler.
 sys/time.h: sys_time.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H) $(WARN_ON_USE_H)
-	$(AM_V_at)$(MKDIR_P) sys
+	$(AM_V_at)$(MKDIR_P) '%reldir%/sys'
 	$(AM_V_GEN)rm -f $@-t $@ && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
 	  sed -e 's|@''GUARD_PREFIX''@|${gl_include_guard_prefix}|g' \
diff --git a/modules/sys_times b/modules/sys_times
index 96c16e475f..704fc17cb7 100644
--- a/modules/sys_times
+++ b/modules/sys_times
@@ -21,7 +21,7 @@ BUILT_SOURCES += sys/times.h
 # We need the following in order to create <sys/times.h> when the system
 # doesn't have one that works with the given compiler.
 sys/times.h: sys_times.in.h $(top_builddir)/config.status $(WARN_ON_USE_H) $(ARG_NONNULL_H)
-	$(AM_V_at)$(MKDIR_P) sys
+	$(AM_V_at)$(MKDIR_P) '%reldir%/sys'
 	$(AM_V_GEN)rm -f $@-t $@ && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
 	  sed -e 's|@''GUARD_PREFIX''@|${gl_include_guard_prefix}|g' \
diff --git a/modules/sys_types b/modules/sys_types
index 38823d890d..e7d9511a6b 100644
--- a/modules/sys_types
+++ b/modules/sys_types
@@ -22,7 +22,7 @@ BUILT_SOURCES += sys/types.h
 # We need the following in order to create <sys/types.h> when the system
 # doesn't have one that works with the given compiler.
 sys/types.h: sys_types.in.h $(top_builddir)/config.status
-	$(AM_V_at)$(MKDIR_P) sys
+	$(AM_V_at)$(MKDIR_P) '%reldir%/sys'
 	$(AM_V_GEN)rm -f $@-t $@ && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
 	  sed -e 's|@''GUARD_PREFIX''@|${gl_include_guard_prefix}|g' \
diff --git a/modules/sys_uio b/modules/sys_uio
index 460e0ebebb..1cb23da97b 100644
--- a/modules/sys_uio
+++ b/modules/sys_uio
@@ -21,7 +21,7 @@ BUILT_SOURCES += sys/uio.h
 # We need the following in order to create <sys/uio.h> when the system
 # doesn't have one that works with the given compiler.
 sys/uio.h: sys_uio.in.h $(top_builddir)/config.status
-	$(AM_V_at)$(MKDIR_P) sys
+	$(AM_V_at)$(MKDIR_P) '%reldir%/sys'
 	$(AM_V_GEN)rm -f $@-t $@ && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
 	  sed -e 's|@''GUARD_PREFIX''@|${gl_include_guard_prefix}|g' \
diff --git a/modules/sys_utsname b/modules/sys_utsname
index 05f3b5cc8f..d3f70dbabf 100644
--- a/modules/sys_utsname
+++ b/modules/sys_utsname
@@ -21,7 +21,7 @@ BUILT_SOURCES += sys/utsname.h
 # We need the following in order to create <sys/utsname.h> when the system
 # does not have one.
 sys/utsname.h: sys_utsname.in.h $(top_builddir)/config.status $(WARN_ON_USE_H) $(ARG_NONNULL_H)
-	$(AM_V_at)$(MKDIR_P) sys
+	$(AM_V_at)$(MKDIR_P) '%reldir%/sys'
 	$(AM_V_GEN)rm -f $@-t $@ && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
 	  sed -e 's|@''GUARD_PREFIX''@|${gl_include_guard_prefix}|g' \
diff --git a/modules/sys_wait b/modules/sys_wait
index 0ae9b4f929..e46fbb9d9e 100644
--- a/modules/sys_wait
+++ b/modules/sys_wait
@@ -22,7 +22,7 @@ BUILT_SOURCES += sys/wait.h
 # We need the following in order to create <sys/wait.h> when the system
 # has one that is incomplete.
 sys/wait.h: sys_wait.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(WARN_ON_USE_H)
-	$(AM_V_at)$(MKDIR_P) sys
+	$(AM_V_at)$(MKDIR_P) '%reldir%/sys'
 	$(AM_V_GEN)rm -f $@-t $@ && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
 	  sed -e 's|@''GUARD_PREFIX''@|${gl_include_guard_prefix}|g' \
diff --git a/modules/sysexits b/modules/sysexits
index 64236a2ccc..715a2e55d9 100644
--- a/modules/sysexits
+++ b/modules/sysexits
@@ -11,6 +11,7 @@ stdlib
 
 configure.ac:
 gl_SYSEXITS
+AC_PROG_MKDIR_P
 
 Makefile.am:
 BUILT_SOURCES += $(SYSEXITS_H)
@@ -20,6 +21,7 @@ BUILT_SOURCES += $(SYSEXITS_H)
 if GL_GENERATE_SYSEXITS_H
 sysexits.h: sysexits.in.h $(top_builddir)/config.status
 	$(AM_V_GEN)rm -f $@-t $@ && \
+	$(MKDIR_P) '%reldir%' && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
 	  sed -e 's|@''GUARD_PREFIX''@|${gl_include_guard_prefix}|g' \
 	      -e 's|@''HAVE_SYSEXITS_H''@|$(HAVE_SYSEXITS_H)|g' \
diff --git a/modules/termios b/modules/termios
index 59cd4489ac..b1a6c5767e 100644
--- a/modules/termios
+++ b/modules/termios
@@ -14,6 +14,7 @@ snippet/warn-on-use
 configure.ac:
 gl_TERMIOS_H
 gl_TERMIOS_H_REQUIRE_DEFAULTS
+AC_PROG_MKDIR_P
 
 Makefile.am:
 BUILT_SOURCES += termios.h
@@ -22,6 +23,7 @@ BUILT_SOURCES += termios.h
 # version does not have all declarations.
 termios.h: termios.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(WARN_ON_USE_H)
 	$(AM_V_GEN)rm -f $@-t $@ && \
+	$(MKDIR_P) '%reldir%' && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
 	  sed -e 's|@''GUARD_PREFIX''@|${gl_include_guard_prefix}|g' \
 	      -e 's|@''INCLUDE_NEXT''@|$(INCLUDE_NEXT)|g' \
diff --git a/modules/threads-h b/modules/threads-h
index 0ea7a48c73..0c01f8d7a2 100644
--- a/modules/threads-h
+++ b/modules/threads-h
@@ -31,6 +31,7 @@ gl_ANYTHREADLIB_EARLY
 configure.ac:
 gl_THREADS_H
 gl_THREADS_H_REQUIRE_DEFAULTS
+AC_PROG_MKDIR_P
 
 Makefile.am:
 BUILT_SOURCES += threads.h
@@ -39,6 +40,7 @@ BUILT_SOURCES += threads.h
 # doesn't have one.
 threads.h: threads.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(_NORETURN_H) $(ARG_NONNULL_H) $(WARN_ON_USE_H)
 	$(AM_V_GEN)rm -f $@-t $@ && \
+	$(MKDIR_P) '%reldir%' && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */' && \
 	  sed -e 's|@''GUARD_PREFIX''@|${gl_include_guard_prefix}|g' \
 	      -e 's|@''HAVE_THREADS_H''@|$(HAVE_THREADS_H)|g' \
diff --git a/modules/time b/modules/time
index 236b5ea25e..2b5d9a3646 100644
--- a/modules/time
+++ b/modules/time
@@ -16,6 +16,7 @@ stddef
 configure.ac:
 gl_TIME_H
 gl_TIME_H_REQUIRE_DEFAULTS
+AC_PROG_MKDIR_P
 
 Makefile.am:
 BUILT_SOURCES += time.h
@@ -24,6 +25,7 @@ BUILT_SOURCES += time.h
 # doesn't have one that works with the given compiler.
 time.h: time.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H) $(WARN_ON_USE_H)
 	$(AM_V_GEN)rm -f $@-t $@ && \
+	$(MKDIR_P) '%reldir%' && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */' && \
 	  sed -e 's|@''GUARD_PREFIX''@|${gl_include_guard_prefix}|g' \
 	      -e 's|@''INCLUDE_NEXT''@|$(INCLUDE_NEXT)|g' \
diff --git a/modules/uchar b/modules/uchar
index e57ee9976d..1cf8b7a1ea 100644
--- a/modules/uchar
+++ b/modules/uchar
@@ -15,12 +15,14 @@ wchar
 configure.ac:
 gl_UCHAR_H
 gl_UCHAR_H_REQUIRE_DEFAULTS
+AC_PROG_MKDIR_P
 
 Makefile.am:
 BUILT_SOURCES += uchar.h
 
 uchar.h: uchar.in.h $(top_builddir)/config.status $(CXXDEFS_H)
 	$(AM_V_GEN)rm -f $@-t $@ && \
+	$(MKDIR_P) '%reldir%' && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
 	  sed -e 's|@''GUARD_PREFIX''@|${gl_include_guard_prefix}|g' \
 	      -e 's/@''HAVE_UCHAR_H''@/$(HAVE_UCHAR_H)/g' \
diff --git a/modules/unicase/base b/modules/unicase/base
index ef952e78a1..2dfea48b04 100644
--- a/modules/unicase/base
+++ b/modules/unicase/base
@@ -12,12 +12,14 @@ stdbool
 
 configure.ac:
 gl_LIBUNISTRING_LIBHEADER([0.9.11], [unicase.h])
+AC_PROG_MKDIR_P
 
 Makefile.am:
 BUILT_SOURCES += $(LIBUNISTRING_UNICASE_H)
 
 unicase.h: unicase.in.h
 	$(AM_V_GEN)rm -f $@-t $@ && \
+	$(MKDIR_P) '%reldir%' && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
 	  cat $(srcdir)/unicase.in.h; \
 	} > $@-t && \
diff --git a/modules/unicase/special-casing b/modules/unicase/special-casing
index bf2f7c59e2..352ac60fac 100644
--- a/modules/unicase/special-casing
+++ b/modules/unicase/special-casing
@@ -10,6 +10,7 @@ Depends-on:
 gperf
 
 configure.ac:
+AC_PROG_MKDIR_P
 
 Makefile.am:
 lib_SOURCES += unicase/special-casing.c
@@ -24,7 +25,7 @@ EXTRA_DIST           += unicase/special-casing-table.h
 
 # Generate special-casing.h with a declaration that depends on the gperf version.
 unicase/special-casing.h: unicase/special-casing.in.h unicase/special-casing-table.h
-	$(AM_V_at)$(MKDIR_P) unicase
+	$(AM_V_at)$(MKDIR_P) '%reldir%/unicase'
 	$(AM_V_GEN)rm -f $@-t $@ && \
 	declaration=`grep '^gl_unicase_special_lookup' $(srcdir)/unicase/special-casing-table.h | sed -e 's/register //g'` && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
diff --git a/modules/uniconv/base b/modules/uniconv/base
index c6c35fc89a..0d27e23c2d 100644
--- a/modules/uniconv/base
+++ b/modules/uniconv/base
@@ -13,12 +13,14 @@ unitypes
 
 configure.ac:
 gl_LIBUNISTRING_LIBHEADER([0.9.11], [uniconv.h])
+AC_PROG_MKDIR_P
 
 Makefile.am:
 BUILT_SOURCES += $(LIBUNISTRING_UNICONV_H)
 
 uniconv.h: uniconv.in.h
 	$(AM_V_GEN)rm -f $@-t $@ && \
+	$(MKDIR_P) '%reldir%' && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
 	  cat $(srcdir)/uniconv.in.h; \
 	} > $@-t && \
diff --git a/modules/unictype/base b/modules/unictype/base
index f6b80b8f96..25c16fa1a0 100644
--- a/modules/unictype/base
+++ b/modules/unictype/base
@@ -11,12 +11,14 @@ stdbool
 
 configure.ac:
 gl_LIBUNISTRING_LIBHEADER([0.9.8], [unictype.h])
+AC_PROG_MKDIR_P
 
 Makefile.am:
 BUILT_SOURCES += $(LIBUNISTRING_UNICTYPE_H)
 
 unictype.h: unictype.in.h
 	$(AM_V_GEN)rm -f $@-t $@ && \
+	$(MKDIR_P) '%reldir%' && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
 	  cat $(srcdir)/unictype.in.h; \
 	} > $@-t && \
diff --git a/modules/unigbrk/base b/modules/unigbrk/base
index 731d6a34ce..fa3f869d0b 100644
--- a/modules/unigbrk/base
+++ b/modules/unigbrk/base
@@ -11,12 +11,14 @@ stdbool
 
 configure.ac:
 gl_LIBUNISTRING_LIBHEADER([0.9.8], [unigbrk.h])
+AC_PROG_MKDIR_P
 
 Makefile.am:
 BUILT_SOURCES += $(LIBUNISTRING_UNIGBRK_H)
 
 unigbrk.h: unigbrk.in.h
 	$(AM_V_GEN)rm -f $@-t $@ && \
+	$(MKDIR_P) '%reldir%' && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
 	  cat $(srcdir)/unigbrk.in.h; \
 	} > $@-t && \
diff --git a/modules/unilbrk/base b/modules/unilbrk/base
index 0d29796a56..de3d29dcb1 100644
--- a/modules/unilbrk/base
+++ b/modules/unilbrk/base
@@ -11,12 +11,14 @@ localcharset
 
 configure.ac:
 gl_LIBUNISTRING_LIBHEADER([0.9.11], [unilbrk.h])
+AC_PROG_MKDIR_P
 
 Makefile.am:
 BUILT_SOURCES += $(LIBUNISTRING_UNILBRK_H)
 
 unilbrk.h: unilbrk.in.h
 	$(AM_V_GEN)rm -f $@-t $@ && \
+	$(MKDIR_P) '%reldir%' && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
 	  cat $(srcdir)/unilbrk.in.h; \
 	} > $@-t && \
diff --git a/modules/uniname/base b/modules/uniname/base
index 4edb924c43..c316cc4bbf 100644
--- a/modules/uniname/base
+++ b/modules/uniname/base
@@ -10,12 +10,14 @@ unitypes
 
 configure.ac:
 gl_LIBUNISTRING_LIBHEADER([0.9.5], [uniname.h])
+AC_PROG_MKDIR_P
 
 Makefile.am:
 BUILT_SOURCES += $(LIBUNISTRING_UNINAME_H)
 
 uniname.h: uniname.in.h
 	$(AM_V_GEN)rm -f $@-t $@ && \
+	$(MKDIR_P) '%reldir%' && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
 	  cat $(srcdir)/uniname.in.h; \
 	} > $@-t && \
diff --git a/modules/uninorm/base b/modules/uninorm/base
index 67bb3261da..edaf088f7e 100644
--- a/modules/uninorm/base
+++ b/modules/uninorm/base
@@ -10,12 +10,14 @@ unitypes
 
 configure.ac:
 gl_LIBUNISTRING_LIBHEADER([0.9.11], [uninorm.h])
+AC_PROG_MKDIR_P
 
 Makefile.am:
 BUILT_SOURCES += $(LIBUNISTRING_UNINORM_H)
 
 uninorm.h: uninorm.in.h
 	$(AM_V_GEN)rm -f $@-t $@ && \
+	$(MKDIR_P) '%reldir%' && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
 	  cat $(srcdir)/uninorm.in.h; \
 	} > $@-t && \
diff --git a/modules/unistd b/modules/unistd
index acc9d86024..fd81e2b827 100644
--- a/modules/unistd
+++ b/modules/unistd
@@ -21,6 +21,7 @@ sys_types
 configure.ac:
 gl_UNISTD_H
 gl_UNISTD_H_REQUIRE_DEFAULTS
+AC_PROG_MKDIR_P
 
 Makefile.am:
 BUILT_SOURCES += unistd.h
@@ -30,6 +31,7 @@ lib_SOURCES += unistd.c
 # <unistd.h> when the system doesn't have one.
 unistd.h: unistd.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H) $(WARN_ON_USE_H)
 	$(AM_V_GEN)rm -f $@-t $@ && \
+	$(MKDIR_P) '%reldir%' && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
 	  sed -e 's|@''GUARD_PREFIX''@|${gl_include_guard_prefix}|g' \
 	      -e 's|@''HAVE_UNISTD_H''@|$(HAVE_UNISTD_H)|g' \
diff --git a/modules/unistdio/base b/modules/unistdio/base
index 6acc7de096..9f2203bbb6 100644
--- a/modules/unistdio/base
+++ b/modules/unistdio/base
@@ -10,12 +10,14 @@ unitypes
 
 configure.ac:
 gl_LIBUNISTRING_LIBHEADER([0.9.11], [unistdio.h])
+AC_PROG_MKDIR_P
 
 Makefile.am:
 BUILT_SOURCES += $(LIBUNISTRING_UNISTDIO_H)
 
 unistdio.h: unistdio.in.h
 	$(AM_V_GEN)rm -f $@-t $@ && \
+	$(MKDIR_P) '%reldir%' && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
 	  cat $(srcdir)/unistdio.in.h; \
 	} > $@-t && \
diff --git a/modules/unistr/base b/modules/unistr/base
index 4f2fb3737f..e0f2113b70 100644
--- a/modules/unistr/base
+++ b/modules/unistr/base
@@ -12,12 +12,14 @@ inline
 
 configure.ac:
 gl_LIBUNISTRING_LIBHEADER([0.9.11], [unistr.h])
+AC_PROG_MKDIR_P
 
 Makefile.am:
 BUILT_SOURCES += $(LIBUNISTRING_UNISTR_H)
 
 unistr.h: unistr.in.h
 	$(AM_V_GEN)rm -f $@-t $@ && \
+	$(MKDIR_P) '%reldir%' && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
 	  cat $(srcdir)/unistr.in.h; \
 	} > $@-t && \
diff --git a/modules/unitypes b/modules/unitypes
index 46ed80d8c8..a8e4413140 100644
--- a/modules/unitypes
+++ b/modules/unitypes
@@ -10,6 +10,7 @@ stdint
 
 configure.ac:
 gl_LIBUNISTRING_LIBHEADER([0.9.11], [unitypes.h])
+AC_PROG_MKDIR_P
 
 AH_VERBATIM([unitypes_restrict], [
 /* This definition is a duplicate of the one in unitypes.h.
@@ -32,6 +33,7 @@ BUILT_SOURCES += $(LIBUNISTRING_UNITYPES_H)
 
 unitypes.h: unitypes.in.h
 	$(AM_V_GEN)rm -f $@-t $@ && \
+	$(MKDIR_P) '%reldir%' && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
 	  cat $(srcdir)/unitypes.in.h; \
 	} > $@-t && \
diff --git a/modules/uniwbrk/base b/modules/uniwbrk/base
index 3dc11b472d..f9a947bbf1 100644
--- a/modules/uniwbrk/base
+++ b/modules/uniwbrk/base
@@ -10,12 +10,14 @@ unitypes
 
 configure.ac:
 gl_LIBUNISTRING_LIBHEADER([0.9.11], [uniwbrk.h])
+AC_PROG_MKDIR_P
 
 Makefile.am:
 BUILT_SOURCES += $(LIBUNISTRING_UNIWBRK_H)
 
 uniwbrk.h: uniwbrk.in.h
 	$(AM_V_GEN)rm -f $@-t $@ && \
+	$(MKDIR_P) '%reldir%' && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
 	  cat $(srcdir)/uniwbrk.in.h; \
 	} > $@-t && \
diff --git a/modules/uniwidth/base b/modules/uniwidth/base
index fbf4d41f50..ac7167db8f 100644
--- a/modules/uniwidth/base
+++ b/modules/uniwidth/base
@@ -11,12 +11,14 @@ unitypes
 
 configure.ac:
 gl_LIBUNISTRING_LIBHEADER([0.9.11], [uniwidth.h])
+AC_PROG_MKDIR_P
 
 Makefile.am:
 BUILT_SOURCES += $(LIBUNISTRING_UNIWIDTH_H)
 
 uniwidth.h: uniwidth.in.h
 	$(AM_V_GEN)rm -f $@-t $@ && \
+	$(MKDIR_P) '%reldir%' && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
 	  cat $(srcdir)/uniwidth.in.h; \
 	} > $@-t && \
diff --git a/modules/utime-h b/modules/utime-h
index beb5159cb1..0b2a341192 100644
--- a/modules/utime-h
+++ b/modules/utime-h
@@ -14,6 +14,7 @@ snippet/warn-on-use
 configure.ac:
 gl_UTIME_H
 gl_UTIME_H_REQUIRE_DEFAULTS
+AC_PROG_MKDIR_P
 
 Makefile.am:
 BUILT_SOURCES += utime.h
@@ -22,6 +23,7 @@ BUILT_SOURCES += utime.h
 # doesn't have one that works with the given compiler.
 utime.h: utime.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H) $(WARN_ON_USE_H)
 	$(AM_V_GEN)rm -f $@-t $@ && \
+	$(MKDIR_P) '%reldir%' && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */' && \
 	  sed -e 's|@''GUARD_PREFIX''@|${gl_include_guard_prefix}|g' \
 	      -e 's/@''HAVE_UTIME_H''@/$(HAVE_UTIME_H)/g' \
diff --git a/modules/wchar b/modules/wchar
index becd7968a9..9c37594a53 100644
--- a/modules/wchar
+++ b/modules/wchar
@@ -19,6 +19,7 @@ stdlib
 configure.ac:
 gl_WCHAR_H
 gl_WCHAR_H_REQUIRE_DEFAULTS
+AC_PROG_MKDIR_P
 
 Makefile.am:
 BUILT_SOURCES += wchar.h
@@ -27,6 +28,7 @@ BUILT_SOURCES += wchar.h
 # version does not work standalone.
 wchar.h: wchar.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H) $(WARN_ON_USE_H)
 	$(AM_V_GEN)rm -f $@-t $@ && \
+	$(MKDIR_P) '%reldir%' && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
 	  sed -e 's|@''GUARD_PREFIX''@|${gl_include_guard_prefix}|g' \
 	      -e 's|@''INCLUDE_NEXT''@|$(INCLUDE_NEXT)|g' \
diff --git a/modules/wctype-h b/modules/wctype-h
index 24b5fe68fe..0993a7c3c1 100644
--- a/modules/wctype-h
+++ b/modules/wctype-h
@@ -16,6 +16,7 @@ snippet/warn-on-use
 configure.ac:
 gl_WCTYPE_H
 gl_WCTYPE_H_REQUIRE_DEFAULTS
+AC_PROG_MKDIR_P
 
 Makefile.am:
 BUILT_SOURCES += wctype.h
@@ -25,6 +26,7 @@ lib_SOURCES += wctype-h.c
 # doesn't have one that works with the given compiler.
 wctype.h: wctype.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(WARN_ON_USE_H)
 	$(AM_V_GEN)rm -f $@-t $@ && \
+	$(MKDIR_P) '%reldir%' && \
 	{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
 	  sed -e 's|@''GUARD_PREFIX''@|${gl_include_guard_prefix}|g' \
 	      -e 's/@''HAVE_WCTYPE_H''@/$(HAVE_WCTYPE_H)/g' \
-- 
2.39.5