Skip to content

Commit 1c4cab5

Browse files
committed
[clang][driver] Special care for -l flags in config files
Currently, if a -l flag is added into a config file (e.g. clang.cfg), it is situated before any object file in the effective command line. If the library requested by given -l flag is static, its symbols will not be made visible to any of the object files provided by the user. Also, the presence of any of the -l flags in a config file confuses the driver whenever the user invokes clang without any parameters. This patch solves both of those problems, by moving the -lm flags specified in a config file to the end of the input list, and by discarding them when there are no other inputs provided by the user, or the last phase is not the linking phase.
1 parent 4d8eb00 commit 1c4cab5

File tree

8 files changed

+77
-0
lines changed

8 files changed

+77
-0
lines changed

clang/include/clang/Driver/Driver.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,9 @@ class Driver {
297297
/// Object that stores strings read from configuration file.
298298
llvm::StringSaver Saver;
299299

300+
/// Linker inputs originated from configuration file.
301+
std::unique_ptr<llvm::opt::InputArgList> CfgLinkerInputs;
302+
300303
/// Arguments originated from configuration file.
301304
std::unique_ptr<llvm::opt::InputArgList> CfgOptions;
302305

clang/lib/Driver/Driver.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,6 +1062,15 @@ bool Driver::readConfigFile(StringRef FileName,
10621062
for (Arg *A : *NewOptions)
10631063
A->claim();
10641064

1065+
std::unique_ptr<InputArgList> NewLinkerIns = std::make_unique<InputArgList>();
1066+
for (Arg *A : NewOptions->filtered(options::OPT_l)) {
1067+
const Arg *BaseArg = &A->getBaseArg();
1068+
if (BaseArg == A)
1069+
BaseArg = nullptr;
1070+
appendOneArg(*NewLinkerIns, A, BaseArg);
1071+
}
1072+
NewOptions->eraseArg(options::OPT_l);
1073+
10651074
if (!CfgOptions)
10661075
CfgOptions = std::move(NewOptions);
10671076
else {
@@ -1073,6 +1082,19 @@ bool Driver::readConfigFile(StringRef FileName,
10731082
appendOneArg(*CfgOptions, Opt, BaseArg);
10741083
}
10751084
}
1085+
1086+
if (!CfgLinkerInputs)
1087+
CfgLinkerInputs = std::move(NewLinkerIns);
1088+
else {
1089+
// If this is a subsequent config file, append options to the previous one.
1090+
for (auto *Opt : *NewLinkerIns) {
1091+
const Arg *BaseArg = &Opt->getBaseArg();
1092+
if (BaseArg == Opt)
1093+
BaseArg = nullptr;
1094+
appendOneArg(*CfgLinkerInputs, Opt, BaseArg);
1095+
}
1096+
}
1097+
10761098
ConfigFiles.push_back(std::string(CfgFileName));
10771099
return false;
10781100
}
@@ -1250,6 +1272,7 @@ Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) {
12501272
if (!ContainsError)
12511273
ContainsError = loadConfigFiles();
12521274
bool HasConfigFile = !ContainsError && (CfgOptions.get() != nullptr);
1275+
bool HasConfigLinkerIn = !ContainsError && (CfgLinkerInputs.get() != nullptr);
12531276

12541277
// All arguments, from both config file and command line.
12551278
InputArgList Args = std::move(HasConfigFile ? std::move(*CfgOptions)
@@ -1552,6 +1575,15 @@ Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) {
15521575
// Construct the list of inputs.
15531576
InputList Inputs;
15541577
BuildInputs(C->getDefaultToolChain(), *TranslatedArgs, Inputs);
1578+
if (HasConfigLinkerIn && Inputs.size()) {
1579+
Arg *FinalPhaseArg;
1580+
if (getFinalPhase(*TranslatedArgs, &FinalPhaseArg) == phases::Link) {
1581+
DerivedArgList TranslatedLinkerIns(*CfgLinkerInputs);
1582+
for (Arg *A : *CfgLinkerInputs)
1583+
TranslatedLinkerIns.append(A);
1584+
BuildInputs(C->getDefaultToolChain(), TranslatedLinkerIns, Inputs);
1585+
}
1586+
}
15551587

15561588
// Populate the tool chains for the offloading devices, if any.
15571589
CreateOffloadingDeviceToolChains(*C, Inputs);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-Wall -fopenmp -lm -lhappy

clang/test/Driver/Inputs/config-l.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-Wall -lm -lhappy

clang/test/Driver/config-file.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,22 @@
8282
// CHECK-TWO-CONFIGS: -isysroot
8383
// CHECK-TWO-CONFIGS-SAME: /opt/data
8484
// CHECK-TWO-CONFIGS-SAME: -Wall
85+
86+
//--- The -l flags should be moved to the end of input list and appear only when linking.
87+
// RUN: %clang --config %S/Inputs/config-l.cfg -o %s.out %s -### 2>&1 | FileCheck %s -check-prefix CHECK-LINKING
88+
// RUN: %clang --config %S/Inputs/config-l-openmp.cfg -o %s.out %s -### 2>&1 | FileCheck %s -check-prefix CHECK-LINKING-LIBOMP-GOES-LAST
89+
// RUN: %clang --config %S/Inputs/config-l.cfg -S %s -### 2>&1 | FileCheck %s -check-prefix CHECK-NOLINKING
90+
// RUN: %clang --config %S/Inputs/config-l-openmp.cfg -S %s -### 2>&1 | FileCheck %s -check-prefix CHECK-NOLINKING
91+
// CHECK-LINKING: Configuration file: {{.*}}Inputs{{.}}config-l.cfg
92+
// CHECK-LINKING: "-Wall"
93+
// CHECK-LINKING: "/tmp/{{.*}}-{{.*}}.o" "-lm" "-lhappy"
94+
// CHECK-LINKING-LIBOMP-GOES-LAST: Configuration file: {{.*}}Inputs{{.}}config-l-openmp.cfg
95+
// CHECK-LINKING-LIBOMP-GOES-LAST: "-Wall"
96+
// CHECK-LINKING-LIBOMP-GOES-LAST-SAME: "-fopenmp"
97+
// CHECK-LINKING-LIBOMP-GOES-LAST: "/tmp/{{.*}}-{{.*}}.o" "-lm" "-lhappy"
98+
// CHECK-LINKING-LIBOMP-GOES-LAST-SAME: "-lomp"
99+
// CHECK-NOLINKING: Configuration file: {{.*}}Inputs{{.}}config-l{{.*}}.cfg
100+
// CHECK-NOLINKING: "-Wall"
101+
// CHECK-NOLINKING-NO: "-lm"
102+
// CHECK-NOLINKING-NO: "-lhappy"
103+
// CHECK-NOLINKING-NO: "-lomp"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-ffast-math -fopenmp -lm -lhappy

flang/test/Driver/Inputs/config-l.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-ffast-math -lm -lhappy

flang/test/Driver/config-file.f90

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,22 @@
6161
! CHECK-TWO-CONFIGS-NEXT: Configuration file: {{.*}}Inputs{{.}}config2{{.}}config-4.cfg
6262
! CHECK-TWO-CONFIGS: -ffp-contract=fast
6363
! CHECK-TWO-CONFIGS: -O3
64+
65+
!--- The -l flags should be moved to the end of input list and appear only when linking.
66+
! RUN: %flang --config %S/Inputs/config-l.cfg -o %s.out %s -### 2>&1 | FileCheck %s -check-prefix CHECK-LINKING
67+
! RUN: %flang --config %S/Inputs/config-l-openmp.cfg -o %s.out %s -### 2>&1 | FileCheck %s -check-prefix CHECK-LINKING-LIBOMP-GOES-LAST
68+
! RUN: %flang --config %S/Inputs/config-l.cfg -S %s -### 2>&1 | FileCheck %s -check-prefix CHECK-NOLINKING
69+
! RUN: %flang --config %S/Inputs/config-l-openmp.cfg -S %s -### 2>&1 | FileCheck %s -check-prefix CHECK-NOLINKING
70+
! CHECK-LINKING: Configuration file: {{.*}}Inputs{{.}}config-l.cfg
71+
! CHECK-LINKING: "-ffast-math"
72+
! CHECK-LINKING: "/tmp/{{.*}}-{{.*}}.o" "-lm" "-lhappy"
73+
! CHECK-LINKING-LIBOMP-GOES-LAST: Configuration file: {{.*}}Inputs{{.}}config-l-openmp.cfg
74+
! CHECK-LINKING-LIBOMP-GOES-LAST: "-ffast-math"
75+
! CHECK-LINKING-LIBOMP-GOES-LAST-SAME: "-fopenmp"
76+
! CHECK-LINKING-LIBOMP-GOES-LAST: "/tmp/{{.*}}-{{.*}}.o" "-lm" "-lhappy"
77+
! CHECK-LINKING-LIBOMP-GOES-LAST-SAME: "-lomp"
78+
! CHECK-NOLINKING: Configuration file: {{.*}}Inputs{{.}}config-l{{.*}}.cfg
79+
! CHECK-NOLINKING: "-ffast-math"
80+
! CHECK-NOLINKING-NO: "-lm"
81+
! CHECK-NOLINKING-NO: "-lhappy"
82+
! CHECK-NOLINKING-NO: "-lomp"

0 commit comments

Comments
 (0)