[RFC] Replicate src dir in build dir

Message ID 1505832159-23038-1-git-send-email-yao.qi@linaro.org
State New, archived
Headers

Commit Message

Yao Qi Sept. 19, 2017, 2:42 p.m. UTC
  Nowadays, GDB build tree is almost flat, but source tree isn't.  We
have arch/ nat/ target/ common/ cli/ mi/ tui/ python/ guile/ directories.
We need to some rules in Makefile for source files in different source
directories, like,

 # Rules for compiling .c files in the various source subdirectories.
%.o: ${srcdir}/arch/%.c
	$(COMPILE) $<
	$(POSTCOMPILE)

%.o: ${srcdir}/nat/%.c
	$(COMPILE) $<
	$(POSTCOMPILE)

so we should take care of some special case that files' base name is the
same, like,

 # Specify an explicit rule for gdb/common/agent.c, to avoid a clash with the
 # object file generate by gdb/agent.c.
common-agent.o: $(srcdir)/common/agent.c
	$(COMPILE) $(srcdir)/common/agent.c
	$(POSTCOMPILE)

As we add more and more files in different directories, it becomes tricky
to name files, because we need take this into account.

This patch takes the first step toward "Replicate src dir in build dir",
that is, we create arch/ directory in buildtree, and put amd64.o there
as an example.  Dependency tracking is updated for files with directory
name.  Currently, when we build amd64.o,

  "-c -o amd64.o -MT amd64.o -MMD -MP -MF .deps/amd64.Tpo"

with this patch applied, it becomes,

  "-c -o arch/amd64.o -MT arch/amd64.o -MMD -MP -MF arch/.deps/amd64.o.Tpo"

"make clean" removes the object files, and "make distclean" removes .deps
additionally.  configure file create .deps directory in each of
CONFIG_SRC_SUBDIR, and pass it to Makefile.in, so that "make clean" and
"make distclean" can remove stuffs there.

If people agree with this change, I'll add more directories to
CONFIG_SRC_SUBDIR.  I want to do the same to GDBserver, but I haven't
looked at GDBserver configure/Makefile yet.

gdb:

2017-09-19  Yao Qi  <yao.qi@linaro.org>

	* Makefile.in (CONFIG_SRC_SUBDIR): New.
	(ALL_64_TARGET_OBS): Replace amd64.o with arch/amd64.o.
	(clean): Remove object files and dependency files.
	(distclean): Remove the directory.
	* configure.ac: Invoke AC_CONFIG_COMMANDS.
	* configure: Re-generated.
	* configure.tgt: Replace amd64.o with arch/amd64.o.
---
 gdb/Makefile.in   | 16 ++++++++++++----
 gdb/configure     | 15 +++++++++++++++
 gdb/configure.ac  | 11 +++++++++++
 gdb/configure.tgt | 24 ++++++++++++------------
 4 files changed, 50 insertions(+), 16 deletions(-)
  

Comments

Simon Marchi Sept. 19, 2017, 8:44 p.m. UTC | #1
On 2017-09-19 16:42, Yao Qi wrote:
> Nowadays, GDB build tree is almost flat, but source tree isn't.  We
> have arch/ nat/ target/ common/ cli/ mi/ tui/ python/ guile/ 
> directories.
> We need to some rules in Makefile for source files in different source
> directories, like,
> 
>  # Rules for compiling .c files in the various source subdirectories.
> %.o: ${srcdir}/arch/%.c
> 	$(COMPILE) $<
> 	$(POSTCOMPILE)
> 
> %.o: ${srcdir}/nat/%.c
> 	$(COMPILE) $<
> 	$(POSTCOMPILE)
> 
> so we should take care of some special case that files' base name is 
> the
> same, like,
> 
>  # Specify an explicit rule for gdb/common/agent.c, to avoid a clash 
> with the
>  # object file generate by gdb/agent.c.
> common-agent.o: $(srcdir)/common/agent.c
> 	$(COMPILE) $(srcdir)/common/agent.c
> 	$(POSTCOMPILE)
> 
> As we add more and more files in different directories, it becomes 
> tricky
> to name files, because we need take this into account.
> 
> This patch takes the first step toward "Replicate src dir in build 
> dir",
> that is, we create arch/ directory in buildtree, and put amd64.o there
> as an example.  Dependency tracking is updated for files with directory
> name.  Currently, when we build amd64.o,
> 
>   "-c -o amd64.o -MT amd64.o -MMD -MP -MF .deps/amd64.Tpo"
> 
> with this patch applied, it becomes,
> 
>   "-c -o arch/amd64.o -MT arch/amd64.o -MMD -MP -MF 
> arch/.deps/amd64.o.Tpo"
> 
> "make clean" removes the object files, and "make distclean" removes 
> .deps
> additionally.  configure file create .deps directory in each of
> CONFIG_SRC_SUBDIR, and pass it to Makefile.in, so that "make clean" and
> "make distclean" can remove stuffs there.
> 
> If people agree with this change, I'll add more directories to
> CONFIG_SRC_SUBDIR.  I want to do the same to GDBserver, but I haven't
> looked at GDBserver configure/Makefile yet.

