Subject: NetBSD cross compile FreeBSD->NetBSD/PowerPC brain dump.
To: None <tech-toolchain@netbsd.org>
From: Andrew Cagney <ac131313@cygnus.com>
List: tech-toolchain
Date: 08/16/1999 15:46:40
This is a multi-part message in MIME format.
--------------1BBC34EDE3C1DCA14C91C0D8
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Just BTW,

Below are random notes on cross compiling NetBSD (kernel, lib,
usr/bin/printenv) from a FreeBSD host.
At this point I'm just brain-dumping what I've got here.  Perhaphs,
later, I'll go back through and clean it up and turn it into something
useful.

The attached file ``CROSS-COMPILING'' looks like a shell script but
isn't.  It is me cut/pasting commands as I've typed them in.  In that
script you'll notice I've quoted someone elses e-mail.  Sorry I've lost
who's it was :-(.  I've also drawn on earlier notes I'd made on this
task.

Broadly:

1.	You'll need to modify / update the src/gnu/dist directory
	Per several comments some of the files are missing:

	The most annoying one for me was the ChangeLog's. Those
	are really useful when trying to figure out the heritage
	of a given GNU toolchain.

	cvs update complains about how the following mysteriously
	appearing:
? ltmain.sh
? ylwrap
? mkinstalldirs
? mkdep
? ltconfig (actually the file under CVS is empty)

	I've attached a patch for the files I changed.


2.	In my wanderings I tweeked a couple other files and I've
	attached self documenting patches for them :-^

2.	You'll need to work through (perhaphs decode is a better
	description of the task) that attached CROSS-COMPILING.

Of all the tasks, the most obvious thing to do first is to eliminate the
need for patches.  Any suggestions on where they should be sent?

enjoy,
	Andrew

[The target boards are definitly embedded.]
--------------1BBC34EDE3C1DCA14C91C0D8
Content-Type: text/plain; charset=us-ascii;
 name="CROSS-COMPILING"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="CROSS-COMPILING"

#---------------------

exit 1

MACHINE=macppc
MACHINE_ARCH=powerpc
installdir=/home/scratch/H-i386-unknown-freebsdelf3.2/T-${MACHINE_ARCH}-netbsd/B-NetBSD-current
installbin=${installdir}/${MACHINE_ARCH}-netbsd/bin
bsdroot=/home/scratch/NetBSD/R-${MACHINE}
srcdir=/home/scratch/NetBSD/src


#> 1) Build a cross compiler. If you're lucky, there will be a package
#> for what you want in /usr/pkgsrc/cross. (I was lucky, so I don't know
#> what to do if you're not.)

(
  test -d ${srcdir} || exit 1
  test -d X-${MACHINE_ARCH}-netbsd || mkdir X-${MACHINE_ARCH}-netbsd
  cd X-${MACHINE_ARCH}-netbsd || exit 1

  test -r config.status \
    || with_newlib=yes ${srcdir}/gnu/dist/configure \
	--target=${MACHINE_ARCH}-netbsd \
	--prefix=${installdir}

  gmake M4=gm4 LANGUAGES="c c++" all-gcc all-gas all-ld all-binutils || exit 1
  gmake M4=gm4 LANGUAGES="c c++" install-gcc install-gas install-ld install-binutils || exit 1

  test -r ${installbin}/size \
    || ln -s ${installdir}/bin/${MACHINE_ARCH}-netbsd-size ${installbin}/size
)


#---------------------

# Assumes NetBSD's berkley make!
(
  cd ${srcdir}/usr.bin/make
  gmake -f Makefile.boot
  cp bmake ${installbin}
  gmake -f Makefile.boot clean
)



# Assumes NetBSD configure
(
  cd ${srcdir}/usr.sbin/config
  gmake -f Makefile.boot
  cp config ${installbin}
  gmake -f Makefile.boot clean
)


# Assumes NetBSD mkdep
(
  PATH=${installbin}:${install}/bin:$PATH
  cd ${srcdir}/usr.bin/mkdep
  bmake mkdep
  cp mkdep ${installbin}
  bmake clean
)


> 2) Cross-compiling a kernel is really easy and just works. configure
> it like you would normally and then run (eg)
> 
>    env PATH=/usr/pkg/cross/${MACHINE_ARCH}-netbsd/bin:$PATH MACHINE=${MACHINE} MACHINE_ARCH=${MACHINE_ARCH} make depend
>    env PATH=/usr/pkg/cross/${MACHINE_ARCH}-netbsd/bin:$PATH MACHINE=${MACHINE} MACHINE_ARCH=${MACHINE_ARCH} make all
> 
> (You may not need the "env" depending on your choice of root shell ;-)

(
  cd ${srcdir}/sys/arch/${MACHINE}/conf/
  PATH=${installbin}:${install}/bin:$PATH
  config GENERIC
)

(
  cd ${srcdir}/sys/arch/${MACHINE}/compile/GENERIC
  PATH=${installbin}:${install}/bin:$PATH
  bmake \
    CC=${MACHINE_ARCH}-netbsd-gcc \
    MACHINE=${MACHINE} \
    MACHINE_ARCH=${MACHINE_ARCH} \
    -m ${srcdir}/share/mk \
    depend
  bmake \
    CC=${MACHINE_ARCH}-netbsd-gcc \
    MACHINE=${MACHINE} \
    MACHINE_ARCH=${MACHINE_ARCH} \
    -m ${srcdir}/share/mk \
    all
)


> 

#---------------------



> 3) You can try to build userland the same way. You'll need a DESTDIR
> to build into. You'll probably want to use obj dirs as well, with
> OBJMACHINE set in /etc/mk.conf, if you're going to be compiling for
> both your host architecture and your target architecture from the same
> tree.

# A cheap imitation of chown
cat <<EOF > ${installbin}/chown
#!/bin/sh
echo Ignoring "$@"
exit 0
EOF
chmod a+x ${installbin}/chown

# A cheap imitation of chgrp
cat <<EOF > ${installbin}/chgrp
#!/bin/sh
echo Ignoring "$@"
exit 0
EOF
chmod a+x ${installbin}/chgrp

# A cheap imitation of install
cat <<EOF > ${installbin}/install
#!/bin/sh
while [ \$# -gt 0 ]
do
  case "\$1" in
  -m ) shift ; echo "Ignoring -m \$1" 1>&2 ; shift ;; # -m <mode>
  -o ) shift ; echo "Ignoring -o \$1" 1>&2 ; shift ;; # -o <owner>
  -g ) shift ; echo "Ignoring -g \$1" 1>&2 ; shift ;; # -g <group>
  -d ) shift ; dir=true ;; 
  -* ) args="\$args \$1" ; shift ;;
  * )
    if [ "\$dir" ]; then
      set -x
      exec mkdir -p \$1
    fi
    args="\$args \$1" ; shift ;;
  esac
