Skip to contentSkip to conversation

Makefile: use -Wno-main in the full kernel tree

PATCH In review Randy Dunlap 3 participants 11 messages Aug 13, 2021 – Sep 7, 2021 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Makefile: use -Wno-main in the full kernel tree

When using gcc (SUSE Linux) 7.5.0 (on openSUSE 15.3), I see a build warning:

../kernel/trace/trace_osnoise.c: In function 'start_kthread':
../kernel/trace/trace_osnoise.c:1461:8: warning: 'main' is usually a function [-Wmain]

  void *main = osnoise_main;
        ^~~~

Quieten that warning by using "-Wno-main". It's OK to use "main" as a declaration name in the kernel.

Build-tested on most ARCHes.

---
 Makefile |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Makefile +1 -1
@@ -522,7 +522,7 @@ KBUILD_AFLAGS   := -D__ASSEMBLY__ -fno-P
 KBUILD_CFLAGS   := -Wall -Wundef -Werror=strict-prototypes -Wno-trigraphs \
 		   -fno-strict-aliasing -fno-common -fshort-wchar -fno-PIE \
 		   -Werror=implicit-function-declaration -Werror=implicit-int \
-		   -Werror=return-type -Wno-format-security \
+		   -Werror=return-type -Wno-format-security -Wno-main \
 		   -std=gnu89
 KBUILD_CPPFLAGS := -D__KERNEL__
 KBUILD_RUST_TARGET := $(srctree)/arch/$(SRCARCH)/rust/target.json
#1 lore raw
Quoted 2 lines · Randy Dunlap

Build-tested on most ARCHes.

How about old versions of gcc? Or clang?

From a quick google, it seems like '-Wmain' means something else for clang. But it is probably ok.

Linus

#2 lore raw
Quoted 8 lines · Linus Torvalds
Quoted 2 lines · Randy Dunlap

Build-tested on most ARCHes.

How about old versions of gcc? Or clang?

From a quick google, it seems like '-Wmain' means something else for
clang. But it is probably ok.

Dunno. Let's just let it ride in linux-next for a bit to see what it hits.

#3 lore raw
In reply to Linus Torvalds
Quoted 3 lines · Linus Torvalds

From a quick google, it seems like '-Wmain' means something else for clang. But it is probably ok.

So the warnings that clang gives with -Wmain seem to be much more reasonable.

Which makes me think that the '-Wno-main' thing would likely be better as a gcc-only thing.

Maybe something like this instead?

Linus

 Makefile | 2 ++
 1 file changed, 2 insertions(+)
Makefile +2 -0
index c19d1638da25..a33fba083df7 100644
@@ -803,6 +803,8 @@ else
 # Disabled for clang while comment to attribute conversion happens and
 # https://github.com/ClangBuiltLinux/linux/issues/636 is discussed.
 KBUILD_CFLAGS += $(call cc-option,-Wimplicit-fallthrough=5,)
+# gcc inanely warns about local variables called 'main'
+KBUILD_CFLAGS += -Wno-main
 endif
 
 # These warnings generated too much noise in a regular build.
#4 lore raw
Quoted 2 lines · Linus Torvalds

So the warnings that clang gives with -Wmain seem to be much more reasonable.

In fact, I can't recreate the warning even with gcc and -Wmain.

I get the feeling that this was a temporary misake in gcc, and perhaps only warns in a few very specific versions, and the gcc people already fixed this long ago.

Oh well.

Linus

#5 lore raw
Quoted 5 lines · Linus Torvalds
Quoted 2 lines · Linus Torvalds

So the warnings that clang gives with -Wmain seem to be much more reasonable.

Quoted 7 lines

In fact, I can't recreate the warning even with gcc and -Wmain.

I get the feeling that this was a temporary misake in gcc, and perhaps only warns in a few very specific versions, and the gcc people already fixed this long ago.

Oh well.

That sounds likely. We can just ignore this if you had rather go that way.

Signature · 1 line
~Randy
#6 lore raw
Quoted 2 lines · Randy Dunlap

That sounds likely. We can just ignore this if you had rather go that way.

I'm happy doing it, I don't think the Wmain warnings buy anything for the kernel. My main worry is just that some compiler version might not support it at all or similar, and it would cause problems that way.

Which is really the only reason why I'd like to narrow it down as much as (trivially) possible.

It doesn't sound worth trying to pinpoint exact gcc version, but making it gcc-only would at least narrow it down a _bit_.

