Jamie
Jamie

Reputation: 7411

How can I assign command output to a variable in GNU make target rule?

At a BASH prompt, I can do the following:

~/repo$ HISTORY_LOG=$(git log $(get_old_version)..$(get_new_version)); [[ ! -z ${HISTOR_LOG} ]] && ( echo "Some header"; echo "${HISTORY_LOG}" )

Where git log is demonstrably simplified version of what I actually have.

In a make file I have the following command as part of a target:

$(OUTPUT): $(INPUT)
        ...
        echo "Some header" > $(LOG_FILE)
        git log $(shell get_old_version)..$(shell get_new_version) >> $(LOG_FILE)

How can I rewrite the make target to behave like the bash command?

If I do the following line-feeds are being stripped:

$(OUTPUT): $(INPUT)
        ...
        HISTORY_LOG="$(shell git log $(shell get_old_version)..$(shell get_new_version))" ; \
           [ -z "$${HISTORY_LOG}" ] && \
             true || \ 
             (echo "Some header" ; echo "$${HISTORY_LOG}" )

when run looks like:

~/repo $ make
commit 2b4d87b0e64d129028c1a7a0b46ccde2f42c5e93     Author: Jamie   <[email protected]>   Date:   Mon Jun 25 18:46:27 2012 -0400              Issue #468: This sucker's been sped up.

and what I prefer would be:

~/repo $ make
commit 2b4d87b0e64d129028c1a7a0b46ccde2f42c5e93     
Author: Jamie   <[email protected]>
Date:   Mon Jun 25 18:46:27 2012 -0400 

    Issue #468: This sucker's been sped up.

I think the issue is the that make executes commands in /bin/sh and not /bin/bash. Regardless I'm looking for a portable solution if there is one.

Upvotes: 0

Views: 1814

Answers (1)

William Pursell
William Pursell

Reputation: 212218

Make's shell is eating your newlines. Just stop using it. Instead of $( shell get_old_version ), escape the $:

$$( get_old_version )

Upvotes: 5

Related Questions