done
PATH=\`echo \$PATH | sed -e 's/^[^:]*://'\` ; export PATH
set -x
install \$args \$1
EOF
chmod a+x ${installbin}/install

# The dest tree - BSD doesn't appear to populate it
mkdir -p ${bsdroot}/usr/include
mkdir -p ${bsdroot}/usr/include/rpcsvc
mkdir -p ${bsdroot}/usr/include/arpa
mkdir -p ${bsdroot}/usr/include/protocols
mkdir -p ${bsdroot}/usr/include/rpc
mkdir -p ${bsdroot}/usr/include/g++
mkdir -p ${bsdroot}/usr/include/g++/std
mkdir -p ${bsdroot}/usr/include/objc

# Install all the include files...
(
  PATH=${installbin}:${install}/bin:$PATH
  cd ${srcdir}
  bmake \
    CC=${MACHINE_ARCH}-netbsd-gcc \
    MACHINE=${MACHINE} \
    MACHINE_ARCH=${MACHINE_ARCH} \
    CONFIG_NM=powerpc-netbsd-nm  \
    CXX="powerpc-netbsd-c++" \
    -m ${srcdir}/share/mk \
    includes
)



## There is something of a chicken/egg with the tmac files... and trying
## to build the library.  First idea is to install share => bad idea!!!
#
#mkdir -p ${bsdroot}/usr/share/dict
#mkdir -p ${bsdroot}/usr/share/doc/psd/05.sysman
#mkdir -p ${bsdroot}/usr/share/doc/psd/20.ipctut
#mkdir -p ${bsdroot}/usr/share/doc/psd/21.ipc
#mkdir -p ${bsdroot}/usr/share/doc/smm/01.setup
#mkdir -p ${bsdroot}/usr/share/doc/smm/04.quotas
#mkdir -p ${bsdroot}/usr/share/doc/smm/05.fastfs
#mkdir -p ${bsdroot}/usr/share/doc/smm/06.nfs
#mkdir -p ${bsdroot}/usr/share/doc/smm/18.net
#mkdir -p ${bsdroot}/usr/share/doc/usd/18.msdiffs
#mkdir -p ${bsdroot}/usr/share/doc/usd/19.memacros
#mkdir -p ${bsdroot}/usr/share/doc/usd/20.meref
#mkdir -p ${bsdroot}/usr/share/examples/amd
#mkdir -p ${bsdroot}/usr/share/examples/emul/svr4/etc
#mkdir -p ${bsdroot}/usr/share/examples/emul/ultrix/etc
#mkdir -p ${bsdroot}/usr/share/examples/ftpd
#mkdir -p ${bsdroot}/usr/share/examples/supfiles
#mkdir -p ${bsdroot}/usr/share/man
#mkdir -p ${bsdroot}/usr/share/man/man1/atari
#mkdir -p ${bsdroot}/usr/share/man/cat1/atari
## At this point we give up:
##install -c edahdi.cat1 /home/scratch/NetBSD/R-macppc/usr/share/man/cat1/atari/edahdi.0
##install: edahdi.cat1: No such file or directory
#
#
#/* The Share */
#(
#  PATH=${installbin}:${install}/bin:$PATH
#  cd ${srcdir}
#  bmake \
#    CC=${MACHINE_ARCH}-netbsd-gcc \
#    MACHINE=${MACHINE} \
#    MACHINE_ARCH=${MACHINE_ARCH} \
#    CONFIG_NM=powerpc-netbsd-nm  \
#    CXX="powerpc-netbsd-c++" \
#    -m ${srcdir}/share/mk \
#    install-share
#)


/* The Library */
mkdir -p ${bsdroot}/usr/lib
mkdir -p ${bsdroot}/var/db
mkdir -p ${bsdroot}/usr/share/doc/psd/19.curses
mkdir -p ${bsdroot}/usr/share/misc
(
  PATH=${installbin}:${install}/bin:$PATH
  cd ${srcdir}/lib
  bmake \
    CC=${MACHINE_ARCH}-netbsd-gcc \
    MACHINE=${MACHINE} \
    MACHINE_ARCH=${MACHINE_ARCH} \
    CONFIG_NM=${MACHINE_ARCH}-netbsd-nm  \
    CXX="${MACHINE_ARCH}-netbsd-c++" \
    NOPIC=1 \
    NOMAN=1 \
    NOPROFILE=1 \
    NOLINT=1 \
    NOTAGS=1 \
    NOGCCERROR=1 \
    CPP=${installdir}/lib/gcc-lib/${MACHINE_ARCH}-netbsd/egcs-2.91.66/cpp \
    AS=${MACHINE_ARCH}-netbsd-as \
    LD=${MACHINE_ARCH}-netbsd-ld \
    -m ${srcdir}/share/mk \
    all install
)


> 
> There are a handful of programs that build small tools as part of
> their build process which then get run to generate files. This
> includes at least sh, tn3270, gcc, and a bunch of things in games.
> Some of them are set up to use variables like HOST_CC, HOST_LD, etc to
> build the local tools, so if you're mucking with your path as above to
> get the cross-compilers by default, you'll have to define each of
> those additionally by hand. Except then they still won't work because
> HOST_CC will end up invoking "as" out of your PATH and get the wrong
> one. The alternative is to set CC, LD, AS, etc, etc, etc, to point
> into the cross-compiler path and not change PATH, but that seems like
> way too much work. I'm betting there's an easier way I just didn't
> notice. (Anyone?)
> 
> I ended up just letting the build bomb out and then building the host
> programs by hand.
> 
> Oh, also, when I finished (cross-compiling a powerpc userland from
> i386), only the statically-linked programs ended up working. So then I
> gave up and ftped installation tarballs.


(
  PATH=${installbin}:${install}/bin:$PATH
  cd ${srcdir}/usr.bin/printenv
  bmake \
    CC=${MACHINE_ARCH}-netbsd-gcc \
    MACHINE=${MACHINE} \
    MACHINE_ARCH=${MACHINE_ARCH} \
    CONFIG_NM=${MACHINE_ARCH}-netbsd-nm  \
    CXX="${MACHINE_ARCH}-netbsd-c++" \
    NOPIC=1 \
    NOMAN=1 \
    NOPROFILE=1 \
    NOLINT=1 \
    NOTAGS=1 \
    NOGCCERROR=1 \
    CPP=${installdir}/lib/gcc-lib/${MACHINE_ARCH}-netbsd/egcs-2.91.66/cpp \
    AS=${MACHINE_ARCH}-netbsd-as \
    LD=${MACHINE_ARCH}-netbsd-ld \
    -m ${srcdir}/share/mk \
    all
#    powerpc-eabi-run printenv
)

--------------1BBC34EDE3C1DCA14C91C0D8
Content-Type: text/plain; charset=us-ascii;
 name="gnu.dist.diffs"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="gnu.dist.diffs"

Index: ./gnu/dist/gcc/configure
===================================================================
RCS file: /cvsroot/gnusrc/gnu/dist/gcc/configure,v
retrieving revision 1.15
diff -p -r1.15 configure
*** configure	1999/05/27 14:45:00	1.15
--- configure	1999/08/16 05:24:20
*************** fi
*** 5445,5451 ****
  
  # Figure out what assembler alignment features are present.
  echo $ac_n "checking assembler alignment features""... $ac_c" 1>&6
! echo "configure:5413: checking assembler alignment features" >&5
  gcc_cv_as=
  gcc_cv_as_alignment_features=
  gcc_cv_as_gas_srcdir=`echo $srcdir | sed -e 's,gcc$,gas,'`
--- 5445,5451 ----
  
  # Figure out what assembler alignment features are present.
  echo $ac_n "checking assembler alignment features""... $ac_c" 1>&6
! echo "configure:5449: checking assembler alignment features" >&5
  gcc_cv_as=
  gcc_cv_as_alignment_features=
  gcc_cv_as_gas_srcdir=`echo $srcdir | sed -e 's,gcc$,gas,'`
Index: ./gnu/dist/gcc/cplus-dem.c
===================================================================
RCS file: /cvsroot/gnusrc/gnu/dist/gcc/cplus-dem.c,v
retrieving revision 1.3
diff -p -r1.3 cplus-dem.c
*** cplus-dem.c	1999/02/03 18:07:45	1.3
--- cplus-dem.c	1999/08/16 05:24:49
*************** fatal (str)
*** 4398,4404 ****
  
  PTR
  xmalloc (size)
!   size_t size;
  {
    register PTR value = (PTR) malloc (size);
    if (value == 0)
--- 4398,4404 ----
  
  PTR
  xmalloc (size)
!   unsigned long size;
  {
    register PTR value = (PTR) malloc (size);
    if (value == 0)
*************** xmalloc (size)
*** 4409,4415 ****
  PTR
  xrealloc (ptr, size)
    PTR ptr;
!   size_t size;
  {
    register PTR value = (PTR) realloc (ptr, size);
    if (value == 0)
--- 4409,4415 ----
  PTR
  xrealloc (ptr, size)
    PTR ptr;
!   unsigned long size;
  {
    register PTR value = (PTR) realloc (ptr, size);
    if (value == 0)
Index: ./gnu/dist/gcc/libgcc2.c
===================================================================
RCS file: /cvsroot/gnusrc/gnu/dist/gcc/libgcc2.c,v
retrieving revision 1.5
diff -p -r1.5 libgcc2.c
*** libgcc2.c	1999/01/28 16:05:26	1.5
--- libgcc2.c	1999/08/16 05:25:14
*************** __eprintf (const char *string, const cha
*** 1417,1423 ****
--- 1417,1425 ----
  
  #ifdef L_bb
  
+ #ifndef inhibit_libc
  #include <sys/time.h>
+ #endif
  
  /* Structure emitted by -a  */
  struct bb
Index: ./gnu/dist/gcc/scan.h
===================================================================
RCS file: /cvsroot/gnusrc/gnu/dist/gcc/scan.h,v
retrieving revision 1.1.1.2
diff -p -r1.1.1.2 scan.h
*** scan.h	1998/08/16 17:38:21	1.1.1.2
--- scan.h	1999/08/16 05:25:16
*************** extern int skip_spaces _PARAMS((FILE *, 
*** 57,64 ****
  extern int scan_ident _PARAMS((FILE *, sstring *, int));
  extern int scan_string _PARAMS((FILE *, sstring *, int));
  extern int read_upto _PARAMS((FILE *, sstring *, int));
! extern char *xmalloc _PARAMS((unsigned));
! extern char *xrealloc _PARAMS((char *, unsigned));
  extern unsigned long hash _PARAMS((const char *));
  extern void recognized_function _PARAMS((char *, int, int, char *, int, int, char *, int));
  extern void recognized_extern _PARAMS((char *, int, char *, int));
--- 57,64 ----
  extern int scan_ident _PARAMS((FILE *, sstring *, int));
  extern int scan_string _PARAMS((FILE *, sstring *, int));
  extern int read_upto _PARAMS((FILE *, sstring *, int));
! extern PTR xmalloc _PARAMS((unsigned long));
! extern PTR xrealloc _PARAMS((PTR, unsigned long));
  extern unsigned long hash _PARAMS((const char *));
  extern void recognized_function _PARAMS((char *, int, int, char *, int, int, char *, int));
  extern void recognized_extern _PARAMS((char *, int, char *, int));
Index: ./gnu/dist/gcc/ginclude/stddef.h
===================================================================
RCS file: /cvsroot/gnusrc/gnu/dist/gcc/ginclude/stddef.h,v
retrieving revision 1.1.1.2
diff -p -r1.1.1.2 stddef.h
*** stddef.h	1998/08/16 17:46:02	1.1.1.2
--- stddef.h	1999/08/16 05:25:32
***************
*** 25,31 ****
--- 25,33 ----
  /* On 4.3bsd-net2, make sure ansi.h is included, so we have
     one less case to deal with in the following.  */
  #if defined (__BSD_NET2__) || defined (____386BSD____) || defined (__FreeBSD__) || defined(__NetBSD__)
+ #if !defined (CROSS_COMPILE) && !defined (IN_GCC)
  #include <machine/ansi.h>
+ #endif
  #endif
  
  /* In 4.3bsd-net2, machine/ansi.h defines these symbols, which are
Index: ./gnu/dist/gdb/configure.tgt
===================================================================
RCS file: /cvsroot/gnusrc/gnu/dist/gdb/configure.tgt,v
retrieving revision 1.1.1.1
diff -p -r1.1.1.1 configure.tgt
*** configure.tgt	1999/02/10 22:06:24	1.1.1.1
--- configure.tgt	1999/08/16 05:25:40
*************** powerpc-*-netware*)	gdb_target=ppc-nw
*** 213,219 ****
  powerpc-*-aix*)		gdb_target=aix ;;
  powerpcle-*-cygwin32)	gdb_target=cygwin32 ;;
  powerpcle-*-solaris*)	gdb_target=solaris ;;
! powerpc-*-eabi* | powerpc-*-linux* | powerpc-*-sysv* | powerpc-*-elf*)
  			if test -f ../sim/ppc/Makefile; then
  			  gdb_target=ppc-sim
  			else
--- 213,219 ----
  powerpc-*-aix*)		gdb_target=aix ;;
  powerpcle-*-cygwin32)	gdb_target=cygwin32 ;;
  powerpcle-*-solaris*)	gdb_target=solaris ;;
! powerpc-*-eabi* | powerpc-*-linux* | powerpc-*-sysv* | powerpc-*-elf* | powerpc-*-netbsd*)
  			if test -f ../sim/ppc/Makefile; then
  			  gdb_target=ppc-sim
  			else

--------------1BBC34EDE3C1DCA14C91C0D8
Content-Type: text/plain; charset=us-ascii;
 name="share.mk.diffs"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="share.mk.diffs"

Index: ./share/mk/bsd.kinc.mk
===================================================================
RCS file: /cvsroot/sharesrc/share/mk/bsd.kinc.mk,v
retrieving revision 1.5
diff -p -r1.5 bsd.kinc.mk
*** bsd.kinc.mk	1999/02/04 11:58:30	1.5
--- bsd.kinc.mk	1999/08/16 05:36:24
*************** SYMLINKS+=	${KDIR} ${INCSDIR}
*** 67,73 ****
  .PRECIOUS: ${DESTDIR}${INCSDIR}
  .PHONY: ${DESTDIR}${INCSDIR}
  ${DESTDIR}${INCSDIR}:
! 	@if [ ! -d ${.TARGET} ] || [ -L ${.TARGET} ] ; then \
  		echo creating ${.TARGET}; \
  		/bin/rm -rf ${.TARGET}; \
  		${INSTALL} -d -o ${BINOWN} -g ${BINGRP} -m 755 ${.TARGET}; \
--- 67,73 ----
  .PRECIOUS: ${DESTDIR}${INCSDIR}
  .PHONY: ${DESTDIR}${INCSDIR}
  ${DESTDIR}${INCSDIR}:
! 	@if [ ! -d ${.TARGET} ] || [ -h ${.TARGET} ] ; then \
  		echo creating ${.TARGET}; \
  		/bin/rm -rf ${.TARGET}; \
  		${INSTALL} -d -o ${BINOWN} -g ${BINGRP} -m 755 ${.TARGET}; \

--------------1BBC34EDE3C1DCA14C91C0D8
Content-Type: text/plain; charset=us-ascii;
 name="usr.sbin.config.diff"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="usr.sbin.config.diff"

Index: ./usr.sbin/config/Makefile.boot
===================================================================
RCS file: /cvsroot/syssrc/usr.sbin/config/Makefile.boot,v
retrieving revision 1.2
diff -p -r1.2 Makefile.boot
*** Makefile.boot	1999/04/02 06:36:30	1.2
--- Makefile.boot	1999/08/16 05:37:31
*************** lex.yy.o : gram.h
*** 48,52 ****
  	${CC} ${CFLAGS} -c $<
  
  clean:
! 	rm -f *.o config lex.yy.c y.tab.[ch]
! 
--- 48,51 ----
  	${CC} ${CFLAGS} -c $<
  
  clean:
! 	rm -f *.o config lex.yy.c y.tab.[ch] gram.c gram.h

--------------1BBC34EDE3C1DCA14C91C0D8--