Order of libraries and source files in the f18 frontend

When the f18 frontend calls the link editor, put the libraries and object files in the correct order.

Fixes the issues reported here https://github.com/flang-compiler/flang/issues/897

Reviewed By: sscalpone, AlexisPerry

Differential Revision: https://reviews.llvm.org/D84340
This commit is contained in:
Camille Coti 2020-07-27 16:58:39 -06:00 committed by AlexisPerry
parent 6784d82d5b
commit ca0bf440db
1 changed files with 22 additions and 12 deletions

View File

@ -369,20 +369,24 @@ std::string CompileOtherLanguage(std::string path, DriverOptions &driver) {
return {}; return {};
} }
void Link(std::vector<std::string> &relocatables, DriverOptions &driver) { void Link(std::vector<std::string> &liblist, std::vector<std::string> &objects,
DriverOptions &driver) {
if (!ParentProcess()) { if (!ParentProcess()) {
std::vector<char *> argv; std::vector<char *> argv;
for (size_t j{0}; j < driver.F18_FCArgs.size(); ++j) { for (size_t j{0}; j < driver.F18_FCArgs.size(); ++j) {
argv.push_back(driver.F18_FCArgs[j].data()); argv.push_back(driver.F18_FCArgs[j].data());
} }
for (auto &relo : relocatables) { for (auto &obj : objects) {
argv.push_back(relo.data()); argv.push_back(obj.data());
} }
if (!driver.outputPath.empty()) { if (!driver.outputPath.empty()) {
char dashO[3] = "-o"; char dashO[3] = "-o";
argv.push_back(dashO); argv.push_back(dashO);
argv.push_back(driver.outputPath.data()); argv.push_back(driver.outputPath.data());
} }
for (auto &lib : liblist) {
argv.push_back(lib.data());
}
Exec(argv, driver.verbose); Exec(argv, driver.verbose);
} }
} }
@ -397,6 +401,7 @@ int main(int argc, char *const argv[]) {
bool isPGF90{driver.F18_FCArgs.back().rfind("pgf90") != std::string::npos}; bool isPGF90{driver.F18_FCArgs.back().rfind("pgf90") != std::string::npos};
std::list<std::string> args{argList(argc, argv)}; std::list<std::string> args{argList(argc, argv)};
std::vector<std::string> objlist, liblist;
std::string prefix{args.front()}; std::string prefix{args.front()};
args.pop_front(); args.pop_front();
prefix += ": "; prefix += ": ";
@ -413,32 +418,37 @@ int main(int argc, char *const argv[]) {
Fortran::common::IntrinsicTypeDefaultKinds defaultKinds; Fortran::common::IntrinsicTypeDefaultKinds defaultKinds;
std::vector<std::string> fortranSources, otherSources, relocatables; std::vector<std::string> fortranSources, otherSources;
bool anyFiles{false}; bool anyFiles{false};
while (!args.empty()) { while (!args.empty()) {
std::string arg{std::move(args.front())}; std::string arg{std::move(args.front())};
auto dot{arg.rfind(".")};
std::string suffix{arg.substr(dot + 1)};
std::string prefix{arg.substr(0, 2)};
args.pop_front(); args.pop_front();
if (arg.empty()) { if (arg.empty()) {
} else if (arg.at(0) != '-') { } else if (arg.at(0) != '-') {
anyFiles = true; anyFiles = true;
auto dot{arg.rfind(".")};
if (dot == std::string::npos) { if (dot == std::string::npos) {
driver.F18_FCArgs.push_back(arg); driver.F18_FCArgs.push_back(arg);
} else { } else {
std::string suffix{arg.substr(dot + 1)};
if (suffix == "f" || suffix == "F" || suffix == "ff" || if (suffix == "f" || suffix == "F" || suffix == "ff" ||
suffix == "f90" || suffix == "F90" || suffix == "ff90" || suffix == "f90" || suffix == "F90" || suffix == "ff90" ||
suffix == "f95" || suffix == "F95" || suffix == "ff95" || suffix == "f95" || suffix == "F95" || suffix == "ff95" ||
suffix == "cuf" || suffix == "CUF" || suffix == "f18" || suffix == "cuf" || suffix == "CUF" || suffix == "f18" ||
suffix == "F18" || suffix == "ff18") { suffix == "F18" || suffix == "ff18") {
fortranSources.push_back(arg); fortranSources.push_back(arg);
} else if (suffix == "o" || suffix == "a") { } else if (suffix == "o" || suffix == "so") {
relocatables.push_back(arg); objlist.push_back(arg);
} else if (suffix == "a") {
liblist.push_back(arg);
} else { } else {
otherSources.push_back(arg); otherSources.push_back(arg);
} }
} }
} else if (prefix == "-l" || suffix == "a") {
liblist.push_back(arg);
} else if (arg == "-") { } else if (arg == "-") {
fortranSources.push_back("-"); fortranSources.push_back("-");
} else if (arg == "--") { } else if (arg == "--") {
@ -682,17 +692,17 @@ int main(int argc, char *const argv[]) {
for (const auto &path : fortranSources) { for (const auto &path : fortranSources) {
std::string relo{CompileFortran(path, options, driver, defaultKinds)}; std::string relo{CompileFortran(path, options, driver, defaultKinds)};
if (!driver.compileOnly && !relo.empty()) { if (!driver.compileOnly && !relo.empty()) {
relocatables.push_back(relo); objlist.push_back(relo);
} }
} }
for (const auto &path : otherSources) { for (const auto &path : otherSources) {
std::string relo{CompileOtherLanguage(path, driver)}; std::string relo{CompileOtherLanguage(path, driver)};
if (!driver.compileOnly && !relo.empty()) { if (!driver.compileOnly && !relo.empty()) {
relocatables.push_back(relo); objlist.push_back(relo);
} }
} }
if (!relocatables.empty()) { if (!objlist.empty()) {
Link(relocatables, driver); Link(liblist, objlist, driver);
} }
return exitStatus; return exitStatus;
} }