5

The below code works in the ptxdist Makefile, but like to know if there is any better solution to check if all required packages are installed before proceeding the build?

ENV_VERIFICATION:
    @echo ------------START ENV VERIFICATION---------------
    if ! dpkg -s sudo | grep Status | grep -q installed; then \
      echo ERROR: sudo package not installed!; \
      exit 1; \
    fi
    if ! dpkg -s scons | grep Status | grep -q installed; then \
      echo scons package not installed!; \
      exit 1; \
    fi
    @echo ------------END ENV VERIFICATION---------------

I could run below command in my system, would be nice to print the same in make log, any help to improve the above code and print below output (if package is installed) to the log is appreciated. Thanks in advance!

$ dpkg-query -W -f='${Package} ${Status}\n' sudo
sudo install ok installed
rodee
  • 3,233
  • 5
  • 34
  • 70

2 Answers2

5

Checking using dpkg is unfriendly to people trying to install your package on a distro that's not a Debian derivative. Maybe they are running CentOS, or Fedora, or Arch, or Gentoo...

So the usual solution in this case is to look for a package's main executable in the user's path. For instance, look for sudo using which sudo, or in bash, type -P sudo. This doesn't protect against someone having a completely unrelated program called sudo in their path, but at some point, you do have to allow people to shoot themselves in the foot.

tetromino
  • 3,490
  • 1
  • 15
  • 8
4

You will need to check to see if a specific needed component of that package exists on the system. Look at Check if a program exists from a makefile for different ways of doing that. Some distributions may have specific tools available, but since what constitutes a 'package' varies from distro to distro, I know of no generic test of a 'package'.

Community
  • 1
  • 1
David C. Rankin
  • 81,885
  • 6
  • 58
  • 85