Skip to content

Commit ca0bf44

Browse files
cotiAlexisPerry
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
1 parent 6784d82 commit ca0bf44

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
@@ -369,20 +369,24 @@ std::string CompileOtherLanguage(std::string path, DriverOptions &driver) {
369369
return {};
370370
}
371371

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

399403
std::list<std::string> args{argList(argc, argv)};
404+
std::vector<std::string> objlist, liblist;
400405
std::string prefix{args.front()};
401406
args.pop_front();
402407
prefix += ": ";
@@ -413,32 +418,37 @@ int main(int argc, char *const argv[]) {
413418

414419
Fortran::common::IntrinsicTypeDefaultKinds defaultKinds;
415420

416-
std::vector<std::string> fortranSources, otherSources, relocatables;
421+
std::vector<std::string> fortranSources, otherSources;
417422
bool anyFiles{false};
418423

419424
while (!args.empty()) {
420425
std::string arg{std::move(args.front())};
426+
auto dot{arg.rfind(".")};
427+
std::string suffix{arg.substr(dot + 1)};
428+
std::string prefix{arg.substr(0, 2)};
421429
args.pop_front();
422430
if (arg.empty()) {
423431
} else if (arg.at(0) != '-') {
424432
anyFiles = true;
425-
auto dot{arg.rfind(".")};
426433
if (dot == std::string::npos) {
427434
driver.F18_FCArgs.push_back(arg);
428435
} else {
429-
std::string suffix{arg.substr(dot + 1)};
430436
if (suffix == "f" || suffix == "F" || suffix == "ff" ||
431437
suffix == "f90" || suffix == "F90" || suffix == "ff90" ||
432438
suffix == "f95" || suffix == "F95" || suffix == "ff95" ||
433439
suffix == "cuf" || suffix == "CUF" || suffix == "f18" ||
434440
suffix == "F18" || suffix == "ff18") {
435441
fortranSources.push_back(arg);
436-
} else if (suffix == "o" || suffix == "a") {
437-
relocatables.push_back(arg);
442+
} else if (suffix == "o" || suffix == "so") {
443+
objlist.push_back(arg);
444+
} else if (suffix == "a") {
445+
liblist.push_back(arg);
438446
} else {
439447
otherSources.push_back(arg);
440448
}
441449
}
450+
} else if (prefix == "-l" || suffix == "a") {
451+
liblist.push_back(arg);
442452
} else if (arg == "-") {
443453
fortranSources.push_back("-");
444454
} else if (arg == "--") {
@@ -682,17 +692,17 @@ int main(int argc, char *const argv[]) {
682692
for (const auto &path : fortranSources) {
683693
std::string relo{CompileFortran(path, options, driver, defaultKinds)};
684694
if (!driver.compileOnly && !relo.empty()) {
685-
relocatables.push_back(relo);
695+
objlist.push_back(relo);
686696
}
687697
}
688698
for (const auto &path : otherSources) {
689699
std::string relo{CompileOtherLanguage(path, driver)};
690700
if (!driver.compileOnly && !relo.empty()) {
691-
relocatables.push_back(relo);
701+
objlist.push_back(relo);
692702
}
693703
}
694-
if (!relocatables.empty()) {
695-
Link(relocatables, driver);
704+
if (!objlist.empty()) {
705+
Link(liblist, objlist, driver);
696706
}
697707
return exitStatus;
698708
}

0 commit comments

Comments
 (0)