Skip to content

Commit

Permalink
Merge pull request #5507 from swiftwasm/katei/cherry-pick-to-downstre…
Browse files Browse the repository at this point in the history
…am/refactor-wasi-libc

Cherry-pick WASILibc upstreaming changes
  • Loading branch information
kateinoigakukun committed Jun 10, 2023
2 parents c39d86c + 6efb965 commit d7950b4
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 128 deletions.
43 changes: 0 additions & 43 deletions lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -453,42 +453,6 @@ static inline bool isPCHFilenameExtension(StringRef path) {
.endswith(file_types::getExtension(file_types::TY_PCH));
}

static Optional<StringRef>
getWasiLibcModuleMapPath(SearchPathOptions& Opts, llvm::Triple triple,
SmallVectorImpl<char> &buffer) {
StringRef platform = swift::getPlatformNameForTriple(triple);
StringRef arch = swift::getMajorArchitectureName(triple);
StringRef SDKPath = Opts.getSDKPath();

if (!SDKPath.empty()) {
buffer.clear();
buffer.append(SDKPath.begin(), SDKPath.end());
llvm::sys::path::append(buffer, "usr", "lib", "swift");
llvm::sys::path::append(buffer, platform, arch, "wasi.modulemap");

// Only specify the module map if that file actually exists. It may not;
// for example in the case that `swiftc -target x86_64-unknown-linux-gnu
// -emit-ir` is invoked using a Swift compiler not built for Linux targets.
if (llvm::sys::fs::exists(buffer))
return StringRef(buffer.data(), buffer.size());
}

if (!Opts.RuntimeResourcePath.empty()) {
buffer.clear();
buffer.append(Opts.RuntimeResourcePath.begin(),
Opts.RuntimeResourcePath.end());
llvm::sys::path::append(buffer, platform, arch, "wasi.modulemap");

// Only specify the module map if that file actually exists. It may not;
// for example in the case that `swiftc -target x86_64-unknown-linux-gnu
// -emit-ir` is invoked using a Swift compiler not built for Linux targets.
if (llvm::sys::fs::exists(buffer))
return StringRef(buffer.data(), buffer.size());
}

return None;
}

void
importer::getNormalInvocationArguments(
std::vector<std::string> &invocationArgStrs,
Expand Down Expand Up @@ -665,13 +629,6 @@ importer::getNormalInvocationArguments(
});
}

if (triple.isOSWASI()) {
SmallString<128> buffer;
if (auto path = getWasiLibcModuleMapPath(searchPathOpts, triple, buffer)) {
invocationArgStrs.push_back((Twine("-fmodule-map-file=") + *path).str());
}
}