Hi Yao,

I like the idea.  I tried to do that when I cleaned up the Makefiles 
some time ago, but since it wasn't obvious I let it go.

>  # This used to depend on c-exp.c m2-exp.c TAGS
>  # I believe this is wrong; the makefile standards for distclean just
> @@ -2317,6 +2322,9 @@ distclean: clean
>  	rm -f config.log config.cache
>  	rm -f Makefile
>  	rm -rf $(DEPDIR)
> +	@for i in $(CONFIG_SRC_SUBDIR); do \
> +		rm -rf $$i/$(DEPDIR); \
> +	done

I'm always a bit uncomfortable with putting some rm -rf commands in 
scripts, if we can avoid it.  Can we replace that with rmdir, since the 
clean target should have emptied that directory just before?

Also I wouldn't silence the commands (the @), it's always good to be 
able to read what's being executed.

> 
>  maintainer-clean: local-maintainer-clean do-maintainer-clean distclean
>  realclean: maintainer-clean
> @@ -2941,9 +2949,9 @@ ifeq ($(DEPMODE),depmode=gcc3)
>  # into place if the compile succeeds.  We need this because gcc does
>  # not atomically write the dependency output file.
>  override COMPILE.post = -c -o $@ -MT $@ -MMD -MP \
> -	-MF $(DEPDIR)/$(basename $(@F)).Tpo
> -override POSTCOMPILE = @mv $(DEPDIR)/$(basename $(@F)).Tpo \
> -	$(DEPDIR)/$(basename $(@F)).Po
> +	-MF $(@D)/$(DEPDIR)/$(@F).Tpo
> +override POSTCOMPILE = @mv $(@D)/$(DEPDIR)/$(@F).Tpo \
> +	$(@D)/$(DEPDIR)/$(@F).Po
>  else
>  override COMPILE.pre = source='$<' object='$@' libtool=no \
>  	DEPDIR=$(DEPDIR) $(DEPMODE) $(depcomp) $(CC)

I have never tested it, but I assume the "depcomp" mode of dependency 
management will have to be updated too.

Thanks!

Simon
  
Pedro Alves Sept. 20, 2017, 11:26 a.m. UTC | #2
On 09/19/2017 03:42 PM, Yao Qi wrote:
> Nowadays, GDB build tree is almost flat, but source tree isn't.  We
> have arch/ nat/ target/ common/ cli/ mi/ tui/ python/ guile/ directories.
> We need to some rules in Makefile for source files in different source
> directories, like,
> 
>  # Rules for compiling .c files in the various source subdirectories.
> %.o: ${srcdir}/arch/%.c
> 	$(COMPILE) $<
> 	$(POSTCOMPILE)
> 
> %.o: ${srcdir}/nat/%.c
> 	$(COMPILE) $<
> 	$(POSTCOMPILE)
> 
> so we should take care of some special case that files' base name is the
> same, like,
> 
>  # Specify an explicit rule for gdb/common/agent.c, to avoid a clash with the
>  # object file generate by gdb/agent.c.
> common-agent.o: $(srcdir)/common/agent.c
> 	$(COMPILE) $(srcdir)/common/agent.c
> 	$(POSTCOMPILE)
> 
> As we add more and more files in different directories, it becomes tricky
> to name files, because we need take this into account.
> 
> This patch takes the first step toward "Replicate src dir in build dir",
> that is, we create arch/ directory in buildtree, and put amd64.o there
> as an example.  Dependency tracking is updated for files with directory
> name.  Currently, when we build amd64.o,
> 
>   "-c -o amd64.o -MT amd64.o -MMD -MP -MF .deps/amd64.Tpo"
> 
> with this patch applied, it becomes,
> 
>   "-c -o arch/amd64.o -MT arch/amd64.o -MMD -MP -MF arch/.deps/amd64.o.Tpo"
> 
> "make clean" removes the object files, and "make distclean" removes .deps
> additionally.  configure file create .deps directory in each of
> CONFIG_SRC_SUBDIR, and pass it to Makefile.in, so that "make clean" and
> "make distclean" can remove stuffs there.
> 
> If people agree with this change, I'll add more directories to
> CONFIG_SRC_SUBDIR.  I want to do the same to GDBserver, but I haven't
> looked at GDBserver configure/Makefile yet.

