views:

1198

answers:

4

I'm experimenting with an updated build system at work; currently, I'm trying to find a good way to set compiler & flags depending on the target platform.

What I would like to do is something like

switch $(PLATFORM)_$(BUILD_TYPE)
  case "Linux_x86_release"
     CFLAGS = -O3
  case "Linux_x86_debug"
     CFLAGS = -O0 -g
  case "ARM_release"
     CC = armcc
     AR = armlink
     CFLAGS = -O2 -fx
     ...

which is not supported by GNU Make. Now, my first thought was to just do

-include $(PLATFORM)_$(BUILD_TYPE)

which is a pretty decent solution, however, it makes it hard to get an overview of what differs between files, not to mention that I'm looking forward to writing & maintaining a good 60-80 files, each containing a set of variable definitions.

Does anyone happen to know a better way to accomplish this? I.e. setting a set of flags and other options based on another variable?

+4  A: 

Configuring such parameters would be the task of a configure script.

That being said, you can look into the syntax for conditionals and conditional functions. For example, you could try the following:

ifeq ($(PLATFORM)_$(BUILD_TYPE),Linux_x86_release)
    CFLAGS = -O3
endif
ifeq ($(PLATFORM)_$(BUILD_TYPE),Linux_x86_debug)
    CFLAGS = -O0 -g
endif
Bruno De Fraine
Yes, but this syntax is GNUmake-specific (the portable part of the makefile language is very small. Almost every non-trivial Makefile is non portable)
bortzmeyer
Well, either depend on gmake and use ifeq/endif, or depend on pmake and use #if/#endif, or depend on bmake and use .if/.endif, or depend on a Makefile-generator.
ephemient
+2  A: 

How about:

CFLAGS_Linux_x86_release        = -O3
CFLAGS_Linux_x86_debug          = -O0 -g


CFLAGS  = ${CFLAGS_${PLATFORM}_${BUILD}}
Martin York
+1  A: 

Switching to a system which does it for you (automake/autoconf) may be simpler...

bortzmeyer
+1  A: 

The Makefile used by git is a good example of a Makefile which does non-trivial configuration tasks in the Makefile itself (such as switching on the host type). It's actually quite readable and reasonably simple to use.

JesperE