Skip to content

Commit cfac721

Browse files
authored
Update removal of linker flags (#628)
1 parent 8058482 commit cfac721

File tree

1 file changed

+29
-27
lines changed

1 file changed

+29
-27
lines changed

server/src/printers/NativeMakefilePrinter.cpp

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,31 @@ namespace printer {
6666
}
6767
}
6868

69-
static void removeLinkerFlag(std::string &argument, std::string const &flag) {
69+
static bool removeLinkerFlag(std::string &argument, std::string const &flag, bool isFlagNext) {
7070
auto options = StringUtils::split(argument, ',');
7171
size_t erased = CollectionUtils::erase_if(options, [&flag](std::string const &option) {
72-
return StringUtils::startsWith(option, flag);
72+
return StringUtils::startsWith(option, flag + '=');
7373
});
74+
std::vector<std::string> result;
75+
for (std::string const &option : options) {
76+
if (option == flag) {
77+
isFlagNext = true;
78+
erased++;
79+
continue;
80+
}
81+
if (isFlagNext && option != "-Wl") {
82+
isFlagNext = false;
83+
erased++;
84+
continue;
85+
}
86+
result.push_back(option);
87+
}
7488
if (erased == 0) {
75-
return;
89+
return false;
7690
}
77-
argument = StringUtils::joinWith(options, ",");
91+
argument = StringUtils::joinWith(result, ",");
7892
eraseIfWlOnly(argument);
93+
return isFlagNext;
7994
}
8095

8196
// transforms -Wl,<arg>,<arg2>... to <arg> <arg2>...
@@ -92,27 +107,12 @@ namespace printer {
92107
argument = StringUtils::joinWith(options, " ");
93108
}
94109

95-
static void removeScriptFlag(std::string &argument) {
96-
removeLinkerFlag(argument, "--version-script");
110+
static bool removeScriptFlag(std::string &argument, bool isFlagNext) {
111+
return removeLinkerFlag(argument, "--version-script", isFlagNext);
97112
}
98113

99-
static void removeSonameFlag(std::string &argument) {
100-
auto options = StringUtils::split(argument, ',');
101-
bool isSonameNext = false;
102-
std::vector<std::string> result;
103-
for (std::string const &option : options) {
104-
if (option == "-soname") {
105-
isSonameNext = true;
106-
continue;
107-
}
108-
if (isSonameNext) {
109-
isSonameNext = false;
110-
continue;
111-
}
112-
result.push_back(option);
113-
}
114-
argument = StringUtils::joinWith(result, ",");
115-
eraseIfWlOnly(argument);
114+
static bool removeSonameFlag(std::string &argument, bool isFlagNext) {
115+
return removeLinkerFlag(argument, "-soname", isFlagNext);
116116
}
117117

118118
NativeMakefilePrinter::NativeMakefilePrinter(
@@ -378,9 +378,10 @@ namespace printer {
378378
StringUtils::startsWith(argument, libraryDirOption) ||
379379
StringUtils::startsWith(argument, linkFlag);
380380
});
381+
bool isScriptNext = false, isSonameNext = false;
381382
for (std::string &argument : dynamicLinkCommand.getCommandLine()) {
382-
removeScriptFlag(argument);
383-
removeSonameFlag(argument);
383+
isScriptNext = removeScriptFlag(argument, isScriptNext);
384+
isSonameNext = removeSonameFlag(argument, isSonameNext);
384385
}
385386
dynamicLinkCommand.setOptimizationLevel(OPTIMIZATION_FLAG);
386387
dynamicLinkCommand.addFlagsToBegin(
@@ -539,9 +540,10 @@ namespace printer {
539540
CompilationUtils::getCompilerName(linkCommand.getBuildTool())));
540541
}
541542
std::vector <std::string> libraryDirectoriesFlags;
543+
bool isScriptNext = false, isSonameNext = false;
542544
for (std::string &argument : linkCommand.getCommandLine()) {
543-
removeScriptFlag(argument);
544-
removeSonameFlag(argument);
545+
isScriptNext = removeScriptFlag(argument, isScriptNext);
546+
isSonameNext = removeSonameFlag(argument, isSonameNext);
545547
auto optionalLibraryAbsolutePath =
546548
getLibraryAbsolutePath(argument, linkCommand.getDirectory());
547549
if (optionalLibraryAbsolutePath.has_value()) {

0 commit comments

Comments
 (0)