All of lore.kernel.org
 help / color / mirror / Atom feed
From: Masahiro Yamada <masahiroy@kernel.org>
To: Dominique Martinet <asmadeus@codewreck.org>
Cc: Linux Kbuild mailing list <linux-kbuild@vger.kernel.org>,
	Michal Marek <michal.lkml@markovi.net>,
	Linux Kernel Mailing List <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH 3/3] kbuild: rewrite ld-version.sh in shell script
Date: Mon, 21 Dec 2020 23:23:02 +0900	[thread overview]
Message-ID: <CAK7LNATOMnNDt5E8j1qkj=FELezZ551Amfz3i=eMiSUg0nygCg@mail.gmail.com> (raw)
In-Reply-To: <20201212174828.GA17179@nautica>

On Sun, Dec 13, 2020 at 2:48 AM Dominique Martinet
<asmadeus@codewreck.org> wrote:
>
> Masahiro Yamada wrote on Sun, Dec 13, 2020:
> > This script was written in awk in spite of the file extension '.sh'.
> > Rewrite it as a shell script.
>
> Wow! I wasn't expecting so much, would have sent some rework after the
> upcoming merge window.
> Thank you.
>
> Some comments below that you can probably ignore, this works for me.
>
>
> > diff --git a/scripts/ld-version.sh b/scripts/ld-version.sh
> > index 0f8a2c0f9502..c214aeb3200d 100755
> > --- a/scripts/ld-version.sh
> > +++ b/scripts/ld-version.sh
> > @@ -1,11 +1,22 @@
> > -#!/usr/bin/awk -f
> > +#!/bin/sh
> >  # SPDX-License-Identifier: GPL-2.0
> > -# extract linker version number from stdin and turn into single number
> > -     {
> > -     gsub(".*\\)", "");
> > -     gsub(".*version ", "");
> > -     gsub("-.*", "");
> > -     split($1,a, ".");
> > -     print a[1]*10000 + a[2]*100 + a[3];
> > -     exit
> > -     }
> > +#
> > +# Usage: $ ./scripts/ld-version.sh ld
> > +#
> > +# Print the linker version of `ld' in a 5 or 6-digit form
> > +# such as `23501' for GNU ld 2.35.1 etc.
> > +
> > +first_line="$($* --version | head -n 1)"
>
> Just nitpicking: this ($*) would fail if the argument contains spaces,
> it's generally better to use "$@" or "$1" (with quotes)

This is just a copy-paste work based on scripts/lld-version.sh.

"$@" is better, I agree.




> Probably doesn't matter here as gcc/clang-version scripts have the same
> problem, so if someone had a problem with that they probably would have
> reported it there.

Talking about gcc/clang-version, "$1" is not acceptable because the first
word of the arguments may not be the compiler.

For example, when CC="ccache gcc" is passed in,
scripts/gcc-version.sh  ccache  gcc
must return the gcc version.




Difference between "$@" and "$*" matters only
when it is directly passed to some command.


masahiro@grover:~$ set  "a   b"  c  d
masahiro@grover:~$ ls  "$*"
ls: cannot access 'a   b c d': No such file or directory
masahiro@grover:~$ ls  "$@"
ls: cannot access 'a   b': No such file or directory
ls: cannot access 'c': No such file or directory
ls: cannot access 'd': No such file or directory


"$*" was expanded into a single string, 'a   b c d'.
It forgot which spaces were the part of the argument,
and which spaces were argument delimiters.

In contrast, "$@" was expanded into three arguments,
'a   b', 'c', and 'd'.
It correctly preserved the original arguments.



Let me continue some more experiments...


masahiro@grover:~$ set  "a   b"  c  d
masahiro@grover:~$ compiler="$*"
masahiro@grover:~$ ls "$compiler"
ls: cannot access 'a   b c d': No such file or directory
masahiro@grover:~$ ls $compiler
ls: cannot access 'a': No such file or directory
ls: cannot access 'b': No such file or directory
ls: cannot access 'c': No such file or directory
ls: cannot access 'd': No such file or directory



masahiro@grover:~$ set  "a   b"  c  d
masahiro@grover:~$ compiler="$@"
masahiro@grover:~$ ls "$compiler"
ls: cannot access 'a   b c d': No such file or directory
masahiro@grover:~$ ls $compiler
ls: cannot access 'a': No such file or directory
ls: cannot access 'b': No such file or directory
ls: cannot access 'c': No such file or directory
ls: cannot access 'd': No such file or directory


