Subject: Re: Cross compiling NetBSD
To: None <Ian.Dall@dsto.defence.gov.au>
From: Gordon W. Ross <gwr@mc.com>
List: port-sun3
Date: 09/04/1996 17:59:02
> Date: Thu, 29 Aug 1996 09:12:08 +0930
> From: Ian Dall <Ian.Dall@dsto.defence.gov.au>

> I have built a cross compiler + ld + as to cross compile for the sun3
> on my pc532 in the hope of speeding up my compilations.  [...]

> Now I would like to build a kernel and maybe other things from the
> NetBSD source tree.  I have a sun3 tree with the sun3 include files,
> libraries etc. My question is: Is there a preferred way to specify a
> cross compiler when building?
> 
> A lot of things will build if you just do "make CC=sun3-gcc", but you
> can get caught out if the build process needs to compile and run a
> native executeable. Also there can be problems at the install stage
> where if you are not careful your native executables get overwritten
> with foreign executeables!
> 
> I seem to recall someone (Gordon?) saying they cross compiled Sun3
> kernels on a sparc, so I am hoping that the build process has
> been designed with support for cross compiling.
> 
> Is config documented anywhere? And I don't mean "man config, I
> mean to syntax and semantics of the config files!
> 
> Ian

The way I deal with those issues is basically to cheat.
I do these steps on my NetBSD test machine: (native)
	cd ../compile/GENERIC
	make assym.h
Then the rest of the make can easily be done elsewhere.
(I use my SPARCstation for kernel builds! 8^)  The only
cross-development tools I built are GNU-cc, GNU-binutils,
and the NetBSD make.  The latter is easy thanks to the
Makefile.boot provided.  The GNU stuff does not quite work
out of the box for host=sparc, target=m68k-*-netbsd so I
just used a "standard" m68k-*-sun3 configured gcc.  That
will produce Sun magic numbers, but for the kernel (all I
care to build with this) one can just hack the header.

Here is my /usr/share/mk/sys.mk file on the build machine.
[ My cross tools are installed as m68k-sun-sunos4-* ]


#	$NetBSD: sys.mk,v hacked...$
#	@(#)sys.mk	5.11 (Berkeley) 3/13/91

unix=		We run NetBSD.

.SUFFIXES: .out .a .ln .o .c .cc .C .F .f .r .y .l .s .S .cl .p .h .sh .m4

.LIBS:		.a

TOOLPREFIX?=m68k-sun-sunos4-

AR?=		${TOOLPREFIX}ar
ARFLAGS?=	rl
RANLIB?=	${TOOLPREFIX}ranlib

AS?=		${TOOLPREFIX}as
AFLAGS?=
COMPILE.s?=	${CC} ${AFLAGS} -c
LINK.s?=	${CC} ${AFLAGS} ${LDFLAGS}
COMPILE.S?=	${CC} ${AFLAGS} ${CPPFLAGS} -c
LINK.S?=	${CC} ${AFLAGS} ${CPPFLAGS} ${LDFLAGS}

CC?=		${TOOLPREFIX}gcc
CFLAGS?=	-O
COMPILE.c?=	${CC} ${CFLAGS} ${CPPFLAGS} -c
LINK.c?=	${CC} ${CFLAGS} ${CPPFLAGS} ${LDFLAGS}

CXX?=		${TOOLPREFIX}g++
CXXFLAGS?=	${CFLAGS}
COMPILE.cc?=	${CXX} ${CXXFLAGS} ${CPPFLAGS} -c
LINK.cc?=	${CXX} ${CXXFLAGS} ${CPPFLAGS} ${LDFLAGS}

CPP?=		bsd_cpp
CPPFLAGS?=	

FC?=		${TOOLPREFIX}f77
FFLAGS?=	-O
RFLAGS?=
COMPILE.f?=	${FC} ${FFLAGS} -c
LINK.f?=	${FC} ${FFLAGS} ${LDFLAGS}
COMPILE.F?=	${FC} ${FFLAGS} ${CPPFLAGS} -c
LINK.F?=	${FC} ${FFLAGS} ${CPPFLAGS} ${LDFLAGS}
COMPILE.r?=	${FC} ${FFLAGS} ${RFLAGS} -c
LINK.r?=	${FC} ${FFLAGS} ${RFLAGS} ${LDFLAGS}

LEX?=		lex
LFLAGS?=
LEX.l?=		${LEX} ${LFLAGS}

LD?=		${TOOLPREFIX}ld
LDFLAGS?=

LINT?=		lint
LINTFLAGS?=	-chapbx

MAKE?=		bmake

MKDEP?= 	bsd_mkdep

