On 2021-02-02, Steve Keller <
[email protected]> wrote:
How should I write a Makefile so that I can compile one of a number of
source files, depending on the architecture the code is compiled for.
Say, I have a source foo.c and several files implementing a function bar(), in C and in assembly language for several architectures, e.g. bar.c, bar-i386.s, bar-amd64.s, and bar-mips.s.
Currently, my Makefile looks similar to this
foo: foo.o bar.o
$(CC) $(LDFLAGS) -o foo foo.o bar.o $(LDLIBS)
ARCH = $(shell uname -m)
Inquire the cross-compiler, not uname.
If it is GCC with a GNU linker, this couild be done, among other
possibilities.
Capture the output of this command, and parse it:
$(CC) -Wl,--print-output-format 2> /dev/null
I.e. we ask the linker (indirectly thorugh the compiler invocation)
what is the output format, which produces a string like elf32-i386.
This works as long as I compile natively but not for cross-compilation. So my questions is how to write the Makefile so that I can call
make CC=mips-linux-gcc
Well, here is a possible answer. Just take $(basename $(CC)) and parse
that.
and have the correct assembly file compiled and linked into the executable.
If the amount of assembly language is small, you can put it all in one
file. There are certain benefits in doing so, because the code may
be able to use some shared #define macros.
An example of this is the jmp.S file in my TXR project:
http://www.kylheku.com/cgit/txr/tree/jmp.S
It has i386, i686, arm, aarch64, PPC and MIPS all in one file.
Another idea:
ifeq ($(ARCH),x86_64)
bar.o: bar-amd64.o
cp $< $@
else ifeq ($(ARCH),i686)
bar.o: bar-i386.o
cp $< $@
else ifeq ($(ARCH),mips)
bar.o: bar-mips.o
cp $< $@
This copy pointless: if you know what .o is needed, then you pull it
into OBJS:
ifeq ($(ARCH),x86_64)
OBJS += bar-amd64.o
else ifeq ...
Or even:
OBJS = foo.o ... bar-$(ARCH).o ...
using an adjusted ARCH which matches your convention, calculated
elsewhere.
--
TXR Programming Language:
http://nongnu.org/txr
Cygna: Cygwin Native Application Library:
http://kylheku.com/cygnal
--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)