The result is the same.
So, whichever we use, after it is assigned to the variable "compiler",
it forgets the origin of the spaces.


If we really want to preserve the passed arguments,
we need to use "$@" directly in scripts/gcc-version.sh
like this:

MAJOR=$(echo __GNUC__ | "$@" -E -x c - | tail -n 1)

If we do this,

  scripts/gcc-version.sh  ccache gcc

will succeed, and

  scripts/gcc-version.sh  "ccache gcc"

will fail.




> > +
> > +if ! ( echo $first_line | grep -q "GNU ld"); then
> > +     echo 0
> > +     exit 1
> > +fi
> > +
> > +# Distributions may append an extra string like 2.35-15.fc33
> > +# Take the part that consists of numbers and dots.
> > +VERSION=$(echo $first_line | sed 's/.* \([^ ]*\)$/\1/' | sed 's/^\(^[0-9.]*\).*/\1/')
> > +MAJOR=$(echo $VERSION | cut -d . -f 1)
> > +MINOR=$(echo $VERSION | cut -d . -f 2)
> > +PATCHLEVEL=$(echo $VERSION | cut -d . -f 3)
> > +printf "%d%02d%02d\\n" $MAJOR $MINOR $PATCHLEVEL
>
> There is a bug if there is no dot at all (e.g. if binutils ever releases
> a version 3 and call it version 3 and not 3.0, the script would print
> 30303 because cut when no delimiter is found always returns the whole
> string)
> This can be fixed by artificially appending a dot to VERSION:
> VERSION=$(echo $first_line | sed 's/.* \([^ ]*\)$/\1/' | sed 's/^\(^[0-9.]*\).*/\1./')
>
> I'm not sure it's worth worrying about either.

Ah, good catch.



--
Best Regards
Masahiro Yamada

  reply	other threads:[~2020-12-21 14:24 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-12 16:54 [PATCH 1/3] kbuild: do not use scripts/ld-version.sh for checking spatch version Masahiro Yamada
2020-12-12 16:54 ` [Cocci] " Masahiro Yamada
2020-12-12 16:54 ` [PATCH 2/3] kbuild: LD_VERSION redenomination Masahiro Yamada
2020-12-12 16:54   ` Masahiro Yamada
2020-12-12 16:54   ` Masahiro Yamada
2020-12-14 23:05   ` Will Deacon
2020-12-14 23:05     ` Will Deacon
2020-12-14 23:05     ` Will Deacon
2020-12-15 13:48   ` Thomas Bogendoerfer
2020-12-15 13:48     ` Thomas Bogendoerfer
2020-12-15 13:48     ` Thomas Bogendoerfer
2021-01-28  6:38   ` Masahiro Yamada
2021-01-28  6:38     ` Masahiro Yamada
2021-01-28  6:38     ` Masahiro Yamada
2020-12-12 16:54 ` [PATCH 3/3] kbuild: rewrite ld-version.sh in shell script Masahiro Yamada
2020-12-12 17:48   ` Dominique Martinet
2020-12-21 14:23     ` Masahiro Yamada [this message]
2020-12-12 20:21   ` kernel test robot
2020-12-12 20:21     ` kernel test robot
2020-12-12 20:53   ` kernel test robot
2020-12-12 20:53     ` kernel test robot
2020-12-12 21:42   ` kernel test robot
2020-12-12 21:42     ` kernel test robot
2020-12-12 21:47   ` David Laight
2020-12-21 14:29     ` Masahiro Yamada
2020-12-21 14:51       ` David Laight
2020-12-12 17:30 ` [Cocci] [PATCH 1/3] kbuild: do not use scripts/ld-version.sh for checking spatch version Julia Lawall
2020-12-12 17:30   ` Julia Lawall
2020-12-13 13:25 ` Markus Elfring

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAK7LNATOMnNDt5E8j1qkj=FELezZ551Amfz3i=eMiSUg0nygCg@mail.gmail.com' \
    --to=masahiroy@kernel.org \
    --cc=asmadeus@codewreck.org \
    --cc=linux-kbuild@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=michal.lkml@markovi.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.