Skip to content

Commit 15d3377

Browse files
[ClangImporter] Support wasi-libc.modulemap import with VFS
1 parent ba061fa commit 15d3377

File tree

1 file changed

+30
-10
lines changed

1 file changed

+30
-10
lines changed

lib/ClangImporter/ClangIncludePaths.cpp

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ static Optional<Path> getGlibcModuleMapPath(
9191
return getActualModuleMapPath("glibc.modulemap", Opts, triple, vfs);
9292
}
9393

94+
static Optional<Path> getWASILibcModuleMapPath(
95+
SearchPathOptions &Opts, const llvm::Triple &triple,
96+
const llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> &vfs) {
97+
return getActualModuleMapPath("wasi-libc.modulemap", Opts, triple, vfs);
98+
}
99+
94100
static Optional<Path> getLibStdCxxModuleMapPath(
95101
SearchPathOptions &opts, const llvm::Triple &triple,
96102
const llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> &vfs) {
@@ -183,11 +189,32 @@ static bool shouldInjectGlibcModulemap(const llvm::Triple &triple) {
183189
triple.isAndroid();
184190
}
185191

192+
static bool shouldInjectWASILibcModulemap(const llvm::Triple &triple) {
193+
return triple.isOSWASI();
194+
}
195+
186196
static SmallVector<std::pair<std::string, std::string>, 2> getGlibcFileMapping(
187197
ASTContext &ctx,
188198
const llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> &vfs) {
189199
const llvm::Triple &triple = ctx.LangOpts.Target;
190-
if (!shouldInjectGlibcModulemap(triple))
200+
201+
std::string auxiliaryHeaderName;
202+
llvm::Optional<Path> maybeActualModuleMapPath;
203+
if (shouldInjectGlibcModulemap(triple)) {
204+
auxiliaryHeaderName = "SwiftGlibc.h";
205+
maybeActualModuleMapPath = getGlibcModuleMapPath(ctx.SearchPathOpts, triple, vfs);
206+
} else if (shouldInjectWASILibcModulemap(triple)) {
207+
auxiliaryHeaderName = "SwiftWASILibc.h";
208+
maybeActualModuleMapPath = getWASILibcModuleMapPath(ctx.SearchPathOpts, triple, vfs);
209+
} else {
210+
return {};
211+
}
212+
213+
Path actualModuleMapPath;
214+
if (auto path = maybeActualModuleMapPath)
215+
actualModuleMapPath = path.value();
216+
else
217+
// FIXME: Emit a warning of some kind.
191218
return {};
192219

193220
// Extract the Glibc path from Clang driver.
@@ -213,24 +240,17 @@ static SmallVector<std::pair<std::string, std::string>, 2> getGlibcFileMapping(
213240
return {};
214241
}
215242

216-
Path actualModuleMapPath;
217-
if (auto path = getGlibcModuleMapPath(ctx.SearchPathOpts, triple, vfs))
218-
actualModuleMapPath = path.value();
219-
else
220-
// FIXME: Emit a warning of some kind.
221-
return {};
222-
223243
// TODO: remove the SwiftGlibc.h header and reference all Glibc headers
224244
// directly from the modulemap.
225245
Path actualHeaderPath = actualModuleMapPath;
226246
llvm::sys::path::remove_filename(actualHeaderPath);
227-
llvm::sys::path::append(actualHeaderPath, "SwiftGlibc.h");
247+
llvm::sys::path::append(actualHeaderPath, auxiliaryHeaderName);
228248

229249
Path injectedModuleMapPath(glibcDir);
230250
llvm::sys::path::append(injectedModuleMapPath, "module.modulemap");
231251

232252
Path injectedHeaderPath(glibcDir);
233-
llvm::sys::path::append(injectedHeaderPath, "SwiftGlibc.h");
253+
llvm::sys::path::append(injectedHeaderPath, auxiliaryHeaderName);
234254

235255
return {
236256
{std::string(injectedModuleMapPath), std::string(actualModuleMapPath)},

0 commit comments

Comments
 (0)