检查程序是否存在于Makefile中 [英] Check if a program exists from a Makefile

查看:184
本文介绍了检查程序是否存在于Makefile中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查程序是否可以从Makefile调用?

How can I check if a program is callable from a Makefile?

(也就是说,程序应该存在于路径中,否则可以被调用.)

(That is, the program should exist in the path or otherwise be callable.)

例如,它可用于检查安装了哪个编译器.

It could be used to check for which compiler is installed, for instance.

例如类似于此问题,但不假设其基础该外壳与POSIX兼容.

E.g. something like this question, but without assuming the underlying shell is POSIX compatible.

推荐答案

有时您需要一个Makefile才能在不同的目标OS上运行,并且如果所需的可执行文件不在PATH中,则您希望构建早期失败.而不是在失败之前可能要运行很长时间.

Sometimes you need a Makefile to be able to run on different target OS's and you want the build to fail early if a required executable is not in PATH rather than to run for a possibly long time before failing.

engineerchuan 提供的出色解决方案要求制定一个目标.但是,如果要测试的可执行文件很多,并且Makefile有很多独立的目标,每个目标都需要测试,那么每个目标都需要测试目标作为依赖项.当您一次创建多个目标时,这会带来很多额外的输入以及处理时间.

The excellent solution provided by engineerchuan requires making a target. However, if you have many executables to test and your Makefile has many independent targets, each of which requires the tests, then each target requires the test target as a dependency. That makes for a lot of extra typing as well as processing time when you make more than one target at a time.

0xf 提供的解决方案可以测试可执行文件而无需确定目标.当可以单独或一起构建多个目标时,可以节省大量的键入和执行时间.

The solution provided by 0xf can test for an executable without making a target. That saves a lot of typing and execution time when there are multiple targets that can be built either separately or together.

我对后一种解决方案的改进是使用which可执行文件(在Windows中为where),而不是直接在GNU Make ifeq指令中依赖每个可执行文件中都有一个--version选项. ,而不是定义新变量,并且如果${PATH}中没有所需的可执行文件,则可以使用GNU Make error函数来停止构建.例如,要测试lzop可执行文件:

My improvement to the latter solution is to use the which executable (where in Windows), rather than to rely on there being a --version option in each executable, directly in the GNU Make ifeq directive, rather than to define a new variable, and to use the GNU Make error function to stop the build if a required executable is not in ${PATH}. For example, to test for the lzop executable:

 ifeq (, $(shell which lzop))
 $(error "No lzop in $(PATH), consider doing apt-get install lzop")
 endif

如果要检查几个可执行文件,则可能要对which可执行文件使用foreach函数:

If you have several executables to check, then you might want to use a foreach function with the which executable:

EXECUTABLES = ls dd dudu lxop
K := $(foreach exec,$(EXECUTABLES),\
        $(if $(shell which $(exec)),some string,$(error "No $(exec) in PATH")))

请注意使用:=赋值运算符,以强制立即评估RHS表达式.如果您的Makefile更改了PATH,那么您将需要:

Note the use of the := assignment operator that is required in order to force immediate evaluation of the RHS expression. If your Makefile changes the PATH, then instead of the last line above you will need:

        $(if $(shell PATH=$(PATH) which $(exec)),some string,$(error "No $(exec) in PATH")))

这应该为您提供类似于以下内容的输出:

This should give you output similar to:

ads$ make
Makefile:5: *** "No dudu in PATH.  Stop.

这篇关于检查程序是否存在于Makefile中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