Check if a program exists from a Makefile

后端 未结 12 1097
时光取名叫无心
时光取名叫无心 2020-12-12 20:20

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 chec

12条回答
  •  醉酒成梦
    2020-12-12 20:29

    For me all above answers are based on linux and are not working with windows. I'm new to make so my approach may not be ideal. But complete example that works for me on both linux and windows is this:

    # detect what shell is used
    ifeq ($(findstring cmd.exe,$(SHELL)),cmd.exe)
    $(info "shell Windows cmd.exe")
    DEVNUL := NUL
    WHICH := where
    else
    $(info "shell Bash")
    DEVNUL := /dev/null
    WHICH := which
    endif
    
    # detect platform independently if gcc is installed
    ifeq ($(shell ${WHICH} gcc 2>${DEVNUL}),)
    $(error "gcc is not in your system PATH")
    else
    $(info "gcc found")
    endif
    

    optionally when I need to detect more tools I can use:

    EXECUTABLES = ls dd 
    K := $(foreach myTestCommand,$(EXECUTABLES),\
            $(if $(shell ${WHICH} $(myTestCommand) 2>${DEVNUL} ),\
                $(myTestCommand) found,\
                $(error "No $(myTestCommand) in PATH)))
    $(info ${K})        
    

提交回复
热议问题