diff --git a/lib/ClangImporter/ClangImporter.cpp b/lib/ClangImporter/ClangImporter.cpp index 7dfab43730647..8b9e4a2663b82 100644 --- a/lib/ClangImporter/ClangImporter.cpp +++ b/lib/ClangImporter/ClangImporter.cpp @@ -453,42 +453,6 @@ static inline bool isPCHFilenameExtension(StringRef path) { .endswith(file_types::getExtension(file_types::TY_PCH)); } -static Optional -getWasiLibcModuleMapPath(SearchPathOptions& Opts, llvm::Triple triple, - SmallVectorImpl &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 &invocationArgStrs, @@ -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"); diff --git a/lib/ClangImporter/ClangIncludePaths.cpp b/lib/ClangImporter/ClangIncludePaths.cpp index 59afe4fbb3332..bafcd3bc21521 100644 --- a/lib/ClangImporter/ClangIncludePaths.cpp +++ b/lib/ClangImporter/ClangIncludePaths.cpp @@ -91,6 +91,12 @@ static Optional getGlibcModuleMapPath( return getActualModuleMapPath("glibc.modulemap", Opts, triple, vfs); } +static Optional getWASILibcModuleMapPath( + SearchPathOptions &Opts, const llvm::Triple &triple, + const llvm::IntrusiveRefCntPtr &vfs) { + return getActualModuleMapPath("wasi-libc.modulemap", Opts, triple, vfs); +} + static Optional getLibStdCxxModuleMapPath( SearchPathOptions &opts, const llvm::Triple &triple, const llvm::IntrusiveRefCntPtr &vfs) { @@ -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, 2> getGlibcFileMapping( ASTContext &ctx, const llvm::IntrusiveRefCntPtr &vfs) { const llvm::Triple &triple = ctx.LangOpts.Target; - if (!shouldInjectGlibcModulemap(triple)) + + std::string auxiliaryHeaderName; + llvm::Optional 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. @@ -213,24 +240,17 @@ static SmallVector, 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)}, diff --git a/stdlib/public/Platform/CMakeLists.txt b/stdlib/public/Platform/CMakeLists.txt index 5b708bd137e71..cf1fb695500a8 100644 --- a/stdlib/public/Platform/CMakeLists.txt +++ b/stdlib/public/Platform/CMakeLists.txt @@ -96,7 +96,7 @@ 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} @@ -104,15 +104,16 @@ add_swift_target_library(swiftWASILibc ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SD 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 @@ -131,7 +132,7 @@ 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 @@ -139,65 +140,65 @@ foreach(sdk ${SWIFT_SDKS}) "${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 @@ -205,15 +206,15 @@ foreach(sdk ${SWIFT_SDKS}) # 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 @@ -221,26 +222,26 @@ foreach(sdk ${SWIFT_SDKS}) # 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 diff --git a/stdlib/public/Platform/WASI.swift.gyb b/stdlib/public/Platform/WASILibc.swift.gyb similarity index 100% rename from stdlib/public/Platform/WASI.swift.gyb rename to stdlib/public/Platform/WASILibc.swift.gyb diff --git a/stdlib/public/Platform/wasi-libc.modulemap.gyb b/stdlib/public/Platform/wasi-libc.modulemap.gyb new file mode 100644 index 0000000000000..59be5d56499b8 --- /dev/null +++ b/stdlib/public/Platform/wasi-libc.modulemap.gyb @@ -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 * +} diff --git a/stdlib/public/Platform/wasi.modulemap.gyb b/stdlib/public/Platform/wasi.modulemap.gyb deleted file mode 100644 index c51c81f61c367..0000000000000 --- a/stdlib/public/Platform/wasi.modulemap.gyb +++ /dev/null @@ -1,26 +0,0 @@ -//===--- wasi.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 -// -//===----------------------------------------------------------------------===// - -/// This is a semi-complete modulemap that maps WASI headers in a roughly -/// similar way to the Darwin SDK modulemap. We do not take care to list every -/// single header which may be included by a particular submodule, so there can -/// still be issues if imported into the same context as one in which someone -/// included those headers directly. -/// -/// It's not named just WASI so that it doesn't conflict in the event of a -/// future official WASI modulemap. -module SwiftWASILibc [system] { - // C standard library - header "SwiftWASILibc.h" - - export * -} diff --git a/test/lit.cfg b/test/lit.cfg index a1ac8e4b30b0c..b067270535f64 100644 --- a/test/lit.cfg +++ b/test/lit.cfg @@ -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]) @@ -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