19

I have a command that works one way in OSX/Unix and another in Debian/Linux. I want to create a make file for my application but need to detect the OS and issue the command accordingly. How would I go about doing this?

LarsH
  • 27,481
  • 8
  • 94
  • 152
Usman Ismail
  • 17,999
  • 14
  • 83
  • 165

4 Answers4

32

You could use uname to do this. In your Makefile, you could write something like:

OS := $(shell uname)
ifeq $(OS) Darwin
# Run MacOS commands 
else
# check for Linux and run other commands
endif
flexlingie
  • 352
  • 2
  • 3
19

What worked for me

OS := $(shell uname)
ifeq ($(OS),Darwin)
  # Run MacOS commands
else
  # check for Linux and run other commands
endif
Noémien Kocher
  • 1,324
  • 15
  • 17
1

Use uname and an if statement as you do in shell commands, as suggested here.

.PHONY: foo

OS := $(shell uname)

foo:
    @if [ OS = "Darwin" ]; then\
      echo "Hello world";\
    fi
    @if [ OS = "Linux" ]; then\
      echo "Hello world";\
    fi

Note that the closing; and \ at each line are necessary

(This is because make interpret each line as a separate command unless it ends with )

-1

Use autotools. It's a standard way of building portable source code packages.

bonsaiviking
  • 5,825
  • 1
  • 20
  • 35