Skip to content

Commit 902d75e

Browse files
[wasm][build] Add WASILibc module and its build support
[wasi-libc](https://github.com/WebAssembly/wasi-libc) is a libc implementation widely used in WASI toolchains. This patch adds a new module named WASILibc. Some extra emulation libraries are also added as wasi_emulated_xxx.
1 parent 7eac199 commit 902d75e

File tree

3 files changed

+311
-0
lines changed

3 files changed

+311
-0
lines changed

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
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
//===--- wasi-libc.modulemap.gyb ------------------------------------------===//
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+
module SwiftWASILibc [system] {
14+
header "stdc-predef.h"
15+
header "features.h"
16+
17+
// C standard library
18+
header "complex.h"
19+
header "ctype.h"
20+
header "errno.h"
21+
header "fenv.h"
22+
header "float.h"
23+
header "inttypes.h"
24+
header "iso646.h"
25+
header "limits.h"
26+
header "locale.h"
27+
header "math.h"
28+
header "stdarg.h"
29+
header "stdbool.h"
30+
header "stddef.h"
31+
header "stdint.h"
32+
header "stdio.h"
33+
header "stdlib.h"
34+
header "string.h"
35+
header "tgmath.h"
36+
header "time.h"
37+
38+
// POSIX
39+
header "arpa/inet.h"
40+
header "cpio.h"
41+
header "dirent.h"
42+
header "fcntl.h"
43+
header "fmtmsg.h"
44+
header "fnmatch.h"
45+
header "ftw.h"
46+
header "glob.h"
47+
header "iconv.h"
48+
header "ifaddrs.h"
49+
header "langinfo.h"
50+
header "libgen.h"
51+
header "monetary.h"
52+
header "netinet/in.h"
53+
header "netinet/tcp.h"
54+
header "nl_types.h"
55+
header "poll.h"
56+
header "regex.h"
57+
header "sched.h"
58+
header "search.h"
59+
header "semaphore.h"
60+
header "strings.h"
61+
header "sys/file.h"
62+
header "sys/ioctl.h"
63+
header "sys/select.h"
64+
header "sys/socket.h"
65+
header "sys/stat.h"
66+
header "sys/time.h"
67+
header "sys/types.h"
68+
header "sys/uio.h"
69+
header "sys/un.h"
70+
header "sys/utsname.h"
71+
header "sysexits.h"
72+
header "tar.h"
73+
header "unistd.h"
74+
header "utime.h"
75+
76+
// WASI specific
77+
header "wasi/api.h"
78+
export *
79+
}
80+
81+
module wasi_emulated_mman {
82+
header "sys/mman.h"
83+
link "wasi-emulated-mman"
84+
export *
85+
}
86+
87+
module wasi_emulated_process_clock {
88+
header "sys/times.h"
89+
header "sys/resource.h"
90+
link "wasi-emulated-process-clocks"
91+
export *
92+
}
93+
94+
module wasi_emulated_signal {
95+
header "signal.h"
96+
link "wasi-emulated-signal"
97+
export *
98+
}

0 commit comments

Comments
 (0)