awarzynski created this revision.
awarzynski added reviewers: rovka, rouson, MaskRay, h-vetinari, schweitz, 
kiranchandramohan.
Herald added a reviewer: sscalpone.
Herald added subscribers: StephenFan, mehdi_amini.
Herald added projects: Flang, All.
awarzynski requested review of this revision.
Herald added subscribers: cfe-commits, jdoerfert.
Herald added a project: clang.

This patch basically extends https://reviews.llvm.org/D122008 with
support for MacOSX/Darwin.

To facilitate this, I've added `MacOSX` to the list of supported OSes in
Target.cpp. Flang already supports `Darwin` and it doesn't really do
anything OS-specific there (it could probably safely skip checking the
OS for now).

Note that generating executables remains hidden behind the
`-flang-experimental-exec` flag.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D125628

Files:
  clang/lib/Driver/ToolChains/CommonArgs.cpp
  clang/lib/Driver/ToolChains/CommonArgs.h
  clang/lib/Driver/ToolChains/Darwin.cpp
  clang/lib/Driver/ToolChains/Gnu.cpp
  flang/lib/Optimizer/CodeGen/Target.cpp
  flang/test/Driver/linker-flags.f90

Index: flang/test/Driver/linker-flags.f90
===================================================================
--- flang/test/Driver/linker-flags.f90
+++ flang/test/Driver/linker-flags.f90
@@ -2,12 +2,12 @@
 ! invocation. These libraries are added on top of other standard runtime
 ! libraries that the Clang driver will include.
 
-! NOTE: The additional linker flags tested here are currently specified in
-! clang/lib/Driver/Toolchains/Gnu.cpp. This makes the current implementation GNU
-! (Linux) specific. The following line will make sure that this test is skipped
-! on Windows. Ideally we should find a more robust way of testing this.
-! REQUIRES: shell
-! UNSUPPORTED: darwin, macos, system-windows
+! NOTE: The additional linker flags tested here are currently only specified for
+! GNU and Darwin. The following line will make sure that this test is skipped on
+! Windows. If you are running this test on a yet another platform and it is
+! failing for you, please either update the following or (preferably) update the
+! linker invocation accordingly.
+! UNSUPPORTED: system-windows
 
 !------------
 ! RUN COMMAND
Index: flang/lib/Optimizer/CodeGen/Target.cpp
===================================================================
--- flang/lib/Optimizer/CodeGen/Target.cpp
+++ flang/lib/Optimizer/CodeGen/Target.cpp
@@ -251,6 +251,7 @@
       break;
     case llvm::Triple::OSType::Linux:
     case llvm::Triple::OSType::Darwin:
+    case llvm::Triple::OSType::MacOSX:
     case llvm::Triple::OSType::Win32:
       return std::make_unique<TargetI386>(ctx, std::move(trp),
                                           std::move(kindMap));
@@ -262,6 +263,7 @@
       break;
     case llvm::Triple::OSType::Linux:
     case llvm::Triple::OSType::Darwin:
+    case llvm::Triple::OSType::MacOSX:
     case llvm::Triple::OSType::Win32:
       return std::make_unique<TargetX86_64>(ctx, std::move(trp),
                                             std::move(kindMap));
@@ -273,6 +275,7 @@
       break;
     case llvm::Triple::OSType::Linux:
     case llvm::Triple::OSType::Darwin:
+    case llvm::Triple::OSType::MacOSX:
     case llvm::Triple::OSType::Win32:
       return std::make_unique<TargetAArch64>(ctx, std::move(trp),
                                              std::move(kindMap));
Index: clang/lib/Driver/ToolChains/Gnu.cpp
===================================================================
--- clang/lib/Driver/ToolChains/Gnu.cpp
+++ clang/lib/Driver/ToolChains/Gnu.cpp
@@ -382,28 +382,6 @@
                                          Exec, CmdArgs, Inputs, Output));
 }
 
