If the condition is inside the target in the Makefile

I want to do something like this where I want to run svn commit if the file has changed. The file has a time stamp that always changes. Therefore, if nothing more than a timestamp, then I want to commit the file.

There will be something similar in the makefile. But the If condition is not working properly. It is executed even if it is not. Can someone help me what is the problem?

UPDATE_STATE_FILE : $(eval NO_LINES_CHANGES_IN_STATE = $(shell svn di STATE/build.state --diff-cmd=diff -x --normal | grep "^[<>]" | wc -l)) @echo $(NO_LINES_CHANGES_IN_STATE) ifneq ($(strip $(NO_LINES_CHANGES_IN_STATE)), 2) ifneq ($(strip $(NO_LINES_CHANGES_IN_STATE)), 0) @echo $(NO_LINES_CHANGES_IN_STATE) $(SVN) commit; $(SVN) update; endif endif 
+4
source share
2 answers

You cannot mix execution conditions inside command rules. Conditional expressions are like preprocessor instructions in C or C ++; they are processed as the file is read, before any processing is performed (for example, the rules being executed).

If you need conditional conditions inside rules, you should write a rule using conditional conditional expressions, and not create legend:

 UPDATE_STATE_FILE : @NO_LINES_CHANGES_IN_STATE=`svn di STATE/build.state --diff-cmd=diff -x --normal | grep "^[<>]" | wc -l`; \ echo $$NO_LINES_CHANGES_IN_STATE; \ if [ $$NO_LINES_CHANGES_IN_STATE -ne 2 ] && [ $$NO_LINES_CHANGES_IN_STATE -ne 0 ]; then \ echo $$NO_LINES_CHANGES_IN_STATE; \ $(SVN) commit; \ $(SVN) update; \ fi 
+9
source

After reading the answer given by @MadScientist, I came up with a different approach.

Not knowing if the shell conditions would work in any environment (windows vs. linux), I wrapped the rules inside the conditional expression instead of having the conditional rule inside the rule. i.e.

 ifdef MY_FLAG %.o: %.c $(CC) -o $@ -c $^ $(CFLAGS) else %.o: %.c @$(CC) -o $@ -c $^ $(CFLAGS) endif 

Good luck to everyone who has been here.

EDIT

As James Moore noted in the comments, this different approach requires caution and notification about when and how variables are defined with respect to the placement of the if in the control flow.

+2
source

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


All Articles