Skip to content

Commit e5769df

Browse files
committed
[clang][driver] Special care for -l and -Wl, flags in config files
Currently, if a -l (or -Wl,) 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 linker 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 -l and -Wl, 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 baaf111 commit e5769df

File tree

6 files changed

+93
-0
lines changed

6 files changed

+93
-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: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,6 +1062,19 @@ bool Driver::readConfigFile(StringRef FileName,
10621062
for (Arg *A : *NewOptions)
10631063
A->claim();
10641064

1065+
// Filter out all -l and -Wl, options, put them into a separate list and erase
1066+
// from the original list of configuration file options. These will be used
1067+
// only when linking and appended after all of the command line options.
1068+
auto NewLinkerIns = std::make_unique<InputArgList>();
1069+
for (Arg *A : NewOptions->filtered(options::OPT_Wl_COMMA, options::OPT_l)) {
1070+
const Arg *BaseArg = &A->getBaseArg();
1071+
if (BaseArg == A)
1072+
BaseArg = nullptr;
1073+
appendOneArg(*NewLinkerIns, A, BaseArg);
1074+
}
1075+
NewOptions->eraseArg(options::OPT_Wl_COMMA);
1076+
NewOptions->eraseArg(options::OPT_l);
1077+
10651078
if (!CfgOptions)
10661079
CfgOptions = std::move(NewOptions);
10671080
else {
@@ -1073,6 +1086,19 @@ bool Driver::readConfigFile(StringRef FileName,
10731086
appendOneArg(*CfgOptions, Opt, BaseArg);
10741087
}
10751088
}
1089+
1090+
if (!CfgLinkerInputs)
1091+
CfgLinkerInputs = std::move(NewLinkerIns);
1092+
else {
1093+
// If this is a subsequent config file, append options to the previous one.
1094+
for (auto *Opt : *NewLinkerIns) {
1095+
const Arg *BaseArg = &Opt->getBaseArg();
1096+
if (BaseArg == Opt)
1097+
BaseArg = nullptr;
1098+
appendOneArg(*CfgLinkerInputs, Opt, BaseArg);
1099+
}
1100+
}
1101+
10761102
ConfigFiles.push_back(std::string(CfgFileName));
10771103
return false;
10781104
}
@@ -1250,6 +1276,7 @@ Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) {
12501276
if (!ContainsError)
12511277
ContainsError = loadConfigFiles();
12521278
bool HasConfigFile = !ContainsError && (CfgOptions.get() != nullptr);
1279+
bool HasConfigLinkerIn = !ContainsError && CfgLinkerInputs;
12531280

12541281
// All arguments, from both config file and command line.
12551282
InputArgList Args = std::move(HasConfigFile ? std::move(*CfgOptions)
@@ -1552,6 +1579,15 @@ Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) {
15521579
// Construct the list of inputs.
15531580
InputList Inputs;
15541581
BuildInputs(C->getDefaultToolChain(), *TranslatedArgs, Inputs);
1582+
if (HasConfigLinkerIn && Inputs.size()) {
1583+
Arg *FinalPhaseArg;
1584+
if (getFinalPhase(*TranslatedArgs, &FinalPhaseArg) == phases::Link) {
1585+
DerivedArgList TranslatedLinkerIns(*CfgLinkerInputs);
1586+
for (Arg *A : *CfgLinkerInputs)
1587+
TranslatedLinkerIns.append(A);
1588+
BuildInputs(C->getDefaultToolChain(), TranslatedLinkerIns, Inputs);
1589+
}
1590+
}
15551591

15561592
// Populate the tool chains for the offloading devices, if any.
15571593
CreateOffloadingDeviceToolChains(*C, Inputs);

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 -Wl,-Bstatic -lhappy -Wl,-Bdynamic

clang/test/Driver/config-file.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,29 @@
8282
// CHECK-TWO-CONFIGS: -isysroot
8383
// CHECK-TWO-CONFIGS-SAME: /opt/data
8484
// CHECK-TWO-CONFIGS-SAME: -Wall
85+
86+
//--- The linker input flags should be moved to the end of input list and appear only when linking.
87+
// RUN: %clang --target=aarch64-unknown-linux-gnu --config %S/Inputs/config-l.cfg %s -lmylib -Wl,foo.a -### 2>&1 | FileCheck %s -check-prefix CHECK-LINKING
88+
// RUN: %clang --target=aarch64-unknown-linux-gnu --config %S/Inputs/config-l.cfg -fopenmp %s -lmylib -Wl,foo.a -### 2>&1 | FileCheck %s -check-prefix CHECK-LINKING-LIBOMP-GOES-LAST
89+
// RUN: %clang --target=aarch64-unknown-linux-gnu --config %S/Inputs/config-l.cfg -S %s -### 2>&1 | FileCheck %s -check-prefix CHECK-NOLINKING
90+
// RUN: %clang --target=aarch64-unknown-linux-gnu --config %S/Inputs/config-l.cfg -fopenmp -S %s -### 2>&1 | FileCheck %s -check-prefix CHECK-NOLINKING
91+
// RUN: %clang --target=x86_64-pc-windows-msvc --config %S/Inputs/config-l.cfg %s -lmylib -Wl,foo.lib -### 2>&1 | FileCheck %s -check-prefix CHECK-LINKING-MSVC
92+
// RUN: %clang --target=x86_64-pc-windows-msvc --config %S/Inputs/config-l.cfg -S %s -### 2>&1 | FileCheck %s -check-prefix CHECK-NOLINKING-MSVC
93+
// CHECK-LINKING: Configuration file: {{.*}}Inputs{{.}}config-l.cfg
94+
// CHECK-LINKING: "-Wall"
95+
// CHECK-LINKING: "{{.*}}-{{.*}}.o" "-lmylib" "foo.a" "-lm" "-Bstatic" "-lhappy" "-Bdynamic"
96+
// CHECK-LINKING-LIBOMP-GOES-LAST: Configuration file: {{.*}}Inputs{{.}}config-l.cfg
97+
// CHECK-LINKING-LIBOMP-GOES-LAST: "-Wall"
98+
// CHECK-LINKING-LIBOMP-GOES-LAST-SAME: "-fopenmp"
99+
// CHECK-LINKING-LIBOMP-GOES-LAST: "{{.*}}-{{.*}}.o" "-lmylib" "foo.a" "-lm" "-Bstatic" "-lhappy" "-Bdynamic"
100+
// CHECK-LINKING-LIBOMP-GOES-LAST-SAME: "-lomp"
101+
// CHECK-NOLINKING: Configuration file: {{.*}}Inputs{{.}}config-l.cfg
102+
// CHECK-NOLINKING: "-Wall"
103+
// CHECK-NOLINKING-NO: "-lm" "-Bstatic" "-lhappy" "-Bdynamic"
104+
// CHECK-NOLINKING-NO: "-lomp"
105+
// CHECK-LINKING-MSVC: Configuration file: {{.*}}Inputs{{.}}config-l.cfg
106+
// CHECK-LINKING-MSVC: "-Wall"
107+
// CHECK-LINKING-MSVC: "{{.*}}-{{.*}}.o" "mylib.lib" "foo.lib" "m.lib" "-Bstatic" "happy.lib" "-Bdynamic"
108+
// CHECK-NOLINKING-MSVC: Configuration file: {{.*}}Inputs{{.}}config-l.cfg
109+
// CHECK-NOLINKING-MSVC: "-Wall"
110+
// CHECK-NOLINKING-MSVC-NO: "m.lib" "-Bstatic" "happy.lib" "-Bdynamic"

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 -Wl,-Bstatic -lhappy -Wl,-Bdynamic

flang/test/Driver/config-file.f90

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,29 @@
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 linker input flags should be moved to the end of input list and appear only when linking.
66+
! RUN: %flang --target=aarch64-unknown-linux-gnu --config %S/Inputs/config-l.cfg %s -lmylib -Wl,foo.a -### 2>&1 | FileCheck %s -check-prefix CHECK-LINKING
67+
! RUN: %flang --target=aarch64-unknown-linux-gnu --config %S/Inputs/config-l.cfg -fopenmp %s -lmylib -Wl,foo.a -### 2>&1 | FileCheck %s -check-prefix CHECK-LINKING-LIBOMP-GOES-LAST
68+
! RUN: %flang --target=aarch64-unknown-linux-gnu --config %S/Inputs/config-l.cfg -S %s -### 2>&1 | FileCheck %s -check-prefix CHECK-NOLINKING
69+
! RUN: %flang --target=aarch64-unknown-linux-gnu --config %S/Inputs/config-l.cfg -fopenmp -S %s -### 2>&1 | FileCheck %s -check-prefix CHECK-NOLINKING
70+
! RUN: %flang --target=x86_64-pc-windows-msvc --config %S/Inputs/config-l.cfg %s -lmylib -Wl,foo.lib -### 2>&1 | FileCheck %s -check-prefix CHECK-LINKING-MSVC
71+
! RUN: %flang --target=x86_64-pc-windows-msvc --config %S/Inputs/config-l.cfg -S %s -### 2>&1 | FileCheck %s -check-prefix CHECK-NOLINKING-MSVC
72+
! CHECK-LINKING: Configuration file: {{.*}}Inputs{{.}}config-l.cfg
73+
! CHECK-LINKING: "-ffast-math"
74+
! CHECK-LINKING: "{{.*}}-{{.*}}.o" "-lmylib" "foo.a" "-lm" "-Bstatic" "-lhappy" "-Bdynamic"
75+
! CHECK-LINKING-LIBOMP-GOES-LAST: Configuration file: {{.*}}Inputs{{.}}config-l.cfg
76+
! CHECK-LINKING-LIBOMP-GOES-LAST: "-ffast-math"
77+
! CHECK-LINKING-LIBOMP-GOES-LAST-SAME: "-fopenmp"
78+
! CHECK-LINKING-LIBOMP-GOES-LAST: "{{.*}}-{{.*}}.o" "-lmylib" "foo.a" "-lm" "-Bstatic" "-lhappy" "-Bdynamic"
79+
! CHECK-LINKING-LIBOMP-GOES-LAST-SAME: "-lomp"
80+
! CHECK-NOLINKING: Configuration file: {{.*}}Inputs{{.}}config-l.cfg
81+
! CHECK-NOLINKING: "-ffast-math"
82+
! CHECK-NOLINKING-NO: "-lm" "-Bstatic" "-lhappy" "-Bdynamic"
83+
! CHECK-NOLINKING-NO: "-lomp"
84+
! CHECK-LINKING-MSVC: Configuration file: {{.*}}Inputs{{.}}config-l.cfg
85+
! CHECK-LINKING-MSVC: "-ffast-math"
86+
! CHECK-LINKING-MSVC: "{{.*}}-{{.*}}.o" "mylib.lib" "foo.lib" "m.lib" "-Bstatic" "happy.lib" "-Bdynamic"
87+
! CHECK-NOLINKING-MSVC: Configuration file: {{.*}}Inputs{{.}}config-l.cfg
88+
! CHECK-NOLINKING-MSVC: "-ffast-math"
89+
! CHECK-NOLINKING-MSVC-NO: "m.lib" "-Bstatic" "happy.lib" "-Bdynamic"

0 commit comments

Comments
 (0)