@@ -89,18 +89,6 @@ static llvm::Optional<Path> getInjectedModuleMapPath(
89
89
return path;
90
90
}
91
91
92
- // / Finds the glibc.modulemap file relative to the provided resource dir.
93
- // /
94
- // / Note that the module map used for Glibc depends on the target we're
95
- // / compiling for, and is not included in the resource directory with the other
96
- // / implicit module maps. It's at {freebsd|linux}/{arch}/glibc.modulemap.
97
- static llvm::Optional<Path> getGlibcModuleMapPath (
98
- SearchPathOptions &Opts, const llvm::Triple &triple,
99
- const llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> &vfs) {
100
- return getActualModuleMapPath (" glibc.modulemap" , Opts, triple,
101
- /* isArchSpecific*/ true , vfs);
102
- }
103
-
104
92
static llvm::Optional<Path> getLibStdCxxModuleMapPath (
105
93
SearchPathOptions &opts, const llvm::Triple &triple,
106
94
const llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> &vfs) {
@@ -190,19 +178,20 @@ createClangArgs(const ASTContext &ctx, clang::driver::Driver &clangDriver) {
190
178
return clangDriverArgs;
191
179
}
192
180
193
- static bool shouldInjectGlibcModulemap (const llvm::Triple &triple) {
181
+ static bool shouldInjectLibcModulemap (const llvm::Triple &triple) {
194
182
return triple.isOSGlibc () || triple.isOSOpenBSD () || triple.isOSFreeBSD () ||
195
- triple.isAndroid ();
183
+ triple.isAndroid () || triple. isOSWASI () ;
196
184
}
197
185
198
- static SmallVector<std::pair<std::string, std::string>, 2 > getGlibcFileMapping (
199
- ASTContext &ctx,
200
- const llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> &vfs) {
186
+ static SmallVector<std::pair<std::string, std::string>, 2 >
187
+ getLibcFileMapping (ASTContext &ctx, StringRef modulemapFileName,
188
+ std::optional<StringRef> maybeHeaderFileName,
189
+ const llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> &vfs) {
201
190
const llvm::Triple &triple = ctx.LangOpts .Target ;
202
- if (!shouldInjectGlibcModulemap (triple))
191
+ if (!shouldInjectLibcModulemap (triple))
203
192
return {};
204
193
205
- // Extract the Glibc path from Clang driver.
194
+ // Extract the libc path from Clang driver.
206
195
auto clangDriver = createClangDriver (ctx, vfs);
207
196
auto clangDriverArgs = createClangArgs (ctx, clangDriver);
208
197
@@ -212,42 +201,47 @@ static SmallVector<std::pair<std::string, std::string>, 2> getGlibcFileMapping(
212
201
clangToolchain.AddClangSystemIncludeArgs (clangDriverArgs, includeArgStrings);
213
202
auto parsedIncludeArgs = parseClangDriverArgs (clangDriver, includeArgStrings);
214
203
215
- // Find the include path that contains Glibc headers. We use three arbitrarily
216
- // chosen headers to determine if the include path actually contains Glibc .
204
+ // Find the include path that contains libc headers. We use three arbitrarily
205
+ // chosen headers to determine if the include path actually contains libc .
217
206
// Ideally we would check that all of the headers referenced from the
218
207
// modulemap are present.
219
- Path glibcDir ;
208
+ Path libcDir ;
220
209
if (auto dir = findFirstIncludeDir (
221
210
parsedIncludeArgs, {" inttypes.h" , " unistd.h" , " stdint.h" }, vfs)) {
222
- glibcDir = dir.value ();
211
+ libcDir = dir.value ();
223
212
} else {
224
- ctx.Diags .diagnose (SourceLoc (), diag::glibc_not_found , triple.str ());
213
+ ctx.Diags .diagnose (SourceLoc (), diag::libc_not_found , triple.str ());
225
214
return {};
226
215
}
227
216
228
217
Path actualModuleMapPath;
229
- if (auto path = getGlibcModuleMapPath (ctx.SearchPathOpts , triple, vfs))
218
+ if (auto path = getActualModuleMapPath (modulemapFileName, ctx.SearchPathOpts ,
219
+ triple, /* isArchSpecific*/ true , vfs))
230
220
actualModuleMapPath = path.value ();
231
221
else
232
222
// FIXME: Emit a warning of some kind.
233
223
return {};
234
224
235
- // TODO: remove the SwiftGlibc.h header and reference all Glibc headers
236
- // directly from the modulemap.
237
- Path actualHeaderPath = actualModuleMapPath;
238
- llvm::sys::path::remove_filename (actualHeaderPath);
239
- llvm::sys::path::append (actualHeaderPath, " SwiftGlibc.h" );
240
-
241
- Path injectedModuleMapPath (glibcDir);
225
+ Path injectedModuleMapPath (libcDir);
242
226
llvm::sys::path::append (injectedModuleMapPath, " module.modulemap" );
227
+ SmallVector<std::pair<std::string, std::string>, 2 > vfsMappings{
228
+ {std::string (injectedModuleMapPath), std::string (actualModuleMapPath)}};
243
229
244
- Path injectedHeaderPath (glibcDir);
245
- llvm::sys::path::append (injectedHeaderPath, " SwiftGlibc.h" );
230
+ if (maybeHeaderFileName) {
231
+ // TODO: remove the SwiftGlibc.h header and reference all Glibc headers
232
+ // directly from the modulemap.
233
+ Path actualHeaderPath = actualModuleMapPath;
234
+ llvm::sys::path::remove_filename (actualHeaderPath);
235
+ llvm::sys::path::append (actualHeaderPath, maybeHeaderFileName.value ());
246
236
247
- return {
248
- {std::string (injectedModuleMapPath), std::string (actualModuleMapPath)},
249
- {std::string (injectedHeaderPath), std::string (actualHeaderPath)},
250
- };
237
+ Path injectedHeaderPath (libcDir);
238
+ llvm::sys::path::append (injectedHeaderPath, maybeHeaderFileName.value ());
239
+
240
+ vfsMappings.push_back (
241
+ {std::string (injectedHeaderPath), std::string (actualHeaderPath)});
242
+ }
243
+
244
+ return vfsMappings;
251
245
}
252
246
253
247
static void getLibStdCxxFileMapping (
@@ -518,8 +512,19 @@ ClangInvocationFileMapping swift::getClangInvocationFileMapping(
518
512
ClangInvocationFileMapping result;
519
513
if (!vfs)
520
514
vfs = llvm::vfs::getRealFileSystem ();
521
- // Android/BSD/Linux Mappings
522
- result.redirectedFiles .append (getGlibcFileMapping (ctx, vfs));
515
+
516
+ const llvm::Triple &triple = ctx.LangOpts .Target ;
517
+
518
+ if (triple.isOSWASI ()) {
519
+ // WASI Mappings
520
+ result.redirectedFiles .append (
521
+ getLibcFileMapping (ctx, " wasi-libc.modulemap" , std::nullopt, vfs));
522
+ } else {
523
+ // Android/BSD/Linux Mappings
524
+ result.redirectedFiles .append (getLibcFileMapping (
525
+ ctx, " glibc.modulemap" , StringRef (" SwiftGlibc.h" ), vfs));
526
+ }
527
+
523
528
if (ctx.LangOpts .EnableCXXInterop )
524
529
getLibStdCxxFileMapping (result, ctx, vfs);
525
530
0 commit comments