From 04898dc23636e5e746f03292246089338836b38a Mon Sep 17 00:00:00 2001 From: Zhuowei Zhang Date: Mon, 8 Apr 2019 22:30:48 -0700 Subject: [PATCH 01/80] Start adding wasm --- CMakeLists.txt | 4 ++- cmake/modules/AddSwift.cmake | 9 +++++++ cmake/modules/SwiftConfigureSDK.cmake | 5 ++++ cmake/modules/SwiftSetIfArchBitness.cmake | 3 ++- lib/Basic/LangOptions.cpp | 2 ++ utils/build-script-impl | 14 +++++++++- .../build_swift/driver_arguments.py | 27 +++++++++++++++++++ .../swift_build_support/targets.py | 9 ++++++- 8 files changed, 69 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 852ef3cf48a48..2eaf289f42979 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -264,7 +264,7 @@ set(SWIFT_ANDROID_DEPLOY_DEVICE_PATH "" CACHE STRING # foreach(sdk ANDROID;FREEBSD;LINUX;WINDOWS;HAIKU) - foreach(arch aarch64;armv6;armv7;i686;powerpc64;powerpc64le;s390x;x86_64) + foreach(arch aarch64;armv6;armv7;i686;powerpc64;powerpc64le;s390x;wasm32;x86_64) set(SWIFT_${sdk}_${arch}_ICU_UC "" CACHE STRING "Path to a directory containing the icuuc library for ${sdk}") set(SWIFT_${sdk}_${arch}_ICU_UC_INCLUDE "" CACHE STRING @@ -595,6 +595,8 @@ else() set(SWIFT_HOST_VARIANT_ARCH_default "powerpc64le") elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "s390x") set(SWIFT_HOST_VARIANT_ARCH_default "s390x") + elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "wasm32") + set(SWIFT_HOST_VARIANT_ARCH_default "wasm32") # FIXME: Only matches v6l/v7l - by far the most common variants elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "armv6l") set(SWIFT_HOST_VARIANT_ARCH_default "armv6") diff --git a/cmake/modules/AddSwift.cmake b/cmake/modules/AddSwift.cmake index dc59e57379d02..f77a964fac7aa 100644 --- a/cmake/modules/AddSwift.cmake +++ b/cmake/modules/AddSwift.cmake @@ -516,6 +516,8 @@ function(_add_variant_link_flags) list(APPEND link_libraries "pthread") elseif("${LFLAGS_SDK}" STREQUAL "CYGWIN") # No extra libraries required. + elseif("${LFLAGS_SDK}" STREQUAL "WASM") + # No extra libraries required. elseif("${LFLAGS_SDK}" STREQUAL "WINDOWS") # We don't need to add -nostdlib using MSVC or clang-cl, as MSVC and clang-cl rely on auto-linking entirely. if(NOT SWIFT_COMPILER_IS_MSVC_LIKE) @@ -1710,6 +1712,9 @@ endfunction() # SWIFT_MODULE_DEPENDS_HAIKU # Swift modules this library depends on when built for Haiku. # +# SWIFT_MODULE_DEPENDS_WASM +# Swift modules this library depends on when built for WebAssembly. +# # FRAMEWORK_DEPENDS # System frameworks this library depends on. # @@ -1826,6 +1831,7 @@ function(add_swift_target_library name) SWIFT_MODULE_DEPENDS_OSX SWIFT_MODULE_DEPENDS_TVOS SWIFT_MODULE_DEPENDS_WATCHOS + SWIFT_MODULE_DEPENDS_WASM SWIFT_MODULE_DEPENDS_WINDOWS SWIFT_MODULE_DEPENDS_FROM_SDK TARGET_SDKS @@ -1987,6 +1993,9 @@ function(add_swift_target_library name) elseif(${sdk} STREQUAL HAIKU) list(APPEND swiftlib_module_depends_flattened ${SWIFTLIB_SWIFT_MODULE_DEPENDS_HAIKU}) + elseif(${sdk} STREQUAL WASM) + list(APPEND swiftlib_module_depends_flattened + ${SWIFTLIB_SWIFT_MODULE_DEPENDS_WASM}) elseif(${sdk} STREQUAL WINDOWS) list(APPEND swiftlib_module_depends_flattened ${SWIFTLIB_SWIFT_MODULE_DEPENDS_WINDOWS}) diff --git a/cmake/modules/SwiftConfigureSDK.cmake b/cmake/modules/SwiftConfigureSDK.cmake index 52a3a4f3aeb0c..c21f0cbe4d0de 100644 --- a/cmake/modules/SwiftConfigureSDK.cmake +++ b/cmake/modules/SwiftConfigureSDK.cmake @@ -333,6 +333,11 @@ macro(configure_sdk_unix name architectures) message(FATAL_ERROR "unsupported arch for Haiku: ${arch}") endif() set(SWIFT_SDK_HAIKU_ARCH_x86_64_TRIPLE "x86_64-unknown-haiku") + elseif("${prefix}" STREQUAL "WASM") + if(NOT arch STREQUAL wasm32) + message(FATAL_ERROR "unsupported arch for WebAssembly: ${arch}") + endif() + set(SWIFT_SDK_WASM_ARCH_wasm32_TRIPLE "wasm32-unknown-unknown-wasm") else() message(FATAL_ERROR "unknown Unix OS: ${prefix}") endif() diff --git a/cmake/modules/SwiftSetIfArchBitness.cmake b/cmake/modules/SwiftSetIfArchBitness.cmake index 5212cf3ccb854..d38a9689150ba 100644 --- a/cmake/modules/SwiftSetIfArchBitness.cmake +++ b/cmake/modules/SwiftSetIfArchBitness.cmake @@ -12,7 +12,8 @@ function(set_if_arch_bitness var_name) "${SIA_ARCH}" STREQUAL "armv6" OR "${SIA_ARCH}" STREQUAL "armv7" OR "${SIA_ARCH}" STREQUAL "armv7k" OR - "${SIA_ARCH}" STREQUAL "armv7s") + "${SIA_ARCH}" STREQUAL "armv7s" OR + "${SIA_ARCH}" STREQUAL "wasm32") set("${var_name}" "${SIA_CASE_32_BIT}" PARENT_SCOPE) elseif("${SIA_ARCH}" STREQUAL "x86_64" OR "${SIA_ARCH}" STREQUAL "arm64" OR diff --git a/lib/Basic/LangOptions.cpp b/lib/Basic/LangOptions.cpp index def6143af764f..2ed2b34aed21a 100644 --- a/lib/Basic/LangOptions.cpp +++ b/lib/Basic/LangOptions.cpp @@ -333,6 +333,8 @@ std::pair LangOptions::setTarget(llvm::Triple triple) { case llvm::Triple::ArchType::systemz: addPlatformConditionValue(PlatformConditionKind::Endianness, "big"); break; + default: + llvm_unreachable("undefined architecture endianness"); } // Set the "runtime" platform condition. diff --git a/utils/build-script-impl b/utils/build-script-impl index 49239434d12e0..3cb93b5f3668d 100755 --- a/utils/build-script-impl +++ b/utils/build-script-impl @@ -114,6 +114,14 @@ KNOWN_SETTINGS=( darwin-toolchain-version "" "Version for xctoolchain info plist and installer pkg" darwin-xcrun-toolchain "default" "the name of the toolchain to use on Darwin" + ## WebAssembly/WASI Options + wasi-sdk "" "An absolute path to the WASI SDK that will be used as a libc implementation for Wasm builds" + wasi-icu-uc "" "Path to libicuuc.so" + wasi-icu-uc-include "" "Path to a directory containing headers for libicuuc" + wasi-icu-i18n "" "Path to libicui18n.so" + wasi-icu-i18n-include "" "Path to a directory containing headers libicui18n" + wasi-icu-data "" "Path to libicudata.so" + ## Build Types for Components swift-stdlib-build-type "Debug" "the CMake build variant for Swift" @@ -133,6 +141,7 @@ KNOWN_SETTINGS=( skip-build-osx "" "set to skip building Swift stdlibs for OS X" skip-build-tvos-device "" "set to skip building Swift stdlibs for tvOS devices (i.e. build simulators only)" skip-build-tvos-simulator "" "set to skip building Swift stdlibs for tvOS simulators (i.e. build devices only)" + skip-build-wasm "" "set to skip building Swift stdlibs for WebAssembly" skip-build-watchos-device "" "set to skip building Swift stdlibs for Apple watchOS devices (i.e. build simulators only)" skip-build-watchos-simulator "" "set to skip building Swift stdlibs for Apple watchOS simulators (i.e. build devices only)" @@ -150,6 +159,8 @@ KNOWN_SETTINGS=( skip-test-osx "" "set to skip testing Swift stdlibs for OS X" skip-test-tvos-host "" "set to skip testing the host parts of the tvOS toolchain" skip-test-tvos-simulator "" "set to skip testing Swift stdlibs for tvOS simulators (i.e. test devices only)" + skip-test-wasm "" "set to skip testing Swift stdlibs for WebAssembly" + skip-test-wasm-host "" "set to skip testing the host parts of the WebAssembly toolchain" skip-test-watchos-host "" "set to skip testing the host parts of the watchOS toolchain" skip-test-watchos-simulator "" "set to skip testing Swift stdlibs for Apple watchOS simulators (i.e. test devices only)" skip-test-benchmarks "" "set to skip running Swift Benchmark Suite" @@ -423,7 +434,8 @@ function verify_host_is_supported() { | watchsimulator-i386 \ | watchos-armv7k \ | android-armv7 \ - | android-aarch64) + | android-aarch64 \ + | wasm-wasm32) ;; *) echo "Unknown host tools target: ${host}" diff --git a/utils/build_swift/build_swift/driver_arguments.py b/utils/build_swift/build_swift/driver_arguments.py index 108ea4793315a..74b108e1f7e4f 100644 --- a/utils/build_swift/build_swift/driver_arguments.py +++ b/utils/build_swift/build_swift/driver_arguments.py @@ -141,6 +141,7 @@ def _apply_default_arguments(args): args.build_tvos = False args.build_watchos = False args.build_android = False + args.build_wasm = False args.build_benchmarks = False args.build_external_benchmarks = False args.build_lldb = False @@ -170,6 +171,9 @@ def _apply_default_arguments(args): if not args.android or not args.build_android: args.build_android = False + if not args.wasm or not args.build_wasm: + args.build_wasm = False + # --test-paths implies --test and/or --validation-test # depending on what directories/files have been specified. if args.test_paths: @@ -206,6 +210,7 @@ def _apply_default_arguments(args): args.test_tvos = False args.test_watchos = False args.test_android = False + args.test_wasm = False args.test_swiftpm = False args.test_swiftsyntax = False args.test_indexstoredb = False @@ -252,11 +257,19 @@ def _apply_default_arguments(args): if not args.test_android: args.test_android_host = False + if not args.build_wasm: + args.test_wasm = False + args.test_wasm_host = False + + if not args.test_android: + args.test_android_host = False + if not args.host_test: args.test_ios_host = False args.test_tvos_host = False args.test_watchos_host = False args.test_android_host = False + args.test_wasm_host = False def create_argument_parser(): @@ -345,6 +358,9 @@ def create_argument_parser(): option('--android', toggle_true, help='also build for Android') + option('--wasm', toggle_true, + help='also build for WebAssembly') + option('--swift-analyze-code-coverage', store, choices=['false', 'not-merged', 'merged'], # so CMake can see the inert mode as a false value @@ -933,6 +949,9 @@ def create_argument_parser(): option('--skip-build-android', toggle_false('build_android'), help='skip building Swift stdlibs for Android') + option('--skip-build-wasm', toggle_false('build_wasm'), + help='skip building Swift stdlibs for WebAssembly') + option('--skip-build-benchmarks', toggle_false('build_benchmarks'), help='skip building Swift Benchmark Suite') @@ -989,6 +1008,14 @@ def create_argument_parser(): help='skip testing Android device targets on the host machine (the ' 'phone itself)') + option('--skip-test-wasm', + toggle_false('test_wasm'), + help='skip testing all WebAssembly targets.') + option('--skip-test-wasm-host', + toggle_false('test_wasm_host'), + help='skip testing WebAssembly device targets on the host machine (the ' + 'WebAssembly runtime)') + option('--skip-test-swiftpm', toggle_false('test_swiftpm'), help='skip testing swiftpm') option('--skip-test-swiftsyntax', toggle_false('test_swiftsyntax'), diff --git a/utils/swift_build_support/swift_build_support/targets.py b/utils/swift_build_support/swift_build_support/targets.py index 45bb45ca9916a..fb286d58e7a46 100644 --- a/utils/swift_build_support/swift_build_support/targets.py +++ b/utils/swift_build_support/swift_build_support/targets.py @@ -158,6 +158,8 @@ class StdlibDeploymentTarget(object): Haiku = Platform("haiku", archs=["x86_64"]) + Wasm = Platform("wasm", archs=["wasm32"]) + # The list of known platforms. known_platforms = [ OSX, @@ -169,7 +171,8 @@ class StdlibDeploymentTarget(object): Cygwin, Android, Windows, - Haiku] + Haiku, + Wasm] # Cache of targets by name. _targets_by_name = dict((target.name, target) @@ -233,6 +236,10 @@ def host_target(): if machine == 'x86_64': return StdlibDeploymentTarget.Haiku.x86_64 + elif system == 'Wasm': + if machine == 'wasm32': + return StdlibDeploymentTarget.Wasm.wasm32 + raise NotImplementedError('System "%s" with architecture "%s" is not ' 'supported' % (system, machine)) From 73c32e48f2a05df988a9a40c6e330a456c710757 Mon Sep 17 00:00:00 2001 From: Zhuowei Zhang Date: Tue, 9 Apr 2019 15:40:01 -0700 Subject: [PATCH 02/80] Add wasm object emit support --- lib/Basic/Platform.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/Basic/Platform.cpp b/lib/Basic/Platform.cpp index 9581df610fab2..62c0a4d3eb444 100644 --- a/lib/Basic/Platform.cpp +++ b/lib/Basic/Platform.cpp @@ -171,6 +171,8 @@ static StringRef getPlatformNameForDarwin(const DarwinPlatformKind platform) { StringRef swift::getPlatformNameForTriple(const llvm::Triple &triple) { switch (triple.getOS()) { case llvm::Triple::UnknownOS: + if (triple.isOSBinFormatWasm()) + return "wasm"; llvm_unreachable("unknown OS"); case llvm::Triple::Ananas: case llvm::Triple::CloudABI: From f4fffeeb9bca2511f81ff7c6599955294538dcc1 Mon Sep 17 00:00:00 2001 From: Zhuowei Zhang Date: Tue, 9 Apr 2019 15:54:47 -0700 Subject: [PATCH 03/80] build-script: parse skip wasm command line args --- utils/build-script | 6 ++++++ utils/build-script-impl | 3 +++ .../swift_build_support/host_specific_configuration.py | 2 ++ 3 files changed, 11 insertions(+) diff --git a/utils/build-script b/utils/build-script index 120f75a5cd928..4502b21b2e516 100755 --- a/utils/build-script +++ b/utils/build-script @@ -501,6 +501,8 @@ class BuildScriptInvocation(object): impl_args += ["--skip-build-watchos-simulator"] if not args.build_android: impl_args += ["--skip-build-android"] + if not args.build_wasm: + impl_args += ["--skip-build-wasm"] if not args.build_clang_tools_extra: impl_args += ["--skip-build-clang-tools-extra"] @@ -541,6 +543,10 @@ class BuildScriptInvocation(object): impl_args += ["--skip-test-android"] if not args.test_android_host: impl_args += ["--skip-test-android-host"] + if not args.test_wasm: + impl_args += ["--skip-test-wasm"] + if not args.test_wasm_host: + impl_args += ["--skip-test-wasm-host"] if args.build_runtime_with_host_compiler: impl_args += ["--build-runtime-with-host-compiler"] if args.validation_test: diff --git a/utils/build-script-impl b/utils/build-script-impl index 3cb93b5f3668d..b715a6b875f2b 100755 --- a/utils/build-script-impl +++ b/utils/build-script-impl @@ -1200,6 +1200,9 @@ function common_cross_c_flags() { watchos-*) echo -n " -arch ${arch} -mwatchos-version-min=${DARWIN_DEPLOYMENT_VERSION_WATCHOS}" ;; + wasm-wasm32) + echo -n " -arch wasm32" + ;; esac local build_type=$2 diff --git a/utils/swift_build_support/swift_build_support/host_specific_configuration.py b/utils/swift_build_support/swift_build_support/host_specific_configuration.py index ecdb437113321..d4d38eeab3c16 100644 --- a/utils/swift_build_support/swift_build_support/host_specific_configuration.py +++ b/utils/swift_build_support/swift_build_support/host_specific_configuration.py @@ -201,6 +201,8 @@ def __platforms_to_skip_build(self, args): StdlibDeploymentTarget.AppleWatchSimulator) if not args.build_android: platforms_to_skip_build.add(StdlibDeploymentTarget.Android) + if not args.build_wasm: + platforms_to_skip_build.add(StdlibDeploymentTarget.Wasm) return platforms_to_skip_build def __platforms_to_skip_test(self, args): From 5cff1a427279ab3ccfc5866f576b12d654ce2c31 Mon Sep 17 00:00:00 2001 From: Zhuowei Zhang Date: Tue, 9 Apr 2019 16:35:13 -0700 Subject: [PATCH 04/80] Handle WASM sdk in cmake --- CMakeLists.txt | 20 +++++++++++++++++-- test/CMakeLists.txt | 3 ++- utils/build-script | 3 +++ .../host_specific_configuration.py | 4 ++++ 4 files changed, 27 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2eaf289f42979..058633a703aa2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -260,10 +260,10 @@ set(SWIFT_ANDROID_DEPLOY_DEVICE_PATH "" CACHE STRING "Path on an Android device where build products will be pushed. These are used when running the test suite against the device") # -# User-configurable ICU specific options for Android, FreeBSD, Linux and Haiku. +# User-configurable ICU specific options for Android, FreeBSD, Linux, Haiku, and WebAssembly. # -foreach(sdk ANDROID;FREEBSD;LINUX;WINDOWS;HAIKU) +foreach(sdk ANDROID;FREEBSD;LINUX;WINDOWS;HAIKU;WASM) foreach(arch aarch64;armv6;armv7;i686;powerpc64;powerpc64le;s390x;wasm32;x86_64) set(SWIFT_${sdk}_${arch}_ICU_UC "" CACHE STRING "Path to a directory containing the icuuc library for ${sdk}") @@ -781,6 +781,22 @@ if(swift_build_windows AND NOT "${CMAKE_SYSTEM_NAME}" STREQUAL "Windows") configure_sdk_windows("Windows" "msvc" "${SWIFT_SDK_WINDOWS_ARCHITECTURES}") endif() +# Should we cross-compile the standard library for WebAssembly (Emscripten)? +is_sdk_requested(WASM swift_build_wasm) +if(swift_build_wasm AND NOT "${SWIFT_HOST_VARIANT_SDK}" STREQUAL "WASM") + #if ("${SWIFT_ANDROID_NDK_PATH}" STREQUAL "") + # message(FATAL_ERROR "You must set SWIFT_ANDROID_NDK_PATH to cross-compile the Swift runtime for Android") + #endif() + #if (NOT ("${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "Darwin" OR "${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "Linux")) + # message(FATAL_ERROR "A Darwin or Linux host is required to build the Swift runtime for Android") + #endif() + + if("${SWIFT_SDK_WASM_ARCHITECTURES}" STREQUAL "") + set(SWIFT_SDK_WASM_ARCHITECTURES wasm32) + endif() + configure_sdk_unix("Wasm" "${SWIFT_SDK_WASM_ARCHITECTURES}") +endif() + if("${SWIFT_SDKS}" STREQUAL "") set(SWIFT_SDKS "${SWIFT_CONFIGURED_SDKS}") endif() diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 796b9bf1da4cb..84f11ddcdb4af 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -75,7 +75,8 @@ function(get_test_dependencies SDK result_var_name) ("${SDK}" STREQUAL "FREEBSD") OR ("${SDK}" STREQUAL "ANDROID") OR ("${SDK}" STREQUAL "WINDOWS") OR - ("${SDK}" STREQUAL "HAIKU")) + ("${SDK}" STREQUAL "HAIKU") OR + ("${SDK}" STREQUAL "WASM")) # No extra dependencies. else() message(FATAL_ERROR "Unknown SDK: ${SDK}") diff --git a/utils/build-script b/utils/build-script index 4502b21b2e516..ef738e9ac7c6a 100755 --- a/utils/build-script +++ b/utils/build-script @@ -216,6 +216,9 @@ class BuildScriptInvocation(object): elif args.android_arch == "aarch64": args.stdlib_deployment_targets.append( StdlibDeploymentTarget.Android.aarch64.name) + if args.wasm: + args.stdlib_deployment_targets.append( + StdlibDeploymentTarget.Wasm.wasm32.name) # Infer platform flags from manually-specified configure targets. # This doesn't apply to Darwin platforms, as they are diff --git a/utils/swift_build_support/swift_build_support/host_specific_configuration.py b/utils/swift_build_support/swift_build_support/host_specific_configuration.py index d4d38eeab3c16..cacf49e473104 100644 --- a/utils/swift_build_support/swift_build_support/host_specific_configuration.py +++ b/utils/swift_build_support/swift_build_support/host_specific_configuration.py @@ -242,6 +242,8 @@ def __platforms_to_skip_test(self, args): StdlibDeploymentTarget.AppleWatchSimulator) if not args.test_android: platforms_to_skip_test.add(StdlibDeploymentTarget.Android) + if not args.test_wasm: + platforms_to_skip_test.add(StdlibDeploymentTarget.Wasm) return platforms_to_skip_test @@ -262,4 +264,6 @@ def __platforms_to_skip_test_host(self, args): platforms_to_skip_test_host.add(StdlibDeploymentTarget.AppleTV) if not args.test_watchos_host: platforms_to_skip_test_host.add(StdlibDeploymentTarget.AppleWatch) + if not args.test_wasm_host: + platforms_to_skip_test_host.add(StdlibDeploymentTarget.Wasm) return platforms_to_skip_test_host From 38207f6c76449ebad93b6f29eea8220ba170ba37 Mon Sep 17 00:00:00 2001 From: Zhuowei Zhang Date: Tue, 9 Apr 2019 19:46:11 -0700 Subject: [PATCH 05/80] Integrate Emscripten sdk path --- cmake/modules/AddSwift.cmake | 19 +++++++++++++-- cmake/modules/SwiftConfigureSDK.cmake | 13 +++++++++- cmake/modules/SwiftWasmSupport.cmake | 16 +++++++++++++ include/swift/Runtime/Mutex.h | 2 +- include/swift/Runtime/MutexPThread.h | 2 +- stdlib/public/SwiftShims/Visibility.h | 2 +- utils/build-script | 24 +++++++++++++++++++ utils/build-script-impl | 12 ++++++++++ .../build_swift/driver_arguments.py | 17 +++++++++++++ 9 files changed, 101 insertions(+), 6 deletions(-) create mode 100644 cmake/modules/SwiftWasmSupport.cmake diff --git a/cmake/modules/AddSwift.cmake b/cmake/modules/AddSwift.cmake index f77a964fac7aa..f2bd809e70bf0 100644 --- a/cmake/modules/AddSwift.cmake +++ b/cmake/modules/AddSwift.cmake @@ -3,6 +3,7 @@ include(SwiftList) include(SwiftXcodeSupport) include(SwiftWindowsSupport) include(SwiftAndroidSupport) +include(SwiftWasmSupport) # SWIFTLIB_DIR is the directory in the build tree where Swift resource files # should be placed. Note that $CMAKE_CFG_INTDIR expands to "." for @@ -371,6 +372,12 @@ function(_add_variant_c_compile_flags) foreach(path ${${CFLAGS_ARCH}_INCLUDE}) list(APPEND result "\"${CMAKE_INCLUDE_FLAG_C}${path}\"") endforeach() + elseif(CFLAGS_SDK STREQUAL WASM) + list(APPEND result "-D__EMSCRIPTEN__=1") + swift_wasm_include_for_arch(${CFLAGS_ARCH} ${CFLAGS_ARCH}_INCLUDE) + foreach(path ${${CFLAGS_ARCH}_INCLUDE}) + list(APPEND result "\"${CMAKE_INCLUDE_FLAG_C}${path}\"") + endforeach() endif() set(ICU_UC_INCLUDE_DIR ${SWIFT_${CFLAGS_SDK}_${CFLAGS_ARCH}_ICU_UC_INCLUDE}) @@ -439,6 +446,11 @@ function(_add_variant_swift_compile_flags foreach(path IN LISTS ${arch}_swift_include) list(APPEND result "\"${CMAKE_INCLUDE_FLAG_C}${path}\"") endforeach() + elseif("${sdk}" STREQUAL "WASM") + swift_wasm_include_for_arch(${arch} ${arch}_swift_include) + foreach(path IN LISTS ${arch}_swift_include) + list(APPEND result "\"${CMAKE_INCLUDE_FLAG_C}${path}\"") + endforeach() endif() if(NOT BUILD_STANDALONE) @@ -516,8 +528,6 @@ function(_add_variant_link_flags) list(APPEND link_libraries "pthread") elseif("${LFLAGS_SDK}" STREQUAL "CYGWIN") # No extra libraries required. - elseif("${LFLAGS_SDK}" STREQUAL "WASM") - # No extra libraries required. elseif("${LFLAGS_SDK}" STREQUAL "WINDOWS") # We don't need to add -nostdlib using MSVC or clang-cl, as MSVC and clang-cl rely on auto-linking entirely. if(NOT SWIFT_COMPILER_IS_MSVC_LIKE) @@ -553,6 +563,11 @@ function(_add_variant_link_flags) foreach(path IN LISTS ${LFLAGS_ARCH}_LIB) list(APPEND library_search_directories ${path}) endforeach() + elseif("${LFLAGS_SDK}" STREQUAL "WASM") + swift_wasm_lib_for_arch(${LFLAGS_ARCH} ${LFLAGS_ARCH}_LIB) + foreach(path IN LISTS ${LFLAGS_ARCH}_LIB) + list(APPEND library_search_directories ${path}) + endforeach() else() # If lto is enabled, we need to add the object path flag so that the LTO code # generator leaves the intermediate object file in a place where it will not diff --git a/cmake/modules/SwiftConfigureSDK.cmake b/cmake/modules/SwiftConfigureSDK.cmake index c21f0cbe4d0de..e8cb4d380749f 100644 --- a/cmake/modules/SwiftConfigureSDK.cmake +++ b/cmake/modules/SwiftConfigureSDK.cmake @@ -7,6 +7,7 @@ set(SWIFT_CONFIGURED_SDKS) include(SwiftWindowsSupport) include(SwiftAndroidSupport) +include(SwiftWasmSupport) # Report the given SDK to the user. function(_report_sdk prefix) @@ -59,6 +60,14 @@ function(_report_sdk prefix) message(STATUS " ${arch} INCLUDE: ${${arch}_INCLUDE}") message(STATUS " ${arch} LIB: ${${arch}_LIB}") endforeach() + elseif("${prefix}" STREQUAL "WASM") + message(STATUS " Emscripten Dir: $ENV{SWIFT_WASM_EMSCRIPTEN_PATH}") + foreach(arch ${SWIFT_SDK_${prefix}_ARCHITECTURES}) + swift_wasm_include_for_arch(${arch} ${arch}_INCLUDE) + swift_wasm_lib_for_arch(${arch} ${arch}_LIB) + message(STATUS " ${arch} INCLUDE: ${${arch}_INCLUDE}") + message(STATUS " ${arch} LIB: ${${arch}_LIB}") + endforeach() else() foreach(arch ${SWIFT_SDK_${prefix}_ARCHITECTURES}) message(STATUS " ${arch} Path: ${SWIFT_SDK_${prefix}_ARCH_${arch}_PATH}") @@ -304,7 +313,7 @@ macro(configure_sdk_unix name architectures) if("${prefix}" STREQUAL "LINUX") if(arch MATCHES "(armv6|armv7)") - set(SWIFT_SDK_LINUX_ARCH_${arch}_TRIPLE "${arch}-unknown-linux-gnueabihf") + set(SWIFT_SDK_LINUX_ARCH_${arch}i_TRIPLE "${arch}-unknown-linux-gnueabihf") elseif(arch MATCHES "(aarch64|i686|powerpc64|powerpc64le|s390x|x86_64)") set(SWIFT_SDK_LINUX_ARCH_${arch}_TRIPLE "${arch}-unknown-linux-gnu") else() @@ -337,6 +346,8 @@ macro(configure_sdk_unix name architectures) if(NOT arch STREQUAL wasm32) message(FATAL_ERROR "unsupported arch for WebAssembly: ${arch}") endif() + # FIXME: this is actually wrong: emscripten doesn't use sysroot. + set(SWIFT_SDK_WASM_ARCH_wasm32_PATH "${SWIFT_WASM_EMSCRIPTEN_PATH}/system") set(SWIFT_SDK_WASM_ARCH_wasm32_TRIPLE "wasm32-unknown-unknown-wasm") else() message(FATAL_ERROR "unknown Unix OS: ${prefix}") diff --git a/cmake/modules/SwiftWasmSupport.cmake b/cmake/modules/SwiftWasmSupport.cmake new file mode 100644 index 0000000000000..1d14263fbb49a --- /dev/null +++ b/cmake/modules/SwiftWasmSupport.cmake @@ -0,0 +1,16 @@ +function(swift_wasm_include_for_arch arch var) + set(paths) + list(APPEND paths + "${SWIFT_WASM_EMSCRIPTEN_PATH}/system/include/libcxx" + "${SWIFT_WASM_EMSCRIPTEN_PATH}/system/lib/libcxxabi/include" + "${SWIFT_WASM_EMSCRIPTEN_PATH}/system/include/compat" + "${SWIFT_WASM_EMSCRIPTEN_PATH}/system/include" + "${SWIFT_WASM_EMSCRIPTEN_PATH}/system/include/libc" + "${SWIFT_WASM_EMSCRIPTEN_PATH}/system/lib/libc/musl/arch/emscripten" + "${SWIFT_WASM_EMSCRIPTEN_PATH}/system/local/include" + "${SWIFT_WASM_EMSCRIPTEN_PATH}/system/include/SDL") + set(${var} ${paths} PARENT_SCOPE) +endfunction() + +function(swift_wasm_lib_for_arch arch var) +endfunction() diff --git a/include/swift/Runtime/Mutex.h b/include/swift/Runtime/Mutex.h index 1b320e9d22a6e..bfaa8a397e9f3 100644 --- a/include/swift/Runtime/Mutex.h +++ b/include/swift/Runtime/Mutex.h @@ -20,7 +20,7 @@ #include -#if (defined(__APPLE__) || defined(__linux__) || defined(__CYGWIN__) || defined(__FreeBSD__) || defined(__HAIKU__)) +#if (defined(__APPLE__) || defined(__linux__) || defined(__CYGWIN__) || defined(__FreeBSD__) || defined(__HAIKU__) || defined(__EMSCRIPTEN__)) #include "swift/Runtime/MutexPThread.h" #elif defined(_WIN32) #include "swift/Runtime/MutexWin32.h" diff --git a/include/swift/Runtime/MutexPThread.h b/include/swift/Runtime/MutexPThread.h index 1b06a2977bd06..fe976147ab3de 100644 --- a/include/swift/Runtime/MutexPThread.h +++ b/include/swift/Runtime/MutexPThread.h @@ -26,7 +26,7 @@ typedef pthread_cond_t ConditionHandle; typedef pthread_mutex_t MutexHandle; typedef pthread_rwlock_t ReadWriteLockHandle; -#if defined(__CYGWIN__) || defined(__ANDROID__) || defined(__HAIKU__) +#if defined(__CYGWIN__) || defined(__ANDROID__) || defined(__HAIKU__) || defined(__EMSCRIPTEN__) // At the moment CYGWIN pthreads implementation doesn't support the use of // constexpr for static allocation versions. The way they define things // results in a reinterpret_cast which violates constexpr. Similarly, Android's diff --git a/stdlib/public/SwiftShims/Visibility.h b/stdlib/public/SwiftShims/Visibility.h index 8577fad1653b9..6ebea03367541 100644 --- a/stdlib/public/SwiftShims/Visibility.h +++ b/stdlib/public/SwiftShims/Visibility.h @@ -76,7 +76,7 @@ // SWIFT_RUNTIME_EXPORT on the library it's exported from. /// Attribute used to export symbols from the runtime. -#if defined(__MACH__) +#if defined(__MACH__) || defined(__EMSCRIPTEN__) # define SWIFT_EXPORT_ATTRIBUTE __attribute__((__visibility__("default"))) diff --git a/utils/build-script b/utils/build-script index ef738e9ac7c6a..2caf618060568 100755 --- a/utils/build-script +++ b/utils/build-script @@ -122,6 +122,20 @@ class BuildScriptInvocation(object): "--android-icu-i18n-include, and --android-icu-data " "must be specified") + if args.wasm: + if args.wasm_emscripten is None or \ + args.wasm_icu_uc is None or \ + args.wasm_icu_uc_include is None or \ + args.wasm_icu_i18n is None or \ + args.wasm_icu_i18n_include is None or \ + args.wasm_icu_data is None: + diagnostics.fatal( + "when building for WebAssembly, --wasm-emscripten, " + "--wasm-icu-uc, " + "--wasm-icu-uc-include, --wasm-icu-i18n, " + "--wasm-icu-i18n-include, and --wasm-icu-data " + "must be specified") + targets_needing_toolchain = [ 'build_indexstoredb', 'build_sourcekitlsp', @@ -590,6 +604,16 @@ class BuildScriptInvocation(object): args.android_deploy_device_path, ] + if args.wasm: + impl_args += [ + "--wasm-emscripten", args.wasm_emscripten, + "--wasm-icu-uc", args.wasm_icu_uc, + "--wasm-icu-uc-include", args.wasm_icu_uc_include, + "--wasm-icu-i18n", args.wasm_icu_i18n, + "--wasm-icu-i18n-include", args.wasm_icu_i18n_include, + "--wasm-icu-data", args.wasm_icu_data, + ] + if platform.system() == 'Darwin': impl_args += [ "--toolchain-prefix", diff --git a/utils/build-script-impl b/utils/build-script-impl index b715a6b875f2b..32b587c1d8954 100755 --- a/utils/build-script-impl +++ b/utils/build-script-impl @@ -1586,6 +1586,18 @@ for host in "${ALL_HOSTS[@]}"; do ) fi + if [[ ! "${SKIP_BUILD_WASM}" ]]; then + cmake_options=( + "${cmake_options[@]}" + -DSWIFT_WASM_EMSCRIPTEN_PATH:STRING="${WASM_EMSCRIPTEN}" + -DSWIFT_WASM_ICU_UC:STRING="${WASM_ICU_UC}" + -DSWIFT_WASM_ICU_UC_INCLUDE:STRING="${WASM_ICU_UC_INCLUDE}" + -DSWIFT_WASM_ICU_I18N:STRING="${WASM_ICU_I18N}" + -DSWIFT_WASM_ICU_I18N_INCLUDE:STRING="${WASM_ICU_I18N_INCLUDE}" + -DSWIFT_WASM_ICU_DATA:STRING="${WASM_ICU_DATA}" + ) + fi + if [[ "${DARWIN_OVERLAY_TARGET}" != "" ]]; then # Split LOCAL_HOST into a pair ``arch-sdk`` # Example LOCAL_HOST: macosx-x86_64 diff --git a/utils/build_swift/build_swift/driver_arguments.py b/utils/build_swift/build_swift/driver_arguments.py index 74b108e1f7e4f..9a8a4820117bc 100644 --- a/utils/build_swift/build_swift/driver_arguments.py +++ b/utils/build_swift/build_swift/driver_arguments.py @@ -1084,6 +1084,23 @@ def create_argument_parser(): 'Currently only armv7 and aarch64 are supported. ' '%(default)s is the default.') + in_group('Build settings for Android') + + option('--wasm-emscripten', store_path, + help='An absolute path to Emscripten that will be used as a libc ' + 'implementation for Wasm builds') + + option('--wasm-icu-uc', store_path, + help='Path to libicuuc.so') + option('--wasm-icu-uc-include', store_path, + help='Path to a directory containing headers for libicuuc') + option('--wasm-icu-i18n', store_path, + help='Path to libicui18n.so') + option('--wasm-icu-i18n-include', store_path, + help='Path to a directory containing headers libicui18n') + option('--wasm-icu-data', store_path, + help='Path to libicudata.so') + # ------------------------------------------------------------------------- in_group('Experimental language features') From 600d18d884c90dacd83f1898451ae71f5c281394 Mon Sep 17 00:00:00 2001 From: Zhuowei Zhang Date: Tue, 9 Apr 2019 22:59:56 -0700 Subject: [PATCH 06/80] start adding Emscripten defines in stdlib --- stdlib/public/SwiftShims/LibcShims.h | 4 +++- stdlib/public/runtime/Errors.cpp | 2 +- stdlib/public/runtime/Exclusivity.cpp | 8 +++++++- stdlib/public/runtime/Heap.cpp | 5 +++++ stdlib/public/runtime/ImageInspectionCOFF.cpp | 2 +- stdlib/public/runtime/ImageInspectionCOFF.h | 2 +- stdlib/public/runtime/ImageInspectionELF.h | 2 +- stdlib/public/runtime/ThreadLocalStorage.h | 2 ++ stdlib/public/stubs/LibcShims.cpp | 2 +- 9 files changed, 22 insertions(+), 7 deletions(-) diff --git a/stdlib/public/SwiftShims/LibcShims.h b/stdlib/public/SwiftShims/LibcShims.h index 5725f294885f9..e8ac251ed0f5d 100644 --- a/stdlib/public/SwiftShims/LibcShims.h +++ b/stdlib/public/SwiftShims/LibcShims.h @@ -43,6 +43,8 @@ typedef __swift_uint32_t __swift_mode_t; typedef __swift_uint16_t __swift_mode_t; #elif defined(_WIN32) typedef __swift_int32_t __swift_mode_t; +#elif defined(__EMSCRIPTEN__) +typedef __swift_uint32_t __swift_mode_t; #else // just guessing typedef __swift_uint16_t __swift_mode_t; #endif @@ -105,7 +107,7 @@ static inline __swift_size_t _swift_stdlib_malloc_size(const void *ptr) { return malloc_size(ptr); } #elif defined(__linux__) || defined(__CYGWIN__) || defined(__ANDROID__) \ - || defined(__HAIKU__) || defined(__FreeBSD__) + || defined(__HAIKU__) || defined(__FreeBSD__) || defined(__EMSCRIPTEN__) static inline __swift_size_t _swift_stdlib_malloc_size(const void *ptr) { #if defined(__ANDROID__) #if !defined(__ANDROID_API__) || __ANDROID_API__ >= 17 diff --git a/stdlib/public/runtime/Errors.cpp b/stdlib/public/runtime/Errors.cpp index 6e202434750f0..e2c0dd4a7487d 100644 --- a/stdlib/public/runtime/Errors.cpp +++ b/stdlib/public/runtime/Errors.cpp @@ -14,7 +14,7 @@ // //===----------------------------------------------------------------------===// -#if defined(__CYGWIN__) || defined(__HAIKU__) +#if defined(__CYGWIN__) || defined(__HAIKU__) || defined(__EMSCRIPTEN__) #define SWIFT_SUPPORTS_BACKTRACE_REPORTING 0 #else #define SWIFT_SUPPORTS_BACKTRACE_REPORTING 1 diff --git a/stdlib/public/runtime/Exclusivity.cpp b/stdlib/public/runtime/Exclusivity.cpp index dfff68f105f0f..09c33a4b2e9e9 100644 --- a/stdlib/public/runtime/Exclusivity.cpp +++ b/stdlib/public/runtime/Exclusivity.cpp @@ -24,7 +24,9 @@ #include // Pick a return-address strategy -#if __GNUC__ +#if defined(__EMSCRIPTEN__) +#define get_return_address() ((void*) 0) +#elif __GNUC__ #define get_return_address() __builtin_return_address(0) #elif _MSC_VER #include @@ -36,7 +38,11 @@ using namespace swift; +#ifdef __EMSCRIPTEN__ +bool swift::_swift_disableExclusivityChecking = true; +#else bool swift::_swift_disableExclusivityChecking = false; +#endif static const char *getAccessName(ExclusivityFlags flags) { switch (flags) { diff --git a/stdlib/public/runtime/Heap.cpp b/stdlib/public/runtime/Heap.cpp index 46e133a7b1d17..c32fbafdf25ef 100644 --- a/stdlib/public/runtime/Heap.cpp +++ b/stdlib/public/runtime/Heap.cpp @@ -42,6 +42,11 @@ using namespace swift; #elif defined(_WIN32) # define MALLOC_ALIGN_MASK 7 +#elif defined(__EMSCRIPTEN__) +// Musl malloc is 4*sizeof(size_t), so 16 bytes on 32-bit? +// For some reason the unknown alignment code fails because std::max isn't constexpr? +# define MALLOC_ALIGN_MASK 15 + #else // Unknown alignment, but the standard requires alignment suitable for the largest // standard types. diff --git a/stdlib/public/runtime/ImageInspectionCOFF.cpp b/stdlib/public/runtime/ImageInspectionCOFF.cpp index a6f6d906f98e6..4c534b8ef82e1 100644 --- a/stdlib/public/runtime/ImageInspectionCOFF.cpp +++ b/stdlib/public/runtime/ImageInspectionCOFF.cpp @@ -10,7 +10,7 @@ // //===----------------------------------------------------------------------===// -#if !defined(__ELF__) && !defined(__MACH__) +#if !defined(__ELF__) && !defined(__MACH__) && !defined(__EMSCRIPTEN__) #include "ImageInspection.h" #include "ImageInspectionCOFF.h" diff --git a/stdlib/public/runtime/ImageInspectionCOFF.h b/stdlib/public/runtime/ImageInspectionCOFF.h index c76a33e25d0c4..5f9d2eb7a836c 100644 --- a/stdlib/public/runtime/ImageInspectionCOFF.h +++ b/stdlib/public/runtime/ImageInspectionCOFF.h @@ -19,7 +19,7 @@ #ifndef SWIFT_RUNTIME_IMAGEINSPECTIONCOFF_H #define SWIFT_RUNTIME_IMAGEINSPECTIONCOFF_H -#if !defined(__ELF__) && !defined(__MACH__) +#if !defined(__ELF__) && !defined(__MACH__) && !defined(__EMSCRIPTEN__) #include "../SwiftShims/Visibility.h" #include diff --git a/stdlib/public/runtime/ImageInspectionELF.h b/stdlib/public/runtime/ImageInspectionELF.h index afa2ee7150603..67d2c758418dc 100644 --- a/stdlib/public/runtime/ImageInspectionELF.h +++ b/stdlib/public/runtime/ImageInspectionELF.h @@ -21,7 +21,7 @@ #define SWIFT_REFLECTION_METADATA_ELF_NOTE_MAGIC_STRING "swift_reflection_metadata_magic_string" -#if defined(__ELF__) +#if defined(__ELF__) || defined(__EMSCRIPTEN__) #include "../SwiftShims/Visibility.h" #include diff --git a/stdlib/public/runtime/ThreadLocalStorage.h b/stdlib/public/runtime/ThreadLocalStorage.h index 82e2457b24f8a..243b12da5b30a 100644 --- a/stdlib/public/runtime/ThreadLocalStorage.h +++ b/stdlib/public/runtime/ThreadLocalStorage.h @@ -81,6 +81,8 @@ typedef int __swift_thread_key_t; typedef unsigned long __swift_thread_key_t; # elif defined(__HAIKU__) typedef int __swift_thread_key_t; +# elif defined(__EMSCRIPTEN__) +typedef unsigned int __swift_thread_key_t; # else typedef unsigned long __swift_thread_key_t; # endif diff --git a/stdlib/public/stubs/LibcShims.cpp b/stdlib/public/stubs/LibcShims.cpp index 083deaa4747a1..8368698067bd7 100644 --- a/stdlib/public/stubs/LibcShims.cpp +++ b/stdlib/public/stubs/LibcShims.cpp @@ -23,7 +23,7 @@ #include #include -#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) +#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) || defined(__EMSCRIPTEN__) #include #endif From 0183a71c1d6f449b5b85a8dc3968867a8c804cfd Mon Sep 17 00:00:00 2001 From: Zhuowei Zhang Date: Tue, 9 Apr 2019 23:27:33 -0700 Subject: [PATCH 07/80] actually pass the icu paths into CMake --- utils/build-script-impl | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/utils/build-script-impl b/utils/build-script-impl index 32b587c1d8954..37e8c6a1a8a69 100755 --- a/utils/build-script-impl +++ b/utils/build-script-impl @@ -1590,11 +1590,11 @@ for host in "${ALL_HOSTS[@]}"; do cmake_options=( "${cmake_options[@]}" -DSWIFT_WASM_EMSCRIPTEN_PATH:STRING="${WASM_EMSCRIPTEN}" - -DSWIFT_WASM_ICU_UC:STRING="${WASM_ICU_UC}" - -DSWIFT_WASM_ICU_UC_INCLUDE:STRING="${WASM_ICU_UC_INCLUDE}" - -DSWIFT_WASM_ICU_I18N:STRING="${WASM_ICU_I18N}" - -DSWIFT_WASM_ICU_I18N_INCLUDE:STRING="${WASM_ICU_I18N_INCLUDE}" - -DSWIFT_WASM_ICU_DATA:STRING="${WASM_ICU_DATA}" + -DSWIFT_WASM_wasm32_ICU_UC:STRING="${WASM_ICU_UC}" + -DSWIFT_WASM_wasm32_ICU_UC_INCLUDE:STRING="${WASM_ICU_UC_INCLUDE}" + -DSWIFT_WASM_wasm32_ICU_I18N:STRING="${WASM_ICU_I18N}" + -DSWIFT_WASM_wasm32_ICU_I18N_INCLUDE:STRING="${WASM_ICU_I18N_INCLUDE}" + -DSWIFT_WASM_wasm32_ICU_DATA:STRING="${WASM_ICU_DATA}" ) fi From 00d28d5d24032247e1de9b14ca13f75f7f5614c1 Mon Sep 17 00:00:00 2001 From: Zhuowei Zhang Date: Tue, 9 Apr 2019 23:51:43 -0700 Subject: [PATCH 08/80] do not link WebAssembly shared libraries for now We have no working linker. --- cmake/modules/AddSwift.cmake | 4 +++- fakeld | 9 +++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) create mode 100755 fakeld diff --git a/cmake/modules/AddSwift.cmake b/cmake/modules/AddSwift.cmake index f2bd809e70bf0..1a7f6a575e090 100644 --- a/cmake/modules/AddSwift.cmake +++ b/cmake/modules/AddSwift.cmake @@ -597,7 +597,9 @@ function(_add_variant_link_flags) if(NOT SWIFT_COMPILER_IS_MSVC_LIKE) # FIXME: On Apple platforms, find_program needs to look for "ld64.lld" find_program(LDLLD_PATH "ld.lld") - if((SWIFT_ENABLE_LLD_LINKER AND LDLLD_PATH AND NOT APPLE) OR + if("${LFLAGS_SDK}" STREQUAL "WASM") + list(APPEND result "-fuse-ld=/home/zhuowei/swift-source/swift/fakeld") + elseif((SWIFT_ENABLE_LLD_LINKER AND LDLLD_PATH AND NOT APPLE) OR ("${LFLAGS_SDK}" STREQUAL "WINDOWS" AND NOT "${CMAKE_SYSTEM_NAME}" STREQUAL "WINDOWS")) list(APPEND result "-fuse-ld=lld") diff --git a/fakeld b/fakeld new file mode 100755 index 0000000000000..1df157004de91 --- /dev/null +++ b/fakeld @@ -0,0 +1,9 @@ +#!/usr/bin/env python3 +import sys +def outputname(): + for i in range(len(sys.argv)): + if sys.argv[i] == "-o": + return sys.argv[i + 1] + return "a.out" +with open(outputname(), "wb") as outfile: + pass From d3960c38a7f54bb7b943c28ae74ff777c3cccaac Mon Sep 17 00:00:00 2001 From: Zhuowei Zhang Date: Wed, 10 Apr 2019 16:50:21 -0700 Subject: [PATCH 09/80] add script to launch build --- vvv.sh | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100755 vvv.sh diff --git a/vvv.sh b/vvv.sh new file mode 100755 index 0000000000000..bcb75036bf6b1 --- /dev/null +++ b/vvv.sh @@ -0,0 +1,10 @@ +utils/build-script --release-debuginfo --wasm \ + --llvm-targets-to-build "X86;ARM;AArch64;PowerPC;SystemZ;WebAssembly" \ + --llvm-max-parallel-lto-link-jobs 1 --swift-tools-max-parallel-lto-link-jobs 1 \ + --wasm-emscripten "/home/zhuowei/Documents/emsdk/emscripten/1.38.30" \ + --wasm-icu-uc "todo" \ + --wasm-icu-uc-include "$PWD/NO" \ + --wasm-icu-i18n "todo" \ + --wasm-icu-i18n-include "todo" \ + --wasm-icu-data "todo" \ + "$@" From d05719bdf3e89befbe50cab6ce4976e38d6674d8 Mon Sep 17 00:00:00 2001 From: Zhuowei Zhang Date: Wed, 10 Apr 2019 17:46:07 -0700 Subject: [PATCH 10/80] start adding wasm32 to 32-bit checks in stdlib --- stdlib/public/core/AtomicInt.swift.gyb | 8 +++---- stdlib/public/core/Builtin.swift | 2 +- stdlib/public/core/SmallString.swift | 2 +- stdlib/public/core/StringGuts.swift | 2 +- stdlib/public/core/StringObject.swift | 32 +++++++++++++------------- stdlib/public/core/StringStorage.swift | 12 +++++----- 6 files changed, 29 insertions(+), 29 deletions(-) diff --git a/stdlib/public/core/AtomicInt.swift.gyb b/stdlib/public/core/AtomicInt.swift.gyb index 62217f282ccc5..80514f42c147c 100644 --- a/stdlib/public/core/AtomicInt.swift.gyb +++ b/stdlib/public/core/AtomicInt.swift.gyb @@ -65,7 +65,7 @@ internal func _swift_stdlib_atomicCompareExchangeStrongInt( object target: UnsafeMutablePointer, expected: UnsafeMutablePointer, desired: Int) -> Bool { -#if arch(i386) || arch(arm) +#if arch(i386) || arch(arm) || arch(wasm32) let (oldValue, won) = Builtin.cmpxchg_seqcst_seqcst_Int32( target._rawValue, expected.pointee._value, desired._value) #elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x) @@ -82,7 +82,7 @@ internal func _swift_stdlib_atomicCompareExchangeStrongInt( public // Existing uses outside stdlib func _swift_stdlib_atomicLoadInt( object target: UnsafeMutablePointer) -> Int { -#if arch(i386) || arch(arm) +#if arch(i386) || arch(arm) || arch(wasm32) let value = Builtin.atomicload_seqcst_Int32(target._rawValue) return Int(value) #elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x) @@ -95,7 +95,7 @@ func _swift_stdlib_atomicLoadInt( internal func _swift_stdlib_atomicStoreInt( object target: UnsafeMutablePointer, desired: Int) { -#if arch(i386) || arch(arm) +#if arch(i386) || arch(arm) || arch(wasm32) Builtin.atomicstore_seqcst_Int32(target._rawValue, desired._value) #elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x) Builtin.atomicstore_seqcst_Int64(target._rawValue, desired._value) @@ -111,7 +111,7 @@ func _swift_stdlib_atomicFetch${operation}Int( object target: UnsafeMutablePointer, operand: Int) -> Int { let rawTarget = UnsafeMutableRawPointer(target) -#if arch(i386) || arch(arm) +#if arch(i386) || arch(arm) || arch(wasm32) let value = _swift_stdlib_atomicFetch${operation}Int32( object: rawTarget.assumingMemoryBound(to: Int32.self), operand: Int32(operand)) diff --git a/stdlib/public/core/Builtin.swift b/stdlib/public/core/Builtin.swift index 07c9ce14af6a7..b29bcc0d6aa59 100644 --- a/stdlib/public/core/Builtin.swift +++ b/stdlib/public/core/Builtin.swift @@ -379,7 +379,7 @@ internal var _objectPointerLowSpareBitShift: UInt { } #if arch(i386) || arch(arm) || arch(powerpc64) || arch(powerpc64le) || arch( - s390x) + s390x) || arch(wasm32) @inlinable internal var _objectPointerIsObjCBit: UInt { @inline(__always) get { return 0x0000_0002 } diff --git a/stdlib/public/core/SmallString.swift b/stdlib/public/core/SmallString.swift index 1f53f8e7ff4fb..9792ed1cf485f 100644 --- a/stdlib/public/core/SmallString.swift +++ b/stdlib/public/core/SmallString.swift @@ -76,7 +76,7 @@ internal struct _SmallString { extension _SmallString { @inlinable @inline(__always) internal static var capacity: Int { -#if arch(i386) || arch(arm) +#if arch(i386) || arch(arm) || arch(wasm32) return 10 #else return 15 diff --git a/stdlib/public/core/StringGuts.swift b/stdlib/public/core/StringGuts.swift index 2b7765cd4093e..cc6ed08226e67 100644 --- a/stdlib/public/core/StringGuts.swift +++ b/stdlib/public/core/StringGuts.swift @@ -178,7 +178,7 @@ extension _StringGuts { #else @usableFromInline @inline(never) @_effects(releasenone) internal func _invariantCheck() { - #if arch(i386) || arch(arm) + #if arch(i386) || arch(arm) || arch(wasm32) _internalInvariant(MemoryLayout.size == 12, """ the runtime is depending on this, update Reflection.mm and \ this if you change it diff --git a/stdlib/public/core/StringObject.swift b/stdlib/public/core/StringObject.swift index 501d81b66ff53..72cf15c5200c7 100644 --- a/stdlib/public/core/StringObject.swift +++ b/stdlib/public/core/StringObject.swift @@ -77,7 +77,7 @@ internal struct _StringObject { internal init(zero: ()) { self._storage = 0 } } -#if arch(i386) || arch(arm) +#if arch(i386) || arch(arm) || arch(wasm32) @usableFromInline @frozen internal enum Variant { case immortal(UInt) @@ -169,7 +169,7 @@ extension _StringObject { @usableFromInline internal typealias RawBitPattern = (UInt64, UInt64) -#if arch(i386) || arch(arm) +#if arch(i386) || arch(arm) || arch(wasm32) // On 32-bit platforms, raw bit conversion is one-way only and uses the same // layout as on 64-bit platforms. @usableFromInline @@ -245,7 +245,7 @@ extension _StringObject { @inlinable @_transparent internal var discriminatedObjectRawBits: UInt64 { -#if arch(i386) || arch(arm) +#if arch(i386) || arch(arm) || arch(wasm32) let low32: UInt switch _variant { case .immortal(let bitPattern): @@ -387,7 +387,7 @@ extension _StringObject.Nibbles { extension _StringObject { @inlinable @inline(__always) internal static var nativeBias: UInt { -#if arch(i386) || arch(arm) +#if arch(i386) || arch(arm) || arch(wasm32) return 20 #else return 32 @@ -512,7 +512,7 @@ extension _StringObject { // spare bits (the most significant nibble) in a pointer. let word1 = small.rawBits.0.littleEndian let word2 = small.rawBits.1.littleEndian -#if arch(i386) || arch(arm) +#if arch(i386) || arch(arm) || arch(wasm32) // On 32-bit, we need to unpack the small string. let smallStringDiscriminatorAndCount: UInt64 = 0xFF00_0000_0000_0000 @@ -556,7 +556,7 @@ extension _StringObject { @inlinable @inline(__always) internal init(empty:()) { // Canonical empty pattern: small zero-length string -#if arch(i386) || arch(arm) +#if arch(i386) || arch(arm) || arch(wasm32) self.init( count: 0, variant: .immortal(0), @@ -819,7 +819,7 @@ extension _StringObject { @inline(__always) internal var nativeStorage: __StringStorage { -#if arch(i386) || arch(arm) +#if arch(i386) || arch(arm) || arch(wasm32) guard case .native(let storage) = _variant else { _internalInvariantFailure() } @@ -832,7 +832,7 @@ extension _StringObject { @inline(__always) internal var sharedStorage: __SharedStringStorage { -#if arch(i386) || arch(arm) +#if arch(i386) || arch(arm) || arch(wasm32) guard case .native(let storage) = _variant else { _internalInvariantFailure() } @@ -846,7 +846,7 @@ extension _StringObject { @inline(__always) internal var cocoaObject: AnyObject { -#if arch(i386) || arch(arm) +#if arch(i386) || arch(arm) || arch(wasm32) guard case .bridged(let object) = _variant else { _internalInvariantFailure() } @@ -935,7 +935,7 @@ extension _StringObject { internal init(immortal bufPtr: UnsafeBufferPointer, isASCII: Bool) { let countAndFlags = CountAndFlags( immortalCount: bufPtr.count, isASCII: isASCII) -#if arch(i386) || arch(arm) +#if arch(i386) || arch(arm) || arch(wasm32) self.init( variant: .immortal(start: bufPtr.baseAddress._unsafelyUnwrappedUnchecked), discriminator: Nibbles.largeImmortal(), @@ -955,7 +955,7 @@ extension _StringObject { @inline(__always) internal init(_ storage: __StringStorage) { -#if arch(i386) || arch(arm) +#if arch(i386) || arch(arm) || arch(wasm32) self.init( variant: .native(storage), discriminator: Nibbles.largeMortal(), @@ -969,7 +969,7 @@ extension _StringObject { } internal init(_ storage: __SharedStringStorage) { -#if arch(i386) || arch(arm) +#if arch(i386) || arch(arm) || arch(wasm32) self.init( variant: .native(storage), discriminator: Nibbles.largeMortal(), @@ -987,7 +987,7 @@ extension _StringObject { ) { let countAndFlags = CountAndFlags(sharedCount: length, isASCII: isASCII) let discriminator = Nibbles.largeCocoa(providesFastUTF8: providesFastUTF8) -#if arch(i386) || arch(arm) +#if arch(i386) || arch(arm) || arch(wasm32) self.init( variant: .bridged(cocoa), discriminator: discriminator, @@ -1009,7 +1009,7 @@ extension _StringObject { #else @usableFromInline @inline(never) @_effects(releasenone) internal func _invariantCheck() { - #if arch(i386) || arch(arm) + #if arch(i386) || arch(arm) || arch(wasm32) _internalInvariant(MemoryLayout<_StringObject>.size == 12) _internalInvariant(MemoryLayout<_StringObject>.stride == 12) _internalInvariant(MemoryLayout<_StringObject>.alignment == 4) @@ -1079,7 +1079,7 @@ extension _StringObject { } } - #if arch(i386) || arch(arm) + #if arch(i386) || arch(arm) || arch(wasm32) switch _variant { case .immortal: _internalInvariant(isImmortal) @@ -1099,7 +1099,7 @@ extension _StringObject { let raw = self.rawBits let word0 = ("0000000000000000" + String(raw.0, radix: 16)).suffix(16) let word1 = ("0000000000000000" + String(raw.1, radix: 16)).suffix(16) -#if arch(i386) || arch(arm) +#if arch(i386) || arch(arm) || arch(wasm32) print(""" StringObject(\ <\(word0) \(word1)> \ diff --git a/stdlib/public/core/StringStorage.swift b/stdlib/public/core/StringStorage.swift index 8e8a3d29d2133..a80937d1f7141 100644 --- a/stdlib/public/core/StringStorage.swift +++ b/stdlib/public/core/StringStorage.swift @@ -46,7 +46,7 @@ private typealias CountAndFlags = _StringObject.CountAndFlags // renamed. The old name must not be used in the new runtime. final internal class __StringStorage : __SwiftNativeNSString, _AbstractStringStorage { -#if arch(i386) || arch(arm) +#if arch(i386) || arch(arm) || arch(wasm32) // The total allocated storage capacity. Note that this includes the required // nul-terminator. internal var _realCapacity: Int @@ -106,7 +106,7 @@ final internal class __StringStorage // for Strings ~1KB or larger, though at this point we're well into our growth // curve. private func determineCodeUnitCapacity(_ desiredCapacity: Int) -> Int { -#if arch(i386) || arch(arm) +#if arch(i386) || arch(arm) || arch(wasm32) // FIXME: Adapt to actual 32-bit allocator. For now, let's arrange things so // that the instance size will be a multiple of 4. let bias = Int(bitPattern: _StringObject.nativeBias) @@ -139,7 +139,7 @@ extension __StringStorage { __StringStorage.self, realCodeUnitCapacity._builtinWordValue, UInt8.self, 1._builtinWordValue, Optional<_StringBreadcrumbs>.self) -#if arch(i386) || arch(arm) +#if arch(i386) || arch(arm) || arch(wasm32) storage._realCapacity = realCodeUnitCapacity storage._count = countAndFlags.count storage._flags = countAndFlags.flags @@ -319,7 +319,7 @@ extension __StringStorage { internal func _updateCountAndFlags(newCount: Int, newIsASCII: Bool) { let countAndFlags = CountAndFlags( mortalCount: newCount, isASCII: newIsASCII) -#if arch(i386) || arch(arm) +#if arch(i386) || arch(arm) || arch(wasm32) self._count = countAndFlags.count self._flags = countAndFlags.flags #else @@ -463,7 +463,7 @@ final internal class __SharedStringStorage internal var _owner: AnyObject? internal var start: UnsafePointer -#if arch(i386) || arch(arm) +#if arch(i386) || arch(arm) || arch(wasm32) internal var _count: Int internal var _flags: UInt16 @@ -485,7 +485,7 @@ final internal class __SharedStringStorage ) { self._owner = nil self.start = ptr -#if arch(i386) || arch(arm) +#if arch(i386) || arch(arm) || arch(wasm32) self._count = countAndFlags.count self._flags = countAndFlags.flags #else From 51e68012a5643f9e5f44aa281823a236f3010ffe Mon Sep 17 00:00:00 2001 From: Zhuowei Zhang Date: Wed, 10 Apr 2019 18:31:05 -0700 Subject: [PATCH 11/80] WebAssembly: more conditional compile defines --- stdlib/public/Platform/Platform.swift | 4 ++-- stdlib/public/core/BridgeStorage.swift | 2 +- stdlib/public/core/DictionaryVariant.swift | 2 +- stdlib/public/core/Hasher.swift | 4 ++-- stdlib/public/core/SetVariant.swift | 2 +- stdlib/public/core/StringBridge.swift | 2 +- 6 files changed, 8 insertions(+), 8 deletions(-) diff --git a/stdlib/public/Platform/Platform.swift b/stdlib/public/Platform/Platform.swift index f5e54c7da1283..347931eb3b2fe 100644 --- a/stdlib/public/Platform/Platform.swift +++ b/stdlib/public/Platform/Platform.swift @@ -332,7 +332,7 @@ public var SIG_DFL: sig_t? { return nil } public var SIG_IGN: sig_t { return unsafeBitCast(1, to: sig_t.self) } public var SIG_ERR: sig_t { return unsafeBitCast(-1, to: sig_t.self) } public var SIG_HOLD: sig_t { return unsafeBitCast(5, to: sig_t.self) } -#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Haiku) +#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Haiku) || os(Wasm) public typealias sighandler_t = __sighandler_t public var SIG_DFL: sighandler_t? { return nil } @@ -380,7 +380,7 @@ public var SEM_FAILED: UnsafeMutablePointer? { #if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) // The value is ABI. Value verified to be correct for OS X, iOS, watchOS, tvOS. return UnsafeMutablePointer(bitPattern: -1) -#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku) +#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku) || os(Wasm) // The value is ABI. Value verified to be correct on Glibc. return UnsafeMutablePointer(bitPattern: 0) #else diff --git a/stdlib/public/core/BridgeStorage.swift b/stdlib/public/core/BridgeStorage.swift index 4b100f28f7cfe..fcc971f8b6f76 100644 --- a/stdlib/public/core/BridgeStorage.swift +++ b/stdlib/public/core/BridgeStorage.swift @@ -61,7 +61,7 @@ internal struct _BridgeStorage { rawValue = Builtin.reinterpretCast(native) } -#if !(arch(i386) || arch(arm)) +#if !(arch(i386) || arch(arm) || arch(wasm32)) @inlinable @inline(__always) internal init(taggedPayload: UInt) { diff --git a/stdlib/public/core/DictionaryVariant.swift b/stdlib/public/core/DictionaryVariant.swift index 988ef9f7d0679..5ffaacc45ef32 100644 --- a/stdlib/public/core/DictionaryVariant.swift +++ b/stdlib/public/core/DictionaryVariant.swift @@ -46,7 +46,7 @@ extension Dictionary { @inlinable @inline(__always) init(dummy: Void) { -#if arch(i386) || arch(arm) +#if arch(i386) || arch(arm) || arch(wasm32) self.init(native: _NativeDictionary()) #else self.object = _BridgeStorage(taggedPayload: 0) diff --git a/stdlib/public/core/Hasher.swift b/stdlib/public/core/Hasher.swift index f7098db934aa3..202c2830610ab 100644 --- a/stdlib/public/core/Hasher.swift +++ b/stdlib/public/core/Hasher.swift @@ -160,7 +160,7 @@ extension Hasher { @inline(__always) internal mutating func combine(_ value: UInt) { -#if arch(i386) || arch(arm) +#if arch(i386) || arch(arm) || arch(wasm32) combine(UInt32(truncatingIfNeeded: value)) #else combine(UInt64(truncatingIfNeeded: value)) @@ -423,7 +423,7 @@ public struct Hasher { @usableFromInline internal static func _hash(seed: Int, _ value: UInt) -> Int { var state = _State(seed: seed) -#if arch(i386) || arch(arm) +#if arch(i386) || arch(arm) || arch(wasm32) _internalInvariant(UInt.bitWidth < UInt64.bitWidth) let tbc = _TailBuffer( tail: UInt64(truncatingIfNeeded: value), diff --git a/stdlib/public/core/SetVariant.swift b/stdlib/public/core/SetVariant.swift index 0092fc74ca45e..ae3149002c7c4 100644 --- a/stdlib/public/core/SetVariant.swift +++ b/stdlib/public/core/SetVariant.swift @@ -36,7 +36,7 @@ extension Set { @inlinable @inline(__always) init(dummy: ()) { -#if arch(i386) || arch(arm) +#if arch(i386) || arch(arm) || arch(wasm32) self.init(native: _NativeSet()) #else self.object = _BridgeStorage(taggedPayload: 0) diff --git a/stdlib/public/core/StringBridge.swift b/stdlib/public/core/StringBridge.swift index 1896d974204b6..2b892c2d52867 100644 --- a/stdlib/public/core/StringBridge.swift +++ b/stdlib/public/core/StringBridge.swift @@ -285,7 +285,7 @@ internal enum _KnownCocoaString { case storage case shared case cocoa -#if !(arch(i386) || arch(arm)) +#if !(arch(i386) || arch(arm) || arch(wasm32)) case tagged #endif From 53291de3540eb6eb5f9551825fa5f2ba62fc2ef3 Mon Sep 17 00:00:00 2001 From: Zhuowei Zhang Date: Wed, 10 Apr 2019 21:11:53 -0700 Subject: [PATCH 12/80] Hack: Remove returnaddress calls emitted by exclusivity checks WebAssembly doesn't have __builtin_return_address. --- lib/IRGen/IRGenSIL.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/IRGen/IRGenSIL.cpp b/lib/IRGen/IRGenSIL.cpp index 7292f9e0160ca..d3394346328bd 100644 --- a/lib/IRGen/IRGenSIL.cpp +++ b/lib/IRGen/IRGenSIL.cpp @@ -4343,7 +4343,8 @@ void IRGenSILFunction::visitBeginUnpairedAccessInst( // in which case we should use the caller, which is generally ok because // materializeForSet can't usually be thunked. llvm::Value *pc; - if (hasBeenInlined(access)) { + // hack: wasm doesn't have returnaddress + if (true || hasBeenInlined(access)) { pc = llvm::ConstantPointerNull::get(IGM.Int8PtrTy); } else { auto retAddrFn = From 24804838abebd605555b53fb73eb4afeb8bdb782 Mon Sep 17 00:00:00 2001 From: Zhuowei Zhang Date: Wed, 10 Apr 2019 21:58:07 -0700 Subject: [PATCH 13/80] WebAssembly: Hack: disable all atomic instructions This matches what Clang does. This doesn't check that the target is Wasm; will need to do that. Patcheng's port seems to have working? atomics, so it's probably possible to get atomics working, but this is fine for now. This still doesn't get the stdlib to compile, but at least the error now matches what I get when I run clang on the -emit-ir LLVM IR output. --- lib/IRGen/IRGen.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/IRGen/IRGen.cpp b/lib/IRGen/IRGen.cpp index a39ff7e63e4cd..40ecb7b8379ea 100644 --- a/lib/IRGen/IRGen.cpp +++ b/lib/IRGen/IRGen.cpp @@ -162,6 +162,11 @@ swift::getIRTargetOptions(const IRGenOptions &Opts, ASTContext &Ctx) { // Explicitly request debugger tuning for LLDB which is the default // on Darwin platforms but not on others. TargetOpts.DebuggerTuning = llvm::DebuggerKind::LLDB; + + // WebAssembly HACK: disable atomics + if (Ctx->LangOpts.Target.isOSBinFormatWasm()) { + TargetOpts.ThreadModel = llvm::ThreadModel::Single; + } TargetOpts.FunctionSections = Opts.FunctionSections; auto *Clang = static_cast(Ctx.getClangModuleLoader()); From 14b49257a9e43d5a15d55829dc72aa8c7572ca29 Mon Sep 17 00:00:00 2001 From: Zhuowei Zhang Date: Fri, 12 Apr 2019 21:47:00 -0700 Subject: [PATCH 14/80] change non-Emscripten specific ifdefs to check for __wasm__ instead --- stdlib/public/SwiftShims/Visibility.h | 2 +- stdlib/public/runtime/Exclusivity.cpp | 4 ++-- stdlib/public/runtime/ImageInspectionCOFF.cpp | 2 +- stdlib/public/runtime/ImageInspectionCOFF.h | 2 +- stdlib/public/runtime/ImageInspectionELF.h | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/stdlib/public/SwiftShims/Visibility.h b/stdlib/public/SwiftShims/Visibility.h index 6ebea03367541..8245a3b9be7e7 100644 --- a/stdlib/public/SwiftShims/Visibility.h +++ b/stdlib/public/SwiftShims/Visibility.h @@ -76,7 +76,7 @@ // SWIFT_RUNTIME_EXPORT on the library it's exported from. /// Attribute used to export symbols from the runtime. -#if defined(__MACH__) || defined(__EMSCRIPTEN__) +#if defined(__MACH__) || defined(__wasm__) # define SWIFT_EXPORT_ATTRIBUTE __attribute__((__visibility__("default"))) diff --git a/stdlib/public/runtime/Exclusivity.cpp b/stdlib/public/runtime/Exclusivity.cpp index 09c33a4b2e9e9..59acc280dfadf 100644 --- a/stdlib/public/runtime/Exclusivity.cpp +++ b/stdlib/public/runtime/Exclusivity.cpp @@ -24,7 +24,7 @@ #include // Pick a return-address strategy -#if defined(__EMSCRIPTEN__) +#if defined(__wasm__) #define get_return_address() ((void*) 0) #elif __GNUC__ #define get_return_address() __builtin_return_address(0) @@ -38,7 +38,7 @@ using namespace swift; -#ifdef __EMSCRIPTEN__ +#ifdef __wasm__ bool swift::_swift_disableExclusivityChecking = true; #else bool swift::_swift_disableExclusivityChecking = false; diff --git a/stdlib/public/runtime/ImageInspectionCOFF.cpp b/stdlib/public/runtime/ImageInspectionCOFF.cpp index 4c534b8ef82e1..09f0251f12bb6 100644 --- a/stdlib/public/runtime/ImageInspectionCOFF.cpp +++ b/stdlib/public/runtime/ImageInspectionCOFF.cpp @@ -10,7 +10,7 @@ // //===----------------------------------------------------------------------===// -#if !defined(__ELF__) && !defined(__MACH__) && !defined(__EMSCRIPTEN__) +#if !defined(__ELF__) && !defined(__MACH__) && !defined(__wasm__) #include "ImageInspection.h" #include "ImageInspectionCOFF.h" diff --git a/stdlib/public/runtime/ImageInspectionCOFF.h b/stdlib/public/runtime/ImageInspectionCOFF.h index 5f9d2eb7a836c..b5e54c46687db 100644 --- a/stdlib/public/runtime/ImageInspectionCOFF.h +++ b/stdlib/public/runtime/ImageInspectionCOFF.h @@ -19,7 +19,7 @@ #ifndef SWIFT_RUNTIME_IMAGEINSPECTIONCOFF_H #define SWIFT_RUNTIME_IMAGEINSPECTIONCOFF_H -#if !defined(__ELF__) && !defined(__MACH__) && !defined(__EMSCRIPTEN__) +#if !defined(__ELF__) && !defined(__MACH__) && !defined(__wasm__) #include "../SwiftShims/Visibility.h" #include diff --git a/stdlib/public/runtime/ImageInspectionELF.h b/stdlib/public/runtime/ImageInspectionELF.h index 67d2c758418dc..6b120897fc926 100644 --- a/stdlib/public/runtime/ImageInspectionELF.h +++ b/stdlib/public/runtime/ImageInspectionELF.h @@ -21,7 +21,7 @@ #define SWIFT_REFLECTION_METADATA_ELF_NOTE_MAGIC_STRING "swift_reflection_metadata_magic_string" -#if defined(__ELF__) || defined(__EMSCRIPTEN__) +#if defined(__ELF__) || defined(__wasm__) #include "../SwiftShims/Visibility.h" #include From 9354b788f85850f55c3d5d6e687798f2d2d11851 Mon Sep 17 00:00:00 2001 From: Zhuowei Zhang Date: Fri, 12 Apr 2019 22:42:06 -0700 Subject: [PATCH 15/80] Switch to WASI --- CMakeLists.txt | 2 +- cmake/modules/AddSwift.cmake | 17 +---------------- cmake/modules/SwiftConfigureSDK.cmake | 13 ++----------- cmake/modules/SwiftWasmSupport.cmake | 16 ---------------- include/swift/Runtime/Mutex.h | 2 +- include/swift/Runtime/MutexPThread.h | 2 +- stdlib/public/SwiftShims/LibcShims.h | 4 ++-- stdlib/public/runtime/Errors.cpp | 2 +- stdlib/public/runtime/Heap.cpp | 2 +- stdlib/public/runtime/ThreadLocalStorage.h | 2 +- stdlib/public/stubs/LibcShims.cpp | 2 +- utils/build-script | 6 +++--- utils/build-script-impl | 2 +- .../build_swift/build_swift/driver_arguments.py | 4 ++-- vvv.sh | 2 +- 15 files changed, 19 insertions(+), 59 deletions(-) delete mode 100644 cmake/modules/SwiftWasmSupport.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 058633a703aa2..544c6dc4aabd3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -781,7 +781,7 @@ if(swift_build_windows AND NOT "${CMAKE_SYSTEM_NAME}" STREQUAL "Windows") configure_sdk_windows("Windows" "msvc" "${SWIFT_SDK_WINDOWS_ARCHITECTURES}") endif() -# Should we cross-compile the standard library for WebAssembly (Emscripten)? +# Should we cross-compile the standard library for WebAssembly (WASI)? is_sdk_requested(WASM swift_build_wasm) if(swift_build_wasm AND NOT "${SWIFT_HOST_VARIANT_SDK}" STREQUAL "WASM") #if ("${SWIFT_ANDROID_NDK_PATH}" STREQUAL "") diff --git a/cmake/modules/AddSwift.cmake b/cmake/modules/AddSwift.cmake index 1a7f6a575e090..80b5dbc8088f9 100644 --- a/cmake/modules/AddSwift.cmake +++ b/cmake/modules/AddSwift.cmake @@ -3,7 +3,6 @@ include(SwiftList) include(SwiftXcodeSupport) include(SwiftWindowsSupport) include(SwiftAndroidSupport) -include(SwiftWasmSupport) # SWIFTLIB_DIR is the directory in the build tree where Swift resource files # should be placed. Note that $CMAKE_CFG_INTDIR expands to "." for @@ -372,12 +371,6 @@ function(_add_variant_c_compile_flags) foreach(path ${${CFLAGS_ARCH}_INCLUDE}) list(APPEND result "\"${CMAKE_INCLUDE_FLAG_C}${path}\"") endforeach() - elseif(CFLAGS_SDK STREQUAL WASM) - list(APPEND result "-D__EMSCRIPTEN__=1") - swift_wasm_include_for_arch(${CFLAGS_ARCH} ${CFLAGS_ARCH}_INCLUDE) - foreach(path ${${CFLAGS_ARCH}_INCLUDE}) - list(APPEND result "\"${CMAKE_INCLUDE_FLAG_C}${path}\"") - endforeach() endif() set(ICU_UC_INCLUDE_DIR ${SWIFT_${CFLAGS_SDK}_${CFLAGS_ARCH}_ICU_UC_INCLUDE}) @@ -446,11 +439,6 @@ function(_add_variant_swift_compile_flags foreach(path IN LISTS ${arch}_swift_include) list(APPEND result "\"${CMAKE_INCLUDE_FLAG_C}${path}\"") endforeach() - elseif("${sdk}" STREQUAL "WASM") - swift_wasm_include_for_arch(${arch} ${arch}_swift_include) - foreach(path IN LISTS ${arch}_swift_include) - list(APPEND result "\"${CMAKE_INCLUDE_FLAG_C}${path}\"") - endforeach() endif() if(NOT BUILD_STANDALONE) @@ -564,10 +552,7 @@ function(_add_variant_link_flags) list(APPEND library_search_directories ${path}) endforeach() elseif("${LFLAGS_SDK}" STREQUAL "WASM") - swift_wasm_lib_for_arch(${LFLAGS_ARCH} ${LFLAGS_ARCH}_LIB) - foreach(path IN LISTS ${LFLAGS_ARCH}_LIB) - list(APPEND library_search_directories ${path}) - endforeach() + # No extra libraries needed. else() # If lto is enabled, we need to add the object path flag so that the LTO code # generator leaves the intermediate object file in a place where it will not diff --git a/cmake/modules/SwiftConfigureSDK.cmake b/cmake/modules/SwiftConfigureSDK.cmake index e8cb4d380749f..1383a20c4e5d6 100644 --- a/cmake/modules/SwiftConfigureSDK.cmake +++ b/cmake/modules/SwiftConfigureSDK.cmake @@ -7,7 +7,6 @@ set(SWIFT_CONFIGURED_SDKS) include(SwiftWindowsSupport) include(SwiftAndroidSupport) -include(SwiftWasmSupport) # Report the given SDK to the user. function(_report_sdk prefix) @@ -60,14 +59,6 @@ function(_report_sdk prefix) message(STATUS " ${arch} INCLUDE: ${${arch}_INCLUDE}") message(STATUS " ${arch} LIB: ${${arch}_LIB}") endforeach() - elseif("${prefix}" STREQUAL "WASM") - message(STATUS " Emscripten Dir: $ENV{SWIFT_WASM_EMSCRIPTEN_PATH}") - foreach(arch ${SWIFT_SDK_${prefix}_ARCHITECTURES}) - swift_wasm_include_for_arch(${arch} ${arch}_INCLUDE) - swift_wasm_lib_for_arch(${arch} ${arch}_LIB) - message(STATUS " ${arch} INCLUDE: ${${arch}_INCLUDE}") - message(STATUS " ${arch} LIB: ${${arch}_LIB}") - endforeach() else() foreach(arch ${SWIFT_SDK_${prefix}_ARCHITECTURES}) message(STATUS " ${arch} Path: ${SWIFT_SDK_${prefix}_ARCH_${arch}_PATH}") @@ -346,8 +337,8 @@ macro(configure_sdk_unix name architectures) if(NOT arch STREQUAL wasm32) message(FATAL_ERROR "unsupported arch for WebAssembly: ${arch}") endif() - # FIXME: this is actually wrong: emscripten doesn't use sysroot. - set(SWIFT_SDK_WASM_ARCH_wasm32_PATH "${SWIFT_WASM_EMSCRIPTEN_PATH}/system") + set(SWIFT_SDK_WASM_ARCH_wasm32_PATH "${SWIFT_WASM_WASI_SDK_PATH}/share/sysroot") + # fixme: Wasi is wasm32-unknown-wasi-musl. This LLVM doesn't have it yet. set(SWIFT_SDK_WASM_ARCH_wasm32_TRIPLE "wasm32-unknown-unknown-wasm") else() message(FATAL_ERROR "unknown Unix OS: ${prefix}") diff --git a/cmake/modules/SwiftWasmSupport.cmake b/cmake/modules/SwiftWasmSupport.cmake deleted file mode 100644 index 1d14263fbb49a..0000000000000 --- a/cmake/modules/SwiftWasmSupport.cmake +++ /dev/null @@ -1,16 +0,0 @@ -function(swift_wasm_include_for_arch arch var) - set(paths) - list(APPEND paths - "${SWIFT_WASM_EMSCRIPTEN_PATH}/system/include/libcxx" - "${SWIFT_WASM_EMSCRIPTEN_PATH}/system/lib/libcxxabi/include" - "${SWIFT_WASM_EMSCRIPTEN_PATH}/system/include/compat" - "${SWIFT_WASM_EMSCRIPTEN_PATH}/system/include" - "${SWIFT_WASM_EMSCRIPTEN_PATH}/system/include/libc" - "${SWIFT_WASM_EMSCRIPTEN_PATH}/system/lib/libc/musl/arch/emscripten" - "${SWIFT_WASM_EMSCRIPTEN_PATH}/system/local/include" - "${SWIFT_WASM_EMSCRIPTEN_PATH}/system/include/SDL") - set(${var} ${paths} PARENT_SCOPE) -endfunction() - -function(swift_wasm_lib_for_arch arch var) -endfunction() diff --git a/include/swift/Runtime/Mutex.h b/include/swift/Runtime/Mutex.h index bfaa8a397e9f3..3bf61177f9790 100644 --- a/include/swift/Runtime/Mutex.h +++ b/include/swift/Runtime/Mutex.h @@ -20,7 +20,7 @@ #include -#if (defined(__APPLE__) || defined(__linux__) || defined(__CYGWIN__) || defined(__FreeBSD__) || defined(__HAIKU__) || defined(__EMSCRIPTEN__)) +#if (defined(__APPLE__) || defined(__linux__) || defined(__CYGWIN__) || defined(__FreeBSD__) || defined(__HAIKU__) || defined(__wasi__)) #include "swift/Runtime/MutexPThread.h" #elif defined(_WIN32) #include "swift/Runtime/MutexWin32.h" diff --git a/include/swift/Runtime/MutexPThread.h b/include/swift/Runtime/MutexPThread.h index fe976147ab3de..50eef549cc0be 100644 --- a/include/swift/Runtime/MutexPThread.h +++ b/include/swift/Runtime/MutexPThread.h @@ -26,7 +26,7 @@ typedef pthread_cond_t ConditionHandle; typedef pthread_mutex_t MutexHandle; typedef pthread_rwlock_t ReadWriteLockHandle; -#if defined(__CYGWIN__) || defined(__ANDROID__) || defined(__HAIKU__) || defined(__EMSCRIPTEN__) +#if defined(__CYGWIN__) || defined(__ANDROID__) || defined(__HAIKU__) || defined(__wasi__) // At the moment CYGWIN pthreads implementation doesn't support the use of // constexpr for static allocation versions. The way they define things // results in a reinterpret_cast which violates constexpr. Similarly, Android's diff --git a/stdlib/public/SwiftShims/LibcShims.h b/stdlib/public/SwiftShims/LibcShims.h index e8ac251ed0f5d..1c60a7561c5b8 100644 --- a/stdlib/public/SwiftShims/LibcShims.h +++ b/stdlib/public/SwiftShims/LibcShims.h @@ -43,7 +43,7 @@ typedef __swift_uint32_t __swift_mode_t; typedef __swift_uint16_t __swift_mode_t; #elif defined(_WIN32) typedef __swift_int32_t __swift_mode_t; -#elif defined(__EMSCRIPTEN__) +#elif defined(__wasi__) typedef __swift_uint32_t __swift_mode_t; #else // just guessing typedef __swift_uint16_t __swift_mode_t; @@ -107,7 +107,7 @@ static inline __swift_size_t _swift_stdlib_malloc_size(const void *ptr) { return malloc_size(ptr); } #elif defined(__linux__) || defined(__CYGWIN__) || defined(__ANDROID__) \ - || defined(__HAIKU__) || defined(__FreeBSD__) || defined(__EMSCRIPTEN__) + || defined(__HAIKU__) || defined(__FreeBSD__) || defined(__wasi__) static inline __swift_size_t _swift_stdlib_malloc_size(const void *ptr) { #if defined(__ANDROID__) #if !defined(__ANDROID_API__) || __ANDROID_API__ >= 17 diff --git a/stdlib/public/runtime/Errors.cpp b/stdlib/public/runtime/Errors.cpp index e2c0dd4a7487d..48201d54d6a2a 100644 --- a/stdlib/public/runtime/Errors.cpp +++ b/stdlib/public/runtime/Errors.cpp @@ -14,7 +14,7 @@ // //===----------------------------------------------------------------------===// -#if defined(__CYGWIN__) || defined(__HAIKU__) || defined(__EMSCRIPTEN__) +#if defined(__CYGWIN__) || defined(__HAIKU__) || defined(__wasi__) #define SWIFT_SUPPORTS_BACKTRACE_REPORTING 0 #else #define SWIFT_SUPPORTS_BACKTRACE_REPORTING 1 diff --git a/stdlib/public/runtime/Heap.cpp b/stdlib/public/runtime/Heap.cpp index c32fbafdf25ef..a241d153299e3 100644 --- a/stdlib/public/runtime/Heap.cpp +++ b/stdlib/public/runtime/Heap.cpp @@ -42,7 +42,7 @@ using namespace swift; #elif defined(_WIN32) # define MALLOC_ALIGN_MASK 7 -#elif defined(__EMSCRIPTEN__) +#elif defined(__wasi__) // Musl malloc is 4*sizeof(size_t), so 16 bytes on 32-bit? // For some reason the unknown alignment code fails because std::max isn't constexpr? # define MALLOC_ALIGN_MASK 15 diff --git a/stdlib/public/runtime/ThreadLocalStorage.h b/stdlib/public/runtime/ThreadLocalStorage.h index 243b12da5b30a..ebf57f6ceb69a 100644 --- a/stdlib/public/runtime/ThreadLocalStorage.h +++ b/stdlib/public/runtime/ThreadLocalStorage.h @@ -81,7 +81,7 @@ typedef int __swift_thread_key_t; typedef unsigned long __swift_thread_key_t; # elif defined(__HAIKU__) typedef int __swift_thread_key_t; -# elif defined(__EMSCRIPTEN__) +# elif defined(__wasi__) typedef unsigned int __swift_thread_key_t; # else typedef unsigned long __swift_thread_key_t; diff --git a/stdlib/public/stubs/LibcShims.cpp b/stdlib/public/stubs/LibcShims.cpp index 8368698067bd7..b54ede8c61f66 100644 --- a/stdlib/public/stubs/LibcShims.cpp +++ b/stdlib/public/stubs/LibcShims.cpp @@ -23,7 +23,7 @@ #include #include -#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) || defined(__EMSCRIPTEN__) +#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) || defined(__wasi__) #include #endif diff --git a/utils/build-script b/utils/build-script index 2caf618060568..da46d62e0cf4c 100755 --- a/utils/build-script +++ b/utils/build-script @@ -123,14 +123,14 @@ class BuildScriptInvocation(object): "must be specified") if args.wasm: - if args.wasm_emscripten is None or \ + if args.wasm_wasi_sdk is None or \ args.wasm_icu_uc is None or \ args.wasm_icu_uc_include is None or \ args.wasm_icu_i18n is None or \ args.wasm_icu_i18n_include is None or \ args.wasm_icu_data is None: diagnostics.fatal( - "when building for WebAssembly, --wasm-emscripten, " + "when building for WebAssembly, --wasm-wasi-sdk, " "--wasm-icu-uc, " "--wasm-icu-uc-include, --wasm-icu-i18n, " "--wasm-icu-i18n-include, and --wasm-icu-data " @@ -606,7 +606,7 @@ class BuildScriptInvocation(object): if args.wasm: impl_args += [ - "--wasm-emscripten", args.wasm_emscripten, + "--wasm-wasi-sdk", args.wasm_wasi_sdk, "--wasm-icu-uc", args.wasm_icu_uc, "--wasm-icu-uc-include", args.wasm_icu_uc_include, "--wasm-icu-i18n", args.wasm_icu_i18n, diff --git a/utils/build-script-impl b/utils/build-script-impl index 37e8c6a1a8a69..09e21458b9da8 100755 --- a/utils/build-script-impl +++ b/utils/build-script-impl @@ -1589,7 +1589,7 @@ for host in "${ALL_HOSTS[@]}"; do if [[ ! "${SKIP_BUILD_WASM}" ]]; then cmake_options=( "${cmake_options[@]}" - -DSWIFT_WASM_EMSCRIPTEN_PATH:STRING="${WASM_EMSCRIPTEN}" + -DSWIFT_WASM_WASI_SDK_PATH:STRING="${WASM_WASI_SDK}" -DSWIFT_WASM_wasm32_ICU_UC:STRING="${WASM_ICU_UC}" -DSWIFT_WASM_wasm32_ICU_UC_INCLUDE:STRING="${WASM_ICU_UC_INCLUDE}" -DSWIFT_WASM_wasm32_ICU_I18N:STRING="${WASM_ICU_I18N}" diff --git a/utils/build_swift/build_swift/driver_arguments.py b/utils/build_swift/build_swift/driver_arguments.py index 9a8a4820117bc..2af4b117319bc 100644 --- a/utils/build_swift/build_swift/driver_arguments.py +++ b/utils/build_swift/build_swift/driver_arguments.py @@ -1086,8 +1086,8 @@ def create_argument_parser(): in_group('Build settings for Android') - option('--wasm-emscripten', store_path, - help='An absolute path to Emscripten that will be used as a libc ' + option('--wasm-wasi-sdk', store_path, + help='An absolute path to WASI SDK that will be used as a libc ' 'implementation for Wasm builds') option('--wasm-icu-uc', store_path, diff --git a/vvv.sh b/vvv.sh index bcb75036bf6b1..7fb2c02157ec3 100755 --- a/vvv.sh +++ b/vvv.sh @@ -1,7 +1,7 @@ utils/build-script --release-debuginfo --wasm \ --llvm-targets-to-build "X86;ARM;AArch64;PowerPC;SystemZ;WebAssembly" \ --llvm-max-parallel-lto-link-jobs 1 --swift-tools-max-parallel-lto-link-jobs 1 \ - --wasm-emscripten "/home/zhuowei/Documents/emsdk/emscripten/1.38.30" \ + --wasm-wasi-sdk "/home/zhuowei/Downloads/wasi-sdk-3.0-linux/wasi-sdk-3.0/opt/wasi-sdk" \ --wasm-icu-uc "todo" \ --wasm-icu-uc-include "$PWD/NO" \ --wasm-icu-i18n "todo" \ From 99c0a17c2a63df71d24d779ee2423535632b16da Mon Sep 17 00:00:00 2001 From: Zhuowei Zhang Date: Fri, 12 Apr 2019 22:56:43 -0700 Subject: [PATCH 16/80] fix WASI header includes --- stdlib/public/runtime/Metadata.cpp | 3 +++ stdlib/public/stubs/Stubs.cpp | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/stdlib/public/runtime/Metadata.cpp b/stdlib/public/runtime/Metadata.cpp index 2546d90dec5cd..23b4ecce1e370 100644 --- a/stdlib/public/runtime/Metadata.cpp +++ b/stdlib/public/runtime/Metadata.cpp @@ -44,7 +44,10 @@ #else #include #include +// WASI doesn't have dynamic linking yet +#ifndef __wasi__ #include +#endif //__wasi #endif #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/Hashing.h" diff --git a/stdlib/public/stubs/Stubs.cpp b/stdlib/public/stubs/Stubs.cpp index 55bada6a1db92..e2551564f31f9 100644 --- a/stdlib/public/stubs/Stubs.cpp +++ b/stdlib/public/stubs/Stubs.cpp @@ -26,7 +26,7 @@ #define NOMINMAX #include #else -#if !defined(__HAIKU__) +#if !defined(__HAIKU__) && !defined(__wasi__) #include #else #include @@ -67,7 +67,7 @@ static float swift_strtof_l(const char *nptr, char **endptr, locale_t loc) { #define strtod_l swift_strtod_l #define strtof_l swift_strtof_l #endif -#elif defined(__linux__) +#elif defined(__linux__) || defined(__wasi__) #include #else #include From 0dfa4f5941d4dfd9975b387634c75f90617cbbff Mon Sep 17 00:00:00 2001 From: Zhuowei Zhang Date: Sat, 13 Apr 2019 17:41:32 -0700 Subject: [PATCH 17/80] WebAssembly: Disable Comdat for reflection metadata --- include/swift/IRGen/Linking.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/include/swift/IRGen/Linking.h b/include/swift/IRGen/Linking.h index 56b76c07be474..eeaabd8bbcbd9 100644 --- a/include/swift/IRGen/Linking.h +++ b/include/swift/IRGen/Linking.h @@ -1157,6 +1157,11 @@ class ApplyIRLinkage { // TODO: BFD and gold do not handle COMDATs properly if (Triple.isOSBinFormatELF()) return; + // WebAssembly: hack: comdat + custom section = explosion + // the comdat code assumes section name would be unique for each comdat + // this doesn't happen for metadata. + if (Triple.isOSBinFormatWasm()) + return; if (IRL.Linkage == llvm::GlobalValue::LinkOnceODRLinkage || IRL.Linkage == llvm::GlobalValue::WeakODRLinkage) From 5e864fb77ab953b892add9c178e74f0f1bb7f3a3 Mon Sep 17 00:00:00 2001 From: Zhuowei Zhang Date: Sun, 14 Apr 2019 11:52:02 -0700 Subject: [PATCH 18/80] Disable generating PC-relative relocations in constant builder This doesn't take care of all the PC relative relocations in constant builder yet. This still breaks on `((.Lgot.$sSTMp.656-($ss18EnumeratedSequenceVMn))-56)+1` Which sits in rodata. Not sure where that's generated yet --- lib/IRGen/ConstantBuilder.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/lib/IRGen/ConstantBuilder.h b/lib/IRGen/ConstantBuilder.h index 3bb9e7d1fb338..aca1fda1be3d0 100644 --- a/lib/IRGen/ConstantBuilder.h +++ b/lib/IRGen/ConstantBuilder.h @@ -82,6 +82,11 @@ class ConstantAggregateBuilderBase void addRelativeAddress(llvm::Constant *target) { assert(!isa(target)); + if (IGM().TargetInfo.OutputObjectFormat == llvm::Triple::Wasm) { + // WebAssembly: hack: doesn't support PCrel data relocations + add(llvm::ConstantExpr::getPtrToInt(target, IGM().RelativeAddressTy, false)); + return; + } addRelativeOffset(IGM().RelativeAddressTy, target); } @@ -90,6 +95,13 @@ class ConstantAggregateBuilderBase /// a "GOT-equivalent", i.e. a pointer to an external object; if so, /// set the low bit of the offset to indicate that this is true. void addRelativeAddress(ConstantReference reference) { + if (IGM().TargetInfo.OutputObjectFormat == llvm::Triple::Wasm) { + // WebAssembly: hack: doesn't support PCrel data relocations + // also, we should set the lowest bit, but I don't know how to do that + // there's no GOT on WebAssembly anyways though + add(llvm::ConstantExpr::getPtrToInt(reference.getValue(), IGM().RelativeAddressTy, false)); + return; + } addTaggedRelativeOffset(IGM().RelativeAddressTy, reference.getValue(), unsigned(reference.isIndirect())); @@ -99,6 +111,11 @@ class ConstantAggregateBuilderBase /// The target must be a "GOT-equivalent", i.e. a pointer to an /// external object. void addIndirectRelativeAddress(ConstantReference reference) { + if (IGM().TargetInfo.OutputObjectFormat == llvm::Triple::Wasm) { + // WebAssembly: hack: doesn't support PCrel data relocations + add(llvm::ConstantExpr::getPtrToInt(reference.getValue(), IGM().RelativeAddressTy, false)); + return; + } assert(reference.isIndirect()); addRelativeOffset(IGM().RelativeAddressTy, reference.getValue()); From f7bb4f57f0c0d2a53e80c94b235bbc70765fdb03 Mon Sep 17 00:00:00 2001 From: Zhuowei Zhang Date: Sun, 14 Apr 2019 15:52:09 -0700 Subject: [PATCH 19/80] WebAssembly: remove even more PCRel relocations This removes the PCRel reloc in \01l_type_metadata_table but nowhere else --- lib/IRGen/GenDecl.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/IRGen/GenDecl.cpp b/lib/IRGen/GenDecl.cpp index 440963c22d129..6cffbd53e5d2b 100644 --- a/lib/IRGen/GenDecl.cpp +++ b/lib/IRGen/GenDecl.cpp @@ -3071,6 +3071,11 @@ IRGenModule::emitDirectRelativeReference(llvm::Constant *target, // Convert the target to an integer. auto targetAddr = llvm::ConstantExpr::getPtrToInt(target, SizeTy); + // WebAssembly hack: WebAssembly doesn't support PC-relative references + if (TargetInfo.OutputObjectFormat == llvm::Triple::Wasm) { + return targetAddr; + } + SmallVector indices; indices.push_back(llvm::ConstantInt::get(Int32Ty, 0)); for (unsigned baseIndex : baseIndices) { From 31edacc487beedb898d6f81698d67e93d2e9ed67 Mon Sep 17 00:00:00 2001 From: Zhuowei Zhang Date: Sun, 14 Apr 2019 17:05:57 -0700 Subject: [PATCH 20/80] WebAssembly: remove more PCRel relocations This at least seems to get rid of all the PCRel relocations emitted. Now it crashes in: (($sSTMp)+48)-8 The expr type is 0 expression kind not supported yay?! --- lib/IRGen/GenMeta.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/IRGen/GenMeta.cpp b/lib/IRGen/GenMeta.cpp index 392b6c4159f10..c2c687c52b6bb 100644 --- a/lib/IRGen/GenMeta.cpp +++ b/lib/IRGen/GenMeta.cpp @@ -4516,6 +4516,12 @@ GenericRequirementsMetadata irgen::addGenericRequirements( unsigned tag = unsigned(descriptorRef.isIndirect()); if (protocol->isObjC()) tag |= 0x02; + // WebAssembly: hack: Wasm doesn't support PC-relative offsets. + // also doesn't handle tag yet + if (IGM.TargetInfo.OutputObjectFormat == llvm::Triple::Wasm) { + B.add(llvm::ConstantExpr::getPtrToInt(descriptorRef.getValue(), IGM.RelativeAddressTy, false)); + return; + } B.addTaggedRelativeOffset(IGM.RelativeAddressTy, descriptorRef.getValue(), From 85c1cdc2655e64ff2f7a28a553912c84b35b7c80 Mon Sep 17 00:00:00 2001 From: Zhuowei Zhang Date: Sun, 14 Apr 2019 23:38:32 -0700 Subject: [PATCH 21/80] WebAssembly: disable relative pointers in the runtime This is completely untested. --- include/swift/Basic/RelativePointer.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/include/swift/Basic/RelativePointer.h b/include/swift/Basic/RelativePointer.h index f5cffd1c33c25..31cfd2971ece2 100644 --- a/include/swift/Basic/RelativePointer.h +++ b/include/swift/Basic/RelativePointer.h @@ -146,6 +146,11 @@ static inline uintptr_t applyRelativeOffset(BasePtrTy *basePtr, Offset offset) { std::is_signed::value, "offset type should be signed integer"); +#ifdef __wasm__ + // WebAssembly: hack: disable relative pointers + return (uintptr_t)(intptr_t)offset; +#endif + auto base = reinterpret_cast(basePtr); // We want to do wrapping arithmetic, but with a sign-extended // offset. To do this in C, we need to do signed promotion to get @@ -164,6 +169,13 @@ static inline Offset measureRelativeOffset(A *referent, B *base) { static_assert(std::is_integral::value && std::is_signed::value, "offset type should be signed integer"); +#ifdef __wasm__ + // WebAssembly: hack: disable relative pointers + auto offset = (Offset)(uintptr_t)referent; + assert((intptr_t)offset == (intptr_t)(uintptr_t)referent + && "pointer too large to fit in offset type"); + return offset; +#endif auto distance = (uintptr_t)referent - (uintptr_t)base; // Truncate as unsigned, then wrap around to signed. From 2dfc622affefb49e1f5e5a268e361a9ad8fa87e4 Mon Sep 17 00:00:00 2001 From: Zhuowei Zhang Date: Mon, 15 Apr 2019 20:23:25 -0700 Subject: [PATCH 22/80] Start exporting WASI musl in Glibc modulemap --- .../SwiftPrivateLibcExtras/Subprocess.swift | 2 +- stdlib/public/Platform/CMakeLists.txt | 5 +++-- stdlib/public/Platform/Platform.swift | 4 +++- stdlib/public/Platform/glibc.modulemap.gyb | 18 +++++++++++++++++- 4 files changed, 24 insertions(+), 5 deletions(-) diff --git a/stdlib/private/SwiftPrivateLibcExtras/Subprocess.swift b/stdlib/private/SwiftPrivateLibcExtras/Subprocess.swift index e95e07e142c3c..510ac1e9d8dc3 100644 --- a/stdlib/private/SwiftPrivateLibcExtras/Subprocess.swift +++ b/stdlib/private/SwiftPrivateLibcExtras/Subprocess.swift @@ -13,7 +13,7 @@ import SwiftPrivate #if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) import Darwin -#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku) +#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku) || os(Wasm) import Glibc #elseif os(Windows) import MSVCRT diff --git a/stdlib/public/Platform/CMakeLists.txt b/stdlib/public/Platform/CMakeLists.txt index 841dc6c602d2d..079e9cc37d6d2 100644 --- a/stdlib/public/Platform/CMakeLists.txt +++ b/stdlib/public/Platform/CMakeLists.txt @@ -44,7 +44,7 @@ add_swift_target_library(swiftGlibc ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_O ${SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS} ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS} LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}" - TARGET_SDKS ANDROID CYGWIN FREEBSD LINUX HAIKU + TARGET_SDKS ANDROID CYGWIN FREEBSD LINUX HAIKU WASM INSTALL_IN_COMPONENT sdk-overlay DEPENDS glibc_modulemap) @@ -70,7 +70,8 @@ foreach(sdk ${SWIFT_SDKS}) NOT "${sdk}" STREQUAL "FREEBSD" AND NOT "${sdk}" STREQUAL "ANDROID" AND NOT "${sdk}" STREQUAL "CYGWIN" AND - NOT "${sdk}" STREQUAL "HAIKU") + NOT "${sdk}" STREQUAL "HAIKU" AND + NOT "${sdk}" STREQUAL "WASM") continue() endif() diff --git a/stdlib/public/Platform/Platform.swift b/stdlib/public/Platform/Platform.swift index 347931eb3b2fe..17c60c276036b 100644 --- a/stdlib/public/Platform/Platform.swift +++ b/stdlib/public/Platform/Platform.swift @@ -332,7 +332,7 @@ public var SIG_DFL: sig_t? { return nil } public var SIG_IGN: sig_t { return unsafeBitCast(1, to: sig_t.self) } public var SIG_ERR: sig_t { return unsafeBitCast(-1, to: sig_t.self) } public var SIG_HOLD: sig_t { return unsafeBitCast(5, to: sig_t.self) } -#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Haiku) || os(Wasm) +#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Haiku) public typealias sighandler_t = __sighandler_t public var SIG_DFL: sighandler_t? { return nil } @@ -366,6 +366,8 @@ public var SIG_IGN: _crt_signal_t { public var SIG_ERR: _crt_signal_t { return unsafeBitCast(-1, to: _crt_signal_t.self) } +#elseif os(Wasm) +// WebAssembly/WASI doesn't have signals. #else internal var _ignore = _UnsupportedPlatformError() #endif diff --git a/stdlib/public/Platform/glibc.modulemap.gyb b/stdlib/public/Platform/glibc.modulemap.gyb index 12de1073b6317..97c52fe1ce4fa 100644 --- a/stdlib/public/Platform/glibc.modulemap.gyb +++ b/stdlib/public/Platform/glibc.modulemap.gyb @@ -126,10 +126,12 @@ module SwiftGlibc [system] { header "${GLIBC_INCLUDE_PATH}/math.h" export * } +% if CMAKE_SDK != "WASM": module setjmp { header "${GLIBC_INCLUDE_PATH}/setjmp.h" export * } +% end module signal { header "${GLIBC_INCLUDE_PATH}/signal.h" export * @@ -319,6 +321,7 @@ module SwiftGlibc [system] { header "${GLIBC_INCLUDE_PATH}/dirent.h" export * } +% if CMAKE_SDK != "WASM": module dl { header "${GLIBC_INCLUDE_PATH}/link.h" export * @@ -327,6 +330,7 @@ module SwiftGlibc [system] { header "${GLIBC_INCLUDE_PATH}/dlfcn.h" export * } +% end module fcntl { header "${GLIBC_INCLUDE_PATH}/fcntl.h" export * @@ -335,10 +339,12 @@ module SwiftGlibc [system] { header "${GLIBC_INCLUDE_PATH}/fnmatch.h" export * } +% if CMAKE_SDK != "WASM": module grp { header "${GLIBC_INCLUDE_PATH}/grp.h" export * } +% end module ioctl { header "${GLIBC_ARCH_INCLUDE_PATH}/sys/ioctl.h" export * @@ -373,10 +379,12 @@ module SwiftGlibc [system] { header "${GLIBC_INCLUDE_PATH}/pthread.h" export * } +% if CMAKE_SDK != "WASM": module pwd { header "${GLIBC_INCLUDE_PATH}/pwd.h" export * } +% end module regex { header "${GLIBC_INCLUDE_PATH}/regex.h" export * @@ -422,18 +430,22 @@ module SwiftGlibc [system] { } % end +% if CMAKE_SDK != "WASM": module ipc { header "${GLIBC_ARCH_INCLUDE_PATH}/sys/ipc.h" export * } +% end module mman { header "${GLIBC_ARCH_INCLUDE_PATH}/sys/mman.h" export * } +% if CMAKE_SDK != "WASM": module msg { header "${GLIBC_ARCH_INCLUDE_PATH}/sys/msg.h" export * } +% end module resource { header "${GLIBC_ARCH_INCLUDE_PATH}/sys/resource.h" export * @@ -442,7 +454,7 @@ module SwiftGlibc [system] { header "${GLIBC_ARCH_INCLUDE_PATH}/sys/select.h" export * } -% if CMAKE_SDK != "FREEBSD" and CMAKE_SDK != "HAIKU": +% if CMAKE_SDK != "FREEBSD" and CMAKE_SDK != "HAIKU" and CMAKE_SDK != "WASM": module sendfile { header "${GLIBC_ARCH_INCLUDE_PATH}/sys/sendfile.h" export * @@ -492,10 +504,12 @@ module SwiftGlibc [system] { header "${GLIBC_ARCH_INCLUDE_PATH}/sys/utsname.h" export * } +% if CMAKE_SDK != "WASM": module wait { header "${GLIBC_ARCH_INCLUDE_PATH}/sys/wait.h" export * } +% end } % if CMAKE_SDK in ["LINUX", "FREEBSD"]: module sysexits { @@ -518,8 +532,10 @@ module SwiftGlibc [system] { } } +% if CMAKE_SDK != "WASM": module CUUID [system] { header "${GLIBC_INCLUDE_PATH}/uuid/uuid.h" link "uuid" export * } +% end From 3fc86e10de79d5f1b0ee1af5767b7923d8dc4668 Mon Sep 17 00:00:00 2001 From: Zhuowei Zhang Date: Tue, 16 Apr 2019 00:06:37 -0700 Subject: [PATCH 23/80] WebAssembly: disable subprocesses --- .../private/SwiftPrivateLibcExtras/CMakeLists.txt | 1 + stdlib/private/SwiftPrivateLibcExtras/Subprocess.c | 2 +- .../private/SwiftPrivateLibcExtras/Subprocess.swift | 13 +++++++++++++ .../SwiftPrivateLibcExtras.swift | 4 +++- .../private/SwiftPrivateThreadExtras/CMakeLists.txt | 1 + .../SwiftPrivateThreadExtras.swift | 2 +- .../SwiftPrivateThreadExtras/ThreadBarriers.swift | 2 +- 7 files changed, 21 insertions(+), 4 deletions(-) diff --git a/stdlib/private/SwiftPrivateLibcExtras/CMakeLists.txt b/stdlib/private/SwiftPrivateLibcExtras/CMakeLists.txt index 22979ebc7ce8b..86ad055eb4f0b 100644 --- a/stdlib/private/SwiftPrivateLibcExtras/CMakeLists.txt +++ b/stdlib/private/SwiftPrivateLibcExtras/CMakeLists.txt @@ -17,6 +17,7 @@ add_swift_target_library(swiftSwiftPrivateLibcExtras ${SWIFT_STDLIB_LIBRARY_BUIL SWIFT_MODULE_DEPENDS_FREEBSD Glibc SWIFT_MODULE_DEPENDS_CYGWIN Glibc SWIFT_MODULE_DEPENDS_HAIKU Glibc + SWIFT_MODULE_DEPENDS_WASM Glibc SWIFT_MODULE_DEPENDS_WINDOWS MSVCRT WinSDK INSTALL_IN_COMPONENT stdlib-experimental DARWIN_INSTALL_NAME_DIR "${SWIFT_DARWIN_STDLIB_PRIVATE_INSTALL_NAME_DIR}") diff --git a/stdlib/private/SwiftPrivateLibcExtras/Subprocess.c b/stdlib/private/SwiftPrivateLibcExtras/Subprocess.c index 4ef6fdbff8537..26d2402db4626 100644 --- a/stdlib/private/SwiftPrivateLibcExtras/Subprocess.c +++ b/stdlib/private/SwiftPrivateLibcExtras/Subprocess.c @@ -11,7 +11,7 @@ //===----------------------------------------------------------------------===// // posix_spawn is not available on Android or Windows (MSVC). -#if !defined(__ANDROID__) && !defined(__HAIKU__) && (!defined(_WIN32) || defined(__CYGWIN__)) +#if !defined(__ANDROID__) && !defined(__HAIKU__) && (!defined(_WIN32) || defined(__CYGWIN__)) && !defined(__wasi__) #include "swift/Runtime/Config.h" diff --git a/stdlib/private/SwiftPrivateLibcExtras/Subprocess.swift b/stdlib/private/SwiftPrivateLibcExtras/Subprocess.swift index 510ac1e9d8dc3..f8ec3333de45d 100644 --- a/stdlib/private/SwiftPrivateLibcExtras/Subprocess.swift +++ b/stdlib/private/SwiftPrivateLibcExtras/Subprocess.swift @@ -21,6 +21,9 @@ import WinSDK #endif internal func _signalToString(_ signal: Int) -> String { +#if os(Wasm) + return "unsupported" +#else switch CInt(signal) { case SIGILL: return "SIGILL" case SIGABRT: return "SIGABRT" @@ -33,6 +36,7 @@ internal func _signalToString(_ signal: Int) -> String { #endif default: return "SIG???? (\(signal))" } +#endif // os(Wasm) } public enum ProcessTerminationStatus : CustomStringConvertible { @@ -141,6 +145,15 @@ public func waitProcess(_ process: HANDLE) -> ProcessTerminationStatus { } return .exit(Int(status)) } +#elseif os(Wasm) +// Oops, we can't launch tests in subprocesses yet! +public func spawnChild(_ args: [String]) + -> (pid: pid_t, stdinFD: CInt, stdoutFD: CInt, stderrFD: CInt) { + fatalError("Not supported on WebAssembly!") +} +public func posixWaitpid(_ pid: pid_t) -> ProcessTerminationStatus { + fatalError("Not supported on WebAssembly!") +} #else // posix_spawn is not available on Android. // posix_spawn is not available on Haiku. diff --git a/stdlib/private/SwiftPrivateLibcExtras/SwiftPrivateLibcExtras.swift b/stdlib/private/SwiftPrivateLibcExtras/SwiftPrivateLibcExtras.swift index 6fa2c06b6f6ee..8c09674b4faae 100644 --- a/stdlib/private/SwiftPrivateLibcExtras/SwiftPrivateLibcExtras.swift +++ b/stdlib/private/SwiftPrivateLibcExtras/SwiftPrivateLibcExtras.swift @@ -13,7 +13,7 @@ import SwiftPrivate #if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) import Darwin -#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku) +#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku) || os(Wasm) import Glibc #elseif os(Windows) import MSVCRT @@ -125,6 +125,8 @@ public func _stdlib_pipe() -> (readEnd: CInt, writeEnd: CInt, error: CInt) { let ret = fds.withUnsafeMutableBufferPointer { unsafeFds -> CInt in #if os(Windows) return _pipe(unsafeFds.baseAddress, 0, 0) +#elseif os(Wasm) + fatalError("no pipes on WebAssembly") #else return pipe(unsafeFds.baseAddress) #endif diff --git a/stdlib/private/SwiftPrivateThreadExtras/CMakeLists.txt b/stdlib/private/SwiftPrivateThreadExtras/CMakeLists.txt index 97e2cc0d2af5b..b6d794dfc051d 100644 --- a/stdlib/private/SwiftPrivateThreadExtras/CMakeLists.txt +++ b/stdlib/private/SwiftPrivateThreadExtras/CMakeLists.txt @@ -14,6 +14,7 @@ add_swift_target_library(swiftSwiftPrivateThreadExtras ${SWIFT_STDLIB_LIBRARY_BU SWIFT_MODULE_DEPENDS_FREEBSD Glibc SWIFT_MODULE_DEPENDS_CYGWIN Glibc SWIFT_MODULE_DEPENDS_HAIKU Glibc + SWIFT_MODULE_DEPENDS_WASM Glibc SWIFT_MODULE_DEPENDS_WINDOWS MSVCRT WinSDK SWIFT_COMPILE_FLAGS ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS} INSTALL_IN_COMPONENT stdlib-experimental diff --git a/stdlib/private/SwiftPrivateThreadExtras/SwiftPrivateThreadExtras.swift b/stdlib/private/SwiftPrivateThreadExtras/SwiftPrivateThreadExtras.swift index 002aef8e74145..feff077024252 100644 --- a/stdlib/private/SwiftPrivateThreadExtras/SwiftPrivateThreadExtras.swift +++ b/stdlib/private/SwiftPrivateThreadExtras/SwiftPrivateThreadExtras.swift @@ -17,7 +17,7 @@ #if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) import Darwin -#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku) +#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku) || os(Wasm) import Glibc #elseif os(Windows) import MSVCRT diff --git a/stdlib/private/SwiftPrivateThreadExtras/ThreadBarriers.swift b/stdlib/private/SwiftPrivateThreadExtras/ThreadBarriers.swift index 9f5e183447021..68a7880d18a07 100644 --- a/stdlib/private/SwiftPrivateThreadExtras/ThreadBarriers.swift +++ b/stdlib/private/SwiftPrivateThreadExtras/ThreadBarriers.swift @@ -12,7 +12,7 @@ #if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) import Darwin -#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku) +#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku) || os(Wasm) import Glibc #elseif os(Windows) import MSVCRT From 80342ee1b2c3def9cf207bc2d17c1cde7ef86503 Mon Sep 17 00:00:00 2001 From: Zhuowei Zhang Date: Tue, 16 Apr 2019 22:42:40 -0700 Subject: [PATCH 24/80] WebAssembly: add EINVAL constant manually Swift's importer won't import error constants from WASI, because the constants are wrapped in UINT16_C(), which Swift doesn't support. --- stdlib/public/Platform/Glibc.swift.gyb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/stdlib/public/Platform/Glibc.swift.gyb b/stdlib/public/Platform/Glibc.swift.gyb index 2de990adfffaf..b8376b159138e 100644 --- a/stdlib/public/Platform/Glibc.swift.gyb +++ b/stdlib/public/Platform/Glibc.swift.gyb @@ -69,3 +69,8 @@ public let ${prefix}_TRUE_MIN = ${type}.leastNonzeroMagnitude #endif % end %end + +#if os(Wasm) +// WebAssembly's error.h uses a macro that Swift can't import. +public let EINVAL:Int32 = 28 +#endif From 5e46b85c7c012103d7dce794419d670a6f1ef07b Mon Sep 17 00:00:00 2001 From: Zhuowei Zhang Date: Tue, 16 Apr 2019 23:39:43 -0700 Subject: [PATCH 25/80] WebAssembly: add more wasm Glibc target dependencies to cmakefiles --- stdlib/private/StdlibUnittest/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/stdlib/private/StdlibUnittest/CMakeLists.txt b/stdlib/private/StdlibUnittest/CMakeLists.txt index 710ccede83171..5f6cc350fbe3a 100644 --- a/stdlib/private/StdlibUnittest/CMakeLists.txt +++ b/stdlib/private/StdlibUnittest/CMakeLists.txt @@ -40,6 +40,7 @@ add_swift_target_library(swiftStdlibUnittest ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} SWIFT_MODULE_DEPENDS_FREEBSD Glibc SWIFT_MODULE_DEPENDS_CYGWIN Glibc SWIFT_MODULE_DEPENDS_HAIKU Glibc + SWIFT_MODULE_DEPENDS_WASM Glibc SWIFT_MODULE_DEPENDS_WINDOWS MSVCRT WinSDK SWIFT_COMPILE_FLAGS ${swift_stdlib_unittest_compile_flags} ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS} INSTALL_IN_COMPONENT stdlib-experimental From b1210acfa83d342d4d71e324d0d83858a2a48886 Mon Sep 17 00:00:00 2001 From: Zhuowei Zhang Date: Tue, 16 Apr 2019 23:41:39 -0700 Subject: [PATCH 26/80] WebAssembly: hack: Disable tests for now --- CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 544c6dc4aabd3..e068cc12d3c8d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1121,8 +1121,8 @@ if ("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin") endif() if(SWIFT_INCLUDE_TESTS) - add_subdirectory(test) - add_subdirectory(unittests) + #add_subdirectory(test) + #add_subdirectory(unittests) endif() if(SWIFT_INCLUDE_DOCS) add_subdirectory(docs) From e7b56e9a7adacd44979b5caddeec77a4feb32e54 Mon Sep 17 00:00:00 2001 From: Zhuowei Zhang Date: Wed, 17 Apr 2019 00:14:16 -0700 Subject: [PATCH 27/80] WebAssembly: add more ifdefs for WebAssembly --- include/swift/SwiftRemoteMirror/Platform.h | 4 ++-- stdlib/private/StdlibUnittest/InterceptTraps.cpp | 11 ++++++++++- stdlib/private/StdlibUnittest/RaceTest.swift | 7 ++++++- stdlib/private/StdlibUnittest/StdlibCoreExtras.swift | 2 +- stdlib/private/StdlibUnittest/StdlibUnittest.swift | 9 ++++++++- stdlib/public/Platform/Glibc.swift.gyb | 1 + .../swift-reflection-test/swift-reflection-test.c | 2 +- 7 files changed, 29 insertions(+), 7 deletions(-) diff --git a/include/swift/SwiftRemoteMirror/Platform.h b/include/swift/SwiftRemoteMirror/Platform.h index cefdea2c4de6f..1ec61dde9cad9 100644 --- a/include/swift/SwiftRemoteMirror/Platform.h +++ b/include/swift/SwiftRemoteMirror/Platform.h @@ -18,7 +18,7 @@ extern "C" { #endif #if defined(swiftRemoteMirror_EXPORTS) -# if defined(__ELF__) +# if defined(__ELF__) || defined(__wasm__) # define SWIFT_REMOTE_MIRROR_LINKAGE __attribute__((__visibility__("protected"))) # elif defined(__MACH__) # define SWIFT_REMOTE_MIRROR_LINKAGE __attribute__((__visibility__("default"))) @@ -30,7 +30,7 @@ extern "C" { # endif # endif #else -# if defined(__ELF__) +# if defined(__ELF__) || defined(__wasm__) # define SWIFT_REMOTE_MIRROR_LINKAGE __attribute__((__visibility__("default"))) # elif defined(__MACH__) # define SWIFT_REMOTE_MIRROR_LINKAGE __attribute__((__visibility__("default"))) diff --git a/stdlib/private/StdlibUnittest/InterceptTraps.cpp b/stdlib/private/StdlibUnittest/InterceptTraps.cpp index 377397bebb9bf..2e76ac6e84aea 100644 --- a/stdlib/private/StdlibUnittest/InterceptTraps.cpp +++ b/stdlib/private/StdlibUnittest/InterceptTraps.cpp @@ -26,6 +26,9 @@ #include "swift/Runtime/Config.h" +// WebAssembly: no signals on WASI yet +#ifndef __wasi__ + static void CrashCatcher(int Sig) { const char *Msg; switch (Sig) { @@ -65,11 +68,16 @@ VectoredCrashHandler(PEXCEPTION_POINTERS ExceptionInfo) { } #endif +#endif // __wasi__ + SWIFT_CC(swift) SWIFT_RUNTIME_LIBRARY_VISIBILITY extern "C" void installTrapInterceptor() { // Disable buffering on stdout so that everything is printed before crashing. setbuf(stdout, 0); +// WebAssembly: no signals on WASI +#ifndef __wasi__ + #if defined(_WIN32) _set_abort_behavior(0, _WRITE_ABORT_MSG); #endif @@ -85,5 +93,6 @@ void installTrapInterceptor() { signal(SIGBUS, CrashCatcher); signal(SIGSYS, CrashCatcher); #endif -} +#endif // __wasi__ +} diff --git a/stdlib/private/StdlibUnittest/RaceTest.swift b/stdlib/private/StdlibUnittest/RaceTest.swift index 3bdee4f03d32f..60ba5e368c083 100644 --- a/stdlib/private/StdlibUnittest/RaceTest.swift +++ b/stdlib/private/StdlibUnittest/RaceTest.swift @@ -41,7 +41,7 @@ import SwiftPrivateLibcExtras import SwiftPrivateThreadExtras #if os(macOS) || os(iOS) import Darwin -#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku) +#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku) || os(Wasm) import Glibc #elseif os(Windows) import MSVCRT @@ -562,7 +562,12 @@ class _InterruptibleSleep { return } +#if os(Wasm) +// WebAssembly/WASI on wasm32 is the only 32-bit platform with Int64 time_t + var timeout = timeval(tv_sec: time_t(duration), tv_usec: 0) +#else var timeout = timeval(tv_sec: duration, tv_usec: 0) +#endif var readFDs = _stdlib_fd_set() var writeFDs = _stdlib_fd_set() diff --git a/stdlib/private/StdlibUnittest/StdlibCoreExtras.swift b/stdlib/private/StdlibUnittest/StdlibCoreExtras.swift index 20bb2b2c9376f..8b356c2acff2a 100644 --- a/stdlib/private/StdlibUnittest/StdlibCoreExtras.swift +++ b/stdlib/private/StdlibUnittest/StdlibCoreExtras.swift @@ -14,7 +14,7 @@ import SwiftPrivate import SwiftPrivateLibcExtras #if os(macOS) || os(iOS) import Darwin -#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku) +#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku) || os(Wasm) import Glibc #elseif os(Windows) import MSVCRT diff --git a/stdlib/private/StdlibUnittest/StdlibUnittest.swift b/stdlib/private/StdlibUnittest/StdlibUnittest.swift index c67f1778d8a3b..03e0ca2c9cbec 100644 --- a/stdlib/private/StdlibUnittest/StdlibUnittest.swift +++ b/stdlib/private/StdlibUnittest/StdlibUnittest.swift @@ -18,7 +18,7 @@ import SwiftPrivateLibcExtras #if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) import Foundation import Darwin -#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku) +#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku) || os(Wasm) import Glibc #elseif os(Windows) import MSVCRT @@ -748,6 +748,8 @@ extension ProcessTerminationStatus { case .signal(let signal): #if os(Windows) return CInt(signal) == SIGILL +#elseif os(Wasm) + return false #else return CInt(signal) == SIGILL || CInt(signal) == SIGTRAP #endif @@ -1746,6 +1748,7 @@ public enum OSVersion : CustomStringConvertible { case windowsCygnus case windows case haiku + case wasm public var description: String { switch self { @@ -1777,6 +1780,8 @@ public enum OSVersion : CustomStringConvertible { return "Windows" case .haiku: return "Haiku" + case .wasm: + return "Wasm" } } } @@ -1821,6 +1826,8 @@ func _getOSVersion() -> OSVersion { return .windows #elseif os(Haiku) return .haiku +#elseif os(Wasm) + return .wasm #else let productVersion = _getSystemVersionPlistProperty("ProductVersion")! let (major, minor, bugFix) = _parseDottedVersionTriple(productVersion) diff --git a/stdlib/public/Platform/Glibc.swift.gyb b/stdlib/public/Platform/Glibc.swift.gyb index b8376b159138e..3258bcd28b10c 100644 --- a/stdlib/public/Platform/Glibc.swift.gyb +++ b/stdlib/public/Platform/Glibc.swift.gyb @@ -72,5 +72,6 @@ public let ${prefix}_TRUE_MIN = ${type}.leastNonzeroMagnitude #if os(Wasm) // WebAssembly's error.h uses a macro that Swift can't import. +public let EINTR:Int32 = 27 public let EINVAL:Int32 = 28 #endif diff --git a/stdlib/tools/swift-reflection-test/swift-reflection-test.c b/stdlib/tools/swift-reflection-test/swift-reflection-test.c index dfcfe544c1078..e454e0dcd493b 100644 --- a/stdlib/tools/swift-reflection-test/swift-reflection-test.c +++ b/stdlib/tools/swift-reflection-test/swift-reflection-test.c @@ -27,7 +27,7 @@ #include #include #include -#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) +#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) || defined(__wasi__) #include #elif defined(_WIN32) #include From 8d1f660fad6da25e3a422b2342a2d1b9234d770a Mon Sep 17 00:00:00 2001 From: Zhuowei Zhang Date: Thu, 18 Apr 2019 00:06:09 -0700 Subject: [PATCH 28/80] disable DWARF5 and atomics on WebAssembly LLVM doesn't support generating DWARF5 debugging data for WebAssembly, and support for atomics is currently very limited. Set LLVM target options to avoid generating DWARF5 debug data and lower atomics to regular load/stores when building for WebAssembly. This shouldn't affect existing platforms. --- lib/IRGen/IRGen.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/IRGen/IRGen.cpp b/lib/IRGen/IRGen.cpp index 40ecb7b8379ea..5158a591e836a 100644 --- a/lib/IRGen/IRGen.cpp +++ b/lib/IRGen/IRGen.cpp @@ -171,6 +171,13 @@ swift::getIRTargetOptions(const IRGenOptions &Opts, ASTContext &Ctx) { auto *Clang = static_cast(Ctx.getClangModuleLoader()); clang::TargetOptions &ClangOpts = Clang->getTargetInfo().getTargetOpts(); + + // WebAssembly doesn't support atomics or DWARF5 yet. + if (Clang->getTargetInfo().getTriple().isOSBinFormatWasm()) { + TargetOpts.DebuggerTuning = llvm::DebuggerKind::Default; + TargetOpts.ThreadModel = llvm::ThreadModel::Single; + } + return std::make_tuple(TargetOpts, ClangOpts.CPU, ClangOpts.Features, ClangOpts.Triple); } From d967fde81c57d4114c5b06cc23fdc948c5fb61d1 Mon Sep 17 00:00:00 2001 From: Zhuowei Zhang Date: Thu, 18 Apr 2019 00:10:36 -0700 Subject: [PATCH 29/80] remove the older dwarf5 and atomics patch --- lib/IRGen/IRGen.cpp | 5 ----- 1 file changed, 5 deletions(-) diff --git a/lib/IRGen/IRGen.cpp b/lib/IRGen/IRGen.cpp index 5158a591e836a..3aede529fa774 100644 --- a/lib/IRGen/IRGen.cpp +++ b/lib/IRGen/IRGen.cpp @@ -162,11 +162,6 @@ swift::getIRTargetOptions(const IRGenOptions &Opts, ASTContext &Ctx) { // Explicitly request debugger tuning for LLDB which is the default // on Darwin platforms but not on others. TargetOpts.DebuggerTuning = llvm::DebuggerKind::LLDB; - - // WebAssembly HACK: disable atomics - if (Ctx->LangOpts.Target.isOSBinFormatWasm()) { - TargetOpts.ThreadModel = llvm::ThreadModel::Single; - } TargetOpts.FunctionSections = Opts.FunctionSections; auto *Clang = static_cast(Ctx.getClangModuleLoader()); From 28152154842c076af735ef4452550adb76087803 Mon Sep 17 00:00:00 2001 From: Zhuowei Zhang Date: Fri, 19 Apr 2019 14:33:11 -0700 Subject: [PATCH 30/80] fix wasi sdk path --- vvv.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vvv.sh b/vvv.sh index 7fb2c02157ec3..e53a1d5071727 100755 --- a/vvv.sh +++ b/vvv.sh @@ -1,7 +1,7 @@ utils/build-script --release-debuginfo --wasm \ --llvm-targets-to-build "X86;ARM;AArch64;PowerPC;SystemZ;WebAssembly" \ --llvm-max-parallel-lto-link-jobs 1 --swift-tools-max-parallel-lto-link-jobs 1 \ - --wasm-wasi-sdk "/home/zhuowei/Downloads/wasi-sdk-3.0-linux/wasi-sdk-3.0/opt/wasi-sdk" \ + --wasm-wasi-sdk "/home/zhuowei/wasi-sdk" \ --wasm-icu-uc "todo" \ --wasm-icu-uc-include "$PWD/NO" \ --wasm-icu-i18n "todo" \ From 431f9a28a648f4b3a58d700a0732aaad553f0510 Mon Sep 17 00:00:00 2001 From: Zhuowei Zhang Date: Fri, 19 Apr 2019 16:23:15 -0700 Subject: [PATCH 31/80] WebAssembly: HACK: use llvm-ar from the WASI sdk Ubuntu's GNU ar doesn't work with wasm-ld (doesn't see any files inside the archive) --- CMakeLists.txt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index e068cc12d3c8d..2a1c50508d847 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -36,6 +36,9 @@ ENABLE_LANGUAGE(C) include(SwiftUtils) include(CheckSymbolExists) +# WebAssembly: hack: use llvm-ar for creating static libraries; Ubuntu's GNU ar doesn't work with wasm-ld +set(CMAKE_AR "${SWIFT_WASM_WASI_SDK_PATH}/bin/llvm-ar") + # # User-configurable options that control the inclusion and default build # behavior for components which may not strictly be necessary (tools, examples, From 5e5c60577721a628235aecdcb4d22d785c099e1e Mon Sep 17 00:00:00 2001 From: Zhuowei Zhang Date: Fri, 19 Apr 2019 18:24:32 -0700 Subject: [PATCH 32/80] WebAssembly: remove file locking on stdin; WASI doesn't support file locking --- stdlib/public/stubs/Stubs.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/stdlib/public/stubs/Stubs.cpp b/stdlib/public/stubs/Stubs.cpp index e2551564f31f9..d8416c62f1733 100644 --- a/stdlib/public/stubs/Stubs.cpp +++ b/stdlib/public/stubs/Stubs.cpp @@ -503,6 +503,8 @@ const char *swift::_swift_stdlib_strtof_clocale( void swift::_swift_stdlib_flockfile_stdout() { #if defined(_WIN32) _lock_file(stdout); +#elif defined(__wasi__) + // WebAssembly/WASI doesn't support file locking yet #else flockfile(stdout); #endif @@ -511,6 +513,8 @@ void swift::_swift_stdlib_flockfile_stdout() { void swift::_swift_stdlib_funlockfile_stdout() { #if defined(_WIN32) _unlock_file(stdout); +#elif defined(__wasi__) + // WebAssembly/WASI doesn't support file locking yet #else funlockfile(stdout); #endif From 37ed5e57d8a306205eaee4077cf5d8749c975f17 Mon Sep 17 00:00:00 2001 From: Zhuowei Zhang Date: Fri, 19 Apr 2019 18:25:04 -0700 Subject: [PATCH 33/80] WebAssembly: compile ImageInspectionELF for WebAssembly for now --- stdlib/public/runtime/ImageInspectionELF.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/stdlib/public/runtime/ImageInspectionELF.cpp b/stdlib/public/runtime/ImageInspectionELF.cpp index 331ee3614f54f..9c0fe4d949dcf 100644 --- a/stdlib/public/runtime/ImageInspectionELF.cpp +++ b/stdlib/public/runtime/ImageInspectionELF.cpp @@ -18,11 +18,13 @@ /// //===----------------------------------------------------------------------===// -#if defined(__ELF__) +#if defined(__ELF__) || defined(__wasm__) #include "ImageInspection.h" #include "ImageInspectionELF.h" +#ifndef __wasm__ #include +#endif using namespace swift; @@ -132,6 +134,7 @@ void swift_addNewDSOImage(const void *addr) { } int swift::lookupSymbol(const void *address, SymbolInfo *info) { +#ifndef __wasm__ Dl_info dlinfo; if (dladdr(address, &dlinfo) == 0) { return 0; @@ -142,6 +145,9 @@ int swift::lookupSymbol(const void *address, SymbolInfo *info) { info->symbolName.reset(dlinfo.dli_sname); info->symbolAddress = dlinfo.dli_saddr; return 1; +#else + return 0; +#endif } // This is only used for backward deployment hooks, which we currently only support for From fc87f704a124e4fc7bfbb49452c6143a3a35ab47 Mon Sep 17 00:00:00 2001 From: Zhuowei Zhang Date: Fri, 19 Apr 2019 23:29:51 -0700 Subject: [PATCH 34/80] update icu include path --- vvv.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vvv.sh b/vvv.sh index e53a1d5071727..db86f386cd5fa 100755 --- a/vvv.sh +++ b/vvv.sh @@ -3,7 +3,7 @@ utils/build-script --release-debuginfo --wasm \ --llvm-max-parallel-lto-link-jobs 1 --swift-tools-max-parallel-lto-link-jobs 1 \ --wasm-wasi-sdk "/home/zhuowei/wasi-sdk" \ --wasm-icu-uc "todo" \ - --wasm-icu-uc-include "$PWD/NO" \ + --wasm-icu-uc-include "/home/zhuowei/Documents/BuildICU/icu_out/include" \ --wasm-icu-i18n "todo" \ --wasm-icu-i18n-include "todo" \ --wasm-icu-data "todo" \ From 88076d9c0d39dba2f4e17f4f119e4d91f2eaecbb Mon Sep 17 00:00:00 2001 From: Zhuowei Zhang Date: Sat, 20 Apr 2019 01:03:04 -0700 Subject: [PATCH 35/80] WebAssembly: add a sample script for linking a wasm file --- linkPlease.sh | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100755 linkPlease.sh diff --git a/linkPlease.sh b/linkPlease.sh new file mode 100755 index 0000000000000..7316eecbbdb5f --- /dev/null +++ b/linkPlease.sh @@ -0,0 +1,13 @@ +#!/bin/sh +# Copy to ../build/Ninja-RelWithDebInfoAssert/swift-linux-x86_64/bin +exec /home/zhuowei/wasi-sdk/bin/wasm-ld --error-limit=0 -o hello.wasm \ + /home/zhuowei/wasi-sdk/share/sysroot/lib/wasm32-wasi/crt1.o \ + hello.o \ + -L../lib/swift_static/wasm/wasm32 \ + -L../lib/swift/wasm/wasm32 \ + -L/home/zhuowei/wasi-sdk/share/sysroot/lib/wasm32-wasi \ + -L/home/zhuowei/Documents/BuildICU/icu_out/lib \ + -lswiftCore \ + -lc -lc++ -lc++abi -lswiftImageInspectionShared \ + -licuuc -licudata \ + /home/zhuowei/wasi-sdk/lib/clang/8.0.0/lib/wasi/libclang_rt.builtins-wasm32.a /home/zhuowei/Documents/FakePthread/*.o --verbose From e35604e35ccd79b4bea8ea5fa66df3ff615e7e91 Mon Sep 17 00:00:00 2001 From: Zhuowei Zhang Date: Sat, 20 Apr 2019 11:56:10 -0700 Subject: [PATCH 36/80] WebAssembly: use getentropy on Wasi --- stdlib/public/stubs/Random.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/stdlib/public/stubs/Random.cpp b/stdlib/public/stubs/Random.cpp index bb69f7e7793d1..e099d9c5e43ee 100644 --- a/stdlib/public/stubs/Random.cpp +++ b/stdlib/public/stubs/Random.cpp @@ -42,6 +42,10 @@ #include "swift/Runtime/Mutex.h" #include "../SwiftShims/Random.h" +#ifdef __wasi__ +#include // std::min +#endif + #if defined(__APPLE__) SWIFT_RUNTIME_STDLIB_API @@ -88,7 +92,7 @@ void swift::swift_stdlib_random(void *buf, __swift_size_t nbytes) { if (getrandom_available) { actual_nbytes = WHILE_EINTR(syscall(__NR_getrandom, buf, nbytes, 0)); } -#elif __has_include() && (defined(__CYGWIN__) || defined(__Fuchsia__)) +#elif __has_include() && (defined(__CYGWIN__) || defined(__Fuchsia__) || defined(__wasi__)) __swift_size_t getentropy_nbytes = std::min(nbytes, __swift_size_t{256}); if (0 == getentropy(buf, getentropy_nbytes)) { From 0254423bbcb22f5701428fc9b486c4eaa7d0c5bb Mon Sep 17 00:00:00 2001 From: Zhuowei Zhang Date: Sun, 21 Apr 2019 16:30:03 -0700 Subject: [PATCH 37/80] fix path to fakeld --- cmake/modules/AddSwift.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmake/modules/AddSwift.cmake b/cmake/modules/AddSwift.cmake index 80b5dbc8088f9..19301a7966dad 100644 --- a/cmake/modules/AddSwift.cmake +++ b/cmake/modules/AddSwift.cmake @@ -583,7 +583,7 @@ function(_add_variant_link_flags) # FIXME: On Apple platforms, find_program needs to look for "ld64.lld" find_program(LDLLD_PATH "ld.lld") if("${LFLAGS_SDK}" STREQUAL "WASM") - list(APPEND result "-fuse-ld=/home/zhuowei/swift-source/swift/fakeld") + list(APPEND result "-fuse-ld=${CMAKE_SOURCE_DIR}/fakeld") elseif((SWIFT_ENABLE_LLD_LINKER AND LDLLD_PATH AND NOT APPLE) OR ("${LFLAGS_SDK}" STREQUAL "WINDOWS" AND NOT "${CMAKE_SYSTEM_NAME}" STREQUAL "WINDOWS")) From 53d2e9144edb25514ee0208ccdb4680579b19a58 Mon Sep 17 00:00:00 2001 From: Zhuowei Zhang Date: Sun, 21 Apr 2019 17:44:05 -0700 Subject: [PATCH 38/80] WebAssembly: add start/end objects for metadata The WebAssembly linker, unlike ELF linkers, doesn't define __start and __stop symbols for sections. So we have to make our own. If you put an object first in the command line, its symbols are placed first; same for last in command line. So we make two files. This is the same method that Swift used to use on ELF platforms. run ./buildstartend.sh; these two objects must be linked at the start and the end. see the updated linkPlease.sh file. --- buildstartend.sh | 4 ++++ linkPlease.sh | 6 +++++- stdlib/public/runtime/SwiftRT-ELF.cpp | 7 +++++++ swift_end.cpp | 29 +++++++++++++++++++++++++++ swift_start.cpp | 29 +++++++++++++++++++++++++++ 5 files changed, 74 insertions(+), 1 deletion(-) create mode 100755 buildstartend.sh create mode 100644 swift_end.cpp create mode 100644 swift_start.cpp diff --git a/buildstartend.sh b/buildstartend.sh new file mode 100755 index 0000000000000..4739c828f17f9 --- /dev/null +++ b/buildstartend.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +/home/zhuowei/wasi-sdk/bin/clang++ -c swift_start.cpp +/home/zhuowei/wasi-sdk/bin/clang++ -c swift_end.cpp diff --git a/linkPlease.sh b/linkPlease.sh index 7316eecbbdb5f..d3b160ed6bd72 100755 --- a/linkPlease.sh +++ b/linkPlease.sh @@ -2,6 +2,8 @@ # Copy to ../build/Ninja-RelWithDebInfoAssert/swift-linux-x86_64/bin exec /home/zhuowei/wasi-sdk/bin/wasm-ld --error-limit=0 -o hello.wasm \ /home/zhuowei/wasi-sdk/share/sysroot/lib/wasm32-wasi/crt1.o \ + /home/zhuowei/swift-source/swift/swift_start.o \ + ../lib/swift_static/wasm/wasm32/swiftrt.o \ hello.o \ -L../lib/swift_static/wasm/wasm32 \ -L../lib/swift/wasm/wasm32 \ @@ -10,4 +12,6 @@ exec /home/zhuowei/wasi-sdk/bin/wasm-ld --error-limit=0 -o hello.wasm \ -lswiftCore \ -lc -lc++ -lc++abi -lswiftImageInspectionShared \ -licuuc -licudata \ - /home/zhuowei/wasi-sdk/lib/clang/8.0.0/lib/wasi/libclang_rt.builtins-wasm32.a /home/zhuowei/Documents/FakePthread/*.o --verbose + /home/zhuowei/wasi-sdk/lib/clang/8.0.0/lib/wasi/libclang_rt.builtins-wasm32.a /home/zhuowei/Documents/FakePthread/*.o \ + /home/zhuowei/swift-source/swift/swift_end.o \ + --verbose --no-gc-sections diff --git a/stdlib/public/runtime/SwiftRT-ELF.cpp b/stdlib/public/runtime/SwiftRT-ELF.cpp index fa585a8a68241..9b4b8e8dbe2fb 100644 --- a/stdlib/public/runtime/SwiftRT-ELF.cpp +++ b/stdlib/public/runtime/SwiftRT-ELF.cpp @@ -46,9 +46,16 @@ static swift::MetadataSections sections{}; __attribute__((__constructor__)) static void swift_image_constructor() { +#ifndef __wasm__ #define SWIFT_SECTION_RANGE(name) \ { reinterpret_cast(&__start_##name), \ static_cast(&__stop_##name - &__start_##name) } +#else +// WebAssembly hack: ok this should really go in its own file +#define SWIFT_SECTION_RANGE(name) \ + { reinterpret_cast(&__start_##name) + sizeof(void*), \ + static_cast(&__stop_##name - &__start_##name) } +#endif sections = { swift::CurrentSectionMetadataVersion, diff --git a/swift_end.cpp b/swift_end.cpp new file mode 100644 index 0000000000000..714e0d35d7442 --- /dev/null +++ b/swift_end.cpp @@ -0,0 +1,29 @@ +//===--- SwiftRT-ELF.cpp --------------------------------------------------===// +// +// This source file is part of the Swift.org open source project +// +// Copyright (c) 2014 - 2017 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 +// +//===----------------------------------------------------------------------===// + +#include + +// We synthesize the start/stop symbols ourselves. +#define DECLARE_SWIFT_SECTION(name) \ + __attribute__((__section__(#name),__visibility__("hidden"),__aligned__(1))) const void* __stop_##name = (void*)0xfacefeed; \ + +extern "C" { +DECLARE_SWIFT_SECTION(swift5_protocols) +DECLARE_SWIFT_SECTION(swift5_protocol_conformances) +DECLARE_SWIFT_SECTION(swift5_type_metadata) + +DECLARE_SWIFT_SECTION(swift5_typeref) +DECLARE_SWIFT_SECTION(swift5_reflstr) +DECLARE_SWIFT_SECTION(swift5_fieldmd) +DECLARE_SWIFT_SECTION(swift5_assocty) +DECLARE_SWIFT_SECTION(swift5_replace) +} diff --git a/swift_start.cpp b/swift_start.cpp new file mode 100644 index 0000000000000..5a77ea3c0f527 --- /dev/null +++ b/swift_start.cpp @@ -0,0 +1,29 @@ +//===--- SwiftRT-ELF.cpp --------------------------------------------------===// +// +// This source file is part of the Swift.org open source project +// +// Copyright (c) 2014 - 2017 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 +// +//===----------------------------------------------------------------------===// + +#include + +// We synthesize the start/stop symbols ourselves. +#define DECLARE_SWIFT_SECTION(name) \ + __attribute__((__section__(#name),__visibility__("hidden"),__aligned__(1))) const void* __start_##name = (void*)0xdeadbeef; \ + +extern "C" { +DECLARE_SWIFT_SECTION(swift5_protocols) +DECLARE_SWIFT_SECTION(swift5_protocol_conformances) +DECLARE_SWIFT_SECTION(swift5_type_metadata) + +DECLARE_SWIFT_SECTION(swift5_typeref) +DECLARE_SWIFT_SECTION(swift5_reflstr) +DECLARE_SWIFT_SECTION(swift5_fieldmd) +DECLARE_SWIFT_SECTION(swift5_assocty) +DECLARE_SWIFT_SECTION(swift5_replace) +} From 5ce06a349f9d709bcc513e6e3c2bf74706e2d8d8 Mon Sep 17 00:00:00 2001 From: Zhuowei Zhang Date: Tue, 23 Apr 2019 22:33:43 -0700 Subject: [PATCH 39/80] attempt to fix swift_once; doesn't work --- include/swift/Runtime/Once.h | 4 ++++ .../public/runtime/CompatibilityOverride.cpp | 5 +++++ stdlib/public/runtime/HeapObject.cpp | 6 ++++++ stdlib/public/runtime/Once.cpp | 21 +++++++++++++++++++ 4 files changed, 36 insertions(+) diff --git a/include/swift/Runtime/Once.h b/include/swift/Runtime/Once.h index 8a78cddc23c56..95265cfcda794 100644 --- a/include/swift/Runtime/Once.h +++ b/include/swift/Runtime/Once.h @@ -44,6 +44,10 @@ typedef std::once_flag swift_once_t; /// extent of type swift_once_t. SWIFT_RUNTIME_EXPORT void swift_once(swift_once_t *predicate, void (*fn)(void *), void *context); +#ifdef __wasm__ +// WebAssembly: hack +void swift_once_real(swift_once_t *predicate, void (*fn)(void *), void *context); +#endif } diff --git a/stdlib/public/runtime/CompatibilityOverride.cpp b/stdlib/public/runtime/CompatibilityOverride.cpp index 3eebb8581ff24..7723eaad3a439 100644 --- a/stdlib/public/runtime/CompatibilityOverride.cpp +++ b/stdlib/public/runtime/CompatibilityOverride.cpp @@ -49,7 +49,12 @@ static_assert(std::is_pod::value, static OverrideSection *getOverrideSectionPtr() { static OverrideSection *OverrideSectionPtr; static swift_once_t Predicate; + // WebAssembly: hack +#ifdef __wasm__ + swift_once_real(&Predicate, [](void *) { +#else swift_once(&Predicate, [](void *) { +#endif size_t Size; OverrideSectionPtr = static_cast( lookupSection("__DATA", "__swift52_hooks", &Size)); diff --git a/stdlib/public/runtime/HeapObject.cpp b/stdlib/public/runtime/HeapObject.cpp index aa306722f3209..25a171bc7b96b 100644 --- a/stdlib/public/runtime/HeapObject.cpp +++ b/stdlib/public/runtime/HeapObject.cpp @@ -152,7 +152,13 @@ swift::swift_initStaticObject(HeapMetadata const *metadata, // refcount to 1 while another thread already incremented it - and would // decrement it to 0 afterwards. InitStaticObjectContext Ctx = { object, metadata }; +#ifdef __wasm__ + // WebAssembly: hack: swift_once has been modified to take a function pointer without a parameter. + // so use the _real version that does have a parameter + swift_once_real(token, initStaticObjectWithContext, &Ctx); +#else swift_once(token, initStaticObjectWithContext, &Ctx); +#endif return object; } diff --git a/stdlib/public/runtime/Once.cpp b/stdlib/public/runtime/Once.cpp index 57282590cbd06..7554d5d4ae751 100644 --- a/stdlib/public/runtime/Once.cpp +++ b/stdlib/public/runtime/Once.cpp @@ -52,7 +52,28 @@ void swift::swift_once(swift_once_t *predicate, void (*fn)(void *), dispatch_once_f(predicate, context, fn); #elif defined(__CYGWIN__) _swift_once_f(predicate, context, fn); +#elif defined(__wasm__) + // WebAssembly: hack: Swift compiler passes in a fn that doesn't take a parameter, + // which is invalid in WebAssembly. So swift_once casts the function. + // The correct way to fix this is to change + // SILGenModule::emitLazyGlobalInitializer + // but this is OK as a proof of concept. + // Keep a copy of the unmodified swift_once function below. + std::call_once(*predicate, [fn, context]() { ((void (*)())fn)(); }); #else std::call_once(*predicate, [fn, context]() { fn(context); }); #endif } + +#ifdef __wasm__ +void swift::swift_once_real(swift_once_t *predicate, void (*fn)(void *), + void *context) { +#if defined(__APPLE__) + dispatch_once_f(predicate, context, fn); +#elif defined(__CYGWIN__) + _swift_once_f(predicate, context, fn); +#else + std::call_once(*predicate, [fn, context]() { fn(context); }); +#endif +} +#endif From 861684dfd7a723fbdd495ea1c6c490e31fd020e9 Mon Sep 17 00:00:00 2001 From: Zhuowei Zhang Date: Wed, 24 Apr 2019 01:46:13 -0700 Subject: [PATCH 40/80] whoops, missed a swift_once --- stdlib/public/runtime/CompatibilityOverride.h | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/stdlib/public/runtime/CompatibilityOverride.h b/stdlib/public/runtime/CompatibilityOverride.h index abc2eea227424..ad79a27273d51 100644 --- a/stdlib/public/runtime/CompatibilityOverride.h +++ b/stdlib/public/runtime/CompatibilityOverride.h @@ -39,7 +39,7 @@ namespace swift { Override_ ## name getOverride_ ## name(); #include "CompatibilityOverride.def" - +#ifndef __wasm__ /// Used to define an override point. The override point #defines the appropriate /// OVERRIDE macro from CompatibilityOverride.def to this macro, then includes /// the file to generate the override points. The original implementation of the @@ -55,6 +55,20 @@ namespace swift { return Override(COMPATIBILITY_UNPAREN namedArgs, swift_ ## name ## Impl); \ return swift_ ## name ## Impl namedArgs; \ } +#else +// WebAssembly: hack: change to swift_once_real +#define COMPATIBILITY_OVERRIDE(name, ret, attrs, ccAttrs, namespace, typedArgs, namedArgs) \ + attrs ccAttrs ret namespace swift_ ## name typedArgs { \ + static Override_ ## name Override; \ + static swift_once_t Predicate; \ + swift_once_real(&Predicate, [](void *) { \ + Override = getOverride_ ## name(); \ + }, nullptr); \ + if (Override != nullptr) \ + return Override(COMPATIBILITY_UNPAREN namedArgs, swift_ ## name ## Impl); \ + return swift_ ## name ## Impl namedArgs; \ + } +#endif } /* end namespace swift */ From 3d8e4788d63cfe8975e1f9f1157c30ae4c6fca0d Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Mon, 29 Apr 2019 21:03:23 +0100 Subject: [PATCH 41/80] Add wasm branch scheme to update-checkout-config This allows cloning all of the repositories directly with ./swift/utils/update-checkout --clone --scheme wasm without relying on paths hardcoded in swiftwasm-sdk. It also clones icu that way as well, as that one is used when building on Linux. Later we could add "WebAssembly" platform to update-checkout-config.json to clone it only for WebAssembly and Linux platforms, but for now it's pulled for all platforms to make things easy. https://github.com/swiftwasm/swift/pull/1 --- .../update-checkout-config.json | 32 ++++++++++++++++--- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/utils/update_checkout/update-checkout-config.json b/utils/update_checkout/update-checkout-config.json index 969612f899647..923dd356f6707 100644 --- a/utils/update_checkout/update-checkout-config.json +++ b/utils/update_checkout/update-checkout-config.json @@ -3,7 +3,7 @@ "https-clone-pattern": "https://github.com/%s.git", "repos" : { "swift": { - "remote": { "id": "apple/swift" } }, + "remote": { "id": "swiftwasm/swift" } }, "cmark": { "remote": { "id": "apple/swift-cmark" } }, "llbuild": { @@ -29,8 +29,7 @@ "ninja": { "remote": { "id": "ninja-build/ninja" } }, "icu": { - "remote": { "id": "unicode-org/icu" }, - "platforms": [ "Linux" ] + "remote": { "id": "unicode-org/icu" } }, "cmake": { "remote": { "id": "KitWare/CMake" }, @@ -43,11 +42,34 @@ "swift-format": { "remote": { "id": "apple/swift-format" } }, "llvm-project": { - "remote": { "id": "apple/llvm-project" } } + "remote": { "id": "swiftwasm/llvm-project" } } }, "default-branch-scheme": "master", "branch-schemes": { - "master": { + "wasm": { + "aliases": ["wasm"], + "repos": { + "llvm-project": "swiftwasm", + "swift": "swiftwasm", + "cmark": "master", + "llbuild": "master", + "swiftpm": "master", + "swift-syntax": "master", + "swift-stress-tester": "master", + "swift-corelibs-xctest": "master", + "swift-corelibs-foundation": "master", + "swift-corelibs-libdispatch": "master", + "swift-integration-tests": "master", + "swift-xcode-playground-support": "master", + "ninja": "release", + "icu": "release-61-1", + "cmake": "v3.15.1", + "indexstore-db": "master", + "sourcekit-lsp": "master", + "swift-format": "master" + } + }, + "master": { "aliases": ["master", "swift/master"], "repos": { "llvm-project": "swift/master", From 92d60506895f0c4e2df461fc0c86201d445d1266 Mon Sep 17 00:00:00 2001 From: Zhuowei Zhang Date: Tue, 30 Apr 2019 00:40:32 -0700 Subject: [PATCH 42/80] WebAssembly: fix metadata table sizes Whoops. --- stdlib/public/runtime/SwiftRT-ELF.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/public/runtime/SwiftRT-ELF.cpp b/stdlib/public/runtime/SwiftRT-ELF.cpp index 9b4b8e8dbe2fb..65ae9f2aba91b 100644 --- a/stdlib/public/runtime/SwiftRT-ELF.cpp +++ b/stdlib/public/runtime/SwiftRT-ELF.cpp @@ -54,7 +54,7 @@ static void swift_image_constructor() { // WebAssembly hack: ok this should really go in its own file #define SWIFT_SECTION_RANGE(name) \ { reinterpret_cast(&__start_##name) + sizeof(void*), \ - static_cast(&__stop_##name - &__start_##name) } + static_cast(&__stop_##name - &__start_##name - sizeof(void*)) } #endif sections = { From 64c0cd3cabdb661e7458f5dbb94d719b1b461fe2 Mon Sep 17 00:00:00 2001 From: Zhuowei Zhang Date: Tue, 30 Apr 2019 00:40:52 -0700 Subject: [PATCH 43/80] WebAssembly: add a ton of logging when looking up metadata Not sure what's going on here: https://gist.github.com/zhuowei/beff646ebc09bed4458421cf7aba210b --- stdlib/public/runtime/MetadataLookup.cpp | 28 +++++++++++++++++++ stdlib/public/runtime/ProtocolConformance.cpp | 22 +++++++++++++-- 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/stdlib/public/runtime/MetadataLookup.cpp b/stdlib/public/runtime/MetadataLookup.cpp index 3e10ea06c9a04..4e47bbe024188 100644 --- a/stdlib/public/runtime/MetadataLookup.cpp +++ b/stdlib/public/runtime/MetadataLookup.cpp @@ -299,19 +299,47 @@ _findExtendedTypeContextDescriptor(const ContextDescriptor *maybeExtension, /// buildContextDescriptorMangling in MetadataReader. bool swift::_isCImportedTagType(const TypeContextDescriptor *type, const ParsedTypeIdentity &identity) { + fprintf(stderr, "trying to dump type %p\n", type); + fprintf(stderr, "name: %s\n", type->Name.get()); + fprintf(stderr, "trying to dump identity %p\n", &identity); + fprintf(stderr, "User facing name: %s\n", identity.UserFacingName.str().c_str()); + fprintf(stderr, "ok, let's go\n"); // Tag types are always imported as structs or enums. if (type->getKind() != ContextDescriptorKind::Enum && type->getKind() != ContextDescriptorKind::Struct) return false; + fprintf(stderr, "is it a c typedef\n"); + // Not a typedef imported as a nominal type. if (identity.isCTypedef()) return false; + fprintf(stderr, "is related entity\n"); + // Not a related entity. if (identity.isAnyRelatedEntity()) return false; + fprintf(stderr, "is c imported context\n"); + fprintf(stderr, "type's parent, raw: %x\n", *((unsigned int*)&type->Parent)); + fprintf(stderr, "type's parent: %p\n", type->Parent.get()); +// fprintf(stderr, "type's parent name: %s\n", type->Parent->Name.get()); + fprintf(stderr, "trying to get module context\n"); + + for (auto cur = type->Parent.get(); true; cur = cur->Parent.get()) { + fprintf(stderr, "cur %p\n", cur); + fprintf(stderr, "cur %x\n", (unsigned int)cur->getKind()); + if (auto module = dyn_cast(cur)) { + fprintf(stderr, "found\n"); + break; + } + } + + fprintf(stderr, "type's parent module context: %p\n", type->Parent->getModuleContext()); + fprintf(stderr, "trying to get name\n"); + fprintf(stderr, "type's parent module context name: %s\n", type->Parent->getModuleContext()->Name.get()); + // Imported from C. return type->Parent->isCImportedContext(); } diff --git a/stdlib/public/runtime/ProtocolConformance.cpp b/stdlib/public/runtime/ProtocolConformance.cpp index df83c30bce373..f2d24a5d8ec20 100644 --- a/stdlib/public/runtime/ProtocolConformance.cpp +++ b/stdlib/public/runtime/ProtocolConformance.cpp @@ -553,6 +553,7 @@ static const ProtocolConformanceDescriptor * swift_conformsToSwiftProtocolImpl(const Metadata * const type, const ProtocolDescriptor *protocol, StringRef module) { + fprintf(stderr, "in impl2\n"); auto &C = Conformances.get(); // See if we have a cached conformance. The ConcurrentMap data structure @@ -579,17 +580,25 @@ swift_conformsToSwiftProtocolImpl(const Metadata * const type, C.cacheFailure(type, protocol, snapshot.count()); return nullptr; } - + fprintf(stderr, "got to really scan\n"); // Really scan conformance records. for (size_t i = startIndex; i < endIndex; i++) { + fprintf(stderr, "index = %lx\n", (unsigned long)i); auto §ion = snapshot.Start[i]; // Eagerly pull records for nondependent witnesses into our cache. for (const auto &record : section) { - auto &descriptor = *record.get(); + fprintf(stderr, "got a record\n"); + auto descriptorPtr = record.get(); + auto &descriptor = *descriptorPtr; + fprintf(stderr, "got the descriptor: %p\n", descriptorPtr); + fprintf(stderr, "got the protocol: %p\n", descriptor.getProtocol()); + descriptor.getProtocol()->dump(); + fprintf(stderr, "got it\n"); // We only care about conformances for this protocol. if (descriptor.getProtocol() != protocol) continue; + fprintf(stderr, "about to get matching type\n"); // If there's a matching type, record the positive result. ConformanceCandidate candidate(descriptor); @@ -604,6 +613,7 @@ swift_conformsToSwiftProtocolImpl(const Metadata * const type, } // Conformance scan is complete. + fprintf(stderr, "about to update cache\n"); // Search the cache once more, and this time update the cache if necessary. FoundConformance = searchInConformanceCache(type, protocol); @@ -618,10 +628,18 @@ swift_conformsToSwiftProtocolImpl(const Metadata * const type, static const WitnessTable * swift_conformsToProtocolImpl(const Metadata * const type, const ProtocolDescriptor *protocol) { + // WebAssembly: logging pls + fprintf(stderr, "swift_conformsToProtocolImpl: %p %p\n", type, protocol); + protocol->dump(); + fprintf(stderr, "trying to dump the type now\n"); + type->dump(); + fprintf(stderr, "dumped the type\n"); + // end WebAssembly auto description = swift_conformsToSwiftProtocol(type, protocol, StringRef()); if (!description) return nullptr; + description->getProtocol()->dump(); return description->getWitnessTable( findConformingSuperclass(type, description)); From 87be907846abcef9df659619c1d4105bfb48bc0e Mon Sep 17 00:00:00 2001 From: Zhuowei Zhang Date: Tue, 30 Apr 2019 00:44:08 -0700 Subject: [PATCH 44/80] WebAssembly: fix a typo accidentally introduced into CMake file Thanks to maxdesiatov. --- cmake/modules/SwiftConfigureSDK.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmake/modules/SwiftConfigureSDK.cmake b/cmake/modules/SwiftConfigureSDK.cmake index 1383a20c4e5d6..b2a6bf50ed1d2 100644 --- a/cmake/modules/SwiftConfigureSDK.cmake +++ b/cmake/modules/SwiftConfigureSDK.cmake @@ -304,7 +304,7 @@ macro(configure_sdk_unix name architectures) if("${prefix}" STREQUAL "LINUX") if(arch MATCHES "(armv6|armv7)") - set(SWIFT_SDK_LINUX_ARCH_${arch}i_TRIPLE "${arch}-unknown-linux-gnueabihf") + set(SWIFT_SDK_LINUX_ARCH_${arch}_TRIPLE "${arch}-unknown-linux-gnueabihf") elseif(arch MATCHES "(aarch64|i686|powerpc64|powerpc64le|s390x|x86_64)") set(SWIFT_SDK_LINUX_ARCH_${arch}_TRIPLE "${arch}-unknown-linux-gnu") else() From f3c4fafe8244f02e96b19c0cd7e69172d257743c Mon Sep 17 00:00:00 2001 From: Zhuowei Zhang Date: Tue, 30 Apr 2019 12:01:58 -0700 Subject: [PATCH 45/80] WebAssembly: fix GOT-relative pointers without PCrel Previous commits changed a bunch of relative pointers to absolute in the metadata; at the time I didn't add support for adding tag bits, which signifies if the pointers point to the .got section (and thus needs an extra layer of indirection.) This broke _isCImportedTagType on $ss7UnicodeO5ASCIIOMn, so this commit adds back tag support for metadata pointers. --- lib/IRGen/ConstantBuilder.h | 10 +++++++++- lib/IRGen/GenMeta.cpp | 7 ++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/IRGen/ConstantBuilder.h b/lib/IRGen/ConstantBuilder.h index aca1fda1be3d0..8c85f8d3c2a33 100644 --- a/lib/IRGen/ConstantBuilder.h +++ b/lib/IRGen/ConstantBuilder.h @@ -99,7 +99,15 @@ class ConstantAggregateBuilderBase // WebAssembly: hack: doesn't support PCrel data relocations // also, we should set the lowest bit, but I don't know how to do that // there's no GOT on WebAssembly anyways though - add(llvm::ConstantExpr::getPtrToInt(reference.getValue(), IGM().RelativeAddressTy, false)); + + + llvm::Constant *offset = llvm::ConstantExpr::getPtrToInt(reference.getValue(), IGM().RelativeAddressTy, false); + // borrowed from addTaggedRelativeOffset + unsigned tag = unsigned(reference.isIndirect()); + if (tag) { + offset = llvm::ConstantExpr::getAdd(offset, llvm::ConstantInt::get(IGM().RelativeAddressTy, tag)); + } + add(offset); return; } addTaggedRelativeOffset(IGM().RelativeAddressTy, diff --git a/lib/IRGen/GenMeta.cpp b/lib/IRGen/GenMeta.cpp index c2c687c52b6bb..14c0b768f0cf1 100644 --- a/lib/IRGen/GenMeta.cpp +++ b/lib/IRGen/GenMeta.cpp @@ -4519,7 +4519,12 @@ GenericRequirementsMetadata irgen::addGenericRequirements( // WebAssembly: hack: Wasm doesn't support PC-relative offsets. // also doesn't handle tag yet if (IGM.TargetInfo.OutputObjectFormat == llvm::Triple::Wasm) { - B.add(llvm::ConstantExpr::getPtrToInt(descriptorRef.getValue(), IGM.RelativeAddressTy, false)); + llvm::Constant *offset = llvm::ConstantExpr::getPtrToInt(descriptorRef.getValue(), IGM.RelativeAddressTy, false); + // borrowed from addTaggedRelativeOffset + if (tag) { + offset = llvm::ConstantExpr::getAdd(offset, llvm::ConstantInt::get(IGM.RelativeAddressTy, tag)); + } + B.add(offset); return; } From b939a11d5b9e15a7a5f007fc86cb59dc8cc68596 Mon Sep 17 00:00:00 2001 From: Zhuowei Zhang Date: Tue, 30 Apr 2019 15:07:55 -0700 Subject: [PATCH 46/80] WebAssembly: add logging in swift_getAssociatedTypeWitness --- stdlib/public/runtime/Metadata.cpp | 32 ++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/stdlib/public/runtime/Metadata.cpp b/stdlib/public/runtime/Metadata.cpp index 23b4ecce1e370..bab9b02eb82af 100644 --- a/stdlib/public/runtime/Metadata.cpp +++ b/stdlib/public/runtime/Metadata.cpp @@ -4403,6 +4403,7 @@ swift_getAssociatedTypeWitnessSlowImpl( const Metadata *conformingType, const ProtocolRequirement *reqBase, const ProtocolRequirement *assocType) { + fprintf(stderr, "Entering slow path: %p %p %p %p\n", wtable, conformingType, reqBase, assocType); #ifndef NDEBUG { const ProtocolConformanceDescriptor *conformance = wtable->Description; @@ -4429,6 +4430,8 @@ swift_getAssociatedTypeWitnessSlowImpl( const char *mangledNameBase = (const char *)(uintptr_t(witness) & ~ProtocolRequirementFlags::AssociatedTypeMangledNameBit); + fprintf(stderr, "Mangled name base: %p\n", mangledNameBase); + fprintf(stderr, "name: %s\n", mangledNameBase); // Check whether the mangled name has the prefix byte indicating that // the mangled name is relative to the protocol itself. @@ -4443,13 +4446,18 @@ swift_getAssociatedTypeWitnessSlowImpl( const ProtocolConformanceDescriptor *conformance = wtable->Description; const ProtocolDescriptor *protocol = conformance->getProtocol(); + fprintf(stderr, "conformance %p protocol %p\n", conformance, protocol); + // Extract the mangled name itself. StringRef mangledName = Demangle::makeSymbolicMangledNameStringRef(mangledNameBase); + fprintf(stderr, "mangledName: %s\n", mangledName.str().c_str()); + // Demangle the associated type. MetadataResponse response; if (inProtocolContext) { + fprintf(stderr, "in protocol context\n"); // The protocol's Self is the only generic parameter that can occur in the // type. response = @@ -4472,6 +4480,7 @@ swift_getAssociatedTypeWitnessSlowImpl( dependentDescriptor); }).getResponse(); } else { + fprintf(stderr, "getting original conforming type\n"); // The generic parameters in the associated type name are those of the // conforming type. @@ -4490,6 +4499,18 @@ swift_getAssociatedTypeWitnessSlowImpl( }).getResponse(); } auto assocTypeMetadata = response.Value; + fprintf(stderr, "assocTypeMetadata: %p\n", assocTypeMetadata); + + if (true) { + auto conformingTypeNameInfo = swift_getTypeName(conformingType, true); + StringRef conformingTypeName(conformingTypeNameInfo.data, + conformingTypeNameInfo.length); + StringRef assocTypeName = findAssociatedTypeName(protocol, assocType); + fprintf(stderr, "fin: %s %s %s %s\n", assocTypeName.str().c_str(), + conformingTypeName.str().c_str(), + protocol->Name.get(), + mangledName.str().c_str()); + } if (!assocTypeMetadata) { auto conformingTypeNameInfo = swift_getTypeName(conformingType, true); @@ -4525,9 +4546,20 @@ swift::swift_getAssociatedTypeWitness(MetadataRequest request, // If the low bit of the witness is clear, it's already a metadata pointer. unsigned witnessIndex = assocType - reqBase; auto witness = ((const void* const *)wtable)[witnessIndex]; + fprintf(stderr, "getAssociatedTypeWitness fastpath: %x %p\n", witnessIndex, witness); if (LLVM_LIKELY((uintptr_t(witness) & ProtocolRequirementFlags::AssociatedTypeMangledNameBit) == 0)) { // Cached metadata pointers are always complete. + fprintf(stderr, "fastpath: %p\n", witness); + auto witnessPtr = (const Metadata *)witness; + witnessPtr->dump(); + if (witnessPtr->getKind() == MetadataKind::Class) { + fprintf(stderr, "class description:\n"); + auto witnessClass = witnessPtr->getClassObject(); + fprintf(stderr, "%lx\n", *(unsigned long*)&witnessClass->Data); + if (witnessClass->isTypeMetadata()) + fprintf(stderr, "name: %s\n", witnessClass->getDescription()->Name.get()); + } return MetadataResponse{(const Metadata *)witness, MetadataState::Complete}; } From 1843a8740276c2a665dd7cb54bb6936753446309 Mon Sep 17 00:00:00 2001 From: Zhuowei Zhang Date: Thu, 2 May 2019 11:55:24 -0700 Subject: [PATCH 47/80] Revert "WebAssembly: add logging in swift_getAssociatedTypeWitness" Don't need the logging anymore. This reverts commit 7d35644ca7bfa027505f09b9232d34b8191bd42d. --- stdlib/public/runtime/Metadata.cpp | 32 ------------------------------ 1 file changed, 32 deletions(-) diff --git a/stdlib/public/runtime/Metadata.cpp b/stdlib/public/runtime/Metadata.cpp index bab9b02eb82af..23b4ecce1e370 100644 --- a/stdlib/public/runtime/Metadata.cpp +++ b/stdlib/public/runtime/Metadata.cpp @@ -4403,7 +4403,6 @@ swift_getAssociatedTypeWitnessSlowImpl( const Metadata *conformingType, const ProtocolRequirement *reqBase, const ProtocolRequirement *assocType) { - fprintf(stderr, "Entering slow path: %p %p %p %p\n", wtable, conformingType, reqBase, assocType); #ifndef NDEBUG { const ProtocolConformanceDescriptor *conformance = wtable->Description; @@ -4430,8 +4429,6 @@ swift_getAssociatedTypeWitnessSlowImpl( const char *mangledNameBase = (const char *)(uintptr_t(witness) & ~ProtocolRequirementFlags::AssociatedTypeMangledNameBit); - fprintf(stderr, "Mangled name base: %p\n", mangledNameBase); - fprintf(stderr, "name: %s\n", mangledNameBase); // Check whether the mangled name has the prefix byte indicating that // the mangled name is relative to the protocol itself. @@ -4446,18 +4443,13 @@ swift_getAssociatedTypeWitnessSlowImpl( const ProtocolConformanceDescriptor *conformance = wtable->Description; const ProtocolDescriptor *protocol = conformance->getProtocol(); - fprintf(stderr, "conformance %p protocol %p\n", conformance, protocol); - // Extract the mangled name itself. StringRef mangledName = Demangle::makeSymbolicMangledNameStringRef(mangledNameBase); - fprintf(stderr, "mangledName: %s\n", mangledName.str().c_str()); - // Demangle the associated type. MetadataResponse response; if (inProtocolContext) { - fprintf(stderr, "in protocol context\n"); // The protocol's Self is the only generic parameter that can occur in the // type. response = @@ -4480,7 +4472,6 @@ swift_getAssociatedTypeWitnessSlowImpl( dependentDescriptor); }).getResponse(); } else { - fprintf(stderr, "getting original conforming type\n"); // The generic parameters in the associated type name are those of the // conforming type. @@ -4499,18 +4490,6 @@ swift_getAssociatedTypeWitnessSlowImpl( }).getResponse(); } auto assocTypeMetadata = response.Value; - fprintf(stderr, "assocTypeMetadata: %p\n", assocTypeMetadata); - - if (true) { - auto conformingTypeNameInfo = swift_getTypeName(conformingType, true); - StringRef conformingTypeName(conformingTypeNameInfo.data, - conformingTypeNameInfo.length); - StringRef assocTypeName = findAssociatedTypeName(protocol, assocType); - fprintf(stderr, "fin: %s %s %s %s\n", assocTypeName.str().c_str(), - conformingTypeName.str().c_str(), - protocol->Name.get(), - mangledName.str().c_str()); - } if (!assocTypeMetadata) { auto conformingTypeNameInfo = swift_getTypeName(conformingType, true); @@ -4546,20 +4525,9 @@ swift::swift_getAssociatedTypeWitness(MetadataRequest request, // If the low bit of the witness is clear, it's already a metadata pointer. unsigned witnessIndex = assocType - reqBase; auto witness = ((const void* const *)wtable)[witnessIndex]; - fprintf(stderr, "getAssociatedTypeWitness fastpath: %x %p\n", witnessIndex, witness); if (LLVM_LIKELY((uintptr_t(witness) & ProtocolRequirementFlags::AssociatedTypeMangledNameBit) == 0)) { // Cached metadata pointers are always complete. - fprintf(stderr, "fastpath: %p\n", witness); - auto witnessPtr = (const Metadata *)witness; - witnessPtr->dump(); - if (witnessPtr->getKind() == MetadataKind::Class) { - fprintf(stderr, "class description:\n"); - auto witnessClass = witnessPtr->getClassObject(); - fprintf(stderr, "%lx\n", *(unsigned long*)&witnessClass->Data); - if (witnessClass->isTypeMetadata()) - fprintf(stderr, "name: %s\n", witnessClass->getDescription()->Name.get()); - } return MetadataResponse{(const Metadata *)witness, MetadataState::Complete}; } From 2406043c5b56f88b2ea7d3bcdfca61bb9f68f275 Mon Sep 17 00:00:00 2001 From: Zhuowei Zhang Date: Thu, 2 May 2019 11:56:27 -0700 Subject: [PATCH 48/80] Revert "WebAssembly: add a ton of logging when looking up metadata" Don't need logging anymore. This reverts commit 065ab6f61c80c2573d72e1572c751914d42f7a61. --- stdlib/public/runtime/MetadataLookup.cpp | 28 ------------------- stdlib/public/runtime/ProtocolConformance.cpp | 22 ++------------- 2 files changed, 2 insertions(+), 48 deletions(-) diff --git a/stdlib/public/runtime/MetadataLookup.cpp b/stdlib/public/runtime/MetadataLookup.cpp index 4e47bbe024188..3e10ea06c9a04 100644 --- a/stdlib/public/runtime/MetadataLookup.cpp +++ b/stdlib/public/runtime/MetadataLookup.cpp @@ -299,47 +299,19 @@ _findExtendedTypeContextDescriptor(const ContextDescriptor *maybeExtension, /// buildContextDescriptorMangling in MetadataReader. bool swift::_isCImportedTagType(const TypeContextDescriptor *type, const ParsedTypeIdentity &identity) { - fprintf(stderr, "trying to dump type %p\n", type); - fprintf(stderr, "name: %s\n", type->Name.get()); - fprintf(stderr, "trying to dump identity %p\n", &identity); - fprintf(stderr, "User facing name: %s\n", identity.UserFacingName.str().c_str()); - fprintf(stderr, "ok, let's go\n"); // Tag types are always imported as structs or enums. if (type->getKind() != ContextDescriptorKind::Enum && type->getKind() != ContextDescriptorKind::Struct) return false; - fprintf(stderr, "is it a c typedef\n"); - // Not a typedef imported as a nominal type. if (identity.isCTypedef()) return false; - fprintf(stderr, "is related entity\n"); - // Not a related entity. if (identity.isAnyRelatedEntity()) return false; - fprintf(stderr, "is c imported context\n"); - fprintf(stderr, "type's parent, raw: %x\n", *((unsigned int*)&type->Parent)); - fprintf(stderr, "type's parent: %p\n", type->Parent.get()); -// fprintf(stderr, "type's parent name: %s\n", type->Parent->Name.get()); - fprintf(stderr, "trying to get module context\n"); - - for (auto cur = type->Parent.get(); true; cur = cur->Parent.get()) { - fprintf(stderr, "cur %p\n", cur); - fprintf(stderr, "cur %x\n", (unsigned int)cur->getKind()); - if (auto module = dyn_cast(cur)) { - fprintf(stderr, "found\n"); - break; - } - } - - fprintf(stderr, "type's parent module context: %p\n", type->Parent->getModuleContext()); - fprintf(stderr, "trying to get name\n"); - fprintf(stderr, "type's parent module context name: %s\n", type->Parent->getModuleContext()->Name.get()); - // Imported from C. return type->Parent->isCImportedContext(); } diff --git a/stdlib/public/runtime/ProtocolConformance.cpp b/stdlib/public/runtime/ProtocolConformance.cpp index f2d24a5d8ec20..df83c30bce373 100644 --- a/stdlib/public/runtime/ProtocolConformance.cpp +++ b/stdlib/public/runtime/ProtocolConformance.cpp @@ -553,7 +553,6 @@ static const ProtocolConformanceDescriptor * swift_conformsToSwiftProtocolImpl(const Metadata * const type, const ProtocolDescriptor *protocol, StringRef module) { - fprintf(stderr, "in impl2\n"); auto &C = Conformances.get(); // See if we have a cached conformance. The ConcurrentMap data structure @@ -580,25 +579,17 @@ swift_conformsToSwiftProtocolImpl(const Metadata * const type, C.cacheFailure(type, protocol, snapshot.count()); return nullptr; } - fprintf(stderr, "got to really scan\n"); + // Really scan conformance records. for (size_t i = startIndex; i < endIndex; i++) { - fprintf(stderr, "index = %lx\n", (unsigned long)i); auto §ion = snapshot.Start[i]; // Eagerly pull records for nondependent witnesses into our cache. for (const auto &record : section) { - fprintf(stderr, "got a record\n"); - auto descriptorPtr = record.get(); - auto &descriptor = *descriptorPtr; - fprintf(stderr, "got the descriptor: %p\n", descriptorPtr); - fprintf(stderr, "got the protocol: %p\n", descriptor.getProtocol()); - descriptor.getProtocol()->dump(); - fprintf(stderr, "got it\n"); + auto &descriptor = *record.get(); // We only care about conformances for this protocol. if (descriptor.getProtocol() != protocol) continue; - fprintf(stderr, "about to get matching type\n"); // If there's a matching type, record the positive result. ConformanceCandidate candidate(descriptor); @@ -613,7 +604,6 @@ swift_conformsToSwiftProtocolImpl(const Metadata * const type, } // Conformance scan is complete. - fprintf(stderr, "about to update cache\n"); // Search the cache once more, and this time update the cache if necessary. FoundConformance = searchInConformanceCache(type, protocol); @@ -628,18 +618,10 @@ swift_conformsToSwiftProtocolImpl(const Metadata * const type, static const WitnessTable * swift_conformsToProtocolImpl(const Metadata * const type, const ProtocolDescriptor *protocol) { - // WebAssembly: logging pls - fprintf(stderr, "swift_conformsToProtocolImpl: %p %p\n", type, protocol); - protocol->dump(); - fprintf(stderr, "trying to dump the type now\n"); - type->dump(); - fprintf(stderr, "dumped the type\n"); - // end WebAssembly auto description = swift_conformsToSwiftProtocol(type, protocol, StringRef()); if (!description) return nullptr; - description->getProtocol()->dump(); return description->getWitnessTable( findConformingSuperclass(type, description)); From b9b9781ece6f201e4b73b06fa87c41ec1ab0df2e Mon Sep 17 00:00:00 2001 From: Zhuowei Zhang Date: Sat, 8 Jun 2019 17:35:09 -0700 Subject: [PATCH 49/80] WebAssembly: add replac2 section to start/end objects --- swift_end.cpp | 1 + swift_start.cpp | 1 + 2 files changed, 2 insertions(+) diff --git a/swift_end.cpp b/swift_end.cpp index 714e0d35d7442..b3d476ac41305 100644 --- a/swift_end.cpp +++ b/swift_end.cpp @@ -26,4 +26,5 @@ DECLARE_SWIFT_SECTION(swift5_reflstr) DECLARE_SWIFT_SECTION(swift5_fieldmd) DECLARE_SWIFT_SECTION(swift5_assocty) DECLARE_SWIFT_SECTION(swift5_replace) +DECLARE_SWIFT_SECTION(swift5_replac2) } diff --git a/swift_start.cpp b/swift_start.cpp index 5a77ea3c0f527..a9cf1faa390b3 100644 --- a/swift_start.cpp +++ b/swift_start.cpp @@ -26,4 +26,5 @@ DECLARE_SWIFT_SECTION(swift5_reflstr) DECLARE_SWIFT_SECTION(swift5_fieldmd) DECLARE_SWIFT_SECTION(swift5_assocty) DECLARE_SWIFT_SECTION(swift5_replace) +DECLARE_SWIFT_SECTION(swift5_replac2) } From b0b619e76138ef3b29114ae9d4fb23094104f9e7 Mon Sep 17 00:00:00 2001 From: Kyle Verrier Date: Sat, 15 Jun 2019 15:12:59 -0400 Subject: [PATCH 50/80] Update default checkout scheme to "wasm". --- utils/update_checkout/update-checkout-config.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/update_checkout/update-checkout-config.json b/utils/update_checkout/update-checkout-config.json index 923dd356f6707..9c10c92c79b34 100644 --- a/utils/update_checkout/update-checkout-config.json +++ b/utils/update_checkout/update-checkout-config.json @@ -44,7 +44,7 @@ "llvm-project": { "remote": { "id": "swiftwasm/llvm-project" } } }, - "default-branch-scheme": "master", + "default-branch-scheme": "wasm", "branch-schemes": { "wasm": { "aliases": ["wasm"], From c009468cc6da082e76a21387268a83a2ea81d1bb Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Tue, 22 Oct 2019 13:13:57 +0100 Subject: [PATCH 51/80] Add --build-swift-static-stdlib to build script --- vvv.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/vvv.sh b/vvv.sh index db86f386cd5fa..6f202440842fd 100755 --- a/vvv.sh +++ b/vvv.sh @@ -7,4 +7,5 @@ utils/build-script --release-debuginfo --wasm \ --wasm-icu-i18n "todo" \ --wasm-icu-i18n-include "todo" \ --wasm-icu-data "todo" \ + --build-swift-static-stdlib \ "$@" From d62abee376e662f447e8f1909d55b93444727fbd Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Sun, 27 Oct 2019 17:52:26 +0900 Subject: [PATCH 52/80] [WASM] Set LIBC_INCLUDE_DIRECTORY for WASM to build SwiftGlibc properly (#9) `LIBC_INCLUDE_DIRECTORY` is used to generate `glibc.modulemap`. Currently the directory is set `/usr/include` but it's system header path and we should use `wasi-sdk` version. --- cmake/modules/SwiftConfigureSDK.cmake | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cmake/modules/SwiftConfigureSDK.cmake b/cmake/modules/SwiftConfigureSDK.cmake index b2a6bf50ed1d2..7ac91bacc84dc 100644 --- a/cmake/modules/SwiftConfigureSDK.cmake +++ b/cmake/modules/SwiftConfigureSDK.cmake @@ -340,6 +340,8 @@ macro(configure_sdk_unix name architectures) set(SWIFT_SDK_WASM_ARCH_wasm32_PATH "${SWIFT_WASM_WASI_SDK_PATH}/share/sysroot") # fixme: Wasi is wasm32-unknown-wasi-musl. This LLVM doesn't have it yet. set(SWIFT_SDK_WASM_ARCH_wasm32_TRIPLE "wasm32-unknown-unknown-wasm") + set(SWIFT_SDK_WASM_ARCH_wasm32_LIBC_INCLUDE_DIRECTORY "${SWIFT_WASM_WASI_SDK_PATH}/share/sysroot/include" CACHE STRING "Path to C library headers") + set(SWIFT_SDK_WASM_ARCH_wasm32_LIBC_ARCHITECTURE_INCLUDE_DIRECTORY "${SWIFT_WASM_WASI_SDK_PATH}/sysroot/include" CACHE STRING "Path to C library architecture headers") else() message(FATAL_ERROR "unknown Unix OS: ${prefix}") endif() From 14e332ea6eb2012af37d17d98af4d5b01ab33315 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Sun, 3 Nov 2019 00:30:13 +0900 Subject: [PATCH 53/80] Support building on macOS to develop this project (#8) * Add simple GitHub Actions config for macOS * Add brew install to the macOS GH action * Add update-checkout call to macOS GH action * Export sourcedir shell variable in macOS GH action * [WASM] Use prebuilt llvm-ar only on Linux * [WASM] Added new Object format WASM to distinguish from ELF * [WASM] Separate SwiftRT from ELF * [WASM] Add preprocessor condition for wasm32 to track latest upstream * Add wasi-sdk and icu install steps to the script * [WASM] Set LIBC_INCLUDE_DIRECTORY without CACHE attribute to force new value * [WASM] Adding share path to point correct directory * [WASM] Add Linux CI job * [WASM] Checkout branch after checking out Swift repos * [WASM] Use release build to reduce artifact size * [WASM] Use SWIFT_PRIMARY_VARIANT_SDK instead of HOST_SDK In this case, SWIFT_PRIMARY_VARIANT_ARCH is used but host SDK was not PRIMARY_SDK. * [WASM] Remove ICU_STATICLIB on Linux * [WASM] Output build log verbose * [WASM] Set ICU lib directory instead of archive * [WASM] Sort build options * Revert "[WASM] Use SWIFT_PRIMARY_VARIANT_SDK instead of HOST_SDK" --- .github/workflows/main.yml | 111 +++++++++++++++++++++++++ CMakeLists.txt | 8 +- cmake/modules/AddSwift.cmake | 3 +- cmake/modules/SwiftConfigureSDK.cmake | 6 +- lib/Driver/CMakeLists.txt | 3 +- stdlib/public/core/StringStorage.swift | 2 +- stdlib/public/runtime/CMakeLists.txt | 17 +++- stdlib/public/runtime/SwiftRT-ELF.cpp | 7 -- stdlib/public/runtime/SwiftRT-WASM.cpp | 11 +++ 9 files changed, 152 insertions(+), 16 deletions(-) create mode 100644 .github/workflows/main.yml create mode 100644 stdlib/public/runtime/SwiftRT-WASM.cpp diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000000000..ba665370d2b70 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,111 @@ +name: CI + +on: + pull_request: + branches: + - swiftwasm + +jobs: + linux_build: + timeout-minutes: 0 + runs-on: ubuntu-18.04 + + steps: + - uses: actions/checkout@v1 + - name: Run a multi-line script + run: | + sudo apt update + sudo apt install \ + git ninja-build clang python \ + uuid-dev libicu-dev icu-devtools libbsd-dev \ + libedit-dev libxml2-dev libsqlite3-dev swig \ + libpython-dev libncurses5-dev pkg-config \ + libblocksruntime-dev libcurl4-openssl-dev \ + systemtap-sdt-dev tzdata rsync + + ./utils/update-checkout --clone --scheme wasm + git checkout $GITHUB_SHA + export sourcedir=$PWD/.. + cd $sourcedir + + wget -O install_cmake.sh "https://github.com/Kitware/CMake/releases/download/v3.15.3/cmake-3.15.3-Linux-x86_64.sh" + chmod +x install_cmake.sh + sudo mkdir -p /opt/cmake + sudo ./install_cmake.sh --skip-license --prefix=/opt/cmake + sudo ln -sf /opt/cmake/bin/* /usr/local/bin + cmake --version + + wget -O wasi-sdk.tar.gz https://github.com/swiftwasm/wasi-sdk/releases/download/20190421.6/wasi-sdk-3.19gefb17cb478f9.m-linux.tar.gz + tar xfz wasi-sdk.tar.gz + mv wasi-sdk-3.19gefb17cb478f9+m/opt/wasi-sdk ./wasi-sdk + + wget -O icu.tar.xz "https://github.com/swiftwasm/icu4c-wasi/releases/download/20190421.3/icu4c-wasi.tar.xz" + tar xf icu.tar.xz + + cd swift + utils/build-script --release --wasm --verbose \ + --skip-build-benchmarks \ + --extra-cmake-options=" \ + -DSWIFT_SDKS='WASM;LINUX' \ + -DSWIFT_BUILD_SOURCEKIT=FALSE \ + -DSWIFT_ENABLE_SOURCEKIT_TESTS=FALSE \ + " \ + --build-stdlib-deployment-targets "wasm-wasm32" \ + --build-swift-static-stdlib \ + --install-destdir="$sourcedir/install" \ + --install-prefix="/opt/swiftwasm-sdk" \ + --install-swift \ + --installable-package="$sourcedir/swiftwasm.tar.gz" \ + --llvm-targets-to-build "X86;WebAssembly" \ + --stdlib-deployment-targets "wasm-wasm32" \ + --wasm-icu-data "todo-icu-data" \ + --wasm-icu-i18n "$sourcedir/icu_out/lib" \ + --wasm-icu-i18n-include "$sourcedir/icu_out/include" \ + --wasm-icu-uc "$sourcedir/icu_out/lib" \ + --wasm-icu-uc-include "$sourcedir/icu_out/include" \ + --wasm-wasi-sdk "$sourcedir/wasi-sdk" + + macos_build: + timeout-minutes: 0 + runs-on: macOS-10.14 + + steps: + - uses: actions/checkout@v1 + - name: Run a multi-line script + run: | + brew install cmake ninja + ./utils/update-checkout --clone --scheme wasm + git checkout $GITHUB_SHA + export sourcedir=$PWD/.. + cd $sourcedir + wget -O wasi-sdk.tar.gz https://github.com/swiftwasm/wasi-sdk/releases/download/20190421.6/wasi-sdk-3.19gefb17cb478f9.m-linux.tar.gz + tar xfz wasi-sdk.tar.gz + mv wasi-sdk-3.19gefb17cb478f9+m/opt/wasi-sdk ./wasi-sdk + # Link sysroot/usr/include to sysroot/include because Darwin sysroot doesn't + # find header files in sysroot/include but sysroot/usr/include + mkdir wasi-sdk/share/sysroot/usr/ + ln -s ../include wasi-sdk/share/sysroot/usr/include + wget -O icu.tar.xz "https://github.com/swiftwasm/icu4c-wasi/releases/download/20190421.3/icu4c-wasi.tar.xz" + tar xf icu.tar.xz + cd swift + ./utils/build-script --release --wasm --verbose \ + --skip-build-benchmarks \ + --extra-cmake-options=" \ + -DSWIFT_PRIMARY_VARIANT_SDK:STRING=WASM \ + -DSWIFT_PRIMARY_VARIANT_ARCH:STRING=wasm32 \ + -DSWIFT_OSX_x86_64_ICU_STATICLIB=TRUE \ + -DSWIFT_BUILD_SOURCEKIT=FALSE \ + -DSWIFT_ENABLE_SOURCEKIT_TESTS=FALSE \ + " \ + --build-stdlib-deployment-targets "wasm-wasm32" \ + --build-swift-dynamic-sdk-overlay false \ + --build-swift-static-sdk-overlay false \ + --build-swift-static-stdlib \ + --llvm-targets-to-build "X86;WebAssembly" \ + --stdlib-deployment-targets "wasm-wasm32" \ + --wasm-icu-data "todo-icu-data" \ + --wasm-icu-i18n "$sourcedir/icu_out/lib" \ + --wasm-icu-i18n-include "$sourcedir/icu_out/include" \ + --wasm-icu-uc "$sourcedir/icu_out/lib" \ + --wasm-icu-uc-include "$sourcedir/icu_out/include" \ + --wasm-wasi-sdk "$sourcedir/wasi-sdk" diff --git a/CMakeLists.txt b/CMakeLists.txt index 2a1c50508d847..55fe35799c22b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -36,9 +36,6 @@ ENABLE_LANGUAGE(C) include(SwiftUtils) include(CheckSymbolExists) -# WebAssembly: hack: use llvm-ar for creating static libraries; Ubuntu's GNU ar doesn't work with wasm-ld -set(CMAKE_AR "${SWIFT_WASM_WASI_SDK_PATH}/bin/llvm-ar") - # # User-configurable options that control the inclusion and default build # behavior for components which may not strictly be necessary (tools, examples, @@ -794,6 +791,11 @@ if(swift_build_wasm AND NOT "${SWIFT_HOST_VARIANT_SDK}" STREQUAL "WASM") # message(FATAL_ERROR "A Darwin or Linux host is required to build the Swift runtime for Android") #endif() + if ("${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "Linux") + # WebAssembly: hack: use llvm-ar for creating static libraries; Ubuntu's GNU ar doesn't work with wasm-ld + set(CMAKE_AR "${SWIFT_WASM_WASI_SDK_PATH}/bin/llvm-ar") + endif() + if("${SWIFT_SDK_WASM_ARCHITECTURES}" STREQUAL "") set(SWIFT_SDK_WASM_ARCHITECTURES wasm32) endif() diff --git a/cmake/modules/AddSwift.cmake b/cmake/modules/AddSwift.cmake index 19301a7966dad..ce8421b6baff9 100644 --- a/cmake/modules/AddSwift.cmake +++ b/cmake/modules/AddSwift.cmake @@ -1076,7 +1076,8 @@ function(_add_swift_library_single target name) ${INCORPORATED_OBJECT_LIBRARIES_EXPRESSIONS} ${SWIFTLIB_SINGLE_XCODE_WORKAROUND_SOURCES}) if(("${SWIFT_SDK_${SWIFTLIB_SINGLE_SDK}_OBJECT_FORMAT}" STREQUAL "ELF" OR - "${SWIFT_SDK_${SWIFTLIB_SINGLE_SDK}_OBJECT_FORMAT}" STREQUAL "COFF") AND + "${SWIFT_SDK_${SWIFTLIB_SINGLE_SDK}_OBJECT_FORMAT}" STREQUAL "COFF" OR + "${SWIFT_SDK_${SWIFTLIB_SINGLE_SDK}_OBJECT_FORMAT}" STREQUAL "WASM") AND SWIFTLIB_SINGLE_TARGET_LIBRARY) if("${libkind}" STREQUAL "SHARED" AND NOT SWIFTLIB_SINGLE_NOSWIFTRT) # TODO(compnerd) switch to the generator expression when cmake is upgraded diff --git a/cmake/modules/SwiftConfigureSDK.cmake b/cmake/modules/SwiftConfigureSDK.cmake index 7ac91bacc84dc..ec29b60d53d07 100644 --- a/cmake/modules/SwiftConfigureSDK.cmake +++ b/cmake/modules/SwiftConfigureSDK.cmake @@ -217,6 +217,8 @@ macro(configure_sdk_unix name architectures) set(SWIFT_SDK_${prefix}_ARCHITECTURES "${architectures}") if("${prefix}" STREQUAL "CYGWIN") set(SWIFT_SDK_${prefix}_OBJECT_FORMAT "COFF") + elseif("${prefix}" STREQUAL "WASM") + set(SWIFT_SDK_${prefix}_OBJECT_FORMAT "WASM") else() set(SWIFT_SDK_${prefix}_OBJECT_FORMAT "ELF") endif() @@ -340,8 +342,8 @@ macro(configure_sdk_unix name architectures) set(SWIFT_SDK_WASM_ARCH_wasm32_PATH "${SWIFT_WASM_WASI_SDK_PATH}/share/sysroot") # fixme: Wasi is wasm32-unknown-wasi-musl. This LLVM doesn't have it yet. set(SWIFT_SDK_WASM_ARCH_wasm32_TRIPLE "wasm32-unknown-unknown-wasm") - set(SWIFT_SDK_WASM_ARCH_wasm32_LIBC_INCLUDE_DIRECTORY "${SWIFT_WASM_WASI_SDK_PATH}/share/sysroot/include" CACHE STRING "Path to C library headers") - set(SWIFT_SDK_WASM_ARCH_wasm32_LIBC_ARCHITECTURE_INCLUDE_DIRECTORY "${SWIFT_WASM_WASI_SDK_PATH}/sysroot/include" CACHE STRING "Path to C library architecture headers") + set(SWIFT_SDK_WASM_ARCH_wasm32_LIBC_INCLUDE_DIRECTORY "${SWIFT_WASM_WASI_SDK_PATH}/share/sysroot/include") + set(SWIFT_SDK_WASM_ARCH_wasm32_LIBC_ARCHITECTURE_INCLUDE_DIRECTORY "${SWIFT_WASM_WASI_SDK_PATH}/share/sysroot/include") else() message(FATAL_ERROR "unknown Unix OS: ${prefix}") endif() diff --git a/lib/Driver/CMakeLists.txt b/lib/Driver/CMakeLists.txt index 448f5044b7064..83d6afa8ca55c 100644 --- a/lib/Driver/CMakeLists.txt +++ b/lib/Driver/CMakeLists.txt @@ -33,7 +33,8 @@ target_link_libraries(swiftDriver PRIVATE if(SWIFT_BUILD_STATIC_STDLIB) set(static_stdlib_lnk_file_list) foreach(sdk ${SWIFT_CONFIGURED_SDKS}) - if("${SWIFT_SDK_${sdk}_OBJECT_FORMAT}" STREQUAL "ELF") + if("${SWIFT_SDK_${sdk}_OBJECT_FORMAT}" STREQUAL "ELF" OR + "${SWIFT_SDK_${sdk}_OBJECT_FORMAT}" STREQUAL "WASM") string(TOLOWER "${sdk}" lowercase_sdk) if(SWIFT_${SWIFT_HOST_VARIANT_SDK}_${SWIFT_HOST_VARIANT_ARCH}_ICU_STATICLIB) set(ICU_STATICLIB "TRUE") diff --git a/stdlib/public/core/StringStorage.swift b/stdlib/public/core/StringStorage.swift index a80937d1f7141..3e47a43cc3f30 100644 --- a/stdlib/public/core/StringStorage.swift +++ b/stdlib/public/core/StringStorage.swift @@ -186,7 +186,7 @@ extension __StringStorage { let count = try initializer(buffer) let countAndFlags = CountAndFlags(mortalCount: count, isASCII: false) - #if arch(i386) || arch(arm) + #if arch(i386) || arch(arm) || arch(wasm32) storage._count = countAndFlags.count storage._flags = countAndFlags.flags #else diff --git a/stdlib/public/runtime/CMakeLists.txt b/stdlib/public/runtime/CMakeLists.txt index 7b0c16cc9245e..475e78d15d427 100644 --- a/stdlib/public/runtime/CMakeLists.txt +++ b/stdlib/public/runtime/CMakeLists.txt @@ -80,6 +80,7 @@ endif(LLVM_ENABLE_ASSERTIONS) set(LLVM_OPTIONAL_SOURCES SwiftRT-COFF.cpp SwiftRT-ELF.cpp + SwiftRT-WASM.cpp ${swift_runtime_sources} ${swift_runtime_objc_sources} ${swift_runtime_leaks_sources}) @@ -179,11 +180,14 @@ add_swift_target_library(swiftRuntime OBJECT_LIBRARY set(ELFISH_SDKS) set(COFF_SDKS) +set(WASM_SDKS) foreach(sdk ${SWIFT_CONFIGURED_SDKS}) if("${SWIFT_SDK_${sdk}_OBJECT_FORMAT}" STREQUAL "ELF") list(APPEND ELFISH_SDKS ${sdk}) elseif("${SWIFT_SDK_${sdk}_OBJECT_FORMAT}" STREQUAL "COFF") list(APPEND COFF_SDKS ${sdk}) + elseif("${SWIFT_SDK_${sdk}_OBJECT_FORMAT}" STREQUAL "WASM") + list(APPEND WASM_SDKS ${sdk}) endif() endforeach() @@ -196,6 +200,16 @@ add_swift_target_library(swiftImageRegistrationObjectELF TARGET_SDKS ${ELFISH_SDKS} SWIFT_COMPILE_FLAGS ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS} INSTALL_IN_COMPONENT none) + +add_swift_target_library(swiftImageRegistrationObjectWASM + OBJECT_LIBRARY IS_STDLIB IS_STDLIB_CORE + SwiftRT-WASM.cpp + C_COMPILE_FLAGS ${SWIFT_RUNTIME_CORE_CXX_FLAGS} + LINK_FLAGS ${SWIFT_RUNTIME_CORE_LINK_FLAGS} + TARGET_SDKS ${WASM_SDKS} + SWIFT_COMPILE_FLAGS ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS} + INSTALL_IN_COMPONENT none) + # FIXME(compnerd) this should be compiled twice, once for static and once for # shared. The static version should be used for building the standard library. add_swift_target_library(swiftImageRegistrationObjectCOFF @@ -213,7 +227,8 @@ foreach(sdk ${SWIFT_CONFIGURED_SDKS}) set(arch_suffix "${SWIFT_SDK_${sdk}_LIB_SUBDIR}-${arch}") if("${SWIFT_SDK_${sdk}_OBJECT_FORMAT}" STREQUAL "ELF" OR - "${SWIFT_SDK_${sdk}_OBJECT_FORMAT}" STREQUAL "COFF") + "${SWIFT_SDK_${sdk}_OBJECT_FORMAT}" STREQUAL "COFF" OR + "${SWIFT_SDK_${sdk}_OBJECT_FORMAT}" STREQUAL "WASM") # TODO(compnerd) switch to the generator expression when cmake is upgraded # to a version which supports it. # set(swiftrtObject "$") diff --git a/stdlib/public/runtime/SwiftRT-ELF.cpp b/stdlib/public/runtime/SwiftRT-ELF.cpp index 65ae9f2aba91b..fa585a8a68241 100644 --- a/stdlib/public/runtime/SwiftRT-ELF.cpp +++ b/stdlib/public/runtime/SwiftRT-ELF.cpp @@ -46,16 +46,9 @@ static swift::MetadataSections sections{}; __attribute__((__constructor__)) static void swift_image_constructor() { -#ifndef __wasm__ #define SWIFT_SECTION_RANGE(name) \ { reinterpret_cast(&__start_##name), \ static_cast(&__stop_##name - &__start_##name) } -#else -// WebAssembly hack: ok this should really go in its own file -#define SWIFT_SECTION_RANGE(name) \ - { reinterpret_cast(&__start_##name) + sizeof(void*), \ - static_cast(&__stop_##name - &__start_##name - sizeof(void*)) } -#endif sections = { swift::CurrentSectionMetadataVersion, diff --git a/stdlib/public/runtime/SwiftRT-WASM.cpp b/stdlib/public/runtime/SwiftRT-WASM.cpp new file mode 100644 index 0000000000000..6812763e9b1e7 --- /dev/null +++ b/stdlib/public/runtime/SwiftRT-WASM.cpp @@ -0,0 +1,11 @@ +//===--- SwiftRT-WASM.cpp --------------------------------------------------===// +// +// This source file is part of the Swift.org open source project +// +// Copyright (c) 2014 - 2017 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 +// +//===----------------------------------------------------------------------===// From a1088a38ad8e88e12f5433d9fbcfd6a434400207 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Sat, 9 Nov 2019 02:08:11 +0900 Subject: [PATCH 54/80] Swift Runtime on WebAssembly (#11) * [WASM] Build ImageInspectionShared even on macOS ImageInspectionShared was built only if host OS is Linux. But it's necessary when primary sdk is WASM. * [WASM] Use llvm-ar instead of ranlib on macOS Specify ar binary through extra-cmake-options * [WASM] Install llvm through HomeBrew to use llvm-ar * [WASM] Use llvm-ranlib instead of ranlib of Xcode * [WASM] Read offset as pointer when target is wasm Because WASM doesn't support relative pointer, compiler emit direct address instead of offset from current address. * [WASM] Copy SwiftRT-ELF.cpp into SwiftRT-WASM.cpp * [WASM] Emit empty swift5 sections if there aren't * [WASM] Remove swift_end and swift_start files --- .github/workflows/main.yml | 6 ++- CMakeLists.txt | 5 -- lib/IRGen/MetadataRequest.cpp | 16 ++++-- stdlib/public/runtime/CMakeLists.txt | 11 +++- stdlib/public/runtime/SwiftRT-WASM.cpp | 75 ++++++++++++++++++++++++++ swift_end.cpp | 30 ----------- swift_start.cpp | 30 ----------- 7 files changed, 101 insertions(+), 72 deletions(-) delete mode 100644 swift_end.cpp delete mode 100644 swift_start.cpp diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index ba665370d2b70..0950dff14adfa 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -49,6 +49,8 @@ jobs: -DSWIFT_SDKS='WASM;LINUX' \ -DSWIFT_BUILD_SOURCEKIT=FALSE \ -DSWIFT_ENABLE_SOURCEKIT_TESTS=FALSE \ + -DCMAKE_AR='$sourcedir/wasi-sdk/bin/llvm-ar' \ + -DCMAKE_RANLIB='$sourcedir/wasi-sdk/bin/llvm-ranlib' \ " \ --build-stdlib-deployment-targets "wasm-wasm32" \ --build-swift-static-stdlib \ @@ -73,7 +75,7 @@ jobs: - uses: actions/checkout@v1 - name: Run a multi-line script run: | - brew install cmake ninja + brew install cmake ninja llvm ./utils/update-checkout --clone --scheme wasm git checkout $GITHUB_SHA export sourcedir=$PWD/.. @@ -96,6 +98,8 @@ jobs: -DSWIFT_OSX_x86_64_ICU_STATICLIB=TRUE \ -DSWIFT_BUILD_SOURCEKIT=FALSE \ -DSWIFT_ENABLE_SOURCEKIT_TESTS=FALSE \ + -DCMAKE_AR='/usr/local/opt/llvm/bin/llvm-ar' \ + -DCMAKE_RANLIB='/usr/local/opt/llvm/bin/llvm-ranlib' \ " \ --build-stdlib-deployment-targets "wasm-wasm32" \ --build-swift-dynamic-sdk-overlay false \ diff --git a/CMakeLists.txt b/CMakeLists.txt index 55fe35799c22b..e068cc12d3c8d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -791,11 +791,6 @@ if(swift_build_wasm AND NOT "${SWIFT_HOST_VARIANT_SDK}" STREQUAL "WASM") # message(FATAL_ERROR "A Darwin or Linux host is required to build the Swift runtime for Android") #endif() - if ("${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "Linux") - # WebAssembly: hack: use llvm-ar for creating static libraries; Ubuntu's GNU ar doesn't work with wasm-ld - set(CMAKE_AR "${SWIFT_WASM_WASI_SDK_PATH}/bin/llvm-ar") - endif() - if("${SWIFT_SDK_WASM_ARCHITECTURES}" STREQUAL "") set(SWIFT_SDK_WASM_ARCHITECTURES wasm32) endif() diff --git a/lib/IRGen/MetadataRequest.cpp b/lib/IRGen/MetadataRequest.cpp index 3d8385d7db6e6..9e8f308e6f129 100644 --- a/lib/IRGen/MetadataRequest.cpp +++ b/lib/IRGen/MetadataRequest.cpp @@ -2414,10 +2414,18 @@ emitMetadataAccessByMangledName(IRGenFunction &IGF, CanType type, IGM.Int32Ty); stringAddrOffset = subIGF.Builder.CreateSExtOrBitCast(stringAddrOffset, IGM.SizeTy); - auto stringAddrBase = subIGF.Builder.CreatePtrToInt(cache, IGM.SizeTy); - if (IGM.getModule()->getDataLayout().isBigEndian()) { - stringAddrBase = subIGF.Builder.CreateAdd(stringAddrBase, - llvm::ConstantInt::get(IGM.SizeTy, 4)); + llvm::Value *stringAddr; + if (IGM.TargetInfo.OutputObjectFormat == llvm::Triple::Wasm) { + stringAddr = subIGF.Builder.CreateIntToPtr(stringAddrOffset, IGM.Int8PtrTy); + } else { + auto stringAddrBase = subIGF.Builder.CreatePtrToInt(cache, IGM.SizeTy); + if (IGM.getModule()->getDataLayout().isBigEndian()) { + stringAddrBase = subIGF.Builder.CreateAdd(stringAddrBase, + llvm::ConstantInt::get(IGM.SizeTy, 4)); + } + stringAddr = subIGF.Builder.CreateAdd(stringAddrBase, + stringAddrOffset); + stringAddr = subIGF.Builder.CreateIntToPtr(stringAddr, IGM.Int8PtrTy); } auto stringAddr = subIGF.Builder.CreateAdd(stringAddrBase, stringAddrOffset); diff --git a/stdlib/public/runtime/CMakeLists.txt b/stdlib/public/runtime/CMakeLists.txt index 475e78d15d427..f61e59a91e730 100644 --- a/stdlib/public/runtime/CMakeLists.txt +++ b/stdlib/public/runtime/CMakeLists.txt @@ -89,8 +89,15 @@ set(swift_runtime_library_compile_flags ${swift_runtime_compile_flags}) list(APPEND swift_runtime_library_compile_flags -DswiftCore_EXPORTS) list(APPEND swift_runtime_library_compile_flags -I${SWIFT_SOURCE_DIR}/include) -set(sdk "${SWIFT_HOST_VARIANT_SDK}") -if(SWIFT_BUILD_STATIC_STDLIB AND "${sdk}" STREQUAL "LINUX") +set(image_inspection_shared_sdk) +if(SWIFT_BUILD_STATIC_STDLIB AND "${SWIFT_HOST_VARIANT_SDK}" STREQUAL "LINUX") + set(image_inspection_shared_sdk "${SWIFT_HOST_VARIANT_SDK}") +elseif("${SWIFT_PRIMARY_VARIANT_SDK}" STREQUAL "WASM") + set(image_inspection_shared_sdk "${SWIFT_PRIMARY_VARIANT_SDK}") +endif() + +if(NOT "${image_inspection_shared_sdk}" STREQUAL "") + set(sdk "${image_inspection_shared_sdk}") list(REMOVE_ITEM swift_runtime_sources ImageInspectionELF.cpp) set(static_binary_lnk_file_list) string(TOLOWER "${sdk}" lowercase_sdk) diff --git a/stdlib/public/runtime/SwiftRT-WASM.cpp b/stdlib/public/runtime/SwiftRT-WASM.cpp index 6812763e9b1e7..1b4c84f3f2ae9 100644 --- a/stdlib/public/runtime/SwiftRT-WASM.cpp +++ b/stdlib/public/runtime/SwiftRT-WASM.cpp @@ -9,3 +9,78 @@ // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors // //===----------------------------------------------------------------------===// + +#include "ImageInspectionELF.h" + +#include + +// Create empty sections to ensure that the start/stop symbols are synthesized +// by the linker. Otherwise, we may end up with undefined symbol references as +// the linker table section was never constructed. + +#define DECLARE_SWIFT_SECTION(name) \ + __attribute__((__used__,__section__(#name),__aligned__(1))) const char __dummy_##name = 0x00; \ + __attribute__((__visibility__("hidden"),__aligned__(1))) extern const char __start_##name; \ + __attribute__((__visibility__("hidden"),__aligned__(1))) extern const char __stop_##name; + +extern "C" { +DECLARE_SWIFT_SECTION(swift5_protocols) +DECLARE_SWIFT_SECTION(swift5_protocol_conformances) +DECLARE_SWIFT_SECTION(swift5_type_metadata) + +DECLARE_SWIFT_SECTION(swift5_typeref) +DECLARE_SWIFT_SECTION(swift5_reflstr) +DECLARE_SWIFT_SECTION(swift5_fieldmd) +DECLARE_SWIFT_SECTION(swift5_assocty) +DECLARE_SWIFT_SECTION(swift5_replace) +DECLARE_SWIFT_SECTION(swift5_replac2) +DECLARE_SWIFT_SECTION(swift5_builtin) +DECLARE_SWIFT_SECTION(swift5_capture) +} + +#undef DECLARE_SWIFT_SECTION + +namespace { +static swift::MetadataSections sections{}; +} + +__attribute__((__constructor__)) +static void swift_image_constructor() { +#define SWIFT_SECTION_RANGE(name) \ + { reinterpret_cast(&__start_##name), \ + static_cast(&__stop_##name - &__start_##name) } + + sections = { + swift::CurrentSectionMetadataVersion, + 0, + + nullptr, + nullptr, + + SWIFT_SECTION_RANGE(swift5_protocols), + SWIFT_SECTION_RANGE(swift5_protocol_conformances), + SWIFT_SECTION_RANGE(swift5_type_metadata), + + SWIFT_SECTION_RANGE(swift5_typeref), + SWIFT_SECTION_RANGE(swift5_reflstr), + SWIFT_SECTION_RANGE(swift5_fieldmd), + SWIFT_SECTION_RANGE(swift5_assocty), + SWIFT_SECTION_RANGE(swift5_replace), + SWIFT_SECTION_RANGE(swift5_replac2), + SWIFT_SECTION_RANGE(swift5_builtin), + SWIFT_SECTION_RANGE(swift5_capture), + }; + +#undef SWIFT_SECTION_RANGE + + swift_addNewDSOImage(§ions); +} + +static __attribute__((__used__)) +__attribute__((__section__(".note.swift_reflection_metadata"))) +__attribute__((__aligned__(1))) +struct { + const char MagicString[sizeof(SWIFT_REFLECTION_METADATA_ELF_NOTE_MAGIC_STRING)]; + const swift::MetadataSections *Sections; +} __attribute__((__packed__)) +Note = {SWIFT_REFLECTION_METADATA_ELF_NOTE_MAGIC_STRING, §ions}; diff --git a/swift_end.cpp b/swift_end.cpp deleted file mode 100644 index b3d476ac41305..0000000000000 --- a/swift_end.cpp +++ /dev/null @@ -1,30 +0,0 @@ -//===--- SwiftRT-ELF.cpp --------------------------------------------------===// -// -// This source file is part of the Swift.org open source project -// -// Copyright (c) 2014 - 2017 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 -// -//===----------------------------------------------------------------------===// - -#include - -// We synthesize the start/stop symbols ourselves. -#define DECLARE_SWIFT_SECTION(name) \ - __attribute__((__section__(#name),__visibility__("hidden"),__aligned__(1))) const void* __stop_##name = (void*)0xfacefeed; \ - -extern "C" { -DECLARE_SWIFT_SECTION(swift5_protocols) -DECLARE_SWIFT_SECTION(swift5_protocol_conformances) -DECLARE_SWIFT_SECTION(swift5_type_metadata) - -DECLARE_SWIFT_SECTION(swift5_typeref) -DECLARE_SWIFT_SECTION(swift5_reflstr) -DECLARE_SWIFT_SECTION(swift5_fieldmd) -DECLARE_SWIFT_SECTION(swift5_assocty) -DECLARE_SWIFT_SECTION(swift5_replace) -DECLARE_SWIFT_SECTION(swift5_replac2) -} diff --git a/swift_start.cpp b/swift_start.cpp deleted file mode 100644 index a9cf1faa390b3..0000000000000 --- a/swift_start.cpp +++ /dev/null @@ -1,30 +0,0 @@ -//===--- SwiftRT-ELF.cpp --------------------------------------------------===// -// -// This source file is part of the Swift.org open source project -// -// Copyright (c) 2014 - 2017 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 -// -//===----------------------------------------------------------------------===// - -#include - -// We synthesize the start/stop symbols ourselves. -#define DECLARE_SWIFT_SECTION(name) \ - __attribute__((__section__(#name),__visibility__("hidden"),__aligned__(1))) const void* __start_##name = (void*)0xdeadbeef; \ - -extern "C" { -DECLARE_SWIFT_SECTION(swift5_protocols) -DECLARE_SWIFT_SECTION(swift5_protocol_conformances) -DECLARE_SWIFT_SECTION(swift5_type_metadata) - -DECLARE_SWIFT_SECTION(swift5_typeref) -DECLARE_SWIFT_SECTION(swift5_reflstr) -DECLARE_SWIFT_SECTION(swift5_fieldmd) -DECLARE_SWIFT_SECTION(swift5_assocty) -DECLARE_SWIFT_SECTION(swift5_replace) -DECLARE_SWIFT_SECTION(swift5_replac2) -} From aebc3d507039dd3dc307e2cf8aa95dbbb90592ef Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Tue, 12 Nov 2019 14:55:50 +0000 Subject: [PATCH 55/80] Move CI build commands to separate scripts (#13) This allows running build commands in a reproducible way either locally or with any other CI. * Move CI build commands to separate scripts * Run CI on pushes to `swiftwasm` branch * Split build and setup steps into separate scrtipts * Fix execution directories in the build scripts --- .github/workflows/main.yml | 97 ++------------------------------------ build-linux.sh | 25 ++++++++++ build-mac.sh | 25 ++++++++++ ci-linux.sh | 33 +++++++++++++ ci-mac.sh | 20 ++++++++ 5 files changed, 108 insertions(+), 92 deletions(-) create mode 100755 build-linux.sh create mode 100755 build-mac.sh create mode 100755 ci-linux.sh create mode 100755 ci-mac.sh diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 0950dff14adfa..9ca46da5c1f08 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -1,6 +1,9 @@ name: CI on: + push: + branches: + - swiftwasm pull_request: branches: - swiftwasm @@ -13,59 +16,7 @@ jobs: steps: - uses: actions/checkout@v1 - name: Run a multi-line script - run: | - sudo apt update - sudo apt install \ - git ninja-build clang python \ - uuid-dev libicu-dev icu-devtools libbsd-dev \ - libedit-dev libxml2-dev libsqlite3-dev swig \ - libpython-dev libncurses5-dev pkg-config \ - libblocksruntime-dev libcurl4-openssl-dev \ - systemtap-sdt-dev tzdata rsync - - ./utils/update-checkout --clone --scheme wasm - git checkout $GITHUB_SHA - export sourcedir=$PWD/.. - cd $sourcedir - - wget -O install_cmake.sh "https://github.com/Kitware/CMake/releases/download/v3.15.3/cmake-3.15.3-Linux-x86_64.sh" - chmod +x install_cmake.sh - sudo mkdir -p /opt/cmake - sudo ./install_cmake.sh --skip-license --prefix=/opt/cmake - sudo ln -sf /opt/cmake/bin/* /usr/local/bin - cmake --version - - wget -O wasi-sdk.tar.gz https://github.com/swiftwasm/wasi-sdk/releases/download/20190421.6/wasi-sdk-3.19gefb17cb478f9.m-linux.tar.gz - tar xfz wasi-sdk.tar.gz - mv wasi-sdk-3.19gefb17cb478f9+m/opt/wasi-sdk ./wasi-sdk - - wget -O icu.tar.xz "https://github.com/swiftwasm/icu4c-wasi/releases/download/20190421.3/icu4c-wasi.tar.xz" - tar xf icu.tar.xz - - cd swift - utils/build-script --release --wasm --verbose \ - --skip-build-benchmarks \ - --extra-cmake-options=" \ - -DSWIFT_SDKS='WASM;LINUX' \ - -DSWIFT_BUILD_SOURCEKIT=FALSE \ - -DSWIFT_ENABLE_SOURCEKIT_TESTS=FALSE \ - -DCMAKE_AR='$sourcedir/wasi-sdk/bin/llvm-ar' \ - -DCMAKE_RANLIB='$sourcedir/wasi-sdk/bin/llvm-ranlib' \ - " \ - --build-stdlib-deployment-targets "wasm-wasm32" \ - --build-swift-static-stdlib \ - --install-destdir="$sourcedir/install" \ - --install-prefix="/opt/swiftwasm-sdk" \ - --install-swift \ - --installable-package="$sourcedir/swiftwasm.tar.gz" \ - --llvm-targets-to-build "X86;WebAssembly" \ - --stdlib-deployment-targets "wasm-wasm32" \ - --wasm-icu-data "todo-icu-data" \ - --wasm-icu-i18n "$sourcedir/icu_out/lib" \ - --wasm-icu-i18n-include "$sourcedir/icu_out/include" \ - --wasm-icu-uc "$sourcedir/icu_out/lib" \ - --wasm-icu-uc-include "$sourcedir/icu_out/include" \ - --wasm-wasi-sdk "$sourcedir/wasi-sdk" + run: ./ci-linux.sh macos_build: timeout-minutes: 0 @@ -74,42 +25,4 @@ jobs: steps: - uses: actions/checkout@v1 - name: Run a multi-line script - run: | - brew install cmake ninja llvm - ./utils/update-checkout --clone --scheme wasm - git checkout $GITHUB_SHA - export sourcedir=$PWD/.. - cd $sourcedir - wget -O wasi-sdk.tar.gz https://github.com/swiftwasm/wasi-sdk/releases/download/20190421.6/wasi-sdk-3.19gefb17cb478f9.m-linux.tar.gz - tar xfz wasi-sdk.tar.gz - mv wasi-sdk-3.19gefb17cb478f9+m/opt/wasi-sdk ./wasi-sdk - # Link sysroot/usr/include to sysroot/include because Darwin sysroot doesn't - # find header files in sysroot/include but sysroot/usr/include - mkdir wasi-sdk/share/sysroot/usr/ - ln -s ../include wasi-sdk/share/sysroot/usr/include - wget -O icu.tar.xz "https://github.com/swiftwasm/icu4c-wasi/releases/download/20190421.3/icu4c-wasi.tar.xz" - tar xf icu.tar.xz - cd swift - ./utils/build-script --release --wasm --verbose \ - --skip-build-benchmarks \ - --extra-cmake-options=" \ - -DSWIFT_PRIMARY_VARIANT_SDK:STRING=WASM \ - -DSWIFT_PRIMARY_VARIANT_ARCH:STRING=wasm32 \ - -DSWIFT_OSX_x86_64_ICU_STATICLIB=TRUE \ - -DSWIFT_BUILD_SOURCEKIT=FALSE \ - -DSWIFT_ENABLE_SOURCEKIT_TESTS=FALSE \ - -DCMAKE_AR='/usr/local/opt/llvm/bin/llvm-ar' \ - -DCMAKE_RANLIB='/usr/local/opt/llvm/bin/llvm-ranlib' \ - " \ - --build-stdlib-deployment-targets "wasm-wasm32" \ - --build-swift-dynamic-sdk-overlay false \ - --build-swift-static-sdk-overlay false \ - --build-swift-static-stdlib \ - --llvm-targets-to-build "X86;WebAssembly" \ - --stdlib-deployment-targets "wasm-wasm32" \ - --wasm-icu-data "todo-icu-data" \ - --wasm-icu-i18n "$sourcedir/icu_out/lib" \ - --wasm-icu-i18n-include "$sourcedir/icu_out/include" \ - --wasm-icu-uc "$sourcedir/icu_out/lib" \ - --wasm-icu-uc-include "$sourcedir/icu_out/include" \ - --wasm-wasi-sdk "$sourcedir/wasi-sdk" + run: ./ci-mac.sh diff --git a/build-linux.sh b/build-linux.sh new file mode 100755 index 0000000000000..7d91a1abd9b57 --- /dev/null +++ b/build-linux.sh @@ -0,0 +1,25 @@ +#/bin/bash + +utils/build-script --release --wasm --verbose \ + --skip-build-benchmarks \ + --extra-cmake-options=" \ + -DSWIFT_SDKS='WASM;LINUX' \ + -DSWIFT_BUILD_SOURCEKIT=FALSE \ + -DSWIFT_ENABLE_SOURCEKIT_TESTS=FALSE \ + -DCMAKE_AR='$sourcedir/wasi-sdk/bin/llvm-ar' \ + -DCMAKE_RANLIB='$sourcedir/wasi-sdk/bin/llvm-ranlib' \ + " \ + --build-stdlib-deployment-targets "wasm-wasm32" \ + --build-swift-static-stdlib \ + --install-destdir="$sourcedir/install" \ + --install-prefix="/opt/swiftwasm-sdk" \ + --install-swift \ + --installable-package="$sourcedir/swiftwasm.tar.gz" \ + --llvm-targets-to-build "X86;WebAssembly" \ + --stdlib-deployment-targets "wasm-wasm32" \ + --wasm-icu-data "todo-icu-data" \ + --wasm-icu-i18n "$sourcedir/icu_out/lib" \ + --wasm-icu-i18n-include "$sourcedir/icu_out/include" \ + --wasm-icu-uc "$sourcedir/icu_out/lib" \ + --wasm-icu-uc-include "$sourcedir/icu_out/include" \ + --wasm-wasi-sdk "$sourcedir/wasi-sdk" diff --git a/build-mac.sh b/build-mac.sh new file mode 100755 index 0000000000000..741b394981492 --- /dev/null +++ b/build-mac.sh @@ -0,0 +1,25 @@ +#/bin/bash + +./utils/build-script --release --wasm --verbose \ + --skip-build-benchmarks \ + --extra-cmake-options=" \ + -DSWIFT_PRIMARY_VARIANT_SDK:STRING=WASM \ + -DSWIFT_PRIMARY_VARIANT_ARCH:STRING=wasm32 \ + -DSWIFT_OSX_x86_64_ICU_STATICLIB=TRUE \ + -DSWIFT_BUILD_SOURCEKIT=FALSE \ + -DSWIFT_ENABLE_SOURCEKIT_TESTS=FALSE \ + -DCMAKE_AR='/usr/local/opt/llvm/bin/llvm-ar' \ + -DCMAKE_RANLIB='/usr/local/opt/llvm/bin/llvm-ranlib' \ + " \ + --build-stdlib-deployment-targets "wasm-wasm32" \ + --build-swift-dynamic-sdk-overlay false \ + --build-swift-static-sdk-overlay false \ + --build-swift-static-stdlib \ + --llvm-targets-to-build "X86;WebAssembly" \ + --stdlib-deployment-targets "wasm-wasm32" \ + --wasm-icu-data "todo-icu-data" \ + --wasm-icu-i18n "$sourcedir/icu_out/lib" \ + --wasm-icu-i18n-include "$sourcedir/icu_out/include" \ + --wasm-icu-uc "$sourcedir/icu_out/lib" \ + --wasm-icu-uc-include "$sourcedir/icu_out/include" \ + --wasm-wasi-sdk "$sourcedir/wasi-sdk" diff --git a/ci-linux.sh b/ci-linux.sh new file mode 100755 index 0000000000000..18953fb7ce1f5 --- /dev/null +++ b/ci-linux.sh @@ -0,0 +1,33 @@ +#/bin/bash + +sudo apt update +sudo apt install \ + git ninja-build clang python \ + uuid-dev libicu-dev icu-devtools libbsd-dev \ + libedit-dev libxml2-dev libsqlite3-dev swig \ + libpython-dev libncurses5-dev pkg-config \ + libblocksruntime-dev libcurl4-openssl-dev \ + systemtap-sdt-dev tzdata rsync + +export current_sha=`git rev-parse HEAD` +./utils/update-checkout --clone --scheme wasm +git checkout $current_sha +export sourcedir=$PWD/.. +cd $sourcedir + +wget -O install_cmake.sh "https://github.com/Kitware/CMake/releases/download/v3.15.3/cmake-3.15.3-Linux-x86_64.sh" +chmod +x install_cmake.sh +sudo mkdir -p /opt/cmake +sudo ./install_cmake.sh --skip-license --prefix=/opt/cmake +sudo ln -sf /opt/cmake/bin/* /usr/local/bin +cmake --version + +wget -O wasi-sdk.tar.gz https://github.com/swiftwasm/wasi-sdk/releases/download/20190421.6/wasi-sdk-3.19gefb17cb478f9.m-linux.tar.gz +tar xfz wasi-sdk.tar.gz +mv wasi-sdk-3.19gefb17cb478f9+m/opt/wasi-sdk ./wasi-sdk + +wget -O icu.tar.xz "https://github.com/swiftwasm/icu4c-wasi/releases/download/20190421.3/icu4c-wasi.tar.xz" +tar xf icu.tar.xz + +cd swift +./build-linux.sh diff --git a/ci-mac.sh b/ci-mac.sh new file mode 100755 index 0000000000000..dc8bab5527073 --- /dev/null +++ b/ci-mac.sh @@ -0,0 +1,20 @@ +#/bin/bash + +brew install cmake ninja llvm +export current_sha=`git rev-parse HEAD` +./utils/update-checkout --clone --scheme wasm +git checkout $current_sha +export sourcedir=$PWD/.. +cd $sourcedir +wget -O wasi-sdk.tar.gz https://github.com/swiftwasm/wasi-sdk/releases/download/20190421.6/wasi-sdk-3.19gefb17cb478f9.m-linux.tar.gz +tar xfz wasi-sdk.tar.gz +mv wasi-sdk-3.19gefb17cb478f9+m/opt/wasi-sdk ./wasi-sdk +# Link sysroot/usr/include to sysroot/include because Darwin sysroot doesn't +# find header files in sysroot/include but sysroot/usr/include +mkdir wasi-sdk/share/sysroot/usr/ +ln -s ../include wasi-sdk/share/sysroot/usr/include +wget -O icu.tar.xz "https://github.com/swiftwasm/icu4c-wasi/releases/download/20190421.3/icu4c-wasi.tar.xz" +tar xf icu.tar.xz + +cd swift +./build-mac.sh From d41ecb23e785be0b42db0a4450203ae3e9432b78 Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Tue, 12 Nov 2019 14:59:29 +0000 Subject: [PATCH 56/80] Install wget in ci-linux.sh, make path explicit in build-linux.sh --- build-linux.sh | 2 +- ci-linux.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/build-linux.sh b/build-linux.sh index 7d91a1abd9b57..720a67971c21e 100755 --- a/build-linux.sh +++ b/build-linux.sh @@ -1,6 +1,6 @@ #/bin/bash -utils/build-script --release --wasm --verbose \ +./utils/build-script --release --wasm --verbose \ --skip-build-benchmarks \ --extra-cmake-options=" \ -DSWIFT_SDKS='WASM;LINUX' \ diff --git a/ci-linux.sh b/ci-linux.sh index 18953fb7ce1f5..1c8da320bc53f 100755 --- a/ci-linux.sh +++ b/ci-linux.sh @@ -7,7 +7,7 @@ sudo apt install \ libedit-dev libxml2-dev libsqlite3-dev swig \ libpython-dev libncurses5-dev pkg-config \ libblocksruntime-dev libcurl4-openssl-dev \ - systemtap-sdt-dev tzdata rsync + systemtap-sdt-dev tzdata rsync wget export current_sha=`git rev-parse HEAD` ./utils/update-checkout --clone --scheme wasm From dbd6ee552b491ff0d7818aeb897da97a0b090a2c Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Thu, 14 Nov 2019 08:17:57 +0000 Subject: [PATCH 57/80] Update WASI SDK, create installable package on macOS (#14) New WASI SDK package should contain a new version of the linker. Also, the script should now create an installable package on macOS so that one could create a full SwiftWasm package later after the build. * Update WASI SDK, create installable package on macOS * Fix sysroot path, update wasi-sdk on Linux * Exclude module net for wasm SDK in glibc.modulemap * Remove module termios from glic.modulemap for wasm * Disable _stdlib_mkstemps for wasm --- build-linux.sh | 2 ++ build-mac.sh | 8 +++++++- ci-linux.sh | 5 +++-- ci-mac.sh | 5 +++-- .../SwiftPrivateLibcExtras/SwiftPrivateLibcExtras.swift | 2 +- stdlib/public/Platform/glibc.modulemap.gyb | 4 ++++ 6 files changed, 20 insertions(+), 6 deletions(-) diff --git a/build-linux.sh b/build-linux.sh index 720a67971c21e..285a5b05a3c9c 100755 --- a/build-linux.sh +++ b/build-linux.sh @@ -1,5 +1,7 @@ #/bin/bash +export sourcedir=$PWD/.. + ./utils/build-script --release --wasm --verbose \ --skip-build-benchmarks \ --extra-cmake-options=" \ diff --git a/build-mac.sh b/build-mac.sh index 741b394981492..2751f98df442c 100755 --- a/build-mac.sh +++ b/build-mac.sh @@ -1,5 +1,7 @@ #/bin/bash +export sourcedir=$PWD/.. + ./utils/build-script --release --wasm --verbose \ --skip-build-benchmarks \ --extra-cmake-options=" \ @@ -22,4 +24,8 @@ --wasm-icu-i18n-include "$sourcedir/icu_out/include" \ --wasm-icu-uc "$sourcedir/icu_out/lib" \ --wasm-icu-uc-include "$sourcedir/icu_out/include" \ - --wasm-wasi-sdk "$sourcedir/wasi-sdk" + --wasm-wasi-sdk "$sourcedir/wasi-sdk" \ + --install-swift \ + --install-prefix="/opt/swiftwasm-sdk" \ + --install-destdir="$sourcedir/install" \ + --installable-package="$sourcedir/swiftwasm-mac.tar.gz" diff --git a/ci-linux.sh b/ci-linux.sh index 1c8da320bc53f..467e38b1f1403 100755 --- a/ci-linux.sh +++ b/ci-linux.sh @@ -22,9 +22,10 @@ sudo ./install_cmake.sh --skip-license --prefix=/opt/cmake sudo ln -sf /opt/cmake/bin/* /usr/local/bin cmake --version -wget -O wasi-sdk.tar.gz https://github.com/swiftwasm/wasi-sdk/releases/download/20190421.6/wasi-sdk-3.19gefb17cb478f9.m-linux.tar.gz +wget -O wasi-sdk.tar.gz https://github.com/swiftwasm/wasi-sdk/releases/download/20191022.1/wasi-sdk-4.39g3025a5f47c04-linux.tar.gz tar xfz wasi-sdk.tar.gz -mv wasi-sdk-3.19gefb17cb478f9+m/opt/wasi-sdk ./wasi-sdk +mv wasi-sdk-4.39g3025a5f47c04 ./wasi-sdk +mv wasi-sdk/share/wasi-sysroot wasi-sdk/share/sysroot wget -O icu.tar.xz "https://github.com/swiftwasm/icu4c-wasi/releases/download/20190421.3/icu4c-wasi.tar.xz" tar xf icu.tar.xz diff --git a/ci-mac.sh b/ci-mac.sh index dc8bab5527073..d3a08d7164af9 100755 --- a/ci-mac.sh +++ b/ci-mac.sh @@ -6,9 +6,10 @@ export current_sha=`git rev-parse HEAD` git checkout $current_sha export sourcedir=$PWD/.. cd $sourcedir -wget -O wasi-sdk.tar.gz https://github.com/swiftwasm/wasi-sdk/releases/download/20190421.6/wasi-sdk-3.19gefb17cb478f9.m-linux.tar.gz +wget -O wasi-sdk.tar.gz https://github.com/swiftwasm/wasi-sdk/releases/download/20191022.1/wasi-sdk-4.39g3025a5f47c04-linux.tar.gz tar xfz wasi-sdk.tar.gz -mv wasi-sdk-3.19gefb17cb478f9+m/opt/wasi-sdk ./wasi-sdk +mv wasi-sdk-4.39g3025a5f47c04 ./wasi-sdk +mv wasi-sdk/share/wasi-sysroot wasi-sdk/share/sysroot # Link sysroot/usr/include to sysroot/include because Darwin sysroot doesn't # find header files in sysroot/include but sysroot/usr/include mkdir wasi-sdk/share/sysroot/usr/ diff --git a/stdlib/private/SwiftPrivateLibcExtras/SwiftPrivateLibcExtras.swift b/stdlib/private/SwiftPrivateLibcExtras/SwiftPrivateLibcExtras.swift index 8c09674b4faae..6042ba6550ad8 100644 --- a/stdlib/private/SwiftPrivateLibcExtras/SwiftPrivateLibcExtras.swift +++ b/stdlib/private/SwiftPrivateLibcExtras/SwiftPrivateLibcExtras.swift @@ -20,7 +20,7 @@ import MSVCRT #endif public func _stdlib_mkstemps(_ template: inout String, _ suffixlen: CInt) -> CInt { -#if os(Android) || os(Haiku) || os(Windows) +#if os(Android) || os(Haiku) || os(Windows) || os(Wasm) preconditionFailure("mkstemps doesn't work on your platform") #else var utf8CStr = template.utf8CString diff --git a/stdlib/public/Platform/glibc.modulemap.gyb b/stdlib/public/Platform/glibc.modulemap.gyb index 97c52fe1ce4fa..eba7fde86588a 100644 --- a/stdlib/public/Platform/glibc.modulemap.gyb +++ b/stdlib/public/Platform/glibc.modulemap.gyb @@ -353,12 +353,14 @@ module SwiftGlibc [system] { header "${GLIBC_INCLUDE_PATH}/libgen.h" export * } +% if CMAKE_SDK != "WASM": module net { module if { header "${GLIBC_INCLUDE_PATH}/net/if.h" export * } } +% end module netinet { module in { header "${GLIBC_INCLUDE_PATH}/netinet/in.h" @@ -517,10 +519,12 @@ module SwiftGlibc [system] { export * } % end +% if CMAKE_SDK != "WASM": module termios { header "${GLIBC_INCLUDE_PATH}/termios.h" export * } +% end module unistd { header "${GLIBC_INCLUDE_PATH}/unistd.h" export * From 3d08a9f3284e99e644e44084a31a7c1c1c9fe425 Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Fri, 15 Nov 2019 12:36:29 +0000 Subject: [PATCH 58/80] Make SymbolLookup.swift compilable for wasm (#16) `SymbolLookup.swift` was added recently, which broke our builds after rebasing on top of Apple's `master`. Using anything from this file wouldn't make sense on WebAssembly. It looks there are no codepaths that would call it on that platform, but there are still references to it around, so it has to be at least compilable. --- stdlib/private/StdlibUnittest/SymbolLookup.swift | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/stdlib/private/StdlibUnittest/SymbolLookup.swift b/stdlib/private/StdlibUnittest/SymbolLookup.swift index 2e9c627487a4f..6967c11327e0e 100644 --- a/stdlib/private/StdlibUnittest/SymbolLookup.swift +++ b/stdlib/private/StdlibUnittest/SymbolLookup.swift @@ -12,7 +12,7 @@ #if os(macOS) || os(iOS) || os(tvOS) || os(watchOS) import Darwin -#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku) +#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku) || os(Wasm) import Glibc #elseif os(Windows) import MSVCRT @@ -23,7 +23,7 @@ #if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) let RTLD_DEFAULT = UnsafeMutableRawPointer(bitPattern: -2) -#elseif os(Linux) +#elseif os(Linux) || os(Wasm) let RTLD_DEFAULT = UnsafeMutableRawPointer(bitPattern: 0) #elseif os(Android) #if arch(arm) || arch(i386) @@ -43,6 +43,8 @@ public func pointerToSwiftCoreSymbol(name: String) -> UnsafeMutableRawPointer? { #if os(Windows) return unsafeBitCast(GetProcAddress(hStdlibCore, name), to: UnsafeMutableRawPointer?.self) +#elseif os(Wasm) + fatalError("\(#function) is not supported on WebAssembly") #else return dlsym(RTLD_DEFAULT, name) #endif From a732d98a0e770d461c20a55c39536f17e7927f7e Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Sat, 16 Nov 2019 09:52:08 +0000 Subject: [PATCH 59/80] Run packaging scripts and smoke test on CI (#15) Any changes in this repository should be tested with the packaging script and a basic smoke test that compiles `hello.swift` on CI. In the future packaging and linking scripts should be moved to this repository, `swiftwasm-sdk` and `swiftwasm-package-sdk` projects probably would be archived when that happens. For now we're pulling the scripts from `swiftwasm-package-sdk` as it is. * Run packaging scripts and smoke test on CI * Make prepare-package.sh executable * Make SymbolLookup.swift compilable for wasm * Use GitHub Actions upload/download steps * Remove packaging steps from ci-*.sh * Move prepare-package.sh to main.yml to avoid clone * Refine formatting in .github/workflows/main.yml --- .github/workflows/main.yml | 46 ++++++++++++++++++++++++++++++++++++-- build-linux.sh | 2 +- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 9ca46da5c1f08..6ece20e7139ad 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -15,8 +15,13 @@ jobs: steps: - uses: actions/checkout@v1 - - name: Run a multi-line script + - name: Build Linux installable archive run: ./ci-linux.sh + - name: Upload Linux installable archive + uses: actions/upload-artifact@v1 + with: + name: linux-installable + path: ../swiftwasm-linux.tar.gz macos_build: timeout-minutes: 0 @@ -24,5 +29,42 @@ jobs: steps: - uses: actions/checkout@v1 - - name: Run a multi-line script + - name: Build macOS installable archive run: ./ci-mac.sh + - name: Upload macOS installable archive + uses: actions/upload-artifact@v1 + with: + name: macos-installable + path: ../swiftwasm-mac.tar.gz + + package: + name: Build SwiftWasm packages + needs: + - linux_build + - macos_build + runs-on: ubuntu-18.04 + steps: + - name: Download installable Linux archive + uses: actions/download-artifact@v1 + with: + name: linux-installable + - name: Download installable macOS archive + uses: actions/download-artifact@v1 + with: + name: macos-installable + - name: Build the packages + shell: bash + run: | + git clone https://github.com/swiftwasm/swiftwasm-package-sdk.git + cd swiftwasm-package-sdk + ./download-prebuilts.sh + + cp ../linux-installable/swiftwasm-linux.tar.gz prebuilt/swiftwasm.tar.gz + cp ../macos-installable/swiftwasm-mac.tar.gz prebuilt/swiftwasm-mac.tar.gz + ./build-packages.sh + + cd output + tar xf swiftwasm-sdk-linux.tar.xz + + cd swiftwasm-sdk + ./swiftwasm example/hello.swift hello.wasm diff --git a/build-linux.sh b/build-linux.sh index 285a5b05a3c9c..30285c85a2dc9 100755 --- a/build-linux.sh +++ b/build-linux.sh @@ -16,7 +16,7 @@ export sourcedir=$PWD/.. --install-destdir="$sourcedir/install" \ --install-prefix="/opt/swiftwasm-sdk" \ --install-swift \ - --installable-package="$sourcedir/swiftwasm.tar.gz" \ + --installable-package="$sourcedir/swiftwasm-linux.tar.gz" \ --llvm-targets-to-build "X86;WebAssembly" \ --stdlib-deployment-targets "wasm-wasm32" \ --wasm-icu-data "todo-icu-data" \ From c55891d109e2add614c7ff242eaf69d7edb97aa0 Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Sun, 17 Nov 2019 09:52:55 +0000 Subject: [PATCH 60/80] Upload SwiftWasm artifacts for later inspection (#17) It would be convenient if packaged SDK and result of the smoke test were uploaded as artifacts of the CI run and become downloadable for later use. * Add more logging to the smoke test * Add a newline to main.yml * Upload packages and hello.wasm as CI artifacts * Normalize filenames for swiftwasm-package-sdk * Add macos_smoke_test job --- .github/workflows/main.yml | 54 ++++++++++++++++++++++++++++++++++---- build-mac.sh | 2 +- 2 files changed, 50 insertions(+), 6 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 6ece20e7139ad..8c81e1643c433 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -35,7 +35,7 @@ jobs: uses: actions/upload-artifact@v1 with: name: macos-installable - path: ../swiftwasm-mac.tar.gz + path: ../swiftwasm-macos.tar.gz package: name: Build SwiftWasm packages @@ -59,12 +59,56 @@ jobs: cd swiftwasm-package-sdk ./download-prebuilts.sh - cp ../linux-installable/swiftwasm-linux.tar.gz prebuilt/swiftwasm.tar.gz - cp ../macos-installable/swiftwasm-mac.tar.gz prebuilt/swiftwasm-mac.tar.gz + cp ../linux-installable/swiftwasm-linux.tar.gz \ + ../macos-installable/swiftwasm-macos.tar.gz \ + prebuilt ./build-packages.sh cd output - tar xf swiftwasm-sdk-linux.tar.xz + tar xf swiftwasm-sdk-linux.tar.xz && echo "Successfully unpacked Linux SDK" cd swiftwasm-sdk - ./swiftwasm example/hello.swift hello.wasm + ./swiftwasm example/hello.swift hello.wasm && echo "Successfully linked hello.wasm" + + - name: Upload macOS package + uses: actions/upload-artifact@v1 + with: + name: macos-package + path: swiftwasm-package-sdk/output/swiftwasm-sdk-macos.tar.xz + + - name: Upload Linux package + uses: actions/upload-artifact@v1 + with: + name: linux-package + path: swiftwasm-package-sdk/output/swiftwasm-sdk-linux.tar.xz + + - name: Upload hello.wasm compiled with Linux package + uses: actions/upload-artifact@v1 + with: + name: linux-hello.wasm + path: swiftwasm-package-sdk/output/swiftwasm-sdk/hello.wasm + + macos_smoke_test: + name: Compile hello.swift on macOS + runs-on: macOS-10.14 + needs: package + steps: + - name: Download SwiftWasm macOS package + uses: actions/download-artifact@v1 + with: + name: macos-package + + - name: Build hello.wasm + shell: bash + run: | + cd macos-package + tar xf swiftwasm-sdk-macos.tar.xz && echo "Successfully unpacked macOS SDK" + + cd swiftwasm-sdk + ./swiftwasm example/hello.swift hello.wasm && echo "Successfully linked hello.wasm" + + - name: Upload hello.wasm compiled with macOS package + uses: actions/upload-artifact@v1 + with: + name: macos-hello.wasm + path: macos-package/swiftwasm-sdk/hello.wasm diff --git a/build-mac.sh b/build-mac.sh index 2751f98df442c..aee098f9703cc 100755 --- a/build-mac.sh +++ b/build-mac.sh @@ -28,4 +28,4 @@ export sourcedir=$PWD/.. --install-swift \ --install-prefix="/opt/swiftwasm-sdk" \ --install-destdir="$sourcedir/install" \ - --installable-package="$sourcedir/swiftwasm-mac.tar.gz" + --installable-package="$sourcedir/swiftwasm-macos.tar.gz" From c897d93ee4ca3148ac183c0e2c7f608b020e4a64 Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Tue, 19 Nov 2019 13:16:40 +0000 Subject: [PATCH 61/80] Move packaging scripts from swiftwasm-package-sdk (#18) Currently our CI scripts rely on validity of scripts in the [`swiftwasm-package-sdk`](https://github.com/swiftwasm/swiftwasm-package-sdk) repository. That can't always be the case, especialy as we don't checkout a specific commit in that repository. And even if we did, managing this isn't convenient. Adding a submodule has its own set of issues and I personally think that a monorepo approach is much simpler for a small set of scripts, at least at an early stage. In fact, it would make sense to have these scripts in the upstream [`swift`](https://github.com/apple/swift) repository at some point, similarly to [what Android people have previously done](https://github.com/apple/swift/tree/master/utils/android). Thus, these scripts and a few small helper files are copied to `utils/webassembly` directory and are executed directly on CI. After this PR is merged, I don't see a particular need for our [`swiftwasm-package-sdk`](https://github.com/swiftwasm/swiftwasm-package-sdk) and [`swiftwasm-sdk`](https://github.com/swiftwasm/swiftwasm-sdk) repos, those probably can be archived. As a small cleanup addition, `.github/workflows/main.yml` file now has consistent indentation. * Move packaging scripts from swiftwasm-package-sdk * Rename `wasm` directory to `webassembly` * Make all .sh scripts executable after download * Make sdkroot/swiftwasm script executable * Add newline to build-mac-package.sh * Add newline to build-packages.sh * Remove swift_start.o and swift_end.o --- .github/workflows/main.yml | 60 +++++++----- utils/webassembly/.gitignore | 5 + utils/webassembly/README.md | 23 +++++ utils/webassembly/build-linux-package.sh | 9 ++ utils/webassembly/build-mac-package.sh | 9 ++ utils/webassembly/build-packages.sh | 5 + utils/webassembly/copy-shared-files.sh | 4 + .../download-installable-prebuilts.sh | 7 ++ utils/webassembly/download-prebuilts.sh | 8 ++ utils/webassembly/linux/unpack-prebuilts.sh | 20 ++++ utils/webassembly/macos/unpack-prebuilts.sh | 30 ++++++ utils/webassembly/remove-swift-extra-files.sh | 26 ++++++ utils/webassembly/remove-wasi-extra-files.sh | 10 ++ utils/webassembly/sdkroot/README.md | 87 ++++++++++++++++++ utils/webassembly/sdkroot/example/hello.swift | 1 + .../sdkroot/extra_objs/fakelocaltime.o | Bin 0 -> 423 bytes .../sdkroot/extra_objs/fakepthread.o | Bin 0 -> 4061 bytes .../sdkroot/extra_utils/generateModulemap.sh | 2 + utils/webassembly/sdkroot/swiftwasm | 40 ++++++++ 19 files changed, 321 insertions(+), 25 deletions(-) create mode 100644 utils/webassembly/.gitignore create mode 100644 utils/webassembly/README.md create mode 100755 utils/webassembly/build-linux-package.sh create mode 100755 utils/webassembly/build-mac-package.sh create mode 100755 utils/webassembly/build-packages.sh create mode 100755 utils/webassembly/copy-shared-files.sh create mode 100755 utils/webassembly/download-installable-prebuilts.sh create mode 100755 utils/webassembly/download-prebuilts.sh create mode 100755 utils/webassembly/linux/unpack-prebuilts.sh create mode 100755 utils/webassembly/macos/unpack-prebuilts.sh create mode 100755 utils/webassembly/remove-swift-extra-files.sh create mode 100755 utils/webassembly/remove-wasi-extra-files.sh create mode 100644 utils/webassembly/sdkroot/README.md create mode 100644 utils/webassembly/sdkroot/example/hello.swift create mode 100644 utils/webassembly/sdkroot/extra_objs/fakelocaltime.o create mode 100644 utils/webassembly/sdkroot/extra_objs/fakepthread.o create mode 100755 utils/webassembly/sdkroot/extra_utils/generateModulemap.sh create mode 100755 utils/webassembly/sdkroot/swiftwasm diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 8c81e1643c433..46d9785712789 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -3,10 +3,10 @@ name: CI on: push: branches: - - swiftwasm + - swiftwasm pull_request: branches: - - swiftwasm + - swiftwasm jobs: linux_build: @@ -14,34 +14,39 @@ jobs: runs-on: ubuntu-18.04 steps: - - uses: actions/checkout@v1 - - name: Build Linux installable archive - run: ./ci-linux.sh - - name: Upload Linux installable archive - uses: actions/upload-artifact@v1 - with: - name: linux-installable - path: ../swiftwasm-linux.tar.gz + - uses: actions/checkout@v1 + - name: Build Linux installable archive + run: ./ci-linux.sh + - name: Upload Linux installable archive + uses: actions/upload-artifact@v1 + with: + name: linux-installable + path: ../swiftwasm-linux.tar.gz macos_build: timeout-minutes: 0 runs-on: macOS-10.14 steps: - - uses: actions/checkout@v1 - - name: Build macOS installable archive - run: ./ci-mac.sh - - name: Upload macOS installable archive - uses: actions/upload-artifact@v1 - with: - name: macos-installable - path: ../swiftwasm-macos.tar.gz + - uses: actions/checkout@v1 + - name: Build macOS installable archive + run: ./ci-mac.sh + - name: Upload macOS installable archive + uses: actions/upload-artifact@v1 + with: + name: macos-installable + path: ../swiftwasm-macos.tar.gz + - name: Upload packaging scripts + uses: actions/upload-artifact@v1 + with: + name: packaging-scripts + path: utils/webassembly package: name: Build SwiftWasm packages needs: - - linux_build - - macos_build + - linux_build + - macos_build runs-on: ubuntu-18.04 steps: - name: Download installable Linux archive @@ -52,11 +57,16 @@ jobs: uses: actions/download-artifact@v1 with: name: macos-installable + - name: Download packaging scripts + uses: actions/download-artifact@v1 + with: + name: packaging-scripts - name: Build the packages shell: bash run: | - git clone https://github.com/swiftwasm/swiftwasm-package-sdk.git - cd swiftwasm-package-sdk + cd packaging-scripts + find . -name '*.sh' -exec chmod +x {} \; + chmod +x sdkroot/swiftwasm ./download-prebuilts.sh cp ../linux-installable/swiftwasm-linux.tar.gz \ @@ -74,19 +84,19 @@ jobs: uses: actions/upload-artifact@v1 with: name: macos-package - path: swiftwasm-package-sdk/output/swiftwasm-sdk-macos.tar.xz + path: packaging-scripts/output/swiftwasm-sdk-macos.tar.xz - name: Upload Linux package uses: actions/upload-artifact@v1 with: name: linux-package - path: swiftwasm-package-sdk/output/swiftwasm-sdk-linux.tar.xz + path: packaging-scripts/output/swiftwasm-sdk-linux.tar.xz - name: Upload hello.wasm compiled with Linux package uses: actions/upload-artifact@v1 with: name: linux-hello.wasm - path: swiftwasm-package-sdk/output/swiftwasm-sdk/hello.wasm + path: packaging-scripts/output/swiftwasm-sdk/hello.wasm macos_smoke_test: name: Compile hello.swift on macOS diff --git a/utils/webassembly/.gitignore b/utils/webassembly/.gitignore new file mode 100644 index 0000000000000..411f44532e7bc --- /dev/null +++ b/utils/webassembly/.gitignore @@ -0,0 +1,5 @@ +compiler +swiftwasm-sdk +prebuilt +output +tmpdir diff --git a/utils/webassembly/README.md b/utils/webassembly/README.md new file mode 100644 index 0000000000000..106f4fc35d84d --- /dev/null +++ b/utils/webassembly/README.md @@ -0,0 +1,23 @@ +Creates packages containing everything needed to build WebAssembly programs with Swift. + +# Building + +``` +./download-prebuilts.sh +./download-installable-prebuilts.sh +./build-packages.sh +``` + +# Contents of package + +- Swift toolchain from [swiftwasm-sdk](https://github.com/swiftwasm/swiftwasm-sdk) +- WASI modified sysroot from [wasi-sdk](https://github.com/swiftwasm/wasi-sdk) +- libicu from [icu4c-wasi](https://github.com/swiftwasm/icu4c-wasi) +- linking helpers from [swiftwasm-wasi-stubs](https://github.com/swiftwasm/swiftwasm-wasi-stubs) +- wasi-ld, either from wasi-sdk (on Linux) or upstream LLVM 9.0 (on Mac) +- build script for compiling a Swift file to a .wasm +- a Getting Started guide + +# Notes + +This shares a lot with the [swiftwasm-compile-service](https://github.com/swiftwasm/swiftwasm-compile-service). diff --git a/utils/webassembly/build-linux-package.sh b/utils/webassembly/build-linux-package.sh new file mode 100755 index 0000000000000..685d5c247a18e --- /dev/null +++ b/utils/webassembly/build-linux-package.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +echo "Unpacking Linux prebuilts" +mkdir -p output +cd linux +./unpack-prebuilts.sh +echo "Compressing" +tar cJf ../output/swiftwasm-sdk-linux.tar.xz swiftwasm-sdk +cd .. diff --git a/utils/webassembly/build-mac-package.sh b/utils/webassembly/build-mac-package.sh new file mode 100755 index 0000000000000..cd3c2602e8b5d --- /dev/null +++ b/utils/webassembly/build-mac-package.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +echo "Unpacking macOS prebuilts" +mkdir -p output +cd macos +./unpack-prebuilts.sh +echo "Compressing macOS package" +tar cJf ../output/swiftwasm-sdk-macos.tar.xz swiftwasm-sdk +cd .. diff --git a/utils/webassembly/build-packages.sh b/utils/webassembly/build-packages.sh new file mode 100755 index 0000000000000..e83fc38beb5d6 --- /dev/null +++ b/utils/webassembly/build-packages.sh @@ -0,0 +1,5 @@ +#!/bin/bash +set -e +rm -rf output || true +./build-linux-package.sh +./build-mac-package.sh diff --git a/utils/webassembly/copy-shared-files.sh b/utils/webassembly/copy-shared-files.sh new file mode 100755 index 0000000000000..7e1ce8a81832d --- /dev/null +++ b/utils/webassembly/copy-shared-files.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cp -r ../sdkroot/* compiler/ +cp ../linux/compiler/opt/swiftwasm-sdk/lib/swift/wasm/wasm32/glibc.modulemap compiler/extra_utils diff --git a/utils/webassembly/download-installable-prebuilts.sh b/utils/webassembly/download-installable-prebuilts.sh new file mode 100755 index 0000000000000..c407580547fc4 --- /dev/null +++ b/utils/webassembly/download-installable-prebuilts.sh @@ -0,0 +1,7 @@ +#!/bin/bash +set -e +mkdir -p prebuilt +cd prebuilt +wget -O swiftwasm-linux.tar.gz https://github.com/swiftwasm/swiftwasm-sdk/releases/download/20191112.1.linux/swiftwasm.tar.gz +# Mac specific +wget -O swiftwasm-macos.tar.gz https://github.com/swiftwasm/swiftwasm-sdk/releases/download/20191112.1.mac/swiftwasm-mac.tar.gz diff --git a/utils/webassembly/download-prebuilts.sh b/utils/webassembly/download-prebuilts.sh new file mode 100755 index 0000000000000..5ee1c8b3bf756 --- /dev/null +++ b/utils/webassembly/download-prebuilts.sh @@ -0,0 +1,8 @@ +#!/bin/bash +set -e +mkdir -p prebuilt +cd prebuilt +wget https://github.com/swiftwasm/wasi-sdk/releases/download/20191022.1/wasi-sdk-4.39g3025a5f47c04-linux.tar.gz +wget https://github.com/swiftwasm/icu4c-wasi/releases/download/20190421.3/icu4c-wasi.tar.xz +# Mac specific +wget http://releases.llvm.org/9.0.0/clang+llvm-9.0.0-x86_64-darwin-apple.tar.xz diff --git a/utils/webassembly/linux/unpack-prebuilts.sh b/utils/webassembly/linux/unpack-prebuilts.sh new file mode 100755 index 0000000000000..a5d5f669aa39f --- /dev/null +++ b/utils/webassembly/linux/unpack-prebuilts.sh @@ -0,0 +1,20 @@ +#!/bin/bash +set -e +rm -rf swiftwasm-sdk compiler +mkdir swiftwasm-sdk +ln -s swiftwasm-sdk compiler +cd compiler +untar="../../prebuilt/wasi-sdk-"*"-linux.tar.gz +../../prebuilt/swiftwasm-linux.tar.gz +../../prebuilt/icu4c-wasi.tar.xz" +for i in $untar +do + echo $i + tar xf $i +done +cd .. +mv "compiler/wasi-sdk-"* "compiler/wasi-sdk" +mv compiler/wasi-sdk/share/wasi-sysroot compiler/wasi-sdk/share/sysroot +../remove-swift-extra-files.sh || true +../remove-wasi-extra-files.sh || true +../copy-shared-files.sh || true diff --git a/utils/webassembly/macos/unpack-prebuilts.sh b/utils/webassembly/macos/unpack-prebuilts.sh new file mode 100755 index 0000000000000..169dd8cde7ce5 --- /dev/null +++ b/utils/webassembly/macos/unpack-prebuilts.sh @@ -0,0 +1,30 @@ +#!/bin/bash +set -e +rm -rf swiftwasm-sdk compiler tmpdir +mkdir swiftwasm-sdk tmpdir +ln -s swiftwasm-sdk compiler +cd compiler +untar="../../prebuilt/wasi-sdk-"*"-linux.tar.gz +../../prebuilt/swiftwasm-macos.tar.gz +../../prebuilt/icu4c-wasi.tar.xz" +for i in $untar +do + echo $i + tar xf $i +done +# Mac: unpack the Linux one and copy stdlibs +cd .. +cd tmpdir +tar xf ../../prebuilt/clang+llvm-*-x86_64-darwin-apple.tar.xz +tar xf ../../prebuilt/swiftwasm-linux.tar.gz +cd .. +mv "compiler/wasi-sdk-"* "compiler/wasi-sdk" +mv compiler/wasi-sdk/share/wasi-sysroot compiler/wasi-sdk/share/sysroot +../remove-swift-extra-files.sh || true +rm -r compiler/wasi-sdk/bin +mkdir compiler/wasi-sdk/bin +cp tmpdir/clang+llvm-*-x86_64-darwin-apple/bin/wasm-ld compiler/wasi-sdk/bin +cp -a tmpdir/opt/swiftwasm-sdk/lib/swift/wasm compiler/opt/swiftwasm-sdk/lib/swift/wasm +cp -a tmpdir/opt/swiftwasm-sdk/lib/swift_static compiler/opt/swiftwasm-sdk/lib/swift_static +# ok, finally copy over the shared files +../copy-shared-files.sh || true diff --git a/utils/webassembly/remove-swift-extra-files.sh b/utils/webassembly/remove-swift-extra-files.sh new file mode 100755 index 0000000000000..2e4e2d541a45f --- /dev/null +++ b/utils/webassembly/remove-swift-extra-files.sh @@ -0,0 +1,26 @@ +#!/bin/bash +basepath="compiler/opt/swiftwasm-sdk" +filestoremove="bin/sil-* +bin/lldb* +bin/sourcekitd-* +bin/swift-api-digester +bin/swift-autolink-extract +bin/swift-demangle +bin/swift-demangle-yamldump +bin/swift-format +bin/swift-llvm-opt +bin/swift-refactor +bin/swift-reflection-dump +bin/swift-*-test +lib/libsourcekitdInProc.so +lib/swift/clang/lib/linux/* +lib/swift_static/linux/* +lib/swift/linux/x86_64/* +lib/swift/linux/*" +for i in $filestoremove +do + echo $basepath/$i + rm $basepath/$i +done +# Mac only +rm -r $basepath/lib/swift/macosx $basepath/lib/sourcekitd.framework diff --git a/utils/webassembly/remove-wasi-extra-files.sh b/utils/webassembly/remove-wasi-extra-files.sh new file mode 100755 index 0000000000000..19ac34009874f --- /dev/null +++ b/utils/webassembly/remove-wasi-extra-files.sh @@ -0,0 +1,10 @@ +#!/bin/bash +basepath="compiler/wasi-sdk" +filestoremove="bin/clang* +bin/llvm* +bin/llc" +for i in $filestoremove +do + echo $basepath/$i + rm $basepath/$i +done diff --git a/utils/webassembly/sdkroot/README.md b/utils/webassembly/sdkroot/README.md new file mode 100644 index 0000000000000..db8048d2a504d --- /dev/null +++ b/utils/webassembly/sdkroot/README.md @@ -0,0 +1,87 @@ +SwiftWasm: Getting started +========================== + +Thank you for trying SwiftWasm! Here's how to get started. + +Please visit our website at https://swiftwasm.org for the latest updates. + + +Install dependencies +==================== + +Before running SwiftWasm, you will need to install some dependencies. + +Ubuntu: + +``` +sudo apt-get install libatomic1 +``` + +macOS: + +(No dependencies needed.) + +Windows: + +Install Windows Subsystem for Linux, then follow the Ubuntu instructions. + + + + +Compile SwiftWasm +================= + +Run + +``` +./swiftwasm example/hello.swift hello.wasm +``` + +To compile example/hello.swift to hello.wasm. + + + + +Running Wasm files +================== + +To run the resulting hello.wasm file: + +- Visit https://swiftwasm.org/polyfill/ +- select "Browse", and choose the hello.wasm file +- you should get output in the textbox. + +This polyfill should work in Firefox 66, Chrome 74, and Safari 12.1. + +You can also run the file outside a browser with: + +- Wasmtime https://github.com/CraneStation/wasmtime +- Lucet https://github.com/swiftwasm/lucet/tree/swiftwasm +- or any other WASI-compatible WebAssembly runtime. + + + + +Questions and support +===================== + +If you have any questions, please open an issue on + +https://github.com/swiftwasm/swift + + + +Third-party licenses +==================== + +This package contains components with their own license requirements. + +Swift compiler: https://github.com/apple/swift/blob/master/LICENSE.txt + +LLVM/Clang: https://github.com/llvm/llvm-project/blob/master/lld/LICENSE.TXT + +WASI sysroot: https://github.com/CraneStation/wasi-sysroot/blob/master/LICENSE + +ICU: https://github.com/unicode-org/icu/blob/master/icu4c/LICENSE + +WASI polyfill: https://github.com/CraneStation/wasmtime/blob/master/wasmtime-wasi/LICENSE diff --git a/utils/webassembly/sdkroot/example/hello.swift b/utils/webassembly/sdkroot/example/hello.swift new file mode 100644 index 0000000000000..e61506705879c --- /dev/null +++ b/utils/webassembly/sdkroot/example/hello.swift @@ -0,0 +1 @@ +print("Hello, 🌐!") diff --git a/utils/webassembly/sdkroot/extra_objs/fakelocaltime.o b/utils/webassembly/sdkroot/extra_objs/fakelocaltime.o new file mode 100644 index 0000000000000000000000000000000000000000..904d3a6dedc6108030be87a4d3012e63e153ed9a GIT binary patch literal 423 zcmb7Au};G<5WTaVCN!0rg#ocRf`O^(#KKeo8xn|x(M^M?OcSSaTqLGM%=|zHet-|) zTVUo#uoGZl!^7Er@7_DTgRCwQ0PwkOTOf06u$hy_Rr2W(Mx}lj3sER7Gb2h_R_2ia zdU#I=rEisyxfORCom*9DVY5XcaSiY-tZkOB#JW=2N<*7Y7#CHZ6;_o}7-#k52O$hU zy88yB=D@vLNO6{=gd-=blyZ95fwP`soN_oOBTkRVC=GbPS<*8DCkh=K-lq|ddJ!Lt zcBFrG3y1@TC?_L|aC9ITQlIYcc88F=WO+petmMkxRl3h!?F&bbB2?X12^zLL) h*OR|31mMNfLnEEp5leBaE@>~2}=BA9S^yPfa% zd++<+d%y4P6ujml0RZyO%E}5Tv$WMBtyv;JSS$V;U=~P=UWsw$2hZnSccB{iUg$3R zi}i4s0+GuTu3HT%)zB|T?%YyPj;i&*jl6veK4}279h;F?uDOkRHHiF>wPHsx*9h@> z4yYt4-oAPm0b{4+hEqTY9#4J{pz$Zi$9M%7sd3_{WbGonwH^5@;LwugFq_(}=I|0Y zip_19p+opZW~gQ>d2_l$CP=0>v1!d=Nai+clkYFUSjkY zSkGv{gofn2*hw|)x#9?0bu>&t+wJHj;uzRs+wOPvij~YJ*+E`^oB35W^oK`O98ka<;d4Jd&*x!bGx zd!zlK?^PbRmxAU}qk%uQe#M?{8UUq9bEJ`=Zn+**+=E^OfaIgZ%%@8gAKxAJ zm5d-T?c;x>qTCO-#bdIHlOsTBlV1 zww4Y9LPxu5k*?~XD?h&|=i+cNtXI6qE2bje(OI~&?g1YDH(DlWfa!I(9XSF#v<|OJ zeCrdtqy{jxPQ+xwI_ogffxBLmWd3gH6D6%R{%?6LE_LW*M~hE9{p3^Z8KDyK7!l9n z+a?jG2@x03ZlRr~L|j1o1MMUu;w!XEjIe7ArFfGK13$v^0B>^(PVs^`%Zd1c6ZRuV z_zK4heM7>B6$_3j1#v<_80{Cdn+joXsYrWQ#R+pxMcSvT1z)NK@hzU_CzY_jCA=ag zfFBbGuL=v^69w^+AmVF5*bf5X%VG=g>k@uRv*0zYAWmyU{DpQzC+vM4Dd+Sl;GgM8 zxu9EcQ7?$^bt3*m_!R@;V}=9#q=E2R!-9{Eg81AZ;yZ(|-z0q9*be+<6XD~g1t-jc u_`pOM?FQPZAtKJB{fhQxj)+gtzR3}GIft&l=XL?Vl|$Fphb=fhT=)lRhUi}a literal 0 HcmV?d00001 diff --git a/utils/webassembly/sdkroot/extra_utils/generateModulemap.sh b/utils/webassembly/sdkroot/extra_utils/generateModulemap.sh new file mode 100755 index 0000000000000..940e5a413339b --- /dev/null +++ b/utils/webassembly/sdkroot/extra_utils/generateModulemap.sh @@ -0,0 +1,2 @@ +#!/bin/sh +exec sed -e "s@\"/include@\"$1/include@g" "$(dirname $0)/glibc.modulemap" diff --git a/utils/webassembly/sdkroot/swiftwasm b/utils/webassembly/sdkroot/swiftwasm new file mode 100755 index 0000000000000..d87894df816cd --- /dev/null +++ b/utils/webassembly/sdkroot/swiftwasm @@ -0,0 +1,40 @@ +#!/bin/bash +set -e + +if [ "$#" -lt 2 ] +then + echo "usage: swiftwasm file1.swift file2.swift.... output.wasm" + exit 1 +fi +sdk="$(dirname $0)" +tmpobj="$(mktemp -t swiftwasm-XXXXXXXX)" +outputfile="${@: -1}" +if [[ "$outputfile" != *.wasm ]] +then + echo "output should end in .wasm" + exit 1 +fi +sysroot="$(dirname $0)/wasi-sdk/share/sysroot" +abssysroot="$(cd "$(dirname "$sysroot")" && pwd)/$(basename "$sysroot")" + +"$sdk/extra_utils/generateModulemap.sh" "$abssysroot" >"$sdk/opt/swiftwasm-sdk/lib/swift/wasm/wasm32/glibc.modulemap" + +"$sdk/opt/swiftwasm-sdk/bin/swiftc" -target wasm32-unknown-unknown-wasm \ + -sdk "$sysroot" -O -c \ + -o "$tmpobj" \ + "${@:1:$#-1}" +"$sdk/wasi-sdk/bin/wasm-ld" --error-limit=0 -o "$outputfile" \ + "$sysroot/lib/wasm32-wasi/crt1.o" \ + "$sdk/opt/swiftwasm-sdk/lib/swift_static/wasm/wasm32/swiftrt.o" \ + "$tmpobj" \ + "-L$sdk/opt/swiftwasm-sdk/lib/swift_static/wasm" \ + "-L$sysroot/lib/wasm32-wasi" \ + "-L$sdk/icu_out/lib" \ + -lswiftCore \ + -lc -lc++ -lc++abi -lswiftImageInspectionShared \ + -licuuc -licudata \ + "$sdk/wasi-sdk/lib/clang/9.0.0/lib/wasi/libclang_rt.builtins-wasm32.a" \ + "$sdk/extra_objs/fakepthread.o" \ + --no-gc-sections \ + --no-threads +rm "$tmpobj" From 8b49c06a7dfd930ae669efa4a1a0ede1d93b1b9d Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Sat, 14 Dec 2019 21:28:46 +0900 Subject: [PATCH 62/80] [WASM] Link start/stop symbol weakly (#21) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This PR fixed my runtime implementation in SwiftRT. I've inserted dummy `char` data in each metadata sections to ensure that all start/stop symbols are generated in https://github.com/swiftwasm/swift/pull/11. But of cource this dummy data can be inserted anywhere in the section, so metadata sections were broken by this 1 byte. I changed to link these start/stop symbols weakly. Non-generated start/stop variables get to be uninitialized. So `stop-start` results 0 length, and runtime library can avoid to load empty section. After this and https://github.com/swiftwasm/swift/pull/6 are merged, `print("Hello")` will work again! 🎉 --- stdlib/public/runtime/SwiftRT-WASM.cpp | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/stdlib/public/runtime/SwiftRT-WASM.cpp b/stdlib/public/runtime/SwiftRT-WASM.cpp index 1b4c84f3f2ae9..24dc7130def4d 100644 --- a/stdlib/public/runtime/SwiftRT-WASM.cpp +++ b/stdlib/public/runtime/SwiftRT-WASM.cpp @@ -14,14 +14,10 @@ #include -// Create empty sections to ensure that the start/stop symbols are synthesized -// by the linker. Otherwise, we may end up with undefined symbol references as -// the linker table section was never constructed. - +// Link start/stop symbols weakly to link them if they aren't synthesized by the linker. #define DECLARE_SWIFT_SECTION(name) \ - __attribute__((__used__,__section__(#name),__aligned__(1))) const char __dummy_##name = 0x00; \ - __attribute__((__visibility__("hidden"),__aligned__(1))) extern const char __start_##name; \ - __attribute__((__visibility__("hidden"),__aligned__(1))) extern const char __stop_##name; + __attribute__((__visibility__("hidden"),__aligned__(1),weak)) extern const char __start_##name; \ + __attribute__((__visibility__("hidden"),__aligned__(1),weak)) extern const char __stop_##name; extern "C" { DECLARE_SWIFT_SECTION(swift5_protocols) From 5cf1bed05f9e07ce0c3fda2116c2c39eda92b9ef Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Sat, 14 Dec 2019 22:06:45 +0900 Subject: [PATCH 63/80] Emit thunk function for specific ABI on WebAssembly. (#6) Changed to make thunk to convert thin-to-thick and non-throws-to-throws. We needs it on WebAssembly host because WASM checks number of arguments strictly for indirect function call. This patch allows you to run the Swift code below. ```swift func f(_ a: (Int) -> Void) { g(a) } func g(_ b: (Int) throws -> Void) { try! b(1) } f { _ in } ``` --- lib/SIL/TypeLowering.cpp | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/SIL/TypeLowering.cpp b/lib/SIL/TypeLowering.cpp index 5566950fb6acd..a54f106db3558 100644 --- a/lib/SIL/TypeLowering.cpp +++ b/lib/SIL/TypeLowering.cpp @@ -2633,6 +2633,14 @@ TypeConverter::checkFunctionForABIDifferences(SILModule &M, return ABIDifference::NeedsThunk; } + // There is no ABI compatibility between non-throws and throws on WebAssembly, + // so need thunk. + if (M.getASTContext().LangOpts.Target.isOSBinFormatWasm()) { + if (!fnTy1->hasErrorResult() && fnTy2->hasErrorResult()) { + return ABIDifference::NeedsThunk; + } + } + auto rep1 = fnTy1->getRepresentation(), rep2 = fnTy2->getRepresentation(); if (rep1 != rep2) { if (rep1 == SILFunctionTypeRepresentation::Thin && @@ -2644,8 +2652,14 @@ TypeConverter::checkFunctionForABIDifferences(SILModule &M, } else { return ABIDifference::CompatibleRepresentation_ThinToThick; } - } + // There is no ABI compatibility between thin and thick on WebAssembly, + // so need thunk. + if (M.getASTContext().LangOpts.Target.isOSBinFormatWasm()) { + return ABIDifference::NeedsThunk; + } + return ABIDifference::ThinToThick; + } return ABIDifference::NeedsThunk; } From 61d7f2319b1319b0309d68a87508d7a5f14b4327 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Sun, 15 Dec 2019 01:43:11 +0000 Subject: [PATCH 64/80] [WebAssembly] Remove conflicted default case --- lib/Basic/LangOptions.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/Basic/LangOptions.cpp b/lib/Basic/LangOptions.cpp index 2ed2b34aed21a..def6143af764f 100644 --- a/lib/Basic/LangOptions.cpp +++ b/lib/Basic/LangOptions.cpp @@ -333,8 +333,6 @@ std::pair LangOptions::setTarget(llvm::Triple triple) { case llvm::Triple::ArchType::systemz: addPlatformConditionValue(PlatformConditionKind::Endianness, "big"); break; - default: - llvm_unreachable("undefined architecture endianness"); } // Set the "runtime" platform condition. From d780253c7f69229474129448dc6a3a5d2914f29d Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Sun, 15 Dec 2019 03:46:40 +0000 Subject: [PATCH 65/80] [WASM] Replace Wasm with WASI to switch target OS --- CMakeLists.txt | 12 +++---- build-linux.sh | 18 +++++----- build-mac.sh | 4 +-- cmake/modules/AddSwift.cmake | 14 ++++---- cmake/modules/SwiftConfigureSDK.cmake | 12 +++---- stdlib/private/StdlibUnittest/CMakeLists.txt | 2 +- stdlib/private/StdlibUnittest/RaceTest.swift | 4 +-- .../StdlibUnittest/StdlibCoreExtras.swift | 2 +- .../StdlibUnittest/StdlibUnittest.swift | 14 ++++---- .../private/StdlibUnittest/SymbolLookup.swift | 6 ++-- .../SwiftPrivateLibcExtras/CMakeLists.txt | 2 +- .../SwiftPrivateLibcExtras/Subprocess.swift | 8 ++--- .../SwiftPrivateLibcExtras.swift | 6 ++-- .../SwiftPrivateThreadExtras/CMakeLists.txt | 2 +- .../SwiftPrivateThreadExtras.swift | 2 +- .../ThreadBarriers.swift | 2 +- stdlib/public/Platform/CMakeLists.txt | 4 +-- stdlib/public/Platform/Glibc.swift.gyb | 2 +- stdlib/public/Platform/Platform.swift | 4 +-- stdlib/public/Platform/glibc.modulemap.gyb | 22 ++++++------ stdlib/public/runtime/CMakeLists.txt | 2 +- utils/build-script | 34 +++++++++---------- utils/build-script-impl | 16 ++++----- .../build_swift/driver_arguments.py | 12 +++---- .../host_specific_configuration.py | 6 ++-- .../swift_build_support/targets.py | 8 ++--- 26 files changed, 110 insertions(+), 110 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e068cc12d3c8d..0248830db36eb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -263,7 +263,7 @@ set(SWIFT_ANDROID_DEPLOY_DEVICE_PATH "" CACHE STRING # User-configurable ICU specific options for Android, FreeBSD, Linux, Haiku, and WebAssembly. # -foreach(sdk ANDROID;FREEBSD;LINUX;WINDOWS;HAIKU;WASM) +foreach(sdk ANDROID;FREEBSD;LINUX;WINDOWS;HAIKU;WASI) foreach(arch aarch64;armv6;armv7;i686;powerpc64;powerpc64le;s390x;wasm32;x86_64) set(SWIFT_${sdk}_${arch}_ICU_UC "" CACHE STRING "Path to a directory containing the icuuc library for ${sdk}") @@ -782,8 +782,8 @@ if(swift_build_windows AND NOT "${CMAKE_SYSTEM_NAME}" STREQUAL "Windows") endif() # Should we cross-compile the standard library for WebAssembly (WASI)? -is_sdk_requested(WASM swift_build_wasm) -if(swift_build_wasm AND NOT "${SWIFT_HOST_VARIANT_SDK}" STREQUAL "WASM") +is_sdk_requested(WASI swift_build_wasm) +if(swift_build_wasm AND NOT "${SWIFT_HOST_VARIANT_SDK}" STREQUAL "WASI") #if ("${SWIFT_ANDROID_NDK_PATH}" STREQUAL "") # message(FATAL_ERROR "You must set SWIFT_ANDROID_NDK_PATH to cross-compile the Swift runtime for Android") #endif() @@ -791,10 +791,10 @@ if(swift_build_wasm AND NOT "${SWIFT_HOST_VARIANT_SDK}" STREQUAL "WASM") # message(FATAL_ERROR "A Darwin or Linux host is required to build the Swift runtime for Android") #endif() - if("${SWIFT_SDK_WASM_ARCHITECTURES}" STREQUAL "") - set(SWIFT_SDK_WASM_ARCHITECTURES wasm32) + if("${SWIFT_SDK_WASI_ARCHITECTURES}" STREQUAL "") + set(SWIFT_SDK_WASI_ARCHITECTURES wasm32) endif() - configure_sdk_unix("Wasm" "${SWIFT_SDK_WASM_ARCHITECTURES}") + configure_sdk_unix("WASI" "${SWIFT_SDK_WASI_ARCHITECTURES}") endif() if("${SWIFT_SDKS}" STREQUAL "") diff --git a/build-linux.sh b/build-linux.sh index 30285c85a2dc9..d5bad958f906d 100755 --- a/build-linux.sh +++ b/build-linux.sh @@ -5,23 +5,23 @@ export sourcedir=$PWD/.. ./utils/build-script --release --wasm --verbose \ --skip-build-benchmarks \ --extra-cmake-options=" \ - -DSWIFT_SDKS='WASM;LINUX' \ + -DSWIFT_SDKS='WASI;LINUX' \ -DSWIFT_BUILD_SOURCEKIT=FALSE \ -DSWIFT_ENABLE_SOURCEKIT_TESTS=FALSE \ -DCMAKE_AR='$sourcedir/wasi-sdk/bin/llvm-ar' \ -DCMAKE_RANLIB='$sourcedir/wasi-sdk/bin/llvm-ranlib' \ " \ - --build-stdlib-deployment-targets "wasm-wasm32" \ + --build-stdlib-deployment-targets "wasi-wasm32" \ --build-swift-static-stdlib \ --install-destdir="$sourcedir/install" \ --install-prefix="/opt/swiftwasm-sdk" \ --install-swift \ --installable-package="$sourcedir/swiftwasm-linux.tar.gz" \ --llvm-targets-to-build "X86;WebAssembly" \ - --stdlib-deployment-targets "wasm-wasm32" \ - --wasm-icu-data "todo-icu-data" \ - --wasm-icu-i18n "$sourcedir/icu_out/lib" \ - --wasm-icu-i18n-include "$sourcedir/icu_out/include" \ - --wasm-icu-uc "$sourcedir/icu_out/lib" \ - --wasm-icu-uc-include "$sourcedir/icu_out/include" \ - --wasm-wasi-sdk "$sourcedir/wasi-sdk" + --stdlib-deployment-targets "wasi-wasm32" \ + --wasi-icu-data "todo-icu-data" \ + --wasi-icu-i18n "$sourcedir/icu_out/lib" \ + --wasi-icu-i18n-include "$sourcedir/icu_out/include" \ + --wasi-icu-uc "$sourcedir/icu_out/lib" \ + --wasi-icu-uc-include "$sourcedir/icu_out/include" \ + --wasi-sdk "$sourcedir/wasi-sdk" diff --git a/build-mac.sh b/build-mac.sh index aee098f9703cc..90a8be35acb1e 100755 --- a/build-mac.sh +++ b/build-mac.sh @@ -13,12 +13,12 @@ export sourcedir=$PWD/.. -DCMAKE_AR='/usr/local/opt/llvm/bin/llvm-ar' \ -DCMAKE_RANLIB='/usr/local/opt/llvm/bin/llvm-ranlib' \ " \ - --build-stdlib-deployment-targets "wasm-wasm32" \ + --build-stdlib-deployment-targets "wasi-wasm32" \ --build-swift-dynamic-sdk-overlay false \ --build-swift-static-sdk-overlay false \ --build-swift-static-stdlib \ --llvm-targets-to-build "X86;WebAssembly" \ - --stdlib-deployment-targets "wasm-wasm32" \ + --stdlib-deployment-targets "wasi-wasm32" \ --wasm-icu-data "todo-icu-data" \ --wasm-icu-i18n "$sourcedir/icu_out/lib" \ --wasm-icu-i18n-include "$sourcedir/icu_out/include" \ diff --git a/cmake/modules/AddSwift.cmake b/cmake/modules/AddSwift.cmake index ce8421b6baff9..886d8172c1ca3 100644 --- a/cmake/modules/AddSwift.cmake +++ b/cmake/modules/AddSwift.cmake @@ -551,7 +551,7 @@ function(_add_variant_link_flags) foreach(path IN LISTS ${LFLAGS_ARCH}_LIB) list(APPEND library_search_directories ${path}) endforeach() - elseif("${LFLAGS_SDK}" STREQUAL "WASM") + elseif("${LFLAGS_SDK}" STREQUAL "WASI") # No extra libraries needed. else() # If lto is enabled, we need to add the object path flag so that the LTO code @@ -582,7 +582,7 @@ function(_add_variant_link_flags) if(NOT SWIFT_COMPILER_IS_MSVC_LIKE) # FIXME: On Apple platforms, find_program needs to look for "ld64.lld" find_program(LDLLD_PATH "ld.lld") - if("${LFLAGS_SDK}" STREQUAL "WASM") + if("${SWIFT_SDK_${LFLAGS_SDK}_OBJECT_FORMAT}" STREQUAL "WASM") list(APPEND result "-fuse-ld=${CMAKE_SOURCE_DIR}/fakeld") elseif((SWIFT_ENABLE_LLD_LINKER AND LDLLD_PATH AND NOT APPLE) OR ("${LFLAGS_SDK}" STREQUAL "WINDOWS" AND @@ -1715,8 +1715,8 @@ endfunction() # SWIFT_MODULE_DEPENDS_HAIKU # Swift modules this library depends on when built for Haiku. # -# SWIFT_MODULE_DEPENDS_WASM -# Swift modules this library depends on when built for WebAssembly. +# SWIFT_MODULE_DEPENDS_WASI +# Swift modules this library depends on when built for WASI. # # FRAMEWORK_DEPENDS # System frameworks this library depends on. @@ -1834,7 +1834,7 @@ function(add_swift_target_library name) SWIFT_MODULE_DEPENDS_OSX SWIFT_MODULE_DEPENDS_TVOS SWIFT_MODULE_DEPENDS_WATCHOS - SWIFT_MODULE_DEPENDS_WASM + SWIFT_MODULE_DEPENDS_WASI SWIFT_MODULE_DEPENDS_WINDOWS SWIFT_MODULE_DEPENDS_FROM_SDK TARGET_SDKS @@ -1996,9 +1996,9 @@ function(add_swift_target_library name) elseif(${sdk} STREQUAL HAIKU) list(APPEND swiftlib_module_depends_flattened ${SWIFTLIB_SWIFT_MODULE_DEPENDS_HAIKU}) - elseif(${sdk} STREQUAL WASM) + elseif(${sdk} STREQUAL WASI) list(APPEND swiftlib_module_depends_flattened - ${SWIFTLIB_SWIFT_MODULE_DEPENDS_WASM}) + ${SWIFTLIB_SWIFT_MODULE_DEPENDS_WASI}) elseif(${sdk} STREQUAL WINDOWS) list(APPEND swiftlib_module_depends_flattened ${SWIFTLIB_SWIFT_MODULE_DEPENDS_WINDOWS}) diff --git a/cmake/modules/SwiftConfigureSDK.cmake b/cmake/modules/SwiftConfigureSDK.cmake index ec29b60d53d07..a81763d6fe22c 100644 --- a/cmake/modules/SwiftConfigureSDK.cmake +++ b/cmake/modules/SwiftConfigureSDK.cmake @@ -217,7 +217,7 @@ macro(configure_sdk_unix name architectures) set(SWIFT_SDK_${prefix}_ARCHITECTURES "${architectures}") if("${prefix}" STREQUAL "CYGWIN") set(SWIFT_SDK_${prefix}_OBJECT_FORMAT "COFF") - elseif("${prefix}" STREQUAL "WASM") + elseif("${prefix}" STREQUAL "WASI") set(SWIFT_SDK_${prefix}_OBJECT_FORMAT "WASM") else() set(SWIFT_SDK_${prefix}_OBJECT_FORMAT "ELF") @@ -335,15 +335,15 @@ macro(configure_sdk_unix name architectures) message(FATAL_ERROR "unsupported arch for Haiku: ${arch}") endif() set(SWIFT_SDK_HAIKU_ARCH_x86_64_TRIPLE "x86_64-unknown-haiku") - elseif("${prefix}" STREQUAL "WASM") + elseif("${prefix}" STREQUAL "WASI") if(NOT arch STREQUAL wasm32) message(FATAL_ERROR "unsupported arch for WebAssembly: ${arch}") endif() - set(SWIFT_SDK_WASM_ARCH_wasm32_PATH "${SWIFT_WASM_WASI_SDK_PATH}/share/sysroot") + set(SWIFT_SDK_WASI_ARCH_wasm32_PATH "${SWIFT_WASI_SDK_PATH}/share/sysroot") # fixme: Wasi is wasm32-unknown-wasi-musl. This LLVM doesn't have it yet. - set(SWIFT_SDK_WASM_ARCH_wasm32_TRIPLE "wasm32-unknown-unknown-wasm") - set(SWIFT_SDK_WASM_ARCH_wasm32_LIBC_INCLUDE_DIRECTORY "${SWIFT_WASM_WASI_SDK_PATH}/share/sysroot/include") - set(SWIFT_SDK_WASM_ARCH_wasm32_LIBC_ARCHITECTURE_INCLUDE_DIRECTORY "${SWIFT_WASM_WASI_SDK_PATH}/share/sysroot/include") + set(SWIFT_SDK_WASI_ARCH_wasm32_TRIPLE "wasm32-unknown-unknown-wasi") + set(SWIFT_SDK_WASI_ARCH_wasm32_LIBC_INCLUDE_DIRECTORY "${SWIFT_WASI_SDK_PATH}/share/sysroot/include") + set(SWIFT_SDK_WASI_ARCH_wasm32_LIBC_ARCHITECTURE_INCLUDE_DIRECTORY "${SWIFT_WASI_SDK_PATH}/share/sysroot/include") else() message(FATAL_ERROR "unknown Unix OS: ${prefix}") endif() diff --git a/stdlib/private/StdlibUnittest/CMakeLists.txt b/stdlib/private/StdlibUnittest/CMakeLists.txt index 5f6cc350fbe3a..9614bfbdcbc04 100644 --- a/stdlib/private/StdlibUnittest/CMakeLists.txt +++ b/stdlib/private/StdlibUnittest/CMakeLists.txt @@ -40,7 +40,7 @@ add_swift_target_library(swiftStdlibUnittest ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} SWIFT_MODULE_DEPENDS_FREEBSD Glibc SWIFT_MODULE_DEPENDS_CYGWIN Glibc SWIFT_MODULE_DEPENDS_HAIKU Glibc - SWIFT_MODULE_DEPENDS_WASM Glibc + SWIFT_MODULE_DEPENDS_WASI Glibc SWIFT_MODULE_DEPENDS_WINDOWS MSVCRT WinSDK SWIFT_COMPILE_FLAGS ${swift_stdlib_unittest_compile_flags} ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS} INSTALL_IN_COMPONENT stdlib-experimental diff --git a/stdlib/private/StdlibUnittest/RaceTest.swift b/stdlib/private/StdlibUnittest/RaceTest.swift index 60ba5e368c083..d793930527e71 100644 --- a/stdlib/private/StdlibUnittest/RaceTest.swift +++ b/stdlib/private/StdlibUnittest/RaceTest.swift @@ -41,7 +41,7 @@ import SwiftPrivateLibcExtras import SwiftPrivateThreadExtras #if os(macOS) || os(iOS) import Darwin -#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku) || os(Wasm) +#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku) || os(WASI) import Glibc #elseif os(Windows) import MSVCRT @@ -562,7 +562,7 @@ class _InterruptibleSleep { return } -#if os(Wasm) +#if os(WASI) // WebAssembly/WASI on wasm32 is the only 32-bit platform with Int64 time_t var timeout = timeval(tv_sec: time_t(duration), tv_usec: 0) #else diff --git a/stdlib/private/StdlibUnittest/StdlibCoreExtras.swift b/stdlib/private/StdlibUnittest/StdlibCoreExtras.swift index 8b356c2acff2a..bc8bd4fe980e0 100644 --- a/stdlib/private/StdlibUnittest/StdlibCoreExtras.swift +++ b/stdlib/private/StdlibUnittest/StdlibCoreExtras.swift @@ -14,7 +14,7 @@ import SwiftPrivate import SwiftPrivateLibcExtras #if os(macOS) || os(iOS) import Darwin -#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku) || os(Wasm) +#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku) || os(WASI) import Glibc #elseif os(Windows) import MSVCRT diff --git a/stdlib/private/StdlibUnittest/StdlibUnittest.swift b/stdlib/private/StdlibUnittest/StdlibUnittest.swift index 03e0ca2c9cbec..51ac487875af9 100644 --- a/stdlib/private/StdlibUnittest/StdlibUnittest.swift +++ b/stdlib/private/StdlibUnittest/StdlibUnittest.swift @@ -18,7 +18,7 @@ import SwiftPrivateLibcExtras #if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) import Foundation import Darwin -#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku) || os(Wasm) +#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku) || os(WASI) import Glibc #elseif os(Windows) import MSVCRT @@ -748,7 +748,7 @@ extension ProcessTerminationStatus { case .signal(let signal): #if os(Windows) return CInt(signal) == SIGILL -#elseif os(Wasm) +#elseif os(WASI) return false #else return CInt(signal) == SIGILL || CInt(signal) == SIGTRAP @@ -1748,7 +1748,7 @@ public enum OSVersion : CustomStringConvertible { case windowsCygnus case windows case haiku - case wasm + case wasi public var description: String { switch self { @@ -1780,8 +1780,8 @@ public enum OSVersion : CustomStringConvertible { return "Windows" case .haiku: return "Haiku" - case .wasm: - return "Wasm" + case .wasi: + return "WASI" } } } @@ -1826,8 +1826,8 @@ func _getOSVersion() -> OSVersion { return .windows #elseif os(Haiku) return .haiku -#elseif os(Wasm) - return .wasm +#elseif os(WASI) + return .wasi #else let productVersion = _getSystemVersionPlistProperty("ProductVersion")! let (major, minor, bugFix) = _parseDottedVersionTriple(productVersion) diff --git a/stdlib/private/StdlibUnittest/SymbolLookup.swift b/stdlib/private/StdlibUnittest/SymbolLookup.swift index 6967c11327e0e..f827f6e2fe956 100644 --- a/stdlib/private/StdlibUnittest/SymbolLookup.swift +++ b/stdlib/private/StdlibUnittest/SymbolLookup.swift @@ -12,7 +12,7 @@ #if os(macOS) || os(iOS) || os(tvOS) || os(watchOS) import Darwin -#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku) || os(Wasm) +#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku) || os(WASI) import Glibc #elseif os(Windows) import MSVCRT @@ -23,7 +23,7 @@ #if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) let RTLD_DEFAULT = UnsafeMutableRawPointer(bitPattern: -2) -#elseif os(Linux) || os(Wasm) +#elseif os(Linux) || os(WASI) let RTLD_DEFAULT = UnsafeMutableRawPointer(bitPattern: 0) #elseif os(Android) #if arch(arm) || arch(i386) @@ -43,7 +43,7 @@ public func pointerToSwiftCoreSymbol(name: String) -> UnsafeMutableRawPointer? { #if os(Windows) return unsafeBitCast(GetProcAddress(hStdlibCore, name), to: UnsafeMutableRawPointer?.self) -#elseif os(Wasm) +#elseif os(WASI) fatalError("\(#function) is not supported on WebAssembly") #else return dlsym(RTLD_DEFAULT, name) diff --git a/stdlib/private/SwiftPrivateLibcExtras/CMakeLists.txt b/stdlib/private/SwiftPrivateLibcExtras/CMakeLists.txt index 86ad055eb4f0b..a4545a05728ae 100644 --- a/stdlib/private/SwiftPrivateLibcExtras/CMakeLists.txt +++ b/stdlib/private/SwiftPrivateLibcExtras/CMakeLists.txt @@ -17,7 +17,7 @@ add_swift_target_library(swiftSwiftPrivateLibcExtras ${SWIFT_STDLIB_LIBRARY_BUIL SWIFT_MODULE_DEPENDS_FREEBSD Glibc SWIFT_MODULE_DEPENDS_CYGWIN Glibc SWIFT_MODULE_DEPENDS_HAIKU Glibc - SWIFT_MODULE_DEPENDS_WASM Glibc + SWIFT_MODULE_DEPENDS_WASI Glibc SWIFT_MODULE_DEPENDS_WINDOWS MSVCRT WinSDK INSTALL_IN_COMPONENT stdlib-experimental DARWIN_INSTALL_NAME_DIR "${SWIFT_DARWIN_STDLIB_PRIVATE_INSTALL_NAME_DIR}") diff --git a/stdlib/private/SwiftPrivateLibcExtras/Subprocess.swift b/stdlib/private/SwiftPrivateLibcExtras/Subprocess.swift index f8ec3333de45d..84ab3b44398b2 100644 --- a/stdlib/private/SwiftPrivateLibcExtras/Subprocess.swift +++ b/stdlib/private/SwiftPrivateLibcExtras/Subprocess.swift @@ -13,7 +13,7 @@ import SwiftPrivate #if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) import Darwin -#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku) || os(Wasm) +#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku) || os(WASI) import Glibc #elseif os(Windows) import MSVCRT @@ -21,7 +21,7 @@ import WinSDK #endif internal func _signalToString(_ signal: Int) -> String { -#if os(Wasm) +#if os(WASI) return "unsupported" #else switch CInt(signal) { @@ -36,7 +36,7 @@ internal func _signalToString(_ signal: Int) -> String { #endif default: return "SIG???? (\(signal))" } -#endif // os(Wasm) +#endif // os(WASI) } public enum ProcessTerminationStatus : CustomStringConvertible { @@ -145,7 +145,7 @@ public func waitProcess(_ process: HANDLE) -> ProcessTerminationStatus { } return .exit(Int(status)) } -#elseif os(Wasm) +#elseif os(WASI) // Oops, we can't launch tests in subprocesses yet! public func spawnChild(_ args: [String]) -> (pid: pid_t, stdinFD: CInt, stdoutFD: CInt, stderrFD: CInt) { diff --git a/stdlib/private/SwiftPrivateLibcExtras/SwiftPrivateLibcExtras.swift b/stdlib/private/SwiftPrivateLibcExtras/SwiftPrivateLibcExtras.swift index 6042ba6550ad8..68e955f7d648a 100644 --- a/stdlib/private/SwiftPrivateLibcExtras/SwiftPrivateLibcExtras.swift +++ b/stdlib/private/SwiftPrivateLibcExtras/SwiftPrivateLibcExtras.swift @@ -13,14 +13,14 @@ import SwiftPrivate #if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) import Darwin -#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku) || os(Wasm) +#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku) || os(WASI) import Glibc #elseif os(Windows) import MSVCRT #endif public func _stdlib_mkstemps(_ template: inout String, _ suffixlen: CInt) -> CInt { -#if os(Android) || os(Haiku) || os(Windows) || os(Wasm) +#if os(Android) || os(Haiku) || os(Windows) || os(WASI) preconditionFailure("mkstemps doesn't work on your platform") #else var utf8CStr = template.utf8CString @@ -125,7 +125,7 @@ public func _stdlib_pipe() -> (readEnd: CInt, writeEnd: CInt, error: CInt) { let ret = fds.withUnsafeMutableBufferPointer { unsafeFds -> CInt in #if os(Windows) return _pipe(unsafeFds.baseAddress, 0, 0) -#elseif os(Wasm) +#elseif os(WASI) fatalError("no pipes on WebAssembly") #else return pipe(unsafeFds.baseAddress) diff --git a/stdlib/private/SwiftPrivateThreadExtras/CMakeLists.txt b/stdlib/private/SwiftPrivateThreadExtras/CMakeLists.txt index b6d794dfc051d..82a68f1962560 100644 --- a/stdlib/private/SwiftPrivateThreadExtras/CMakeLists.txt +++ b/stdlib/private/SwiftPrivateThreadExtras/CMakeLists.txt @@ -14,7 +14,7 @@ add_swift_target_library(swiftSwiftPrivateThreadExtras ${SWIFT_STDLIB_LIBRARY_BU SWIFT_MODULE_DEPENDS_FREEBSD Glibc SWIFT_MODULE_DEPENDS_CYGWIN Glibc SWIFT_MODULE_DEPENDS_HAIKU Glibc - SWIFT_MODULE_DEPENDS_WASM Glibc + SWIFT_MODULE_DEPENDS_WASI Glibc SWIFT_MODULE_DEPENDS_WINDOWS MSVCRT WinSDK SWIFT_COMPILE_FLAGS ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS} INSTALL_IN_COMPONENT stdlib-experimental diff --git a/stdlib/private/SwiftPrivateThreadExtras/SwiftPrivateThreadExtras.swift b/stdlib/private/SwiftPrivateThreadExtras/SwiftPrivateThreadExtras.swift index feff077024252..831d721f3c588 100644 --- a/stdlib/private/SwiftPrivateThreadExtras/SwiftPrivateThreadExtras.swift +++ b/stdlib/private/SwiftPrivateThreadExtras/SwiftPrivateThreadExtras.swift @@ -17,7 +17,7 @@ #if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) import Darwin -#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku) || os(Wasm) +#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku) || os(WASI) import Glibc #elseif os(Windows) import MSVCRT diff --git a/stdlib/private/SwiftPrivateThreadExtras/ThreadBarriers.swift b/stdlib/private/SwiftPrivateThreadExtras/ThreadBarriers.swift index 68a7880d18a07..b06ed029bf9b0 100644 --- a/stdlib/private/SwiftPrivateThreadExtras/ThreadBarriers.swift +++ b/stdlib/private/SwiftPrivateThreadExtras/ThreadBarriers.swift @@ -12,7 +12,7 @@ #if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) import Darwin -#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku) || os(Wasm) +#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku) || os(WASI) import Glibc #elseif os(Windows) import MSVCRT diff --git a/stdlib/public/Platform/CMakeLists.txt b/stdlib/public/Platform/CMakeLists.txt index 079e9cc37d6d2..6a6a2e05ed29c 100644 --- a/stdlib/public/Platform/CMakeLists.txt +++ b/stdlib/public/Platform/CMakeLists.txt @@ -44,7 +44,7 @@ add_swift_target_library(swiftGlibc ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_O ${SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS} ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS} LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}" - TARGET_SDKS ANDROID CYGWIN FREEBSD LINUX HAIKU WASM + TARGET_SDKS ANDROID CYGWIN FREEBSD LINUX HAIKU WASI INSTALL_IN_COMPONENT sdk-overlay DEPENDS glibc_modulemap) @@ -71,7 +71,7 @@ foreach(sdk ${SWIFT_SDKS}) NOT "${sdk}" STREQUAL "ANDROID" AND NOT "${sdk}" STREQUAL "CYGWIN" AND NOT "${sdk}" STREQUAL "HAIKU" AND - NOT "${sdk}" STREQUAL "WASM") + NOT "${sdk}" STREQUAL "WASI") continue() endif() diff --git a/stdlib/public/Platform/Glibc.swift.gyb b/stdlib/public/Platform/Glibc.swift.gyb index 3258bcd28b10c..dc5ca8b711484 100644 --- a/stdlib/public/Platform/Glibc.swift.gyb +++ b/stdlib/public/Platform/Glibc.swift.gyb @@ -70,7 +70,7 @@ public let ${prefix}_TRUE_MIN = ${type}.leastNonzeroMagnitude % end %end -#if os(Wasm) +#if os(WASI) // WebAssembly's error.h uses a macro that Swift can't import. public let EINTR:Int32 = 27 public let EINVAL:Int32 = 28 diff --git a/stdlib/public/Platform/Platform.swift b/stdlib/public/Platform/Platform.swift index 17c60c276036b..fce8fb75eadf3 100644 --- a/stdlib/public/Platform/Platform.swift +++ b/stdlib/public/Platform/Platform.swift @@ -366,7 +366,7 @@ public var SIG_IGN: _crt_signal_t { public var SIG_ERR: _crt_signal_t { return unsafeBitCast(-1, to: _crt_signal_t.self) } -#elseif os(Wasm) +#elseif os(WASI) // WebAssembly/WASI doesn't have signals. #else internal var _ignore = _UnsupportedPlatformError() @@ -382,7 +382,7 @@ public var SEM_FAILED: UnsafeMutablePointer? { #if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) // The value is ABI. Value verified to be correct for OS X, iOS, watchOS, tvOS. return UnsafeMutablePointer(bitPattern: -1) -#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku) || os(Wasm) +#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku) || os(WASI) // The value is ABI. Value verified to be correct on Glibc. return UnsafeMutablePointer(bitPattern: 0) #else diff --git a/stdlib/public/Platform/glibc.modulemap.gyb b/stdlib/public/Platform/glibc.modulemap.gyb index eba7fde86588a..5207835d369d2 100644 --- a/stdlib/public/Platform/glibc.modulemap.gyb +++ b/stdlib/public/Platform/glibc.modulemap.gyb @@ -126,7 +126,7 @@ module SwiftGlibc [system] { header "${GLIBC_INCLUDE_PATH}/math.h" export * } -% if CMAKE_SDK != "WASM": +% if CMAKE_SDK != "WASI": module setjmp { header "${GLIBC_INCLUDE_PATH}/setjmp.h" export * @@ -321,7 +321,7 @@ module SwiftGlibc [system] { header "${GLIBC_INCLUDE_PATH}/dirent.h" export * } -% if CMAKE_SDK != "WASM": +% if CMAKE_SDK != "WASI": module dl { header "${GLIBC_INCLUDE_PATH}/link.h" export * @@ -339,7 +339,7 @@ module SwiftGlibc [system] { header "${GLIBC_INCLUDE_PATH}/fnmatch.h" export * } -% if CMAKE_SDK != "WASM": +% if CMAKE_SDK != "WASI": module grp { header "${GLIBC_INCLUDE_PATH}/grp.h" export * @@ -353,7 +353,7 @@ module SwiftGlibc [system] { header "${GLIBC_INCLUDE_PATH}/libgen.h" export * } -% if CMAKE_SDK != "WASM": +% if CMAKE_SDK != "WASI": module net { module if { header "${GLIBC_INCLUDE_PATH}/net/if.h" @@ -381,7 +381,7 @@ module SwiftGlibc [system] { header "${GLIBC_INCLUDE_PATH}/pthread.h" export * } -% if CMAKE_SDK != "WASM": +% if CMAKE_SDK != "WASI": module pwd { header "${GLIBC_INCLUDE_PATH}/pwd.h" export * @@ -432,7 +432,7 @@ module SwiftGlibc [system] { } % end -% if CMAKE_SDK != "WASM": +% if CMAKE_SDK != "WASI": module ipc { header "${GLIBC_ARCH_INCLUDE_PATH}/sys/ipc.h" export * @@ -442,7 +442,7 @@ module SwiftGlibc [system] { header "${GLIBC_ARCH_INCLUDE_PATH}/sys/mman.h" export * } -% if CMAKE_SDK != "WASM": +% if CMAKE_SDK != "WASI": module msg { header "${GLIBC_ARCH_INCLUDE_PATH}/sys/msg.h" export * @@ -456,7 +456,7 @@ module SwiftGlibc [system] { header "${GLIBC_ARCH_INCLUDE_PATH}/sys/select.h" export * } -% if CMAKE_SDK != "FREEBSD" and CMAKE_SDK != "HAIKU" and CMAKE_SDK != "WASM": +% if CMAKE_SDK != "FREEBSD" and CMAKE_SDK != "HAIKU" and CMAKE_SDK != "WASI": module sendfile { header "${GLIBC_ARCH_INCLUDE_PATH}/sys/sendfile.h" export * @@ -506,7 +506,7 @@ module SwiftGlibc [system] { header "${GLIBC_ARCH_INCLUDE_PATH}/sys/utsname.h" export * } -% if CMAKE_SDK != "WASM": +% if CMAKE_SDK != "WASI": module wait { header "${GLIBC_ARCH_INCLUDE_PATH}/sys/wait.h" export * @@ -519,7 +519,7 @@ module SwiftGlibc [system] { export * } % end -% if CMAKE_SDK != "WASM": +% if CMAKE_SDK != "WASI": module termios { header "${GLIBC_INCLUDE_PATH}/termios.h" export * @@ -536,7 +536,7 @@ module SwiftGlibc [system] { } } -% if CMAKE_SDK != "WASM": +% if CMAKE_SDK != "WASI": module CUUID [system] { header "${GLIBC_INCLUDE_PATH}/uuid/uuid.h" link "uuid" diff --git a/stdlib/public/runtime/CMakeLists.txt b/stdlib/public/runtime/CMakeLists.txt index f61e59a91e730..cb49fad4e839d 100644 --- a/stdlib/public/runtime/CMakeLists.txt +++ b/stdlib/public/runtime/CMakeLists.txt @@ -92,7 +92,7 @@ list(APPEND swift_runtime_library_compile_flags -I${SWIFT_SOURCE_DIR}/include) set(image_inspection_shared_sdk) if(SWIFT_BUILD_STATIC_STDLIB AND "${SWIFT_HOST_VARIANT_SDK}" STREQUAL "LINUX") set(image_inspection_shared_sdk "${SWIFT_HOST_VARIANT_SDK}") -elseif("${SWIFT_PRIMARY_VARIANT_SDK}" STREQUAL "WASM") +elseif("${SWIFT_PRIMARY_VARIANT_SDK}" STREQUAL "WASI") set(image_inspection_shared_sdk "${SWIFT_PRIMARY_VARIANT_SDK}") endif() diff --git a/utils/build-script b/utils/build-script index da46d62e0cf4c..7271993e9d665 100755 --- a/utils/build-script +++ b/utils/build-script @@ -123,17 +123,17 @@ class BuildScriptInvocation(object): "must be specified") if args.wasm: - if args.wasm_wasi_sdk is None or \ - args.wasm_icu_uc is None or \ - args.wasm_icu_uc_include is None or \ - args.wasm_icu_i18n is None or \ - args.wasm_icu_i18n_include is None or \ - args.wasm_icu_data is None: + if args.wasi_sdk is None or \ + args.wasi_icu_uc is None or \ + args.wasi_icu_uc_include is None or \ + args.wasi_icu_i18n is None or \ + args.wasi_icu_i18n_include is None or \ + args.wasi_icu_data is None: diagnostics.fatal( - "when building for WebAssembly, --wasm-wasi-sdk, " - "--wasm-icu-uc, " - "--wasm-icu-uc-include, --wasm-icu-i18n, " - "--wasm-icu-i18n-include, and --wasm-icu-data " + "when building for WebAssembly, --wasi-sdk, " + "--wasi-icu-uc, " + "--wasi-icu-uc-include, --wasi-icu-i18n, " + "--wasi-icu-i18n-include, and --wasi-icu-data " "must be specified") targets_needing_toolchain = [ @@ -232,7 +232,7 @@ class BuildScriptInvocation(object): StdlibDeploymentTarget.Android.aarch64.name) if args.wasm: args.stdlib_deployment_targets.append( - StdlibDeploymentTarget.Wasm.wasm32.name) + StdlibDeploymentTarget.WASI.wasm32.name) # Infer platform flags from manually-specified configure targets. # This doesn't apply to Darwin platforms, as they are @@ -606,12 +606,12 @@ class BuildScriptInvocation(object): if args.wasm: impl_args += [ - "--wasm-wasi-sdk", args.wasm_wasi_sdk, - "--wasm-icu-uc", args.wasm_icu_uc, - "--wasm-icu-uc-include", args.wasm_icu_uc_include, - "--wasm-icu-i18n", args.wasm_icu_i18n, - "--wasm-icu-i18n-include", args.wasm_icu_i18n_include, - "--wasm-icu-data", args.wasm_icu_data, + "--wasi-sdk", args.wasi_sdk, + "--wasi-icu-uc", args.wasi_icu_uc, + "--wasi-icu-uc-include", args.wasi_icu_uc_include, + "--wasi-icu-i18n", args.wasi_icu_i18n, + "--wasi-icu-i18n-include", args.wasi_icu_i18n_include, + "--wasi-icu-data", args.wasi_icu_data, ] if platform.system() == 'Darwin': diff --git a/utils/build-script-impl b/utils/build-script-impl index 09e21458b9da8..6fe416c60b9ab 100755 --- a/utils/build-script-impl +++ b/utils/build-script-impl @@ -435,7 +435,7 @@ function verify_host_is_supported() { | watchos-armv7k \ | android-armv7 \ | android-aarch64 \ - | wasm-wasm32) + | wasi-wasm32) ;; *) echo "Unknown host tools target: ${host}" @@ -1200,7 +1200,7 @@ function common_cross_c_flags() { watchos-*) echo -n " -arch ${arch} -mwatchos-version-min=${DARWIN_DEPLOYMENT_VERSION_WATCHOS}" ;; - wasm-wasm32) + wasi-wasm32) echo -n " -arch wasm32" ;; esac @@ -1589,12 +1589,12 @@ for host in "${ALL_HOSTS[@]}"; do if [[ ! "${SKIP_BUILD_WASM}" ]]; then cmake_options=( "${cmake_options[@]}" - -DSWIFT_WASM_WASI_SDK_PATH:STRING="${WASM_WASI_SDK}" - -DSWIFT_WASM_wasm32_ICU_UC:STRING="${WASM_ICU_UC}" - -DSWIFT_WASM_wasm32_ICU_UC_INCLUDE:STRING="${WASM_ICU_UC_INCLUDE}" - -DSWIFT_WASM_wasm32_ICU_I18N:STRING="${WASM_ICU_I18N}" - -DSWIFT_WASM_wasm32_ICU_I18N_INCLUDE:STRING="${WASM_ICU_I18N_INCLUDE}" - -DSWIFT_WASM_wasm32_ICU_DATA:STRING="${WASM_ICU_DATA}" + -DSWIFT_WASI_SDK_PATH:STRING="${WASI_SDK}" + -DSWIFT_WASI_wasm32_ICU_UC:STRING="${WASI_ICU_UC}" + -DSWIFT_WASI_wasm32_ICU_UC_INCLUDE:STRING="${WASI_ICU_UC_INCLUDE}" + -DSWIFT_WASI_wasm32_ICU_I18N:STRING="${WASI_ICU_I18N}" + -DSWIFT_WASI_wasm32_ICU_I18N_INCLUDE:STRING="${WASI_ICU_I18N_INCLUDE}" + -DSWIFT_WASI_wasm32_ICU_DATA:STRING="${WASI_ICU_DATA}" ) fi diff --git a/utils/build_swift/build_swift/driver_arguments.py b/utils/build_swift/build_swift/driver_arguments.py index 2af4b117319bc..47dcf1f8f5eed 100644 --- a/utils/build_swift/build_swift/driver_arguments.py +++ b/utils/build_swift/build_swift/driver_arguments.py @@ -1086,19 +1086,19 @@ def create_argument_parser(): in_group('Build settings for Android') - option('--wasm-wasi-sdk', store_path, + option('--wasi-sdk', store_path, help='An absolute path to WASI SDK that will be used as a libc ' 'implementation for Wasm builds') - option('--wasm-icu-uc', store_path, + option('--wasi-icu-uc', store_path, help='Path to libicuuc.so') - option('--wasm-icu-uc-include', store_path, + option('--wasi-icu-uc-include', store_path, help='Path to a directory containing headers for libicuuc') - option('--wasm-icu-i18n', store_path, + option('--wasi-icu-i18n', store_path, help='Path to libicui18n.so') - option('--wasm-icu-i18n-include', store_path, + option('--wasi-icu-i18n-include', store_path, help='Path to a directory containing headers libicui18n') - option('--wasm-icu-data', store_path, + option('--wasi-icu-data', store_path, help='Path to libicudata.so') # ------------------------------------------------------------------------- diff --git a/utils/swift_build_support/swift_build_support/host_specific_configuration.py b/utils/swift_build_support/swift_build_support/host_specific_configuration.py index cacf49e473104..085957cda2828 100644 --- a/utils/swift_build_support/swift_build_support/host_specific_configuration.py +++ b/utils/swift_build_support/swift_build_support/host_specific_configuration.py @@ -202,7 +202,7 @@ def __platforms_to_skip_build(self, args): if not args.build_android: platforms_to_skip_build.add(StdlibDeploymentTarget.Android) if not args.build_wasm: - platforms_to_skip_build.add(StdlibDeploymentTarget.Wasm) + platforms_to_skip_build.add(StdlibDeploymentTarget.WASI) return platforms_to_skip_build def __platforms_to_skip_test(self, args): @@ -243,7 +243,7 @@ def __platforms_to_skip_test(self, args): if not args.test_android: platforms_to_skip_test.add(StdlibDeploymentTarget.Android) if not args.test_wasm: - platforms_to_skip_test.add(StdlibDeploymentTarget.Wasm) + platforms_to_skip_test.add(StdlibDeploymentTarget.WASI) return platforms_to_skip_test @@ -265,5 +265,5 @@ def __platforms_to_skip_test_host(self, args): if not args.test_watchos_host: platforms_to_skip_test_host.add(StdlibDeploymentTarget.AppleWatch) if not args.test_wasm_host: - platforms_to_skip_test_host.add(StdlibDeploymentTarget.Wasm) + platforms_to_skip_test_host.add(StdlibDeploymentTarget.WASI) return platforms_to_skip_test_host diff --git a/utils/swift_build_support/swift_build_support/targets.py b/utils/swift_build_support/swift_build_support/targets.py index fb286d58e7a46..6a2f8f55c5013 100644 --- a/utils/swift_build_support/swift_build_support/targets.py +++ b/utils/swift_build_support/swift_build_support/targets.py @@ -158,7 +158,7 @@ class StdlibDeploymentTarget(object): Haiku = Platform("haiku", archs=["x86_64"]) - Wasm = Platform("wasm", archs=["wasm32"]) + WASI = Platform("wasi", archs=["wasm32"]) # The list of known platforms. known_platforms = [ @@ -172,7 +172,7 @@ class StdlibDeploymentTarget(object): Android, Windows, Haiku, - Wasm] + WASI] # Cache of targets by name. _targets_by_name = dict((target.name, target) @@ -236,9 +236,9 @@ def host_target(): if machine == 'x86_64': return StdlibDeploymentTarget.Haiku.x86_64 - elif system == 'Wasm': + elif system == 'WASI': if machine == 'wasm32': - return StdlibDeploymentTarget.Wasm.wasm32 + return StdlibDeploymentTarget.WASI.wasm32 raise NotImplementedError('System "%s" with architecture "%s" is not ' 'supported' % (system, machine)) From 61efa16b3fd52b963b4b40e4bd6b80d0abe59820 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Sun, 15 Dec 2019 03:47:36 +0000 Subject: [PATCH 66/80] [WebAssembly] Fix merge conflicts --- lib/Basic/Platform.cpp | 2 -- lib/IRGen/MetadataRequest.cpp | 3 --- lib/SIL/TypeLowering.cpp | 12 +++++------- 3 files changed, 5 insertions(+), 12 deletions(-) diff --git a/lib/Basic/Platform.cpp b/lib/Basic/Platform.cpp index 62c0a4d3eb444..9581df610fab2 100644 --- a/lib/Basic/Platform.cpp +++ b/lib/Basic/Platform.cpp @@ -171,8 +171,6 @@ static StringRef getPlatformNameForDarwin(const DarwinPlatformKind platform) { StringRef swift::getPlatformNameForTriple(const llvm::Triple &triple) { switch (triple.getOS()) { case llvm::Triple::UnknownOS: - if (triple.isOSBinFormatWasm()) - return "wasm"; llvm_unreachable("unknown OS"); case llvm::Triple::Ananas: case llvm::Triple::CloudABI: diff --git a/lib/IRGen/MetadataRequest.cpp b/lib/IRGen/MetadataRequest.cpp index 9e8f308e6f129..bf919157c8036 100644 --- a/lib/IRGen/MetadataRequest.cpp +++ b/lib/IRGen/MetadataRequest.cpp @@ -2427,9 +2427,6 @@ emitMetadataAccessByMangledName(IRGenFunction &IGF, CanType type, stringAddrOffset); stringAddr = subIGF.Builder.CreateIntToPtr(stringAddr, IGM.Int8PtrTy); } - auto stringAddr = subIGF.Builder.CreateAdd(stringAddrBase, - stringAddrOffset); - stringAddr = subIGF.Builder.CreateIntToPtr(stringAddr, IGM.Int8PtrTy); llvm::CallInst *call; if (request.isStaticallyAbstract()) { diff --git a/lib/SIL/TypeLowering.cpp b/lib/SIL/TypeLowering.cpp index a54f106db3558..1ce304e85296e 100644 --- a/lib/SIL/TypeLowering.cpp +++ b/lib/SIL/TypeLowering.cpp @@ -2645,6 +2645,11 @@ TypeConverter::checkFunctionForABIDifferences(SILModule &M, if (rep1 != rep2) { if (rep1 == SILFunctionTypeRepresentation::Thin && rep2 == SILFunctionTypeRepresentation::Thick) { + // There is no ABI compatibility between thin and thick on WebAssembly, + // so need thunk. + if (M.getASTContext().LangOpts.Target.isOSBinFormatWasm()) { + return ABIDifference::NeedsThunk; + } if (DifferentFunctionTypesHaveDifferentRepresentation) { // FIXME: check whether the representations are compatible modulo // context @@ -2652,13 +2657,6 @@ TypeConverter::checkFunctionForABIDifferences(SILModule &M, } else { return ABIDifference::CompatibleRepresentation_ThinToThick; } - - // There is no ABI compatibility between thin and thick on WebAssembly, - // so need thunk. - if (M.getASTContext().LangOpts.Target.isOSBinFormatWasm()) { - return ABIDifference::NeedsThunk; - } - return ABIDifference::ThinToThick; } return ABIDifference::NeedsThunk; } From c8b92811aba7c9d2ab0cea77e6a133815f5affec Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Sun, 15 Dec 2019 04:12:16 +0000 Subject: [PATCH 67/80] [WebAssembly] Fix target triple for CI --- utils/webassembly/sdkroot/swiftwasm | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/utils/webassembly/sdkroot/swiftwasm b/utils/webassembly/sdkroot/swiftwasm index d87894df816cd..278ce7dbc256a 100755 --- a/utils/webassembly/sdkroot/swiftwasm +++ b/utils/webassembly/sdkroot/swiftwasm @@ -17,17 +17,17 @@ fi sysroot="$(dirname $0)/wasi-sdk/share/sysroot" abssysroot="$(cd "$(dirname "$sysroot")" && pwd)/$(basename "$sysroot")" -"$sdk/extra_utils/generateModulemap.sh" "$abssysroot" >"$sdk/opt/swiftwasm-sdk/lib/swift/wasm/wasm32/glibc.modulemap" +"$sdk/extra_utils/generateModulemap.sh" "$abssysroot" >"$sdk/opt/swiftwasm-sdk/lib/swift/wasi/wasm32/glibc.modulemap" -"$sdk/opt/swiftwasm-sdk/bin/swiftc" -target wasm32-unknown-unknown-wasm \ +"$sdk/opt/swiftwasm-sdk/bin/swiftc" -target wasm32-unknown-unknown-wasi \ -sdk "$sysroot" -O -c \ -o "$tmpobj" \ "${@:1:$#-1}" "$sdk/wasi-sdk/bin/wasm-ld" --error-limit=0 -o "$outputfile" \ "$sysroot/lib/wasm32-wasi/crt1.o" \ - "$sdk/opt/swiftwasm-sdk/lib/swift_static/wasm/wasm32/swiftrt.o" \ + "$sdk/opt/swiftwasm-sdk/lib/swift_static/wasi/wasm32/swiftrt.o" \ "$tmpobj" \ - "-L$sdk/opt/swiftwasm-sdk/lib/swift_static/wasm" \ + "-L$sdk/opt/swiftwasm-sdk/lib/swift_static/wasi" \ "-L$sysroot/lib/wasm32-wasi" \ "-L$sdk/icu_out/lib" \ -lswiftCore \ From 433b143caf8e1dd97fd8a68f28cd5c90a393ccd6 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Sun, 15 Dec 2019 05:04:44 +0000 Subject: [PATCH 68/80] Fix build comand script for macOS --- build-mac.sh | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/build-mac.sh b/build-mac.sh index 90a8be35acb1e..b9dd3a718ded2 100755 --- a/build-mac.sh +++ b/build-mac.sh @@ -5,7 +5,7 @@ export sourcedir=$PWD/.. ./utils/build-script --release --wasm --verbose \ --skip-build-benchmarks \ --extra-cmake-options=" \ - -DSWIFT_PRIMARY_VARIANT_SDK:STRING=WASM \ + -DSWIFT_PRIMARY_VARIANT_SDK:STRING=WASI \ -DSWIFT_PRIMARY_VARIANT_ARCH:STRING=wasm32 \ -DSWIFT_OSX_x86_64_ICU_STATICLIB=TRUE \ -DSWIFT_BUILD_SOURCEKIT=FALSE \ @@ -19,12 +19,12 @@ export sourcedir=$PWD/.. --build-swift-static-stdlib \ --llvm-targets-to-build "X86;WebAssembly" \ --stdlib-deployment-targets "wasi-wasm32" \ - --wasm-icu-data "todo-icu-data" \ - --wasm-icu-i18n "$sourcedir/icu_out/lib" \ - --wasm-icu-i18n-include "$sourcedir/icu_out/include" \ - --wasm-icu-uc "$sourcedir/icu_out/lib" \ - --wasm-icu-uc-include "$sourcedir/icu_out/include" \ - --wasm-wasi-sdk "$sourcedir/wasi-sdk" \ + --wasi-icu-data "todo-icu-data" \ + --wasi-icu-i18n "$sourcedir/icu_out/lib" \ + --wasi-icu-i18n-include "$sourcedir/icu_out/include" \ + --wasi-icu-uc "$sourcedir/icu_out/lib" \ + --wasi-icu-uc-include "$sourcedir/icu_out/include" \ + --wasi-sdk "$sourcedir/wasi-sdk" \ --install-swift \ --install-prefix="/opt/swiftwasm-sdk" \ --install-destdir="$sourcedir/install" \ From 0dea906c909648cd60f328fc4aa2ea1e5aefd745 Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Sun, 15 Dec 2019 22:42:49 +0000 Subject: [PATCH 69/80] Fix paths in packaging scripts (#23) `wasm` was replaced with `wasi` in the platform triple, the packaging scripts are now updated accordingly. --- utils/webassembly/copy-shared-files.sh | 2 +- utils/webassembly/macos/unpack-prebuilts.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/utils/webassembly/copy-shared-files.sh b/utils/webassembly/copy-shared-files.sh index 7e1ce8a81832d..daaf0c5d30f82 100755 --- a/utils/webassembly/copy-shared-files.sh +++ b/utils/webassembly/copy-shared-files.sh @@ -1,4 +1,4 @@ #!/bin/bash set -e cp -r ../sdkroot/* compiler/ -cp ../linux/compiler/opt/swiftwasm-sdk/lib/swift/wasm/wasm32/glibc.modulemap compiler/extra_utils +cp ../linux/compiler/opt/swiftwasm-sdk/lib/swift/wasi/wasm32/glibc.modulemap compiler/extra_utils diff --git a/utils/webassembly/macos/unpack-prebuilts.sh b/utils/webassembly/macos/unpack-prebuilts.sh index 169dd8cde7ce5..199552346db2e 100755 --- a/utils/webassembly/macos/unpack-prebuilts.sh +++ b/utils/webassembly/macos/unpack-prebuilts.sh @@ -24,7 +24,7 @@ mv compiler/wasi-sdk/share/wasi-sysroot compiler/wasi-sdk/share/sysroot rm -r compiler/wasi-sdk/bin mkdir compiler/wasi-sdk/bin cp tmpdir/clang+llvm-*-x86_64-darwin-apple/bin/wasm-ld compiler/wasi-sdk/bin -cp -a tmpdir/opt/swiftwasm-sdk/lib/swift/wasm compiler/opt/swiftwasm-sdk/lib/swift/wasm +cp -a tmpdir/opt/swiftwasm-sdk/lib/swift/wasi compiler/opt/swiftwasm-sdk/lib/swift/wasi cp -a tmpdir/opt/swiftwasm-sdk/lib/swift_static compiler/opt/swiftwasm-sdk/lib/swift_static # ok, finally copy over the shared files ../copy-shared-files.sh || true From 4ad17cf669029ad9a345abf50997f16d34bb88d9 Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Wed, 18 Dec 2019 08:03:24 +0000 Subject: [PATCH 70/80] Bump macOS to 10.15 in GitHub Actions (#25) * Bump macOS to 10.15 in GitHub Actions Potentially this should enable switching to Xcode 11.2.1 and even 11.3 when the latter is available. * Use macos-latest image in main.yml --- .github/workflows/main.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 46d9785712789..8ea42eb65111b 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -25,7 +25,7 @@ jobs: macos_build: timeout-minutes: 0 - runs-on: macOS-10.14 + runs-on: macos-latest steps: - uses: actions/checkout@v1 @@ -100,7 +100,7 @@ jobs: macos_smoke_test: name: Compile hello.swift on macOS - runs-on: macOS-10.14 + runs-on: macos-latest needs: package steps: - name: Download SwiftWasm macOS package From af61a0ecf1669152c78b5414c2f1cf8a49dbdc23 Mon Sep 17 00:00:00 2001 From: Karl Date: Thu, 19 Dec 2019 16:08:31 +0100 Subject: [PATCH 71/80] Remove fakeld (#26) * Remove fakeld It isn't needed if we don't try to build dynamic libraries for WASM. * Don't build StdlibUnittestFoundationExtras when cross-compiling on Darwin * Build static SDK overlay on WASI --- CMakeLists.txt | 10 +++------- build-linux.sh | 3 +++ build-mac.sh | 3 ++- cmake/modules/AddSwift.cmake | 10 +++++++--- fakeld | 9 --------- stdlib/private/CMakeLists.txt | 3 ++- 6 files changed, 17 insertions(+), 21 deletions(-) delete mode 100755 fakeld diff --git a/CMakeLists.txt b/CMakeLists.txt index 0248830db36eb..c9c5789db7aa6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -784,13 +784,9 @@ endif() # Should we cross-compile the standard library for WebAssembly (WASI)? is_sdk_requested(WASI swift_build_wasm) if(swift_build_wasm AND NOT "${SWIFT_HOST_VARIANT_SDK}" STREQUAL "WASI") - #if ("${SWIFT_ANDROID_NDK_PATH}" STREQUAL "") - # message(FATAL_ERROR "You must set SWIFT_ANDROID_NDK_PATH to cross-compile the Swift runtime for Android") - #endif() - #if (NOT ("${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "Darwin" OR "${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "Linux")) - # message(FATAL_ERROR "A Darwin or Linux host is required to build the Swift runtime for Android") - #endif() - + if(SWIFT_BUILD_DYNAMIC_SDK_OVERLAY OR SWIFT_BUILD_DYNAMIC_STDLIB) + message(FATAL_ERROR "Unable to build dynamic overlay/stdlib for WASI. WASM does not support dynamic linking.") + endif() if("${SWIFT_SDK_WASI_ARCHITECTURES}" STREQUAL "") set(SWIFT_SDK_WASI_ARCHITECTURES wasm32) endif() diff --git a/build-linux.sh b/build-linux.sh index d5bad958f906d..c13e91b796dfc 100755 --- a/build-linux.sh +++ b/build-linux.sh @@ -12,6 +12,9 @@ export sourcedir=$PWD/.. -DCMAKE_RANLIB='$sourcedir/wasi-sdk/bin/llvm-ranlib' \ " \ --build-stdlib-deployment-targets "wasi-wasm32" \ + --build-swift-dynamic-sdk-overlay false \ + --build-swift-dynamic-stdlib false \ + --build-swift-static-sdk-overlay \ --build-swift-static-stdlib \ --install-destdir="$sourcedir/install" \ --install-prefix="/opt/swiftwasm-sdk" \ diff --git a/build-mac.sh b/build-mac.sh index b9dd3a718ded2..ba621a52b1636 100755 --- a/build-mac.sh +++ b/build-mac.sh @@ -15,7 +15,8 @@ export sourcedir=$PWD/.. " \ --build-stdlib-deployment-targets "wasi-wasm32" \ --build-swift-dynamic-sdk-overlay false \ - --build-swift-static-sdk-overlay false \ + --build-swift-dynamic-stdlib false \ + --build-swift-static-sdk-overlay \ --build-swift-static-stdlib \ --llvm-targets-to-build "X86;WebAssembly" \ --stdlib-deployment-targets "wasi-wasm32" \ diff --git a/cmake/modules/AddSwift.cmake b/cmake/modules/AddSwift.cmake index 886d8172c1ca3..25a918c31d94a 100644 --- a/cmake/modules/AddSwift.cmake +++ b/cmake/modules/AddSwift.cmake @@ -582,9 +582,7 @@ function(_add_variant_link_flags) if(NOT SWIFT_COMPILER_IS_MSVC_LIKE) # FIXME: On Apple platforms, find_program needs to look for "ld64.lld" find_program(LDLLD_PATH "ld.lld") - if("${SWIFT_SDK_${LFLAGS_SDK}_OBJECT_FORMAT}" STREQUAL "WASM") - list(APPEND result "-fuse-ld=${CMAKE_SOURCE_DIR}/fakeld") - elseif((SWIFT_ENABLE_LLD_LINKER AND LDLLD_PATH AND NOT APPLE) OR + if((SWIFT_ENABLE_LLD_LINKER AND LDLLD_PATH AND NOT APPLE) OR ("${LFLAGS_SDK}" STREQUAL "WINDOWS" AND NOT "${CMAKE_SYSTEM_NAME}" STREQUAL "WINDOWS")) list(APPEND result "-fuse-ld=lld") @@ -1404,6 +1402,12 @@ function(_add_swift_library_single target name) list(APPEND c_compile_flags -D_WINDLL) endif() endif() + # Double-check that we're not trying to build a dynamic library for WASM. + if(SWIFTLIB_SINGLE_SDK MATCHES WASM) + if(libkind STREQUAL SHARED) + message(FATAL_ERROR "WASM does not support shared libraries.") + endif() + endif() _add_variant_link_flags( SDK "${SWIFTLIB_SINGLE_SDK}" ARCH "${SWIFTLIB_SINGLE_ARCHITECTURE}" diff --git a/fakeld b/fakeld deleted file mode 100755 index 1df157004de91..0000000000000 --- a/fakeld +++ /dev/null @@ -1,9 +0,0 @@ -#!/usr/bin/env python3 -import sys -def outputname(): - for i in range(len(sys.argv)): - if sys.argv[i] == "-o": - return sys.argv[i + 1] - return "a.out" -with open(outputname(), "wb") as outfile: - pass diff --git a/stdlib/private/CMakeLists.txt b/stdlib/private/CMakeLists.txt index 45cf31363574b..9e900d1178773 100644 --- a/stdlib/private/CMakeLists.txt +++ b/stdlib/private/CMakeLists.txt @@ -18,7 +18,8 @@ if(SWIFT_BUILD_SDK_OVERLAY) add_subdirectory(OSLog) - if(CMAKE_SYSTEM_NAME STREQUAL "Darwin") + list_intersect("${SWIFT_APPLE_PLATFORMS}" "${SWIFT_SDKS}" building_darwin_sdks) + if(building_darwin_sdks) add_subdirectory(StdlibUnittestFoundationExtras) if (SWIFT_INCLUDE_TESTS) add_subdirectory(SwiftReflectionTest) From b4f0d39d97e6371fb37e0393ddfad3bfa7eab29c Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Wed, 25 Dec 2019 01:12:36 +0900 Subject: [PATCH 72/80] Re-enable tests (#24) * [WASM] Re-enable test targets * [WASM] Support WASI OS target for lit.cfg * [WASM] Link wasm32-wasi-unknown to wasm32-wasi * [WASM] Add sysroot to clang_linker to find crt1.o * [WASM] Separate WebAssembly test from linux * [WASM] Add static-executable-args.lnk for wasm * [WASM] First support of wasm in autolink-extract * [WASM] Extract link flags from named data segment from wasm object file * [WASM] Fix stdlib build on Linux * [WASM] Fix ICU lib flag to specify lib file instead of dir * [WASM] wip * [WASM] Iterate all configured sdks * [WASM] Remove object format specific code from macro * [WASM] Copy libswiftImageInspection.a to lib/swift_static/wasi * Use brew installed clang because Xcode version is old * [WASM] Fix new wasm/wasi triple * [WASM] Run executable test on wasmtime * Fix typo * [WASM] Cut environment from triple * Move build script into wasm dir * Run test on CI * Cleanup unused scripts * [WASM] Use -static instead of -static-executable to work emit-library * Proxy arguments to build-script * Ignore test failure temporarily * Fix packing command * Add missing x * Use wasi-sdk's clang compiler * [WASM] Avoid to build BlocksRuntime temporary due to os toolchains's clang limitation * [WASM] Comment out utime.h from glibc * [WASM] Change sysroot dir as wasi-sysroot * [WASM] Avoid to build BlocksRuntime on linux * [WASM] Add mman flag for wasi This eliminate clang hack which defines _WASI_EMULATED_MMAN as a predefined macro in clang ref: https://github.com/apple/llvm-project/compare/swift/master...swiftwasm:swiftwasm#diff-773abe7c69fccf723aa2d75447faa136R63-R66 * [WASM] Use latest wasi-sdk * [WASM] Avoid to build swift-reflection-test temporarily * [WASM] Install wasmtime on CI * [WASM] Set wasm as primary target to avoid to build SwiftRuntimeTests * [WASM] Fix macro arguments validation * [WASM] Copy ICU libs into toolchain * [WASM] Fix to specify libicu on mac * Remove extra space from build scripts --- .github/workflows/main.yml | 19 +++- CMakeLists.txt | 4 +- build-linux.sh | 30 ------ buildstartend.sh | 4 - ci-linux.sh | 34 ------- ci-mac.sh | 21 ---- cmake/modules/AddSwift.cmake | 6 +- cmake/modules/SwiftConfigureSDK.cmake | 9 +- lib/Driver/CMakeLists.txt | 43 +++++++- linkPlease.sh | 17 ---- stdlib/public/Platform/glibc.modulemap.gyb | 2 + stdlib/public/runtime/CMakeLists.txt | 99 +++++++++++++------ test/CMakeLists.txt | 42 ++++---- test/lit.cfg | 63 ++++++++++++ test/lit.site.cfg.in | 3 + tools/driver/autolink_extract_main.cpp | 29 ++++++ utils/webassembly/build-linux.sh | 35 +++++++ .../webassembly/build-mac.sh | 23 +++-- utils/webassembly/ci-linux.sh | 53 ++++++++++ utils/webassembly/ci-mac.sh | 43 ++++++++ utils/webassembly/static-executable-args.lnk | 14 +++ utils/webassembly/static-stdlib-args.lnk | 15 +++ vvv.sh | 11 --- 23 files changed, 427 insertions(+), 192 deletions(-) delete mode 100755 build-linux.sh delete mode 100755 buildstartend.sh delete mode 100755 ci-linux.sh delete mode 100755 ci-mac.sh delete mode 100755 linkPlease.sh create mode 100755 utils/webassembly/build-linux.sh rename build-mac.sh => utils/webassembly/build-mac.sh (55%) create mode 100755 utils/webassembly/ci-linux.sh create mode 100755 utils/webassembly/ci-mac.sh create mode 100644 utils/webassembly/static-executable-args.lnk create mode 100644 utils/webassembly/static-stdlib-args.lnk delete mode 100755 vvv.sh diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 8ea42eb65111b..f300cbd721967 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -16,12 +16,19 @@ jobs: steps: - uses: actions/checkout@v1 - name: Build Linux installable archive - run: ./ci-linux.sh + run: ./utils/webassembly/ci-linux.sh - name: Upload Linux installable archive uses: actions/upload-artifact@v1 with: name: linux-installable path: ../swiftwasm-linux.tar.gz + - name: Pack test results + run: tar cJf swift-test-results.tar.gz ../build/*/swift-linux-x86_64/swift-test-results + - name: Upload test results + uses: actions/upload-artifact@v1 + with: + name: linux-test-results + path: ./swift-test-results.tar.gz macos_build: timeout-minutes: 0 @@ -30,7 +37,7 @@ jobs: steps: - uses: actions/checkout@v1 - name: Build macOS installable archive - run: ./ci-mac.sh + run: ./utils/webassembly/ci-mac.sh - name: Upload macOS installable archive uses: actions/upload-artifact@v1 with: @@ -41,7 +48,13 @@ jobs: with: name: packaging-scripts path: utils/webassembly - + - name: Pack test results + run: tar cJf swift-test-results.tar.gz ../build/*/swift-macosx-x86_64/swift-test-results + - name: Upload test results + uses: actions/upload-artifact@v1 + with: + name: macos-test-results + path: ./swift-test-results.tar.gz package: name: Build SwiftWasm packages needs: diff --git a/CMakeLists.txt b/CMakeLists.txt index c9c5789db7aa6..0f0b9d9f7598c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1117,8 +1117,8 @@ if ("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin") endif() if(SWIFT_INCLUDE_TESTS) - #add_subdirectory(test) - #add_subdirectory(unittests) + add_subdirectory(test) + add_subdirectory(unittests) endif() if(SWIFT_INCLUDE_DOCS) add_subdirectory(docs) diff --git a/build-linux.sh b/build-linux.sh deleted file mode 100755 index c13e91b796dfc..0000000000000 --- a/build-linux.sh +++ /dev/null @@ -1,30 +0,0 @@ -#/bin/bash - -export sourcedir=$PWD/.. - -./utils/build-script --release --wasm --verbose \ - --skip-build-benchmarks \ - --extra-cmake-options=" \ - -DSWIFT_SDKS='WASI;LINUX' \ - -DSWIFT_BUILD_SOURCEKIT=FALSE \ - -DSWIFT_ENABLE_SOURCEKIT_TESTS=FALSE \ - -DCMAKE_AR='$sourcedir/wasi-sdk/bin/llvm-ar' \ - -DCMAKE_RANLIB='$sourcedir/wasi-sdk/bin/llvm-ranlib' \ - " \ - --build-stdlib-deployment-targets "wasi-wasm32" \ - --build-swift-dynamic-sdk-overlay false \ - --build-swift-dynamic-stdlib false \ - --build-swift-static-sdk-overlay \ - --build-swift-static-stdlib \ - --install-destdir="$sourcedir/install" \ - --install-prefix="/opt/swiftwasm-sdk" \ - --install-swift \ - --installable-package="$sourcedir/swiftwasm-linux.tar.gz" \ - --llvm-targets-to-build "X86;WebAssembly" \ - --stdlib-deployment-targets "wasi-wasm32" \ - --wasi-icu-data "todo-icu-data" \ - --wasi-icu-i18n "$sourcedir/icu_out/lib" \ - --wasi-icu-i18n-include "$sourcedir/icu_out/include" \ - --wasi-icu-uc "$sourcedir/icu_out/lib" \ - --wasi-icu-uc-include "$sourcedir/icu_out/include" \ - --wasi-sdk "$sourcedir/wasi-sdk" diff --git a/buildstartend.sh b/buildstartend.sh deleted file mode 100755 index 4739c828f17f9..0000000000000 --- a/buildstartend.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/bash -set -e -/home/zhuowei/wasi-sdk/bin/clang++ -c swift_start.cpp -/home/zhuowei/wasi-sdk/bin/clang++ -c swift_end.cpp diff --git a/ci-linux.sh b/ci-linux.sh deleted file mode 100755 index 467e38b1f1403..0000000000000 --- a/ci-linux.sh +++ /dev/null @@ -1,34 +0,0 @@ -#/bin/bash - -sudo apt update -sudo apt install \ - git ninja-build clang python \ - uuid-dev libicu-dev icu-devtools libbsd-dev \ - libedit-dev libxml2-dev libsqlite3-dev swig \ - libpython-dev libncurses5-dev pkg-config \ - libblocksruntime-dev libcurl4-openssl-dev \ - systemtap-sdt-dev tzdata rsync wget - -export current_sha=`git rev-parse HEAD` -./utils/update-checkout --clone --scheme wasm -git checkout $current_sha -export sourcedir=$PWD/.. -cd $sourcedir - -wget -O install_cmake.sh "https://github.com/Kitware/CMake/releases/download/v3.15.3/cmake-3.15.3-Linux-x86_64.sh" -chmod +x install_cmake.sh -sudo mkdir -p /opt/cmake -sudo ./install_cmake.sh --skip-license --prefix=/opt/cmake -sudo ln -sf /opt/cmake/bin/* /usr/local/bin -cmake --version - -wget -O wasi-sdk.tar.gz https://github.com/swiftwasm/wasi-sdk/releases/download/20191022.1/wasi-sdk-4.39g3025a5f47c04-linux.tar.gz -tar xfz wasi-sdk.tar.gz -mv wasi-sdk-4.39g3025a5f47c04 ./wasi-sdk -mv wasi-sdk/share/wasi-sysroot wasi-sdk/share/sysroot - -wget -O icu.tar.xz "https://github.com/swiftwasm/icu4c-wasi/releases/download/20190421.3/icu4c-wasi.tar.xz" -tar xf icu.tar.xz - -cd swift -./build-linux.sh diff --git a/ci-mac.sh b/ci-mac.sh deleted file mode 100755 index d3a08d7164af9..0000000000000 --- a/ci-mac.sh +++ /dev/null @@ -1,21 +0,0 @@ -#/bin/bash - -brew install cmake ninja llvm -export current_sha=`git rev-parse HEAD` -./utils/update-checkout --clone --scheme wasm -git checkout $current_sha -export sourcedir=$PWD/.. -cd $sourcedir -wget -O wasi-sdk.tar.gz https://github.com/swiftwasm/wasi-sdk/releases/download/20191022.1/wasi-sdk-4.39g3025a5f47c04-linux.tar.gz -tar xfz wasi-sdk.tar.gz -mv wasi-sdk-4.39g3025a5f47c04 ./wasi-sdk -mv wasi-sdk/share/wasi-sysroot wasi-sdk/share/sysroot -# Link sysroot/usr/include to sysroot/include because Darwin sysroot doesn't -# find header files in sysroot/include but sysroot/usr/include -mkdir wasi-sdk/share/sysroot/usr/ -ln -s ../include wasi-sdk/share/sysroot/usr/include -wget -O icu.tar.xz "https://github.com/swiftwasm/icu4c-wasi/releases/download/20190421.3/icu4c-wasi.tar.xz" -tar xf icu.tar.xz - -cd swift -./build-mac.sh diff --git a/cmake/modules/AddSwift.cmake b/cmake/modules/AddSwift.cmake index 25a918c31d94a..567616670f283 100644 --- a/cmake/modules/AddSwift.cmake +++ b/cmake/modules/AddSwift.cmake @@ -366,6 +366,8 @@ function(_add_variant_c_compile_flags) list(APPEND result -isystem;${path}) endforeach() list(APPEND result "-D__ANDROID_API__=${SWIFT_ANDROID_API_LEVEL}") + elseif("${CFLAGS_SDK}" STREQUAL "WASI") + list(APPEND result "-D_WASI_EMULATED_MMAN") elseif(CFLAGS_SDK STREQUAL WINDOWS) swift_windows_include_for_arch(${CFLAGS_ARCH} ${CFLAGS_ARCH}_INCLUDE) foreach(path ${${CFLAGS_ARCH}_INCLUDE}) @@ -439,6 +441,8 @@ function(_add_variant_swift_compile_flags foreach(path IN LISTS ${arch}_swift_include) list(APPEND result "\"${CMAKE_INCLUDE_FLAG_C}${path}\"") endforeach() + elseif("${sdk}" STREQUAL "WASI") + list(APPEND result "-Xcc" "-D_WASI_EMULATED_MMAN") endif() if(NOT BUILD_STANDALONE) @@ -552,7 +556,7 @@ function(_add_variant_link_flags) list(APPEND library_search_directories ${path}) endforeach() elseif("${LFLAGS_SDK}" STREQUAL "WASI") - # No extra libraries needed. + list(APPEND result "-Wl,wasi-emulated-mman") else() # If lto is enabled, we need to add the object path flag so that the LTO code # generator leaves the intermediate object file in a place where it will not diff --git a/cmake/modules/SwiftConfigureSDK.cmake b/cmake/modules/SwiftConfigureSDK.cmake index a81763d6fe22c..5862add80e424 100644 --- a/cmake/modules/SwiftConfigureSDK.cmake +++ b/cmake/modules/SwiftConfigureSDK.cmake @@ -339,11 +339,10 @@ macro(configure_sdk_unix name architectures) if(NOT arch STREQUAL wasm32) message(FATAL_ERROR "unsupported arch for WebAssembly: ${arch}") endif() - set(SWIFT_SDK_WASI_ARCH_wasm32_PATH "${SWIFT_WASI_SDK_PATH}/share/sysroot") - # fixme: Wasi is wasm32-unknown-wasi-musl. This LLVM doesn't have it yet. - set(SWIFT_SDK_WASI_ARCH_wasm32_TRIPLE "wasm32-unknown-unknown-wasi") - set(SWIFT_SDK_WASI_ARCH_wasm32_LIBC_INCLUDE_DIRECTORY "${SWIFT_WASI_SDK_PATH}/share/sysroot/include") - set(SWIFT_SDK_WASI_ARCH_wasm32_LIBC_ARCHITECTURE_INCLUDE_DIRECTORY "${SWIFT_WASI_SDK_PATH}/share/sysroot/include") + set(SWIFT_SDK_WASI_ARCH_wasm32_PATH "${SWIFT_WASI_SDK_PATH}/share/wasi-sysroot") + set(SWIFT_SDK_WASI_ARCH_wasm32_TRIPLE "wasm32-unknown-wasi") + set(SWIFT_SDK_WASI_ARCH_wasm32_LIBC_INCLUDE_DIRECTORY "${SWIFT_WASI_SDK_PATH}/share/wasi-sysroot/include") + set(SWIFT_SDK_WASI_ARCH_wasm32_LIBC_ARCHITECTURE_INCLUDE_DIRECTORY "${SWIFT_WASI_SDK_PATH}/share/wasi-sysroot/include") else() message(FATAL_ERROR "unknown Unix OS: ${prefix}") endif() diff --git a/lib/Driver/CMakeLists.txt b/lib/Driver/CMakeLists.txt index 83d6afa8ca55c..d1c4893e30152 100644 --- a/lib/Driver/CMakeLists.txt +++ b/lib/Driver/CMakeLists.txt @@ -33,9 +33,8 @@ target_link_libraries(swiftDriver PRIVATE if(SWIFT_BUILD_STATIC_STDLIB) set(static_stdlib_lnk_file_list) foreach(sdk ${SWIFT_CONFIGURED_SDKS}) - if("${SWIFT_SDK_${sdk}_OBJECT_FORMAT}" STREQUAL "ELF" OR - "${SWIFT_SDK_${sdk}_OBJECT_FORMAT}" STREQUAL "WASM") - string(TOLOWER "${sdk}" lowercase_sdk) + string(TOLOWER "${sdk}" lowercase_sdk) + if("${SWIFT_SDK_${sdk}_OBJECT_FORMAT}" STREQUAL "ELF") if(SWIFT_${SWIFT_HOST_VARIANT_SDK}_${SWIFT_HOST_VARIANT_ARCH}_ICU_STATICLIB) set(ICU_STATICLIB "TRUE") else() @@ -62,6 +61,44 @@ if(SWIFT_BUILD_STATIC_STDLIB) swift_install_in_component(FILES "${SWIFTSTATICLIB_DIR}/${linkfile}" DESTINATION "lib/swift_static/${lowercase_sdk}" COMPONENT stdlib) + elseif("${sdk}" STREQUAL "WASI") + set(linkfile_src "${SWIFT_SOURCE_DIR}/utils/webassembly/static-stdlib-args.lnk") + set(linkfile "${lowercase_sdk}/static-stdlib-args.lnk") + add_custom_command_target(swift_static_stdlib_${sdk}_args + COMMAND + "${CMAKE_COMMAND}" -E copy + "${linkfile_src}" + "${SWIFTSTATICLIB_DIR}/${linkfile}" + OUTPUT + "${SWIFTSTATICLIB_DIR}/${linkfile}" + DEPENDS + "${linkfile_src}") + + list(APPEND static_stdlib_lnk_file_list ${swift_static_stdlib_${sdk}_args}) + swift_install_in_component(FILES "${SWIFTSTATICLIB_DIR}/${linkfile}" + DESTINATION "lib/swift_static/${lowercase_sdk}" + COMPONENT stdlib) + set(swift_icu_libs_wasi_list) + set(icu_modules UC I18N DATA) + foreach(module IN LISTS icu_modules) + set(module_lib "${SWIFT_WASI_wasm32_ICU_${module}}") + get_filename_component(module_lib_name ${module_lib} NAME) + add_custom_command_target(swift_icu_${module}_${sdk} + COMMAND + "${CMAKE_COMMAND}" -E copy + "${module_lib}" + "${SWIFTSTATICLIB_DIR}/${lowercase_sdk}/${module_lib_name}" + OUTPUT + "${SWIFTSTATICLIB_DIR}/${lowercase_sdk}/${module_lib_name}" + DEPENDS + "${module_lib}") + list(APPEND swift_icu_libs_wasi_list ${swift_icu_${module}_${sdk}}) + swift_install_in_component(FILES "${SWIFTSTATICLIB_DIR}/${lowercase_sdk}/${module_lib_name}" + DESTINATION "lib/swift_static/${lowercase_sdk}" + COMPONENT stdlib) + endforeach() + add_custom_target(swift_icu_libs_wasi ALL DEPENDS ${swift_icu_libs_wasi_list}) + add_dependencies(stdlib swift_icu_libs_wasi) endif() endforeach() add_custom_target(swift_static_lnk_args ALL DEPENDS ${static_stdlib_lnk_file_list}) diff --git a/linkPlease.sh b/linkPlease.sh deleted file mode 100755 index d3b160ed6bd72..0000000000000 --- a/linkPlease.sh +++ /dev/null @@ -1,17 +0,0 @@ -#!/bin/sh -# Copy to ../build/Ninja-RelWithDebInfoAssert/swift-linux-x86_64/bin -exec /home/zhuowei/wasi-sdk/bin/wasm-ld --error-limit=0 -o hello.wasm \ - /home/zhuowei/wasi-sdk/share/sysroot/lib/wasm32-wasi/crt1.o \ - /home/zhuowei/swift-source/swift/swift_start.o \ - ../lib/swift_static/wasm/wasm32/swiftrt.o \ - hello.o \ - -L../lib/swift_static/wasm/wasm32 \ - -L../lib/swift/wasm/wasm32 \ - -L/home/zhuowei/wasi-sdk/share/sysroot/lib/wasm32-wasi \ - -L/home/zhuowei/Documents/BuildICU/icu_out/lib \ - -lswiftCore \ - -lc -lc++ -lc++abi -lswiftImageInspectionShared \ - -licuuc -licudata \ - /home/zhuowei/wasi-sdk/lib/clang/8.0.0/lib/wasi/libclang_rt.builtins-wasm32.a /home/zhuowei/Documents/FakePthread/*.o \ - /home/zhuowei/swift-source/swift/swift_end.o \ - --verbose --no-gc-sections diff --git a/stdlib/public/Platform/glibc.modulemap.gyb b/stdlib/public/Platform/glibc.modulemap.gyb index 5207835d369d2..963d3c7990cb7 100644 --- a/stdlib/public/Platform/glibc.modulemap.gyb +++ b/stdlib/public/Platform/glibc.modulemap.gyb @@ -529,10 +529,12 @@ module SwiftGlibc [system] { header "${GLIBC_INCLUDE_PATH}/unistd.h" export * } +% if CMAKE_SDK != "WASI": module utime { header "${GLIBC_INCLUDE_PATH}/utime.h" export * } +% end } } diff --git a/stdlib/public/runtime/CMakeLists.txt b/stdlib/public/runtime/CMakeLists.txt index cb49fad4e839d..702cf69df2207 100644 --- a/stdlib/public/runtime/CMakeLists.txt +++ b/stdlib/public/runtime/CMakeLists.txt @@ -89,86 +89,121 @@ set(swift_runtime_library_compile_flags ${swift_runtime_compile_flags}) list(APPEND swift_runtime_library_compile_flags -DswiftCore_EXPORTS) list(APPEND swift_runtime_library_compile_flags -I${SWIFT_SOURCE_DIR}/include) -set(image_inspection_shared_sdk) -if(SWIFT_BUILD_STATIC_STDLIB AND "${SWIFT_HOST_VARIANT_SDK}" STREQUAL "LINUX") - set(image_inspection_shared_sdk "${SWIFT_HOST_VARIANT_SDK}") -elseif("${SWIFT_PRIMARY_VARIANT_SDK}" STREQUAL "WASI") - set(image_inspection_shared_sdk "${SWIFT_PRIMARY_VARIANT_SDK}") -endif() - -if(NOT "${image_inspection_shared_sdk}" STREQUAL "") - set(sdk "${image_inspection_shared_sdk}") - list(REMOVE_ITEM swift_runtime_sources ImageInspectionELF.cpp) - set(static_binary_lnk_file_list) +set(static_binary_lnk_file_list) +set(static_binary_dependencies_list) +macro(add_image_inspection_shared sdk primary_arch inspection_file linkfile_src) + if(${inspection_file} IN_LIST swift_runtime_sources) + list(REMOVE_ITEM swift_runtime_sources ${inspection_file}) + endif() string(TOLOWER "${sdk}" lowercase_sdk) # These two libraries are only used with the static swiftcore add_swift_target_library(swiftImageInspectionShared STATIC - ImageInspectionELF.cpp + ${inspection_file} C_COMPILE_FLAGS ${swift_runtime_library_compile_flags} LINK_FLAGS ${swift_runtime_linker_flags} SWIFT_COMPILE_FLAGS ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS} + TARGET_SDKS ${sdk} INSTALL_IN_COMPONENT stdlib) foreach(arch IN LISTS SWIFT_SDK_${sdk}_ARCHITECTURES) set(FragileSupportLibrary swiftImageInspectionShared-${SWIFT_SDK_${sdk}_LIB_SUBDIR}-${arch}) set(LibraryLocation ${SWIFTSTATICLIB_DIR}/${lowercase_sdk}/${arch}) - add_custom_command_target(swift_image_inspection_${arch}_static + + add_custom_command_target(swift_image_inspection_${lowercase_sdk}_${arch}_static COMMAND "${CMAKE_COMMAND}" -E copy $ ${LibraryLocation} OUTPUT "${LibraryLocation}/${CMAKE_STATIC_LIBRARY_PREFIX}swiftImageInspectionShared${CMAKE_STATIC_LIBRARY_SUFFIX}" DEPENDS ${FragileSupportLibrary}) + + list(APPEND static_binary_dependencies_list ${swift_image_inspection_${lowercase_sdk}_${arch}_static}) add_dependencies(stdlib ${FragileSupportLibrary}) swift_install_in_component(FILES $ DESTINATION "lib/swift_static/${lowercase_sdk}/${arch}" COMPONENT stdlib) endforeach() - set(FragileSupportLibraryPrimary swiftImageInspectionShared-${SWIFT_SDK_${sdk}_LIB_SUBDIR}-${SWIFT_PRIMARY_VARIANT_ARCH}) - set(LibraryLocationPrimary ${SWIFTSTATICLIB_DIR}/${lowercase_sdk}) - add_custom_command_target(swift_image_inspection_static_primary_arch - COMMAND - "${CMAKE_COMMAND}" -E copy $ ${LibraryLocationPrimary} - OUTPUT - "${LibraryLocationPrimary}/${CMAKE_STATIC_LIBRARY_PREFIX}swiftImageInspectionShared${CMAKE_STATIC_LIBRARY_SUFFIX}" - DEPENDS - ${FragileSupportLibraryPrimary}) + if(NOT "${primary_arch}" STREQUAL "") + set(FragileSupportLibraryPrimary swiftImageInspectionShared-${SWIFT_SDK_${sdk}_LIB_SUBDIR}-${primary_arch}) + set(LibraryLocationPrimary ${SWIFTSTATICLIB_DIR}/${lowercase_sdk}) + add_custom_command_target(swift_image_inspection_static_${lowercase_sdk}_primary_arch + COMMAND + "${CMAKE_COMMAND}" -E copy $ ${LibraryLocationPrimary} + OUTPUT + "${LibraryLocationPrimary}/${CMAKE_STATIC_LIBRARY_PREFIX}swiftImageInspectionShared${CMAKE_STATIC_LIBRARY_SUFFIX}" + DEPENDS + ${FragileSupportLibraryPrimary}) + list(APPEND static_binary_dependencies_list ${swift_image_inspection_static_${lowercase_sdk}_primary_arch}) add_dependencies(stdlib ${FragileSupportLibraryPrimary}) swift_install_in_component(FILES $ DESTINATION "lib/swift_static/${lowercase_sdk}" COMPONENT stdlib) + endif() - # Generate the static-executable-args.lnk file used for ELF systems (eg linux) set(linkfile "${lowercase_sdk}/static-executable-args.lnk") add_custom_command_target(swift_static_binary_${sdk}_args COMMAND "${CMAKE_COMMAND}" -E copy - "${SWIFT_SOURCE_DIR}/utils/static-executable-args.lnk" + "${linkfile_src}" "${SWIFTSTATICLIB_DIR}/${linkfile}" OUTPUT "${SWIFTSTATICLIB_DIR}/${linkfile}" DEPENDS - "${SWIFT_SOURCE_DIR}/utils/static-executable-args.lnk") + "${linkfile_src}") list(APPEND static_binary_lnk_file_list ${swift_static_binary_${sdk}_args}) swift_install_in_component(FILES "${SWIFTSTATICLIB_DIR}/${linkfile}" DESTINATION "lib/swift_static/${lowercase_sdk}" COMPONENT stdlib) - add_custom_target(static_binary_magic ALL DEPENDS ${static_binary_lnk_file_list}) - foreach(arch IN LISTS SWIFT_SDK_LINUX_ARCHITECTURES) - add_dependencies(static_binary_magic ${swift_image_inspection_${arch}_static}) - endforeach() - add_dependencies(static_binary_magic ${swift_image_inspection_static_primary_arch}) - add_dependencies(stdlib static_binary_magic) add_swift_target_library(swiftImageInspectionSharedObject OBJECT_LIBRARY - ImageInspectionELF.cpp + ${inspection_file} C_COMPILE_FLAGS ${swift_runtime_library_compile_flags} LINK_FLAGS ${swift_runtime_linker_flags} SWIFT_COMPILE_FLAGS ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS} + TARGET_SDKS ${sdk} INSTALL_IN_COMPONENT never_install) +endmacro() + +set(is_image_inspection_required) +foreach(sdk IN LISTS SWIFT_SDKS) + set(image_inspection_shared_sdk) + set(primary_arch) + set(image_inspection_shared_file) + set(linkfile_src) + + if("${SWIFT_SDK_${sdk}_OBJECT_FORMAT}" STREQUAL "ELF") + list(APPEND ELFISH_SDKS ${sdk}) + set(linkfile_src "${SWIFT_SOURCE_DIR}/utils/static-executable-args.lnk") + elseif("${SWIFT_SDK_${sdk}_OBJECT_FORMAT}" STREQUAL "WASM") + set(linkfile_src "${SWIFT_SOURCE_DIR}/utils/webassembly/static-executable-args.lnk") + endif() + + if(SWIFT_BUILD_STATIC_STDLIB AND "${sdk}" STREQUAL "LINUX") + set(image_inspection_shared_sdk "${sdk}") + set(image_inspection_shared_file ImageInspectionELF.cpp) + elseif(SWIFT_BUILD_STATIC_STDLIB AND "${sdk}" STREQUAL "WASI") + set(image_inspection_shared_sdk "${sdk}") + set(image_inspection_shared_file ImageInspectionELF.cpp) + # Set default arch + set(primary_arch "wasm32") + endif() + + if("${sdk}" STREQUAL "${SWIFT_PRIMARY_VARIANT_SDK}") + set(primary_arch ${SWIFT_PRIMARY_VARIANT_ARCH}) + endif() + + if(NOT "${image_inspection_shared_sdk}" STREQUAL "" AND NOT "${primary_arch}" STREQUAL "") + set(is_image_inspection_required TRUE) + add_image_inspection_shared(${image_inspection_shared_sdk} ${primary_arch} ${image_inspection_shared_file} ${linkfile_src}) + endif() +endforeach() + +if(is_image_inspection_required) + add_custom_target(static_binary_magic ALL DEPENDS ${static_binary_lnk_file_list} ${static_binary_dependencies_list}) + add_dependencies(stdlib static_binary_magic) endif() if(SWIFT_STDLIB_USE_NONATOMIC_RC) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 84f11ddcdb4af..2bb57c54b70e8 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -76,7 +76,7 @@ function(get_test_dependencies SDK result_var_name) ("${SDK}" STREQUAL "ANDROID") OR ("${SDK}" STREQUAL "WINDOWS") OR ("${SDK}" STREQUAL "HAIKU") OR - ("${SDK}" STREQUAL "WASM")) + ("${SDK}" STREQUAL "WASI")) # No extra dependencies. else() message(FATAL_ERROR "Unknown SDK: ${SDK}") @@ -225,28 +225,30 @@ foreach(SDK ${SWIFT_SDKS}) # NOTE create a stub BlocksRuntime library that can be used for the # reflection tests - file(WRITE ${test_bin_dir}/Inputs/BlocksRuntime.c + if(NOT CMAKE_SYSTEM_NAME STREQUAL Darwin AND (SWIFT_BUILD_SYNTAXPARSERLIB OR SWIFT_BUILD_SOURCEKIT)) + file(WRITE ${test_bin_dir}/Inputs/BlocksRuntime.c "void #if defined(_WIN32) __declspec(dllexport) #endif _Block_release(void) { }\n") - _add_swift_library_single( - BlocksRuntimeStub${VARIANT_SUFFIX} - BlocksRuntimeStub - SHARED - DONT_EMBED_BITCODE - NOSWIFTRT - ARCHITECTURE ${ARCH} - SDK ${SDK} - INSTALL_IN_COMPONENT dev - ${test_bin_dir}/Inputs/BlocksRuntime.c) - set_target_properties(BlocksRuntimeStub${VARIANT_SUFFIX} PROPERTIES - ARCHIVE_OUTPUT_DIRECTORY ${test_bin_dir} - LIBRARY_OUTPUT_DIRECTORY ${test_bin_dir} - RUNTIME_OUTPUT_DIRECTORY ${test_bin_dir} - OUTPUT_NAME BlocksRuntime) - list(APPEND test_dependencies BlocksRuntimeStub${VARIANT_SUFFIX}) + _add_swift_library_single( + BlocksRuntimeStub${VARIANT_SUFFIX} + BlocksRuntimeStub + SHARED + DONT_EMBED_BITCODE + NOSWIFTRT + ARCHITECTURE ${ARCH} + SDK ${SDK} + INSTALL_IN_COMPONENT dev + ${test_bin_dir}/Inputs/BlocksRuntime.c) + set_target_properties(BlocksRuntimeStub${VARIANT_SUFFIX} PROPERTIES + ARCHIVE_OUTPUT_DIRECTORY ${test_bin_dir} + LIBRARY_OUTPUT_DIRECTORY ${test_bin_dir} + RUNTIME_OUTPUT_DIRECTORY ${test_bin_dir} + OUTPUT_NAME BlocksRuntime) + list(APPEND test_dependencies BlocksRuntimeStub${VARIANT_SUFFIX}) + endif() if(SWIFT_BUILD_STDLIB AND SWIFT_INCLUDE_TESTS) list(APPEND test_dependencies @@ -258,7 +260,9 @@ _Block_release(void) { }\n") # doesn't need to be build for macCatalyst. list(APPEND test_dependencies "swift-reflection-test${DEFAULT_OSX_VARIANT_SUFFIX}") - else() + if("${SDK}" STREQUAL "WASI") + # wasm: Avoid to build swift-reflection-test because it uses unsupported linker flags for wasm-ld + elseif() list(APPEND test_dependencies "swift-reflection-test${VARIANT_SUFFIX}_signed") endif() diff --git a/test/lit.cfg b/test/lit.cfg index b77e33f282e97..97ebcc59490d4 100644 --- a/test/lit.cfg +++ b/test/lit.cfg @@ -1300,6 +1300,69 @@ elif run_os == 'linux-androideabi' or run_os == 'linux-android': '-L%s' % make_path(test_resource_dir, config.target_sdk_name)]) # The Swift interpreter is not available when targeting Android. config.available_features.discard('swift_interpreter') +elif run_os == 'wasi': + tools_directory = pipes.quote(make_path(config.wasi_sdk_path, "bin")) + + if run_cpu == 'wasm32': + lit_config.note("Testing WebAssembly/WASI " + config.variant_triple) + else: + lit_config.fatal("Unknown environment %s %s" % (run_os, run_cpu)) + + config.target_object_format = "wasm" + config.target_shared_library_prefix = 'lib' + config.target_shared_library_suffix = ".so" + config.target_sdk_name = "wasi" + config.target_runtime = "native" + + config.target_swift_autolink_extract = inferSwiftBinary("swift-autolink-extract") + + config.target_build_swift = ' '.join([ + '%s', '-target %s', '-static', '-static-executable', + '-Xcc --sysroot=%s', '-Xclang-linker --sysroot=%s', + '-tools-directory %s', + '-toolchain-stdlib-rpath %s', '%s %s %s %s' + ]) % (config.swiftc, config.variant_triple, + config.variant_sdk, config.variant_sdk, + tools_directory, resource_dir_opt, + mcp_opt, config.swift_test_options, + config.swift_driver_test_options, swift_execution_tests_extra_flags) + config.target_codesign = "echo" + config.target_build_swift_dylib = ( + "%s -parse-as-library -emit-library -o '\\1'" + % (config.target_build_swift)) + config.target_add_rpath = r'-Xlinker -rpath -Xlinker \1' + config.target_swift_frontend = ( + '%s -frontend -target %s %s %s %s %s ' + % (config.swift, config.variant_triple, resource_dir_opt, mcp_opt, + config.swift_test_options, config.swift_frontend_test_options)) + subst_target_swift_frontend_mock_sdk = config.target_swift_frontend + subst_target_swift_frontend_mock_sdk_after = "" + config.target_run = 'wasmtime' + if 'interpret' in lit_config.params: + use_interpreter_for_simple_runs() + config.target_sil_opt = ( + '%s -target %s %s %s %s' % + (config.sil_opt, config.variant_triple, resource_dir_opt, mcp_opt, config.sil_test_options)) + config.target_swift_ide_test = ( + '%s -target %s %s %s %s' % + (config.swift_ide_test, config.variant_triple, resource_dir_opt, + mcp_opt, ccp_opt)) + subst_target_swift_ide_test_mock_sdk = config.target_swift_ide_test + subst_target_swift_ide_test_mock_sdk_after = "" + config.target_swiftc_driver = ( + "%s -target %s -toolchain-stdlib-rpath %s %s" % + (config.swiftc, config.variant_triple, resource_dir_opt, mcp_opt)) + config.target_swift_modulewrap = ( + '%s -modulewrap -target %s' % + (config.swiftc, config.variant_triple)) + config.target_swift_emit_pcm = ( + '%s -emit-pcm -target %s' % + (config.swiftc, config.variant_triple)) + config.target_clang = ( + "clang++ -target %s %s -fobjc-runtime=ios-5.0" % + (config.variant_triple, clang_mcp_opt)) + config.target_ld = "ld -L%r" % (make_path(test_resource_dir, config.target_sdk_name)) + else: lit_config.fatal("Don't know how to define target_run and " diff --git a/test/lit.site.cfg.in b/test/lit.site.cfg.in index 69107930747b0..9f86de7721dcc 100644 --- a/test/lit.site.cfg.in +++ b/test/lit.site.cfg.in @@ -41,6 +41,9 @@ config.darwin_xcrun_toolchain = "@SWIFT_DARWIN_XCRUN_TOOLCHAIN@" config.android_ndk_path = "@SWIFT_ANDROID_NDK_PATH@" config.android_ndk_gcc_version = "@SWIFT_ANDROID_NDK_GCC_VERSION@" +# --- WebAssembly --- +config.wasi_sdk_path = "@SWIFT_WASI_SDK_PATH@" + # --- Windows --- msvc_runtime_flags = { 'MultiThreaded': 'MT', diff --git a/tools/driver/autolink_extract_main.cpp b/tools/driver/autolink_extract_main.cpp index f805714c30eb2..dfc70b9941f5d 100644 --- a/tools/driver/autolink_extract_main.cpp +++ b/tools/driver/autolink_extract_main.cpp @@ -32,6 +32,8 @@ #include "llvm/Object/Archive.h" #include "llvm/Object/ObjectFile.h" #include "llvm/Object/ELFObjectFile.h" +#include "llvm/Object/Wasm.h" +#include "llvm/BinaryFormat/Wasm.h" using namespace swift; using namespace llvm::opt; @@ -140,6 +142,31 @@ extractLinkerFlagsFromObjectFile(const llvm::object::ObjectFile *ObjectFile, return false; } +/// Look inside the object file 'WasmObjectFile' and append any linker flags found in +/// its ".swift1_autolink_entries" section to 'LinkerFlags'. +/// Return 'true' if there was an error, and 'false' otherwise. +static bool +extractLinkerFlagsFromObjectFile(const llvm::object::WasmObjectFile *ObjectFile, + std::vector &LinkerFlags, + CompilerInstance &Instance) { + + // Search for the data segment we hold autolink entries in + for (const llvm::object::WasmSegment &Segment : ObjectFile->dataSegments()) { + if (Segment.Data.Name == ".swift1_autolink_entries") { + + StringRef SegmentData = llvm::toStringRef(Segment.Data.Content); + // entries are null-terminated, so extract them and push them into + // the set. + llvm::SmallVector SplitFlags; + SegmentData.split(SplitFlags, llvm::StringRef("\0", 1), -1, + /*KeepEmpty=*/false); + for (const auto &Flag : SplitFlags) + LinkerFlags.push_back(Flag); + } + } + return false; +} + /// Look inside the binary 'Bin' and append any linker flags found in its /// ".swift1_autolink_entries" section to 'LinkerFlags'. If 'Bin' is an archive, /// recursively look inside all children within the archive. Return 'true' if @@ -150,6 +177,8 @@ static bool extractLinkerFlags(const llvm::object::Binary *Bin, std::vector &LinkerFlags) { if (auto *ObjectFile = llvm::dyn_cast(Bin)) { return extractLinkerFlagsFromObjectFile(ObjectFile, LinkerFlags, Instance); + } else if (auto *ObjectFile = llvm::dyn_cast(Bin)) { + return extractLinkerFlagsFromObjectFile(ObjectFile, LinkerFlags, Instance); } else if (auto *Archive = llvm::dyn_cast(Bin)) { llvm::Error Error = llvm::Error::success(); for (const auto &Child : Archive->children(Error)) { diff --git a/utils/webassembly/build-linux.sh b/utils/webassembly/build-linux.sh new file mode 100755 index 0000000000000..7aa8b90e26cfe --- /dev/null +++ b/utils/webassembly/build-linux.sh @@ -0,0 +1,35 @@ +#/bin/bash + +SOURCE_PATH="$( cd "$(dirname $0)/../../.." && pwd )" +SWIFT_PATH=$SOURCE_PATH/swift + +$SWIFT_PATH/utils/build-script --wasm \ + --skip-build-benchmarks \ + --extra-cmake-options=" \ + -DSWIFT_PRIMARY_VARIANT_SDK:STRING=WASI \ + -DSWIFT_PRIMARY_VARIANT_ARCH:STRING=wasm32 \ + -DSWIFT_SDKS='WASI;LINUX' \ + -DSWIFT_BUILD_SOURCEKIT=FALSE \ + -DSWIFT_ENABLE_SOURCEKIT_TESTS=FALSE \ + -DSWIFT_BUILD_SYNTAXPARSERLIB=FALSE \ + -DCMAKE_AR='$SOURCE_PATH/wasi-sdk/bin/llvm-ar' \ + -DCMAKE_RANLIB='$SOURCE_PATH/wasi-sdk/bin/llvm-ranlib' \ + " \ + --build-stdlib-deployment-targets "wasi-wasm32" \ + --build-swift-dynamic-sdk-overlay false \ + --build-swift-dynamic-stdlib false \ + --build-swift-static-sdk-overlay \ + --build-swift-static-stdlib \ + --install-destdir="$SOURCE_PATH/install" \ + --install-prefix="/opt/swiftwasm-sdk" \ + --install-swift \ + --installable-package="$SOURCE_PATH/swiftwasm-linux.tar.gz" \ + --llvm-targets-to-build "X86;WebAssembly" \ + --stdlib-deployment-targets "wasi-wasm32" \ + --wasi-icu-data "$SOURCE_PATH/icu_out/lib/libicudata.a" \ + --wasi-icu-i18n "$SOURCE_PATH/icu_out/lib/libicui18n.a" \ + --wasi-icu-i18n-include "$SOURCE_PATH/icu_out/include" \ + --wasi-icu-uc "$SOURCE_PATH/icu_out/lib/libicuuc.a" \ + --wasi-icu-uc-include "$SOURCE_PATH/icu_out/include" \ + --wasi-sdk "$SOURCE_PATH/wasi-sdk" \ + "$@" diff --git a/build-mac.sh b/utils/webassembly/build-mac.sh similarity index 55% rename from build-mac.sh rename to utils/webassembly/build-mac.sh index ba621a52b1636..1fef9d76ac8d6 100755 --- a/build-mac.sh +++ b/utils/webassembly/build-mac.sh @@ -1,8 +1,9 @@ #/bin/bash -export sourcedir=$PWD/.. +SOURCE_PATH="$( cd "$(dirname $0)/../../.." && pwd )" +SWIFT_PATH=$SOURCE_PATH/swift -./utils/build-script --release --wasm --verbose \ +$SWIFT_PATH/utils/build-script --wasm \ --skip-build-benchmarks \ --extra-cmake-options=" \ -DSWIFT_PRIMARY_VARIANT_SDK:STRING=WASI \ @@ -10,6 +11,7 @@ export sourcedir=$PWD/.. -DSWIFT_OSX_x86_64_ICU_STATICLIB=TRUE \ -DSWIFT_BUILD_SOURCEKIT=FALSE \ -DSWIFT_ENABLE_SOURCEKIT_TESTS=FALSE \ + -DSWIFT_BUILD_SYNTAXPARSERLIB=FALSE \ -DCMAKE_AR='/usr/local/opt/llvm/bin/llvm-ar' \ -DCMAKE_RANLIB='/usr/local/opt/llvm/bin/llvm-ranlib' \ " \ @@ -20,13 +22,14 @@ export sourcedir=$PWD/.. --build-swift-static-stdlib \ --llvm-targets-to-build "X86;WebAssembly" \ --stdlib-deployment-targets "wasi-wasm32" \ - --wasi-icu-data "todo-icu-data" \ - --wasi-icu-i18n "$sourcedir/icu_out/lib" \ - --wasi-icu-i18n-include "$sourcedir/icu_out/include" \ - --wasi-icu-uc "$sourcedir/icu_out/lib" \ - --wasi-icu-uc-include "$sourcedir/icu_out/include" \ - --wasi-sdk "$sourcedir/wasi-sdk" \ + --wasi-icu-data "$SOURCE_PATH/icu_out/lib/libicudata.a" \ + --wasi-icu-i18n "$SOURCE_PATH/icu_out/lib/libicui18n.a" \ + --wasi-icu-i18n-include "$SOURCE_PATH/icu_out/include" \ + --wasi-icu-uc "$SOURCE_PATH/icu_out/lib/libicuuc.a" \ + --wasi-icu-uc-include "$SOURCE_PATH/icu_out/include" \ + --wasi-sdk "$SOURCE_PATH/wasi-sdk" \ --install-swift \ --install-prefix="/opt/swiftwasm-sdk" \ - --install-destdir="$sourcedir/install" \ - --installable-package="$sourcedir/swiftwasm-macos.tar.gz" + --install-destdir="$SOURCE_PATH/install" \ + --installable-package="$SOURCE_PATH/swiftwasm-macos.tar.gz" \ + "$@" diff --git a/utils/webassembly/ci-linux.sh b/utils/webassembly/ci-linux.sh new file mode 100755 index 0000000000000..8bfb2229a4574 --- /dev/null +++ b/utils/webassembly/ci-linux.sh @@ -0,0 +1,53 @@ +#/bin/bash + +sudo apt update +sudo apt install \ + git ninja-build clang python \ + uuid-dev libicu-dev icu-devtools libbsd-dev \ + libedit-dev libxml2-dev libsqlite3-dev swig \ + libpython-dev libncurses5-dev pkg-config \ + libblocksruntime-dev libcurl4-openssl-dev \ + systemtap-sdt-dev tzdata rsync wget + +SOURCE_PATH="$( cd "$(dirname $0)/../../.." && pwd )" +SWIFT_PATH=$SOURCE_PATH/swift +BUILD_SCRIPT=$SWIFT_PATH/utils/webassembly/build-linux.sh +cd $SWIFT_PATH + +export current_sha=`git rev-parse HEAD` +./utils/update-checkout --clone --scheme wasm +git checkout $current_sha + +# Install wasmtime + +sudo mkdir /opt/wasmtime && cd /opt/wasmtime +wget -O - "https://github.com/bytecodealliance/wasmtime/releases/download/v0.8.0/wasmtime-v0.8.0-x86_64-linux.tar.xz" | \ + sudo tar x --strip-components 1 +sudo ln -sf /opt/wasmtime/* /usr/local/bin + +cd $SOURCE_PATH + +wget -O install_cmake.sh "https://github.com/Kitware/CMake/releases/download/v3.15.3/cmake-3.15.3-Linux-x86_64.sh" +chmod +x install_cmake.sh +sudo mkdir -p /opt/cmake +sudo ./install_cmake.sh --skip-license --prefix=/opt/cmake +sudo ln -sf /opt/cmake/bin/* /usr/local/bin +cmake --version + +wget -O dist-wasi-sdk.tgz https://github.com/swiftwasm/wasi-sdk/suites/370986556/artifacts/809002 +unzip dist-wasi-sdk.tgz +WASI_SDK_TAR_PATH=$(find dist-ubuntu-latest.tgz -type f -name "wasi-sdk-*") +WASI_SDK_FULL_NAME=$(basename $WASI_SDK_TAR_PATH -linux.tar.gz) +tar xfz $WASI_SDK_TAR_PATH +mv $WASI_SDK_FULL_NAME ./wasi-sdk + +# Link wasm32-wasi-unknown to wasm32-wasi because clang finds crt1.o from sysroot +# with os and environment name `getMultiarchTriple`. +ln -s wasm32-wasi wasi-sdk/share/wasi-sysroot/lib/wasm32-wasi-unknown + +wget -O icu.tar.xz "https://github.com/swiftwasm/icu4c-wasi/releases/download/20190421.3/icu4c-wasi.tar.xz" +tar xf icu.tar.xz + +$BUILD_SCRIPT --release --debug-swift-stdlib --verbose +# Run test but ignore failure temporarily +$BUILD_SCRIPT --release --debug-swift-stdlib --verbose -t || true diff --git a/utils/webassembly/ci-mac.sh b/utils/webassembly/ci-mac.sh new file mode 100755 index 0000000000000..a1b52e648da8e --- /dev/null +++ b/utils/webassembly/ci-mac.sh @@ -0,0 +1,43 @@ +#/bin/bash + +brew install cmake ninja llvm + +SOURCE_PATH="$( cd "$(dirname $0)/../../.." && pwd )" +SWIFT_PATH=$SOURCE_PATH/swift +BUILD_SCRIPT=$SWIFT_PATH/utils/webassembly/build-mac.sh +cd $SWIFT_PATH + +export current_sha=`git rev-parse HEAD` +./utils/update-checkout --clone --scheme wasm +git checkout $current_sha + +# Install wasmtime + +sudo mkdir /opt/wasmtime && cd /opt/wasmtime +wget -O - "https://github.com/bytecodealliance/wasmtime/releases/download/v0.8.0/wasmtime-v0.8.0-x86_64-macos.tar.xz" | \ + sudo tar x --strip-components 1 +sudo ln -sf /opt/wasmtime/* /usr/local/bin + +cd $SOURCE_PATH + +wget -O dist-wasi-sdk.tgz https://github.com/swiftwasm/wasi-sdk/suites/370986556/artifacts/809001 +tar xfz dist-wasi-sdk.tgz +WASI_SDK_TAR_PATH=$(find dist-macos-latest.tgz -type f -name "wasi-sdk-*") +WASI_SDK_FULL_NAME=$(basename $WASI_SDK_TAR_PATH -macos.tar.gz) +tar xfz $WASI_SDK_TAR_PATH +mv $WASI_SDK_FULL_NAME ./wasi-sdk + +# Link sysroot/usr/include to sysroot/include because Darwin sysroot doesn't +# find header files in sysroot/include but sysroot/usr/include +mkdir wasi-sdk/share/wasi-sysroot/usr/ +ln -s ../include wasi-sdk/share/wasi-sysroot/usr/include +# Link wasm32-wasi-unknown to wasm32-wasi because clang finds crt1.o from sysroot +# with os and environment name `getMultiarchTriple`. +ln -s wasm32-wasi wasi-sdk/share/wasi-sysroot/lib/wasm32-wasi-unknown + +wget -O icu.tar.xz "https://github.com/swiftwasm/icu4c-wasi/releases/download/20190421.3/icu4c-wasi.tar.xz" +tar xf icu.tar.xz + +$BUILD_SCRIPT --release --debug-swift-stdlib --verbose +# Run test but ignore failure temporarily +$BUILD_SCRIPT --release --debug-swift-stdlib --verbose -t || true diff --git a/utils/webassembly/static-executable-args.lnk b/utils/webassembly/static-executable-args.lnk new file mode 100644 index 0000000000000..0f8105ca97de6 --- /dev/null +++ b/utils/webassembly/static-executable-args.lnk @@ -0,0 +1,14 @@ +-static +-lswiftCore +-lswiftImageInspectionShared +-lswiftSwiftOnoneSupport +-licui18n +-licuuc +-licudata +-ldl +-lstdc++ +-lm +-Xlinker --error-limit=0 +-Xlinker --no-gc-sections +-Xlinker --no-threads + diff --git a/utils/webassembly/static-stdlib-args.lnk b/utils/webassembly/static-stdlib-args.lnk new file mode 100644 index 0000000000000..5d1397e2b74b8 --- /dev/null +++ b/utils/webassembly/static-stdlib-args.lnk @@ -0,0 +1,15 @@ +-ldl +-lpthread +-latomic +-lswiftCore +-latomic +-lswiftImageInspectionShared +-licui18n +-licuuc +-licudata +-lstdc++ +-lm +-Xlinker +--exclude-libs +-Xlinker +ALL diff --git a/vvv.sh b/vvv.sh deleted file mode 100755 index 6f202440842fd..0000000000000 --- a/vvv.sh +++ /dev/null @@ -1,11 +0,0 @@ -utils/build-script --release-debuginfo --wasm \ - --llvm-targets-to-build "X86;ARM;AArch64;PowerPC;SystemZ;WebAssembly" \ - --llvm-max-parallel-lto-link-jobs 1 --swift-tools-max-parallel-lto-link-jobs 1 \ - --wasm-wasi-sdk "/home/zhuowei/wasi-sdk" \ - --wasm-icu-uc "todo" \ - --wasm-icu-uc-include "/home/zhuowei/Documents/BuildICU/icu_out/include" \ - --wasm-icu-i18n "todo" \ - --wasm-icu-i18n-include "todo" \ - --wasm-icu-data "todo" \ - --build-swift-static-stdlib \ - "$@" From 5fa53d0c4d07fca9dead6cb0b382cd67cac4aac7 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Wed, 25 Dec 2019 10:07:34 +0900 Subject: [PATCH 73/80] Eliminate fakepthread dependency (#27) * [WASM] Minimize dependences of pthread * [WASM] Built fakepthread as a part of swift project * [WASM] Always build WasiPthread as static * [WASM] Fix to emit libswiftWasiPthread.a in swift_static * [WASM] Remove unused header file --- include/swift/Basic/Lazy.h | 10 ++ include/swift/Runtime/Mutex.h | 4 +- include/swift/Runtime/MutexWASI.h | 66 ++++++++ lib/ClangImporter/ClangImporter.cpp | 4 + .../SwiftPrivateThreadExtras.swift | 6 + .../ThreadBarriers.swift | 15 ++ stdlib/public/CMakeLists.txt | 4 + stdlib/public/Platform/glibc.modulemap.gyb | 2 +- stdlib/public/WASI/CMakeLists.txt | 3 + stdlib/public/WASI/Pthread.cpp | 153 ++++++++++++++++++ stdlib/public/runtime/CMakeLists.txt | 1 + stdlib/public/runtime/Casting.cpp | 13 +- stdlib/public/runtime/MutexPThread.cpp | 2 +- stdlib/public/runtime/MutexWASI.cpp | 25 +++ stdlib/public/runtime/ThreadLocalStorage.h | 8 +- stdlib/public/stubs/ThreadLocalStorage.cpp | 20 +++ utils/webassembly/static-executable-args.lnk | 4 +- utils/webassembly/static-stdlib-args.lnk | 2 +- 18 files changed, 335 insertions(+), 7 deletions(-) create mode 100644 include/swift/Runtime/MutexWASI.h create mode 100644 stdlib/public/WASI/CMakeLists.txt create mode 100644 stdlib/public/WASI/Pthread.cpp create mode 100644 stdlib/public/runtime/MutexWASI.cpp diff --git a/include/swift/Basic/Lazy.h b/include/swift/Basic/Lazy.h index 594e2ed8b9fcd..647e5aff41c59 100644 --- a/include/swift/Basic/Lazy.h +++ b/include/swift/Basic/Lazy.h @@ -16,12 +16,18 @@ #include #ifdef __APPLE__ #include +#elif defined(__wasi__) +// No pthread on wasi #else #include #endif #include "swift/Basic/Malloc.h" #include "swift/Basic/type_traits.h" +#ifdef __wasi__ +void wasi_polyfill_call_once(int *flag, void *context, void (*func)(void *)); +#endif + namespace swift { #ifdef __APPLE__ @@ -36,6 +42,10 @@ namespace swift { using OnceToken_t = unsigned long; # define SWIFT_ONCE_F(TOKEN, FUNC, CONTEXT) \ _swift_once_f(&TOKEN, CONTEXT, FUNC) +#elif defined(__wasi__) + using OnceToken_t = int; +# define SWIFT_ONCE_F(TOKEN, FUNC, CONTEXT) \ + ::wasi_polyfill_call_once(&TOKEN, CONTEXT, FUNC) #else using OnceToken_t = std::once_flag; # define SWIFT_ONCE_F(TOKEN, FUNC, CONTEXT) \ diff --git a/include/swift/Runtime/Mutex.h b/include/swift/Runtime/Mutex.h index 3bf61177f9790..a24c555ccca03 100644 --- a/include/swift/Runtime/Mutex.h +++ b/include/swift/Runtime/Mutex.h @@ -20,10 +20,12 @@ #include -#if (defined(__APPLE__) || defined(__linux__) || defined(__CYGWIN__) || defined(__FreeBSD__) || defined(__HAIKU__) || defined(__wasi__)) +#if (defined(__APPLE__) || defined(__linux__) || defined(__CYGWIN__) || defined(__FreeBSD__) || defined(__HAIKU__)) #include "swift/Runtime/MutexPThread.h" #elif defined(_WIN32) #include "swift/Runtime/MutexWin32.h" +#elif defined(__wasi__) +#include "swift/Runtime/MutexWASI.h" #else #error "Implement equivalent of MutexPThread.h/cpp for your platform." #endif diff --git a/include/swift/Runtime/MutexWASI.h b/include/swift/Runtime/MutexWASI.h new file mode 100644 index 0000000000000..28f212d6f48e9 --- /dev/null +++ b/include/swift/Runtime/MutexWASI.h @@ -0,0 +1,66 @@ +//===--- MutexWin32.h - -----------------------------------------*- C++ -*-===// +// +// This source file is part of the Swift.org open source project +// +// Copyright (c) 2014 - 2017 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 +// +//===----------------------------------------------------------------------===// +// +// Mutex, ConditionVariable, Read/Write lock, and Scoped lock implementations +// using Windows Slim Reader/Writer Locks and Conditional Variables. +// +//===----------------------------------------------------------------------===// + +#ifndef SWIFT_RUNTIME_MUTEX_WASI_H +#define SWIFT_RUNTIME_MUTEX_WASI_H + +namespace swift { + +typedef void* ConditionHandle; +typedef void* MutexHandle; +typedef void* ReadWriteLockHandle; + +#define SWIFT_CONDITION_SUPPORTS_CONSTEXPR 1 +#define SWIFT_MUTEX_SUPPORTS_CONSTEXPR 1 +#define SWIFT_READWRITELOCK_SUPPORTS_CONSTEXPR 1 + +struct ConditionPlatformHelper { + static constexpr ConditionHandle staticInit() { + return nullptr; + }; + static void init(ConditionHandle &condition) {} + static void destroy(ConditionHandle &condition) {} + static void notifyOne(ConditionHandle &condition) {} + static void notifyAll(ConditionHandle &condition) {} + static void wait(ConditionHandle &condition, MutexHandle &mutex); +}; + +struct MutexPlatformHelper { + static constexpr MutexHandle staticInit() { return nullptr; } + static void init(MutexHandle &mutex, bool checked = false) {} + static void destroy(MutexHandle &mutex) {} + static void lock(MutexHandle &mutex) {} + static void unlock(MutexHandle &mutex) {} + static bool try_lock(MutexHandle &mutex) { return true; } + static void unsafeLock(MutexHandle &mutex) {} + static void unsafeUnlock(MutexHandle &mutex) {} +}; + +struct ReadWriteLockPlatformHelper { + static constexpr ReadWriteLockHandle staticInit() { return nullptr; } + static void init(ReadWriteLockHandle &rwlock) {} + static void destroy(ReadWriteLockHandle &rwlock) {} + static void readLock(ReadWriteLockHandle &rwlock) {} + static bool try_readLock(ReadWriteLockHandle &rwlock) { return true; } + static void readUnlock(ReadWriteLockHandle &rwlock) {} + static void writeLock(ReadWriteLockHandle &rwlock) {} + static bool try_writeLock(ReadWriteLockHandle &rwlock) { return true; } + static void writeUnlock(ReadWriteLockHandle &rwlock) {} +}; +} + +#endif diff --git a/lib/ClangImporter/ClangImporter.cpp b/lib/ClangImporter/ClangImporter.cpp index 3bc3c4b5d1e3d..f879e383d7bf1 100644 --- a/lib/ClangImporter/ClangImporter.cpp +++ b/lib/ClangImporter/ClangImporter.cpp @@ -598,6 +598,10 @@ getNormalInvocationArguments(std::vector &invocationArgStrs, }); } + if (triple.isOSWASI()) { + invocationArgStrs.insert(invocationArgStrs.end(), {"-D_WASI_EMULATED_MMAN"}); + } + if (triple.isOSWindows()) { switch (triple.getArch()) { default: llvm_unreachable("unsupported Windows architecture"); diff --git a/stdlib/private/SwiftPrivateThreadExtras/SwiftPrivateThreadExtras.swift b/stdlib/private/SwiftPrivateThreadExtras/SwiftPrivateThreadExtras.swift index 831d721f3c588..c9d34e35017c2 100644 --- a/stdlib/private/SwiftPrivateThreadExtras/SwiftPrivateThreadExtras.swift +++ b/stdlib/private/SwiftPrivateThreadExtras/SwiftPrivateThreadExtras.swift @@ -98,6 +98,9 @@ public func _stdlib_thread_create_block( } else { return (0, ThreadHandle(bitPattern: threadID)) } +#elseif os(WASI) + // WASI environment has a only single thread + return (0, nil) #else var threadID = _make_pthread_t() let result = pthread_create(&threadID, nil, @@ -128,6 +131,9 @@ public func _stdlib_thread_join( } else { return (CInt(result), nil) } +#elseif os(WASI) + // WASI environment has a only single thread + return (0, nil) #else var threadResultRawPtr: UnsafeMutableRawPointer? let result = pthread_join(thread, &threadResultRawPtr) diff --git a/stdlib/private/SwiftPrivateThreadExtras/ThreadBarriers.swift b/stdlib/private/SwiftPrivateThreadExtras/ThreadBarriers.swift index b06ed029bf9b0..8bef3340ec82a 100644 --- a/stdlib/private/SwiftPrivateThreadExtras/ThreadBarriers.swift +++ b/stdlib/private/SwiftPrivateThreadExtras/ThreadBarriers.swift @@ -36,6 +36,8 @@ public struct _stdlib_thread_barrier_t { #elseif os(Cygwin) || os(FreeBSD) var mutex: UnsafeMutablePointer? var cond: UnsafeMutablePointer? +#elseif os(WASI) + // No pthread for WASI #else var mutex: UnsafeMutablePointer? var cond: UnsafeMutablePointer? @@ -67,6 +69,8 @@ public func _stdlib_thread_barrier_init( barrier.pointee.cond = UnsafeMutablePointer.allocate(capacity: 1) InitializeConditionVariable(barrier.pointee.cond!) +#elseif os(WASI) + // WASI environment has a only single thread #else barrier.pointee.mutex = UnsafeMutablePointer.allocate(capacity: 1) if pthread_mutex_init(barrier.pointee.mutex!, nil) != 0 { @@ -89,6 +93,8 @@ public func _stdlib_thread_barrier_destroy( #if os(Windows) // Condition Variables do not need to be explicitly destroyed // Mutexes do not need to be explicitly destroyed +#elseif os(WASI) + // WASI environment has a only single thread #else if pthread_cond_destroy(barrier.pointee.cond!) != 0 { // FIXME: leaking memory, leaking a mutex. @@ -99,11 +105,14 @@ public func _stdlib_thread_barrier_destroy( return -1 } #endif + +#if !os(WASI) barrier.pointee.cond!.deinitialize(count: 1) barrier.pointee.cond!.deallocate() barrier.pointee.mutex!.deinitialize(count: 1) barrier.pointee.mutex!.deallocate() +#endif return 0 } @@ -113,6 +122,8 @@ public func _stdlib_thread_barrier_wait( ) -> CInt { #if os(Windows) AcquireSRWLockExclusive(barrier.pointee.mutex!) +#elseif os(WASI) + // WASI environment has a only single thread #else if pthread_mutex_lock(barrier.pointee.mutex!) != 0 { return -1 @@ -127,6 +138,8 @@ public func _stdlib_thread_barrier_wait( return -1 } ReleaseSRWLockExclusive(barrier.pointee.mutex!) +#elseif os(WASI) + // WASI environment has a only single thread #else if pthread_cond_wait(barrier.pointee.cond!, barrier.pointee.mutex!) != 0 { return -1 @@ -144,6 +157,8 @@ public func _stdlib_thread_barrier_wait( #if os(Windows) WakeAllConditionVariable(barrier.pointee.cond!) ReleaseSRWLockExclusive(barrier.pointee.mutex!) +#elseif os(WASI) + // WASI environment has a only single thread #else if pthread_cond_broadcast(barrier.pointee.cond!) != 0 { return -1 diff --git a/stdlib/public/CMakeLists.txt b/stdlib/public/CMakeLists.txt index e8d5da6490b76..5768d2168c373 100644 --- a/stdlib/public/CMakeLists.txt +++ b/stdlib/public/CMakeLists.txt @@ -62,6 +62,10 @@ if(SWIFT_BUILD_STDLIB) add_subdirectory(core) add_subdirectory(SwiftOnoneSupport) + if(WASI IN_LIST SWIFT_SDKS) + add_subdirectory(WASI) + endif() + if(SWIFT_ENABLE_EXPERIMENTAL_DIFFERENTIABLE_PROGRAMMING) add_subdirectory(Differentiation) endif() diff --git a/stdlib/public/Platform/glibc.modulemap.gyb b/stdlib/public/Platform/glibc.modulemap.gyb index 963d3c7990cb7..b6171c8c321a0 100644 --- a/stdlib/public/Platform/glibc.modulemap.gyb +++ b/stdlib/public/Platform/glibc.modulemap.gyb @@ -377,11 +377,11 @@ module SwiftGlibc [system] { header "${GLIBC_INCLUDE_PATH}/poll.h" export * } +% if CMAKE_SDK != "WASI": module pthread { header "${GLIBC_INCLUDE_PATH}/pthread.h" export * } -% if CMAKE_SDK != "WASI": module pwd { header "${GLIBC_INCLUDE_PATH}/pwd.h" export * diff --git a/stdlib/public/WASI/CMakeLists.txt b/stdlib/public/WASI/CMakeLists.txt new file mode 100644 index 0000000000000..451f1f3af57d7 --- /dev/null +++ b/stdlib/public/WASI/CMakeLists.txt @@ -0,0 +1,3 @@ +add_swift_target_library(swiftWasiPthread STATIC IS_STDLIB + Pthread.cpp + INSTALL_IN_COMPONENT stdlib) diff --git a/stdlib/public/WASI/Pthread.cpp b/stdlib/public/WASI/Pthread.cpp new file mode 100644 index 0000000000000..5a64d9bdb4c39 --- /dev/null +++ b/stdlib/public/WASI/Pthread.cpp @@ -0,0 +1,153 @@ +// SPDX-License-Identifier: 0BSD +// prototypes taken from opengroup +#include +#include +#include +#include + +#define STUB() do {fprintf(stderr, "FakePthread: unsupported %s\n", __func__);abort();}while(0) + +// mutexes: just no-ops + +int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr) { + return 0; +} + +int pthread_mutex_destroy(pthread_mutex_t *mutex) { + return 0; +} + +int pthread_mutexattr_init(pthread_mutexattr_t *attr) { + return 0; +} + +int pthread_mutexattr_destroy(pthread_mutexattr_t *attr) { + return 0; +} + +int pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *type) { + return 0; +} + +int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type) { + return 0; +} + +int pthread_mutex_lock(pthread_mutex_t *mutex) { + return 0; +} + +int pthread_mutex_trylock(pthread_mutex_t *mutex) { + return 0; +} + +int pthread_mutex_unlock(pthread_mutex_t *mutex) { + return 0; +} + +// pthread_cond: STUB + +int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr) { + return 0; +} + +int pthread_cond_destroy(pthread_cond_t *cond) { + return 0; +} + +int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) { + STUB(); +} + +int pthread_cond_timedwait(pthread_cond_t *cond, + pthread_mutex_t *mutex, const struct timespec *abstime) { + STUB(); +} + +int pthread_cond_broadcast(pthread_cond_t *cond) { + return 0; +} + +int pthread_cond_signal(pthread_cond_t *cond) { + return 0; +} + +// tls + +int pthread_key_create(pthread_key_t *key, void (*destructor)(void*)) { + STUB(); +} + +void *pthread_getspecific(pthread_key_t key) { + STUB(); +} + +int pthread_setspecific(pthread_key_t key, const void *value) { + STUB(); +} + +// threads + +pthread_t pthread_self() { + return (pthread_t)1234; +} + +#undef pthread_equal + +int pthread_equal(pthread_t t1, pthread_t t2) { + return t1 == t2; +} + +int pthread_join(pthread_t thread, void **value_ptr) { + STUB(); +} + +int pthread_detach(pthread_t thread) { + STUB(); +} + +int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg) { + return 0; +} + +// once + +int pthread_once(pthread_once_t *once_control, void (*init_routine)(void)) { + STUB(); +} + +// rwlock + +int pthread_rwlock_init(pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *attr) { + return 0; +} + +int pthread_rwlock_destroy(pthread_rwlock_t *rwlock) { + return 0; +} + +int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock) { + return 0; +} + +int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock) { + return 0; +} + +int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock) { + return 0; +} + +int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock) { + return 0; +} + +int pthread_rwlock_unlock(pthread_rwlock_t *rwlock) { + return 0; +} + +// named semaphores + +sem_t *sem_open(const char *name, int oflag, ...) { + STUB(); +} diff --git a/stdlib/public/runtime/CMakeLists.txt b/stdlib/public/runtime/CMakeLists.txt index 702cf69df2207..1463033798a3b 100644 --- a/stdlib/public/runtime/CMakeLists.txt +++ b/stdlib/public/runtime/CMakeLists.txt @@ -56,6 +56,7 @@ set(swift_runtime_sources MetadataLookup.cpp MutexPThread.cpp MutexWin32.cpp + MutexWASI.cpp Numeric.cpp Once.cpp Portability.cpp diff --git a/stdlib/public/runtime/Casting.cpp b/stdlib/public/runtime/Casting.cpp index 2c516a9952fb4..0456b400611ee 100644 --- a/stdlib/public/runtime/Casting.cpp +++ b/stdlib/public/runtime/Casting.cpp @@ -30,7 +30,12 @@ #include "swift/Runtime/ExistentialContainer.h" #include "swift/Runtime/HeapObject.h" #include "swift/Runtime/Metadata.h" -#include "swift/Runtime/Mutex.h" +#ifdef __wasi__ +# define SWIFT_CASTING_SUPPORTS_MUTEX 0 +#else +# define SWIFT_CASTING_SUPPORTS_MUTEX 1 +# include "swift/Runtime/Mutex.h" +#endif #include "swift/Runtime/Unreachable.h" #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/PointerIntPair.h" @@ -125,7 +130,9 @@ TypeNamePair swift::swift_getTypeName(const Metadata *type, bool qualified) { using Key = llvm::PointerIntPair; + #if SWIFT_CASTING_SUPPORTS_MUTEX static StaticReadWriteLock TypeNameCacheLock; + #endif static Lazy>> TypeNameCache; @@ -134,7 +141,9 @@ swift::swift_getTypeName(const Metadata *type, bool qualified) { // Attempt read-only lookup of cache entry. { + #if SWIFT_CASTING_SUPPORTS_MUTEX StaticScopedReadLock guard(TypeNameCacheLock); + #endif auto found = cache.find(key); if (found != cache.end()) { @@ -145,7 +154,9 @@ swift::swift_getTypeName(const Metadata *type, bool qualified) { // Read-only lookup failed to find item, we may need to create it. { + #if SWIFT_CASTING_SUPPORTS_MUTEX StaticScopedWriteLock guard(TypeNameCacheLock); + #endif // Do lookup again just to make sure it wasn't created by another // thread before we acquired the write lock. diff --git a/stdlib/public/runtime/MutexPThread.cpp b/stdlib/public/runtime/MutexPThread.cpp index 62e718c16abaa..92db58226cd09 100644 --- a/stdlib/public/runtime/MutexPThread.cpp +++ b/stdlib/public/runtime/MutexPThread.cpp @@ -15,7 +15,7 @@ // //===----------------------------------------------------------------------===// -#if !defined(_WIN32) +#if !defined(_WIN32) && !defined(__wasi__) #include "swift/Runtime/Mutex.h" #include "swift/Runtime/Debug.h" diff --git a/stdlib/public/runtime/MutexWASI.cpp b/stdlib/public/runtime/MutexWASI.cpp new file mode 100644 index 0000000000000..6288ecce03449 --- /dev/null +++ b/stdlib/public/runtime/MutexWASI.cpp @@ -0,0 +1,25 @@ +//===--- MutexWin32.cpp - -------------------------------------------------===// +// +// This source file is part of the Swift.org open source project +// +// Copyright (c) 2014 - 2017 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 +// +//===----------------------------------------------------------------------===// +// +// Mutex, ConditionVariable, Read/Write lock, and Scoped lock implementations +// using Windows Slim Reader/Writer Locks and Conditional Variables. +// +//===----------------------------------------------------------------------===// + +#if defined(__wasi__) +#include "swift/Runtime/Mutex.h" + +using namespace swift; + +void ConditionPlatformHelper::wait(void* &condition, + void* &mutex) {} +#endif diff --git a/stdlib/public/runtime/ThreadLocalStorage.h b/stdlib/public/runtime/ThreadLocalStorage.h index ebf57f6ceb69a..4e62b76b1d74e 100644 --- a/stdlib/public/runtime/ThreadLocalStorage.h +++ b/stdlib/public/runtime/ThreadLocalStorage.h @@ -98,7 +98,13 @@ static_assert(std::is_same<__swift_thread_key_t, DWORD>::value, # define SWIFT_THREAD_KEY_CREATE _stdlib_thread_key_create # define SWIFT_THREAD_GETSPECIFIC FlsGetValue # define SWIFT_THREAD_SETSPECIFIC(key, value) (FlsSetValue(key, value) == FALSE) - +# elif defined(__wasi__) +int wasi_polyfill_pthread_key_create(__swift_thread_key_t *key, void (*destructor)(void*)); +void *wasi_polyfill_pthread_getspecific(__swift_thread_key_t key); +int wasi_polyfill_pthread_setspecific(__swift_thread_key_t key, const void *value); +# define SWIFT_THREAD_KEY_CREATE wasi_polyfill_pthread_key_create +# define SWIFT_THREAD_GETSPECIFIC wasi_polyfill_pthread_getspecific +# define SWIFT_THREAD_SETSPECIFIC wasi_polyfill_pthread_setspecific # else // Otherwise use the pthread API. # include diff --git a/stdlib/public/stubs/ThreadLocalStorage.cpp b/stdlib/public/stubs/ThreadLocalStorage.cpp index 50168f955bc6c..4e855cb593b72 100644 --- a/stdlib/public/stubs/ThreadLocalStorage.cpp +++ b/stdlib/public/stubs/ThreadLocalStorage.cpp @@ -25,6 +25,26 @@ void *_stdlib_createTLS(void); #if !SWIFT_TLS_HAS_RESERVED_PTHREAD_SPECIFIC || (defined(_WIN32) && !defined(__CYGWIN__)) +#if defined(__wasi__) +#define STUB() do { /* fprintf(stderr, "%s is unsupported on WASI environment\n", __func__);*/ abort(); } while(0) +void wasi_polyfill_call_once(int *flag, void *context, void (*func)(void *)) + { + switch (*flag) { + case 0: + func(context); + *flag = 1; + return; + case 1: + return; + default: + STUB(); + } + } +int wasi_polyfill_pthread_key_create(__swift_thread_key_t *key, void (*destructor)(void*)) { STUB(); } +void *wasi_polyfill_pthread_getspecific(__swift_thread_key_t key) { STUB(); } +int wasi_polyfill_pthread_setspecific(__swift_thread_key_t key, const void *value) { STUB(); } +#endif + static void #if defined(_M_IX86) __stdcall diff --git a/utils/webassembly/static-executable-args.lnk b/utils/webassembly/static-executable-args.lnk index 0f8105ca97de6..d80b4a58a272c 100644 --- a/utils/webassembly/static-executable-args.lnk +++ b/utils/webassembly/static-executable-args.lnk @@ -2,13 +2,15 @@ -lswiftCore -lswiftImageInspectionShared -lswiftSwiftOnoneSupport +-lswiftWasiPthread -licui18n -licuuc -licudata -ldl -lstdc++ -lm +-lwasi-emulated-mman -Xlinker --error-limit=0 -Xlinker --no-gc-sections -Xlinker --no-threads - +-D_WASI_EMULATED_MMAN diff --git a/utils/webassembly/static-stdlib-args.lnk b/utils/webassembly/static-stdlib-args.lnk index 5d1397e2b74b8..af390c6413bd2 100644 --- a/utils/webassembly/static-stdlib-args.lnk +++ b/utils/webassembly/static-stdlib-args.lnk @@ -1,6 +1,6 @@ -ldl --lpthread -latomic +-lswiftWasiPthread -lswiftCore -latomic -lswiftImageInspectionShared From 20a16aaf6ae71987ee6507dabec8eda5a4c7d397 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Wed, 25 Dec 2019 10:07:49 +0900 Subject: [PATCH 74/80] Fix sil-func-extractor and driver (#28) * [WASM] Remove rpath flag and link objects using llvm-ar instead of ar * [WASM] Disable objc interop for sil-func-extractor when targeting wasm * [WASM] Exclude test cases which use -enable-objc-interop * [WASM] Make availability_returns_twice.swift unsupoorted on wasi due to lack of setjmp --- lib/Driver/UnixToolChains.cpp | 7 +++++- .../availability_returns_twice.swift | 1 + test/lit.cfg | 24 +++++++++++++++++-- .../SILFunctionExtractor.cpp | 2 ++ 4 files changed, 31 insertions(+), 3 deletions(-) diff --git a/lib/Driver/UnixToolChains.cpp b/lib/Driver/UnixToolChains.cpp index 07a19d9e548ac..7607c31b86ff2 100644 --- a/lib/Driver/UnixToolChains.cpp +++ b/lib/Driver/UnixToolChains.cpp @@ -346,7 +346,12 @@ toolchains::GenericUnix::constructInvocation(const StaticLinkJobAction &job, ArgStringList Arguments; // Configure the toolchain. - const char *AR = "ar"; + const char *AR; + if (getTriple().isOSBinFormatWasm()) { + AR = "llvm-ar"; + } else { + AR = "ar"; + } Arguments.push_back("crs"); Arguments.push_back( diff --git a/test/ClangImporter/availability_returns_twice.swift b/test/ClangImporter/availability_returns_twice.swift index ef9ff6ffad251..c91e922fb0e89 100644 --- a/test/ClangImporter/availability_returns_twice.swift +++ b/test/ClangImporter/availability_returns_twice.swift @@ -1,5 +1,6 @@ // RUN: %target-typecheck-verify-swift // UNSUPPORTED: OS=windows-msvc +// UNSUPPORTED: OS=wasi // In Android jmp_buf is int[16], which doesn't convert to &Int (SR-9136) // XFAIL: OS=linux-androideabi // XFAIL: OS=linux-android diff --git a/test/lit.cfg b/test/lit.cfg index 97ebcc59490d4..d25b688e8aeb8 100644 --- a/test/lit.cfg +++ b/test/lit.cfg @@ -1310,10 +1310,30 @@ elif run_os == 'wasi': config.target_object_format = "wasm" config.target_shared_library_prefix = 'lib' - config.target_shared_library_suffix = ".so" + config.target_shared_library_suffix = ".a" config.target_sdk_name = "wasi" config.target_runtime = "native" + # Exclude test cases that use objc-interop because clang doesn't support it + # with WebAssembly binary file yet. + testfiles = glob.glob(os.path.join(config.test_source_root, "**", "*.swift")) + + def use_objc_interop(path): + with open(path) as f: + return '-enable-objc-interop' in f.read() + + import fnmatch + def objc_interop_enabled_filenames(path, filename_pat): + matches = [] + for root, dirnames, filenames in os.walk(path): + for filename in fnmatch.filter(filenames, filename_pat): + filepath = os.path.join(root, filename) + if not use_objc_interop(filepath): continue + matches.append(filename) + return matches + + config.excludes += objc_interop_enabled_filenames(config.test_source_root, "*.swift") + config.target_swift_autolink_extract = inferSwiftBinary("swift-autolink-extract") config.target_build_swift = ' '.join([ @@ -1330,7 +1350,7 @@ elif run_os == 'wasi': config.target_build_swift_dylib = ( "%s -parse-as-library -emit-library -o '\\1'" % (config.target_build_swift)) - config.target_add_rpath = r'-Xlinker -rpath -Xlinker \1' + config.target_add_rpath = '' config.target_swift_frontend = ( '%s -frontend -target %s %s %s %s %s ' % (config.swift, config.variant_triple, resource_dir_opt, mcp_opt, diff --git a/tools/sil-func-extractor/SILFunctionExtractor.cpp b/tools/sil-func-extractor/SILFunctionExtractor.cpp index 33d0082c8b352..3b9cd3d1c121f 100644 --- a/tools/sil-func-extractor/SILFunctionExtractor.cpp +++ b/tools/sil-func-extractor/SILFunctionExtractor.cpp @@ -249,6 +249,8 @@ int main(int argc, char **argv) { Invocation.getLangOptions().DisableAvailabilityChecking = true; Invocation.getLangOptions().EnableAccessControl = false; Invocation.getLangOptions().EnableObjCAttrRequiresFoundation = false; + if (Invocation.getLangOptions().Target.isOSBinFormatWasm()) + Invocation.getLangOptions().EnableObjCInterop = false; serialization::ExtendedValidationInfo extendedInfo; llvm::ErrorOr> FileBufOrErr = From f297a62ecf37f53f457bdae2b75b8fa04ae8fb7b Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Fri, 24 Jan 2020 11:57:00 +0000 Subject: [PATCH 75/80] [WASM] [CI] Install llvm on Linux --- utils/webassembly/ci-linux.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/webassembly/ci-linux.sh b/utils/webassembly/ci-linux.sh index 8bfb2229a4574..3d95c6cbaef72 100755 --- a/utils/webassembly/ci-linux.sh +++ b/utils/webassembly/ci-linux.sh @@ -7,7 +7,7 @@ sudo apt install \ libedit-dev libxml2-dev libsqlite3-dev swig \ libpython-dev libncurses5-dev pkg-config \ libblocksruntime-dev libcurl4-openssl-dev \ - systemtap-sdt-dev tzdata rsync wget + systemtap-sdt-dev tzdata rsync wget llvm SOURCE_PATH="$( cd "$(dirname $0)/../../.." && pwd )" SWIFT_PATH=$SOURCE_PATH/swift From 4651cb65f68990f143bbf76c623e52b9a37fa4d6 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Fri, 24 Jan 2020 12:39:38 +0000 Subject: [PATCH 76/80] [WASM] [CI] Fix to un-archive with J due to vm tool version And use --skip-repository to clone without checking out swift repo And install new dependency python-six due to python3 migration --- utils/webassembly/ci-linux.sh | 14 +++++++------- utils/webassembly/ci-mac.sh | 12 ++++++------ 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/utils/webassembly/ci-linux.sh b/utils/webassembly/ci-linux.sh index 3d95c6cbaef72..9a00e5d4cc821 100755 --- a/utils/webassembly/ci-linux.sh +++ b/utils/webassembly/ci-linux.sh @@ -1,8 +1,10 @@ #/bin/bash +set -ex + sudo apt update sudo apt install \ - git ninja-build clang python \ + git ninja-build clang python python-six \ uuid-dev libicu-dev icu-devtools libbsd-dev \ libedit-dev libxml2-dev libsqlite3-dev swig \ libpython-dev libncurses5-dev pkg-config \ @@ -14,15 +16,13 @@ SWIFT_PATH=$SOURCE_PATH/swift BUILD_SCRIPT=$SWIFT_PATH/utils/webassembly/build-linux.sh cd $SWIFT_PATH -export current_sha=`git rev-parse HEAD` -./utils/update-checkout --clone --scheme wasm -git checkout $current_sha +./utils/update-checkout --clone --scheme wasm --skip-repository swift # Install wasmtime sudo mkdir /opt/wasmtime && cd /opt/wasmtime wget -O - "https://github.com/bytecodealliance/wasmtime/releases/download/v0.8.0/wasmtime-v0.8.0-x86_64-linux.tar.xz" | \ - sudo tar x --strip-components 1 + sudo tar Jx --strip-components 1 sudo ln -sf /opt/wasmtime/* /usr/local/bin cd $SOURCE_PATH @@ -35,8 +35,8 @@ sudo ln -sf /opt/cmake/bin/* /usr/local/bin cmake --version wget -O dist-wasi-sdk.tgz https://github.com/swiftwasm/wasi-sdk/suites/370986556/artifacts/809002 -unzip dist-wasi-sdk.tgz -WASI_SDK_TAR_PATH=$(find dist-ubuntu-latest.tgz -type f -name "wasi-sdk-*") +unzip dist-wasi-sdk.tgz -d . +WASI_SDK_TAR_PATH=$(find . -type f -name "wasi-sdk-*") WASI_SDK_FULL_NAME=$(basename $WASI_SDK_TAR_PATH -linux.tar.gz) tar xfz $WASI_SDK_TAR_PATH mv $WASI_SDK_FULL_NAME ./wasi-sdk diff --git a/utils/webassembly/ci-mac.sh b/utils/webassembly/ci-mac.sh index a1b52e648da8e..2606942e5d0aa 100755 --- a/utils/webassembly/ci-mac.sh +++ b/utils/webassembly/ci-mac.sh @@ -1,5 +1,7 @@ #/bin/bash +set -ex + brew install cmake ninja llvm SOURCE_PATH="$( cd "$(dirname $0)/../../.." && pwd )" @@ -7,22 +9,20 @@ SWIFT_PATH=$SOURCE_PATH/swift BUILD_SCRIPT=$SWIFT_PATH/utils/webassembly/build-mac.sh cd $SWIFT_PATH -export current_sha=`git rev-parse HEAD` -./utils/update-checkout --clone --scheme wasm -git checkout $current_sha +./utils/update-checkout --clone --scheme wasm --skip-repository swift # Install wasmtime sudo mkdir /opt/wasmtime && cd /opt/wasmtime wget -O - "https://github.com/bytecodealliance/wasmtime/releases/download/v0.8.0/wasmtime-v0.8.0-x86_64-macos.tar.xz" | \ - sudo tar x --strip-components 1 + sudo tar Jx --strip-components 1 sudo ln -sf /opt/wasmtime/* /usr/local/bin cd $SOURCE_PATH wget -O dist-wasi-sdk.tgz https://github.com/swiftwasm/wasi-sdk/suites/370986556/artifacts/809001 -tar xfz dist-wasi-sdk.tgz -WASI_SDK_TAR_PATH=$(find dist-macos-latest.tgz -type f -name "wasi-sdk-*") +unzip dist-wasi-sdk.tgz -d . +WASI_SDK_TAR_PATH=$(find . -type f -name "wasi-sdk-*") WASI_SDK_FULL_NAME=$(basename $WASI_SDK_TAR_PATH -macos.tar.gz) tar xfz $WASI_SDK_TAR_PATH mv $WASI_SDK_FULL_NAME ./wasi-sdk From 62294fd1e44858729f256a62f9d363b7cb10c167 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Fri, 24 Jan 2020 12:45:26 +0000 Subject: [PATCH 77/80] [WASM] [CI] Specify repository name --- .github/workflows/main.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index f300cbd721967..80eb85fecf0e6 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -15,6 +15,8 @@ jobs: steps: - uses: actions/checkout@v1 + with: + path: swift - name: Build Linux installable archive run: ./utils/webassembly/ci-linux.sh - name: Upload Linux installable archive @@ -36,6 +38,8 @@ jobs: steps: - uses: actions/checkout@v1 + with: + path: swift - name: Build macOS installable archive run: ./utils/webassembly/ci-mac.sh - name: Upload macOS installable archive From 7e457d5d7b1676f7384a49c82f55b8bdc364d1eb Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Fri, 24 Jan 2020 13:31:08 +0000 Subject: [PATCH 78/80] [WASM] [CI] Install six manually --- utils/webassembly/ci-mac.sh | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/utils/webassembly/ci-mac.sh b/utils/webassembly/ci-mac.sh index 2606942e5d0aa..e2c6e3bb49589 100755 --- a/utils/webassembly/ci-mac.sh +++ b/utils/webassembly/ci-mac.sh @@ -2,7 +2,10 @@ set -ex -brew install cmake ninja llvm +brew install cmake ninja llvm python@2 + +# Install six for python3 migration +pip2 install six SOURCE_PATH="$( cd "$(dirname $0)/../../.." && pwd )" SWIFT_PATH=$SOURCE_PATH/swift From 119a61b6de4dccbdb0ab540253251371306261cd Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Fri, 24 Jan 2020 14:50:03 +0000 Subject: [PATCH 79/80] Fix conflict mistake --- test/CMakeLists.txt | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 2bb57c54b70e8..e457525edbe19 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -260,9 +260,11 @@ _Block_release(void) { }\n") # doesn't need to be build for macCatalyst. list(APPEND test_dependencies "swift-reflection-test${DEFAULT_OSX_VARIANT_SUFFIX}") - if("${SDK}" STREQUAL "WASI") + + endif() + + if(NOT "${SDK}" STREQUAL "WASI") # wasm: Avoid to build swift-reflection-test because it uses unsupported linker flags for wasm-ld - elseif() list(APPEND test_dependencies "swift-reflection-test${VARIANT_SUFFIX}_signed") endif() From f4fbad55eabebf32cb39b6bfc4082ef1ffa39d29 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Sun, 12 Jan 2020 22:33:33 +0900 Subject: [PATCH 80/80] Fix unit tests part1 (#31) * [WASM] Disable mmap test case for WASI OS due to lack of WASI API * [WASM] Disable simd test case for WASI OS simd api maybe stable in future https://github.com/WebAssembly/simd * [WASM] Fix to build pthread pollyfill only when wasi sdk * [WASM] Implement parsing command line arguments * [WASM] Run StdlibUnittest in process instead of child proc --- .../StdlibUnittest/StdlibUnittest.swift | 5 +++ stdlib/public/WASI/CMakeLists.txt | 1 + stdlib/public/stubs/CommandLine.cpp | 36 +++++++++++++++++++ test/stdlib/mmap.swift | 1 + test/stdlib/simd_diagnostics.swift | 2 +- 5 files changed, 44 insertions(+), 1 deletion(-) diff --git a/stdlib/private/StdlibUnittest/StdlibUnittest.swift b/stdlib/private/StdlibUnittest/StdlibUnittest.swift index 51ac487875af9..019de920c4201 100644 --- a/stdlib/private/StdlibUnittest/StdlibUnittest.swift +++ b/stdlib/private/StdlibUnittest/StdlibUnittest.swift @@ -1449,7 +1449,12 @@ public func runAllTests() { if _isChildProcess { _childProcess() } else { + #if os(WASI) + // WASI doesn't support child process + var runTestsInProcess: Bool = true + #else var runTestsInProcess: Bool = false + #endif var filter: String? var args = [String]() var i = 0 diff --git a/stdlib/public/WASI/CMakeLists.txt b/stdlib/public/WASI/CMakeLists.txt index 451f1f3af57d7..0d088443ad7be 100644 --- a/stdlib/public/WASI/CMakeLists.txt +++ b/stdlib/public/WASI/CMakeLists.txt @@ -1,3 +1,4 @@ add_swift_target_library(swiftWasiPthread STATIC IS_STDLIB Pthread.cpp + TARGET_SDKS WASI INSTALL_IN_COMPONENT stdlib) diff --git a/stdlib/public/stubs/CommandLine.cpp b/stdlib/public/stubs/CommandLine.cpp index 0b4506391beeb..cf371e5ababe4 100644 --- a/stdlib/public/stubs/CommandLine.cpp +++ b/stdlib/public/stubs/CommandLine.cpp @@ -206,6 +206,42 @@ char ** _swift_stdlib_getUnsafeArgvArgc(int *outArgLen) { return outBuf; } +#elif defined(__wasi__) +#include +#include +#include + +SWIFT_RUNTIME_STDLIB_API +char ** _swift_stdlib_getUnsafeArgvArgc(int *outArgLen) { + assert(outArgLen != nullptr); + + if (_swift_stdlib_ProcessOverrideUnsafeArgv) { + *outArgLen = _swift_stdlib_ProcessOverrideUnsafeArgc; + return _swift_stdlib_ProcessOverrideUnsafeArgv; + } + + __wasi_errno_t err; + + size_t argv_buf_size; + size_t argc; + err = __wasi_args_sizes_get(&argc, &argv_buf_size); + if (err != __WASI_ERRNO_SUCCESS) return nullptr; + + size_t num_ptrs = argc + 1; + char *argv_buf = (char *)malloc(argv_buf_size); + char **argv = (char **)calloc(num_ptrs, sizeof(char *)); + + err = __wasi_args_get((uint8_t **)argv, (uint8_t *)argv_buf); + if (err != __WASI_ERRNO_SUCCESS) { + free(argv_buf); + free(argv); + return nullptr; + } + + *outArgLen = static_cast(argc); + + return argv; +} #else // Add your favorite OS's command line arg grabber here. SWIFT_RUNTIME_STDLIB_API char ** _swift_stdlib_getUnsafeArgvArgc(int *outArgLen) { diff --git a/test/stdlib/mmap.swift b/test/stdlib/mmap.swift index 6879941e9f7dd..7008923d4c660 100644 --- a/test/stdlib/mmap.swift +++ b/test/stdlib/mmap.swift @@ -1,6 +1,7 @@ // RUN: %target-run-simple-swift %t // REQUIRES: executable_test // UNSUPPORTED: OS=windows-msvc +// UNSUPPORTED: OS=wasi import StdlibUnittest #if os(macOS) || os(iOS) || os(tvOS) || os(watchOS) diff --git a/test/stdlib/simd_diagnostics.swift b/test/stdlib/simd_diagnostics.swift index 2aa1be085bc37..d2230cc2a0970 100644 --- a/test/stdlib/simd_diagnostics.swift +++ b/test/stdlib/simd_diagnostics.swift @@ -1,7 +1,7 @@ // RUN: %target-typecheck-verify-swift // FIXME: No simd module on linux rdar://problem/20795411 -// XFAIL: linux, windows +// XFAIL: linux, windows, wasm import simd