r341231 - Refactor Addlibgcc to make the when and what logic more straightfoward.

Sterling Augustine via cfe-commits cfe-commits at lists.llvm.org
Fri Aug 31 10:59:03 PDT 2018


Author: saugustine
Date: Fri Aug 31 10:59:03 2018
New Revision: 341231

URL: http://llvm.org/viewvc/llvm-project?rev=341231&view=rev
Log:
Refactor Addlibgcc to make the when and what logic more straightfoward.

Add Android tests.


Modified:
    cfe/trunk/lib/Driver/ToolChains/CommonArgs.cpp
    cfe/trunk/test/Driver/linux-ld.c

Modified: cfe/trunk/lib/Driver/ToolChains/CommonArgs.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Driver/ToolChains/CommonArgs.cpp?rev=341231&r1=341230&r2=341231&view=diff
==============================================================================
--- cfe/trunk/lib/Driver/ToolChains/CommonArgs.cpp (original)
+++ cfe/trunk/lib/Driver/ToolChains/CommonArgs.cpp Fri Aug 31 10:59:03 2018
@@ -1126,23 +1126,42 @@ static void AddLibgcc(const llvm::Triple
   bool IsIAMCU = Triple.isOSIAMCU();
   bool StaticLibgcc = Args.hasArg(options::OPT_static_libgcc) ||
                       Args.hasArg(options::OPT_static);
-  if (!D.CCCIsCXX())
+
+  // The driver ignores -shared-libgcc and therefore treats such cases as
+  // unspecified.  Breaking out the two variables as below makes the current
+  // behavior explicit.
+  bool UnspecifiedLibgcc = !StaticLibgcc;
+  bool SharedLibgcc = !StaticLibgcc;
+
+  // Gcc adds libgcc arguments in various ways:
+  //
+  // gcc <none>: -lgcc --as-needed -lgcc_s --no-as-needed
+  // g++ <none>:                   -lgcc_s               -lgcc
+  // gcc shared:                   -lgcc_s               -lgcc
+  // g++ shared:                   -lgcc_s               -lgcc
+  // gcc static: -lgcc             -lgcc_eh
+  // g++ static: -lgcc             -lgcc_eh
+  //
+  // Also, certain targets need additional adjustments.
+
+  bool LibGccFirst = (D.CCCIsCC() && UnspecifiedLibgcc) || StaticLibgcc;
+  if (LibGccFirst)
     CmdArgs.push_back("-lgcc");
 
-  if (StaticLibgcc || isAndroid) {
-    if (D.CCCIsCXX())
-      CmdArgs.push_back("-lgcc");
-  } else {
-    if (!D.CCCIsCXX() && !isCygMing)
-      CmdArgs.push_back("--as-needed");
+  bool AsNeeded = D.CCCIsCC() && !StaticLibgcc && !isAndroid && !isCygMing;
+  if (AsNeeded)
+    CmdArgs.push_back("--as-needed");
+
+  if ((UnspecifiedLibgcc || SharedLibgcc) && !isAndroid)
     CmdArgs.push_back("-lgcc_s");
-    if (!D.CCCIsCXX() && !isCygMing)
-      CmdArgs.push_back("--no-as-needed");
-  }
 
-  if (StaticLibgcc && !isAndroid && !IsIAMCU)
+  else if (StaticLibgcc && !isAndroid && !IsIAMCU)
     CmdArgs.push_back("-lgcc_eh");
-  else if (!Args.hasArg(options::OPT_shared) && D.CCCIsCXX())
+
+  if (AsNeeded)
+    CmdArgs.push_back("--no-as-needed");
+
+  if (!LibGccFirst)
     CmdArgs.push_back("-lgcc");
 
   // According to Android ABI, we have to link with libdl if we are

Modified: cfe/trunk/test/Driver/linux-ld.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Driver/linux-ld.c?rev=341231&r1=341230&r2=341231&view=diff
==============================================================================
--- cfe/trunk/test/Driver/linux-ld.c (original)
+++ cfe/trunk/test/Driver/linux-ld.c Fri Aug 31 10:59:03 2018
@@ -230,6 +230,30 @@
 // SKIP-CHECK-CLANG-SHARED-LIBGCC-DYNAMIC: "-lc"
 // SKIP-CHECK-CLANG-SHARED-LIBGCC-DYNAMIC: "-lgcc_s" "-lgcc"
 //
+// RUN: %clang -no-canonical-prefixes %s -### -o %t.o 2>&1 \
+// RUN:     --target=aarch64-linux-android -rtlib=platform \
+// RUN:     --gcc-toolchain="" \
+// RUN:     --sysroot=%S/Inputs/basic_linux_tree \
+// RUN:   | FileCheck --check-prefix=CHECK-CLANG-ANDROID-NONE %s
+// CHECK-CLANG-ANDROID-NONE: "{{.*}}ld{{(.exe)?}}" "--sysroot=[[SYSROOT:[^"]+]]"
+// CHECK-CLANG-ANDROID-NONE: "-lgcc" "-ldl" "-lc"
+//
+// RUN: %clang -shared -no-canonical-prefixes %s -### -o %t.o 2>&1 \
+// RUN:     --target=aarch64-linux-android -rtlib=platform \
+// RUN:     --gcc-toolchain="" \
+// RUN:     --sysroot=%S/Inputs/basic_linux_tree \
+// RUN:   | FileCheck --check-prefix=CHECK-CLANG-ANDROID-SHARED %s
+// CHECK-CLANG-ANDROID-SHARED: "{{.*}}ld{{(.exe)?}}" "--sysroot=[[SYSROOT:[^"]+]]"
+// CHECK-CLANG-ANDROID-SHARED: "-lgcc" "-ldl" "-lc"
+//
+// RUN: %clang -static -no-canonical-prefixes %s -### -o %t.o 2>&1 \
+// RUN:     --target=aarch64-linux-android -rtlib=platform \
+// RUN:     --gcc-toolchain="" \
+// RUN:     --sysroot=%S/Inputs/basic_linux_tree \
+// RUN:   | FileCheck --check-prefix=CHECK-CLANG-ANDROID-STATIC %s
+// CHECK-CLANG-ANDROID-STATIC: "{{.*}}ld{{(.exe)?}}" "--sysroot=[[SYSROOT:[^"]+]]"
+// CHECK-CLANG-ANDROID-STATIC: "--start-group" "-lgcc" "-lc" "--end-group"
+//
 // RUN: %clang -no-canonical-prefixes %s -### -o %t.o 2>&1      \
 // RUN:     --target=x86_64-unknown-linux -rtlib=platform \
 // RUN:     -static \




More information about the cfe-commits mailing list