Subject: Can't build sgimips toolchain natively
To: None <port-sgimips@netbsd.org>
From: sgimips NetBSD list <sgimips@mrynet.com>
List: port-sgimips
Date: 05/15/2004 22:58:10
All,

I'm just set up an INDY R4k machine to do native source builds.

The install was performed from a release build I cross-built
yesterday:

NetBSD mod80 2.0E NetBSD 2.0E (GENERIC32_IP2x) #0: Fri May 14 12:40:08 CDT 2004  XXXXXXXX@mrynet.com:/tiny/obj/sgimips-main/sys/arch/sgimips/compile/GENERIC32_IP2x sgimips

However, I've come across a very bizarre problem manifesting itself
in many of the tools' "configure" scripts:

updating cache .././config.cache
creating ./config.status
creating Makefile
creating doc/Makefile
creating .gdbinit
creating po/Makefile.in
creating config.h
sed: 13: conftest.frag: invalid command code %
/tiny/obj/sgimips-main/tooldir.NetBSD-2.0E-mipseb/bin/nbmake  -f /tiny/src/sgimips-main/tools/binutils/../Makefile.gnuwrap  all-recursive

I've narrowed this sed(1) problem down to what appears to be a bug
in either sh(1) or the kernel itself.

What happens in the configure scripts is this:

1) colletion of parameters to #define or #undef
2) a complicated method of generating config.h which involves creating
   the file through many incantations that append to it sections at a
   time.  These sections are "conftest.frag" which are edited by sed(1)
   and the resulting output placed in config.h.

The above all works fine and is not an issue.

The issue lies in what happens when config.frag is overwritten by
the successive iterations when it already exists.  It appears that
the file is not bring properly truncated, and data that previously
existed in the file remains when it is written over.

Take the following example of configure script coding:

          cat $ac_file_inputs > conftest.in

P1--->    cat > conftest.frag <<CEOF
        ${ac_dA}HAVE_SYS_FILE_H${ac_dB}HAVE_SYS_FILE_H${ac_dC}1${ac_dD}
        ${ac_uA}HAVE_SYS_FILE_H${ac_uB}HAVE_SYS_FILE_H${ac_uC}1${ac_uD}
        ${ac_eA}HAVE_SYS_FILE_H${ac_eB}HAVE_SYS_FILE_H${ac_eC}1${ac_eD}
        ${ac_dA}HAVE_SYS_PARAM_H${ac_dB}HAVE_SYS_PARAM_H${ac_dC}1${ac_dD}
        ${ac_uA}HAVE_SYS_PARAM_H${ac_uB}HAVE_SYS_PARAM_H${ac_uC}1${ac_uD}
        ${ac_eA}HAVE_SYS_PARAM_H${ac_eB}HAVE_SYS_PARAM_H${ac_eC}1${ac_eD}
        ${ac_dA}HAVE_LIMITS_H${ac_dB}HAVE_LIMITS_H${ac_dC}1${ac_dD}
        ${ac_uA}HAVE_LIMITS_H${ac_uB}HAVE_LIMITS_H${ac_uC}1${ac_uD}
        ${ac_eA}HAVE_LIMITS_H${ac_eB}HAVE_LIMITS_H${ac_eC}1${ac_eD}
        ${ac_dA}HAVE_STDLIB_H${ac_dB}HAVE_STDLIB_H${ac_dC}1${ac_dD}
        ${ac_uA}HAVE_STDLIB_H${ac_uB}HAVE_STDLIB_H${ac_uC}1${ac_uD}
        ${ac_eA}HAVE_STDLIB_H${ac_eB}HAVE_STDLIB_H${ac_eC}1${ac_eD}
        CEOF

          sed -f conftest.frag conftest.in > conftest.out
          rm -f conftest.in
          mv conftest.out conftest.in
  
P2--->    cat > conftest.frag <<CEOF
        ${ac_dA}HAVE_STRING_H${ac_dB}HAVE_STRING_H${ac_dC}1${ac_dD}
        ${ac_uA}HAVE_STRING_H${ac_uB}HAVE_STRING_H${ac_uC}1${ac_uD} 
        ${ac_eA}HAVE_STRING_H${ac_eB}HAVE_STRING_H${ac_eC}1${ac_eD}
        ${ac_dA}HAVE_UNISTD_H${ac_dB}HAVE_UNISTD_H${ac_dC}1${ac_dD}
        ${ac_uA}HAVE_UNISTD_H${ac_uB}HAVE_UNISTD_H${ac_uC}1${ac_uD}
        ${ac_eA}HAVE_UNISTD_H${ac_eB}HAVE_UNISTD_H${ac_eC}1${ac_eD}
        ${ac_dA}HAVE_STRINGS_H${ac_dB}HAVE_STRINGS_H${ac_dC}1${ac_dD}
        ${ac_uA}HAVE_STRINGS_H${ac_uB}HAVE_STRINGS_H${ac_uC}1${ac_uD}
        ${ac_eA}HAVE_STRINGS_H${ac_eB}HAVE_STRINGS_H${ac_eC}1${ac_eD}  
        ${ac_dA}HAVE_SYS_TIME_H${ac_dB}HAVE_SYS_TIME_H${ac_dC}1${ac_dD}
        ${ac_uA}HAVE_SYS_TIME_H${ac_uB}HAVE_SYS_TIME_H${ac_uC}1${ac_uD}
        ${ac_eA}HAVE_SYS_TIME_H${ac_eB}HAVE_SYS_TIME_H${ac_eC}1${ac_eD}  
        CEOF

These result in the following conftest.frag files:

[P1]
+ cat conftest.frag
s%^\([  ]*\)#\([        ]*define[       ][      ]*\)HAVE_SYS_FILE_H\([  ][      ]*\)[^  ]*%\1#\2HAVE_SYS_FILE_H\31%g
s%^\([  ]*\)#\([        ]*\)undef\([    ][      ]*\)HAVE_SYS_FILE_H\([  ]\)%\1#\2define\3HAVE_SYS_FILE_H 1\4%g
s%^\([  ]*\)#\([        ]*\)undef\([    ][      ]*\)HAVE_SYS_FILE_H$%\1#\2define\3HAVE_SYS_FILE_H 1%g
s%^\([  ]*\)#\([        ]*define[       ][      ]*\)HAVE_SYS_PARAM_H\([         ][      ]*\)[^ ]*%\1#\2HAVE_SYS_PARAM_H\31%g
s%^\([  ]*\)#\([        ]*\)undef\([    ][      ]*\)HAVE_SYS_PARAM_H\([         ]\)%\1#\2define\3HAVE_SYS_PARAM_H 1\4%g
s%^\([  ]*\)#\([        ]*\)undef\([    ][      ]*\)HAVE_SYS_PARAM_H$%\1#\2define\3HAVE_SYS_PARAM_H 1%g
s%^\([  ]*\)#\([        ]*define[       ][      ]*\)HAVE_LIMITS_H\([    ][      ]*\)[^  ]*%\1#\2HAVE_LIMITS_H\31%g
s%^\([  ]*\)#\([        ]*\)undef\([    ][      ]*\)HAVE_LIMITS_H\([    ]\)%\1#\2define\3HAVE_LIMITS_H 1\4%g
s%^\([  ]*\)#\([        ]*\)undef\([    ][      ]*\)HAVE_LIMITS_H$%\1#\2define\3HAVE_LIMITS_H 1%g
s%^\([  ]*\)#\([        ]*define[       ][      ]*\)HAVE_STDLIB_H\([    ][      ]*\)[^  ]*%\1#\2HAVE_STDLIB_H\31%g
s%^\([  ]*\)#\([        ]*\)undef\([    ][      ]*\)HAVE_STDLIB_H\([    ]\)%\1#\2define\3HAVE_STDLIB_H 1\4%g
s%^\([  ]*\)#\([        ]*\)undef\([    ][      ]*\)HAVE_STDLIB_H$%\1#\2define\3HAVE_STDLIB_H 1%g

