Subject: Standard Make defines for X-compiling
To: None <tech-toolchain@NetBSD.ORG>
From: Jeremy Cooper <jeremy@broder.com>
List: tech-toolchain
Date: 06/02/1997 15:45:26
With so many supported hardware architectures with one unified OS
interface, it is puzzling why the current in-tree compiler, assembler and
linker are not easily configured for cross-compilation.  It's not too hard
to change the build process to do this.

Currently, the Makefiles for gcc, gas, and ld assume that the host
and target architecture are the same - determined by the MACHINE_ARCH
variable.  To make use of GNU's existing division between host and target,
I propose we use another variable, TARGET_MACHINE_ARCH, to indicate the
target architecture for which the application should emit.

Another problem which is not quite as easily solved is the use inclusion
of header files from the <machine/> directory.  This is an ambiguity that
needs to be resolved; Does <machine/> refer to the host or target arch?
Ideally, no code in these utilities should include these files under the
machine hierarchy to get the information they need.  Instead, these files
should be copied into the application's tree and included as <host/> or
<target/>.
 
I made a few changes to the Makefile for GAS to implement the first step,
and it works very well.  I have tested:

Host->Target
sparc->m68k
sparc->i386
m68k->sparc
m68k->i386

Cross-assembling to the PowerPC under any environment does not
work however, because it makes use of <machine/reloc.h>, which has the
ambiguity problem mentioned above.

Any comments/suggestions?
-J

diff -u -r1.21 Makefile
--- Makefile    1997/05/11 08:05:08     1.21
+++ Makefile    1997/05/31 21:19:00
@@ -1,20 +1,25 @@
 #      $NetBSD: Makefile,v 1.21 1997/05/11 08:05:08 mikel Exp $
 #      @(#)Makefile    6.1 (Berkeley) 3/3/91
+TARGET_MACHINE_ARCH?=  $(MACHINE_ARCH)
 
-.if exists(config/Makefile.$(MACHINE_ARCH))
-.include "config/Makefile.$(MACHINE_ARCH)"
+.if exists(config/Makefile.$(TARGET_MACHINE_ARCH))
+.include "config/Makefile.$(TARGET_MACHINE_ARCH)"
 .endif
 
 .if !defined (gas_hosttype)
 gas_hosttype=$(MACHINE_ARCH)
 .endif
 .if !defined (gas_target)
-gas_target=$(MACHINE_ARCH)
+gas_target=$(TARGET_MACHINE_ARCH)
 .endif
 .if !defined (gas_objformat)
 gas_objformat=aout
 .endif
 
+.if ($(TARGET_MACHINE_ARCH) != $(MACHINE_ARCH))
+CFLAGS+=       -DCROSS_COMPILE
+.endif
+
 ADDINCLUDE=-I${.OBJDIR}
 
 PROG=          as
@@ -36,12 +41,12 @@
 
 beforedepend ${PROG}: ${CONF_HEADERS}
 
-targ-cpu.h: Makefile config/Makefile.$(MACHINE_ARCH)
+targ-cpu.h: Makefile config/Makefile.$(TARGET_MACHINE_ARCH)
        @cmp -s $(.CURDIR)/config/tc-$(gas_target).h targ-cpu.h || \
            ( echo "updating ${.TARGET}..." ; /bin/rm -f targ-cpu.h ; \
            cp $(.CURDIR)/config/tc-$(gas_target).h targ-cpu.h )
 
-obj-format.h: Makefile config/Makefile.$(MACHINE_ARCH)
+obj-format.h: Makefile config/Makefile.$(TARGET_MACHINE_ARCH)
        @cmp -s $(.CURDIR)/config/obj-$(gas_objformat).h obj-format.h || \
            ( echo "updating ${.TARGET}..." ; /bin/rm -f obj-format.h ; \
            cp $(.CURDIR)/config/obj-$(gas_objformat).h obj-format.h )
@@ -57,13 +62,13 @@
            ( echo "updating ${.TARGET}..." ; /bin/rm -f host.h ; \
            cp $(config_hostfile) host.h )
 
-.if exists ($(.CURDIR)/config/te-$(MACHINE_ARCH).h)
-config_targenvfile=    $(.CURDIR)/config/te-$(MACHINE_ARCH).h
+.if exists ($(.CURDIR)/config/te-$(TARGET_MACHINE_ARCH).h)
+config_targenvfile=    $(.CURDIR)/config/te-$(TARGET_MACHINE_ARCH).h
 .else
 config_targenvfile=    $(.CURDIR)/config/te-generic.h
 .endif
 
-targ-env.h: Makefile config/Makefile.$(MACHINE_ARCH)
+targ-env.h: Makefile config/Makefile.$(TARGET_MACHINE_ARCH)
        @cmp -s $(config_targenvfile) targ-env.h || \
            ( echo "updating ${.TARGET}..." ; /bin/rm -f targ-env.h ; \
            cp $(config_targenvfile) targ-env.h )