For the record [since you already know this], I agree with the 
direction.  Thanks for doing this!

> @@ -2317,6 +2322,9 @@ distclean: clean
>  	rm -f config.log config.cache
>  	rm -f Makefile
>  	rm -rf $(DEPDIR)
> +	@for i in $(CONFIG_SRC_SUBDIR); do \
> +		rm -rf $$i/$(DEPDIR); \
> +	done

I agree with Simon; "rm -rf" is scary.

> +# Create sub-directories for objects and depedencies.

Typo: "depedencies" -> "dependencies".  Appears several times.

> --- a/gdb/configure.tgt
> +++ b/gdb/configure.tgt
> @@ -194,7 +194,7 @@ i[34567]86-*-darwin*)
>  			i386-darwin-tdep.o solib-darwin.o"
>  	if test "x$enable_64_bit_bfd" = "xyes"; then
>  	    # Target: GNU/Linux x86-64
> -	    gdb_target_obs="amd64-tdep.o amd64.o amd64-darwin-tdep.o ${gdb_target_obs}"
> +	    gdb_target_obs="amd64-tdep.o arch/amd64.o amd64-darwin-tdep.o ${gdb_target_obs}"
>  	fi
>  	;;
>  i[34567]86-*-dicos*)
> @@ -225,7 +225,7 @@ i[34567]86-*-nto*)
>  	;;
>  i[34567]86-*-solaris2.1[0-9]* | x86_64-*-solaris2.1[0-9]*)
>  	# Target: Solaris x86_64
> -	gdb_target_obs="i386-tdep.o i386.o i387-tdep.o amd64-tdep.o amd64.o \
> +	gdb_target_obs="i386-tdep.o i386.o i387-tdep.o amd64-tdep.o arch/amd64.o \
>  			 amd64-sol2-tdep.o i386-sol2-tdep.o sol2-tdep.o \
>  			 solib-svr4.o"


We've had to touch these lists several times recently to add some object to
a bunch of triplets.  It'd be nice to move the common CPU-specific files to
variables shared by the different OS triplets, so that we'd have simple places
to edit them.  Similar to srv_i386_linux_regobj etc. in gdbserver/configure.srv.

Like e.g.:

+ i386_tobjs="i386-tdep.o i386.o i387-tdep.o"
+ amd64_tobjs="${i386_tobjs} amd64-tdep.o arch/amd64.o"

And then use these variables throughout, like:

  x86_64-*-netbsd* | x86_64-*-knetbsd*-gnu)
  	# Target: NetBSD/amd64
 -	gdb_target_obs="amd64-tdep.o amd64.o amd64-nbsd-tdep.o i386-tdep.o \
 - 			i386.o i387-tdep.o nbsd-tdep.o solib-svr4.o"
 +      gdb_target_obs="${amd64_tobjs} amd64-nbsd-tdep.o nbsd-tdep.o solib-svr4.o"

Etc.

Thanks,
Pedro Alves
  
Yao Qi Sept. 20, 2017, 4:49 p.m. UTC | #3
Pedro Alves <palves@redhat.com> writes:

> We've had to touch these lists several times recently to add some object to
> a bunch of triplets.  It'd be nice to move the common CPU-specific files to
> variables shared by the different OS triplets, so that we'd have simple places
> to edit them.  Similar to srv_i386_linux_regobj etc. in
> gdbserver/configure.srv.

This is about common CPU-specific files shared by different triplets.
This makes think a little bit further, how do we handle common
OS-specific files shared by different triplets?  We can match triplet
CPU part and OS part respectively, and append the right objects to
gdb_target_obs, like this,

i386_tobjs="i386-tdep.o i386.o i387-tdep.o"
amd64_tobjs="amd64-tdep.o arch/amd64.o"
gdb_target_obs=""

# Fill in gdb_target_obs according to CPU.

case "${targ}" in
aarch64*-*-*)
        gdb_target_obs="aarch64-tdep.o aarch64-insn.o ${gdb_target_obs}";;
arm*-*-*)
        gdb_target_obs="arm.o arm-get-next-pcs.o arm-tdep.o ${gdb_target_obs}";;
i[34567]86-*-*)
        gdb_target_obs="${i386_tobjs} ${gdb_target_obs}";;
	if test "x$enable_64_bit_bfd" = "xyes"; then
	    gdb_target_obs="${amd64_tobjs} ${gdb_target_obs}"
	fi
sparc-*-*)
        gdb_target_obs="sparc-tdep.o ravenscar-thread.o sparc-ravenscar-thread.o ${gdb_target_obs}";;

