Skip to content

Commit 11a9ef9

Browse files
kateinoigakukunAnka
authored andcommitted
[ClangImporter] Support wasi-libc.modulemap import with VFS
1 parent 832764d commit 11a9ef9

File tree

1 file changed

+31
-10
lines changed

1 file changed

+31
-10
lines changed

lib/ClangImporter/ClangIncludePaths.cpp

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,13 @@ static llvm::Optional<Path> getGlibcModuleMapPath(
101101
/*isArchSpecific*/ true, vfs);
102102
}
103103

104+
static llvm::Optional<Path> getWASILibcModuleMapPath(
105+
SearchPathOptions &Opts, const llvm::Triple &triple,
106+
const llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> &vfs) {
107+
return getActualModuleMapPath("wasi-libc.modulemap", Opts, triple,
108+
/*isArchSpecific*/ true, vfs);
109+
}
110+
104111
static llvm::Optional<Path> getLibStdCxxModuleMapPath(
105112
SearchPathOptions &opts, const llvm::Triple &triple,
106113
const llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> &vfs) {
@@ -195,11 +202,32 @@ static bool shouldInjectGlibcModulemap(const llvm::Triple &triple) {
195202
triple.isAndroid();
196203
}
197204

205+
static bool shouldInjectWASILibcModulemap(const llvm::Triple &triple) {
206+
return triple.isOSWASI();
207+
}
208+
198209
static SmallVector<std::pair<std::string, std::string>, 2> getGlibcFileMapping(
199210
ASTContext &ctx,
200211
const llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> &vfs) {
201212
const llvm::Triple &triple = ctx.LangOpts.Target;
202-
if (!shouldInjectGlibcModulemap(triple))
213+
214+
std::string auxiliaryHeaderName;
215+
llvm::Optional<Path> maybeActualModuleMapPath;
216+
if (shouldInjectGlibcModulemap(triple)) {
217+
auxiliaryHeaderName = "SwiftGlibc.h";
218+
maybeActualModuleMapPath = getGlibcModuleMapPath(ctx.SearchPathOpts, triple, vfs);
219+
} else if (shouldInjectWASILibcModulemap(triple)) {
220+
auxiliaryHeaderName = "SwiftWASILibc.h";
221+
maybeActualModuleMapPath = getWASILibcModuleMapPath(ctx.SearchPathOpts, triple, vfs);
222+
} else {
223+
return {};
224+
}
225+
226+
Path actualModuleMapPath;
227+
if (auto path = maybeActualModuleMapPath)
228+
actualModuleMapPath = path.value();
229+
else
230+
// FIXME: Emit a warning of some kind.
203231
return {};
204232

205233
// Extract the Glibc path from Clang driver.
@@ -225,24 +253,17 @@ static SmallVector<std::pair<std::string, std::string>, 2> getGlibcFileMapping(
225253
return {};
226254
}
227255

228-
Path actualModuleMapPath;
229-
if (auto path = getGlibcModuleMapPath(ctx.SearchPathOpts, triple, vfs))
230-
actualModuleMapPath = path.value();
231-
else
232-
// FIXME: Emit a warning of some kind.
233-
return {};
234-
235256
// TODO: remove the SwiftGlibc.h header and reference all Glibc headers
236257
// directly from the modulemap.
237258
Path actualHeaderPath = actualModuleMapPath;
238259
llvm::sys::path::remove_filename(actualHeaderPath);
239-
llvm::sys::path::append(actualHeaderPath, "SwiftGlibc.h");
260+
llvm::sys::path::append(actualHeaderPath, auxiliaryHeaderName);
240261

241262
Path injectedModuleMapPath(glibcDir);
242263
llvm::sys::path::append(injectedModuleMapPath, "module.modulemap");
243264

244265
Path injectedHeaderPath(glibcDir);
245-
llvm::sys::path::append(injectedHeaderPath, "SwiftGlibc.h");
266+
llvm::sys::path::append(injectedHeaderPath, auxiliaryHeaderName);
246267

247268
return {
248269
{std::string(injectedModuleMapPath), std::string(actualModuleMapPath)},

0 commit comments

Comments
 (0)