Skip to content

Commit 556e65b

Browse files
cotizmodem
authored andcommitted
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 flang-compiler/flang#897 Reviewed By: sscalpone, AlexisPerry Differential Revision: https://reviews.llvm.org/D84340 (cherry picked from commit ca0bf44)
1 parent cea0ff3 commit 556e65b

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

flang/tools/f18/f18.cpp

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -368,20 +368,24 @@ std::string CompileOtherLanguage(std::string path, DriverOptions &driver) {
368368
return {};
369369
}
370370

371-
void Link(std::vector<std::string> &relocatables, DriverOptions &driver) {
371+
void Link(std::vector<std::string> &liblist, std::vector<std::string> &objects,
372+
DriverOptions &driver) {
372373
if (!ParentProcess()) {
373374
std::vector<char *> argv;
374375
for (size_t j{0}; j < driver.F18_FCArgs.size(); ++j) {
375376
argv.push_back(driver.F18_FCArgs[j].data());
376377
}
377-
for (auto &relo : relocatables) {
378-
argv.push_back(relo.data());
378+
for (auto &obj : objects) {
379+
argv.push_back(obj.data());
379380
}
380381
if (!driver.outputPath.empty()) {
381382
char dashO[3] = "-o";
382383
argv.push_back(dashO);
383384
argv.push_back(driver.outputPath.data());
384385
}
386+
for (auto &lib : liblist) {
387+
argv.push_back(lib.data());
388+
}
385389
Exec(argv, driver.verbose);
386390
}
387391
}
@@ -396,6 +400,7 @@ int main(int argc, char *const argv[]) {
396400
bool isPGF90{driver.F18_FCArgs.back().rfind("pgf90") != std::string::npos};
397401

398402
std::list<std::string> args{argList(argc, argv)};
403+
std::vector<std::string> objlist, liblist;
399404
std::string prefix{args.front()};
400405
args.pop_front();
401406
prefix += ": ";
@@ -412,32 +417,37 @@ int main(int argc, char *const argv[]) {
412417

413418
Fortran::common::IntrinsicTypeDefaultKinds defaultKinds;
414419

415-
std::vector<std::string> fortranSources, otherSources, relocatables;
420+
std::vector<std::string> fortranSources, otherSources;
416421
bool anyFiles{false};
417422

418423
while (!args.empty()) {
419424
std::string arg{std::move(args.front())};
425+
auto dot{arg.rfind(".")};
426+
std::string suffix{arg.substr(dot + 1)};
427+
std::string prefix{arg.substr(0, 2)};
420428
args.pop_front();
421429
if (arg.empty()) {
422430
} else if (arg.at(0) != '-') {
423431
anyFiles = true;
424-
auto dot{arg.rfind(".")};
425432
if (dot == std::string::npos) {
426433
driver.F18_FCArgs.push_back(arg);
427434
} else {
428-
std::string suffix{arg.substr(dot + 1)};
429435
if (suffix == "f" || suffix == "F" || suffix == "ff" ||
430436
suffix == "f90" || suffix == "F90" || suffix == "ff90" ||
431437
suffix == "f95" || suffix == "F95" || suffix == "ff95" ||
432438
suffix == "cuf" || suffix == "CUF" || suffix == "f18" ||
433439
suffix == "F18" || suffix == "ff18") {
434440
fortranSources.push_back(arg);
435-
} else if (suffix == "o" || suffix == "a") {
436-
relocatables.push_back(arg);
441+
} else if (suffix == "o" || suffix == "so") {
442+
objlist.push_back(arg);
443+
} else if (suffix == "a") {
444+
liblist.push_back(arg);
437445
} else {
438446
otherSources.push_back(arg);
439447
}
440448
}
449+
} else if (prefix == "-l" || suffix == "a") {
450+
liblist.push_back(arg);
441451
} else if (arg == "-") {
442452
fortranSources.push_back("-");
443453
} else if (arg == "--") {
@@ -679,17 +689,17 @@ int main(int argc, char *const argv[]) {
679689
for (const auto &path : fortranSources) {
680690
std::string relo{CompileFortran(path, options, driver, defaultKinds)};
681691
if (!driver.compileOnly && !relo.empty()) {
682-
relocatables.push_back(relo);
692+
objlist.push_back(relo);
683693
}
684694
}
685695
for (const auto &path : otherSources) {
686696
std::string relo{CompileOtherLanguage(path, driver)};
687697
if (!driver.compileOnly && !relo.empty()) {
688-
relocatables.push_back(relo);
698+
objlist.push_back(relo);
689699
}
690700
}
691-
if (!relocatables.empty()) {
692-
Link(relocatables, driver);
701+
if (!objlist.empty()) {
702+
Link(liblist, objlist, driver);
693703
}
694704
return exitStatus;
695705
}

0 commit comments

Comments
 (0)