sparc64-*-*)
        gdb_target_obs="sparc-tdep.o sparc64-tdep.o ravenscar-thread.o sparc-ravenscar-thread.o ${gdb_target_obs}";;

x86_64-*-*)
        gdb_target_obs="${i386_tobjs} ${amd64_tobjs} ${gdb_target_obs}";;
esac

# Fill in gdb_target_obs according to OS.

case "${targ}" in
*-*-freebsd*)
        gdb_target_obs="fbsd-tdep.o solib-svr4.o ${gdb_target_obs}";;
*-*-netbsd*)
        gdb_target_obs="nbsd-tdep.o solib-svr4.o ${gdb_target_obs}";;
*-*-openbsd*)
        gdb_target_obs="obsd-tdep.o solib-svr4.o ${gdb_target_obs}";;
esac

# Fill in the rest according to the triplet.
...

What do you think?

>
> Like e.g.:
>
> + i386_tobjs="i386-tdep.o i386.o i387-tdep.o"
> + amd64_tobjs="${i386_tobjs} amd64-tdep.o arch/amd64.o"
>
> And then use these variables throughout, like:
>
>   x86_64-*-netbsd* | x86_64-*-knetbsd*-gnu)
>   	# Target: NetBSD/amd64
>  -	gdb_target_obs="amd64-tdep.o amd64.o amd64-nbsd-tdep.o i386-tdep.o \
>  - 			i386.o i387-tdep.o nbsd-tdep.o solib-svr4.o"
>  +      gdb_target_obs="${amd64_tobjs} amd64-nbsd-tdep.o nbsd-tdep.o solib-svr4.o"
  
Pedro Alves Sept. 20, 2017, 5:44 p.m. UTC | #4
On 09/20/2017 05:49 PM, Yao Qi wrote:
> Pedro Alves <palves@redhat.com> writes:
> 
>> We've had to touch these lists several times recently to add some object to
>> a bunch of triplets.  It'd be nice to move the common CPU-specific files to
>> variables shared by the different OS triplets, so that we'd have simple places
>> to edit them.  Similar to srv_i386_linux_regobj etc. in
>> gdbserver/configure.srv.
> 
> This is about common CPU-specific files shared by different triplets.
> This makes think a little bit further, how do we handle common
> OS-specific files shared by different triplets?  We can match triplet
> CPU part and OS part respectively, and append the right objects to
> gdb_target_obs, like this,
> 
> i386_tobjs="i386-tdep.o i386.o i387-tdep.o"
> amd64_tobjs="amd64-tdep.o arch/amd64.o"
> gdb_target_obs=""
> 
> # Fill in gdb_target_obs according to CPU.
> 
> case "${targ}" in
> aarch64*-*-*)
>         gdb_target_obs="aarch64-tdep.o aarch64-insn.o ${gdb_target_obs}";;
> arm*-*-*)
>         gdb_target_obs="arm.o arm-get-next-pcs.o arm-tdep.o ${gdb_target_obs}";;
> i[34567]86-*-*)
>         gdb_target_obs="${i386_tobjs} ${gdb_target_obs}";;
> 	if test "x$enable_64_bit_bfd" = "xyes"; then
> 	    gdb_target_obs="${amd64_tobjs} ${gdb_target_obs}"
> 	fi
> sparc-*-*)
>         gdb_target_obs="sparc-tdep.o ravenscar-thread.o sparc-ravenscar-thread.o ${gdb_target_obs}";;
> 
> sparc64-*-*)
>         gdb_target_obs="sparc-tdep.o sparc64-tdep.o ravenscar-thread.o sparc-ravenscar-thread.o ${gdb_target_obs}";;
> 
> x86_64-*-*)
>         gdb_target_obs="${i386_tobjs} ${amd64_tobjs} ${gdb_target_obs}";;
> esac
> 
> # Fill in gdb_target_obs according to OS.
> 
> case "${targ}" in
> *-*-freebsd*)
>         gdb_target_obs="fbsd-tdep.o solib-svr4.o ${gdb_target_obs}";;
> *-*-netbsd*)
>         gdb_target_obs="nbsd-tdep.o solib-svr4.o ${gdb_target_obs}";;
> *-*-openbsd*)
>         gdb_target_obs="obsd-tdep.o solib-svr4.o ${gdb_target_obs}";;
> esac
> 
> # Fill in the rest according to the triplet.
> ...
> 
> What do you think?

Yes, that works for me too.

