Read the Makefile variable from the console if it is not set

I am updating a Makefile that accesses some resources from an external source, i.e. there is a rule of form

$(External)/% : cvs up $@ 

... which works as expected for unlimited resources. Now the function has drifted, and external resources require a more complex login, so the rule has changed to something that is not too different from this:

 $(External)/% : cvs -d :pserver:$(CVSUSER)@cvs-server up $@ 

... which makes the rule dependent on the CVSUSER variable. A quick and easy way to enforce this will be to abort the message with a useful error message if it is undefined. But this is not fun, I would like to read the CVSUSER variable from the console if it was not configured by the time it was needed. I naively tried

 CVSUSER ?= $(shell read -p "User name: ") 

but that clearly doesn't work :) How would you do it?

+3
source share
3 answers

And, of course, I just need an extended variable, not a recursive one. Thus, I can let the variable depend on itself and use the usual Make utilities to conditionally set it. Conditional assignment implicitly creates normal (recursive), so I need to do either

 CVSUSER := $(shell if [ -z $(CVSUSER) ] ; then read -p "User name: " REPLY ; echo $$REPLY ; fi ) 

or

 ifndef CVSUSER CVSUSER := $(shell then read -p "User name: " REPLY ; echo $$REPLY ; fi ) endif 
+1
source

$(shell) captures the output from the shell command. But read by default introduces input into the variable ( REPLY ) and does not output it. Here is a quick solution:

 CVSUSER ?= $(shell read -p "User name: ";echo $$REPLY) 
+3
source

You have already found the solution yourself, but I would suggest that it is bad practice to create a makefile that depends on user interaction at any time after the call. This makes assembly more difficult to integrate into automated build systems (e.g. CruiseControl, AntHill, etc.), and also makes integration into distributed build systems (pvmgmake, etc.) more difficult.

A better approach would be to put check for CVSUSER in a shell script that calls the makefile, and then immediately make the makefile fail if CVSUSER not installed.

Respectfully,

Eric Melsky

http://blog.electric-cloud.com

+2
source

Source: https://habr.com/ru/post/905459/


All Articles