@@ -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 > &SwiftRuntimeLibraries,
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,13 @@ 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
+ auto RuntimeLibEntry = SwiftRuntimeLibraries.find (Flag.str ());
146
+ if (RuntimeLibEntry == SwiftRuntimeLibraries.end ())
147
+ LinkerFlags.emplace_back (Flag.str ());
148
+ else
149
+ RuntimeLibEntry->second = true ;
150
+ }
145
151
}
146
152
}
147
153
return false ;
@@ -153,6 +159,7 @@ extractLinkerFlagsFromObjectFile(const llvm::object::ObjectFile *ObjectFile,
153
159
static bool
154
160
extractLinkerFlagsFromObjectFile (const llvm::object::WasmObjectFile *ObjectFile,
155
161
std::vector<std::string> &LinkerFlags,
162
+ std::unordered_map<std::string, bool > &SwiftRuntimeLibraries,
156
163
CompilerInstance &Instance) {
157
164
// Search for the data segment we hold autolink entries in
158
165
for (const llvm::object::WasmSegment &Segment : ObjectFile->dataSegments ()) {
@@ -164,8 +171,13 @@ extractLinkerFlagsFromObjectFile(const llvm::object::WasmObjectFile *ObjectFile,
164
171
llvm::SmallVector<llvm::StringRef, 4 > SplitFlags;
165
172
SegmentData.split (SplitFlags, llvm::StringRef (" \0 " , 1 ), -1 ,
166
173
/* KeepEmpty=*/ false );
167
- for (const auto &Flag : SplitFlags)
168
- LinkerFlags.push_back (Flag.str ());
174
+ for (const auto &Flag : SplitFlags) {
175
+ auto RuntimeLibEntry = SwiftRuntimeLibraries.find (Flag.str ());
176
+ if (RuntimeLibEntry == SwiftRuntimeLibraries.end ())
177
+ LinkerFlags.emplace_back (Flag.str ());
178
+ else
179
+ RuntimeLibEntry->second = true ;
180
+ }
169
181
}
170
182
}
171
183
return false ;
@@ -178,12 +190,13 @@ extractLinkerFlagsFromObjectFile(const llvm::object::WasmObjectFile *ObjectFile,
178
190
static bool extractLinkerFlags (const llvm::object::Binary *Bin,
179
191
CompilerInstance &Instance,
180
192
StringRef BinaryFileName,
181
- std::vector<std::string> &LinkerFlags) {
193
+ std::vector<std::string> &LinkerFlags,
194
+ std::unordered_map<std::string, bool > &SwiftRuntimeLibraries) {
182
195
if (auto *ObjectFile = llvm::dyn_cast<llvm::object::ELFObjectFileBase>(Bin)) {
183
- return extractLinkerFlagsFromObjectFile (ObjectFile, LinkerFlags, Instance);
196
+ return extractLinkerFlagsFromObjectFile (ObjectFile, LinkerFlags, SwiftRuntimeLibraries, Instance);
184
197
} else if (auto *ObjectFile =
185
198
llvm::dyn_cast<llvm::object::WasmObjectFile>(Bin)) {
186
- return extractLinkerFlagsFromObjectFile (ObjectFile, LinkerFlags, Instance);
199
+ return extractLinkerFlagsFromObjectFile (ObjectFile, LinkerFlags, SwiftRuntimeLibraries, Instance);
187
200
} else if (auto *Archive = llvm::dyn_cast<llvm::object::Archive>(Bin)) {
188
201
llvm::Error Error = llvm::Error::success ();
189
202
for (const auto &Child : Archive->children (Error)) {
@@ -197,7 +210,7 @@ static bool extractLinkerFlags(const llvm::object::Binary *Bin,
197
210
return true ;
198
211
}
199
212
if (extractLinkerFlags (ChildBinary->get (), Instance, BinaryFileName,
200
- LinkerFlags)) {
213
+ LinkerFlags, SwiftRuntimeLibraries )) {
201
214
return true ;
202
215
}
203
216
}
@@ -229,6 +242,15 @@ int autolink_extract_main(ArrayRef<const char *> Args, const char *Argv0,
229
242
230
243
std::vector<std::string> LinkerFlags;
231
244
245
+ // Keep track of whether we've already added the common
246
+ // Swift libraries that ususally have autolink directives
247
+ // in most object fiels
248
+ std::unordered_map<std::string, bool > SwiftRuntimeLibraries = {
249
+ {" -lswiftSwiftOnoneSupport" , false },
250
+ {" -lswiftCore" , false },
251
+ {" -lswift_Concurrency" , false },
252
+ };
253
+
232
254
// Extract the linker flags from the objects.
233
255
for (const auto &BinaryFileName : Invocation.getInputFilenames ()) {
234
256
auto BinaryOwner = llvm::object::createBinary (BinaryFileName);
@@ -245,7 +267,7 @@ int autolink_extract_main(ArrayRef<const char *> Args, const char *Argv0,
245
267
}
246
268
247
269
if (extractLinkerFlags (BinaryOwner->getBinary (), Instance, BinaryFileName,
248
- LinkerFlags)) {
270
+ LinkerFlags, SwiftRuntimeLibraries )) {
249
271
return 1 ;
250
272
}
251
273
}
@@ -264,5 +286,11 @@ int autolink_extract_main(ArrayRef<const char *> Args, const char *Argv0,
264
286
OutOS << Flag << ' \n ' ;
265
287
}
266
288
289
+ for (const auto &RuntimeLib : SwiftRuntimeLibraries) {
290
+ if (RuntimeLib.second )
291
+ OutOS << RuntimeLib.first << ' \n ' ;
292
+ }
293
+
294
+
267
295
return 0 ;
268
296
}
0 commit comments