d: Fix order of precedence for -defaultlib and -debuglib
authorIain Buclaw <ibuclaw@gdcproject.org>
Fri, 24 Apr 2020 21:39:32 +0000 (23:39 +0200)
committerIain Buclaw <ibuclaw@gdcproject.org>
Fri, 24 Apr 2020 21:39:32 +0000 (23:39 +0200)
The order of precedence used by the upstream reference compiler for
determining what library to link against is:
- No library if -nophoboslib or -fno-druntime was seen.
- The library passed to -debuglib if -g was also seen.
- The library passed to -defaultlib
- The in-tree libgphobos library.

This aligns the D language driver to follow the same rules.

gcc/d/ChangeLog:

* d-spec.cc (need_phobos): Remove.
(lang_specific_driver): Replace need_phobos with phobos_library.
Reorder -debuglib and -defaultlib to have precedence over libphobos.
(lang_specific_pre_link): Remove test for need_phobos.

gcc/d/ChangeLog
gcc/d/d-spec.cc

index 6c3eb89c4d71d279869852fb7371a91175b1b490..b96f0ffb82f5921bd1236ba483d4aa4404581683 100644 (file)
@@ -1,3 +1,10 @@
+2020-04-24  Iain Buclaw  <ibuclaw@gdcproject.org>
+
+       * d-spec.cc (need_phobos): Remove.
+       (lang_specific_driver): Replace need_phobos with phobos_library.
+       Reorder -debuglib and -defaultlib to have precedence over libphobos.
+       (lang_specific_pre_link): Remove test for need_phobos.
+
 2020-04-19  Iain Buclaw  <ibuclaw@gdcproject.org>
 
        PR d/94609
index e0844222520aa3ec84eba057bd0fe939ab30c8b3..f4744763ab617e3adf762ca0a791a512597b0fa8 100644 (file)
@@ -61,10 +61,6 @@ enum phobos_action
 
 static phobos_action phobos_library = PHOBOS_DEFAULT;
 
-/* If true, use the standard D runtime library when linking with
-   standard libraries.  */
-static bool need_phobos = true;
-
 /* If true, do load libgphobos.spec even if not needed otherwise.  */
 static bool need_spec = false;
 
