0

I have a target like this:

curly:
    curl --header "Content-Type: application/json" --data \
    'data line 1 \
    data line 2' \
    ${URL}

It's important that the two data lines echo separately to make it easier for the user of the Makefile to see what's going on.

This works nicely. But then I wanted to add a second target. I don't want to duplicate the data so hoped to define a macro and use it in both targets:

DATA = data line 1 \
       data line 2

curlier:
    curl --header "Content-Type: application/json" --data \
    '${DATA}' \
    ${URL2}

This doesn't work as well. The command executes correctly, but the data lines are run together when the recipe is echoed.

I tried the trick of defining a newline macro and embedding it in the DATA string, to no avail.

I can get clean output by invoking make recusively, but my reviewer doesn't like that.

Any other suggestions?

Adam Wildavsky
  • 281
  • 2
  • 7

2 Answers2

0
define newln :=


endef

DATA := data line 1 \$(newln) data line 2

curlier:
        curl --header "Content-Type: application/json" --data \
        '${DATA}' \
        ${URL2}

But to be honest, I would try to explain to your reviewer that fiddling with technically superfluous spaces and newlines in makefiles is lowering the quality of a makefile instead of raising it.

Vroomfondel
  • 2,704
  • 1
  • 15
  • 29
0

This is what I ended up with:

curly_URL    := http://somewhere
curlier_URL  := http://somewhere_else
CURL_URL     ?= ${$@_URL}
    
# Slightly unusual approach in order to keep the command-line output legible.
curly curlier:
    curl --header "Content-Type: application/json" --data \
    'data line 1 \
    data line 2' \
    ${CURL_URL}

H/T Pauli Nieminen

Adam Wildavsky
  • 281
  • 2
  • 7
  • It seems this works on macOS but not on some other platforms. It may have to do with the versions of make and bash in use. In any case, I circumvented the issue by having curl load the data from a file, per https://stackoverflow.com/questions/6408904/send-post-request-with-data-specified-in-file-via-curl – Adam Wildavsky Jul 18 '20 at 21:19