tbaeder created this revision.
tbaeder added reviewers: rsmith, EricWF, ldionne, tstellar, hfinkel.
tbaeder requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Hi,

currently the following fails:

  clang++ -stdlib=libc++ -static ./test.cpp

because libc++ needs libc++abi and pthreads.

This change has already been proposed in a different version, using 
`-static-libc++` in https://reviews.llvm.org/D63329

And https://reviews.llvm.org/D60794 tried to do it in libc++ instead of clang.

This version makes both linking with `-static` as above work, as well as 
`-static-libstdc++ -stdlib=libc++`.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D96070

Files:
  clang/lib/Driver/ToolChain.cpp
  clang/lib/Driver/ToolChains/Gnu.cpp
  clang/test/Driver/libcxx-link.cpp
  clang/test/Driver/netbsd.cpp


Index: clang/test/Driver/netbsd.cpp
===================================================================
--- clang/test/Driver/netbsd.cpp
+++ clang/test/Driver/netbsd.cpp
@@ -250,7 +250,7 @@
 // S-ARM-7: clang{{.*}}" "-cc1" "-triple" "armv5e-unknown-netbsd7.0.0-eabi"
 // S-ARM-7: ld{{.*}}" "--eh-frame-hdr" "-Bstatic"
 // S-ARM-7: "-o" "a.out" "{{.*}}/usr/lib{{/|\\\\}}crt0.o" 
"{{.*}}/usr/lib{{/|\\\\}}eabi{{/|\\\\}}crti.o"
-// S-ARM-7: "{{.*}}/usr/lib{{/|\\\\}}crtbegin.o" "{{.*}}.o" "-lc++" "-lm" "-lc"
+// S-ARM-7: "{{.*}}/usr/lib{{/|\\\\}}crtbegin.o" "{{.*}}.o" "-lc++" "-lc++abi" 
"-lm" "-lc"
 // S-ARM-7: "{{.*}}/usr/lib{{/|\\\\}}crtend.o" "{{.*}}/usr/lib{{/|\\\\}}crtn.o"
 
 // S-AARCH64: clang{{.*}}" "-cc1" "-triple" "aarch64-unknown-netbsd"
Index: clang/test/Driver/libcxx-link.cpp
===================================================================
--- /dev/null
+++ clang/test/Driver/libcxx-link.cpp
@@ -0,0 +1,20 @@
+
+
+// Regular shared link
+// RUN: %clang++ %s -stdlib=libc++
+
+// Static link via -static-libstdc++
+// RUN: %clang++ %s -stdlib=libc++ -static-libstdc++
+
+// Static link via -static
+// RUN: %clang++ %s -stdlib=libc++ -static
+
+// Both
+// RUN: %clang++ %s -stdlib=libc++ -static-libstdc++ -static
+
+#include <iostream>
+int main(int argc, char **argv) {
+  std::cout << "Hello Word\n";
+
+  return 0;
+}
Index: clang/lib/Driver/ToolChains/Gnu.cpp
===================================================================
--- clang/lib/Driver/ToolChains/Gnu.cpp
+++ clang/lib/Driver/ToolChains/Gnu.cpp
@@ -613,6 +613,14 @@
         // FIXME: Does this really make sense for all GNU toolchains?
         WantPthread = true;
 
+      // libc++ links against pthreads so for static links we need
+      // to supply this manually
+      if (!WantPthread &&
+          getToolChain().GetCXXStdlibType(Args) == ToolChain::CST_Libcxx &&
+          (Args.hasArg(options::OPT_static) ||
+           Args.hasArg(options::OPT_static_libstdcxx)))
+        WantPthread = true;
+
       AddRunTimeLibs(ToolChain, D, CmdArgs, Args);
 
       if (WantPthread && !isAndroid)
