Skip to content

Commit d7950b4

Browse files
Merge pull request #5507 from swiftwasm/katei/cherry-pick-to-downstream/refactor-wasi-libc
Cherry-pick WASILibc upstreaming changes
2 parents c39d86c + 6efb965 commit d7950b4

File tree

7 files changed

+97
-128
lines changed

7 files changed

+97
-128
lines changed

lib/ClangImporter/ClangImporter.cpp

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -453,42 +453,6 @@ static inline bool isPCHFilenameExtension(StringRef path) {
453453
.endswith(file_types::getExtension(file_types::TY_PCH));
454454
}
455455

456-
static Optional<StringRef>
457-
getWasiLibcModuleMapPath(SearchPathOptions& Opts, llvm::Triple triple,
458-
SmallVectorImpl<char> &buffer) {
459-
StringRef platform = swift::getPlatformNameForTriple(triple);
460-
StringRef arch = swift::getMajorArchitectureName(triple);
461-
StringRef SDKPath = Opts.getSDKPath();
462-
463-
if (!SDKPath.empty()) {
464-
buffer.clear();
465-
buffer.append(SDKPath.begin(), SDKPath.end());
466-
llvm::sys::path::append(buffer, "usr", "lib", "swift");
467-
llvm::sys::path::append(buffer, platform, arch, "wasi.modulemap");
468-
469-
// Only specify the module map if that file actually exists. It may not;
470-
// for example in the case that `swiftc -target x86_64-unknown-linux-gnu
471-
// -emit-ir` is invoked using a Swift compiler not built for Linux targets.
472-
if (llvm::sys::fs::exists(buffer))
473-
return StringRef(buffer.data(), buffer.size());
474-
}
475-
476-
if (!Opts.RuntimeResourcePath.empty()) {
477-
buffer.clear();
478-
buffer.append(Opts.RuntimeResourcePath.begin(),
479-
Opts.RuntimeResourcePath.end());
480-
llvm::sys::path::append(buffer, platform, arch, "wasi.modulemap");
481-
482-
// Only specify the module map if that file actually exists. It may not;
483-
// for example in the case that `swiftc -target x86_64-unknown-linux-gnu
484-
// -emit-ir` is invoked using a Swift compiler not built for Linux targets.
485-
if (llvm::sys::fs::exists(buffer))
486-
return StringRef(buffer.data(), buffer.size());
487-
}
488-
489-
return None;
490-
}
491-
492456
void
493457
importer::getNormalInvocationArguments(
494458
std::vector<std::string> &invocationArgStrs,
@@ -665,13 +629,6 @@ importer::getNormalInvocationArguments(
665629
});
666630
}
667631

668-
if (triple.isOSWASI()) {
669-
SmallString<128> buffer;
670-
if (auto path = getWasiLibcModuleMapPath(searchPathOpts, triple, buffer)) {
671-
invocationArgStrs.push_back((Twine("-fmodule-map-file=") + *path).str());
672-
}
673-
}
674-
675632
if (triple.isOSWindows()) {
676633
switch (triple.getArch()) {
677634
default: llvm_unreachable("unsupported Windows architecture");

lib/ClangImporter/ClangIncludePaths.cpp

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ static Optional<Path> getGlibcModuleMapPath(
9191
return getActualModuleMapPath("glibc.modulemap", Opts, triple, vfs);
9292
}
9393

94+
static Optional<Path> getWASILibcModuleMapPath(
95+
SearchPathOptions &Opts, const llvm::Triple &triple,
96+
const llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> &vfs) {
97+
return getActualModuleMapPath("wasi-libc.modulemap", Opts, triple, vfs);
98+
}
99+
94100
static Optional<Path> getLibStdCxxModuleMapPath(
95101
SearchPathOptions &opts, const llvm::Triple &triple,
96102
const llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> &vfs) {
@@ -183,11 +189,32 @@ static bool shouldInjectGlibcModulemap(const llvm::Triple &triple) {
183189
triple.isAndroid();
184190
}
185191

192+
static bool shouldInjectWASILibcModulemap(const llvm::Triple &triple) {
193+
return triple.isOSWASI();
194+
}
195+
186196
static SmallVector<std::pair<std::string, std::string>, 2> getGlibcFileMapping(
187197
ASTContext &ctx,
188198
const llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> &vfs) {
189199
const llvm::Triple &triple = ctx.LangOpts.Target;
190-
if (!shouldInjectGlibcModulemap(triple))
200+
201+
std::string auxiliaryHeaderName;
202+
llvm::Optional<Path> maybeActualModuleMapPath;
203+
if (shouldInjectGlibcModulemap(triple)) {
204+
auxiliaryHeaderName = "SwiftGlibc.h";
205+
maybeActualModuleMapPath = getGlibcModuleMapPath(ctx.SearchPathOpts, triple, vfs);
206+
} else if (shouldInjectWASILibcModulemap(triple)) {
207+
auxiliaryHeaderName = "SwiftWASILibc.h";
208+
maybeActualModuleMapPath = getWASILibcModuleMapPath(ctx.SearchPathOpts, triple, vfs);
209+
} else {
210+
return {};
211+
}
212+
213+
Path actualModuleMapPath;
214+
if (auto path = maybeActualModuleMapPath)
215+
actualModuleMapPath = path.value();
216+
else
217+
// FIXME: Emit a warning of some kind.
191218
return {};
192219

193220
// Extract the Glibc path from Clang driver.
@@ -213,24 +240,17 @@ static SmallVector<std::pair<std::string, std::string>, 2> getGlibcFileMapping(
213240
return {};
214241
}
215242

216-
Path actualModuleMapPath;
217-
if (auto path = getGlibcModuleMapPath(ctx.SearchPathOpts, triple, vfs))
218-
actualModuleMapPath = path.value();
219-
else
220-
// FIXME: Emit a warning of some kind.
221-
return {};
222-
223243
// TODO: remove the SwiftGlibc.h header and reference all Glibc headers
224244
// directly from the modulemap.
225245
Path actualHeaderPath = actualModuleMapPath;
226246
llvm::sys::path::remove_filename(actualHeaderPath);
227-
llvm::sys::path::append(actualHeaderPath, "SwiftGlibc.h");
247+
llvm::sys::path::append(actualHeaderPath, auxiliaryHeaderName);
228248

229249
Path injectedModuleMapPath(glibcDir);
230250
llvm::sys::path::append(injectedModuleMapPath, "module.modulemap");
231251

232252
Path injectedHeaderPath(glibcDir);
233-
llvm::sys::path::append(injectedHeaderPath, "SwiftGlibc.h");
253+
llvm::sys::path::append(injectedHeaderPath, auxiliaryHeaderName);
234254

235255
return {
236256
{std::string(injectedModuleMapPath), std::string(actualModuleMapPath)},

stdlib/public/Platform/CMakeLists.txt

Lines changed: 47 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -96,23 +96,24 @@ add_swift_target_library(swiftGlibc ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_O
9696
LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}"
9797
TARGET_SDKS "${swiftGlibc_target_sdks}"
9898
INSTALL_IN_COMPONENT sdk-overlay
99-
DEPENDS glibc_modulemap)
99+
DEPENDS libc_modulemap)
100100

101101
add_swift_target_library(swiftWASILibc ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_OVERLAY
102102
${swift_platform_sources}
103103
POSIXError.swift
104104

105105
GYB_SOURCES
106106
${swift_platform_gyb_sources}
107-
WASI.swift.gyb
107+
WASILibc.swift.gyb
108108

109109
SWIFT_COMPILE_FLAGS
110110
${SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS}
111111
${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS}
112+
${swift_platform_compile_flags}
112113
LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}"
113114
TARGET_SDKS WASI
114115
INSTALL_IN_COMPONENT sdk-overlay
115-
DEPENDS glibc_modulemap)
116+
DEPENDS libc_modulemap)
116117

117118
add_swift_target_library(swiftCRT ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_OVERLAY
118119
ucrt.swift
@@ -131,116 +132,116 @@ add_swift_target_library(swiftCRT ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_OVE
131132
TARGET_SDKS WINDOWS
132133
INSTALL_IN_COMPONENT sdk-overlay)
133134

134-
set(glibc_modulemap_target_list)
135+
set(libc_modulemap_target_list)
135136
foreach(sdk ${SWIFT_SDKS})
136137
if("${sdk}" STREQUAL "LINUX" OR
137138
"${sdk}" STREQUAL "FREEBSD" OR
138139
"${sdk}" STREQUAL "OPENBSD" OR
139140
"${sdk}" STREQUAL "ANDROID" OR
140141
"${sdk}" STREQUAL "CYGWIN" OR
141142
"${sdk}" STREQUAL "HAIKU")
142-
set(glibc_modulemap_source "glibc.modulemap.gyb")
143-
set(glibc_header_source "SwiftGlibc.h.gyb")
143+
set(libc_modulemap_source "glibc.modulemap.gyb")
144+
set(libc_header_source "SwiftGlibc.h.gyb")
144145
elseif("${sdk}" STREQUAL "WASI")
145-
set(glibc_modulemap_source "wasi.modulemap.gyb")
146-
set(glibc_header_source "SwiftWASILibc.h.gyb")
146+
set(libc_modulemap_source "wasi-libc.modulemap.gyb")
147+
set(libc_header_source "SwiftWASILibc.h.gyb")
147148
else()
148149
continue()
149150
endif()
150151

151-
string(REGEX REPLACE "\\.gyb$" "" glibc_modulemap_outname "${glibc_modulemap_source}")
152-
string(REGEX REPLACE "\\.gyb$" "" glibc_header_outname "${glibc_header_source}")
152+
string(REGEX REPLACE "\\.gyb$" "" libc_modulemap_outname "${libc_modulemap_source}")
153+
string(REGEX REPLACE "\\.gyb$" "" libc_header_outname "${libc_header_source}")
153154

154155
foreach(arch ${SWIFT_SDK_${sdk}_ARCHITECTURES})
155156
set(arch_subdir "${SWIFT_SDK_${sdk}_LIB_SUBDIR}/${arch}")
156157
set(module_dir "${SWIFTLIB_DIR}/${arch_subdir}")
157158
set(module_dir_static "${SWIFTSTATICLIB_DIR}/${arch_subdir}")
158159

159-
set(glibc_modulemap_out "${module_dir}/${glibc_modulemap_outname}")
160-
set(glibc_modulemap_out_static "${module_dir_static}/${glibc_modulemap_outname}")
160+
set(libc_modulemap_out "${module_dir}/${libc_modulemap_outname}")
161+
set(libc_modulemap_out_static "${module_dir_static}/${libc_modulemap_outname}")
161162

162163
# Configure the module map based on the target. Each platform needs to
163-
# reference different headers, based on what's available in their glibc.
164-
# This is the 'glibc.modulemap' in the 'resource-dir', so
164+
# reference different headers, based on what's available in their libc.
165+
# This is the .modulemap in the 'resource-dir', so
165166
# it's the one we'll look at during the build process.
166-
handle_gyb_source_single(glibc_modulemap_target
167-
SOURCE "${glibc_modulemap_source}"
168-
OUTPUT "${glibc_modulemap_out}"
167+
handle_gyb_source_single(libc_modulemap_target
168+
SOURCE "${libc_modulemap_source}"
169+
OUTPUT "${libc_modulemap_out}"
169170
FLAGS
170171
"-DCMAKE_SDK=${sdk}")
171172

172-
list(APPEND glibc_modulemap_target_list ${glibc_modulemap_target})
173+
list(APPEND libc_modulemap_target_list ${libc_modulemap_target})
173174

174-
set(glibc_header_out "${module_dir}/${glibc_header_outname}")
175-
set(glibc_header_out_static "${module_dir_static}/${glibc_header_outname}")
176-
handle_gyb_source_single(glibc_header_target
177-
SOURCE "${glibc_header_source}"
178-
OUTPUT "${glibc_header_out}"
175+
set(libc_header_out "${module_dir}/${libc_header_outname}")
176+
set(libc_header_out_static "${module_dir_static}/${libc_header_outname}")
177+
handle_gyb_source_single(libc_header_target
178+
SOURCE "${libc_header_source}"
179+
OUTPUT "${libc_header_out}"
179180
FLAGS "-DCMAKE_SDK=${sdk}")
180-
list(APPEND glibc_modulemap_target_list ${glibc_header_target})
181+
list(APPEND libc_modulemap_target_list ${libc_header_target})
181182

182183
if(SWIFT_BUILD_STATIC_STDLIB)
183184
add_custom_command_target(
184-
copy_glibc_modulemap_header_static
185+
copy_libc_modulemap_header_static
185186
COMMAND
186187
"${CMAKE_COMMAND}" "-E" "make_directory" ${module_dir_static}
187188
COMMAND
188189
"${CMAKE_COMMAND}" "-E" "copy"
189-
${glibc_modulemap_out} ${glibc_modulemap_out_static}
190+
${libc_modulemap_out} ${libc_modulemap_out_static}
190191
COMMAND
191192
"${CMAKE_COMMAND}" "-E" "copy"
192-
${glibc_header_out} ${glibc_header_out_static}
193-
OUTPUT ${glibc_modulemap_out_static} ${glibc_header_out_static}
193+
${libc_header_out} ${libc_header_out_static}
194+
OUTPUT ${libc_modulemap_out_static} ${libc_header_out_static}
194195
DEPENDS
195-
"${glibc_modulemap_target}"
196-
"${glibc_header_target}"
197-
COMMENT "Copying Glibc modulemap and header to static resources")
196+
"${libc_modulemap_target}"
197+
"${libc_header_target}"
198+
COMMENT "Copying libc modulemap and header to static resources")
198199

199-
list(APPEND glibc_modulemap_target_list
200-
${copy_glibc_modulemap_header_static})
200+
list(APPEND libc_modulemap_target_list
201+
${copy_libc_modulemap_header_static})
201202
endif()
202203

203204
# If this SDK is a target for a non-native host, except if it's for Android
204205
# with its own native sysroot, create a native modulemap without a sysroot
205206
# prefix. This is the one we'll install instead.
206207
if(NOT "${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_ARCH_${arch}_PATH}" STREQUAL "/" AND
207208
NOT (${sdk} STREQUAL ANDROID AND NOT "${SWIFT_ANDROID_NATIVE_SYSROOT}" STREQUAL ""))
208-
set(glibc_sysroot_relative_modulemap_out "${module_dir}/sysroot-relative-modulemaps/${glibc_modulemap_outname}")
209+
set(libc_sysroot_relative_modulemap_out "${module_dir}/sysroot-relative-modulemaps/${libc_modulemap_outname}")
209210

210-
handle_gyb_source_single(glibc_modulemap_native_target
211-
SOURCE "${glibc_modulemap_source}"
212-
OUTPUT "${glibc_sysroot_relative_modulemap_out}"
211+
handle_gyb_source_single(libc_modulemap_native_target
212+
SOURCE "${libc_modulemap_source}"
213+
OUTPUT "${libc_sysroot_relative_modulemap_out}"
213214
FLAGS "-DCMAKE_SDK=${sdk}")
214215

215-
list(APPEND glibc_modulemap_target_list ${glibc_modulemap_native_target})
216-
set(glibc_modulemap_out ${glibc_sysroot_relative_modulemap_out})
216+
list(APPEND libc_modulemap_target_list ${libc_modulemap_native_target})
217+
set(libc_modulemap_out ${libc_sysroot_relative_modulemap_out})
217218
endif()
218219

219220
# FIXME: When SDK is a cross-compile target (SDK != Host), the generated
220221
# modulemap will be relative to the Host, with hardcoded paths.
221222
# It is not relocatable to the target platform itself.
222223
# This affects any cross-compiled targets that use glibc.modulemap.
223224

224-
swift_install_in_component(FILES "${glibc_modulemap_out}"
225+
swift_install_in_component(FILES "${libc_modulemap_out}"
225226
DESTINATION "lib/swift/${arch_subdir}"
226227
COMPONENT sdk-overlay)
227-
swift_install_in_component(FILES "${glibc_header_out}"
228+
swift_install_in_component(FILES "${libc_header_out}"
228229
DESTINATION "lib/swift/${arch_subdir}"
229230
COMPONENT sdk-overlay)
230231

231232
if(SWIFT_BUILD_STATIC_STDLIB)
232-
swift_install_in_component(FILES "${glibc_modulemap_out}"
233+
swift_install_in_component(FILES "${libc_modulemap_out}"
233234
DESTINATION "lib/swift_static/${arch_subdir}"
234235
COMPONENT sdk-overlay)
235-
swift_install_in_component(FILES "${glibc_header_out}"
236+
swift_install_in_component(FILES "${libc_header_out}"
236237
DESTINATION "lib/swift_static/${arch_subdir}"
237238
COMPONENT sdk-overlay)
238239
endif()
239240
endforeach()
240241
endforeach()
241-
add_custom_target(glibc_modulemap DEPENDS ${glibc_modulemap_target_list})
242-
set_property(TARGET glibc_modulemap PROPERTY FOLDER "Miscellaneous")
243-
add_dependencies(sdk-overlay glibc_modulemap)
242+
add_custom_target(libc_modulemap DEPENDS ${libc_modulemap_target_list})
243+
set_property(TARGET libc_modulemap PROPERTY FOLDER "Miscellaneous")
244+
add_dependencies(sdk-overlay libc_modulemap)
244245

245246
if(WINDOWS IN_LIST SWIFT_SDKS)
246247
swift_install_in_component(FILES
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
// C standard library
15+
header "SwiftWASILibc.h"
16+
17+
export *
18+
}

stdlib/public/Platform/wasi.modulemap.gyb

Lines changed: 0 additions & 26 deletions
This file was deleted.

test/lit.cfg

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1850,8 +1850,7 @@ elif run_os == 'wasi':
18501850
config.target_build_swift = ' '.join([
18511851
config.swiftc,
18521852
'-target', config.variant_triple,
1853-
'-Xcc', '--sysroot=%s' % config.variant_sdk,
1854-
'-Xclang-linker', '--sysroot=%s' % config.variant_sdk,
1853+
'-sdk', config.variant_sdk,
18551854
'-toolchain-stdlib-rpath', config.resource_dir_opt,
18561855
mcp_opt, config.swift_test_options,
18571856
config.swift_driver_test_options, swift_execution_tests_extra_flags])
@@ -1863,7 +1862,7 @@ elif run_os == 'wasi':
18631862
config.target_swift_frontend = ' '.join([
18641863
config.swift_frontend,
18651864
'-target', config.variant_triple,
1866-
'-Xcc', '--sysroot=%s' % config.variant_sdk,
1865+
'-sdk', config.variant_sdk,
18671866
config.resource_dir_opt, mcp_opt,
18681867
config.swift_test_options, config.swift_frontend_test_options])
18691868
subst_target_swift_frontend_mock_sdk = config.target_swift_frontend

0 commit comments

Comments
 (0)