Skip to content

Commit 32c0ec4

Browse files
committed
[Platform] Update musl modulemap and improve musl platform support.
We need to declare separate top-level modules for every header in the musl C library. Note that our set-up here does require some tweaks to musl itself (specifically, the `alltypes.h` header needs some fairly serious massaging to make it module compatible; in particular, we need to pull out every type declaration into its own separate modularized header in `bits/types`). Also update `Platform.swift` a little for more musl support. rdar://123503615
1 parent baa2d02 commit 32c0ec4

File tree

4 files changed

+1063
-33
lines changed

4 files changed

+1063
-33
lines changed

stdlib/public/Platform/CMakeLists.txt

Lines changed: 79 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,9 @@ add_swift_target_library(swiftMusl ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_OV
160160
${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS}
161161
${swift_platform_compile_flags}
162162
LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}"
163-
TARGET_SDKS MUSL
164-
INSTALL_IN_COMPONENT sdk-overlay)
163+
TARGET_SDKS LINUX_STATIC
164+
INSTALL_IN_COMPONENT sdk-overlay
165+
DEPENDS musl_modulemap)
165166

166167
add_swift_target_library(swiftWASILibc ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_OVERLAY
167168
${swift_platform_sources}
@@ -197,6 +198,80 @@ add_swift_target_library(swiftCRT ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_OVE
197198
TARGET_SDKS WINDOWS
198199
INSTALL_IN_COMPONENT sdk-overlay)
199200

201+
foreach(sdk ${SWIFT_SDKS})
202+
# Add more platforms supporting Musl to these checks,
203+
if(NOT "${sdk}" STREQUAL "LINUX_STATIC")
204+
continue()
205+
endif()
206+
207+
set(musl_modulemap "musl.modulemap")
208+
set(musl_header "SwiftMusl.h")
209+
210+
foreach(arch ${SWIFT_SDK_${sdk}_ARCHITECTURES})
211+
set(arch_subdir "${SWIFT_SDK_${sdk}_LIB_SUBDIR}/${arch}")
212+
set(module_dir "${SWIFTLIB_DIR}/${arch_subdir}")
213+
set(module_dir_static "${SWIFTSTATICLIB_DIR}/${arch_subdir}")
214+
215+
set(musl_modulemap_out "${module_dir}/${musl_modulemap}")
216+
set(musl_modulemap_out_static "${module_dir_static}/${musl_modulemap}")
217+
set(musl_header_out "${module_dir}/${musl_header}")
218+
set(musl_header_out_static "${module_dir_static}/${musl_header}")
219+
220+
add_custom_command_target(
221+
copy_musl_modulemap
222+
COMMAND
223+
"${CMAKE_COMMAND}" "-E" "make_directory" ${module_dir}
224+
COMMAND
225+
"${CMAKE_COMMAND}" "-E" "copy"
226+
${CMAKE_CURRENT_LIST_DIR}/${musl_modulemap}
227+
${musl_modulemap_out}
228+
COMMAND
229+
"${CMAKE_COMMAND}" "-E" "copy"
230+
${CMAKE_CURRENT_LIST_DIR}/${musl_header}
231+
${musl_header_out}
232+
OUTPUT ${musl_modulemap_out} ${musl_header_out}
233+
COMMENT "Copying Musl modulemap to resources")
234+
235+
list(APPEND musl_modulemap_target_list ${copy_musl_modulemap})
236+
237+
if(SWIFT_BUILD_STATIC_STDLIB OR SWIFT_SDK_${sdk}_STATIC_ONLY)
238+
add_custom_command_target(
239+
copy_musl_modulemap_static
240+
COMMAND
241+
"${CMAKE_COMMAND}" "-E" "make_directory" ${module_dir_static}
242+
COMMAND
243+
"${CMAKE_COMMAND}" "-E" "copy"
244+
${CMAKE_CURRENT_LIST_DIR}/${musl_modulemap}
245+
${musl_modulemap_out_static}
246+
COMMAND
247+
"${CMAKE_COMMAND}" "-E" "copy"
248+
${CMAKE_CURRENT_LIST_DIR}/${musl_header}
249+
${musl_header_out_static}
250+
OUTPUT ${musl_modulemap_out_static} ${musl_header_out_static}
251+
COMMENT "Copying Musl modulemap to static resources")
252+
253+
list(APPEND musl_modulemap_target_list ${copy_musl_modulemap_static})
254+
255+
swift_install_in_component(FILES "${musl_modulemap_out_static}"
256+
DESTINATION "lib/swift_static/${arch_subdir}"
257+
COMPONENT sdk-overlay)
258+
swift_install_in_component(FILES "${musl_header_out_static}"
259+
DESTINATION "lib/swift_static/${arch_subdir}"
260+
COMPONENT sdk-overlay)
261+
endif()
262+
263+
swift_install_in_component(FILES "${musl_modulemap_out}"
264+
DESTINATION "lib/swift/${arch_subdir}"
265+
COMPONENT sdk-overlay)
266+
swift_install_in_component(FILES "${musl_header_out}"
267+
DESTINATION "lib/swift/${arch_subdir}"
268+
COMPONENT sdk-overlay)
269+
endforeach()
270+
endforeach()
271+
add_custom_target(musl_modulemap DEPENDS ${musl_modulemap_target_list})
272+
set_property(TARGET musl_modulemap PROPERTY FOLDER "Miscellaneous")
273+
add_dependencies(sdk-overlay musl_modulemap)
274+
200275
set(glibc_modulemap_target_list)
201276
foreach(sdk ${SWIFT_SDKS})
202277
if(NOT "${sdk}" STREQUAL "LINUX" AND
@@ -237,7 +312,7 @@ foreach(sdk ${SWIFT_SDKS})
237312
FLAGS "-DCMAKE_SDK=${sdk}")
238313
list(APPEND glibc_modulemap_target_list ${glibc_header_target})
239314

240-
if(SWIFT_BUILD_STATIC_STDLIB)
315+
if(SWIFT_BUILD_STATIC_STDLIB OR SWIFT_SDK_${sdk}_STATIC_ONLY)
241316
add_custom_command_target(
242317
copy_glibc_modulemap_header_static
243318
COMMAND
@@ -286,7 +361,7 @@ foreach(sdk ${SWIFT_SDKS})
286361
DESTINATION "lib/swift/${arch_subdir}"
287362
COMPONENT sdk-overlay)
288363

289-
if(SWIFT_BUILD_STATIC_STDLIB)
364+
if(SWIFT_BUILD_STATIC_STDLIB OR SWIFT_SDK_${sdk}_STATIC_ONLY)
290365
swift_install_in_component(FILES "${glibc_modulemap_out}"
291366
DESTINATION "lib/swift_static/${arch_subdir}"
292367
COMPONENT sdk-overlay)

stdlib/public/Platform/Platform.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,9 @@ public var SIG_IGN: sig_t { return unsafeBitCast(1, to: sig_t.self) }
344344
public var SIG_ERR: sig_t { return unsafeBitCast(-1, to: sig_t.self) }
345345
public var SIG_HOLD: sig_t { return unsafeBitCast(3, to: sig_t.self) }
346346
#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Haiku)
347+
#if !canImport(SwiftMusl)
347348
public typealias sighandler_t = __sighandler_t
349+
#endif
348350

349351
public var SIG_DFL: sighandler_t? { return nil }
350352
public var SIG_IGN: sighandler_t {
@@ -487,7 +489,7 @@ extension Duration {
487489
public var environ: UnsafeMutablePointer<UnsafeMutablePointer<CChar>?> {
488490
return _swift_stdlib_getEnviron()
489491
}
490-
#elseif os(Linux)
492+
#elseif os(Linux) && !canImport(SwiftMusl)
491493
public var environ: UnsafeMutablePointer<UnsafeMutablePointer<CChar>?> {
492494
return __environ
493495
}

stdlib/public/Platform/SwiftMusl.h

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
//===--- SwiftMusl.h ------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2023 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+
#include <assert.h>
14+
#include <float.h>
15+
#include <iso646.h>
16+
#include <limits.h>
17+
#include <stdalign.h>
18+
#include <stdarg.h>
19+
#include <stdatomic.h>
20+
#include <stdbool.h>
21+
#include <stddef.h>
22+
#include <stdint.h>
23+
#include <time.h>
24+
#include <tgmath.h>
25+
#include <complex.h>
26+
#include <ctype.h>
27+
#include <errno.h>
28+
#include <fenv.h>
29+
#include <inttypes.h>
30+
#include <locale.h>
31+
#include <math.h>
32+
#include <setjmp.h>
33+
#include <signal.h>
34+
#include <stdio.h>
35+
#include <stdlib.h>
36+
#include <stdnoreturn.h>
37+
#include <string.h>
38+
#include <threads.h>
39+
#include <uchar.h>
40+
#include <wchar.h>
41+
#include <wctype.h>
42+
#include <aio.h>
43+
#include <arpa/inet.h>
44+
#include <arpa/ftp.h>
45+
#include <arpa/nameser.h>
46+
#include <arpa/nameser_compat.h>
47+
#include <arpa/telnet.h>
48+
#include <arpa/tftp.h>
49+
#include <cpio.h>
50+
#include <dirent.h>
51+
#include <dlfcn.h>
52+
#include <fcntl.h>
53+
#include <fmtmsg.h>
54+
#include <fnmatch.h>
55+
#include <ftw.h>
56+
#include <glob.h>
57+
#include <grp.h>
58+
#include <iconv.h>
59+
#include <langinfo.h>
60+
#include <libgen.h>
61+
#include <monetary.h>
62+
#include <mqueue.h>
63+
#include <net/if.h>
64+
#include <net/ethernet.h>
65+
#include <net/if_arp.h>
66+
#include <net/route.h>
67+
#include <netdb.h>
68+
#include <netinet/in.h>
69+
#include <netinet/tcp.h>
70+
#include <netinet/ether.h>
71+
#include <netinet/icmp6.h>
72+
#include <netinet/if_ether.h>
73+
#include <netinet/igmp.h>
74+
#include <netinet/in_systm.h>
75+
#include <netinet/ip.h>
76+
#include <netinet/ip6.h>
77+
#include <netinet/ip_icmp.h>
78+
#include <netinet/udp.h>
79+
#include <nl_types.h>
80+
#include <poll.h>
81+
#include <pthread.h>
82+
#include <pwd.h>
83+
#include <regex.h>
84+
#include <sched.h>
85+
#include <search.h>
86+
#include <semaphore.h>
87+
#include <spawn.h>
88+
#include <strings.h>
89+
#include <stropts.h>
90+
#include <sys/ipc.h>
91+
#include <sys/mman.h>
92+
#include <sys/msg.h>
93+
#include <sys/resource.h>
94+
#include <sys/select.h>
95+
#include <sys/sem.h>
96+
#include <sys/shm.h>
97+
#include <sys/socket.h>
98+
#include <sys/stat.h>
99+
#include <sys/statvfs.h>
100+
#include <sys/time.h>
101+
#include <sys/times.h>
102+
#include <sys/types.h>
103+
#include <sys/uio.h>
104+
#include <sys/un.h>
105+
#include <sys/utsname.h>
106+
#include <sys/wait.h>
107+
#include <sys/acct.h>
108+
#include <sys/auxv.h>
109+
#include <sys/cachectl.h>
110+
#include <sys/dir.h>
111+
#include <sys/epoll.h>
112+
#include <sys/eventfd.h>
113+
#include <sys/fanotify.h>
114+
#include <sys/file.h>
115+
#include <sys/fsuid.h>
116+
#include <sys/inotify.h>
117+
#include <sys/ioctl.h>
118+
#include <sys/io.h>
119+
#include <sys/klog.h>
120+
#include <sys/membarrier.h>
121+
#include <sys/mount.h>
122+
#include <sys/mtio.h>
123+
#include <sys/param.h>
124+
#include <sys/personality.h>
125+
#include <sys/prctl.h>
126+
#include <sys/procfs.h>
127+
#include <sys/ptrace.h>
128+
#include <sys/quota.h>
129+
#include <sys/random.h>
130+
#include <sys/reboot.h>
131+
#include <sys/reg.h>
132+
#include <sys/sendfile.h>
133+
#include <sys/signalfd.h>
134+
#include <sys/statfs.h>
135+
#include <sys/stropts.h>
136+
#include <sys/swap.h>
137+
#include <sys/syscall.h>
138+
#include <sys/sysinfo.h>
139+
#include <sys/syslog.h>
140+
#include <sys/sysmacros.h>
141+
#include <sys/timeb.h>
142+
#include <sys/timerfd.h>
143+
#include <sys/timex.h>
144+
#include <sys/ttydefaults.h>
145+
#include <sys/ucontext.h>
146+
#include <sys/user.h>
147+
#include <sys/vfs.h>
148+
#include <sys/xattr.h>
149+
#include <syslog.h>
150+
#include <tar.h>
151+
#include <termios.h>
152+
#include <ucontext.h>
153+
#include <unistd.h>
154+
#include <ulimit.h>
155+
#include <utime.h>
156+
#include <utmpx.h>
157+
#include <alloca.h>
158+
#include <ar.h>
159+
#include <byteswap.h>
160+
#include <crypt.h>
161+
#include <elf.h>
162+
#include <endian.h>
163+
#include <err.h>
164+
#include <getopt.h>
165+
#include <ifaddrs.h>
166+
#include <lastlog.h>
167+
#include <libintl.h>
168+
#include <link.h>
169+
#include <malloc.h>
170+
#include <memory.h>
171+
#include <mntent.h>
172+
#include <netpacket/packet.h>
173+
#include <paths.h>
174+
#include <pty.h>
175+
#include <resolv.h>
176+
#include <scsi/scsi.h>
177+
#include <scsi/scsi_ioctl.h>
178+
#include <scsi/sg.h>
179+
#include <shadow.h>
180+
#include <stdio_ext.h>
181+
#include <syscall.h>
182+
#include <sysexits.h>
183+
#include <utmp.h>
184+
#include <values.h>
185+
#include <unwind.h>
186+
#include <wordexp.h>
187+
#include <fts.h>

0 commit comments

Comments
 (0)