Index: clang/lib/Driver/ToolChain.cpp
===================================================================
--- clang/lib/Driver/ToolChain.cpp
+++ clang/lib/Driver/ToolChain.cpp
@@ -1041,6 +1041,9 @@
   switch (Type) {
   case ToolChain::CST_Libcxx:
     CmdArgs.push_back("-lc++");
+    if (Args.hasArg(options::OPT_static) ||
+        Args.hasArg(options::OPT_static_libstdcxx))
+      CmdArgs.push_back("-lc++abi");
     break;
 
   case ToolChain::CST_Libstdcxx:


Index: clang/test/Driver/netbsd.cpp
===================================================================
--- clang/test/Driver/netbsd.cpp
+++ clang/test/Driver/netbsd.cpp
@@ -250,7 +250,7 @@
 // S-ARM-7: clang{{.*}}" "-cc1" "-triple" "armv5e-unknown-netbsd7.0.0-eabi"
 // S-ARM-7: ld{{.*}}" "--eh-frame-hdr" "-Bstatic"
 // S-ARM-7: "-o" "a.out" "{{.*}}/usr/lib{{/|\\\\}}crt0.o" "{{.*}}/usr/lib{{/|\\\\}}eabi{{/|\\\\}}crti.o"
-// S-ARM-7: "{{.*}}/usr/lib{{/|\\\\}}crtbegin.o" "{{.*}}.o" "-lc++" "-lm" "-lc"
+// S-ARM-7: "{{.*}}/usr/lib{{/|\\\\}}crtbegin.o" "{{.*}}.o" "-lc++" "-lc++abi" "-lm" "-lc"
 // S-ARM-7: "{{.*}}/usr/lib{{/|\\\\}}crtend.o" "{{.*}}/usr/lib{{/|\\\\}}crtn.o"
 
 // S-AARCH64: clang{{.*}}" "-cc1" "-triple" "aarch64-unknown-netbsd"
Index: clang/test/Driver/libcxx-link.cpp
===================================================================
--- /dev/null
+++ clang/test/Driver/libcxx-link.cpp
@@ -0,0 +1,20 @@
+
+
+// Regular shared link
+// RUN: %clang++ %s -stdlib=libc++
+
+// Static link via -static-libstdc++
+// RUN: %clang++ %s -stdlib=libc++ -static-libstdc++
+
+// Static link via -static
+// RUN: %clang++ %s -stdlib=libc++ -static
+
+// Both
+// RUN: %clang++ %s -stdlib=libc++ -static-libstdc++ -static
+
+#include <iostream>
+int main(int argc, char **argv) {
+  std::cout << "Hello Word\n";
+
+  return 0;
+}
Index: clang/lib/Driver/ToolChains/Gnu.cpp
===================================================================
--- clang/lib/Driver/ToolChains/Gnu.cpp
+++ clang/lib/Driver/ToolChains/Gnu.cpp
@@ -613,6 +613,14 @@
         // FIXME: Does this really make sense for all GNU toolchains?
         WantPthread = true;
 
+      // libc++ links against pthreads so for static links we need
+      // to supply this manually
+      if (!WantPthread &&
+          getToolChain().GetCXXStdlibType(Args) == ToolChain::CST_Libcxx &&
+          (Args.hasArg(options::OPT_static) ||
+           Args.hasArg(options::OPT_static_libstdcxx)))
+        WantPthread = true;
+
       AddRunTimeLibs(ToolChain, D, CmdArgs, Args);
 
       if (WantPthread && !isAndroid)
Index: clang/lib/Driver/ToolChain.cpp
===================================================================
--- clang/lib/Driver/ToolChain.cpp
+++ clang/lib/Driver/ToolChain.cpp
@@ -1041,6 +1041,9 @@
   switch (Type) {
   case ToolChain::CST_Libcxx:
     CmdArgs.push_back("-lc++");
+    if (Args.hasArg(options::OPT_static) ||
+        Args.hasArg(options::OPT_static_libstdcxx))
+      CmdArgs.push_back("-lc++abi");
     break;
 
   case ToolChain::CST_Libstdcxx:
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to