if (triple.isOSWindows()) {
switch (triple.getArch()) {
default: llvm_unreachable("unsupported Windows architecture");
Expand Down
40 changes: 30 additions & 10 deletions lib/ClangImporter/ClangIncludePaths.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ static Optional<Path> getGlibcModuleMapPath(
return getActualModuleMapPath("glibc.modulemap", Opts, triple, vfs);
}

static Optional<Path> getWASILibcModuleMapPath(
SearchPathOptions &Opts, const llvm::Triple &triple,
const llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> &vfs) {
return getActualModuleMapPath("wasi-libc.modulemap", Opts, triple, vfs);
}

static Optional<Path> getLibStdCxxModuleMapPath(
SearchPathOptions &opts, const llvm::Triple &triple,
const llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> &vfs) {
Expand Down Expand Up @@ -183,11 +189,32 @@ static bool shouldInjectGlibcModulemap(const llvm::Triple &triple) {
triple.isAndroid();
}

static bool shouldInjectWASILibcModulemap(const llvm::Triple &triple) {
return triple.isOSWASI();
}

static SmallVector<std::pair<std::string, std::string>, 2> getGlibcFileMapping(
ASTContext &ctx,
const llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> &vfs) {
const llvm::Triple &triple = ctx.LangOpts.Target;
if (!shouldInjectGlibcModulemap(triple))

std::string auxiliaryHeaderName;
llvm::Optional<Path> maybeActualModuleMapPath;
if (shouldInjectGlibcModulemap(triple)) {
auxiliaryHeaderName = "SwiftGlibc.h";
maybeActualModuleMapPath = getGlibcModuleMapPath(ctx.SearchPathOpts, triple, vfs);
} else if (shouldInjectWASILibcModulemap(triple)) {
auxiliaryHeaderName = "SwiftWASILibc.h";
maybeActualModuleMapPath = getWASILibcModuleMapPath(ctx.SearchPathOpts, triple, vfs);
} else {
return {};
}

Path actualModuleMapPath;
if (auto path = maybeActualModuleMapPath)
actualModuleMapPath = path.value();
else
// FIXME: Emit a warning of some kind.
return {};

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

Path actualModuleMapPath;
if (auto path = getGlibcModuleMapPath(ctx.SearchPathOpts, triple, vfs))
actualModuleMapPath = path.value();
else
// FIXME: Emit a warning of some kind.
return {};

// TODO: remove the SwiftGlibc.h header and reference all Glibc headers
// directly from the modulemap.
Path actualHeaderPath = actualModuleMapPath;
llvm::sys::path::remove_filename(actualHeaderPath);
llvm::sys::path::append(actualHeaderPath, "SwiftGlibc.h");
llvm::sys::path::append(actualHeaderPath, auxiliaryHeaderName);

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

Path injectedHeaderPath(glibcDir);
llvm::sys::path::append(injectedHeaderPath, "SwiftGlibc.h");
llvm::sys::path::append(injectedHeaderPath, auxiliaryHeaderName);

return {
{std::string(injectedModuleMapPath), std::string(actualModuleMapPath)},
Expand Down
93 changes: 47 additions & 46 deletions stdlib/public/Platform/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -96,23 +96,24 @@ add_swift_target_library(swiftGlibc ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_O
LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}"
TARGET_SDKS "${swiftGlibc_target_sdks}"
INSTALL_IN_COMPONENT sdk-overlay
DEPENDS glibc_modulemap)
DEPENDS libc_modulemap)

add_swift_target_library(swiftWASILibc ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_OVERLAY
${swift_platform_sources}
POSIXError.swift

GYB_SOURCES
${swift_platform_gyb_sources}
WASI.swift.gyb
WASILibc.swift.gyb

SWIFT_COMPILE_FLAGS
${SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS}
${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS}
${swift_platform_compile_flags}
LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}"
TARGET_SDKS WASI
INSTALL_IN_COMPONENT sdk-overlay
DEPENDS glibc_modulemap)
DEPENDS libc_modulemap)

add_swift_target_library(swiftCRT ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_OVERLAY
ucrt.swift
Expand All @@ -131,116 +132,116 @@ add_swift_target_library(swiftCRT ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_OVE
TARGET_SDKS WINDOWS
INSTALL_IN_COMPONENT sdk-overlay)

set(glibc_modulemap_target_list)
set(libc_modulemap_target_list)
foreach(sdk ${SWIFT_SDKS})
if("${sdk}" STREQUAL "LINUX" OR
"${sdk}" STREQUAL "FREEBSD" OR
"${sdk}" STREQUAL "OPENBSD" OR
"${sdk}" STREQUAL "ANDROID" OR
"${sdk}" STREQUAL "CYGWIN" OR
"${sdk}" STREQUAL "HAIKU")
set(glibc_modulemap_source "glibc.modulemap.gyb")
set(glibc_header_source "SwiftGlibc.h.gyb")
set(libc_modulemap_source "glibc.modulemap.gyb")
set(libc_header_source "SwiftGlibc.h.gyb")
elseif("${sdk}" STREQUAL "WASI")
set(glibc_modulemap_source "wasi.modulemap.gyb")
set(glibc_header_source "SwiftWASILibc.h.gyb")
set(libc_modulemap_source "wasi-libc.modulemap.gyb")
set(libc_header_source "SwiftWASILibc.h.gyb")
else()
continue()
endif()

string(REGEX REPLACE "\\.gyb$" "" glibc_modulemap_outname "${glibc_modulemap_source}")
string(REGEX REPLACE "\\.gyb$" "" glibc_header_outname "${glibc_header_source}")
string(REGEX REPLACE "\\.gyb$" "" libc_modulemap_outname "${libc_modulemap_source}")
string(REGEX REPLACE "\\.gyb$" "" libc_header_outname "${libc_header_source}")

foreach(arch ${SWIFT_SDK_${sdk}_ARCHITECTURES})
set(arch_subdir "${SWIFT_SDK_${sdk}_LIB_SUBDIR}/${arch}")
set(module_dir "${SWIFTLIB_DIR}/${arch_subdir}")
set(module_dir_static "${SWIFTSTATICLIB_DIR}/${arch_subdir}")

set(glibc_modulemap_out "${module_dir}/${glibc_modulemap_outname}")
set(glibc_modulemap_out_static "${module_dir_static}/${glibc_modulemap_outname}")
set(libc_modulemap_out "${module_dir}/${libc_modulemap_outname}")
set(libc_modulemap_out_static "${module_dir_static}/${libc_modulemap_outname}")

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

list(APPEND glibc_modulemap_target_list ${glibc_modulemap_target})
list(APPEND libc_modulemap_target_list ${libc_modulemap_target})

set(glibc_header_out "${module_dir}/${glibc_header_outname}")
set(glibc_header_out_static "${module_dir_static}/${glibc_header_outname}")
handle_gyb_source_single(glibc_header_target
SOURCE "${glibc_header_source}"
OUTPUT "${glibc_header_out}"
set(libc_header_out "${module_dir}/${libc_header_outname}")
set(libc_header_out_static "${module_dir_static}/${libc_header_outname}")
handle_gyb_source_single(libc_header_target
SOURCE "${libc_header_source}"
OUTPUT "${libc_header_out}"
FLAGS "-DCMAKE_SDK=${sdk}")
list(APPEND glibc_modulemap_target_list ${glibc_header_target})
list(APPEND libc_modulemap_target_list ${libc_header_target})

if(SWIFT_BUILD_STATIC_STDLIB)
add_custom_command_target(
copy_glibc_modulemap_header_static
copy_libc_modulemap_header_static
COMMAND
"${CMAKE_COMMAND}" "-E" "make_directory" ${module_dir_static}
COMMAND
"${CMAKE_COMMAND}" "-E" "copy"
${glibc_modulemap_out} ${glibc_modulemap_out_static}
${libc_modulemap_out} ${libc_modulemap_out_static}
COMMAND
"${CMAKE_COMMAND}" "-E" "copy"
${glibc_header_out} ${glibc_header_out_static}
OUTPUT ${glibc_modulemap_out_static} ${glibc_header_out_static}
${libc_header_out} ${libc_header_out_static}
OUTPUT ${libc_modulemap_out_static} ${libc_header_out_static}
DEPENDS
"${glibc_modulemap_target}"
"${glibc_header_target}"
COMMENT "Copying Glibc modulemap and header to static resources")
"${libc_modulemap_target}"
"${libc_header_target}"
COMMENT "Copying libc modulemap and header to static resources")

list(APPEND glibc_modulemap_target_list
${copy_glibc_modulemap_header_static})
list(APPEND libc_modulemap_target_list
${copy_libc_modulemap_header_static})
endif()

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

handle_gyb_source_single(glibc_modulemap_native_target
SOURCE "${glibc_modulemap_source}"
OUTPUT "${glibc_sysroot_relative_modulemap_out}"
handle_gyb_source_single(libc_modulemap_native_target
SOURCE "${libc_modulemap_source}"
OUTPUT "${libc_sysroot_relative_modulemap_out}"
FLAGS "-DCMAKE_SDK=${sdk}")

list(APPEND glibc_modulemap_target_list ${glibc_modulemap_native_target})
set(glibc_modulemap_out ${glibc_sysroot_relative_modulemap_out})
list(APPEND libc_modulemap_target_list ${libc_modulemap_native_target})
set(libc_modulemap_out ${libc_sysroot_relative_modulemap_out})
endif()

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

swift_install_in_component(FILES "${glibc_modulemap_out}"
swift_install_in_component(FILES "${libc_modulemap_out}"
DESTINATION "lib/swift/${arch_subdir}"
COMPONENT sdk-overlay)
swift_install_in_component(FILES "${glibc_header_out}"
swift_install_in_component(FILES "${libc_header_out}"
DESTINATION "lib/swift/${arch_subdir}"
COMPONENT sdk-overlay)

if(SWIFT_BUILD_STATIC_STDLIB)
swift_install_in_component(FILES "${glibc_modulemap_out}"
swift_install_in_component(FILES "${libc_modulemap_out}"
DESTINATION "lib/swift_static/${arch_subdir}"
COMPONENT sdk-overlay)
swift_install_in_component(FILES "${glibc_header_out}"
swift_install_in_component(FILES "${libc_header_out}"
DESTINATION "lib/swift_static/${arch_subdir}"
COMPONENT sdk-overlay)
endif()
endforeach()
endforeach()
add_custom_target(glibc_modulemap DEPENDS ${glibc_modulemap_target_list})
set_property(TARGET glibc_modulemap PROPERTY FOLDER "Miscellaneous")
add_dependencies(sdk-overlay glibc_modulemap)
add_custom_target(libc_modulemap DEPENDS ${libc_modulemap_target_list})
set_property(TARGET libc_modulemap PROPERTY FOLDER "Miscellaneous")
add_dependencies(sdk-overlay libc_modulemap)

if(WINDOWS IN_LIST SWIFT_SDKS)
swift_install_in_component(FILES
Expand Down
File renamed without changes.
18 changes: 18 additions & 0 deletions stdlib/public/Platform/wasi-libc.modulemap.gyb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//===--- wasi-libc.modulemap.gyb ------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

module SwiftWASILibc [system] {
// C standard library
header "SwiftWASILibc.h"

export *
}
26 changes: 0 additions & 26 deletions stdlib/public/Platform/wasi.modulemap.gyb

This file was deleted.

5 changes: 2 additions & 3 deletions test/lit.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -1850,8 +1850,7 @@ elif run_os == 'wasi':
config.target_build_swift = ' '.join([
config.swiftc,
'-target', config.variant_triple,
'-Xcc', '--sysroot=%s' % config.variant_sdk,
'-Xclang-linker', '--sysroot=%s' % config.variant_sdk,
'-sdk', config.variant_sdk,
'-toolchain-stdlib-rpath', config.resource_dir_opt,
mcp_opt, config.swift_test_options,
config.swift_driver_test_options, swift_execution_tests_extra_flags])
Expand All @@ -1863,7 +1862,7 @@ elif run_os == 'wasi':
config.target_swift_frontend = ' '.join([
config.swift_frontend,
'-target', config.variant_triple,
'-Xcc', '--sysroot=%s' % config.variant_sdk,
'-sdk', config.variant_sdk,
config.resource_dir_opt, mcp_opt,
config.swift_test_options, config.swift_frontend_test_options])
subst_target_swift_frontend_mock_sdk = config.target_swift_frontend
Expand Down

0 comments on commit d7950b4

Please sign in to comment.