erikjv created this revision.
erikjv added reviewers: dlj, cfe-commits.
Herald added a reviewer: EricWF.
Herald added a subscriber: jfb.
Herald added a project: clang.

Dynamic linking against libc++ on Linux was already supported, as is
dynamic and static linking against libstdc++. What was missing is being
able to statically link against libc++. This can be used by applications
that choose to use libc++ instead of libstdc++, but do not (or can not)
require the dynamic libraries of libc++(abi) to be installed.


Repository:
  rC Clang

https://reviews.llvm.org/D63329

Files:
  clang/include/clang/Driver/Options.td
  clang/lib/Driver/ToolChains/Gnu.cpp


Index: clang/lib/Driver/ToolChains/Gnu.cpp
===================================================================
--- clang/lib/Driver/ToolChains/Gnu.cpp
+++ clang/lib/Driver/ToolChains/Gnu.cpp
@@ -528,11 +528,14 @@
   if (D.CCCIsCXX() &&
       !Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
     if (ToolChain.ShouldLinkCXXStdlib(Args)) {
-      bool OnlyLibstdcxxStatic = Args.hasArg(options::OPT_static_libstdcxx) &&
+      bool OnlyLibstdcxxStatic = (Args.hasArg(options::OPT_static_libstdcxx) ||
+                                  Args.hasArg(options::OPT_static_libcxx)) &&
                                  !Args.hasArg(options::OPT_static);
       if (OnlyLibstdcxxStatic)
         CmdArgs.push_back("-Bstatic");
       ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
+      if (Args.hasArg(options::OPT_static_libcxx)) // libc++ links against 
libc++abi, so we have to pull that in too.
+        CmdArgs.push_back("-lc++abi");
       if (OnlyLibstdcxxStatic)
         CmdArgs.push_back("-Bdynamic");
     }
@@ -564,6 +567,13 @@
         // FIXME: Does this really make sense for all GNU toolchains?
         WantPthread = true;
 
+      // FIXME: libc++ dynamically links against libpthread, so for static
+      // linking, we need to supply that dependency.
+      if (Args.hasArg(options::OPT_static_libcxx))
+        // FIXME: Again, like above, does this really make sense for all GNU
+        // toolchains?
+        WantPthread = true;
+
       AddRunTimeLibs(ToolChain, D, CmdArgs, Args);
 
       if (WantPthread && !isAndroid)
Index: clang/include/clang/Driver/Options.td
===================================================================
--- clang/include/clang/Driver/Options.td
+++ clang/include/clang/Driver/Options.td
@@ -2602,6 +2602,7 @@
 def specs : Separate<["-", "--"], "specs">, Flags<[Unsupported]>;
 def static_libgcc : Flag<["-"], "static-libgcc">;
 def static_libstdcxx : Flag<["-"], "static-libstdc++">;
+def static_libcxx : Flag<["-"], "static-libc++">;
 def static : Flag<["-", "--"], "static">, Flags<[NoArgumentUnused]>;
 def std_default_EQ : Joined<["-"], "std-default=">;
 def std_EQ : Joined<["-", "--"], "std=">, Flags<[CC1Option]>,


Index: clang/lib/Driver/ToolChains/Gnu.cpp
===================================================================
--- clang/lib/Driver/ToolChains/Gnu.cpp
+++ clang/lib/Driver/ToolChains/Gnu.cpp
@@ -528,11 +528,14 @@
   if (D.CCCIsCXX() &&
       !Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
     if (ToolChain.ShouldLinkCXXStdlib(Args)) {
-      bool OnlyLibstdcxxStatic = Args.hasArg(options::OPT_static_libstdcxx) &&
+      bool OnlyLibstdcxxStatic = (Args.hasArg(options::OPT_static_libstdcxx) ||
+                                  Args.hasArg(options::OPT_static_libcxx)) &&
                                  !Args.hasArg(options::OPT_static);
       if (OnlyLibstdcxxStatic)
         CmdArgs.push_back("-Bstatic");
       ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
+      if (Args.hasArg(options::OPT_static_libcxx)) // libc++ links against libc++abi, so we have to pull that in too.
+        CmdArgs.push_back("-lc++abi");
       if (OnlyLibstdcxxStatic)
         CmdArgs.push_back("-Bdynamic");
     }
@@ -564,6 +567,13 @@
         // FIXME: Does this really make sense for all GNU toolchains?
         WantPthread = true;
 
+      // FIXME: libc++ dynamically links against libpthread, so for static
+      // linking, we need to supply that dependency.
+      if (Args.hasArg(options::OPT_static_libcxx))
+        // FIXME: Again, like above, does this really make sense for all GNU
+        // toolchains?
+        WantPthread = true;
+
       AddRunTimeLibs(ToolChain, D, CmdArgs, Args);
 
       if (WantPthread && !isAndroid)
Index: clang/include/clang/Driver/Options.td
===================================================================
--- clang/include/clang/Driver/Options.td
+++ clang/include/clang/Driver/Options.td
@@ -2602,6 +2602,7 @@
 def specs : Separate<["-", "--"], "specs">, Flags<[Unsupported]>;
 def static_libgcc : Flag<["-"], "static-libgcc">;
 def static_libstdcxx : Flag<["-"], "static-libstdc++">;
+def static_libcxx : Flag<["-"], "static-libc++">;
 def static : Flag<["-", "--"], "static">, Flags<[NoArgumentUnused]>;
 def std_default_EQ : Joined<["-"], "std-default=">;
 def std_EQ : Joined<["-", "--"], "std=">, Flags<[CC1Option]>,
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to