Skip to content

Commit 65917ba

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 691e556 commit 65917ba

File tree

6 files changed

+85
-0
lines changed

6 files changed

+85
-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: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,13 +1060,31 @@ bool Driver::readConfigFile(StringRef FileName,
10601060
for (Arg *A : *NewOptions)
10611061
A->claim();
10621062

1063+
// Filter out all -l and -Wl, options, put them into a separate list and erase
1064+
// from the original list of configuration file options. These will be used
1065+
// only when linking and appended after all of the command line options.
1066+
auto NewLinkerIns = std::make_unique<InputArgList>();
1067+
for (Arg *A : NewOptions->filtered(options::OPT_Wl_COMMA, options::OPT_l))
1068+
appendOneArg(*NewLinkerIns, A);
1069+
NewOptions->eraseArg(options::OPT_Wl_COMMA);
1070+
NewOptions->eraseArg(options::OPT_l);
1071+
10631072
if (!CfgOptions)
10641073
CfgOptions = std::move(NewOptions);
10651074
else {
10661075
// If this is a subsequent config file, append options to the previous one.
10671076
for (auto *Opt : *NewOptions)
10681077
appendOneArg(*CfgOptions, Opt);
10691078
}
1079+
1080+
if (!CfgLinkerInputs)
1081+
CfgLinkerInputs = std::move(NewLinkerIns);
1082+
else {
1083+
// If this is a subsequent config file, append options to the previous one.
1084+
for (auto *Opt : *NewLinkerIns)
1085+
appendOneArg(*CfgLinkerInputs, Opt);
1086+
}
1087+
10701088
ConfigFiles.push_back(std::string(CfgFileName));
10711089
return false;
10721090
}
@@ -1244,6 +1262,7 @@ Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) {
12441262
if (!ContainsError)
12451263
ContainsError = loadConfigFiles();
12461264
bool HasConfigFile = !ContainsError && (CfgOptions.get() != nullptr);
1265+
bool HasConfigLinkerIn = !ContainsError && CfgLinkerInputs;
12471266

12481267
// All arguments, from both config file and command line.
12491268
InputArgList Args = std::move(HasConfigFile ? std::move(*CfgOptions)
@@ -1540,6 +1559,15 @@ Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) {
15401559
// Construct the list of inputs.
15411560
InputList Inputs;
15421561
BuildInputs(C->getDefaultToolChain(), *TranslatedArgs, Inputs);
1562+
if (HasConfigLinkerIn && Inputs.size()) {
1563+
Arg *FinalPhaseArg;
1564+
if (getFinalPhase(*TranslatedArgs, &FinalPhaseArg) == phases::Link) {
1565+
DerivedArgList TranslatedLinkerIns(*CfgLinkerInputs);
1566+
for (Arg *A : *CfgLinkerInputs)
1567+
TranslatedLinkerIns.append(A);
1568+
BuildInputs(C->getDefaultToolChain(), TranslatedLinkerIns, Inputs);
1569+
}
1570+
}
15431571

15441572
// Populate the tool chains for the offloading devices, if any.
15451573
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-OPENMP
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" {{.*}}"-fopenmp"
98+
// CHECK-LINKING-LIBOMP-GOES-LAST: "{{.*}}-{{.*}}.o" "-lmylib" "foo.a" "-lm" "-Bstatic" "-lhappy" "-Bdynamic" {{.*}}"-lomp"
99+
// CHECK-NOLINKING: Configuration file: {{.*}}Inputs{{.}}config-l.cfg
100+
// CHECK-NOLINKING: "-Wall"
101+
// CHECK-NOLINKING-NO: "-lm" "-Bstatic" "-lhappy" "-Bdynamic"
102+
// CHECK-NOLINKING-OPENMP: Configuration file: {{.*}}Inputs{{.}}config-l.cfg
103+
// CHECK-NOLINKING-OPENMP: "-Wall" {{.*}}"-fopenmp"
104+
// CHECK-NOLINKING-OPENMP-NO: "-lm" "-Bstatic" "-lhappy" "-Bdynamic" {{.*}}"-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-OPENMP
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" {{.*}}"-fopenmp"
77+
! CHECK-LINKING-LIBOMP-GOES-LAST: "{{.*}}-{{.*}}.o" "-lmylib" "foo.a" "-lm" "-Bstatic" "-lhappy" "-Bdynamic" {{.*}}"-lomp"
78+
! CHECK-NOLINKING: Configuration file: {{.*}}Inputs{{.}}config-l.cfg
79+
! CHECK-NOLINKING: "-ffast-math"
80+
! CHECK-NOLINKING-NO: "-lm" "-Bstatic" "-lhappy" "-Bdynamic"
81+
! CHECK-NOLINKING-OPENMP: Configuration file: {{.*}}Inputs{{.}}config-l.cfg
82+
! CHECK-NOLINKING-OPENMP: "-ffast-math" {{.*}}"-fopenmp"
83+
! CHECK-NOLINKING-OPENMP-NO: "-lm" "-Bstatic" "-lhappy" "-Bdynamic" {{.}}"-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)