-static void addFortranRuntimeLibraryPath(const ToolChain &TC,
-                                         const ArgList &Args,
-                                         ArgStringList &CmdArgs) {
-  // Default to the <driver-path>/../lib directory. This works fine on the
-  // platforms that we have tested so far. We will probably have to re-fine
-  // this in the future. In particular:
-  //    * on some platforms, we may need to use lib64 instead of lib
-  //    * this logic should also work on other similar platforms too, so we
-  //    should move it to one of Gnu's parent tool{chain} classes
-  SmallString<256> DefaultLibPath =
-      llvm::sys::path::parent_path(TC.getDriver().Dir);
-  llvm::sys::path::append(DefaultLibPath, "lib");
-  CmdArgs.push_back(Args.MakeArgString("-L" + DefaultLibPath));
-}
-
-static void addFortranLinkerFlags(ArgStringList &CmdArgs) {
-  CmdArgs.push_back("-lFortran_main");
-  CmdArgs.push_back("-lFortranRuntime");
-  CmdArgs.push_back("-lFortranDecimal");
-  CmdArgs.push_back("-lm");
-}
-
 void tools::gnutools::Linker::ConstructJob(Compilation &C, const JobAction &JA,
                                            const InputInfo &Output,
                                            const InputInfoList &Inputs,
@@ -621,7 +599,7 @@
   // TODO: Make this work unconditionally once Flang is mature enough.
   if (D.IsFlangMode() && Args.hasArg(options::OPT_flang_experimental_exec)) {
     addFortranRuntimeLibraryPath(ToolChain, Args, CmdArgs);
-    addFortranLinkerFlags(CmdArgs);
+    addFortranRuntimeLibs(CmdArgs);
   }
 
   if (!Args.hasArg(options::OPT_nostdlib, options::OPT_r)) {
Index: clang/lib/Driver/ToolChains/Darwin.cpp
===================================================================
--- clang/lib/Driver/ToolChains/Darwin.cpp
+++ clang/lib/Driver/ToolChains/Darwin.cpp
@@ -635,6 +635,18 @@
     InputFileList.push_back(II.getFilename());
   }
 
+  // Additional linker set-up and flags for Fortran. This is required in order
+  // to generate executables.
+  //
+  // NOTE: Generating executables by Flang is considered an "experimental"
+  // feature and hence this is guarded with a command line option.
+  // TODO: Make this work unconditionally once Flang is mature enough.
+  if (getToolChain().getDriver().IsFlangMode() &&
+      Args.hasArg(options::OPT_flang_experimental_exec)) {
+    addFortranRuntimeLibraryPath(getToolChain(), Args, CmdArgs);
+    addFortranRuntimeLibs(CmdArgs);
+  }
+
   if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs))
     addOpenMPRuntime(CmdArgs, getToolChain(), Args);
 
Index: clang/lib/Driver/ToolChains/CommonArgs.h
===================================================================
--- clang/lib/Driver/ToolChains/CommonArgs.h
+++ clang/lib/Driver/ToolChains/CommonArgs.h
@@ -120,6 +120,14 @@
                       bool ForceStaticHostRuntime = false,
                       bool IsOffloadingHost = false, bool GompNeedsRT = false);
 
+/// Adds Fortran runtime libraries to \p CmdArgs.
+void addFortranRuntimeLibs(llvm::opt::ArgStringList &CmdArgs);
+
+/// Adds the path for the Fortran runtime libraries to \p CmdArgs.
+void addFortranRuntimeLibraryPath(const ToolChain &TC,
+                                  const llvm::opt::ArgList &Args,
+                                  llvm::opt::ArgStringList &CmdArgs);
+
 void addHIPRuntimeLibArgs(const ToolChain &TC, const llvm::opt::ArgList &Args,
                           llvm::opt::ArgStringList &CmdArgs);
 
Index: clang/lib/Driver/ToolChains/CommonArgs.cpp
===================================================================
--- clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -745,6 +745,26 @@
   return true;
 }
 
+void tools::addFortranRuntimeLibs(llvm::opt::ArgStringList &CmdArgs) {
+  CmdArgs.push_back("-lFortran_main");
+  CmdArgs.push_back("-lFortranRuntime");
+  CmdArgs.push_back("-lFortranDecimal");
+  CmdArgs.push_back("-lm");
+}
+
+void tools::addFortranRuntimeLibraryPath(const ToolChain &TC,
+                                         const llvm::opt::ArgList &Args,
+                                         ArgStringList &CmdArgs) {
+  // Default to the <driver-path>/../lib directory. This works fine on the
+  // platforms that we have tested so far. We will probably have to re-fine
+  // this in the future. In particular, on some platforms, we may need to use
+  // lib64 instead of lib.
+  SmallString<256> DefaultLibPath =
+      llvm::sys::path::parent_path(TC.getDriver().Dir);
+  llvm::sys::path::append(DefaultLibPath, "lib");
+  CmdArgs.push_back(Args.MakeArgString("-L" + DefaultLibPath));
+}
+
 static void addSanitizerRuntime(const ToolChain &TC, const ArgList &Args,
                                 ArgStringList &CmdArgs, StringRef Sanitizer,
                                 bool IsShared, bool IsWhole) {
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to