@@ -151,13 +147,15 @@ lang_specific_driver (cl_decoded_option **in_decoded_options,
          break;
 
        case OPT_nophoboslib:
-         need_phobos = false;
+         phobos_library = PHOBOS_NOLINK;
          args[i] |= SKIPOPT;
          break;
 
        case OPT_fdruntime:
          if (!value)
-           need_phobos = false;
+           phobos_library = PHOBOS_NOLINK;
+         else
+           phobos_library = PHOBOS_LINK;
          break;
 
        case OPT_defaultlib_:
@@ -165,7 +163,6 @@ lang_specific_driver (cl_decoded_option **in_decoded_options,
            free (CONST_CAST (char *, defaultlib));
          if (arg != NULL)
            {
-             need_phobos = false;
              args[i] |= SKIPOPT;
              defaultlib = XNEWVEC (char, strlen (arg));
              strcpy (CONST_CAST (char *, defaultlib), arg);
@@ -177,7 +174,6 @@ lang_specific_driver (cl_decoded_option **in_decoded_options,
            free (CONST_CAST (char *, debuglib));
          if (arg != NULL)
            {
-             need_phobos = false;
              args[i] |= SKIPOPT;
              debuglib = XNEWVEC (char, strlen (arg));
              strcpy (CONST_CAST (char *, debuglib), arg);
@@ -314,10 +310,11 @@ lang_specific_driver (cl_decoded_option **in_decoded_options,
 #endif
 
   /* Make sure to have room for the trailing NULL argument.
-     - needstdcxx might add `-lstdcxx'
+     - need_stdcxx might add `-lstdcxx'
      - libphobos adds `-Bstatic -lphobos -Bdynamic'
      - only_source adds 1 more arg, also maybe add `-o'.  */
-  num_args = argc + need_stdcxx + shared_libgcc + need_phobos * 4 + 2;
+  num_args = argc + need_stdcxx + shared_libgcc
+    + (phobos_library != PHOBOS_NOLINK) * 4 + 2;
   new_decoded_options = XNEWVEC (cl_decoded_option, num_args);
 
   i = 0;
@@ -409,60 +406,51 @@ lang_specific_driver (cl_decoded_option **in_decoded_options,
     }
 
   /* Add `-lgphobos' if we haven't already done so.  */
-  if (phobos_library != PHOBOS_NOLINK && need_phobos)
+  if (phobos_library != PHOBOS_NOLINK)
     {
       /* Default to static linking.  */
       if (phobos_library != PHOBOS_DYNAMIC)
        phobos_library = PHOBOS_STATIC;
 
 #ifdef HAVE_LD_STATIC_DYNAMIC
-      if (phobos_library == PHOBOS_DYNAMIC && static_link)
-       {
-         generate_option (OPT_Wl_, LD_DYNAMIC_OPTION, 1, CL_DRIVER,
-                          &new_decoded_options[j]);
-         j++;
-       }
-      else if (phobos_library == PHOBOS_STATIC && !static_link)
+      if (phobos_library == PHOBOS_STATIC && !static_link)
        {
          generate_option (OPT_Wl_, LD_STATIC_OPTION, 1, CL_DRIVER,
-                          &new_decoded_options[j]);
-         j++;
+                          &new_decoded_options[j++]);
        }
 #endif
-
-      generate_option (OPT_l,
-                      saw_profile_flag ? LIBPHOBOS_PROFILE : LIBPHOBOS, 1,
-                      CL_DRIVER, &new_decoded_options[j]);
-      added_libraries++;
-      j++;
-
-#ifdef HAVE_LD_STATIC_DYNAMIC
-      if (phobos_library == PHOBOS_DYNAMIC && static_link)
+      /* Order of precedence in determining what library to link against is:
+        - `-l<lib>' from `-debuglib=<lib>' if `-g' was also seen.
+        - `-l<lib>' from `-defaultlib=<lib>'.
+        - `-lgphobos' unless `-nophoboslib' or `-fno-druntime' was seen.  */
+      if (debuglib && saw_debug_flag)
        {
-         generate_option (OPT_Wl_, LD_STATIC_OPTION, 1, CL_DRIVER,
-                          &new_decoded_options[j]);
-         j++;
+         generate_option (OPT_l, debuglib, 1, CL_DRIVER,
+                          &new_decoded_options[j++]);
+         added_libraries++;
        }
-      else if (phobos_library == PHOBOS_STATIC && !static_link)
+      else if (defaultlib)
+       {
+         generate_option (OPT_l, defaultlib, 1, CL_DRIVER,
+                          &new_decoded_options[j++]);
+         added_libraries++;
+       }
+      else
+       {
+         generate_option (OPT_l,
+                          saw_profile_flag ? LIBPHOBOS_PROFILE : LIBPHOBOS, 1,
+                          CL_DRIVER, &new_decoded_options[j++]);
+         added_libraries++;
+       }
+
+#ifdef HAVE_LD_STATIC_DYNAMIC
+      if (phobos_library == PHOBOS_STATIC && !static_link)
        {
          generate_option (OPT_Wl_, LD_DYNAMIC_OPTION, 1, CL_DRIVER,
-                          &new_decoded_options[j]);
-         j++;
+                          &new_decoded_options[j++]);
        }
 #endif
     }
-  else if (saw_debug_flag && debuglib)
-    {
-      generate_option (OPT_l, debuglib, 1, CL_DRIVER,
-                      &new_decoded_options[j++]);
-      added_libraries++;
-    }
-  else if (defaultlib)
-    {
-      generate_option (OPT_l, defaultlib, 1, CL_DRIVER,
-                      &new_decoded_options[j++]);
-      added_libraries++;
-    }
 
   if (saw_libcxx)
     new_decoded_options[j++] = *saw_libcxx;
@@ -492,7 +480,7 @@ lang_specific_driver (cl_decoded_option **in_decoded_options,
 int
 lang_specific_pre_link (void)
 {
-  if ((phobos_library != PHOBOS_NOLINK && need_phobos) || need_spec)
+  if ((phobos_library != PHOBOS_NOLINK) || need_spec)
     do_spec ("%:include(libgphobos.spec)");
 
   return 0;