[P2]
+ cat conftest.frag
s%^\([  ]*\)#\([        ]*define[       ][      ]*\)HAVE_STRING_H\([    ][      ]*\)[^  ]*%\1#\2HAVE_STRING_H\31%g
s%^\([  ]*\)#\([        ]*\)undef\([    ][      ]*\)HAVE_STRING_H\([    ]\)%\1#\2define\3HAVE_STRING_H 1\4%g
s%^\([  ]*\)#\([        ]*\)undef\([    ][      ]*\)HAVE_STRING_H$%\1#\2define\3HAVE_STRING_H 1%g
s%^\([  ]*\)#\([        ]*define[       ][      ]*\)HAVE_UNISTD_H\([    ][      ]*\)[^  ]*%\1#\2HAVE_UNISTD_H\31%g
s%^\([  ]*\)#\([        ]*\)undef\([    ][      ]*\)HAVE_UNISTD_H\([    ]\)%\1#\2define\3HAVE_UNISTD_H 1\4%g 
s%^\([  ]*\)#\([        ]*\)undef\([    ][      ]*\)HAVE_UNISTD_H$%\1#\2define\3HAVE_UNISTD_H 1%g
s%^\([  ]*\)#\([        ]*define[       ][      ]*\)HAVE_STRINGS_H\([   ][      ]*\)[^  ]*%\1#\2HAVE_STRINGS_H\31%g
s%^\([  ]*\)#\([        ]*\)undef\([    ][      ]*\)HAVE_STRINGS_H\([   ]\)%\1#\2define\3HAVE_STRINGS_H 1\4%g
s%^\([  ]*\)#\([        ]*\)undef\([    ][      ]*\)HAVE_STRINGS_H$%\1#\2define\3HAVE_STRINGS_H1%g
s%^\([  ]*\)#\([        ]*define[       ][      ]*\)HAVE_SYS_TIME_H\([  ][      ]*\)[^  ]*%\1#\2HAVE_SYS_TIME_H\31%g
s%^\([  ]*\)#\([        ]*\)undef\([    ][      ]*\)HAVE_SYS_TIME_H\([  ]\)%\1#\2define\3HAVE_SYS_TIME_H 1\4%g
s%^\([  ]*\)#\([        ]*\)undef\([    ][      ]*\)HAVE_SYS_TIME_H$%\1#\2define\3HAVE_SYS_TIME_H 1%g
TDLIB_H 1%g

^^^^^^^^^^^

See the "TDLIB_H 1%g" above?   That's left over from the previous
contents of the file.   It should have been truncated off at the "--->P2"
noted above.

I can't tell if this is a sh(1) bug, a kernel bug, or my own local problem
resulting from a cross-build.

I have yet to be able to reproduce it in a simple shell script, even though
it fails without exception on my sgimips native build attempts.  It 
manifests itself when libiberty is built in binutils of the tools
step.  I worked around this by simply having configure rm(1) conftest.frag,
but it then proved to occur later in the tools building gas(1) which uses
the identical mechanisms in the configure script.

Is anyone out there building the current MAIN branch sources natively on
an sgimips box?  Or, CAN anyone try after a CVS update and report back?

Anyone know of some regressions I can run on the INDY to test this?
I really don't know how to go about diagnosing this other than getting
someone's up-to-date natively-built sgimips release to compare this to.

I see three possibilities:

	1.	[unlikely] truncate(2) or open(2) are broken on sgimips
	2.	[possible] sh(1) ">" redirect broken on sgimips
	3.	[likely] cross-building results in breaking one of the above

(Man, I hope I got all the details in here... :)

Thanks and regards,
-scott