How do I create a single makefile for both 32- and

2019-01-22 13:52发布

问题:

I have a makefile that works transparently for Linux (x86_64) and OS X Intel (x86_64). This uses 64-bit specific GCC options.

Is there a way to adjust the makefile so that I could build for 32-bit and 64-bit OS X PPC (ppc, ppc64) without having to maintain separate, arch-specific makefiles — perhaps something like a pre-processor directive that can determine the architecture before building?

回答1:

ARCH := $(shell getconf LONG_BIT)

CPP_FLAGS_32 := -D32_BIT ...  Some 32 specific compiler flags ...
CPP_FLAGS_64 := -D64_BIT

CPP_FLAGS := $(CPP_FLAGS_$(ARCH))  ... all the other flags ...


回答2:

Try file inclusion. This is not part of the standard Makefile syntax (the one in the Single Unix v3 specification) but is widely supported. With GNU make, this looks like this:

include otherfile

With this, you could have an x86 Makefile like this:

ARCHFLAGS = -m64 -mtune=core2
include common.mk

and a PowerPC Makefile:

ARCHFLAGS = -mcpu=g3
include common.mk

and the bulk of your compilation rules will be in one file (common.mk), using $(ARCHFLAGS) where necessary.



回答3:

I think that you would achieve your goals with less work (and pain) by employing some kind of a build system such as cmake or GNU autotools.



回答4:

One way to work this is to:

  • have makefile fragments for each architecture (possibly cumulative): posix, win32, win32-gcc, etc.
  • have a script that runs and determines why fragments are adequate for the host
  • include those makefile fragments into the main makefile
  • allow manual override using environment variables (because things can sometimes be complicated; e.g., think of cross-compilers)

I've used this in small-to-medium size projects (2 or 3 persons working on code for a few years), and it fits the bill well.



回答5:

Have a look at this:

http://mad-scientist.net/make/multi-arch.html

It is somewhat dated but has good info that could be useful for you I suppose.