Node.JS
Node.JS

Reputation: 1578

Makefile split string and pipe it to different target

I am trying to write a simple Makefile to build .expected files and compare them but I am failing.

APSSCHED=../../bin/apssched
BASE=.:../../base:../../examples
FLAGS=-DCOT
EXAMPLES=../../examples/
CASES=simple-binding1 simple-binding2

# skipping lines doesn't work ...
# run command and skip the first line
%.aps:
    ${APSSCHED} ${FLAGS} -p ${BASE} ${EXAMPLES}/$* | tail -n +2

# get all cases as an array to pipe it to different make targets
# maybe overcomplicating
cases:
    echo ${CASES} | \
    awk '{split($$0,numbers," ")} END {for(n in numbers){ print numbers[n] }}'

# create all .expected files from ${CASES}
build.expected:
    $(MAKE) cases | xargs -n1 -I file /bin/bash -c '$(MAKE) file.build.expected'

# create single .expected file
%.build.expected:
    $(MAKE) $*.aps > $*.expected

# compare result with 
%.compare:
    $(MAKE) $*.aps | diff $*.expected -

# run command for all cases and diff the result with corresponding expected
all:
    $(MAKE) cases | xargs -n1 -I file /bin/bash -c '$(MAKE) file.compare'

clean.expected:
    rm *.expected

Running make without any target and nothing happens.

echo simple-binding1 simple-binding2 | \
awk '{split($0,numbers," ")} END {for(n in numbers){ print numbers[n] }}'
simple-binding1
simple-binding2

I think the issue is with my cases target. I am not sure if I am on the right track.

I appreciate any help or hint.

Upvotes: 0

Views: 599

Answers (1)

raspy
raspy

Reputation: 4261

I would avoid re-running make just to call a different target - it's a performance hit and may be unreliable (depending on rest of the Makefile) since separate calls may not be able to track dependencies correctly.

Moreover, I would avoid using | - every time a command is concatenated with pipe, exit code of piped command would be exit code of the last command. So a call like command | tail would return the exit code of tail (which would almost always succeed). Even if the command has failed, it would be covered with exit code 0 from tail and make will not detect the error and will not stop.

Thus said, I tried to rewrite your approach by just creating dependencies between the targets, like so:

$ cat Makefile
APSSCHED=../../bin/apssched
EXAMPLES=../../examples
BASE=.:../../base:$(EXAMPLES)
FLAGS=-DCOT
CASES=simple-binding1 simple-binding2

# Just for reproducing
$(EXAMPLES)/%.aps: ;

# Generate output and store it in a file
%.output: $(EXAMPLES)/%.aps
        # echo is only for reproducing
        echo $(APSSCHED) $(FLAGS) -p $(BASE) $< > $@

# Copy actual output as expected
%.expected: %.output
        cp -f $< $@

# Compare actual output with expected
.PHONY: %.compare
%.compare: %.output | %.expected
        diff $| $<

# Generate and verify all outputs
.PHONY: all
all: $(addsuffix .compare,$(CASES))

# Regenerate expected output
.PHONY: build.expected
build.expected: $(addsuffix .expected,$(CASES))

.PHONY: clean.expected
clean.expected:
        -rm -f *.expected

Now the make build.expected will create expected output files, while make all or make will check the actual output against expected:

$ make build.expected
echo ../../bin/apssched -DCOT -p .:../../base:../../examples ../../examples/simple-binding1.aps > simple-binding1.output
cp -f simple-binding1.output simple-binding1.expected
echo ../../bin/apssched -DCOT -p .:../../base:../../examples ../../examples/simple-binding2.aps > simple-binding2.output
cp -f simple-binding2.output simple-binding2.expected
rm simple-binding1.output simple-binding2.output

$ make
echo ../../bin/apssched -DCOT -p .:../../base:../../examples ../../examples/simple-binding1.aps > simple-binding1.output
diff simple-binding1.expected simple-binding1.output
echo ../../bin/apssched -DCOT -p .:../../base:../../examples ../../examples/simple-binding2.aps > simple-binding2.output
diff simple-binding2.expected simple-binding2.output
rm simple-binding1.output simple-binding2.output

Upvotes: 1

Related Questions