Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linking with MSVC toolchain #75

Open
wants to merge 4 commits into
base: release_60
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/Driver/ToolChains/CommonArgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ bool tools::needFortranLibs(const Driver &D, const ArgList &Args) {
}

/// \brief Determine if Fortran "main" object is needed
static bool needFortranMain(const Driver &D, const ArgList &Args) {
bool tools::needFortranMain(const Driver &D, const ArgList &Args) {
return (needFortranLibs(D, Args)
&& (!Args.hasArg(options::OPT_Mnomain) ||
!Args.hasArg(options::OPT_no_fortran_main)));
Expand Down
2 changes: 2 additions & 0 deletions lib/Driver/ToolChains/CommonArgs.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ namespace tools {

bool needFortranLibs(const Driver &D, const llvm::opt::ArgList &Args);

bool needFortranMain(const Driver &D, const llvm::opt::ArgList &Args);

void addPathIfExists(const Driver &D, const Twine &Path,
ToolChain::path_list &Paths);

Expand Down
8 changes: 8 additions & 0 deletions lib/Driver/ToolChains/Flang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,14 @@ if(Args.getAllArgValues(options::OPT_fopenmp_targets_EQ).size() > 0) {
LowerCmdArgs.push_back(Args.MakeArgString(OutFile));
}

bool IsWindowsMSVC = getToolChain().getTriple().isWindowsMSVCEnvironment();
if (IsWindowsMSVC && !Args.hasArg(options::OPT_noFlangLibs)) {
getToolChain().AddFortranStdlibLibArgs(Args, LowerCmdArgs);
for (auto Arg : Args.filtered(options::OPT_noFlangLibs)) {
Arg->claim();
}
}

C.addCommand(llvm::make_unique<Command>(JA, *this, LowerExec, LowerCmdArgs, Inputs));
}

86 changes: 85 additions & 1 deletion lib/Driver/ToolChains/MSVC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ void visualstudio::Linker::ConstructJob(Compilation &C, const JobAction &JA,
Args.MakeArgString(std::string("-out:") + Output.getFilename()));

if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles) &&
!C.getDriver().IsCLMode())
!C.getDriver().IsCLMode() && !C.getDriver().IsFortranMode())
CmdArgs.push_back("-defaultlib:libcmt");

if (!llvm::sys::Process::GetEnv("LIB")) {
Expand Down Expand Up @@ -426,6 +426,11 @@ void visualstudio::Linker::ConstructJob(Compilation &C, const JobAction &JA,
}
}

if (C.getDriver().IsFortranMode()) {
CmdArgs.push_back(Args.MakeArgString(std::string("-libpath:") +
TC.getDriver().Dir + "/../lib"));
}

// Add compiler-rt lib in case if it was explicitly
// specified as an argument for --rtlib option.
if (!Args.hasArg(options::OPT_nostdlib)) {
Expand Down Expand Up @@ -736,6 +741,85 @@ void MSVCToolChain::AddCudaIncludeArgs(const ArgList &DriverArgs,
CudaInstallation.AddCudaIncludeArgs(DriverArgs, CC1Args);
}


void MSVCToolChain::AddFortranStdlibLibArgs(const ArgList &Args,
ArgStringList &CmdArgs) const {
bool staticFlangLibs = false;
bool useOpenMP = false;

if (Args.hasArg(options::OPT_staticFlangLibs)) {
for (auto *A: Args.filtered(options::OPT_staticFlangLibs)) {
A->claim();
staticFlangLibs = true;
}
}

Arg *A = Args.getLastArg(options::OPT_mp, options::OPT_nomp,
options::OPT_fopenmp, options::OPT_fno_openmp);
if (A &&
(A->getOption().matches(options::OPT_mp) ||
A->getOption().matches(options::OPT_fopenmp))) {
useOpenMP = true;
}

if (needFortranMain(getDriver(), Args)) {
// flangmain is always static
CmdArgs.push_back("-linker");
CmdArgs.push_back("/subsystem:console");
CmdArgs.push_back("-linker");
CmdArgs.push_back("/defaultlib:flangmain.lib");
}

if (staticFlangLibs) {
CmdArgs.push_back("-linker");
CmdArgs.push_back("/defaultlib:libflang.lib");
CmdArgs.push_back("-linker");
CmdArgs.push_back("/defaultlib:libflangrti.lib");
CmdArgs.push_back("-linker");
CmdArgs.push_back("/defaultlib:libpgmath.lib");
} else {
CmdArgs.push_back("-linker");
CmdArgs.push_back("/defaultlib:flang.lib");
CmdArgs.push_back("-linker");
CmdArgs.push_back("/defaultlib:flangrti.lib");
CmdArgs.push_back("-linker");
CmdArgs.push_back("/defaultlib:pgmath.lib");
}
if (useOpenMP) {
CmdArgs.push_back("-linker");
CmdArgs.push_back("/nodefaultlib:vcomp.lib");
CmdArgs.push_back("-linker");
CmdArgs.push_back("/nodefaultlib:vcompd.lib");
CmdArgs.push_back("-linker");
CmdArgs.push_back("/defaultlib:libomp.lib");
}
else {
if (staticFlangLibs) {
CmdArgs.push_back("-linker");
CmdArgs.push_back("/defaultlib:libompstub.lib");
} else {
CmdArgs.push_back("-linker");
CmdArgs.push_back("/defaultlib:ompstub.lib");
}
}

// Allways link Fortran executables with Pthreads
// CmdArgs.push_back("-lpthread");

// These options are added clang-cl in Clang.cpp for C/C++
// In clang-cl.exe -MD and -MT control these options, but in
// flang.exe like clang.exe these are different options for
// dependency tracking. Let's assume that if somebody needs
// static flang libs, they need static runtime libs as well.
if (staticFlangLibs) {
CmdArgs.push_back("-linker");
CmdArgs.push_back("/defaultlib:libcmt.lib");
} else {
CmdArgs.push_back("-linker");
CmdArgs.push_back("/defaultlib:msvcrt.lib");
}
}

void MSVCToolChain::printVerboseInfo(raw_ostream &OS) const {
CudaInstallation.print(OS);
}
Expand Down
3 changes: 3 additions & 0 deletions lib/Driver/ToolChains/MSVC.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ class LLVM_LIBRARY_VISIBILITY MSVCToolChain : public ToolChain {
void AddCudaIncludeArgs(const llvm::opt::ArgList &DriverArgs,
llvm::opt::ArgStringList &CC1Args) const override;

void AddFortranStdlibLibArgs(const llvm::opt::ArgList &Args,
llvm::opt::ArgStringList &CmdArgs) const override;

bool getWindowsSDKLibraryPath(std::string &path) const;
/// \brief Check if Universal CRT should be used if available
bool getUniversalCRTLibraryPath(std::string &path) const;
Expand Down