Home > OS >  Makefile: use shell command with generated content as argument
Makefile: use shell command with generated content as argument

Time:11-11

I have a make target, that first calls a CAE tool which generates reports. When this is done, the target calls a python script that shall take the content of the CAE reports (or more specific some grep'ed lines of the reports) as argument.

A minimum example is

target1:
  date > ./bar.txt
  echo $(shell cat ./bar.txt)

Problem is, that make expands the $(shell cat ./bar.txt) before the first command has been called and bar.txt has been updated. So for this minimum example, the echo prints the content of bar.txt before the update (the date from the previous target run).

( I know that I simply could write this example in another way without variables and the shell function call, this is just for the sake of showing the problem where I call a tool that takes an argument from a shell call. So actually I want to do sth like this:

target1:
  cae_tool_call
  report_eval.py -text "$(shell cat $(generated_report) | grep 'foo')"

where cae_tool_call generates the generated_report. And this -text "argument" does not resolve the argument without an explicit call of the shell function. )

I already tried with actual shell variables (instead of make variables), double escapes, immediate vs deferred variables but have no working solution yet. Any ideas?

#######################################

Edit to show some unexpected behavior:

I have this python script argument_example.py

import argparse

def main():
  parser = argparse.ArgumentParser()
  parser.add_argument("-r", "--reporttext", help="text string", required=True)
  args=parser.parse_args()

  if args.reporttext:
    print(args.reporttext)

main()

It just prints the text given with argument -r.

And I have these two make targets:

####################################
#this does not work

REPORTNAME := ./bar.txt

variable_report:
    date > $REPORTNAME
    python3 ./argument_example.py --reporttext "`(cat $REPORTNAME)`"

####################################
#this works

static_report:
    date > ./bar.txt
    python3 ./argument_example.py --reporttext "`(cat ./bar.txt)`"

When calling variable_report, the python scripts prints the outdated bar.txt content. When calling static_report, the python script prints the updated content.

CodePudding user response:

make recipes are already shell scripts. Never use the shell make function inside a recipe. In your first simple example use:

target1:
    date > bar.txt
    cat bar.txt

In your other example use:

generated_report := name-of-generated-report
target1:
    cae_tool_call
    report_eval.py -text "`cat $(generated_report) | grep 'foo'`"

Or even better:

generated_report := name-of-generated-report
target1:
    cae_tool_call
    report_eval.py -text "`grep 'foo' $(generated_report)`"
  • Related