Usman Ismail
Usman Ismail

Reputation: 18659

How to detect OS in a make file?

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?

Upvotes: 21

Views: 24183

Answers (4)

Gabriel Braico Dornas
Gabriel Braico Dornas

Reputation: 665

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 )

Upvotes: 3

Noémien Kocher
Noémien Kocher

Reputation: 1354

What worked for me

OS := $(shell uname)
ifeq ($(OS),Darwin)
  # Run MacOS commands
else
  # check for Linux and run other commands
endif

Upvotes: 19

flexlingie
flexlingie

Reputation: 362

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

Upvotes: 33

bonsaiviking
bonsaiviking

Reputation: 5995

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

Upvotes: -1

Related Questions