I don’t know how to execute a command stored as a variable or how to use ifeq inside of a target, so I have a very redundant Makefile at the moment!
Ideally I’d like to have just one target (all) which would run the stored command on Mac and run it twice on Linux, once with -m32 and once with -m64.
all:
echo PLEASE SELECT OS, e.g. make linux
exit 1
mac:
gcc $(SHARED_OPT) $(GENERAL_CFLAGS) $(PLATFORM_CFLAGS) -o $(BUILD_DIR)$(BUILD_NAME) $(SOURCE) $(LIBRARIES)
linux:
gcc $(SHARED_OPT) $(GENERAL_CFLAGS) $(PLATFORM_CFLAGS) -o $(BUILD_DIR)$(BUILD_NAME64) $(SOURCE) $(LIBRARIES64) -m64
gcc $(SHARED_OPT) $(GENERAL_CFLAGS) $(PLATFORM_CFLAGS) -o $(BUILD_DIR)$(BUILD_NAME) $(SOURCE) $(LIBRARIES) -m32
UPDATE: This is what I ended up with, after reading the various suggestsions. (Yes I know I should use autoconf . . .) Thanks everyone for your help!
ifeq($(PLATFORM), Linux)
COMMON = -pthread -fPIC
PLATFORM_CFLAGS = $(COMMON) -m32
PLATFORM_CFLAGS64 = $(COMMON) -m64
endif
ifeq ($(PLATFORM), Darwin)
PLATFORM_CFLAGS = -arch i386 -arch ppc -arch x86_64 -mmacosx-version-min=10.5 -isysroot /Developer/SDKs/MacOSX10.5.sdk
endif
all: $(PLATFORM)_all
Darwin_all:
mkdir -p ../../../../tmp
gcc $(SHARED_OPT) $(GENERAL_CFLAGS) $(PLATFORM_CFLAGS) -o $(BUILD_DIR)$(BUILD_NAME) $(SOURCE) $(LIBRARIES)
Linux_all: Darwin_all
gcc $(SHARED_OPT) $(GENERAL_CFLAGS) $(PLATFORM_CFLAGS64) -o $(BUILD_DIR)$(BUILD_NAME64) $(SOURCE) $(LIBRARIES64)
You make macros do most of the work, noting that you should use $(CC) rather than gcc.
Oh, and look, the commands are the same for Linux and MacOS X now…so we can do:
Gosh, it is hard work writing $(XXX) instead of ${XXX} as I normally do in my makefiles.
Basically, we apply DRY (Don’t Repeat Yourself) by making names boringly systematic. Makefiles should not be exciting.
If you still want to have a difference between your platforms, then you can do something along the lines suggested by Ivan Andrus. GNU Make allows you to evaluate shell commands, so:
If you feel you can’t rely on GNU Make, then:
By default this will compile both 32-bit and 64-bit versions.
If you want 32-bit only or 64-bit only, run the appropriate one of these two: