Skip to content

Commit 01acb56

Browse files
committed
[android][clang importer] overlay the android NDK module map in the clang importer
1 parent d7d730d commit 01acb56

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

lib/ClangImporter/ClangIncludePaths.cpp

+18-2
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,18 @@ getLibcFileMapping(ASTContext &ctx, StringRef modulemapFileName,
206206
// Ideally we would check that all of the headers referenced from the
207207
// modulemap are present.
208208
Path libcDir;
209-
if (auto dir = findFirstIncludeDir(
210-
parsedIncludeArgs, {"inttypes.h", "unistd.h", "stdint.h"}, vfs)) {
209+
if (triple.isAndroid() &&
210+
!clangDriverArgs.getLastArgValue(clang::driver::options::OPT__sysroot_EQ)
211+
.empty()) {
212+
// Swift's driver passes in the Android NDK path using the --sysroot Clang
213+
// flag.
214+
libcDir =
215+
clangDriverArgs.getLastArgValue(clang::driver::options::OPT__sysroot_EQ)
216+
.str();
217+
llvm::sys::path::append(libcDir, "usr", "include");
218+
} else if (auto dir = findFirstIncludeDir(
219+
parsedIncludeArgs, {"inttypes.h", "unistd.h", "stdint.h"},
220+
vfs)) {
211221
libcDir = dir.value();
212222
} else {
213223
ctx.Diags.diagnose(SourceLoc(), diag::libc_not_found, triple.str());
@@ -540,6 +550,12 @@ ClangInvocationFileMapping swift::getClangInvocationFileMapping(
540550
StringRef("SwiftGlibc.h"), vfs);
541551
}
542552
result.redirectedFiles.append(libcFileMapping);
553+
if (triple.isAndroid()) {
554+
// Android uses the android-specific module map that overlays the NDK.
555+
// FIXME: Drop Glibc mapping for android as well.
556+
result.redirectedFiles.append(
557+
getLibcFileMapping(ctx, "android.modulemap", std::nullopt, vfs));
558+
}
543559
// Both libc module maps have the C standard library headers all together in a
544560
// SwiftLibc module. That leads to module cycles with the clang _Builtin_
545561
// modules. e.g. <inttypes.h> includes <stdint.h> on these platforms. The

0 commit comments

Comments
 (0)