Skip to content

Commit e601b2a

Browse files
committed
[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
1 parent 3b390a1 commit e601b2a

File tree

6 files changed

+50
-29
lines changed

6 files changed

+50
-29
lines changed

clang/lib/Driver/ToolChains/CommonArgs.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,25 @@ bool tools::addOpenMPRuntime(ArgStringList &CmdArgs, const ToolChain &TC,
748748
return true;
749749
}
750750

751+
void tools::addFortranRuntimeLibs(llvm::opt::ArgStringList &CmdArgs) {
752+
CmdArgs.push_back("-lFortran_main");
753+
CmdArgs.push_back("-lFortranRuntime");
754+
CmdArgs.push_back("-lFortranDecimal");
755+
}
756+
757+
void tools::addFortranRuntimeLibraryPath(const ToolChain &TC,
758+
const llvm::opt::ArgList &Args,
759+
ArgStringList &CmdArgs) {
760+
// Default to the <driver-path>/../lib directory. This works fine on the
761+
// platforms that we have tested so far. We will probably have to re-fine
762+
// this in the future. In particular, on some platforms, we may need to use
763+
// lib64 instead of lib.
764+
SmallString<256> DefaultLibPath =
765+
llvm::sys::path::parent_path(TC.getDriver().Dir);
766+
llvm::sys::path::append(DefaultLibPath, "lib");
767+
CmdArgs.push_back(Args.MakeArgString("-L" + DefaultLibPath));
768+
}
769+
751770
static void addSanitizerRuntime(const ToolChain &TC, const ArgList &Args,
752771
ArgStringList &CmdArgs, StringRef Sanitizer,
753772
bool IsShared, bool IsWhole) {

clang/lib/Driver/ToolChains/CommonArgs.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,14 @@ bool addOpenMPRuntime(llvm::opt::ArgStringList &CmdArgs, const ToolChain &TC,
120120
bool ForceStaticHostRuntime = false,
121121
bool IsOffloadingHost = false, bool GompNeedsRT = false);
122122

123+
/// Adds Fortran runtime libraries to \p CmdArgs.
124+
void addFortranRuntimeLibs(llvm::opt::ArgStringList &CmdArgs);
125+
126+
/// Adds the path for the Fortran runtime libraries to \p CmdArgs.
127+
void addFortranRuntimeLibraryPath(const ToolChain &TC,
128+
const llvm::opt::ArgList &Args,
129+
llvm::opt::ArgStringList &CmdArgs);
130+
123131
void addHIPRuntimeLibArgs(const ToolChain &TC, const llvm::opt::ArgList &Args,
124132
llvm::opt::ArgStringList &CmdArgs);
125133

clang/lib/Driver/ToolChains/Darwin.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,18 @@ void darwin::Linker::ConstructJob(Compilation &C, const JobAction &JA,
635635
InputFileList.push_back(II.getFilename());
636636
}
637637

638+
// Additional linker set-up and flags for Fortran. This is required in order
639+
// to generate executables.
640+
//
641+
// NOTE: Generating executables by Flang is considered an "experimental"
642+
// feature and hence this is guarded with a command line option.
643+
// TODO: Make this work unconditionally once Flang is mature enough.
644+
if (getToolChain().getDriver().IsFlangMode() &&
645+
Args.hasArg(options::OPT_flang_experimental_exec)) {
646+
addFortranRuntimeLibraryPath(getToolChain(), Args, CmdArgs);
647+
addFortranRuntimeLibs(CmdArgs);
648+
}
649+
638650
if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs))
639651
addOpenMPRuntime(CmdArgs, getToolChain(), Args);
640652

clang/lib/Driver/ToolChains/Gnu.cpp

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -382,28 +382,6 @@ void tools::gnutools::StaticLibTool::ConstructJob(
382382
Exec, CmdArgs, Inputs, Output));
383383
}
384384

