Setting a variable in a Makefile recipe and then testing it -


in recipe, want git fetch current sha1 hash if git present , i'm in git repository. problem $(git) null. don't understand why yet setting $(hash) , echoing works. what's going on here? how can make execute chunk of code if git installed?

hash: ifneq ("$(wildcard .git)", "")     $(eval git=`which git`)   ifdef $(git)     $(eval hash=`git rev-parse head`)     @echo $(hash)     @echo "#define git_sha1 \"$(hash)\"" > git_sha1.h   endif else     @echo "not in git repository" endif 

i want avoid having use shell script this.

i want git fetch current sha1 hash if git present , i'm in git repository

you like:

makefile 1

hash := $(if $(and $(wildcard .git),$(shell git)), \     $(shell git rev-parse head))  hash: ifdef hash     @echo $(hash)     @echo "#define git_sha1 \"$(hash)\"" > git_sha1.h else     @echo "git not installed or not in git repository" endif 

which runs like:

$ make 7cf328b322f7764144821fdaee170d9842218e36 

when in git repository (with @ least 1 commit) , when not in git repository runs like:

$ make git not installed or not in git repository 

see 8.4 functions conditionals

notice contrast between:

ifdef hash 

and in own attempt:

ifdef $(git) 

the first tests if hash defined (i.e. non-empty) make-variable, want. second tests if $(git), i.e. value of git, hoping `which git`, defined make variable. not want. `which git` isn't defined make-variable, if git is, and:

ifdef `which git` 

would sytax error[1].

presumably have no real need for:

    @echo $(hash) 

in case simplify to:

makefile 2

hash: ifneq ($(and $(wildcard .git),$(shell git)),)     @echo "#define git_sha1 \"$$(git rev-parse head)\"" > git_sha1.h else     @echo "git not installed or not in git repository" endif 


[1] why don't see syntax error ifdef $(git)? because git not = `which git` in context. undefined. following makefile illustrates:

makefile 3

global_var_a := global_var_a all:     $(eval recipe_var_a=recipe_var_a) ifdef recipe_var_a     @echo $(recipe_var_a) recipe_var_a  else     @echo recipe_var_a defined within recipe endif ifdef global_var_a     @echo global_var_a defined globally     @echo $(recipe_var_a) global_var_a endif 

run:

$ make recipe_var_a defined within recipe global_var_a defined globally recipe_var_a global_var_a 

Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -