This option is intended to be used in conjunction with subdir-objects and
Automake-time substitutions for included makefile fragments (%C%, %D%).
Enabling the option shortens the file name of object files such that the prefix
derived (after canonicalization) from the path is not included.

Enabling the option is basically equivalent to setting foo_SHORTNAME =
foo. However, it also works flawlessly if a Makefile fragment is
conditionally included. Note that actually setting foo_SHORTNAME
still overrides the object name, regardless of this option. This can improve
the modularity of Automake-using projects.

Example:
without object-shortname
  sub/Makefile.am:
  bin_PROGRAMS += %D%/foo
  %C%_foo_CFLAGS = $(AM_CFLAGS) -g

results in objects:
  sub/sub_foo-foo.o

with object-shortname the object file name is:
  sub/foo-foo.o

And it allows the following in $(top_srcdir)/Makefile.am (not possible with foo_SHORTNAME=foo)
  if ENABLE_SUB
  include sub/Makefile.am
  endif
>From f5704dffe09c600ae7c90069ef9f6681c8047f49 Mon Sep 17 00:00:00 2001
From: Thomas Martitz <ku...@rockbox.org>
Date: Wed, 6 Jan 2016 10:02:43 +0100
Subject: [PATCH] new option: object-shortname

This option is intended to be used in conjunction with subdir-objects and
Automake-time substitutions for included makefile fragments (%C%, %D%).
Enabling the option shortens the file name of object files such that the prefix
derived (after canonicalization) from the path is not included.

Enabling the option is basically equivalent to setting foo_SHORTNAME =
foo. However, it also works flawlessly if a Makefile fragment is
conditionally included. Note that actually setting foo_SHORTNAME
still overrides the object name, regardless of this option. This can improve
the modularity of Automake-using projects.

Example:
without object-shortname
  sub/Makefile.am:
  bin_PROGRAMS += %D%/foo
  %C%_foo_CFLAGS = $(AM_CFLAGS) -g

results in objects:
  sub/sub_foo-foo.o

with object-shortname the object file name is:
  sub/foo-foo.o

And it allows the following in $(top_srcdir)/Makefile.am (not possible with foo_SHORTNAME=foo)
  if ENABLE_SUB
  include sub/Makefile.am
  endif
---
 NEWS                    |  8 ++++++++
 bin/automake.in         | 19 ++++++++++++++++++-
 doc/automake.texi       | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 lib/Automake/Options.pm |  1 +
 4 files changed, 74 insertions(+), 1 deletion(-)

diff --git a/NEWS b/NEWS
index af904d4..1556389 100644
--- a/NEWS
+++ b/NEWS
@@ -64,6 +64,14 @@
 
 New in 1.16:
 
+* New option object-shortname
+
+  - This option affects the file name Automake uses for object files. Enabling
+    gives shorter, path-independent default object file names.
+
+  - It does not affect the directory where these object files will be placed.
+    Thus, it is recommended to combine this option with subdir-objects.
+
 * Bugs fixed:
 
   - Automatic dependency tracking has been fixed to work also when the
diff --git a/bin/automake.in b/bin/automake.in
index 09a1c95..b5d3aca 100644
--- a/bin/automake.in
+++ b/bin/automake.in
@@ -1711,7 +1711,24 @@ sub handle_single_transform
 		# some situations.  So we provide _SHORTNAME to
 		# override.
 
-		my $dname = $derived;
+    my $dname = $derived;
+    if (option 'object-shortname')
+      {
+        # If object-shortname is enabled the object's filename shall not contain
+        # the parts derived from its path (e.g. if %C% is used), but just the
+        # name of the object's target e.g. instead of path_to_binary-object.o
+        # just binary-object
+        my $dirname = dirname ($_file);
+        if ($dirname ne ".")
+          {
+            my $canon_dirname = canonicalize ($dirname) . "_";
+            # should never fail since $derived is also
+            # constructed using canonicalize()
+            prog_error("unexpected object name: " . $derived)
+              if (index ($derived, $canon_dirname) != 0);
+            $dname = substr ($derived, length ($canon_dirname));
+          }
+      }
 		my $var = var ($derived . '_SHORTNAME');
 		if ($var)
 		{
diff --git a/doc/automake.texi b/doc/automake.texi
index e46212f..e15ad48 100644
--- a/doc/automake.texi
+++ b/doc/automake.texi
@@ -10286,6 +10286,53 @@ the source file.  For instance, if the source file is
 @file{subdir/file.cxx}, then the output file would be
 @file{subdir/file.o}.
 
+@item @option{object-shortname}
+@cindex Options, @option{object-shortname}
+@opindex object-shortname
+If this option is specified, then object names constructed by Automake are
+shortened such that they are prefixed only by the target variable they build,
+therefore omitting the canonicalized path (@pxref{Canonicalization}).  The
+effect is particularly visible if you use @file{Makefile} fragment inclusion
+feature (@pxref{Include}).  Here, object names for files prefixed by @code{%D%}
+would be named @samp{path_to_foo-object.o}.  With the this option the object
+name becomes just @samp{foo-object.o}.
+
+Enabling the option is basically equivalent to setting @code{foo_SHORTNAME =
+foo}.  However, it also works if a Makefile fragment is conditionally included.
+Note that actually setting @code{foo_SHORTNAME} still overrides the object
+name, regardless of this option.
+
+It is recommended to combine this option and @option{subdir-objects} because
+the object names become independent of the path to the source files. This
+increases the possibility of object file name conflicts if only a single output
+directory is used.
+
+The rationale for this option is to allow a setup where there is a top-level
+@file{Makefile.am} which includes fragments from subdirectories, and a Makefile
+is generated in each directory.
+
+@example
+Makefile.am:
+@code{if ENABLE_SUB}
+@code{include sub/Makefile.am}
+@code{endif}
+
+sub/Makefile.am:
+@code{bin_PROGRAMS = %D%/foo}
+@code{%C%_foo_CFLAGS = $(AM_CFLAGS) -g}
+@end example
+
+Provided @option{subdir-objects} is enabled, executing @command{make} in either
+directory will build @file{foo} using the same object files.  Without this
+option, the object file names would be different (@file{sub_foo-foo.o} and
+@file{foo-foo.o}), resulting in extraneous rebuilds and duplicated object
+files.  Without subdir-objects the object file names are identical but since
+they are placed in different directories rebuilds still occur.
+
+The benefit of this setup is that executing @command{make} in the top-level
+directory does not use recursive make, which is usually faster and brings
+global knowledge about dependencies (@pxref{Subdirectories}).
+
 @anchor{tar-formats}
 @item @option{tar-v7}
 @itemx @option{tar-ustar}
diff --git a/lib/Automake/Options.pm b/lib/Automake/Options.pm
index a7f271c..03f2123 100644
--- a/lib/Automake/Options.pm
+++ b/lib/Automake/Options.pm
@@ -293,6 +293,7 @@ sub _is_valid_easy_option ($)
     silent-rules
     std-options
     subdir-objects
+    object-shortname
   );
 }
 
-- 
2.9.3


Reply via email to