[flang][driver] Add support for generating executables on MacOSX/Darwin

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. Also, we don't need to add `-lm` on
MacOSX as `libm` is effectively included in `libSystem` (which is linked
in unconditionally).

Differential Revision: https://reviews.llvm.org/D125628
This commit is contained in:
Andrzej Warzynski 2022-05-15 12:35:37 +01:00 committed by Andrzej Warzynski
parent 3b390a1682
commit e601b2a154
6 changed files with 50 additions and 29 deletions

View File

@ -748,6 +748,25 @@ bool tools::addOpenMPRuntime(ArgStringList &CmdArgs, const ToolChain &TC,
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 <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) {

View File

@ -120,6 +120,14 @@ bool addOpenMPRuntime(llvm::opt::ArgStringList &CmdArgs, const ToolChain &TC,
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);

View File

@ -635,6 +635,18 @@ void darwin::Linker::ConstructJob(Compilation &C, const JobAction &JA,
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);

View File

@ -382,28 +382,6 @@ void tools::gnutools::StaticLibTool::ConstructJob(
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,8 @@ void tools::gnutools::Linker::ConstructJob(Compilation &C, const JobAction &JA,
// 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)) {

View File

@ -251,6 +251,7 @@ fir::CodeGenSpecifics::get(mlir::MLIRContext *ctx, llvm::Triple &&trp,
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 @@ fir::CodeGenSpecifics::get(mlir::MLIRContext *ctx, llvm::Triple &&trp,
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 @@ fir::CodeGenSpecifics::get(mlir::MLIRContext *ctx, llvm::Triple &&trp,
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));

View File

@ -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