Automatic installation of packages from inside the makefile

Purpose: when the user enters “create packages”, he automatically searches for the libx11-dev package (necessary to compile my program) and, if not found, install it. Here is a stripped down version of my makefile:

PACKAGES = $(shell if [ -z $(dpkg -l | grep libx11-dev) ]; then sudo apt-get install libx11-dev; fi) [other definitions and targets] packages: $(PACKAGES) 

When I type "make packages", I am prompted to enter the root password. If entered correctly, it then freezes indefinitely.

Am I trying to do what I am trying to do from a makefile? If so, how?

Many thanks.

+6
source share
2 answers

The problem is that the shell function acts as backreferences in the shell: it outputs the output to stdout and returns it as the value of the function. So, apt-get is not hanging, it is waiting for you to answer some question. But you cannot see the question because make made a conclusion.

The way you do this will not work. Why are you using a shell instead of just writing it as a rule?

 packages: [ -z `dpkg -l | grep libx11-dev` ] && sudo apt-get install libx11-dev .PHONY: packages 
+6
source

I figured out the best way to avoid the problem with unexpected arguments for the if statement:

 if ! dpkg -l | grep libx11-dev -c >>/dev/null; then sudo apt-get install libx11-dev; fi 

The -c flag in grep forces it to return the number of lines in dpkg -l that contain the line libx11-dev, which will either be 0 (if deleted) or 1 (if installed), which allows

 dpkg -l | grep libx11-dev -c 

to be processed as an ordinary boolean variable.

+3
source

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


All Articles