Skip to content

[Driver] Silence stdlib warning when linking C on FreeBSD #68011

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 11, 2023

Conversation

dankm
Copy link
Contributor

@dankm dankm commented Oct 2, 2023

Similar to the Gnu toolchain, ignore uses of '-stdlib=libc++' when linking C code. CMake insists on adding it to the command line when linking C, and a bunch of other build systems do similarly.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' labels Oct 2, 2023
@llvmbot
Copy link
Member

llvmbot commented Oct 2, 2023

@llvm/pr-subscribers-clang-driver

@llvm/pr-subscribers-clang

Changes

Similar to the Gnu toolchain, ignore uses of '-stdlib=libc++' when linking C code. CMake insists on adding it to the command line when linking C, and a bunch of other build systems to similarly.


Full diff: https://github.com/llvm/llvm-project/pull/68011.diff

1 Files Affected:

  • (modified) clang/lib/Driver/ToolChains/FreeBSD.cpp (+3)
diff --git a/clang/lib/Driver/ToolChains/FreeBSD.cpp b/clang/lib/Driver/ToolChains/FreeBSD.cpp
index ff4d94c56f0d0ff..4d998e884f8ad42 100644
--- a/clang/lib/Driver/ToolChains/FreeBSD.cpp
+++ b/clang/lib/Driver/ToolChains/FreeBSD.cpp
@@ -356,6 +356,9 @@ void freebsd::Linker::ConstructJob(Compilation &C, const JobAction &JA,
 
   ToolChain.addProfileRTLibs(Args, CmdArgs);
 
+  // Silence warnings when linking C code with a C++ '-stdlib' argument.
+  Args.ClaimAllArgs(options::OPT_stdlib_EQ);
+
   const char *Exec = Args.MakeArgString(getToolChain().GetLinkerPath());
   C.addCommand(std::make_unique<Command>(JA, *this,
                                          ResponseFileSupport::AtFileCurCP(),

@dankm
Copy link
Contributor Author

dankm commented Oct 2, 2023

This fixes this inconsistency:

Linux:

$ clang -o test -stdlib=libc++ test.c
$ ./test
Hello world

FreeBSD:

$ clang -o test -stdlib=libc++ test.c
clang: warning: argument unused during compilation: '-stdlib=libc++' [-Wunused-command-line-argument]
$ ./test
Hello world

Similar to the Gnu toolchain, ignore uses of '-stdlib=libc++' when
linking C code. CMake insists on adding it to the command line when
linking C, and a bunch of other build systems do similarly.
@dankm dankm force-pushed the dankm/silence-stdlib-warning branch from 70d7622 to bde5fc6 Compare October 3, 2023 18:24
Copy link
Contributor

@egorzhdan egorzhdan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Darwin toolchain also does this. This change LGTM, although I'm not an expert in FreeBSD.

@dankm
Copy link
Contributor Author

dankm commented Oct 6, 2023

The Darwin toolchain also does this. This change LGTM, although I'm not an expert in FreeBSD.

Thanks. I think I'd like some FreeBSD folks to take a look. I wonder if it's rude to @ mention a few... maybe I will.

The ones I can think of immediately are @DimitryAndric, @emaste, and @jrtc27.

@emaste
Copy link
Member

emaste commented Oct 6, 2023

Yeah, I don't see any reason there'd be a problem with this. It seems a bit silly for build systems to pass this in, but if they do there's no value in reporting it.

@DimitryAndric
Copy link
Collaborator

Does this only affect cases where one passes -stdlib=libc++? What happens if somebody passes -stdlib=libstdc++?

@dankm
Copy link
Contributor Author

dankm commented Oct 6, 2023

Does this only affect cases where one passes -stdlib=libc++? What happens if somebody passes -stdlib=libstdc++?

It handles both cases. It simply claims all the -stdlib= arguments. This is the same behaviour as the Gnu driver used by Linux, but seems to diverge from Darwin.

@dankm
Copy link
Contributor Author

dankm commented Oct 6, 2023

You do bring up an interesting point, though. When linking C code with this change, for both Gnu and FreeBSD, -stdlib=libbad is ignored for C and raises an error for C++.

Also for reference, the toolchains that ignore the stdlib argument for C are: baremetal, Darwin, Fuchsia, Gnu, Hexagon, MipsLinux, NaCL, OHOS, and WebAssembly. I was mistaken above about Darwin.

@emaste
Copy link
Member

emaste commented Oct 6, 2023

It handles both cases. It simply claims all the -stdlib= arguments.

If I understand correctly this means in effect -stdlib= becomes a don't-care when linking C, and anyhow it already had no effect.

@dankm
Copy link
Contributor Author

dankm commented Oct 6, 2023

If I understand correctly this means in effect -stdlib= becomes a don't-care when linking C, and anyhow it already had no effect.

That's correct. It was previously a harmless warning, and is now (with this change) completely ignored.

Edit: shouldn't say harmless, -Werror turns this into an error:

$ clang15 -Werror --stdlib=libc++ -o test test.c
clang-15: error: argument unused during compilation: '--stdlib=libc++' [-Werror,-Wunused-command-line-argument]

@dankm
Copy link
Contributor Author

dankm commented Oct 11, 2023

If this looks good to people, is anyone able to merge it? I cannot do it myself.

@emaste emaste merged commit 12b87f6 into llvm:main Oct 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants