-1

Assume you have a makefile of the form

<line n content>    \
<line n+1 content>  \
<line n+2 content>  \
<line n+3 content>  

and you would like the shell to receive

<line n content> <line n+2 content> <line n+3 content> (note: <line n+1 content is missing in the desired output!)

How do you correctly (if possible) comment <line n+1 content> \?

This is not working:

<line n content>    \
# <line n+1 content>  \
<line n+2 content>  \
<line n+3 content>  \

Please note this: Comment multi-line bash statements has no answer and these: How to put a line comment for a multi-line command Commenting in a Bash script inside a multiline command refers on how to ADD a comment on a line, e.g.

<line n content>    \
<line n+1 content>  # comment for line n+1\
<line n+2 content>  \
<line n+3 content>  \

which differs from commenting an entire "line" (ok, a part of it)

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
  • I think \ during transpile of bash script end up as one line so it will be like: # comment for line n+1 . I did not check it but I think it could be like that – Daniel Lazar Jan 17 '23 at 11:07
  • yeah # right after \ is not taken as an comment – Daniel Lazar Jan 17 '23 at 11:10
  • 1
    How about wrapping the line inside the makefile like this: `$(: ' ' ) \ `? As long as _line n+1 content_ does not contain a single-quote, the effect should be as if that line were not present. bash would "evaluate" the `$( : ...)`, but since it does not produce any output, the evaluation would not have any observable effect. – user1934428 Jan 17 '23 at 12:05
  • It would be really helpful if you gave more context. When you say "a makefile of the form" the form you show is not valid, all by itself, in makefiles. It's only valid as part of some larger context, which you don't make clear. Is that content part of a _recipe_? Or is it part of a make variable assignment? Or something else? – MadScientist Jan 17 '23 at 14:34
  • In any event, it's generally not possible in make to have a comment embedded in a backslash-continued list of things. – MadScientist Jan 17 '23 at 14:35
  • If it depends on context @MadScientist I'll be happy to accept an answer that gives me an example of context where it can be done and an example of context where it cannot be done –  Jan 17 '23 at 16:30
  • It is very different. In makefile syntax the backslash removed before the comment is checked, so all the rest of the lines will be all be considered part of the comment. In a recipe, the comment is passed to the shell that make invokes, it's not interpreted by make at all. So the behavior of the comments depends on which shell you invoke and how it manages them. – MadScientist Jan 17 '23 at 18:05

2 Answers2

0

To give more context to my answer above:

You cannot add comments into makefile variable assignments. The make syntax doesn't allow it: make will first remove the backslashes and combine the lines. Only after that will it be checked for comments. So:

foo = foo # a comment \
      bar

is the same as:

foo = foo # a comment bar

In recipes, make doesn't interpret comments at all. It's up to the shell to decide how to interpret them. So for the shell, this works:

foo:
        echo one # a comment ; \
        echo two

That works how you'd "expect":

echo one # a comment; \
echo two
one
two
MadScientist
  • 92,819
  • 9
  • 109
  • 136
0

There is no direct support for this; # in a Makefile comments out any final backslash, too, breaking the requirement to keep shell commands on a single logical line (absent .ONESHELL: or other similar non-portable workarounds). One possible workaround is to use a different mechanism for comments, such as the : shell no-op, which however is slightly problematic because it evaluates its arguments, unlike the proper comment.

target:
    <line n content>    \
    <line n+1 content>;  : 'comment for line n+1' ;\
    <line n+2 content>  \
    <line n+3 content>  \
    etc

This obviously won't work if <line n+2 content> is a continuation of the command in <line n+1 content>, such as

# XXX BROKEN
target:
    sed -n -e '/foo/s/bar/baz/' \
      -e ':quux'  : 'comment for line n+1' ;\
      -e 's/ick/poo/tquux' $<

though of course in this individual case sed has its own facilities for comments.

target:
    sed -n -e '/foo/s/bar/baz/' \
      -e ':quux'  -e '# comment for line n+1' \
      -e 's/ick/poo/tquux' $<

With GNU make you can use some sort of wrapping to have the comments removed. Then you can even inline them within a command line, like this;

example:
    docker run \
        -v $(something)  $(patsubst %,,# volume to mount) \
        $(whatever)      $(patsubst %,,# name of container) \
        etc

Needless to say (I hope), this is pretty ugly and hackish, but might come in handy occasionally.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Thank you for your time triplee. I'm sorry I find really difficult to explain myself, but this aswer is for a different question. I don't need " #comment /" I need "# \" (this also imply that removing " \" keeps the makefile valid) –  Jan 20 '23 at 14:35
  • @Gino I'm afraid I can't make sense of your comment. Could you edit the question to explain in different words what you are looking for, and/or why the answers you have received so far are not acceptable? An actual code sample where these solutions don't work would be ideal; see also the guidance for including a [mre]. – tripleee Jan 20 '23 at 17:51