Meeting in the middle, using separate variables for arch and OS
objects would allow getting rid of the need for spelling out
gdb_target_obs twice per line, like:

 case "${targ}" in
 aarch64*-*-*)
         cpu_obs="aarch64-tdep.o aarch64-insn.o";;
 arm*-*-*)
         cpu_obs="arm.o arm-get-next-pcs.o arm-tdep.o";;
 esac

 case "${targ}" in
 *-*-freebsd*)
         os_obs="fbsd-tdep.o solib-svr4.o";;
 *-*-netbsd*)
         os_obs="nbsd-tdep.o solib-svr4.o";;
 *-*-openbsd*)
         os_obs="obsd-tdep.o solib-svr4.o";;
 esac
 
 gdb_target_obs="${cpu_obs} ${os_obs}"

Thanks,
Pedro Alves
  
Tom Tromey Oct. 8, 2017, 3:24 a.m. UTC | #5
>>>>> "Yao" == Yao Qi <qiyaoltc@gmail.com> writes:

Yao> This patch takes the first step toward "Replicate src dir in build dir",
Yao> that is, we create arch/ directory in buildtree, and put amd64.o there
Yao> as an example.

I think this is very nice.  Thanks for doing it.

But, there are a couple of bugs; one serious, one probably not.

Yao>  override COMPILE.post = -c -o $@ -MT $@ -MMD -MP \
Yao> -	-MF $(DEPDIR)/$(basename $(@F)).Tpo
Yao> -override POSTCOMPILE = @mv $(DEPDIR)/$(basename $(@F)).Tpo \
Yao> -	$(DEPDIR)/$(basename $(@F)).Po
Yao> +	-MF $(@D)/$(DEPDIR)/$(@F).Tpo
Yao> +override POSTCOMPILE = @mv $(@D)/$(DEPDIR)/$(@F).Tpo \
Yao> +	$(@D)/$(DEPDIR)/$(@F).Po
Yao>  else
Yao>  override COMPILE.pre = source='$<' object='$@' libtool=no \
Yao>  	DEPDIR=$(DEPDIR) $(DEPMODE) $(depcomp) $(CC)

This hunk removed the $(basename ...) wrapper.  This renames the
dependency files from "basename.Tpo" to "basename.o.Tpo".

However, it didn't change the dependencies include near the end of the
Makefile:

    -include $(patsubst %.o, $(DEPDIR)/%.Po, $(all_object_files))

So, because the names differ, automatic dependency tracking no longer
works.  I was really puzzled today when my rebuilds were so fast! :-)

I think the correct solution is to add the $(basename ...) back.  I
thought at first that the "-include" line could be fixed, but this won't
work in case where "depcomp" is used -- because the name choice also has
to coincide with what depcomp does.

Finally, I think the arch/ change might not work properly with depcomp.
The COMPILE.pre override (quoted in the context of the patch hunk above)
probably needs a tweak to DEPDIR, I would guess DEPDIR=$(@D)/$(DEPDIR).
However, I didn't try this case.  (I think when I wrote this I tested
the depcomp case by hacking the generated Makefile to ensure that the
depcomp code was used; otherwise you need a non-capable compiler.)

Tom
  
Yao Qi Oct. 8, 2017, 9:04 p.m. UTC | #6
On Sun, Oct 8, 2017 at 4:24 AM, Tom Tromey <tom@tromey.com> wrote:
>>>>>> "Yao" == Yao Qi <qiyaoltc@gmail.com> writes:
>
> Yao> This patch takes the first step toward "Replicate src dir in build dir",
> Yao> that is, we create arch/ directory in buildtree, and put amd64.o there
> Yao> as an example.
>
> I think this is very nice.  Thanks for doing it.
>
> But, there are a couple of bugs; one serious, one probably not.
>

Hi Tom,
Thanks for pointing them out.  I will look at them after
I am back to office (13th Oct).

> Yao>  override COMPILE.post = -c -o $@ -MT $@ -MMD -MP \
> Yao> -  -MF $(DEPDIR)/$(basename $(@F)).Tpo
> Yao> -override POSTCOMPILE = @mv $(DEPDIR)/$(basename $(@F)).Tpo \
> Yao> -  $(DEPDIR)/$(basename $(@F)).Po
> Yao> +  -MF $(@D)/$(DEPDIR)/$(@F).Tpo
> Yao> +override POSTCOMPILE = @mv $(@D)/$(DEPDIR)/$(@F).Tpo \
> Yao> +  $(@D)/$(DEPDIR)/$(@F).Po
> Yao>  else
> Yao>  override COMPILE.pre = source='$<' object='$@' libtool=no \
> Yao>    DEPDIR=$(DEPDIR) $(DEPMODE) $(depcomp) $(CC)
>
> This hunk removed the $(basename ...) wrapper.  This renames the
> dependency files from "basename.Tpo" to "basename.o.Tpo".
>
> However, it didn't change the dependencies include near the end of the
> Makefile:
>
>     -include $(patsubst %.o, $(DEPDIR)/%.Po, $(all_object_files))
>
> So, because the names differ, automatic dependency tracking no longer
> works.  I was really puzzled today when my rebuilds were so fast! :-)
>
> I think the correct solution is to add the $(basename ...) back.  I
> thought at first that the "-include" line could be fixed, but this won't
> work in case where "depcomp" is used -- because the name choice also has
> to coincide with what depcomp does.
>
> Finally, I think the arch/ change might not work properly with depcomp.
> The COMPILE.pre override (quoted in the context of the patch hunk above)
> probably needs a tweak to DEPDIR, I would guess DEPDIR=$(@D)/$(DEPDIR).
> However, I didn't try this case.  (I think when I wrote this I tested
> the depcomp case by hacking the generated Makefile to ensure that the
> depcomp code was used; otherwise you need a non-capable compiler.)
>
> Tom
  

