Skip to content

Commit b9dd5f9

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 attempts to solve both of the problems, by allowing a split of the arguments list around the pipe character. The head part of the list will be used as before, but the tail part will be appended after the command line flags provided by the user and only when it is known that the linking should occur.
1 parent 691e556 commit b9dd5f9

File tree

6 files changed

+104
-8
lines changed

6 files changed

+104
-8
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: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,34 +1039,63 @@ bool Driver::readConfigFile(StringRef FileName,
10391039
}
10401040

10411041
// Try reading the given file.
1042-
SmallVector<const char *, 32> NewCfgArgs;
1043-
if (llvm::Error Err = ExpCtx.readConfigFile(FileName, NewCfgArgs)) {
1042+
SmallVector<const char *, 32> NewCfgFileArgs;
1043+
if (llvm::Error Err = ExpCtx.readConfigFile(FileName, NewCfgFileArgs)) {
10441044
Diag(diag::err_drv_cannot_read_config_file)
10451045
<< FileName << toString(std::move(Err));
10461046
return true;
10471047
}
10481048

1049+
// Populate head and tail lists. The tail list is used only when linking.
1050+
SmallVector<const char *, 32> NewCfgHeadArgs, NewCfgTailArgs;
1051+
bool SeenPipe = false;
1052+
for (const char *Opt : NewCfgFileArgs) {
1053+
if (!strcmp(Opt, "|")) {
1054+
SeenPipe = true;
1055+
} else {
1056+
if (SeenPipe)
1057+
NewCfgTailArgs.push_back(Opt);
1058+
else
1059+
NewCfgHeadArgs.push_back(Opt);
1060+
}
1061+
}
1062+
10491063
// Read options from config file.
10501064
llvm::SmallString<128> CfgFileName(FileName);
10511065
llvm::sys::path::native(CfgFileName);
1052-
bool ContainErrors;
1053-
auto NewOptions = std::make_unique<InputArgList>(
1054-
ParseArgStrings(NewCfgArgs, /*UseDriverMode=*/true, ContainErrors));
1066+
bool ContainErrors = false;
1067+
auto NewHeadOptions = std::make_unique<InputArgList>(
1068+
ParseArgStrings(NewCfgHeadArgs, /*UseDriverMode=*/true, ContainErrors));
1069+
if (ContainErrors)
1070+
return true;
1071+
auto NewTailOptions = std::make_unique<InputArgList>(
1072+
ParseArgStrings(NewCfgTailArgs, /*UseDriverMode=*/true, ContainErrors));
10551073
if (ContainErrors)
10561074
return true;
10571075

10581076
// Claim all arguments that come from a configuration file so that the driver
10591077
// does not warn on any that is unused.
1060-
for (Arg *A : *NewOptions)
1078+
for (Arg *A : *NewHeadOptions)
1079+
A->claim();
1080+
for (Arg *A : *NewTailOptions)
10611081
A->claim();
10621082

10631083
if (!CfgOptions)
1064-
CfgOptions = std::move(NewOptions);
1084+
CfgOptions = std::move(NewHeadOptions);
10651085
else {
10661086
// If this is a subsequent config file, append options to the previous one.
1067-
for (auto *Opt : *NewOptions)
1087+
for (auto *Opt : *NewHeadOptions)
10681088
appendOneArg(*CfgOptions, Opt);
10691089
}
1090+
1091+
if (!CfgLinkerInputs)
1092+
CfgLinkerInputs = std::move(NewTailOptions);
1093+
else {
1094+
// If this is a subsequent config file, append options to the previous one.
1095+
for (auto *Opt : *NewTailOptions)
1096+
appendOneArg(*CfgLinkerInputs, Opt);
1097+
}
1098+
10701099
ConfigFiles.push_back(std::string(CfgFileName));
10711100
return false;
10721101
}
@@ -1244,6 +1273,7 @@ Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) {
12441273
if (!ContainsError)
12451274
ContainsError = loadConfigFiles();
12461275
bool HasConfigFile = !ContainsError && (CfgOptions.get() != nullptr);
1276+
bool HasConfigLinkerIn = !ContainsError && CfgLinkerInputs;
12471277

12481278
// All arguments, from both config file and command line.
12491279
InputArgList Args = std::move(HasConfigFile ? std::move(*CfgOptions)
@@ -1540,6 +1570,15 @@ Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) {
15401570
// Construct the list of inputs.
15411571
InputList Inputs;
15421572
BuildInputs(C->getDefaultToolChain(), *TranslatedArgs, Inputs);
1573+
if (HasConfigLinkerIn && Inputs.size()) {
1574+
Arg *FinalPhaseArg;
1575+
if (getFinalPhase(*TranslatedArgs, &FinalPhaseArg) == phases::Link) {
1576+
DerivedArgList TranslatedLinkerIns(*CfgLinkerInputs);
1577+
for (Arg *A : *CfgLinkerInputs)
1578+
TranslatedLinkerIns.append(A);
1579+
BuildInputs(C->getDefaultToolChain(), TranslatedLinkerIns, Inputs);
1580+
}
1581+
}
15431582

15441583
// Populate the tool chains for the offloading devices, if any.
15451584
CreateOffloadingDeviceToolChains(*C, Inputs);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-Wall -Wl,--as-needed | -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: "--as-needed" "{{.*}}-{{.*}}.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: "--as-needed" "{{.*}}-{{.*}}.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: "--as-needed" "{{.*}}-{{.*}}.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"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-ffast-math -Wl,--as-needed | -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: "--as-needed" "{{.*}}-{{.*}}.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: "--as-needed" "{{.*}}-{{.*}}.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: "--as-needed" "{{.*}}-{{.*}}.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)