1

I want to use dynamic variables in make file, e.g.:

all: ask
        @echo "I want the $$ANSWER here too"
ask:
        @read -p "please enter your answer: " ans && export ANSWER=$$ans
        @echo "Your answer is $$ANSWER"

The above example does not work, the $ANSWER is empty.

EDIT:

Asking question using read is only an example. My purpose is that the value must be dynamically obtained at runtime of the target, not calculated on top of the makefile, or passed in on command line of make.

xrfang
  • 1,754
  • 4
  • 18
  • 36
  • just so you know, GNU Make is not designed for interactive queries. It's possible, but it's like putting a saddle on a cow: the saddle does not fit and the cow is unhappy. The interactivity should be in a wrapper around Make. – Mark Galeck Apr 03 '18 at 05:13
  • 1
    Look maybe at [this other answer](https://stackoverflow.com/questions/49493821/md5sum-fails-to-find-file-after-changing-dir-in-a-makefile-instruction/49511849#49511849) – Renaud Pacalet Apr 03 '18 at 06:09
  • ... and at [this other answer](https://stackoverflow.com/questions/48663572/passing-a-command-line-argument-from-inside-a-makefile/48663753#48663753) if your GNU Make is 3.82 or later. – JFMR Apr 03 '18 at 06:15
  • @考えネロク No. `.ONESHELL` , or chaining commands, are no good for him, read the question carefully. – Mark Galeck Apr 03 '18 at 07:27
  • There are a couple of ways to do this, none of them elegant. Do you have a purpose in mind, or is it just an exercise? – Beta Apr 03 '18 at 16:15
  • @Beta I doubt this is a homework exercise. Because this is not supposed to be done with Make, and the professor should know this. Or if they don't, then it's a dumb exercise and better to just help the person with it. – Mark Galeck Apr 03 '18 at 20:32
  • This is not homework. I just used Make to wrapper packaging and installation of a set of tools I use for setup VPN, here is the repository: https://github.com/xrfang/plumber. I do use interaction in the script, i.e. **read**. But what invoked me to ask this question is I want to create global variable to be used throughout the Make process – xrfang Apr 04 '18 at 00:52
  • @Beta I am assuming one of the "couple" of ways is the kind of thing what I am doing. What is the "other"? I can't imagine any other way. I am interested, let me learn from you. – Mark Galeck Apr 04 '18 at 03:55

1 Answers1

0

As I said in my comment, asking interactive questions in makefiles is a bad idea; Make is not designed for that. The whole point of Make, is that if all files are up to date, nothing happens. In your case, no matter what, you want something to happen. Not a good use case for Make.

Having said that, I still want to give you an answer. Maybe you will like this:

all: ask
    @echo "I want the $$(cat ANSWER) here too"
ask:
    @read -p "please enter your answer: " ans && echo -n "$$ans" > ANSWER
    @echo "Your answer is $$(cat ANSWER)"
Mark Galeck
  • 6,155
  • 1
  • 28
  • 55
  • I am sorry that you missed my point, interaction is NOT the key point of my question, my question is that the value of the variable is dynamically **calculated** inside a command of a specific target, I then want to use it in subsequent execution, no matter what target it is running in. – xrfang Apr 03 '18 at 13:18
  • to be more specific, any answer that pass the variable into make on command line, or calculated outside of any target is NOT what I want. – xrfang Apr 03 '18 at 13:19
  • @xrfang OK how about this new answer above – Mark Galeck Apr 03 '18 at 20:28
  • This is exactly what I am doing, show the github repository... OK, I will accept this answer, if there is no "official" way to export variables in Makefile... @Mark Galeck – xrfang Apr 04 '18 at 00:53
  • @xrfang the way Make works, is that it first parses the makefiles, calculates the dependency tree, and then, second, executes recipes as needed. It's done this way for efficiency. So you can see, what you are asking is impossible to do within Make syntax alone, you have to somehow store the information in the filesystem. – Mark Galeck Apr 04 '18 at 03:52