385-
static void addFortranRuntimeLibraryPath(const ToolChain &TC,
386-
const ArgList &Args,
387-
ArgStringList &CmdArgs) {
388-
// Default to the <driver-path>/../lib directory. This works fine on the
389-
// platforms that we have tested so far. We will probably have to re-fine
390-
// this in the future. In particular:
391-
// * on some platforms, we may need to use lib64 instead of lib
392-
// * this logic should also work on other similar platforms too, so we
393-
// should move it to one of Gnu's parent tool{chain} classes
394-
SmallString<256> DefaultLibPath =
395-
llvm::sys::path::parent_path(TC.getDriver().Dir);
396-
llvm::sys::path::append(DefaultLibPath, "lib");
397-
CmdArgs.push_back(Args.MakeArgString("-L" + DefaultLibPath));
398-
}
399-
400-
static void addFortranLinkerFlags(ArgStringList &CmdArgs) {
401-
CmdArgs.push_back("-lFortran_main");
402-
CmdArgs.push_back("-lFortranRuntime");
403-
CmdArgs.push_back("-lFortranDecimal");
404-
CmdArgs.push_back("-lm");
405-
}
406-
407385
void tools::gnutools::Linker::ConstructJob(Compilation &C, const JobAction &JA,
408386
const InputInfo &Output,
409387
const InputInfoList &Inputs,
@@ -621,7 +599,8 @@ void tools::gnutools::Linker::ConstructJob(Compilation &C, const JobAction &JA,
621599
// TODO: Make this work unconditionally once Flang is mature enough.
622600
if (D.IsFlangMode() && Args.hasArg(options::OPT_flang_experimental_exec)) {
623601
addFortranRuntimeLibraryPath(ToolChain, Args, CmdArgs);
624-
addFortranLinkerFlags(CmdArgs);
602+
addFortranRuntimeLibs(CmdArgs);
603+
CmdArgs.push_back("-lm");
625604
}
626605

627606
if (!Args.hasArg(options::OPT_nostdlib, options::OPT_r)) {

flang/lib/Optimizer/CodeGen/Target.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ fir::CodeGenSpecifics::get(mlir::MLIRContext *ctx, llvm::Triple &&trp,
251251
break;
252252
case llvm::Triple::OSType::Linux:
253253
case llvm::Triple::OSType::Darwin:
254+
case llvm::Triple::OSType::MacOSX:
254255
case llvm::Triple::OSType::Win32:
255256
return std::make_unique<TargetI386>(ctx, std::move(trp),
256257
std::move(kindMap));
@@ -262,6 +263,7 @@ fir::CodeGenSpecifics::get(mlir::MLIRContext *ctx, llvm::Triple &&trp,
262263
break;
263264
case llvm::Triple::OSType::Linux:
264265
case llvm::Triple::OSType::Darwin:
266+
case llvm::Triple::OSType::MacOSX:
265267
case llvm::Triple::OSType::Win32:
266268
return std::make_unique<TargetX86_64>(ctx, std::move(trp),
267269
std::move(kindMap));
@@ -273,6 +275,7 @@ fir::CodeGenSpecifics::get(mlir::MLIRContext *ctx, llvm::Triple &&trp,
273275
break;
274276
case llvm::Triple::OSType::Linux:
275277
case llvm::Triple::OSType::Darwin:
278+
case llvm::Triple::OSType::MacOSX:
276279
case llvm::Triple::OSType::Win32:
277280
return std::make_unique<TargetAArch64>(ctx, std::move(trp),
278281
std::move(kindMap));

flang/test/Driver/linker-flags.f90

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
! invocation. These libraries are added on top of other standard runtime
33
! libraries that the Clang driver will include.
44

5-
! NOTE: The additional linker flags tested here are currently specified in
6-
! clang/lib/Driver/Toolchains/Gnu.cpp. This makes the current implementation GNU
7-
! (Linux) specific. The following line will make sure that this test is skipped
8-
! on Windows. Ideally we should find a more robust way of testing this.
9-
! REQUIRES: shell
10-
! UNSUPPORTED: darwin, macos, system-windows
5+
! NOTE: The additional linker flags tested here are currently only specified for
6+
! GNU and Darwin. The following line will make sure that this test is skipped on
7+
! Windows. If you are running this test on a yet another platform and it is
8+
! failing for you, please either update the following or (preferably) update the
9+
! linker invocation accordingly.
10+
! UNSUPPORTED: system-windows
1111

1212
!------------
1313
! RUN COMMAND

0 commit comments

Comments
 (0)