Patch

diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index 1438f20..a660a27 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -632,6 +632,7 @@  CONFIG_ALL = @CONFIG_ALL@
 CONFIG_CLEAN = @CONFIG_CLEAN@
 CONFIG_INSTALL = @CONFIG_INSTALL@
 CONFIG_UNINSTALL = @CONFIG_UNINSTALL@
+CONFIG_SRC_SUBDIR = @CONFIG_SRC_SUBDIR@
 HAVE_NATIVE_GCORE_TARGET = @HAVE_NATIVE_GCORE_TARGET@
 
 # -I. for config files.
@@ -767,7 +768,6 @@  ALL_64_TARGET_OBS = \
 	alpha-nbsd-tdep.o \
 	alpha-obsd-tdep.o \
 	alpha-tdep.o \
-	amd64.o \
 	amd64-darwin-tdep.o \
 	amd64-dicos-tdep.o \
 	amd64-fbsd-tdep.o \
@@ -777,6 +777,7 @@  ALL_64_TARGET_OBS = \
 	amd64-sol2-tdep.o \
 	amd64-tdep.o \
 	amd64-windows-tdep.o \
+	arch/amd64.o \
 	ia64-linux-tdep.o \
 	ia64-tdep.o \
 	ia64-vms-tdep.o \
@@ -2299,6 +2300,10 @@  clean mostlyclean: $(CONFIG_CLEAN)
 	rm -f test-cp-name-parser$(EXEEXT)
 	rm -f xml-builtin.c stamp-xml
 	rm -f $(DEPDIR)/*
+	@for i in $(CONFIG_SRC_SUBDIR); do \
+		rm -f $$i/*.o;	\
+		rm -f $$i/$(DEPDIR)/*; \
+	done
 
 # This used to depend on c-exp.c m2-exp.c TAGS
 # I believe this is wrong; the makefile standards for distclean just
@@ -2317,6 +2322,9 @@  distclean: clean
 	rm -f config.log config.cache
 	rm -f Makefile
 	rm -rf $(DEPDIR)
+	@for i in $(CONFIG_SRC_SUBDIR); do \
+		rm -rf $$i/$(DEPDIR); \
+	done
 
 maintainer-clean: local-maintainer-clean do-maintainer-clean distclean
 realclean: maintainer-clean
@@ -2941,9 +2949,9 @@  ifeq ($(DEPMODE),depmode=gcc3)
 # into place if the compile succeeds.  We need this because gcc does
 # not atomically write the dependency output file.
 override COMPILE.post = -c -o $@ -MT $@ -MMD -MP \
-	-MF $(DEPDIR)/$(basename $(@F)).Tpo
-override POSTCOMPILE = @mv $(DEPDIR)/$(basename $(@F)).Tpo \
-	$(DEPDIR)/$(basename $(@F)).Po
+	-MF $(@D)/$(DEPDIR)/$(@F).Tpo
+override POSTCOMPILE = @mv $(@D)/$(DEPDIR)/$(@F).Tpo \
+	$(@D)/$(DEPDIR)/$(@F).Po
 else
 override COMPILE.pre = source='$<' object='$@' libtool=no \
 	DEPDIR=$(DEPDIR) $(DEPMODE) $(depcomp) $(CC)
diff --git a/gdb/configure b/gdb/configure
index c7746fc..41b884b 100755
--- a/gdb/configure
+++ b/gdb/configure
@@ -733,6 +733,7 @@  LIBINTL_DEP
 LIBINTL
 USE_NLS
 CCDEPMODE
+CONFIG_SRC_SUBDIR
 DEPDIR
 am__leading_dot
 CXX_DIALECT
@@ -5949,6 +5950,13 @@  DEPDIR="${am__leading_dot}deps"
 ac_config_commands="$ac_config_commands depdir"
 
 
+# Create sub-directories for objects and depedencies.
+CONFIG_SRC_SUBDIR="arch"
+
+
+ac_config_commands="$ac_config_commands gdbdepdir"
+
+
 depcc="$CC"   am_compiler_list=
 
 am_depcomp=$ac_aux_dir/depcomp
@@ -18171,6 +18179,7 @@  cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1
 # INIT-COMMANDS
 #
 ac_aux_dir=$ac_aux_dir DEPDIR=$DEPDIR
+ac_aux_dir=$ac_aux_dir DEPDIR=$DEPDIR CONFIG_SRC_SUBDIR="$CONFIG_SRC_SUBDIR"
 
 _ACEOF
 
@@ -18182,6 +18191,7 @@  do
   case $ac_config_target in
     "config.h") CONFIG_HEADERS="$CONFIG_HEADERS config.h:config.in" ;;
     "depdir") CONFIG_COMMANDS="$CONFIG_COMMANDS depdir" ;;
+    "gdbdepdir") CONFIG_COMMANDS="$CONFIG_COMMANDS gdbdepdir" ;;
     "jit-reader.h") CONFIG_FILES="$CONFIG_FILES jit-reader.h:jit-reader.in" ;;
     "$ac_config_links_1") CONFIG_LINKS="$CONFIG_LINKS $ac_config_links_1" ;;
     "gcore") CONFIG_FILES="$CONFIG_FILES gcore" ;;
@@ -18805,6 +18815,11 @@  $as_echo "$as_me: executing $ac_file commands" >&6;}
 
   case $ac_file$ac_mode in
     "depdir":C) $SHELL $ac_aux_dir/mkinstalldirs $DEPDIR ;;
+    "gdbdepdir":C)
+  for subdir in ${CONFIG_SRC_SUBDIR}
+  do
+      $SHELL $ac_aux_dir/mkinstalldirs $subdir/$DEPDIR
+  done ;;
     "gcore":F) chmod +x gcore ;;
     "Makefile":F)
 case x$CONFIG_HEADERS in
diff --git a/gdb/configure.ac b/gdb/configure.ac
index b4d7a87..081c4c9 100644
--- a/gdb/configure.ac
+++ b/gdb/configure.ac
@@ -44,6 +44,17 @@  AX_CXX_COMPILE_STDCXX(11, , mandatory)
 
 # Dependency checking.
 ZW_CREATE_DEPDIR
+# Create sub-directories for objects and depedencies.
+CONFIG_SRC_SUBDIR="arch"
+AC_SUBST(CONFIG_SRC_SUBDIR)
+
+AC_CONFIG_COMMANDS([gdbdepdir],[
+  for subdir in ${CONFIG_SRC_SUBDIR}
+  do
+      $SHELL $ac_aux_dir/mkinstalldirs $subdir/$DEPDIR
+  done],
+  [ac_aux_dir=$ac_aux_dir DEPDIR=$DEPDIR CONFIG_SRC_SUBDIR="$CONFIG_SRC_SUBDIR"])
+
 ZW_PROG_COMPILER_DEPENDENCIES([CC])
 
 gnulib_extra_configure_args=
diff --git a/gdb/configure.tgt b/gdb/configure.tgt
index 321a739..a7d89c7 100644
--- a/gdb/configure.tgt
+++ b/gdb/configure.tgt
@@ -194,7 +194,7 @@  i[34567]86-*-darwin*)
 			i386-darwin-tdep.o solib-darwin.o"
 	if test "x$enable_64_bit_bfd" = "xyes"; then
 	    # Target: GNU/Linux x86-64
-	    gdb_target_obs="amd64-tdep.o amd64.o amd64-darwin-tdep.o ${gdb_target_obs}"
+	    gdb_target_obs="amd64-tdep.o arch/amd64.o amd64-darwin-tdep.o ${gdb_target_obs}"
 	fi
 	;;
 i[34567]86-*-dicos*)
@@ -225,7 +225,7 @@  i[34567]86-*-nto*)
 	;;
 i[34567]86-*-solaris2.1[0-9]* | x86_64-*-solaris2.1[0-9]*)
 	# Target: Solaris x86_64
-	gdb_target_obs="i386-tdep.o i386.o i387-tdep.o amd64-tdep.o amd64.o \
+	gdb_target_obs="i386-tdep.o i386.o i387-tdep.o amd64-tdep.o arch/amd64.o \
 			 amd64-sol2-tdep.o i386-sol2-tdep.o sol2-tdep.o \
 			 solib-svr4.o"
 	;;
@@ -242,7 +242,7 @@  i[34567]86-*-linux*)
 			linux-tdep.o linux-record.o"
 	if test "x$enable_64_bit_bfd" = "xyes"; then
 	    # Target: GNU/Linux x86-64
-	    gdb_target_obs="amd64-tdep.o amd64.o amd64-linux-tdep.o ${gdb_target_obs}"
+	    gdb_target_obs="amd64-tdep.o arch/amd64.o amd64-linux-tdep.o ${gdb_target_obs}"
 	fi
 	build_gdbserver=yes
 	;;
@@ -671,52 +671,52 @@  vax-*-*)
 
 x86_64-*-darwin*)
 	# Target: Darwin/x86-64
-	gdb_target_obs="amd64-tdep.o amd64.o i386-tdep.o i386.o i387-tdep.o \
+	gdb_target_obs="amd64-tdep.o arch/amd64.o i386-tdep.o i386.o i387-tdep.o \
 			i386-darwin-tdep.o amd64-darwin-tdep.o \
                         solib-darwin.o"
 	;;
 
 x86_64-*-dicos*)
 	# Target: DICOS/x86-64
-	gdb_target_obs="amd64-tdep.o amd64.o i386-tdep.o i386.o i387-tdep.o \
+	gdb_target_obs="amd64-tdep.o arch/amd64.o i386-tdep.o i386.o i387-tdep.o \
 			dicos-tdep.o i386-dicos-tdep.o amd64-dicos-tdep.o"
 	;;
 x86_64-*-elf*)
-	gdb_target_obs="amd64-tdep.o amd64.o i386-tdep.o i386.o i387-tdep.o"
+	gdb_target_obs="amd64-tdep.o arch/amd64.o i386-tdep.o i386.o i387-tdep.o"
 	;;
 x86_64-*-linux*)
 	# Target: GNU/Linux x86-64
-	gdb_target_obs="amd64-tdep.o amd64-linux-tdep.o amd64.o i386-tdep.o \
+	gdb_target_obs="amd64-tdep.o amd64-linux-tdep.o arch/amd64.o i386-tdep.o \
 			i387-tdep.o i386.o i386-linux-tdep.o glibc-tdep.o \
 			solib-svr4.o symfile-mem.o linux-tdep.o linux-record.o"
 	build_gdbserver=yes
 	;;
 x86_64-*-freebsd* | x86_64-*-kfreebsd*-gnu)
 	# Target: FreeBSD/amd64
-	gdb_target_obs="amd64-tdep.o amd64.o amd64-fbsd-tdep.o i386-tdep.o \
+	gdb_target_obs="amd64-tdep.o arch/amd64.o amd64-fbsd-tdep.o i386-tdep.o \
 			i386.o i387-tdep.o i386-bsd-tdep.o i386-fbsd-tdep.o \
 			fbsd-tdep.o solib-svr4.o"
 	;;
 x86_64-*-mingw* | x86_64-*-cygwin*)
         # Target: MingW/amd64
-	gdb_target_obs="amd64-tdep.o amd64.o amd64-windows-tdep.o \
+	gdb_target_obs="amd64-tdep.o arch/amd64.o amd64-windows-tdep.o \
                         i386-tdep.o i386.o i386-cygwin-tdep.o i387-tdep.o \
                         windows-tdep.o"
 	build_gdbserver=yes
         ;;
 x86_64-*-netbsd* | x86_64-*-knetbsd*-gnu)
 	# Target: NetBSD/amd64
-	gdb_target_obs="amd64-tdep.o amd64.o amd64-nbsd-tdep.o i386-tdep.o \
+	gdb_target_obs="amd64-tdep.o arch/amd64.o amd64-nbsd-tdep.o i386-tdep.o \
 			i386.o i387-tdep.o nbsd-tdep.o solib-svr4.o"
 	;;
 x86_64-*-openbsd*)
 	# Target: OpenBSD/amd64
-	gdb_target_obs="amd64-tdep.o amd64.o amd64-obsd-tdep.o i386-tdep.o \
+	gdb_target_obs="amd64-tdep.o arch/amd64.o amd64-obsd-tdep.o i386-tdep.o \
 			i387-tdep.o i386-bsd-tdep.o i386-obsd-tdep.o \
 			i386.o obsd-tdep.o bsd-uthread.o solib-svr4.o"
 	;;
 x86_64-*-rtems*)
-	gdb_target_obs="amd64-tdep.o amd64.o i386-tdep.o i386.o i387-tdep.o \
+	gdb_target_obs="amd64-tdep.o arch/amd64.o i386-tdep.o i386.o i387-tdep.o \
 			i386-bsd-tdep.o"
 	;;
 xtensa*-*-linux*)	gdb_target=linux