@@ -113,6 +113,7 @@ class AutolinkExtractInvocation {
113
113
static bool
114
114
extractLinkerFlagsFromObjectFile (const llvm::object::ObjectFile *ObjectFile,
115
115
std::vector<std::string> &LinkerFlags,
116
+ std::unordered_map<std::string, bool > &CommonAddedSwiftLibraries,
116
117
CompilerInstance &Instance) {
117
118
// Search for the section we hold autolink entries in
118
119
for (auto &Section : ObjectFile->sections ()) {
@@ -140,8 +141,18 @@ extractLinkerFlagsFromObjectFile(const llvm::object::ObjectFile *ObjectFile,
140
141
llvm::SmallVector<llvm::StringRef, 4 > SplitFlags;
141
142
SectionData->split (SplitFlags, llvm::StringRef (" \0 " , 1 ), -1 ,
142
143
/* KeepEmpty=*/ false );
143
- for (const auto &Flag : SplitFlags)
144
- LinkerFlags.push_back (Flag.str ());
144
+ for (const auto &Flag : SplitFlags) {
145
+ // If it is one of the common Swift library flags,
146
+ // only add if not seen before.
147
+ if (auto It = CommonAddedSwiftLibraries.find (Flag.str ());
148
+ It != CommonAddedSwiftLibraries.end ()) {
149
+ if (It->second == false ) {
150
+ LinkerFlags.push_back (Flag.str ());
151
+ It->second = true ;
152
+ }
153
+ } else
154
+ LinkerFlags.push_back (Flag.str ());
155
+ }
145
156
}
146
157
}
147
158
return false ;
@@ -153,6 +164,7 @@ extractLinkerFlagsFromObjectFile(const llvm::object::ObjectFile *ObjectFile,
153
164
static bool
154
165
extractLinkerFlagsFromObjectFile (const llvm::object::WasmObjectFile *ObjectFile,
155
166
std::vector<std::string> &LinkerFlags,
167
+ std::unordered_map<std::string, bool > &CommonAddedSwiftLibraries,
156
168
CompilerInstance &Instance) {
157
169
// Search for the data segment we hold autolink entries in
158
170
for (const llvm::object::WasmSegment &Segment : ObjectFile->dataSegments ()) {
@@ -164,8 +176,18 @@ extractLinkerFlagsFromObjectFile(const llvm::object::WasmObjectFile *ObjectFile,
164
176
llvm::SmallVector<llvm::StringRef, 4 > SplitFlags;
165
177
SegmentData.split (SplitFlags, llvm::StringRef (" \0 " , 1 ), -1 ,
166
178
/* KeepEmpty=*/ false );
167
- for (const auto &Flag : SplitFlags)
168
- LinkerFlags.push_back (Flag.str ());
179
+ for (const auto &Flag : SplitFlags) {
180
+ // If it is one of the common Swift library flags,
181
+ // only add if not seen before.
182
+ if (auto It = CommonAddedSwiftLibraries.find (Flag.str ());
183
+ It != CommonAddedSwiftLibraries.end ()) {
184
+ if (It->second == false ) {
185
+ LinkerFlags.push_back (Flag.str ());
186
+ It->second = true ;
187
+ }
188
+ } else
189
+ LinkerFlags.push_back (Flag.str ());
190
+ }
169
191
}
170
192
}
171
193
return false ;
@@ -178,12 +200,13 @@ extractLinkerFlagsFromObjectFile(const llvm::object::WasmObjectFile *ObjectFile,
178
200
static bool extractLinkerFlags (const llvm::object::Binary *Bin,
179
201
CompilerInstance &Instance,
180
202
StringRef BinaryFileName,
181
- std::vector<std::string> &LinkerFlags) {
203
+ std::vector<std::string> &LinkerFlags,
204
+ std::unordered_map<std::string, bool > &CommonAddedSwiftLibraries) {
182
205
if (auto *ObjectFile = llvm::dyn_cast<llvm::object::ELFObjectFileBase>(Bin)) {
183
- return extractLinkerFlagsFromObjectFile (ObjectFile, LinkerFlags, Instance);
206
+ return extractLinkerFlagsFromObjectFile (ObjectFile, LinkerFlags, CommonAddedSwiftLibraries, Instance);
184
207
} else if (auto *ObjectFile =
185
208
llvm::dyn_cast<llvm::object::WasmObjectFile>(Bin)) {
186
- return extractLinkerFlagsFromObjectFile (ObjectFile, LinkerFlags, Instance);
209
+ return extractLinkerFlagsFromObjectFile (ObjectFile, LinkerFlags, CommonAddedSwiftLibraries, Instance);
187
210
} else if (auto *Archive = llvm::dyn_cast<llvm::object::Archive>(Bin)) {
188
211
llvm::Error Error = llvm::Error::success ();
189
212
for (const auto &Child : Archive->children (Error)) {
@@ -197,7 +220,7 @@ static bool extractLinkerFlags(const llvm::object::Binary *Bin,
197
220
return true ;
198
221
}
199
222
if (extractLinkerFlags (ChildBinary->get (), Instance, BinaryFileName,
200
- LinkerFlags)) {
223
+ LinkerFlags, CommonAddedSwiftLibraries )) {
201
224
return true ;
202
225
}
203
226
}
@@ -229,6 +252,13 @@ int autolink_extract_main(ArrayRef<const char *> Args, const char *Argv0,
229
252
230
253
std::vector<std::string> LinkerFlags;
231
254
255
+ // Keep track of whether we've already added the common
256
+ // Swift libraries that ususally have autolink directives
257
+ // in most object fiels
258
+ std::unordered_map<std::string, bool > CommonAddedSwiftLibraries = {{" -lswiftSwiftOnoneSupport" , false },
259
+ {" -lswiftCore" , false },
260
+ {" -lswift_Concurrency" , false }};
261
+
232
262
// Extract the linker flags from the objects.
233
263
for (const auto &BinaryFileName : Invocation.getInputFilenames ()) {
234
264
auto BinaryOwner = llvm::object::createBinary (BinaryFileName);
@@ -245,7 +275,7 @@ int autolink_extract_main(ArrayRef<const char *> Args, const char *Argv0,
245
275
}
246
276
247
277
if (extractLinkerFlags (BinaryOwner->getBinary (), Instance, BinaryFileName,
248
- LinkerFlags)) {
278
+ LinkerFlags, CommonAddedSwiftLibraries )) {
249
279
return 1 ;
250
280
}
251
281
}
0 commit comments