diff --git a/clang/lib/Driver/ToolChains/CommonArgs.h b/clang/lib/Driver/ToolChains/CommonArgs.h --- a/clang/lib/Driver/ToolChains/CommonArgs.h +++ b/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); diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp --- a/clang/lib/Driver/ToolChains/CommonArgs.cpp +++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp @@ -748,6 +748,25 @@ return true; } +void tools::addFortranRuntimeLibs(llvm::opt::ArgStringList &CmdArgs) { + CmdArgs.push_back("-lFortran_main"); + CmdArgs.push_back("-lFortranRuntime"); + CmdArgs.push_back("-lFortranDecimal"); +} + +void tools::addFortranRuntimeLibraryPath(const ToolChain &TC, + const llvm::opt::ArgList &Args, + ArgStringList &CmdArgs) { + // Default to the /../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) { diff --git a/clang/lib/Driver/ToolChains/Darwin.cpp b/clang/lib/Driver/ToolChains/Darwin.cpp --- a/clang/lib/Driver/ToolChains/Darwin.cpp +++ b/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); diff --git a/clang/lib/Driver/ToolChains/Gnu.cpp b/clang/lib/Driver/ToolChains/Gnu.cpp --- a/clang/lib/Driver/ToolChains/Gnu.cpp +++ b/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 /../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,8 @@ // 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); + CmdArgs.push_back("-lm"); } if (!Args.hasArg(options::OPT_nostdlib, options::OPT_r)) { diff --git a/flang/lib/Optimizer/CodeGen/Target.cpp b/flang/lib/Optimizer/CodeGen/Target.cpp --- a/flang/lib/Optimizer/CodeGen/Target.cpp +++ b/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(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(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(ctx, std::move(trp), std::move(kindMap)); diff --git a/flang/test/Driver/linker-flags.f90 b/flang/test/Driver/linker-flags.f90 --- a/flang/test/Driver/linker-flags.f90 +++ b/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