Re: git: accfb4cc9346 - main - genoffset.sh: stop using a temporary file

From: Warner Losh <imp_at_bsdimp.com>
Date: Thu, 16 Nov 2023 16:23:43 UTC
On Thu, Nov 16, 2023 at 9:07 AM Jessica Clarke <jrtc27@freebsd.org> wrote:

> On 16 Nov 2023, at 15:07, Jonathan T. Looney <jtl@FreeBSD.org> wrote:
> >
> > The branch main has been updated by jtl:
> >
> > URL:
> https://cgit.FreeBSD.org/src/commit/?id=accfb4cc9346b23f6d6383dfc98d2c97ae18ce0d
> >
> > commit accfb4cc9346b23f6d6383dfc98d2c97ae18ce0d
> > Author:     Jonathan T. Looney <jtl@FreeBSD.org>
> > AuthorDate: 2023-11-16 15:02:32 +0000
> > Commit:     Jonathan T. Looney <jtl@FreeBSD.org>
> > CommitDate: 2023-11-16 15:02:32 +0000
> >
> >    genoffset.sh: stop using a temporary file
> >
> >    Instead, use a here document for the input. This allows us to run the
> >    while loop in the main script so we can build the list of asserts in
> >    a shell variable. We then print out the list of asserts at the end of
> >    the loop.
> >
> >    Reviewed by:    imp
> >    Sponsored by:   Netflix
> >    Differential Revision:  https://reviews.freebsd.org/D42407
> > ---
> > sys/kern/genoffset.sh | 15 +++++++--------
> > 1 file changed, 7 insertions(+), 8 deletions(-)
> >
> > diff --git a/sys/kern/genoffset.sh b/sys/kern/genoffset.sh
> > index fda27998ca79..c974a7d52e8c 100644
> > --- a/sys/kern/genoffset.sh
> > +++ b/sys/kern/genoffset.sh
> > @@ -35,16 +35,13 @@ usage()
> >
> > work()
> > (
> > -    local last off x1 x2 x3 struct field type lastoff lasttype
> > +    local last off x1 x2 x3 struct field type lastoff lasttype asserts
> >
> >     echo "#ifndef _OFFSET_INC_"
> >     echo "#define _OFFSET_INC_"
> >     echo "#if !defined(GENOFFSET) && (!defined(KLD_MODULE) ||
> defined(KLD_TIED))"
> >     last=
> > -    temp=$(mktemp -d genoffset.XXXXXXXXXX)
> > -    trap "rm -rf ${temp}" EXIT
> > -    # Note: we need to print symbol values in decimal so the numeric
> sort works
> > -    ${NM:='nm'} ${NMFLAGS} -t d "$1" | grep __assym_offset__ | sed -e
> 's/__/ /g' | sort -k 4 -k 1 -n |
> > +    asserts=
> >     while read off x1 x2 struct field type x3; do
> > off=$(echo "$off" | sed -E 's/^0+//')
> > if [ "$last" != "$struct" ]; then
> > @@ -60,12 +57,14 @@ work()
> > printf "%b" "\t${type}\t${field};\n"
> > lastoff="$off"
> > lasttype="$type"
> > - echo "_SA(${struct}, ${field}, ${off});" >> "$temp/asserts"
> > -    done
> > + asserts="${asserts}_SA(${struct}, ${field}, ${off});\n"
> > +    done <<EOT
> > +$(${NM:='nm'} ${NMFLAGS} -t d "$1" | grep __assym_offset__ | sed -e
> 's/__/ /g' | sort -k 4 -k 1 -n)
> > +EOT
> >     echo "};"
> >     echo "#define _SA(s,f,o) _Static_assert(__builtin_offsetof(struct s
> ## _lite, f) == o, \\"
> >     printf '\t"struct "#s"_lite field "#f" not at offset "#o)\n'
> > -    cat "$temp/asserts"
> > +    echo -e "${asserts}\c"
>
> This isn’t POSIX, and isn’t supported by macOS’s sh, so breaks the
> build there. Please fix or revert promptly.
>

echo "${asserts}"

is semantically the same for C. A stray newline doesn't matter in this
context. It's not worth the effort to remove it.

Sadly, echo -n doesn't work (it's not posix, and posix defines it
specifically as implementation defined). `echo "fred\c"' works on macos,
but not FreeBSD's shell echo built-in (but somehow does for /bin/echo).
macos doesn't implement -e at all (it's also not posix). FreeBSD's shell
echo build-in is not posix compliant. So, to be portable, just echo it, and
cope with the extra newline.

That's what I'd do :)

Warner


> Jess
>
> >     echo "#undef _SA"
> >     echo "#endif"
> >     echo "#endif"
>
>