Eric Blake asks: > > The makefile can also construct a newline as contents of a shell variable. > > In Makefile syntax: > > > > stmt=`echo "nl='"; echo "'"`; eval "$$stmt"; : | sed -e "1{$${nl}}" > > Is this trick worth documenting in the portable Makefile section of > autoconf? I think so, yes. If I had known about this trick earlier, there would not be a file 'remove-potcdate.sin' that is installed by gettextize. I'm using this trick in gettext's intl/Makefile.in too. Here is a patch to do this, together with an (unrelated, separate) patch to document how to write comments in Makefile rules. Does this submission format work? (I don't know whether git-merge-changelog is active when you do "git am".) If not, please use "patch -p1". From 12fc22759ba2a1dc5f22a938a639913a554297cc Mon Sep 17 00:00:00 2001 From: Bruno Haible <bruno@xxxxxxxxx> Date: Sat, 19 Jun 2010 00:06:37 +0200 Subject: [PATCH 1/2] Document how to write comments in makefile rules. --- ChangeLog | 6 ++++++ doc/autoconf.texi | 8 ++++++++ 2 files changed, 14 insertions(+), 0 deletions(-) diff --git a/ChangeLog b/ChangeLog index 52a7bb1..2dbf4ca 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,11 @@ 2010-06-18 Bruno Haible <bruno@xxxxxxxxx> + Document how to write comments in makefile rules. + * doc/autoconf.texi (Comments in Make Rules): Mention a workaround + syntax. + +2010-06-18 Bruno Haible <bruno@xxxxxxxxx> + Document sed -e limitation. * doc/autoconf.texi (Limitations of Usual Tools): Mention portability problem of sed -e option with script fragments. diff --git a/doc/autoconf.texi b/doc/autoconf.texi index eba61f4..90457f1 100644 --- a/doc/autoconf.texi +++ b/doc/autoconf.texi @@ -18937,6 +18937,14 @@ all: # foo @end example +As a workaround, you can use the @command{:} no-op command with a string +argument that gets ignored: + +@example +all: + : "foo" +@end example + @node obj/ and Make @section The @file{obj/} Subdirectory and Make @cindex @file{obj/}, subdirectory -- 1.6.3.2 From f5ef9e544cc65d80442d79bf7b485fed16c4ef9b Mon Sep 17 00:00:00 2001 From: Bruno Haible <bruno@xxxxxxxxx> Date: Sat, 19 Jun 2010 00:49:48 +0200 Subject: [PATCH 2/2] Document how to use literal newlines in makefile rules. --- ChangeLog | 5 +++++ doc/autoconf.texi | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 0 deletions(-) diff --git a/ChangeLog b/ChangeLog index 2dbf4ca..65c225c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,10 @@ 2010-06-18 Bruno Haible <bruno@xxxxxxxxx> + Document how to use literal newlines in makefile rules. + * doc/autoconf.texi (Newlines in Make Rules): New section. + +2010-06-18 Bruno Haible <bruno@xxxxxxxxx> + Document how to write comments in makefile rules. * doc/autoconf.texi (Comments in Make Rules): Mention a workaround syntax. diff --git a/doc/autoconf.texi b/doc/autoconf.texi index 90457f1..9b5bc4b 100644 --- a/doc/autoconf.texi +++ b/doc/autoconf.texi @@ -529,6 +529,7 @@ Portable Make Programming * The Make Macro SHELL:: @code{$(SHELL)} portability issues * Parallel Make:: Parallel @command{make} quirks * Comments in Make Rules:: Other problems with Make comments +* Newlines in Make Rules:: Using literal newlines in rules * obj/ and Make:: Don't name a subdirectory @file{obj} * make -k Status:: Exit status of @samp{make -k} * VPATH and Make:: @code{VPATH} woes @@ -18546,6 +18547,7 @@ itself. * The Make Macro SHELL:: @code{$(SHELL)} portability issues * Parallel Make:: Parallel @command{make} quirks * Comments in Make Rules:: Other problems with Make comments +* Newlines in Make Rules:: Using literal newlines in rules * obj/ and Make:: Don't name a subdirectory @file{obj} * make -k Status:: Exit status of @samp{make -k} * VPATH and Make:: @code{VPATH} woes @@ -18945,6 +18947,43 @@ all: : "foo" @end example +@node Newlines in Make Rules +@section Newlines in Make Rules +@cindex Newlines in @file{Makefile} rules +@cindex @file{Makefile} rules and newlines + +In shell scripts, newlines can be used inside string literals. But in +the shell statements of @file{Makefile} rules, this is not possible: +A newline not preceded by a bashslash is a separator between shell +statements. Whereas a newline that is preceded by a bashslash gets +removed, together with the backslash that precedes it. So, how can a +newline be used in a string literal? + +The trick is to set up a shell variable that contains a newline: + +@example + nlinit=`echo 'nl="'; echo '"'`; eval "$$nlinit" +@end example + +For example, in order to create a multiline @samp{sed} expression that +extracts the major version part of a version number, this code can be +used: + +@example + nlinit=`echo 'nl="'; echo '"'`; eval "$$nlinit"; \ + line1='/^[0-9]/@{'; \ + line2='s/^\([0-9]*\).*/\1/p'; \ + line3='q'; \ + line4='@}'; \ + line5='c\'; \ + line6='0'; \ + line7='q'; \ + sed_script=$$@{line1@}$$@{nl@}$$@{line2@}$$@{nl@}$$@{line3@}; \ + sed_script=$$@{sed_script@}$$@{nl@}$$@{line4@}$$@{nl@}$$@{line5@}; \ + sed_script=$$@{sed_script@}$$@{nl@}$$@{line6@}$$@{nl@}$$@{line7@}; \ + major=`echo '$(VERSION)' | sed -n -e "$$sed_script"` +@end example + @node obj/ and Make @section The @file{obj/} Subdirectory and Make @cindex @file{obj/}, subdirectory -- 1.6.3.2
From 12fc22759ba2a1dc5f22a938a639913a554297cc Mon Sep 17 00:00:00 2001 From: Bruno Haible <bruno@xxxxxxxxx> Date: Sat, 19 Jun 2010 00:06:37 +0200 Subject: [PATCH 1/2] Document how to write comments in makefile rules. --- ChangeLog | 6 ++++++ doc/autoconf.texi | 8 ++++++++ 2 files changed, 14 insertions(+), 0 deletions(-) diff --git a/ChangeLog b/ChangeLog index 52a7bb1..2dbf4ca 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,11 @@ 2010-06-18 Bruno Haible <bruno@xxxxxxxxx> + Document how to write comments in makefile rules. + * doc/autoconf.texi (Comments in Make Rules): Mention a workaround + syntax. + +2010-06-18 Bruno Haible <bruno@xxxxxxxxx> + Document sed -e limitation. * doc/autoconf.texi (Limitations of Usual Tools): Mention portability problem of sed -e option with script fragments. diff --git a/doc/autoconf.texi b/doc/autoconf.texi index eba61f4..90457f1 100644 --- a/doc/autoconf.texi +++ b/doc/autoconf.texi @@ -18937,6 +18937,14 @@ all: # foo @end example +As a workaround, you can use the @command{:} no-op command with a string +argument that gets ignored: + +@example +all: + : "foo" +@end example + @node obj/ and Make @section The @file{obj/} Subdirectory and Make @cindex @file{obj/}, subdirectory -- 1.6.3.2
From f5ef9e544cc65d80442d79bf7b485fed16c4ef9b Mon Sep 17 00:00:00 2001 From: Bruno Haible <bruno@xxxxxxxxx> Date: Sat, 19 Jun 2010 00:49:48 +0200 Subject: [PATCH 2/2] Document how to use literal newlines in makefile rules. --- ChangeLog | 5 +++++ doc/autoconf.texi | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 0 deletions(-) diff --git a/ChangeLog b/ChangeLog index 2dbf4ca..65c225c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,10 @@ 2010-06-18 Bruno Haible <bruno@xxxxxxxxx> + Document how to use literal newlines in makefile rules. + * doc/autoconf.texi (Newlines in Make Rules): New section. + +2010-06-18 Bruno Haible <bruno@xxxxxxxxx> + Document how to write comments in makefile rules. * doc/autoconf.texi (Comments in Make Rules): Mention a workaround syntax. diff --git a/doc/autoconf.texi b/doc/autoconf.texi index 90457f1..9b5bc4b 100644 --- a/doc/autoconf.texi +++ b/doc/autoconf.texi @@ -529,6 +529,7 @@ Portable Make Programming * The Make Macro SHELL:: @code{$(SHELL)} portability issues * Parallel Make:: Parallel @command{make} quirks * Comments in Make Rules:: Other problems with Make comments +* Newlines in Make Rules:: Using literal newlines in rules * obj/ and Make:: Don't name a subdirectory @file{obj} * make -k Status:: Exit status of @samp{make -k} * VPATH and Make:: @code{VPATH} woes @@ -18546,6 +18547,7 @@ itself. * The Make Macro SHELL:: @code{$(SHELL)} portability issues * Parallel Make:: Parallel @command{make} quirks * Comments in Make Rules:: Other problems with Make comments +* Newlines in Make Rules:: Using literal newlines in rules * obj/ and Make:: Don't name a subdirectory @file{obj} * make -k Status:: Exit status of @samp{make -k} * VPATH and Make:: @code{VPATH} woes @@ -18945,6 +18947,43 @@ all: : "foo" @end example +@node Newlines in Make Rules +@section Newlines in Make Rules +@cindex Newlines in @file{Makefile} rules +@cindex @file{Makefile} rules and newlines + +In shell scripts, newlines can be used inside string literals. But in +the shell statements of @file{Makefile} rules, this is not possible: +A newline not preceded by a bashslash is a separator between shell +statements. Whereas a newline that is preceded by a bashslash gets +removed, together with the backslash that precedes it. So, how can a +newline be used in a string literal? + +The trick is to set up a shell variable that contains a newline: + +@example + nlinit=`echo 'nl="'; echo '"'`; eval "$$nlinit" +@end example + +For example, in order to create a multiline @samp{sed} expression that +extracts the major version part of a version number, this code can be +used: + +@example + nlinit=`echo 'nl="'; echo '"'`; eval "$$nlinit"; \ + line1='/^[0-9]/@{'; \ + line2='s/^\([0-9]*\).*/\1/p'; \ + line3='q'; \ + line4='@}'; \ + line5='c\'; \ + line6='0'; \ + line7='q'; \ + sed_script=$$@{line1@}$$@{nl@}$$@{line2@}$$@{nl@}$$@{line3@}; \ + sed_script=$$@{sed_script@}$$@{nl@}$$@{line4@}$$@{nl@}$$@{line5@}; \ + sed_script=$$@{sed_script@}$$@{nl@}$$@{line6@}$$@{nl@}$$@{line7@}; \ + major=`echo '$(VERSION)' | sed -n -e "$$sed_script"` +@end example + @node obj/ and Make @section The @file{obj/} Subdirectory and Make @cindex @file{obj/}, subdirectory -- 1.6.3.2
_______________________________________________ Autoconf mailing list Autoconf@xxxxxxx http://lists.gnu.org/mailman/listinfo/autoconf