Hi Guys, I would like to make a change to the way %* behaves in spec strings. Currently whenever %* is encountered the text that was previously matched is substituted, followed by a space. Thus %{foo=*:bar%*baz} would match -foo=4 and insert 'bar4 baz'. I would like to change this so that the space is only inserted if %* is at the end of a spec sequence. (In all of the strings currently built in to gcc this is the case, so my patch will not change current behaviour).
The motivation is that I want to construct a pathname based on a command line option using a spec string like this: %{mmcu=*:--script=%*/memory.ld} So that if the user invokes -mmcu=msp430f2617 then gcc will generate: --script=msp430f2617/memory.ld A spec string like this however: %{mmcu=*:--script=%*}/memory.ld would match the current behaviour and generate: --script=msp430f2617 /memory.ld As a secondary feature of the patch I have also updated the documentation to explicitly state when a space will be inserted into the generated text. I have tested the patch and found no regressions using an i686-pc-liunx-gnu toolchain and an msp430-elf toolchain. OK to apply ? Cheers Nick gcc/ChangeLog 2013-10-17 Nick Clifton <ni...@redhat.com> * gcc.c (do_spec_1): Do not insert a space after a %* substitution unless it is the last part of a spec substring. * doc/invoke.texi (Spec Files): Document space insertion behaviour of %*. Index: gcc/doc/invoke.texi =================================================================== --- gcc/doc/invoke.texi (revision 203750) +++ gcc/doc/invoke.texi (working copy) @@ -10929,6 +10929,22 @@ for each matching switch, with the @code{%*} replaced by the part of that switch matching the @code{*}. +If @code{%*} appears as the last part of a spec sequence then a space +will added after the end of the last substitution. If there is more +text in the sequence however then a space will not be generated. This +allows the @code{%*} substitution to be used as part of a larger +string. For example, a spec string like this: + +@smallexample +%@{mcu=*:--script=%*/memory.ld@} +@end smallexample + +when matching an option like @code{-mcu=newchip} will produce: + +@smallexample +--script=newchip/memory.ld +@end smallexample + @item %@{.@code{S}:@code{X}@} Substitutes @code{X}, if processing a file with suffix @code{S}. Index: gcc/gcc.c =================================================================== --- gcc/gcc.c (revision 203750) +++ gcc/gcc.c (working copy) @@ -388,7 +388,8 @@ %2 process CC1PLUS_SPEC as a spec. %* substitute the variable part of a matched option. (See below.) Note that each comma in the substituted string is replaced by - a single space. + a single space. A space is appended after the last substitution + unless there is more text in current sequence. %<S remove all occurrences of -S from the command line. Note - this command is position dependent. % commands in the spec string before this one will see -S, % commands in the @@ -422,7 +423,9 @@ once, no matter how many such switches appeared. However, if %* appears somewhere in X, then X will be substituted once for each matching switch, with the %* replaced by the - part of that switch that matched the '*'. + part of that switch that matched the '*'. A space will be + appended after the last substitution unless there is more + text in current sequence. %{.S:X} substitutes X, if processing a file with suffix S. %{!.S:X} substitutes X, if NOT processing a file with suffix S. %{,S:X} substitutes X, if processing a file which will use spec S. @@ -5375,7 +5378,17 @@ { if (soft_matched_part[0]) do_spec_1 (soft_matched_part, 1, NULL); - do_spec_1 (" ", 0, NULL); + /* Only insert a space after the substitution if it is at the + end of the current sequence. So if: + + "%{foo=*:bar%*}%{foo=*:one%*two}" + + matches -foo=hello then it will produce: + + barhello onehellotwo + */ + if (*p == 0 || *p == '}') + do_spec_1 (" ", 0, NULL); } else /* Catch the case where a spec string contains something like