Skip to content

Commit

Permalink
MSVC linking
Browse files Browse the repository at this point in the history
  • Loading branch information
isuruf committed May 24, 2019
1 parent 9579fb8 commit 5d83087
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 2 deletions.
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
79 changes: 78 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,16 @@ void visualstudio::Linker::ConstructJob(Compilation &C, const JobAction &JA,
}
}

// Add Fortran runtime libraries
if (needFortranLibs(TC.getDriver(), Args)) {
TC.AddFortranStdlibLibArgs(Args, CmdArgs);
} else {
// Claim "no Flang libraries" arguments if any
for (auto Arg : Args.filtered(options::OPT_noFlangLibs)) {
Arg->claim();
}
}

// 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 +746,73 @@ 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;
}

CmdArgs.push_back(Args.MakeArgString(std::string("-libpath:") +
getDriver().Dir + "/../lib"));

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

if (staticFlangLibs) {
CmdArgs.push_back("-defaultlib:libflang");
CmdArgs.push_back("-defaultlib:libflangrti");
CmdArgs.push_back("-defaultlib:libpgmath");
} else {
CmdArgs.push_back("-defaultlib:flang");
CmdArgs.push_back("-defaultlib:flangrti");
CmdArgs.push_back("-defaultlib:pgmath");
}
if (useOpenMP) {
// openmp is added in ConstructJob
}
else {
if (staticFlangLibs) {
CmdArgs.push_back("-defaultlib:libompstub");
} else {
CmdArgs.push_back("-defaultlib:ompstub");
}
}

// 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("-defaultlib:libcmt");
} else {
CmdArgs.push_back("-defaultlib:msvcrt");
}
CmdArgs.push_back("-defaultlib:oldnames");

}

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

0 comments on commit 5d83087

Please sign in to comment.