PC?=		${TOOLPREFIX}pc
PFLAGS?=
COMPILE.p?=	${PC} ${PFLAGS} ${CPPFLAGS} -c
LINK.p?=	${PC} ${PFLAGS} ${CPPFLAGS} ${LDFLAGS}

SHELL?=		sh

STRIP?= 	${TOOLPREFIX}strip --strip-debug

YACC?=		yacc
YFLAGS?=	-d
YACC.y?=	${YACC} ${YFLAGS}

# C
.c:
	${LINK.c} -o ${.TARGET} ${.IMPSRC} ${LDLIBS}
.c.o:
	${COMPILE.c} ${.IMPSRC}
.if (${MACHINE_ARCH} != "alpha")
.c.a:
	${COMPILE.c} ${.IMPSRC}
	${AR} ${ARFLAGS} $@ $*.o
	rm -f $*.o
.endif
.c.ln:
	${LINT} ${LINTFLAGS} ${CFLAGS:M-[IDU]*} -i ${.IMPSRC}

# C++
.cc:
	${LINK.cc} -o ${.TARGET} ${.IMPSRC} ${LDLIBS}
.cc.o:
	${COMPILE.cc} ${.IMPSRC}
.cc.a:
	${COMPILE.cc} ${.IMPSRC}
	${AR} ${ARFLAGS} $@ $*.o
	rm -f $*.o

.C:
	${LINK.cc} -o ${.TARGET} ${.IMPSRC} ${LDLIBS}
.C.o:
	${COMPILE.cc} ${.IMPSRC}
.C.a:
	${COMPILE.cc} ${.IMPSRC}
	${AR} ${ARFLAGS} $@ $*.o
	rm -f $*.o

# Fortran/Ratfor
.f:
	${LINK.f} -o ${.TARGET} ${.IMPSRC} ${LDLIBS}
.f.o:
	${COMPILE.f} ${.IMPSRC}
.f.a:
	${COMPILE.f} ${.IMPSRC}
	${AR} ${ARFLAGS} $@ $*.o
	rm -f $*.o

.F:
	${LINK.F} -o ${.TARGET} ${.IMPSRC} ${LDLIBS}
.F.o:
	${COMPILE.F} ${.IMPSRC}
.F.a:
	${COMPILE.F} ${.IMPSRC}
	${AR} ${ARFLAGS} $@ $*.o
	rm -f $*.o

.r:
	${LINK.r} -o ${.TARGET} ${.IMPSRC} ${LDLIBS}
.r.o:
	${COMPILE.r} ${.IMPSRC}
.r.a:
	${COMPILE.r} ${.IMPSRC}
	${AR} ${ARFLAGS} $@ $*.o
	rm -f $*.o

# Pascal
.p:
	${LINK.p} -o ${.TARGET} ${.IMPSRC} ${LDLIBS}
.p.o:
	${COMPILE.p} ${.IMPSRC}
.p.a:
	${COMPILE.p} ${.IMPSRC}
	${AR} ${ARFLAGS} $@ $*.o
	rm -f $*.o

# Assembly
.s:
	${LINK.s} -o ${.TARGET} ${.IMPSRC} ${LDLIBS}
.s.o:
	${COMPILE.s} ${.IMPSRC}
.s.a:
	${COMPILE.s} ${.IMPSRC}
	${AR} ${ARFLAGS} $@ $*.o
	rm -f $*.o
.S:
	${LINK.S} -o ${.TARGET} ${.IMPSRC} ${LDLIBS}
.S.o:
	${COMPILE.S} ${.IMPSRC}
.S.a:
	${COMPILE.S} ${.IMPSRC}
	${AR} ${ARFLAGS} $@ $*.o
	rm -f $*.o

# Lex
.l:
	${LEX.l} ${.IMPSRC}
	${LINK.c} -o ${.TARGET} lex.yy.c ${LDLIBS} -ll
	rm -f lex.yy.c
.l.c:
	${LEX.l} ${.IMPSRC}
	mv lex.yy.c ${.TARGET}
.l.o:
	${LEX.l} ${.IMPSRC}
	${COMPILE.c} -o ${.TARGET} lex.yy.c 
	rm -f lex.yy.c

# Yacc
.y:
	${YACC.y} ${.IMPSRC}
	${LINK.c} -o ${.TARGET} y.tab.c ${LDLIBS}
	rm -f y.tab.c
.y.c:
	${YACC.y} ${.IMPSRC}
	mv y.tab.c ${.TARGET}
.y.o:
	${YACC.y} ${.IMPSRC}
	${COMPILE.c} -o ${.TARGET} y.tab.c
	rm -f y.tab.c

# Shell
.sh:
	rm -f ${.TARGET}
	cp ${.IMPSRC} ${.TARGET}