3

How can I detect shell that will be used by gnu make? I want my make to be running on following platforms:

  • linux
  • windows
  • windows with cygwin (Note that on windows with cygwin the make uses cygwin shell)

Then I would like to detect if gcc is present on the system independently on OS.

Vit Bernatik
  • 3,566
  • 2
  • 34
  • 40

1 Answers1

3

So far I come up with this solution:

# 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})        
Vit Bernatik
  • 3,566
  • 2
  • 34
  • 40
  • 1
    Just to be clear, "not cmd.exe" does NOT imply bash. It might imply POSIX sh but that's not the same thing. Secondly, if you want to know if GCC is available why don't you just run it and see if it fails: `ifneq (,$(shell gcc --version))` ? – MadScientist Nov 22 '16 at 20:44
  • I'm aware there might be other shells. But typically linux shells are behaving quite similarly. At least in terms of supporting `which` and `/dev/null` which I'm using. As for not doing only `ifneq (,$(shell gcc --version))` it is because I do want to spare BFU from crazy messages. For example I would get something like: `process_begin: CreateProcess(NULL, gccx --version, ...) failed. make: M:32: pipe: Bad file descriptor`. Actually with make I'm alergic when ppl left me with some bullshit automatic message instead telling me in plain language what is wrong... – Vit Bernatik Nov 22 '16 at 20:57
  • 3
    Fine, but in fact bash has lots of extra features that aren't available in POSIX sh, and make always runs POSIX sh, never bash (unless you set the SHELL variable). It's very common for people to write make recipes using bash extensions, and then not be able to understand why they don't work. So, it's better to be clear about these things. – MadScientist Nov 22 '16 at 21:54