1

I am trying to run a bash switch case in a make global variable. By itself, the switch run fine:

#!/bin/bash

case "$(uname -s)" in CYGWIN*|MINGW*|MSYS*) echo "Hi!";; esac;
# Hi!

I tried to use this answer as example: Assign a makefile variable value to a bash command result?, but when I put it inside the VARAIBLE := $(shell thing) I got an invalid syntax:

SHELL := /bin/bash
.SHELLFLAGS := -eu -o pipefail -c ${SHELLFLAGS}

IS_UNIX := $(shell case "$$(uname -s)" in CYGWIN*|MINGW*|MSYS*) printf "Hi!";; esac;)

all:
    echo IS_UNIX ${IS_UNIX}

# /bin/bash: -c: line 0: syntax error near unexpected token `newline'

The echo IS_UNIX and the printf "Hi!" are just dummy things to make this example simple. In my real code I am using them like this:

...
ifeq (${OS},Windows_NT)
    ifndef PYTHON_BIN
        PYTHON_BIN := $(shell which python)
        IS_UNIX := $(shell case "$$(uname -s)" in CYGWIN*|MINGW*|MSYS*) printf "Hi!";; esac;)

        ifneq (,${IS_UNIX})
            PYTHON_BIN := $(shell cygpath -w "${PYTHON_BIN}") 
        endif   
    endif
else
    ...
Evandro Coan
  • 8,560
  • 11
  • 83
  • 144

2 Answers2

3

The problem is the unbalanced parentheses in the case statement. Fortunately, since you're using bash, you can use the slightly modified case syntax and do:

IS_UNIX := $(shell case "$$(uname -s)" in (CYGWIN*|MINGW*|MSYS*) ...
William Pursell
  • 204,365
  • 48
  • 270
  • 300
  • 1
    This is not just bash. The balanced parens is part of the POSIX standard and every standard shell should support it. You should always use it. However there's another trivial solution: you can use `${ ... }` instead of `$( ... )` around make variables and functions; they behave identically and you won't have to worry about unbalanced parens. – MadScientist Mar 22 '20 at 18:55
1

I just make it work by putting the bash switch case in a variable before evaluating it:

SHELL := /bin/bash
.SHELLFLAGS := -eu -o pipefail -c ${SHELLFLAGS}

IS_UNIX_COMMAND := case $$(uname -s) in CYGWIN*|MINGW*|MSYS*) printf Hi;; esac;
IS_UNIX := $(shell ${IS_UNIX_COMMAND})

all:
    echo IS_UNIX ${IS_UNIX}

# echo IS_UNIX Hi
# IS_UNIX Hi
Evandro Coan
  • 8,560
  • 11
  • 83
  • 144