diff mbox

[v2,03/18] kbuild: move gcc-version.sh to cc-version.sh and add clang support

Message ID 20171115213428.22559-4-samitolvanen@google.com (mailing list archive)
State New, archived
Headers show

Commit Message

Sami Tolvanen Nov. 15, 2017, 9:34 p.m. UTC
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
---
 scripts/Kbuild.include |  4 ++--
 scripts/cc-version.sh  | 45 +++++++++++++++++++++++++++++++++++++++++++++
 scripts/gcc-version.sh | 33 ---------------------------------
 3 files changed, 47 insertions(+), 35 deletions(-)
 create mode 100755 scripts/cc-version.sh
 delete mode 100755 scripts/gcc-version.sh

Comments

Kees Cook Nov. 15, 2017, 9:48 p.m. UTC | #1
On Wed, Nov 15, 2017 at 1:34 PM, Sami Tolvanen <samitolvanen@google.com> wrote:
> Signed-off-by: Sami Tolvanen <samitolvanen@google.com>

It might make sense to split this patch: do the move and refactoring,
then add clang support.

Though, won't this confuse some tests? A lot of cc-version tests are
expecting only gcc, yes?

-Kees

> ---
>  scripts/Kbuild.include |  4 ++--
>  scripts/cc-version.sh  | 45 +++++++++++++++++++++++++++++++++++++++++++++
>  scripts/gcc-version.sh | 33 ---------------------------------
>  3 files changed, 47 insertions(+), 35 deletions(-)
>  create mode 100755 scripts/cc-version.sh
>  delete mode 100755 scripts/gcc-version.sh
>
> diff --git a/scripts/Kbuild.include b/scripts/Kbuild.include
> index 584d6cecd7c0..de41ab74121e 100644
> --- a/scripts/Kbuild.include
> +++ b/scripts/Kbuild.include
> @@ -143,11 +143,11 @@ cc-disable-warning = $(call try-run,\
>  cc-name = $(shell $(CC) -v 2>&1 | grep -q "clang version" && echo clang || echo gcc)
>
>  # cc-version
> -cc-version = $(shell $(CONFIG_SHELL) $(srctree)/scripts/gcc-version.sh $(CC))
> +cc-version = $(shell $(CONFIG_SHELL) $(srctree)/scripts/cc-version.sh $(CC))
>
>  # cc-fullversion
>  cc-fullversion = $(shell $(CONFIG_SHELL) \
> -       $(srctree)/scripts/gcc-version.sh -p $(CC))
> +       $(srctree)/scripts/cc-version.sh -p $(CC))
>
>  # cc-ifversion
>  # Usage:  EXTRA_CFLAGS += $(call cc-ifversion, -lt, 0402, -O1)
> diff --git a/scripts/cc-version.sh b/scripts/cc-version.sh
> new file mode 100755
> index 000000000000..6d085a8a07e8
> --- /dev/null
> +++ b/scripts/cc-version.sh
> @@ -0,0 +1,45 @@
> +#!/bin/sh
> +# SPDX-License-Identifier: GPL-2.0
> +#
> +# cc-version [-p] cc-command
> +#
> +# Prints the compiler version of `command' in a canonical 4-digit form
> +# such as `0295' for gcc-2.95, `0303' for gcc-3.3, etc.
> +#
> +# With the -p option, prints the patchlevel as well, for example `029503' for
> +# gcc-2.95.3, `030301' for gcc-3.3.1, etc.
> +#
> +
> +if [ "$1" = "-p" ] ; then
> +       with_patchlevel=1;
> +       shift;
> +fi
> +
> +compiler="$*"
> +
> +if [ ${#compiler} -eq 0 ]; then
> +       echo "Error: No compiler specified."
> +       printf "Usage:\n\t$0 <cc-command>\n"
> +       exit 1
> +fi
> +
> +clang=$(echo __clang__ | $compiler -E -x c - | tail -n1)
> +
> +if [ "$clang" == "1" ]; then
> +       major_macro="__clang_major__"
> +       minor_macro="__clang_minor__"
> +       patchlevel_macro="__clang_patchlevel__"
> +else
> +       major_macro="__GNUC__"
> +       minor_macro="__GNUC_MINOR__"
> +       patchlevel_macro="__GNUC_PATCHLEVEL__"
> +fi
> +
> +MAJOR=$(echo $major_macro | $compiler -E -x c - | tail -n 1)
> +MINOR=$(echo $minor_macro | $compiler -E -x c - | tail -n 1)
> +if [ "x$with_patchlevel" != "x" ] ; then
> +       PATCHLEVEL=$(echo $patchlevel_macro | $compiler -E -x c - | tail -n 1)
> +       printf "%02d%02d%02d\\n" $MAJOR $MINOR $PATCHLEVEL
> +else
> +       printf "%02d%02d\\n" $MAJOR $MINOR
> +fi
> diff --git a/scripts/gcc-version.sh b/scripts/gcc-version.sh
> deleted file mode 100755
> index 11bb909845e7..000000000000
> --- a/scripts/gcc-version.sh
> +++ /dev/null
> @@ -1,33 +0,0 @@
> -#!/bin/sh
> -# SPDX-License-Identifier: GPL-2.0
> -#
> -# gcc-version [-p] gcc-command
> -#
> -# Prints the gcc version of `gcc-command' in a canonical 4-digit form
> -# such as `0295' for gcc-2.95, `0303' for gcc-3.3, etc.
> -#
> -# With the -p option, prints the patchlevel as well, for example `029503' for
> -# gcc-2.95.3, `030301' for gcc-3.3.1, etc.
> -#
> -
> -if [ "$1" = "-p" ] ; then
> -       with_patchlevel=1;
> -       shift;
> -fi
> -
> -compiler="$*"
> -
> -if [ ${#compiler} -eq 0 ]; then
> -       echo "Error: No compiler specified."
> -       printf "Usage:\n\t$0 <gcc-command>\n"
> -       exit 1
> -fi
> -
> -MAJOR=$(echo __GNUC__ | $compiler -E -x c - | tail -n 1)
> -MINOR=$(echo __GNUC_MINOR__ | $compiler -E -x c - | tail -n 1)
> -if [ "x$with_patchlevel" != "x" ] ; then
> -       PATCHLEVEL=$(echo __GNUC_PATCHLEVEL__ | $compiler -E -x c - | tail -n 1)
> -       printf "%02d%02d%02d\\n" $MAJOR $MINOR $PATCHLEVEL
> -else
> -       printf "%02d%02d\\n" $MAJOR $MINOR
> -fi
> --
> 2.15.0.448.gf294e3d99a-goog
>
Sami Tolvanen Nov. 15, 2017, 10:06 p.m. UTC | #2
On Wed, Nov 15, 2017 at 01:48:52PM -0800, Kees Cook wrote:
> It might make sense to split this patch: do the move and refactoring,
> then add clang support.

Sure.

> Though, won't this confuse some tests? A lot of cc-version tests are
> expecting only gcc, yes?

There's already a chance of this happening with cc-version. Currently,
gcc-version.sh returns 0402 for clang 5.0, which probably doesn't have
the same issues as gcc 4.2 did.

While I didn't see anything new that would break on platforms that
clang can currently compile, you're correct, we should probably have a
macro that also checks for the compiler, or have separate macros for
different compilers.

I'll address these in v3.

Sami
--
To unsubscribe from this list: send the line "unsubscribe linux-kbuild" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/scripts/Kbuild.include b/scripts/Kbuild.include
index 584d6cecd7c0..de41ab74121e 100644
--- a/scripts/Kbuild.include
+++ b/scripts/Kbuild.include
@@ -143,11 +143,11 @@  cc-disable-warning = $(call try-run,\
 cc-name = $(shell $(CC) -v 2>&1 | grep -q "clang version" && echo clang || echo gcc)
 
 # cc-version
-cc-version = $(shell $(CONFIG_SHELL) $(srctree)/scripts/gcc-version.sh $(CC))
+cc-version = $(shell $(CONFIG_SHELL) $(srctree)/scripts/cc-version.sh $(CC))
 
 # cc-fullversion
 cc-fullversion = $(shell $(CONFIG_SHELL) \
-	$(srctree)/scripts/gcc-version.sh -p $(CC))
+	$(srctree)/scripts/cc-version.sh -p $(CC))
 
 # cc-ifversion
 # Usage:  EXTRA_CFLAGS += $(call cc-ifversion, -lt, 0402, -O1)
diff --git a/scripts/cc-version.sh b/scripts/cc-version.sh
new file mode 100755
index 000000000000..6d085a8a07e8
--- /dev/null
+++ b/scripts/cc-version.sh
@@ -0,0 +1,45 @@ 
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0
+#
+# cc-version [-p] cc-command
+#
+# Prints the compiler version of `command' in a canonical 4-digit form
+# such as `0295' for gcc-2.95, `0303' for gcc-3.3, etc.
+#
+# With the -p option, prints the patchlevel as well, for example `029503' for
+# gcc-2.95.3, `030301' for gcc-3.3.1, etc.
+#
+
+if [ "$1" = "-p" ] ; then
+	with_patchlevel=1;
+	shift;
+fi
+
+compiler="$*"
+
+if [ ${#compiler} -eq 0 ]; then
+	echo "Error: No compiler specified."
+	printf "Usage:\n\t$0 <cc-command>\n"
+	exit 1
+fi
+
+clang=$(echo __clang__ | $compiler -E -x c - | tail -n1)
+
+if [ "$clang" == "1" ]; then
+	major_macro="__clang_major__"
+	minor_macro="__clang_minor__"
+	patchlevel_macro="__clang_patchlevel__"
+else
+	major_macro="__GNUC__"
+	minor_macro="__GNUC_MINOR__"
+	patchlevel_macro="__GNUC_PATCHLEVEL__"
+fi
+
+MAJOR=$(echo $major_macro | $compiler -E -x c - | tail -n 1)
+MINOR=$(echo $minor_macro | $compiler -E -x c - | tail -n 1)
+if [ "x$with_patchlevel" != "x" ] ; then
+	PATCHLEVEL=$(echo $patchlevel_macro | $compiler -E -x c - | tail -n 1)
+	printf "%02d%02d%02d\\n" $MAJOR $MINOR $PATCHLEVEL
+else
+	printf "%02d%02d\\n" $MAJOR $MINOR
+fi
diff --git a/scripts/gcc-version.sh b/scripts/gcc-version.sh
deleted file mode 100755
index 11bb909845e7..000000000000
--- a/scripts/gcc-version.sh
+++ /dev/null
@@ -1,33 +0,0 @@ 
-#!/bin/sh
-# SPDX-License-Identifier: GPL-2.0
-#
-# gcc-version [-p] gcc-command
-#
-# Prints the gcc version of `gcc-command' in a canonical 4-digit form
-# such as `0295' for gcc-2.95, `0303' for gcc-3.3, etc.
-#
-# With the -p option, prints the patchlevel as well, for example `029503' for
-# gcc-2.95.3, `030301' for gcc-3.3.1, etc.
-#
-
-if [ "$1" = "-p" ] ; then
-	with_patchlevel=1;
-	shift;
-fi
-
-compiler="$*"
-
-if [ ${#compiler} -eq 0 ]; then
-	echo "Error: No compiler specified."
-	printf "Usage:\n\t$0 <gcc-command>\n"
-	exit 1
-fi
-
-MAJOR=$(echo __GNUC__ | $compiler -E -x c - | tail -n 1)
-MINOR=$(echo __GNUC_MINOR__ | $compiler -E -x c - | tail -n 1)
-if [ "x$with_patchlevel" != "x" ] ; then
-	PATCHLEVEL=$(echo __GNUC_PATCHLEVEL__ | $compiler -E -x c - | tail -n 1)
-	printf "%02d%02d%02d\\n" $MAJOR $MINOR $PATCHLEVEL
-else
-	printf "%02d%02d\\n" $MAJOR $MINOR
-fi