Linus

#7 lore raw
Quoted 8 lines · On Mon, 16 Aug 2021 15:31:33 -1000 Linus Torvalds…
Quoted 2 lines · Randy Dunlap

That sounds likely. We can just ignore this if you had rather go that way.

I'm happy doing it, I don't think the Wmain warnings buy anything for the kernel. My main worry is just that some compiler version might not support it at all or similar, and it would cause problems that way.

Quoted 5 lines

Which is really the only reason why I'd like to narrow it down as much as (trivially) possible.

It doesn't sound worth trying to pinpoint exact gcc version, but making it gcc-only would at least narrow it down a _bit_.

Looks to me that -Wmain and -Wno-main have been in gcc for a long time. If we limit it to just gcc, I believe it will not be an issue for any compiler version the kernel supports.

https://gcc.gnu.org/onlinedocs/gcc/Option-Index.html

And perhaps you are correct, as this is not a new option (it's in all the gcc versions I have (tested back to 4.2.4)), and it didn't trigger for me, I too believe this was a bug in a particular version that Randy used.

-- Steve

author Tested-by
#8 lore raw
In reply to Linus Torvalds
Quoted 6 lines · Linus Torvalds
Quoted 3 lines · Linus Torvalds

From a quick google, it seems like '-Wmain' means something else for
clang. But it is probably ok.

Quoted 8 lines

So the warnings that clang gives with -Wmain seem to be much more reasonable.

Which makes me think that the '-Wno-main' thing would likely be better as a gcc-only thing.

Maybe something like this instead?

Linus

Should I resend the full patch?

Thanks.

Signature · 1 line
~Randy
author Tested-by
#9 lore raw
In reply to Linus Torvalds
Quoted 6 lines · Linus Torvalds
Quoted 3 lines · Linus Torvalds

From a quick google, it seems like '-Wmain' means something else for
clang. But it is probably ok.

Quoted 9 lines

So the warnings that clang gives with -Wmain seem to be much more reasonable.

Which makes me think that the '-Wno-main' thing would likely be better as a gcc-only thing.

Maybe something like this instead?

Linus

Linus,

Can we get your version of this patch merged?

Should I resend the full patch?

---
 Makefile | 2 ++
 1 file changed, 2 insertions(+)
Makefile +2 -0
index c19d1638da25..a33fba083df7 100644
@@ -803,6 +803,8 @@ else
 # Disabled for clang while comment to attribute conversion happens and
 # https://github.com/ClangBuiltLinux/linux/issues/636 is discussed.
 KBUILD_CFLAGS += $(call cc-option,-Wimplicit-fallthrough=5,)
+# gcc inanely warns about local variables called 'main'
+KBUILD_CFLAGS += -Wno-main
 endif
 
 # These warnings generated too much noise in a regular build.
Acked-by on v1 1/1
#10 lore raw
In reply to Randy Dunlap
Quoted 27 lines · On Fri, 13 Aug 2021 15:41:31 -0700 Randy Dunlap <[email protected]> wrote:

When using gcc (SUSE Linux) 7.5.0 (on openSUSE 15.3), I see a build warning:

../kernel/trace/trace_osnoise.c: In function 'start_kthread':
../kernel/trace/trace_osnoise.c:1461:8: warning: 'main' is usually a function [-Wmain]

  void *main = osnoise_main;
        ^~~~

Quieten that warning by using "-Wno-main". It's OK to use "main" as a declaration name in the kernel.

Build-tested on most ARCHes.

---
 Makefile |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Makefile KBUILD_AFLAGS := -D__ASSEMBLY__ -fno-P · line 525

Quoted 5 lines
Makefile +1 -1
@@ -522,7 +522,7 @@ KBUILD_AFLAGS   := -D__ASSEMBLY__ -fno-P
 KBUILD_CFLAGS   := -Wall -Wundef -Werror=strict-prototypes -Wno-trigraphs \
 		   -fno-strict-aliasing -fno-common -fshort-wchar -fno-PIE \
 		   -Werror=implicit-function-declaration -Werror=implicit-int \
-		   -Werror=return-type -Wno-format-security \
+		   -Werror=return-type -Wno-format-security -Wno-main \

-- Steve

Quoted 3 lines
Makefile +0 -0
 		   -std=gnu89
 KBUILD_CPPFLAGS := -D__KERNEL__
 KBUILD_RUST_TARGET := $(srctree)/arch/$(SRCARCH)/rust/target.json