17

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
  • 26,606
  • 8
  • 88
  • 145
Usman Ismail
  • 17,055
  • 14
  • 78
  • 161
  • possible duplicate of [OS detecting makefile](http://stackoverflow.com/questions/714100/os-detecting-makefile) – Anko Dec 19 '13 at 01:00

4 Answers4

30

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
  • 332
  • 2
  • 3
17

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,294
  • 15
  • 17
0

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 )

0

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

bonsaiviking
  • 5,557
  • 1
  • 19
  • 34