Skip to content

Commit a7d006e

Browse files
Merge pull request #66539 from kateinoigakukun/katei/patch-series/wasi-libc-module
[wasm] Add WASILibc module and its build support
2 parents 9d66ae6 + 643c7c8 commit a7d006e

File tree

5 files changed

+358
-42
lines changed

5 files changed

+358
-42
lines changed

include/swift/AST/DiagnosticsClangImporter.def

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ WARNING(nonmutating_without_mutable_fields,none,
118118

119119
ERROR(module_map_not_found, none, "module map file '%0' not found", (StringRef))
120120

121-
WARNING(glibc_not_found, none,
122-
"glibc not found for '%0'; C stdlib may be unavailable",
121+
WARNING(libc_not_found, none,
122+
"libc not found for '%0'; C stdlib may be unavailable",
123123
(StringRef))
124124
WARNING(libstdcxx_not_found, none,
125125
"libstdc++ not found for '%0'; C++ stdlib may be unavailable",

lib/ClangImporter/ClangIncludePaths.cpp

Lines changed: 45 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -89,18 +89,6 @@ static llvm::Optional<Path> getInjectedModuleMapPath(
8989
return path;
9090
}
9191

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-
10492
static llvm::Optional<Path> getLibStdCxxModuleMapPath(
10593
SearchPathOptions &opts, const llvm::Triple &triple,
10694
const llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> &vfs) {
@@ -190,19 +178,20 @@ createClangArgs(const ASTContext &ctx, clang::driver::Driver &clangDriver) {
190178
return clangDriverArgs;
191179
}
192180

193-
static bool shouldInjectGlibcModulemap(const llvm::Triple &triple) {
181+
static bool shouldInjectLibcModulemap(const llvm::Triple &triple) {
194182
return triple.isOSGlibc() || triple.isOSOpenBSD() || triple.isOSFreeBSD() ||
195-
triple.isAndroid();
183+
triple.isAndroid() || triple.isOSWASI();
196184
}
197185

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) {
201190
const llvm::Triple &triple = ctx.LangOpts.Target;
202-
if (!shouldInjectGlibcModulemap(triple))
191+
if (!shouldInjectLibcModulemap(triple))
203192
return {};
204193

205-
// Extract the Glibc path from Clang driver.
194+
// Extract the libc path from Clang driver.
206195
auto clangDriver = createClangDriver(ctx, vfs);
207196
auto clangDriverArgs = createClangArgs(ctx, clangDriver);
208197

@@ -212,42 +201,47 @@ static SmallVector<std::pair<std::string, std::string>, 2> getGlibcFileMapping(
212201
clangToolchain.AddClangSystemIncludeArgs(clangDriverArgs, includeArgStrings);
213202
auto parsedIncludeArgs = parseClangDriverArgs(clangDriver, includeArgStrings);
214203

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.
217206
// Ideally we would check that all of the headers referenced from the
218207
// modulemap are present.
219-
Path glibcDir;
208+
Path libcDir;
220209
if (auto dir = findFirstIncludeDir(
221210
parsedIncludeArgs, {"inttypes.h", "unistd.h", "stdint.h"}, vfs)) {
222-
glibcDir = dir.value();
211+
libcDir = dir.value();
223212
} else {
224-
ctx.Diags.diagnose(SourceLoc(), diag::glibc_not_found, triple.str());
213+
ctx.Diags.diagnose(SourceLoc(), diag::libc_not_found, triple.str());
225214
return {};
226215
}
227216

228217
Path actualModuleMapPath;
229-
if (auto path = getGlibcModuleMapPath(ctx.SearchPathOpts, triple, vfs))
218+
if (auto path = getActualModuleMapPath(modulemapFileName, ctx.SearchPathOpts,
219+
triple, /*isArchSpecific*/ true, vfs))
230220
actualModuleMapPath = path.value();
231221
else
232222
// FIXME: Emit a warning of some kind.
233223
return {};
234224

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);
242226
llvm::sys::path::append(injectedModuleMapPath, "module.modulemap");
227+
SmallVector<std::pair<std::string, std::string>, 2> vfsMappings{
228+
{std::string(injectedModuleMapPath), std::string(actualModuleMapPath)}};
243229

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());
246236

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;
251245
}
252246

253247
static void getLibStdCxxFileMapping(
@@ -518,8 +512,19 @@ ClangInvocationFileMapping swift::getClangInvocationFileMapping(
518512
ClangInvocationFileMapping result;
519513
if (!vfs)
520514
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+
523528
if (ctx.LangOpts.EnableCXXInterop)
524529
getLibStdCxxFileMapping(result, ctx, vfs);
525530

stdlib/public/Platform/CMakeLists.txt

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,23 @@ add_swift_target_library(swiftMusl ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_OV
114114
TARGET_SDKS MUSL
115115
INSTALL_IN_COMPONENT sdk-overlay)
116116

117+
add_swift_target_library(swiftWASILibc ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_OVERLAY
118+
${swift_platform_sources}
119+
POSIXError.swift
120+
121+
GYB_SOURCES
122+
${swift_platform_gyb_sources}
123+
WASILibc.swift.gyb
124+
125+
SWIFT_COMPILE_FLAGS
126+
${SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS}
127+
${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS}
128+
${swift_platform_compile_flags}
129+
LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}"
130+
TARGET_SDKS WASI
131+
INSTALL_IN_COMPONENT sdk-overlay
132+
DEPENDS wasilibc_modulemap)
133+
117134
add_swift_target_library(swiftCRT ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_OVERLAY
118135
ucrt.swift
119136
${swift_platform_sources}
@@ -234,6 +251,43 @@ add_custom_target(glibc_modulemap DEPENDS ${glibc_modulemap_target_list})
234251
set_property(TARGET glibc_modulemap PROPERTY FOLDER "Miscellaneous")
235252
add_dependencies(sdk-overlay glibc_modulemap)
236253

254+
set(wasilibc_modulemap_target_list)
255+
if("WASI" IN_LIST SWIFT_SDKS)
256+
set(wasilibc_modulemap_source "wasi-libc.modulemap")
257+
foreach(arch ${SWIFT_SDK_WASI_ARCHITECTURES})
258+
set(arch_subdir "${SWIFT_SDK_WASI_LIB_SUBDIR}/${arch}")
259+
set(module_dir "${SWIFTLIB_DIR}/${arch_subdir}")
260+
set(module_dir_static "${SWIFTSTATICLIB_DIR}/${arch_subdir}")
261+
262+
add_custom_command_target(
263+
copy_wasilibc_modulemap_resource
264+
COMMAND
265+
"${CMAKE_COMMAND}" "-E" "make_directory" ${module_dir} ${module_dir_static}
266+
COMMAND
267+
"${CMAKE_COMMAND}" "-E" "copy_if_different"
268+
"${CMAKE_CURRENT_SOURCE_DIR}/${wasilibc_modulemap_source}" ${module_dir}
269+
COMMAND
270+
"${CMAKE_COMMAND}" "-E" "copy_if_different"
271+
"${CMAKE_CURRENT_SOURCE_DIR}/${wasilibc_modulemap_source}" ${module_dir_static}
272+
OUTPUT ${module_dir}/${wasilibc_modulemap_source} ${module_dir_static}/${wasilibc_modulemap_source}
273+
COMMENT "Copying WASILibc modulemap to resource directories")
274+
add_dependencies(sdk-overlay ${copy_wasilibc_modulemap_resource})
275+
list(APPEND wasilibc_modulemap_target_list ${copy_wasilibc_modulemap_resource})
276+
277+
swift_install_in_component(FILES "${wasilibc_modulemap_source}"
278+
DESTINATION "lib/swift/${arch_subdir}"
279+
COMPONENT sdk-overlay)
280+
if(SWIFT_BUILD_STATIC_STDLIB)
281+
swift_install_in_component(FILES "${wasilibc_modulemap_source}"
282+
DESTINATION "lib/swift_static/${arch_subdir}"
283+
COMPONENT sdk-overlay)
284+
endif()
285+
endforeach()
286+
endif()
287+
add_custom_target(wasilibc_modulemap DEPENDS ${wasilibc_modulemap_target_list})
288+
set_property(TARGET wasilibc_modulemap PROPERTY FOLDER "Miscellaneous")
289+
add_dependencies(sdk-overlay wasilibc_modulemap)
290+
237291
if(WINDOWS IN_LIST SWIFT_SDKS)
238292
swift_install_in_component(FILES
239293
ucrt.modulemap
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2020 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
@_exported import SwiftWASILibc // Clang module
14+
15+
// Constants defined by <math.h>
16+
@available(swift, deprecated: 3.0, message: "Please use 'Double.pi' or '.pi' to get the value of correct type and avoid casting.")
17+
public let M_PI = Double.pi
18+
19+
@available(swift, deprecated: 3.0, message: "Please use 'Double.pi / 2' or '.pi / 2' to get the value of correct type and avoid casting.")
20+
public let M_PI_2 = Double.pi / 2
21+
22+
@available(swift, deprecated: 3.0, message: "Please use 'Double.pi / 4' or '.pi / 4' to get the value of correct type and avoid casting.")
23+
public let M_PI_4 = Double.pi / 4
24+
25+
@available(swift, deprecated: 3.0, message: "Please use '2.squareRoot()'.")
26+
public let M_SQRT2 = 2.squareRoot()
27+
28+
@available(swift, deprecated: 3.0, message: "Please use '0.5.squareRoot()'.")
29+
public let M_SQRT1_2 = 0.5.squareRoot()
30+
31+
// Constants defined by <float.h>
32+
@available(swift, deprecated: 3.0, message: "Please use 'T.radix' to get the radix of a FloatingPoint type 'T'.")
33+
public let FLT_RADIX = Double.radix
34+
35+
%for type, prefix in [('Float', 'FLT'), ('Double', 'DBL')]:
36+
// Where does the 1 come from? C counts the usually-implicit leading
37+
// significand bit, but Swift does not. Neither is really right or wrong.
38+
@available(swift, deprecated: 3.0, message: "Please use '${type}.significandBitCount + 1'.")
39+
public let ${prefix}_MANT_DIG = ${type}.significandBitCount + 1
40+
41+
// Where does the 1 come from? C models floating-point numbers as having a
42+
// significand in [0.5, 1), but Swift (following IEEE 754) considers the
43+
// significand to be in [1, 2). This rationale applies to ${prefix}_MIN_EXP
44+
// as well.
45+
@available(swift, deprecated: 3.0, message: "Please use '${type}.greatestFiniteMagnitude.exponent + 1'.")
46+
public let ${prefix}_MAX_EXP = ${type}.greatestFiniteMagnitude.exponent + 1
47+
48+
@available(swift, deprecated: 3.0, message: "Please use '${type}.leastNormalMagnitude.exponent + 1'.")
49+
public let ${prefix}_MIN_EXP = ${type}.leastNormalMagnitude.exponent + 1
50+
51+
@available(swift, deprecated: 3.0, message: "Please use '${type}.greatestFiniteMagnitude' or '.greatestFiniteMagnitude'.")
52+
public let ${prefix}_MAX = ${type}.greatestFiniteMagnitude
53+
54+
@available(swift, deprecated: 3.0, message: "Please use '${type}.ulpOfOne' or '.ulpOfOne'.")
55+
public let ${prefix}_EPSILON = ${type}.ulpOfOne
56+
57+
@available(swift, deprecated: 3.0, message: "Please use '${type}.leastNormalMagnitude' or '.leastNormalMagnitude'.")
58+
public let ${prefix}_MIN = ${type}.leastNormalMagnitude
59+
60+
@available(swift, deprecated: 3.0, message: "Please use '${type}.leastNonzeroMagnitude' or '.leastNonzeroMagnitude'.")
61+
public let ${prefix}_TRUE_MIN = ${type}.leastNonzeroMagnitude
62+
63+
%end
64+
65+
public let MAP_FAILED: UnsafeMutableRawPointer! = UnsafeMutableRawPointer(bitPattern: -1)
66+
67+
// NOTE: wasi-libc's error.h defines these macros as function-like macros, which ClangImporter can't import for now.
68+
%{
69+
posix_error_codes = [
70+
"E2BIG",
71+
"EACCES",
72+
"EADDRINUSE",
73+
"EADDRNOTAVAIL",
74+
"EAFNOSUPPORT",
75+
"EAGAIN",
76+
"EALREADY",
77+
"EBADF",
78+
"EBADMSG",
79+
"EBUSY",
80+
"ECANCELED",
81+
"ECHILD",
82+
"ECONNABORTED",
83+
"ECONNREFUSED",
84+
"ECONNRESET",
85+
"EDEADLK",
86+
"EDESTADDRREQ",
87+
"EDOM",
88+
"EDQUOT",
89+
"EEXIST",
90+
"EFAULT",
91+
"EFBIG",
92+
"EHOSTUNREACH",
93+
"EIDRM",
94+
"EILSEQ",
95+
"EINPROGRESS",
96+
"EINTR",
97+
"EINVAL",
98+
"EIO",
99+
"EISCONN",
100+
"EISDIR",
101+
"ELOOP",
102+
"EMFILE",
103+
"EMLINK",
104+
"EMSGSIZE",
105+
"EMULTIHOP",
106+
"ENAMETOOLONG",
107+
"ENETDOWN",
108+
"ENETRESET",
109+
"ENETUNREACH",
110+
"ENFILE",
111+
"ENOBUFS",
112+
"ENODEV",
113+
"ENOENT",
114+
"ENOEXEC",
115+
"ENOLCK",
116+
"ENOLINK",
117+
"ENOMEM",
118+
"ENOMSG",
119+
"ENOPROTOOPT",
120+
"ENOSPC",
121+
"ENOSYS",
122+
"ENOTCONN",
123+
"ENOTDIR",
124+
"ENOTEMPTY",
125+
"ENOTRECOVERABLE",
126+
"ENOTSOCK",
127+
"ENOTSUP",
128+
"ENOTTY",
129+
"ENXIO",
130+
"EOVERFLOW",
131+
"EOWNERDEAD",
132+
"EPERM",
133+
"EPIPE",
134+
"EPROTO",
135+
"EPROTONOSUPPORT",
136+
"EPROTOTYPE",
137+
"ERANGE",
138+
"EROFS",
139+
"ESPIPE",
140+
"ESRCH",
141+
"ESTALE",
142+
"ETIMEDOUT",
143+
"ETXTBSY",
144+
"EXDEV",
145+
"ENOTCAPABLE",
146+
]
147+
}%
148+
149+
%for ecode in posix_error_codes:
150+
151+
public let ${ecode} = POSIXErrorCode.${ecode}.rawValue
152+
153+
%end
154+
155+
// NOTE: wasi-libc's _seek.h defines these macros as function-like macros, which ClangImporter can't import for now.
156+
157+
public let SEEK_SET: Int32 = 0
158+
public let SEEK_CUR: Int32 = 1
159+
public let SEEK_END: Int32 = 2

0 commit comments

Comments
 (0)