Why are my Makefile variables inconstant after using tr?

822 views Asked by At

I'm working on a Makefile which takes a command line parameter, runs a translation (tr) on it, then uses that variable to create a directory and also prints the result. When the variable is printed, it is as I expect (the translated name), however when the variable is used to create the directory, the results are incorrect.

So for example, when I run:

make build=debug

I see:

Building...
***************************
BuilD Type:       Debug
Build Target:     x86

But the resultant file system is:

out/
 └─── debug | tr d D_x86

I thought that running the translation on the input variable build and assigning it to $BUILD_TYPE would have set this new variable to "Debug" but it seems like that is not the case all the time... How does this actually work?


Full Makefile:

BUILD_TYPE=Release
BUILD_TARGET=Arm
OUTPUT_DIR=out

MKDIR_P = mkdir -p

ifeq ($(build),debug)
    BUILD_TYPE='$(build) | tr d D'
endif
ifeq ($(target),x86)
    BUILD_TARGET=$(target)
endif

TT=$(BUILD_TYPE)_$(BUILD_TARGET)

all: directories
    @echo 'Building...'
    @echo '***************************'
    @echo 'Build Type:       $(BUILD_TYPE)'
    @echo 'Build Target:     $(BUILD_TARGET)'
    @echo 'Output Directory: $(PWD)/$(OUTPUT_DIR)'
    @echo '***************************'

directories:
    ${MKDIR_P} $(OUTPUT_DIR)
    ${MKDIR_P} $(OUTPUT_DIR)/$(TT)
1

There are 1 answers

4
Etan Reisner On BEST ANSWER

This line:

BUILD_TYPE='$(build) | tr d D'

is not running a shell command. That is a literal string assignment.

You are assigning to the variable BUILD_TYPE the literal string 'debug | tr d D'.

To do what you want you need to actually run a shell command. Something like this for example.

BUILD_TYPE:=$(shell echo '$(build)' | tr d D)\

As to why you are seeing things "correctly" on the output line that's because you happen to be generating a valid shell pipeline there and make is running it.

That is this line

@echo 'Build Type:       $(BUILD_TYPE)'

becomes

@echo 'Build Type:       'debug | tr d D''

which, you'll notice, is a valid shell line. Also notice that you get BuilD type and not Build type printed out?