[PATCH] D63329: Allow static linking of libc++ on Linux, just like -static-libstdc++

Erik Verbruggen via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Jun 14 04:59:04 PDT 2019


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]>,


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D63329.204748.patch
Type: text/x-patch
Size: 2192 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190614/968de9b7/attachment.bin>


More information about the cfe-commits mailing list