Search

makefiles: Complex conditional execution

While doing the conditional execution in makefiles, using only if and ifneq allows us to check for one condition but to be able to check multiple conditions one after another, we can use either if or ifneq after the else to check for further conditions.
Syntax:



If the ifneq condition fails then the flow will go to the else and check the condition specified after the else. We can use as many else statements as we require.
Let us say we have three "c" programs, hello_1.c, hello_2.c and hello_3.c. We need to compile them depending on which user is running the make command. hello_1.c should be compiled if we are running make as root, hello_2.c if we are running make as "user1" and for any other user hello_3.c. This can be achieved by the following makefile.



In the above makefile, first variable "user" is compared with "root" then with "user1". If both the conditions turn out to be false then the statements after the final else gets executed, which sets the targets to hello_3.c as required.

If we run the above makefile as root then we will get the output



If we run it as "user1" we will get the output



If we run it as any other user we will get the output


Conditional execution using ifneq

Conditional execution can also be done using ifneq which is the opposite of ifeq.

The syntax of ifneq is



ifneq(,) returns true when and are not equal. If and are equal it jumps to the else statement.

Using the above logic, the example used in the post "Conditional execution using ifeq" can be re written using ifneq as below.



Thus ifneq checks if the current user is not root, which if true then sets the target to $(pf)_1, else it sets it to $(pf)_2.

If we execute the make command as root we will get the following output



If we do the same as any other user other than root, we will get the following output




Makefies : Conditional execution using ifeq

Conditions can be added in makefiles using "if" conditions, to ensure the commands get executed only under certain conditions.

The syntax of if in a makefile is



Let us say we have two files hello1.c and hello2.c which we want to compile using makefile. But the condition for compiling these files is that if the user who is compiling is root then hello_1.c should be compiled,else for any other user hello_2.c should be compiled. We can use the if condition in makefile to achieve this.

This can be done using the following makefile





Above statement assigns the current user name to the variable "user" using the command "whoami".

Then we use the "if" condition and check the user name and assign the target based on the result of the condition check.

If we execute the make command as root we will get the following output



If we do the same as any other user other than root, we will get the following output




seq : To generate a sequence of numbers

seq can be used to generate a sequence of numbers separated by any kind of separator as required.



By default seq generates the range of numbers starting from 1. We can generate the list from any starting number by mentioning the stating number also as an argument.



By default the numbers are generated separated by new lines. We can change the separator by using the option -s



We can make the number to be of equal length using the option -w



The increment by default is by one number,this can be changed by passing the increment number as the second argument.



The number 2 as the second argument indicates that the increment is to be by two numbers.

We can also generate a reverse seq of numbers