views:

339

answers:

2

How do I make a makefile that works on AIX, Linux and SunOS and has the ability to provide different compiler options for each environment?

I have access to an environment variable which describes the OS, but the AIX make utility does not like ifeq, so I can't do something like:

ifeq($(OS), AIX)
    CFLAGS = $(CFLAGS) <IBM compiler options>
endif
A: 

The portability of a Makefile is not directly related to the operating system, but to the implementation of make on the platform in question. (So there is an indirect relationship in that the implementation of make may be guessed (but NOT accurately) from the OS.) In general, this is a difficult problem for which many solutions have been proposed. You might want to look into automake, which will generate portable makefiles for you. However, automake's solution to the problem of setting flags for different unixen may not appeal to you as the solution is (basically) "don't do it". Rather than setting options based on the platform, the philosophy is to determine what flags are needed based on the functionality provided by the host or by the user at configure time. One convenient autoconf/automake based solution for the problem of assigning flags based on platform is to have a distinct file for each of your platforms which assigns CFLAGS at configure time, and have the file be specified in the CONFIG_SITE environment variable of the user running configure. You can assign CONFIG_SITE in the login script of the user based on the platform. (ie, push the problem away from configure/make and into the login setup) This makes the assignment transparent to the user building the software. (transparent but easily overridden).

William Pursell
I am hoping for a simpler solution than autoconf/automake
frankster
You could install a consistent version of make on all the machines.
William Pursell
I only have control over some of the machines
frankster
Others don't have any control on the installed software at all. Some organizations impose strict rules on what can be installed, and any innovation may involve big expenses in certification, etc.
Leonardo Herrera
+4  A: 

You can use a construct like this:

CFLAGS_AIX = AIX flags
CFLAGS_Linux = Linux flags
CFLAGS_SunOS = SunOS flags
CFLAGS = $(CFLAGS_$(OS))

mark4o
CFLAGS = $(CFLAGS_$(OS)) on my AIX system.
Leonardo Herrera
@Leonardo: yes, corrected.
mark4o