From 04898dc23636e5e746f03292246089338836b38a Mon Sep 17 00:00:00 2001 From: Zhuowei Zhang Date: Mon, 8 Apr 2019 22:30:48 -0700 Subject: [PATCH 001/838] 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 002/838] 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 003/838] 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 004/838] 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 005/838] 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 006/838] 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 007/838] 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 008/838] 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 009/838] 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 010/838] 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 011/838] 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 012/838] 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 013/838] 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 014/838] 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 015/838] 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 016/838] 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 017/838] 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 018/838] 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 019/838] 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 020/838] 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 021/838] 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 022/838] 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 023/838] 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 024/838] 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 025/838] 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 026/838] 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 027/838] 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 028/838] 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 029/838] 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 030/838] 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 031/838] 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 032/838] 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 033/838] 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 034/838] 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 035/838] 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 036/838] 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 037/838] 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 038/838] 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 039/838] 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 040/838] 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 041/838] 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 042/838] 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 043/838] 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 044/838] 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 045/838] 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 046/838] 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 047/838] 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 048/838] 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 049/838] 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 050/838] 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 051/838] 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 052/838] [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 053/838] 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 054/838] 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 055/838] 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 056/838] 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 057/838] 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 058/838] 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 059/838] 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 060/838] 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 061/838] 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 062/838] [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 063/838] 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 064/838] [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 065/838] [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 066/838] [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 067/838] [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 068/838] 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 069/838] 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 070/838] 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 071/838] 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 072/838] 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 073/838] 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 074/838] 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 075/838] [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 076/838] [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 077/838] [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 078/838] [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 079/838] 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 080/838] 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 From 480d476ab7e067c111a7c6cba1c757ac0440afe5 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Wed, 29 Jan 2020 23:15:41 +0900 Subject: [PATCH 081/838] Install pull to track upstream (#34) --- .github/pull.yml | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .github/pull.yml diff --git a/.github/pull.yml b/.github/pull.yml new file mode 100644 index 0000000000000..32762ab446c0d --- /dev/null +++ b/.github/pull.yml @@ -0,0 +1,6 @@ +version: "1" +rules: + - base: swiftwasm + upstream: apple:master + mergeMethod: merge +label: ":arrow_heading_down: Upstream Tracking" From 5529fd747d9777f2e8579670c68dedf1738581a4 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Sat, 1 Feb 2020 18:31:46 +0900 Subject: [PATCH 082/838] Update upstream tracking strategy via own master branch (#36) * Update upstream tracking strategy via own master branch Changed to create merge PR from it's own master branch which is automatically synced with apple:master. Creating merge PR from apple:master sends CI failure email to apple members, so we need to change tracking strategy. New Tracking steps: 1. sync master branch from apple:master apple:master -> swiftwasm:master 2. create merge PR from swiftwasm:master swiftwasm:master -> swiftwasm:swiftwasm * Use released artifact instead of Actions artifacts directly --- .github/pull.yml | 5 ++++- utils/webassembly/ci-linux.sh | 4 ++-- utils/webassembly/ci-mac.sh | 4 ++-- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/.github/pull.yml b/.github/pull.yml index 32762ab446c0d..3ff8eb467b088 100644 --- a/.github/pull.yml +++ b/.github/pull.yml @@ -1,6 +1,9 @@ version: "1" rules: - base: swiftwasm - upstream: apple:master + upstream: master mergeMethod: merge + - base: master + upstream: apple:master + mergeMethod: hardreset label: ":arrow_heading_down: Upstream Tracking" diff --git a/utils/webassembly/ci-linux.sh b/utils/webassembly/ci-linux.sh index 9a00e5d4cc821..8d3b5b18316d9 100755 --- a/utils/webassembly/ci-linux.sh +++ b/utils/webassembly/ci-linux.sh @@ -34,8 +34,8 @@ 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 -d . +wget -O dist-wasi-sdk.tgz.zip "https://github.com/swiftwasm/wasi-sdk/releases/download/0.1.0-swiftwasm/dist-ubuntu-latest.tgz.zip" +unzip dist-wasi-sdk.tgz.zip -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 diff --git a/utils/webassembly/ci-mac.sh b/utils/webassembly/ci-mac.sh index e2c6e3bb49589..e3e898366f1b7 100755 --- a/utils/webassembly/ci-mac.sh +++ b/utils/webassembly/ci-mac.sh @@ -23,8 +23,8 @@ 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 -unzip dist-wasi-sdk.tgz -d . +wget -O dist-wasi-sdk.tgz.zip "https://github.com/swiftwasm/wasi-sdk/releases/download/0.1.0-swiftwasm/dist-macos-latest.tgz.zip" +unzip dist-wasi-sdk.tgz.zip -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 From c6ba966d8665e654560ee16c0c162a1d026cbddc Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Tue, 11 Feb 2020 00:54:40 +0000 Subject: [PATCH 083/838] Add ifdef to ThreadBarriers to fix master builds (#107) --- .../SwiftPrivateThreadExtras/ThreadBarriers.swift | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/stdlib/private/SwiftPrivateThreadExtras/ThreadBarriers.swift b/stdlib/private/SwiftPrivateThreadExtras/ThreadBarriers.swift index 8bef3340ec82a..1750ffe1b1a03 100644 --- a/stdlib/private/SwiftPrivateThreadExtras/ThreadBarriers.swift +++ b/stdlib/private/SwiftPrivateThreadExtras/ThreadBarriers.swift @@ -87,6 +87,19 @@ public func _stdlib_thread_barrier_init( return 0 } +#if !os(Windows) && !os(WASI) +private func _stdlib_thread_barrier_mutex_and_cond_init(_ barrier: UnsafeMutablePointer<_stdlib_thread_barrier_t>) -> CInt { + guard pthread_mutex_init(barrier.pointee.mutex!, nil) == 0 else { + return -1 + } + guard pthread_cond_init(barrier.pointee.cond!, nil) == 0 else { + pthread_mutex_destroy(barrier.pointee.mutex!) + return -1 + } + return 0 +} +#endif + public func _stdlib_thread_barrier_destroy( _ barrier: UnsafeMutablePointer<_stdlib_thread_barrier_t> ) -> CInt { From 30d2e69cd6d66c3d405382cf3186bce39fe91825 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Thu, 13 Feb 2020 04:46:13 +0900 Subject: [PATCH 084/838] [WASM] Update WASI SDK to 0.2.0 (#138) * Add swift_symbolgraph_extract variable for wasi to run lit test * [WASM] Update WASI SDK to 0.2.0 When using wasm-ld in wasi-sdk 0.1.0-swiftwasm, some test cases in swift are failed with "failed to demangle witness for associated type 'Iterator' in conformance 'Swift.UInt64.Words: Sequence' from mangled name '$e'" message. This message means that runtime library fails to look up for protocol conformance from swift5_protocol_conformances section. In WebAssembly binary, these protocol conformances data are stored in data segments in each object files and link them into one segment by wasm-ld. Swift runtime library uses special __start and __stop symbols to point where the metadata section is located. These special symbols are emitted by linker wasm-ld. But wasm-ld in wasi-sdk 0.1.0 emitited them for each data segments without combining them by their name. This causes the crash if an user code has a protocol conformance because __start and __stop symbols point only the user defined protocol conformance segment. The wasm-ld bug has been fixed by this patch https://reviews.llvm.org/D64148 --- test/lit.cfg | 4 ++++ utils/webassembly/ci-linux.sh | 2 +- utils/webassembly/ci-mac.sh | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/test/lit.cfg b/test/lit.cfg index 71a49847fa340..3036aabd4bbcd 100644 --- a/test/lit.cfg +++ b/test/lit.cfg @@ -1367,6 +1367,10 @@ elif run_os == 'wasi': 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_symbolgraph_extract = ' '.join([ + config.swift_symbolgraph_extract, + '-target', config.variant_triple, + mcp_opt]) config.target_swift_ide_test = ( '%s -target %s %s %s %s' % (config.swift_ide_test, config.variant_triple, resource_dir_opt, diff --git a/utils/webassembly/ci-linux.sh b/utils/webassembly/ci-linux.sh index 8d3b5b18316d9..0d194e43914f9 100755 --- a/utils/webassembly/ci-linux.sh +++ b/utils/webassembly/ci-linux.sh @@ -34,7 +34,7 @@ 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.zip "https://github.com/swiftwasm/wasi-sdk/releases/download/0.1.0-swiftwasm/dist-ubuntu-latest.tgz.zip" +wget -O dist-wasi-sdk.tgz.zip "https://github.com/swiftwasm/wasi-sdk/releases/download/0.2.0-swiftwasm/dist-ubuntu-latest.tgz.zip" unzip dist-wasi-sdk.tgz.zip -d . WASI_SDK_TAR_PATH=$(find . -type f -name "wasi-sdk-*") WASI_SDK_FULL_NAME=$(basename $WASI_SDK_TAR_PATH -linux.tar.gz) diff --git a/utils/webassembly/ci-mac.sh b/utils/webassembly/ci-mac.sh index e3e898366f1b7..c9caa6b29787e 100755 --- a/utils/webassembly/ci-mac.sh +++ b/utils/webassembly/ci-mac.sh @@ -23,7 +23,7 @@ sudo ln -sf /opt/wasmtime/* /usr/local/bin cd $SOURCE_PATH -wget -O dist-wasi-sdk.tgz.zip "https://github.com/swiftwasm/wasi-sdk/releases/download/0.1.0-swiftwasm/dist-macos-latest.tgz.zip" +wget -O dist-wasi-sdk.tgz.zip "https://github.com/swiftwasm/wasi-sdk/releases/download/0.2.0-swiftwasm/dist-macos-latest.tgz.zip" unzip dist-wasi-sdk.tgz.zip -d . WASI_SDK_TAR_PATH=$(find . -type f -name "wasi-sdk-*") WASI_SDK_FULL_NAME=$(basename $WASI_SDK_TAR_PATH -macos.tar.gz) From 6494fbfbd88914a3bdddb5573bcc3a1c40990dc0 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Thu, 13 Feb 2020 04:27:02 +0000 Subject: [PATCH 085/838] [WASM] Remove static flags from lit test command --- test/lit.cfg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/lit.cfg b/test/lit.cfg index 3036aabd4bbcd..a62f1c8871cc7 100644 --- a/test/lit.cfg +++ b/test/lit.cfg @@ -1341,7 +1341,7 @@ elif run_os == 'wasi': config.target_swift_autolink_extract = inferSwiftBinary("swift-autolink-extract") config.target_build_swift = ' '.join([ - '%s', '-target %s', '-static', '-static-executable', + '%s', '-target %s', '-Xcc --sysroot=%s', '-Xclang-linker --sysroot=%s', '-tools-directory %s', '-toolchain-stdlib-rpath %s', '%s %s %s %s' From dc0dcd16e3de8896eb0885ce11d6abf038ec419a Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Thu, 13 Feb 2020 13:23:56 +0900 Subject: [PATCH 086/838] Add WebAssembly Toolchain --- lib/Driver/CMakeLists.txt | 1 + lib/Driver/Driver.cpp | 2 +- lib/Driver/ToolChains.h | 17 ++ lib/Driver/UnixToolChains.cpp | 7 +- lib/Driver/WebAssemblyToolChains.cpp | 232 +++++++++++++++++++++++++++ 5 files changed, 252 insertions(+), 7 deletions(-) create mode 100644 lib/Driver/WebAssemblyToolChains.cpp diff --git a/lib/Driver/CMakeLists.txt b/lib/Driver/CMakeLists.txt index d1c4893e30152..ef6b9436fec4e 100644 --- a/lib/Driver/CMakeLists.txt +++ b/lib/Driver/CMakeLists.txt @@ -15,6 +15,7 @@ set(swiftDriver_sources ToolChains.cpp UnixToolChains.cpp WindowsToolChains.cpp + WebAssemblyToolChains.cpp ) set(swiftDriver_targetDefines) diff --git a/lib/Driver/Driver.cpp b/lib/Driver/Driver.cpp index 2eb888a76be4d..dc69e8930c521 100644 --- a/lib/Driver/Driver.cpp +++ b/lib/Driver/Driver.cpp @@ -274,7 +274,7 @@ Driver::buildToolChain(const llvm::opt::InputArgList &ArgList) { case llvm::Triple::Haiku: return std::make_unique(*this, target); case llvm::Triple::WASI: - return std::make_unique(*this, target); + return std::make_unique(*this, target); default: Diags.diagnose(SourceLoc(), diag::error_unknown_target, ArgList.getLastArg(options::OPT_target)->getValue()); diff --git a/lib/Driver/ToolChains.h b/lib/Driver/ToolChains.h index addeeba50f52c..7e32a276d5911 100644 --- a/lib/Driver/ToolChains.h +++ b/lib/Driver/ToolChains.h @@ -92,6 +92,23 @@ class LLVM_LIBRARY_VISIBILITY Windows : public ToolChain { bool shared = true) const override; }; +class LLVM_LIBRARY_VISIBILITY WebAssembly : public ToolChain { +protected: + InvocationInfo constructInvocation(const AutolinkExtractJobAction &job, + const JobContext &context) const override; + InvocationInfo constructInvocation(const DynamicLinkJobAction &job, + const JobContext &context) const override; + InvocationInfo constructInvocation(const StaticLinkJobAction &job, + const JobContext &context) const override; + +public: + WebAssembly(const Driver &D, const llvm::Triple &Triple) : ToolChain(D, Triple) {} + ~WebAssembly() = default; + std::string sanitizerRuntimeLibName(StringRef Sanitizer, + bool shared = true) const override; +}; + + class LLVM_LIBRARY_VISIBILITY GenericUnix : public ToolChain { protected: InvocationInfo constructInvocation(const InterpretJobAction &job, diff --git a/lib/Driver/UnixToolChains.cpp b/lib/Driver/UnixToolChains.cpp index 7607c31b86ff2..07a19d9e548ac 100644 --- a/lib/Driver/UnixToolChains.cpp +++ b/lib/Driver/UnixToolChains.cpp @@ -346,12 +346,7 @@ toolchains::GenericUnix::constructInvocation(const StaticLinkJobAction &job, ArgStringList Arguments; // Configure the toolchain. - const char *AR; - if (getTriple().isOSBinFormatWasm()) { - AR = "llvm-ar"; - } else { - AR = "ar"; - } + const char *AR = "ar"; Arguments.push_back("crs"); Arguments.push_back( diff --git a/lib/Driver/WebAssemblyToolChains.cpp b/lib/Driver/WebAssemblyToolChains.cpp new file mode 100644 index 0000000000000..55f1c329a5f39 --- /dev/null +++ b/lib/Driver/WebAssemblyToolChains.cpp @@ -0,0 +1,232 @@ +//===---- WebAssemblyToolChains.cpp - Job invocations (WebAssembly-specific) ------===// +// +// This source file is part of the Swift.org open source project +// +// Copyright (c) 2014 - 2018 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 "ToolChains.h" + +#include "swift/Basic/Dwarf.h" +#include "swift/Basic/LLVM.h" +#include "swift/Basic/Platform.h" +#include "swift/Basic/Range.h" +#include "swift/Basic/TaskQueue.h" +#include "swift/Config.h" +#include "swift/Driver/Compilation.h" +#include "swift/Driver/Driver.h" +#include "swift/Driver/Job.h" +#include "swift/Option/Options.h" +#include "swift/Option/SanitizerOptions.h" +#include "clang/Basic/Version.h" +#include "clang/Driver/Util.h" +#include "llvm/ADT/StringSwitch.h" +#include "llvm/Option/Arg.h" +#include "llvm/Option/ArgList.h" +#include "llvm/ProfileData/InstrProf.h" +#include "llvm/Support/FileSystem.h" +#include "llvm/Support/Path.h" +#include "llvm/Support/Process.h" +#include "llvm/Support/Program.h" + +using namespace swift; +using namespace swift::driver; +using namespace llvm::opt; + +std::string toolchains::WebAssembly::sanitizerRuntimeLibName(StringRef Sanitizer, + bool shared) const { + return (Twine("clang_rt.") + Sanitizer + "-" + + this->getTriple().getArchName() + ".lib") + .str(); +} + +ToolChain::InvocationInfo toolchains::WebAssembly::constructInvocation( + const AutolinkExtractJobAction &job, const JobContext &context) const { + assert(context.Output.getPrimaryOutputType() == file_types::TY_AutolinkFile); + + InvocationInfo II{"swift-autolink-extract"}; + ArgStringList &Arguments = II.Arguments; + II.allowsResponseFiles = true; + + addPrimaryInputsOfType(Arguments, context.Inputs, context.Args, + file_types::TY_Object); + addInputsOfType(Arguments, context.InputActions, file_types::TY_Object); + + Arguments.push_back("-o"); + Arguments.push_back( + context.Args.MakeArgString(context.Output.getPrimaryOutputFilename())); + + return II; +} + +ToolChain::InvocationInfo +toolchains::WebAssembly::constructInvocation(const DynamicLinkJobAction &job, + const JobContext &context) const { + assert(context.Output.getPrimaryOutputType() == file_types::TY_Image && + "Invalid linker output type."); + + ArgStringList Arguments; + + std::string Target = getTriple().str(); + if (!Target.empty()) { + Arguments.push_back("-target"); + Arguments.push_back(context.Args.MakeArgString(Target)); + } + + switch (job.getKind()) { + case LinkKind::None: + llvm_unreachable("invalid link kind"); + case LinkKind::Executable: + // Default case, nothing extra needed. + break; + case LinkKind::DynamicLibrary: + llvm_unreachable("WebAssembly doesn't support dynamic library yet"); + case LinkKind::StaticLibrary: + llvm_unreachable("invalid link kind"); + } + + // Select the linker to use. + std::string Linker; + if (const Arg *A = context.Args.getLastArg(options::OPT_use_ld)) { + Linker = A->getValue(); + } + if (!Linker.empty()) + Arguments.push_back(context.Args.MakeArgString("-fuse-ld=" + Linker)); + + + const char *Clang = "clang"; + if (const Arg *A = context.Args.getLastArg(options::OPT_tools_directory)) { + StringRef toolchainPath(A->getValue()); + + // If there is a clang in the toolchain folder, use that instead. + if (auto tool = llvm::sys::findProgramByName("clang", {toolchainPath})) + Clang = context.Args.MakeArgString(tool.get()); + } + + SmallVector RuntimeLibPaths; + getRuntimeLibraryPaths(RuntimeLibPaths, context.Args, context.OI.SDKPath, + /*Shared=*/false); + + SmallString<128> SharedResourceDirPath; + getResourceDirPath(SharedResourceDirPath, context.Args, /*Shared=*/false); + + SmallString<128> swiftrtPath = SharedResourceDirPath; + llvm::sys::path::append(swiftrtPath, + swift::getMajorArchitectureName(getTriple())); + llvm::sys::path::append(swiftrtPath, "swiftrt.o"); + Arguments.push_back(context.Args.MakeArgString(swiftrtPath)); + + addPrimaryInputsOfType(Arguments, context.Inputs, context.Args, + file_types::TY_Object); + addInputsOfType(Arguments, context.InputActions, file_types::TY_Object); + + if (!context.OI.SDKPath.empty()) { + Arguments.push_back("--sysroot"); + Arguments.push_back(context.Args.MakeArgString(context.OI.SDKPath)); + } + + // Add any autolinking scripts to the arguments + for (const Job *Cmd : context.Inputs) { + auto &OutputInfo = Cmd->getOutput(); + if (OutputInfo.getPrimaryOutputType() == file_types::TY_AutolinkFile) + Arguments.push_back(context.Args.MakeArgString( + Twine("@") + OutputInfo.getPrimaryOutputFilename())); + } + + // Add the runtime library link paths. + for (auto path : RuntimeLibPaths) { + Arguments.push_back("-L"); + Arguments.push_back(context.Args.MakeArgString(path)); + } + // Link the standard library. In two paths, we do this using a .lnk file; + // if we're going that route, we'll set `linkFilePath` to the path to that + // file. + SmallString<128> linkFilePath; + getResourceDirPath(linkFilePath, context.Args, /*Shared=*/false); + llvm::sys::path::append(linkFilePath, "static-executable-args.lnk"); + + auto linkFile = linkFilePath.str(); + if (llvm::sys::fs::is_regular_file(linkFile)) { + Arguments.push_back(context.Args.MakeArgString(Twine("@") + linkFile)); + } else { + llvm::report_fatal_error(linkFile + " not found"); + } + + // Explicitly pass the target to the linker + Arguments.push_back( + context.Args.MakeArgString("--target=" + getTriple().str())); + + // Delegate to Clang for sanitizers. It will figure out the correct linker + // options. + if (job.getKind() == LinkKind::Executable && context.OI.SelectedSanitizers) { + Arguments.push_back(context.Args.MakeArgString( + "-fsanitize=" + getSanitizerList(context.OI.SelectedSanitizers))); + + // The TSan runtime depends on the blocks runtime and libdispatch. + if (context.OI.SelectedSanitizers & SanitizerKind::Thread) { + Arguments.push_back("-lBlocksRuntime"); + Arguments.push_back("-ldispatch"); + } + } + + if (context.Args.hasArg(options::OPT_profile_generate)) { + SmallString<128> LibProfile(SharedResourceDirPath); + llvm::sys::path::remove_filename(LibProfile); // remove platform name + llvm::sys::path::append(LibProfile, "clang", "lib"); + + llvm::sys::path::append(LibProfile, getTriple().getOSName(), + Twine("libclang_rt.profile-") + + getTriple().getArchName() + ".a"); + Arguments.push_back(context.Args.MakeArgString(LibProfile)); + Arguments.push_back(context.Args.MakeArgString( + Twine("-u", llvm::getInstrProfRuntimeHookVarName()))); + } + + // Run clang++ in verbose mode if "-v" is set + if (context.Args.hasArg(options::OPT_v)) { + Arguments.push_back("-v"); + } + + // These custom arguments should be right before the object file at the end. + context.Args.AddAllArgs(Arguments, options::OPT_linker_option_Group); + context.Args.AddAllArgs(Arguments, options::OPT_Xlinker); + context.Args.AddAllArgValues(Arguments, options::OPT_Xclang_linker); + + // This should be the last option, for convenience in checking output. + Arguments.push_back("-o"); + Arguments.push_back( + context.Args.MakeArgString(context.Output.getPrimaryOutputFilename())); + + InvocationInfo II{Clang, Arguments}; + II.allowsResponseFiles = true; + + return II; +} + +ToolChain::InvocationInfo +toolchains::WebAssembly::constructInvocation(const StaticLinkJobAction &job, + const JobContext &context) const { + assert(context.Output.getPrimaryOutputType() == file_types::TY_Image && + "Invalid linker output type."); + + ArgStringList Arguments; + + const char *AR = "llvm-ar"; + Arguments.push_back("crs"); + + Arguments.push_back( + context.Args.MakeArgString(context.Output.getPrimaryOutputFilename())); + + addPrimaryInputsOfType(Arguments, context.Inputs, context.Args, + file_types::TY_Object); + addInputsOfType(Arguments, context.InputActions, file_types::TY_Object); + + InvocationInfo II{AR, Arguments}; + + return II; +} From dbf2808604ea9819350f68e7d000cb822ce0acd3 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Thu, 13 Feb 2020 05:58:50 +0000 Subject: [PATCH 087/838] [WASM] Disable lldb related tests --- test/lit.cfg | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/lit.cfg b/test/lit.cfg index a62f1c8871cc7..0d784bd154278 100644 --- a/test/lit.cfg +++ b/test/lit.cfg @@ -1325,6 +1325,9 @@ elif run_os == 'wasi': def use_objc_interop(path): with open(path) as f: return '-enable-objc-interop' in f.read() + def lldb_related_test(path): + with open(path) as f: + return 'lldb-moduleimport-test' in f.read() import fnmatch def objc_interop_enabled_filenames(path, filename_pat): @@ -1333,6 +1336,7 @@ elif run_os == 'wasi': for filename in fnmatch.filter(filenames, filename_pat): filepath = os.path.join(root, filename) if not use_objc_interop(filepath): continue + if not lldb_related_test(filepath): continue matches.append(filename) return matches From f1a3cfda31d774a42a78098375adda0ee2ab6441 Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Thu, 13 Feb 2020 22:30:08 +0000 Subject: [PATCH 088/838] Exclude SwiftPrivateThreadExtras on WASI --- stdlib/private/StdlibUnittest/CMakeLists.txt | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/stdlib/private/StdlibUnittest/CMakeLists.txt b/stdlib/private/StdlibUnittest/CMakeLists.txt index 9614bfbdcbc04..b61e4e77af1ee 100644 --- a/stdlib/private/StdlibUnittest/CMakeLists.txt +++ b/stdlib/private/StdlibUnittest/CMakeLists.txt @@ -31,17 +31,17 @@ add_swift_target_library(swiftStdlibUnittest ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} TestHelpers.swift TypeIndexed.swift - SWIFT_MODULE_DEPENDS SwiftPrivate SwiftPrivateThreadExtras SwiftPrivateLibcExtras - SWIFT_MODULE_DEPENDS_IOS Darwin Foundation - SWIFT_MODULE_DEPENDS_OSX Darwin Foundation - SWIFT_MODULE_DEPENDS_TVOS Darwin Foundation - SWIFT_MODULE_DEPENDS_WATCHOS Darwin Foundation - SWIFT_MODULE_DEPENDS_LINUX Glibc - SWIFT_MODULE_DEPENDS_FREEBSD Glibc - SWIFT_MODULE_DEPENDS_CYGWIN Glibc - SWIFT_MODULE_DEPENDS_HAIKU Glibc + SWIFT_MODULE_DEPENDS SwiftPrivate SwiftPrivateLibcExtras + SWIFT_MODULE_DEPENDS_IOS Darwin Foundation SwiftPrivateThreadExtras + SWIFT_MODULE_DEPENDS_OSX Darwin Foundation SwiftPrivateThreadExtras + SWIFT_MODULE_DEPENDS_TVOS Darwin Foundation SwiftPrivateThreadExtras + SWIFT_MODULE_DEPENDS_WATCHOS Darwin Foundation SwiftPrivateThreadExtras + SWIFT_MODULE_DEPENDS_LINUX Glibc SwiftPrivateThreadExtras + SWIFT_MODULE_DEPENDS_FREEBSD Glibc SwiftPrivateThreadExtras + SWIFT_MODULE_DEPENDS_CYGWIN Glibc SwiftPrivateThreadExtras + SWIFT_MODULE_DEPENDS_HAIKU Glibc SwiftPrivateThreadExtras SWIFT_MODULE_DEPENDS_WASI Glibc - SWIFT_MODULE_DEPENDS_WINDOWS MSVCRT WinSDK + SWIFT_MODULE_DEPENDS_WINDOWS MSVCRT WinSDK SwiftPrivateThreadExtras SWIFT_COMPILE_FLAGS ${swift_stdlib_unittest_compile_flags} ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS} INSTALL_IN_COMPONENT stdlib-experimental DARWIN_INSTALL_NAME_DIR "${SWIFT_DARWIN_STDLIB_PRIVATE_INSTALL_NAME_DIR}") From 6667e723465cb6e8d34b665b3793c56e97447f56 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Fri, 14 Feb 2020 10:33:20 +0000 Subject: [PATCH 089/838] [WASM] Add TLS polyfill --- include/swift/Basic/Lazy.h | 17 ++++++- stdlib/public/runtime/ThreadLocalStorage.h | 11 ++--- stdlib/public/stubs/ThreadLocalStorage.cpp | 56 ++++++++++++++-------- 3 files changed, 55 insertions(+), 29 deletions(-) diff --git a/include/swift/Basic/Lazy.h b/include/swift/Basic/Lazy.h index 647e5aff41c59..fa260a93df3c6 100644 --- a/include/swift/Basic/Lazy.h +++ b/include/swift/Basic/Lazy.h @@ -25,7 +25,20 @@ #include "swift/Basic/type_traits.h" #ifdef __wasi__ -void wasi_polyfill_call_once(int *flag, void *context, void (*func)(void *)); +inline void wasi_call_once(int *flag, void *context, void (*func)(void *)) { + switch (*flag) { + case 0: + func(context); + *flag = 1; + return; + case 1: + return; + default: + assert(false && "wasi_call_once got invalid flag"); + abort(); + } +} + #endif namespace swift { @@ -45,7 +58,7 @@ namespace swift { #elif defined(__wasi__) using OnceToken_t = int; # define SWIFT_ONCE_F(TOKEN, FUNC, CONTEXT) \ - ::wasi_polyfill_call_once(&TOKEN, CONTEXT, FUNC) + ::wasi_call_once(&TOKEN, CONTEXT, FUNC) #else using OnceToken_t = std::once_flag; # define SWIFT_ONCE_F(TOKEN, FUNC, CONTEXT) \ diff --git a/stdlib/public/runtime/ThreadLocalStorage.h b/stdlib/public/runtime/ThreadLocalStorage.h index 4e62b76b1d74e..6e27888294af1 100644 --- a/stdlib/public/runtime/ThreadLocalStorage.h +++ b/stdlib/public/runtime/ThreadLocalStorage.h @@ -82,7 +82,7 @@ typedef unsigned long __swift_thread_key_t; # elif defined(__HAIKU__) typedef int __swift_thread_key_t; # elif defined(__wasi__) -typedef unsigned int __swift_thread_key_t; +typedef unsigned long __swift_thread_key_t; # else typedef unsigned long __swift_thread_key_t; # endif @@ -99,12 +99,9 @@ static_assert(std::is_same<__swift_thread_key_t, DWORD>::value, # 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 +# define SWIFT_THREAD_KEY_CREATE _stdlib_thread_key_create +# define SWIFT_THREAD_GETSPECIFIC _stdlib_thread_getspecific +# define SWIFT_THREAD_SETSPECIFIC _stdlib_thread_setspecific # else // Otherwise use the pthread API. # include diff --git a/stdlib/public/stubs/ThreadLocalStorage.cpp b/stdlib/public/stubs/ThreadLocalStorage.cpp index 4e855cb593b72..5e7b9d1cef78c 100644 --- a/stdlib/public/stubs/ThreadLocalStorage.cpp +++ b/stdlib/public/stubs/ThreadLocalStorage.cpp @@ -25,26 +25,6 @@ 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 @@ -74,6 +54,42 @@ _stdlib_thread_key_create(__swift_thread_key_t * _Nonnull key, #endif +#if defined(__wasi__) +#include +using __swift_thread_key_destructor = void (*)(void *); + +struct _stdlib_tls_element_t { + const void *value; + __swift_thread_key_destructor destructor; +}; + +using _stdlib_tls_map_t = std::map<__swift_thread_key_t, _stdlib_tls_element_t>; +static void *_stdlib_tls_map; + +static inline int _stdlib_thread_key_create(__swift_thread_key_t *key, + __swift_thread_key_destructor destructor) { + if (!_stdlib_tls_map) + _stdlib_tls_map = new _stdlib_tls_map_t(); + auto &map = reinterpret_cast<_stdlib_tls_map_t &>(_stdlib_tls_map); + *key = map.size(); + _stdlib_tls_element_t element = { nullptr, destructor }; + map.insert(std::make_pair(*key, element)); + return 0; +} + +static inline void *_stdlib_thread_getspecific(__swift_thread_key_t key) { + auto &map = reinterpret_cast<_stdlib_tls_map_t &>(_stdlib_tls_map); + return const_cast(map[key].value); +} + +static inline int _stdlib_thread_setspecific(__swift_thread_key_t key, const void *value) { + auto &map = reinterpret_cast<_stdlib_tls_map_t &>(_stdlib_tls_map); + map[key].value = value; + return 0; +} + +#endif + #if SWIFT_TLS_HAS_RESERVED_PTHREAD_SPECIFIC SWIFT_RUNTIME_STDLIB_INTERNAL From e41107b1e36c4798159700aa0f6bed0ab4bbd863 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Fri, 14 Feb 2020 11:09:41 +0000 Subject: [PATCH 090/838] [WASM] cast as raw pointer and make call_once as reentrant --- include/swift/Basic/Lazy.h | 2 +- stdlib/public/stubs/ThreadLocalStorage.cpp | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/include/swift/Basic/Lazy.h b/include/swift/Basic/Lazy.h index fa260a93df3c6..15df48524585e 100644 --- a/include/swift/Basic/Lazy.h +++ b/include/swift/Basic/Lazy.h @@ -28,8 +28,8 @@ inline void wasi_call_once(int *flag, void *context, void (*func)(void *)) { switch (*flag) { case 0: - func(context); *flag = 1; + func(context); return; case 1: return; diff --git a/stdlib/public/stubs/ThreadLocalStorage.cpp b/stdlib/public/stubs/ThreadLocalStorage.cpp index 5e7b9d1cef78c..1238504a2b6af 100644 --- a/stdlib/public/stubs/ThreadLocalStorage.cpp +++ b/stdlib/public/stubs/ThreadLocalStorage.cpp @@ -70,21 +70,21 @@ static inline int _stdlib_thread_key_create(__swift_thread_key_t *key, __swift_thread_key_destructor destructor) { if (!_stdlib_tls_map) _stdlib_tls_map = new _stdlib_tls_map_t(); - auto &map = reinterpret_cast<_stdlib_tls_map_t &>(_stdlib_tls_map); - *key = map.size(); + auto *map = (_stdlib_tls_map_t *)_stdlib_tls_map; + *key = map->size(); _stdlib_tls_element_t element = { nullptr, destructor }; - map.insert(std::make_pair(*key, element)); + map->insert(std::make_pair(*key, element)); return 0; } static inline void *_stdlib_thread_getspecific(__swift_thread_key_t key) { - auto &map = reinterpret_cast<_stdlib_tls_map_t &>(_stdlib_tls_map); - return const_cast(map[key].value); + auto *map = (_stdlib_tls_map_t *)_stdlib_tls_map; + return const_cast(map->operator[](key).value); } static inline int _stdlib_thread_setspecific(__swift_thread_key_t key, const void *value) { - auto &map = reinterpret_cast<_stdlib_tls_map_t &>(_stdlib_tls_map); - map[key].value = value; + auto *map = (_stdlib_tls_map_t *)_stdlib_tls_map; + map->operator[](key).value = value; return 0; } From 50db842dee30f4c87929788e9df7f68849a78810 Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Sat, 15 Feb 2020 10:17:58 +0000 Subject: [PATCH 091/838] Fix a few tests for WASI (#168) --- test/stdlib/FloatConstants.swift | 2 +- test/stdlib/MathConstants.swift | 2 +- test/stdlib/POSIX.swift | 2 +- test/stdlib/PrintPointer.swift | 4 ++-- test/stdlib/Runtime.swift.gyb | 2 +- test/stdlib/VarArgs.swift | 2 +- test/stdlib/tgmath.swift.gyb | 2 +- test/stdlib/tgmath_optimized.swift | 2 +- 8 files changed, 9 insertions(+), 9 deletions(-) diff --git a/test/stdlib/FloatConstants.swift b/test/stdlib/FloatConstants.swift index 3b29cf076e5b4..e9b491b9e1bf2 100644 --- a/test/stdlib/FloatConstants.swift +++ b/test/stdlib/FloatConstants.swift @@ -2,7 +2,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(WASI) import Glibc #elseif os(Windows) import MSVCRT diff --git a/test/stdlib/MathConstants.swift b/test/stdlib/MathConstants.swift index 8ac352de22e50..85ba2d0bca6b8 100644 --- a/test/stdlib/MathConstants.swift +++ b/test/stdlib/MathConstants.swift @@ -2,7 +2,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(WASI) import Glibc #elseif os(Windows) import MSVCRT diff --git a/test/stdlib/POSIX.swift b/test/stdlib/POSIX.swift index ead9a623554b4..c01589a36f135 100644 --- a/test/stdlib/POSIX.swift +++ b/test/stdlib/POSIX.swift @@ -5,7 +5,7 @@ import StdlibUnittest #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(WASI) import Glibc #else #error("Unsupported platform") diff --git a/test/stdlib/PrintPointer.swift b/test/stdlib/PrintPointer.swift index 5c844d1ed56df..04df219305384 100644 --- a/test/stdlib/PrintPointer.swift +++ b/test/stdlib/PrintPointer.swift @@ -9,12 +9,12 @@ PrintTests.test("Printable") { let lowUP = UnsafeMutablePointer(bitPattern: 0x1)! let fourByteUP = UnsafeMutablePointer(bitPattern: 0xabcd1234 as UInt)! -#if !(arch(i386) || arch(arm)) +#if !(arch(i386) || arch(arm) || arch(wasm32)) let eightByteAddr: UInt = 0xabcddcba12344321 let eightByteUP = UnsafeMutablePointer(bitPattern: eightByteAddr)! #endif -#if arch(i386) || arch(arm) +#if arch(i386) || arch(arm) || arch(wasm32) let expectedLow = "0x00000001" expectPrinted("0xabcd1234", fourByteUP) #else diff --git a/test/stdlib/Runtime.swift.gyb b/test/stdlib/Runtime.swift.gyb index 38db24b6f1994..876e5890bfbff 100644 --- a/test/stdlib/Runtime.swift.gyb +++ b/test/stdlib/Runtime.swift.gyb @@ -12,7 +12,7 @@ import SwiftShims #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(WASI) import Glibc #elseif os(Windows) import MSVCRT diff --git a/test/stdlib/VarArgs.swift b/test/stdlib/VarArgs.swift index d81545865b0c4..94aef60bf1b40 100644 --- a/test/stdlib/VarArgs.swift +++ b/test/stdlib/VarArgs.swift @@ -6,7 +6,7 @@ import Swift #if _runtime(_ObjC) import Darwin import CoreGraphics -#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(WASI) import Glibc typealias CGFloat = Double #elseif os(Windows) diff --git a/test/stdlib/tgmath.swift.gyb b/test/stdlib/tgmath.swift.gyb index c3382face8e38..b1144f706c930 100644 --- a/test/stdlib/tgmath.swift.gyb +++ b/test/stdlib/tgmath.swift.gyb @@ -19,7 +19,7 @@ #if os(macOS) || os(iOS) || os(tvOS) || os(watchOS) import Darwin.C.tgmath -#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(WASI) import Glibc #elseif os(Windows) import MSVCRT diff --git a/test/stdlib/tgmath_optimized.swift b/test/stdlib/tgmath_optimized.swift index 997f11625e4cf..0501aab8cd3ab 100644 --- a/test/stdlib/tgmath_optimized.swift +++ b/test/stdlib/tgmath_optimized.swift @@ -6,7 +6,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(WASI) import Glibc #elseif os(Windows) import MSVCRT From d26949fa6ff0b13d8cab41f7dbd82f64f81b54fa Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Sat, 15 Feb 2020 07:46:14 -0800 Subject: [PATCH 092/838] [WASM] Fix macro syntax to avoid compiler crash --- stdlib/private/StdlibUnittest/InterceptTraps.cpp | 4 ++-- stdlib/public/Platform/glibc.modulemap.gyb | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/stdlib/private/StdlibUnittest/InterceptTraps.cpp b/stdlib/private/StdlibUnittest/InterceptTraps.cpp index e17e6b08a7978..cf81e87255615 100644 --- a/stdlib/private/StdlibUnittest/InterceptTraps.cpp +++ b/stdlib/private/StdlibUnittest/InterceptTraps.cpp @@ -50,8 +50,6 @@ static void CrashCatcher(int Sig) { _exit(0); } -#endif // __wasi__ - #if defined(_WIN32) static LONG WINAPI VectoredCrashHandler(PEXCEPTION_POINTERS ExceptionInfo) { @@ -92,6 +90,8 @@ void installTrapInterceptor() { signal(SIGBUS, CrashCatcher); signal(SIGSYS, CrashCatcher); #endif + +#endif // !defined(__wasi__) } #endif // !defined(__wasi__) \ No newline at end of file diff --git a/stdlib/public/Platform/glibc.modulemap.gyb b/stdlib/public/Platform/glibc.modulemap.gyb index bf3dadda16113..f4cd65e744ec4 100644 --- a/stdlib/public/Platform/glibc.modulemap.gyb +++ b/stdlib/public/Platform/glibc.modulemap.gyb @@ -383,6 +383,7 @@ module SwiftGlibc [system] { export * } % end +% if CMAKE_SDK != "WASI": module pwd { header "${GLIBC_INCLUDE_PATH}/pwd.h" export * @@ -514,7 +515,6 @@ module SwiftGlibc [system] { } % end } -% end % if CMAKE_SDK in ["LINUX", "FREEBSD"]: module sysexits { header "${GLIBC_INCLUDE_PATH}/sysexits.h" @@ -538,7 +538,6 @@ module SwiftGlibc [system] { } % end } -% end } % if CMAKE_SDK != "WASI": From 017bfe06e29fb52edfb3dc1d3f00058e1097474f Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Sat, 15 Feb 2020 09:57:03 -0800 Subject: [PATCH 093/838] [WASM] Avoid to use SwiftPrivateThreadExtras on WASI --- stdlib/private/StdlibUnittest/RaceTest.swift | 33 +++++++++++++++++++ .../StdlibUnittest/StdlibUnittest.swift | 2 ++ 2 files changed, 35 insertions(+) diff --git a/stdlib/private/StdlibUnittest/RaceTest.swift b/stdlib/private/StdlibUnittest/RaceTest.swift index c883b03a8b85b..f940f9676cfa6 100644 --- a/stdlib/private/StdlibUnittest/RaceTest.swift +++ b/stdlib/private/StdlibUnittest/RaceTest.swift @@ -38,7 +38,9 @@ import SwiftPrivate import SwiftPrivateLibcExtras +#if !os(WASI) import SwiftPrivateThreadExtras +#endif #if os(macOS) || os(iOS) import Darwin #elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku) || os(WASI) @@ -335,6 +337,36 @@ public func evaluateObservationsAllEqual(_ observations: [T]) return .pass } +// Notes: WebAssembly/WASI doesn't support multi thread yet +#if os(WASI) +public func runRaceTest( + _: RT.Type, + trials: Int, + timeoutInSeconds: Int? = nil, + threads: Int? = nil +) {} +public func runRaceTest( + _ test: RT.Type, + operations: Int, + timeoutInSeconds: Int? = nil, + threads: Int? = nil +) {} +public func consumeCPU(units amountOfWork: Int) {} +public func runRaceTest( + trials: Int, + timeoutInSeconds: Int? = nil, + threads: Int? = nil, + invoking body: @escaping () -> Void +) {} + +public func runRaceTest( + operations: Int, + timeoutInSeconds: Int? = nil, + threads: Int? = nil, + invoking body: @escaping () -> Void +) {} +#else + struct _RaceTestAggregatedEvaluations : CustomStringConvertible { var passCount: Int = 0 var passInterestingCount = [String: Int]() @@ -756,3 +788,4 @@ public func runRaceTest( timeoutInSeconds: timeoutInSeconds, threads: threads) } +#endif diff --git a/stdlib/private/StdlibUnittest/StdlibUnittest.swift b/stdlib/private/StdlibUnittest/StdlibUnittest.swift index 610071af9ad8e..981d965b93dc6 100644 --- a/stdlib/private/StdlibUnittest/StdlibUnittest.swift +++ b/stdlib/private/StdlibUnittest/StdlibUnittest.swift @@ -12,7 +12,9 @@ import SwiftPrivate +#if !os(WASI) import SwiftPrivateThreadExtras +#endif import SwiftPrivateLibcExtras #if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) From badc3908630007975347719a56b2c619f5897069 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Sun, 16 Feb 2020 03:32:36 +0000 Subject: [PATCH 094/838] [WASM] Rename helper function in lit.cfg --- test/lit.cfg | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/lit.cfg b/test/lit.cfg index 0d784bd154278..26eb5ceadc6c4 100644 --- a/test/lit.cfg +++ b/test/lit.cfg @@ -1330,7 +1330,7 @@ elif run_os == 'wasi': return 'lldb-moduleimport-test' in f.read() import fnmatch - def objc_interop_enabled_filenames(path, filename_pat): + def disabled_filenames(path, filename_pat): matches = [] for root, dirnames, filenames in os.walk(path): for filename in fnmatch.filter(filenames, filename_pat): @@ -1340,7 +1340,7 @@ elif run_os == 'wasi': matches.append(filename) return matches - config.excludes += objc_interop_enabled_filenames(config.test_source_root, "*.swift") + config.excludes += disabled_filenames(config.test_source_root, "*.swift") config.target_swift_autolink_extract = inferSwiftBinary("swift-autolink-extract") From 2e823cf4f231d8866c31af0f639c7809eb06daf8 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Sun, 16 Feb 2020 03:43:26 +0000 Subject: [PATCH 095/838] [WASM] Add WASM object file format case --- test/DebugInfo/modulecache.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/DebugInfo/modulecache.swift b/test/DebugInfo/modulecache.swift index c4eb24e70c66c..4f7c18aac13a4 100644 --- a/test/DebugInfo/modulecache.swift +++ b/test/DebugInfo/modulecache.swift @@ -16,7 +16,7 @@ import ClangModule // RUN: %empty-directory(%t) // RUN: %target-swift-frontend %s -c -g -o %t.o -module-cache-path %t -I %S/Inputs // RUN: llvm-readobj -h %t/*/ClangModule-*.pcm | %FileCheck %s -// CHECK: Format: {{(Mach-O|ELF|COFF)}} +// CHECK: Format: {{(Mach-O|ELF|COFF|WASM)}} // 3. Test that swift-ide-check will not share swiftc's module cache. From 8c70efd4d810fe38afc6d54c2b4f87388cfaf73a Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Sun, 16 Feb 2020 03:43:46 +0000 Subject: [PATCH 096/838] [WASM] Skip interpreter related test cases --- test/lit.cfg | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/lit.cfg b/test/lit.cfg index 26eb5ceadc6c4..27c5dd178bc33 100644 --- a/test/lit.cfg +++ b/test/lit.cfg @@ -1395,6 +1395,8 @@ elif run_os == 'wasi': (config.variant_triple, clang_mcp_opt)) config.target_ld = "ld -L%r" % (make_path(test_resource_dir, config.target_sdk_name)) + # The Swift interpreter is not available when targeting WebAssembly/WASI. + config.available_features.discard('swift_interpreter') else: lit_config.fatal("Don't know how to define target_run and " From 433c52060a42112e85853ee619936465936e30f6 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Sun, 16 Feb 2020 04:10:19 +0000 Subject: [PATCH 097/838] [WASM] Pass sysroot parameter to frontend command --- test/lit.cfg | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/test/lit.cfg b/test/lit.cfg index 27c5dd178bc33..ef7f199f2da00 100644 --- a/test/lit.cfg +++ b/test/lit.cfg @@ -1359,10 +1359,14 @@ elif run_os == 'wasi': "%s -parse-as-library -emit-library -o '\\1'" % (config.target_build_swift)) 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, - config.swift_test_options, config.swift_frontend_test_options)) + config.target_swift_frontend = ' '.join([ + '%s', '-frontend', '-target %s', + '-Xcc --sysroot=%s', '-tools-directory %s', + '%s %s %s %s ', + ]) % (config.swift, config.variant_triple, + config.variant_sdk, tools_directory, + 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' From 6b637b314094e41103d49ce04a9c062e2a95f79b Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Sun, 16 Feb 2020 04:15:48 +0000 Subject: [PATCH 098/838] [WASM] Test autolink-extract for wasm format --- test/AutolinkExtract/import_archive.swift | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/AutolinkExtract/import_archive.swift b/test/AutolinkExtract/import_archive.swift index 5aa0ee41d74ee..1bd6f5323a87a 100644 --- a/test/AutolinkExtract/import_archive.swift +++ b/test/AutolinkExtract/import_archive.swift @@ -12,4 +12,7 @@ // CHECK-coff-DAG: -lswiftCore // CHECK-coff-DAG: -lempty +// CHECK-wasm-DAG: -lswiftCore +// CHECK-wasm-DAG: -lempty + import empty From 1614c73b050e3f154d68c06336b2df918402b66f Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Sun, 16 Feb 2020 04:37:07 +0000 Subject: [PATCH 099/838] [WASM] Support WASI platform on tgmath.swift.gyb --- test/stdlib/tgmath.swift.gyb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/stdlib/tgmath.swift.gyb b/test/stdlib/tgmath.swift.gyb index c3382face8e38..b1144f706c930 100644 --- a/test/stdlib/tgmath.swift.gyb +++ b/test/stdlib/tgmath.swift.gyb @@ -19,7 +19,7 @@ #if os(macOS) || os(iOS) || os(tvOS) || os(watchOS) import Darwin.C.tgmath -#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(WASI) import Glibc #elseif os(Windows) import MSVCRT From b8d328afbd51cea6ff07b6ae171c82004c48fe69 Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Mon, 17 Feb 2020 09:28:40 +0000 Subject: [PATCH 100/838] Split CI scripts for swiftwasm-linux-builder (#173) This allows installing build dependencies without actually building anything. --- utils/webassembly/ci-linux.sh | 46 ++---------------- .../webassembly/linux/install-dependencies.sh | 48 +++++++++++++++++++ 2 files changed, 52 insertions(+), 42 deletions(-) create mode 100755 utils/webassembly/linux/install-dependencies.sh diff --git a/utils/webassembly/ci-linux.sh b/utils/webassembly/ci-linux.sh index 0d194e43914f9..1f68dd2cd13b2 100755 --- a/utils/webassembly/ci-linux.sh +++ b/utils/webassembly/ci-linux.sh @@ -2,51 +2,13 @@ set -ex -sudo apt update -sudo apt install \ - 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 \ - libblocksruntime-dev libcurl4-openssl-dev \ - systemtap-sdt-dev tzdata rsync wget llvm - SOURCE_PATH="$( cd "$(dirname $0)/../../.." && pwd )" SWIFT_PATH=$SOURCE_PATH/swift -BUILD_SCRIPT=$SWIFT_PATH/utils/webassembly/build-linux.sh -cd $SWIFT_PATH - -./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 Jx --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.zip "https://github.com/swiftwasm/wasi-sdk/releases/download/0.2.0-swiftwasm/dist-ubuntu-latest.tgz.zip" -unzip dist-wasi-sdk.tgz.zip -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 - -# 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 +UTILS_PATH=$SWIFT_PATH/utils/webassembly +BUILD_SCRIPT=$UTILS_PATH/build-linux.sh +DEPENDENCIES_SCRIPT=$UTILS_PATH/linux/install-dependencies.sh -wget -O icu.tar.xz "https://github.com/swiftwasm/icu4c-wasi/releases/download/20190421.3/icu4c-wasi.tar.xz" -tar xf icu.tar.xz +$DEPENDENCIES_SCRIPT $BUILD_SCRIPT --release --debug-swift-stdlib --verbose # Run test but ignore failure temporarily diff --git a/utils/webassembly/linux/install-dependencies.sh b/utils/webassembly/linux/install-dependencies.sh new file mode 100755 index 0000000000000..a3b78e34b890a --- /dev/null +++ b/utils/webassembly/linux/install-dependencies.sh @@ -0,0 +1,48 @@ +#/bin/bash + +set -ex + +sudo apt update +sudo apt install -y \ + 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 \ + libblocksruntime-dev libcurl4-openssl-dev \ + systemtap-sdt-dev tzdata rsync wget llvm zip unzip + +SOURCE_PATH="$( cd "$(dirname $0)/../../../.." && pwd )" +SWIFT_PATH=$SOURCE_PATH/swift +cd $SWIFT_PATH + +./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 Jx --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.zip "https://github.com/swiftwasm/wasi-sdk/releases/download/0.2.0-swiftwasm/dist-ubuntu-latest.tgz.zip" +unzip dist-wasi-sdk.tgz.zip -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 + +# 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 From 9ce40f207cde93c1a65f6138cafb3df602fa43b4 Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Mon, 17 Feb 2020 09:29:09 +0000 Subject: [PATCH 101/838] Avoid installing deprecated Python 2 from Homebrew (#160) `brew install python@2` now fails for me locally due to Homebrew/homebrew-core#49796, not sure how it still runs fine on CI, probably due to caches not being cleaned up as frequently. I hope relying on system-installed Python should work fine. --- utils/webassembly/ci-mac.sh | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/utils/webassembly/ci-mac.sh b/utils/webassembly/ci-mac.sh index c9caa6b29787e..c59f8351a6e8e 100755 --- a/utils/webassembly/ci-mac.sh +++ b/utils/webassembly/ci-mac.sh @@ -2,10 +2,8 @@ set -ex -brew install cmake ninja llvm python@2 - -# Install six for python3 migration -pip2 install six +brew uninstall python@2 || true +brew install cmake ninja llvm SOURCE_PATH="$( cd "$(dirname $0)/../../.." && pwd )" SWIFT_PATH=$SOURCE_PATH/swift From 290e05c94bdcdbb8d9cbc9430e0b9bdcfcf213b7 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Sun, 16 Feb 2020 20:17:58 +0900 Subject: [PATCH 102/838] [WASM] Implement thin-to-thick semantic for wasm Add indirect function call test case for WebAssembly and implement IRGen for thin-to-thick thunk emission --- include/swift/Demangling/DemangleNodes.def | 1 + lib/Demangling/Context.cpp | 2 + lib/Demangling/Demangler.cpp | 5 +- lib/Demangling/NodePrinter.cpp | 9 ++++ lib/Demangling/OldDemangler.cpp | 9 ++++ lib/Demangling/OldRemangler.cpp | 5 ++ lib/Demangling/Remangler.cpp | 5 ++ lib/IRGen/GenFunc.cpp | 56 ++++++++++++++++++++++ lib/IRGen/GenFunc.h | 4 ++ lib/IRGen/IRGenMangler.cpp | 15 ++++++ lib/IRGen/IRGenMangler.h | 1 + lib/IRGen/IRGenSIL.cpp | 53 ++++++++++++++++++++ test/IRGen/indirect_func_call_wasm.sil | 33 +++++++++++++ 13 files changed, 197 insertions(+), 1 deletion(-) create mode 100644 test/IRGen/indirect_func_call_wasm.sil diff --git a/include/swift/Demangling/DemangleNodes.def b/include/swift/Demangling/DemangleNodes.def index 69b7d151203d4..52acba9cf6c04 100644 --- a/include/swift/Demangling/DemangleNodes.def +++ b/include/swift/Demangling/DemangleNodes.def @@ -200,6 +200,7 @@ CONTEXT_NODE(Structure) CONTEXT_NODE(Subscript) NODE(Suffix) NODE(ThinFunctionType) +NODE(ThinToThickForwarder) NODE(Tuple) NODE(TupleElement) NODE(TupleElementName) diff --git a/lib/Demangling/Context.cpp b/lib/Demangling/Context.cpp index 7c2e986b3985f..23f771ae22af4 100644 --- a/lib/Demangling/Context.cpp +++ b/lib/Demangling/Context.cpp @@ -88,6 +88,7 @@ bool Context::isThunkSymbol(llvm::StringRef MangledName) { MangledName = stripSuffix(MangledName); // First do a quick check if (MangledName.endswith("TA") || // partial application forwarder + MangledName.endswith("Tu")|| // thin-to-thick forwarder MangledName.endswith("Ta") || // ObjC partial application forwarder MangledName.endswith("To") || // swift-as-ObjC thunk MangledName.endswith("TO") || // ObjC-as-swift thunk @@ -107,6 +108,7 @@ bool Context::isThunkSymbol(llvm::StringRef MangledName) { case Node::Kind::NonObjCAttribute: case Node::Kind::PartialApplyObjCForwarder: case Node::Kind::PartialApplyForwarder: + case Node::Kind::ThinToThickForwarder: case Node::Kind::ReabstractionThunkHelper: case Node::Kind::ReabstractionThunk: case Node::Kind::ProtocolWitness: diff --git a/lib/Demangling/Demangler.cpp b/lib/Demangling/Demangler.cpp index 90a2304df0064..236da80a73f5d 100644 --- a/lib/Demangling/Demangler.cpp +++ b/lib/Demangling/Demangler.cpp @@ -113,6 +113,7 @@ bool swift::Demangle::isFunctionAttr(Node::Kind kind) { case Node::Kind::DirectMethodReferenceAttribute: case Node::Kind::VTableAttribute: case Node::Kind::PartialApplyForwarder: + case Node::Kind::ThinToThickForwarder: case Node::Kind::PartialApplyObjCForwarder: case Node::Kind::OutlinedVariable: case Node::Kind::OutlinedBridgedMethod: @@ -549,7 +550,8 @@ NodePointer Demangler::demangleSymbol(StringRef MangledName, while (NodePointer FuncAttr = popNode(isFunctionAttr)) { Parent->addChild(FuncAttr, *this); if (FuncAttr->getKind() == Node::Kind::PartialApplyForwarder || - FuncAttr->getKind() == Node::Kind::PartialApplyObjCForwarder) + FuncAttr->getKind() == Node::Kind::PartialApplyObjCForwarder || + FuncAttr->getKind() == Node::Kind::ThinToThickForwarder) Parent = FuncAttr; } for (Node *Nd : NodeStack) { @@ -2146,6 +2148,7 @@ NodePointer Demangler::demangleThunkOrSpecialization() { case 'd': return createNode(Node::Kind::DirectMethodReferenceAttribute); case 'a': return createNode(Node::Kind::PartialApplyObjCForwarder); case 'A': return createNode(Node::Kind::PartialApplyForwarder); + case 'u': return createNode(Node::Kind::ThinToThickForwarder); case 'm': return createNode(Node::Kind::MergedFunction); case 'X': return createNode(Node::Kind::DynamicallyReplaceableFunctionVar); case 'x': return createNode(Node::Kind::DynamicallyReplaceableFunctionKey); diff --git a/lib/Demangling/NodePrinter.cpp b/lib/Demangling/NodePrinter.cpp index 94a5247a7063c..c38b2312e7ec9 100644 --- a/lib/Demangling/NodePrinter.cpp +++ b/lib/Demangling/NodePrinter.cpp @@ -466,6 +466,7 @@ class NodePrinter { case Node::Kind::Subscript: case Node::Kind::Suffix: case Node::Kind::ThinFunctionType: + case Node::Kind::ThinToThickForwarder: case Node::Kind::TupleElement: case Node::Kind::TypeMangling: case Node::Kind::TypeMetadata: @@ -1207,6 +1208,14 @@ NodePointer NodePrinter::print(NodePointer Node, bool asPrefixContext) { Printer << "@convention(thin) "; printFunctionType(nullptr, Node); return nullptr; + case Node::Kind::ThinToThickForwarder: + Printer << "thin-to-thick forwarder"; + + if (Node->hasChildren()) { + Printer << " for "; + printChildren(Node); + } + return nullptr; case Node::Kind::FunctionType: case Node::Kind::UncurriedFunctionType: printFunctionType(nullptr, Node); diff --git a/lib/Demangling/OldDemangler.cpp b/lib/Demangling/OldDemangler.cpp index ebe2bec4e5823..a505241ccec8d 100644 --- a/lib/Demangling/OldDemangler.cpp +++ b/lib/Demangling/OldDemangler.cpp @@ -364,6 +364,15 @@ class OldDemangler { DEMANGLE_CHILD_OR_RETURN(forwarder, Global); return forwarder; } + + // thin-to-thick thunks. + if (Mangled.nextIf("Pu")) { + Node::Kind kind = Node::Kind::ThinToThickForwarder; + auto forwarder = Factory.createNode(kind); + if (Mangled.nextIf("__T")) + DEMANGLE_CHILD_OR_RETURN(forwarder, Global); + return forwarder; + } // Top-level types, for various consumers. if (Mangled.nextIf('t')) { diff --git a/lib/Demangling/OldRemangler.cpp b/lib/Demangling/OldRemangler.cpp index 26ed34e4b6abd..8df02b939bbe6 100644 --- a/lib/Demangling/OldRemangler.cpp +++ b/lib/Demangling/OldRemangler.cpp @@ -574,6 +574,11 @@ void Remangler::mangleProtocolSelfConformanceDescriptor(Node *node) { mangleProtocol(node->begin()[0]); } +void Remangler::mangleThinToThickForwarder(Node *node) { + Buffer << "Pu__T"; + mangleSingleChildNode(node); // global +} + void Remangler::manglePartialApplyForwarder(Node *node) { Buffer << "PA__T"; mangleSingleChildNode(node); // global diff --git a/lib/Demangling/Remangler.cpp b/lib/Demangling/Remangler.cpp index 18c880eb43e50..e6434f93dcaef 100644 --- a/lib/Demangling/Remangler.cpp +++ b/lib/Demangling/Remangler.cpp @@ -1721,6 +1721,11 @@ void Remangler::mangleOwningMutableAddressor(Node *node) { mangleAbstractStorage(node->getFirstChild(), "aO"); } +void Remangler::mangleThinToThickForwarder(Node *node) { + mangleChildNodesReversed(node); + Buffer << "Tu"; +} + void Remangler::manglePartialApplyForwarder(Node *node) { mangleChildNodesReversed(node); Buffer << "TA"; diff --git a/lib/IRGen/GenFunc.cpp b/lib/IRGen/GenFunc.cpp index c2b9f730bddf0..3839d986a4a54 100644 --- a/lib/IRGen/GenFunc.cpp +++ b/lib/IRGen/GenFunc.cpp @@ -681,6 +681,62 @@ static unsigned findSinglePartiallyAppliedParameterIndexIgnoringEmptyTypes( return firstNonEmpty; } + +llvm::Function *irgen::getThinToThickForwarder(IRGenModule &IGM, + const Optional &staticFnPtr, + const CanSILFunctionType origType) { + auto origSig = IGM.getSignature(origType); + llvm::FunctionType *origFnTy = origSig.getType(); + auto origTy = origSig.getType()->getPointerTo(); + + llvm::SmallVector thunkParams; + + for (unsigned i = 0; i < origFnTy->getNumParams(); ++i) + thunkParams.push_back(origFnTy->getParamType(i)); + + thunkParams.push_back(IGM.RefCountedPtrTy); + + auto thunkType = llvm::FunctionType::get(origFnTy->getReturnType(), + thunkParams, + /*vararg*/ false); + + StringRef FnName; + if (staticFnPtr) + FnName = staticFnPtr->getPointer()->getName(); + + IRGenMangler Mangler; + std::string thunkName = Mangler.mangleThinToThickForwarder(FnName); + + + // FIXME: Maybe cache the thunk by function and closure types?. + llvm::Function *fwd = + llvm::Function::Create(thunkType, llvm::Function::InternalLinkage, + llvm::StringRef(thunkName), &IGM.Module); + + fwd->setAttributes(origSig.getAttributes()); + fwd->addAttribute(llvm::AttributeList::FirstArgIndex + origFnTy->getNumParams(), llvm::Attribute::SwiftSelf); + IRGenFunction IGF(IGM, fwd); + if (IGM.DebugInfo) + IGM.DebugInfo->emitArtificialFunction(IGF, fwd); + auto args = IGF.collectParameters(); + auto rawFnPtr = args.takeLast(); + + // It comes out of the context as an i8*. Cast to the function type. + rawFnPtr = IGF.Builder.CreateBitCast(rawFnPtr, origTy); + + auto fnPtr = FunctionPointer(rawFnPtr, origSig); + + auto result = IGF.Builder.CreateCall(fnPtr, args.claimAll()); + + // Return the result, if we have one. + if (result->getType()->isVoidTy()) + IGF.Builder.CreateRetVoid(); + else + IGF.Builder.CreateRet(result); + return fwd; +} + + /// Emit the forwarding stub function for a partial application. /// /// If 'layout' is null, there is a single captured value of diff --git a/lib/IRGen/GenFunc.h b/lib/IRGen/GenFunc.h index 04bc2e4732182..620b5ebeb54c0 100644 --- a/lib/IRGen/GenFunc.h +++ b/lib/IRGen/GenFunc.h @@ -54,6 +54,10 @@ namespace irgen { CanSILFunctionType origType, CanSILFunctionType substType, CanSILFunctionType outType, Explosion &out, bool isOutlined); + + llvm::Function *getThinToThickForwarder(IRGenModule &IGM, + const Optional &staticFnPtr, + const CanSILFunctionType origType); } // end namespace irgen } // end namespace swift diff --git a/lib/IRGen/IRGenMangler.cpp b/lib/IRGen/IRGenMangler.cpp index 54e3934a807ef..c8157ac956fed 100644 --- a/lib/IRGen/IRGenMangler.cpp +++ b/lib/IRGen/IRGenMangler.cpp @@ -78,6 +78,21 @@ std::string IRGenMangler::manglePartialApplyForwarder(StringRef FuncName) { return finalize(); } +std::string IRGenMangler::mangleThinToThickForwarder(StringRef FuncName) { + if (FuncName.empty()) { + beginMangling(); + } else { + if (FuncName.startswith(MANGLING_PREFIX_STR)) { + Buffer << FuncName; + } else { + beginMangling(); + appendIdentifier(FuncName); + } + } + appendOperator("Tu"); + return finalize(); +} + SymbolicMangling IRGenMangler::withSymbolicReferences(IRGenModule &IGM, llvm::function_ref body) { diff --git a/lib/IRGen/IRGenMangler.h b/lib/IRGen/IRGenMangler.h index aa4d644d0095b..77198ae596dcb 100644 --- a/lib/IRGen/IRGenMangler.h +++ b/lib/IRGen/IRGenMangler.h @@ -492,6 +492,7 @@ class IRGenMangler : public Mangle::ASTMangler { } std::string manglePartialApplyForwarder(StringRef FuncName); + std::string mangleThinToThickForwarder(StringRef FuncName); std::string mangleTypeForForeignMetadataUniquing(Type type) { return mangleTypeWithoutPrefix(type); diff --git a/lib/IRGen/IRGenSIL.cpp b/lib/IRGen/IRGenSIL.cpp index 82ffd7dc77f2b..154b84b28a45c 100644 --- a/lib/IRGen/IRGenSIL.cpp +++ b/lib/IRGen/IRGenSIL.cpp @@ -4691,8 +4691,61 @@ void IRGenSILFunction::visit##KIND##Inst(swift::KIND##Inst *i) { \ #include "swift/AST/ReferenceStorage.def" #undef NOOP_CONVERSION + +static FunctionPointer +getLoweredFunctionPointer(IRGenSILFunction &IGF, SILValue v) { + LoweredValue &lv = IGF.getLoweredValue(v); + auto fnType = v->getType().castTo(); + + switch (lv.kind) { + case LoweredValue::Kind::ContainedAddress: + case LoweredValue::Kind::StackAddress: + case LoweredValue::Kind::DynamicallyEnforcedAddress: + case LoweredValue::Kind::OwnedAddress: + case LoweredValue::Kind::EmptyExplosion: + case LoweredValue::Kind::CoroutineState: + case LoweredValue::Kind::ObjCMethod: + llvm_unreachable("not a valid function"); + + case LoweredValue::Kind::FunctionPointer: { + return lv.getFunctionPointer(); + } + case LoweredValue::Kind::SingletonExplosion: { + llvm::Value *fnPtr = lv.getKnownSingletonExplosion(); + return FunctionPointer::forExplosionValue(IGF, fnPtr, fnType); + } + case LoweredValue::Kind::ExplosionVector: { + Explosion ex = lv.getExplosion(IGF, v->getType()); + llvm::Value *fnPtr = ex.claimNext(); + auto fn = FunctionPointer::forExplosionValue(IGF, fnPtr, fnType); + return fn; + } + } + llvm_unreachable("bad kind"); +} + void IRGenSILFunction::visitThinToThickFunctionInst( swift::ThinToThickFunctionInst *i) { + + if (IGM.TargetInfo.OutputObjectFormat == llvm::Triple::Wasm) { + auto fn = getLoweredFunctionPointer(*this, i->getCallee()); + auto fnTy = i->getCallee()->getType().castTo(); + Optional staticFn; + if (fn.isConstant()) staticFn = fn; + auto thunkFn = getThinToThickForwarder(IGM, staticFn, fnTy); + Explosion from = getLoweredExplosion(i->getOperand()); + Explosion to; + auto fnPtr = Builder.CreateBitCast(thunkFn, IGM.Int8PtrTy); + to.add(fnPtr); + llvm::Value *ctx = from.claimNext(); + if (fnTy->isNoEscape()) + ctx = Builder.CreateBitCast(ctx, IGM.OpaquePtrTy); + else + ctx = Builder.CreateBitCast(ctx, IGM.RefCountedPtrTy); + to.add(ctx); + setLoweredExplosion(i, to); + return; + } // Take the incoming function pointer and add a null context pointer to it. Explosion from = getLoweredExplosion(i->getOperand()); Explosion to; diff --git a/test/IRGen/indirect_func_call_wasm.sil b/test/IRGen/indirect_func_call_wasm.sil new file mode 100644 index 0000000000000..f64cae7fcaa12 --- /dev/null +++ b/test/IRGen/indirect_func_call_wasm.sil @@ -0,0 +1,33 @@ +// RUN: %target-swift-frontend %s -emit-ir | %FileCheck %s + +// REQUIRES: CPU=wasm32 + +// CHECK-LABEL: define swiftcc void @closureToConvert() +// CHECK: entry: +// CHECK: ret void +// CHECK: } +sil @closureToConvert : $@convention(thin) () -> () { + %99 = tuple () + return %99 : $() +} +// CHECK-LABEL: define swiftcc void @testConvertFunc() +// CHECK: entry: +// CHECK: call swiftcc void @"$s{{.*}}Tu"(%swift.refcounted* swiftself bitcast (void ()* @closureToConvert to %swift.refcounted*)) +// CHECK: ret void +// CHECK-LABEL: } + +sil @testConvertFunc : $@convention(thin) () -> () { +bb0: + %f = function_ref @closureToConvert : $@convention(thin) () -> () + %cf = convert_function %f : $@convention(thin) () -> () to $@noescape @convention(thin) () -> () + %thick = thin_to_thick_function %cf : $@noescape @convention(thin) () -> () to $@noescape @callee_owned () -> () + %apply = apply %thick() : $@noescape @callee_owned () -> () + %99 = tuple () + return %99 : $() +} + +// CHECK-LABEL: define internal void @"$s{{.*}}Tu"(%swift.refcounted* swiftself %0) +// CHECK: entry: +// CHECK: %1 = bitcast %swift.refcounted* %0 to void ()* +// CHECK: call swiftcc void %1() +// CHECK: ret void From 52484b73d486a6b14a9fa4acba283cea457359b6 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Wed, 19 Feb 2020 20:26:25 +0900 Subject: [PATCH 103/838] [WASM] Fix to build StdlibUnittest for WASM (#198) --- stdlib/private/StdlibUnittest/StdlibUnittest.swift | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/stdlib/private/StdlibUnittest/StdlibUnittest.swift b/stdlib/private/StdlibUnittest/StdlibUnittest.swift index 981d965b93dc6..0c8bfdf905a0e 100644 --- a/stdlib/private/StdlibUnittest/StdlibUnittest.swift +++ b/stdlib/private/StdlibUnittest/StdlibUnittest.swift @@ -811,8 +811,10 @@ var _testSuiteNameToIndex: [String : Int] = [:] let _stdlibUnittestStreamPrefix = "__STDLIB_UNITTEST__" let _crashedPrefix = "CRASHED:" +#if !os(WASI) @_silgen_name("installTrapInterceptor") func _installTrapInterceptor() +#endif #if _runtime(_ObjC) @objc protocol _StdlibUnittestNSException { @@ -823,7 +825,9 @@ func _installTrapInterceptor() // Avoid serializing references to objc_setUncaughtExceptionHandler in SIL. @inline(never) func _childProcess() { +#if !os(WASI) _installTrapInterceptor() +#endif #if _runtime(_ObjC) objc_setUncaughtExceptionHandler { From e9e7efe43e98b9d2637bcf77fe0af404d71eecea Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Fri, 21 Feb 2020 20:21:36 +0900 Subject: [PATCH 104/838] [WASM] Update ICU (#210) * [WASM] Update ICU The main change is here https://github.com/swiftwasm/icu4c-wasi/commit/171b2a5b03c33d52f9e794843955e8820b8a09b2 * [WASM] Update to build ICU with latest wasi-sdk * Update ICU to archive with wasi-sdk bin --- utils/webassembly/ci-mac.sh | 2 +- utils/webassembly/linux/install-dependencies.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/utils/webassembly/ci-mac.sh b/utils/webassembly/ci-mac.sh index c59f8351a6e8e..55ab0c72ca47a 100755 --- a/utils/webassembly/ci-mac.sh +++ b/utils/webassembly/ci-mac.sh @@ -36,7 +36,7 @@ ln -s ../include wasi-sdk/share/wasi-sysroot/usr/include # 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" +wget -O icu.tar.xz "https://github.com/swiftwasm/icu4c-wasi/releases/download/0.3.0/icu4c-wasi.tar.xz" tar xf icu.tar.xz $BUILD_SCRIPT --release --debug-swift-stdlib --verbose diff --git a/utils/webassembly/linux/install-dependencies.sh b/utils/webassembly/linux/install-dependencies.sh index a3b78e34b890a..5427c5a5d2e83 100755 --- a/utils/webassembly/linux/install-dependencies.sh +++ b/utils/webassembly/linux/install-dependencies.sh @@ -44,5 +44,5 @@ mv $WASI_SDK_FULL_NAME ./wasi-sdk # 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" +wget -O icu.tar.xz "https://github.com/swiftwasm/icu4c-wasi/releases/download/0.3.0/icu4c-wasi.tar.xz" tar xf icu.tar.xz From 2fc79d5f4d8f6034fadb9a87594d9cb60b39afef Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Sun, 23 Feb 2020 09:34:35 +0000 Subject: [PATCH 105/838] Split dependency installation script for macos --- utils/webassembly/ci-mac.sh | 37 ++---------------- .../webassembly/macos/install-dependencies.sh | 39 +++++++++++++++++++ 2 files changed, 43 insertions(+), 33 deletions(-) create mode 100755 utils/webassembly/macos/install-dependencies.sh diff --git a/utils/webassembly/ci-mac.sh b/utils/webassembly/ci-mac.sh index c59f8351a6e8e..8d1cdf9f73920 100755 --- a/utils/webassembly/ci-mac.sh +++ b/utils/webassembly/ci-mac.sh @@ -2,42 +2,13 @@ set -ex -brew uninstall python@2 || true -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 - -./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 Jx --strip-components 1 -sudo ln -sf /opt/wasmtime/* /usr/local/bin - -cd $SOURCE_PATH - -wget -O dist-wasi-sdk.tgz.zip "https://github.com/swiftwasm/wasi-sdk/releases/download/0.2.0-swiftwasm/dist-macos-latest.tgz.zip" -unzip dist-wasi-sdk.tgz.zip -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 - -# 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 +UTILS_PATH=$SWIFT_PATH/utils/webassembly +BUILD_SCRIPT=$UTILS_PATH/build-macos.sh +DEPENDENCIES_SCRIPT=$UTILS_PATH/macos/install-dependencies.sh -wget -O icu.tar.xz "https://github.com/swiftwasm/icu4c-wasi/releases/download/20190421.3/icu4c-wasi.tar.xz" -tar xf icu.tar.xz +$DEPENDENCIES_SCRIPT $BUILD_SCRIPT --release --debug-swift-stdlib --verbose # Run test but ignore failure temporarily diff --git a/utils/webassembly/macos/install-dependencies.sh b/utils/webassembly/macos/install-dependencies.sh new file mode 100755 index 0000000000000..a1e4728cf086e --- /dev/null +++ b/utils/webassembly/macos/install-dependencies.sh @@ -0,0 +1,39 @@ +#/bin/bash + +set -ex + +brew uninstall python@2 || true +brew install cmake ninja llvm + +SOURCE_PATH="$( cd "$(dirname $0)/../../.." && pwd )" +SWIFT_PATH=$SOURCE_PATH/swift +cd $SWIFT_PATH + +./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 Jx --strip-components 1 +sudo ln -sf /opt/wasmtime/* /usr/local/bin + +cd $SOURCE_PATH + +wget -O dist-wasi-sdk.tgz.zip "https://github.com/swiftwasm/wasi-sdk/releases/download/0.2.0-swiftwasm/dist-macos-latest.tgz.zip" +unzip dist-wasi-sdk.tgz.zip -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 + +# 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 From c13cd82056b5cef2e21fc809abdbed38fd96ff4d Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Sun, 23 Feb 2020 09:57:20 +0000 Subject: [PATCH 106/838] Cache build products --- .github/workflows/main.yml | 12 +++++++-- utils/webassembly/ci-linux.sh | 15 ----------- utils/webassembly/ci-mac.sh | 15 ----------- utils/webassembly/ci.sh | 26 +++++++++++++++++++ .../webassembly/linux/install-dependencies.sh | 7 +++++ .../webassembly/macos/install-dependencies.sh | 7 +++++ 6 files changed, 50 insertions(+), 32 deletions(-) delete mode 100755 utils/webassembly/ci-linux.sh delete mode 100755 utils/webassembly/ci-mac.sh create mode 100755 utils/webassembly/ci.sh diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 80eb85fecf0e6..f8d28f3bfaaed 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -17,8 +17,12 @@ jobs: - uses: actions/checkout@v1 with: path: swift + - uses: actions/cache@v1 + with: + path: ../build-cache + key: ${{ runner.os }}-sccache-v1 }} - name: Build Linux installable archive - run: ./utils/webassembly/ci-linux.sh + run: ./utils/webassembly/ci.sh - name: Upload Linux installable archive uses: actions/upload-artifact@v1 with: @@ -40,8 +44,12 @@ jobs: - uses: actions/checkout@v1 with: path: swift + - uses: actions/cache@v1 + with: + path: ../build-cache + key: ${{ runner.os }}-sccache-v1 }} - name: Build macOS installable archive - run: ./utils/webassembly/ci-mac.sh + run: ./utils/webassembly/ci.sh - name: Upload macOS installable archive uses: actions/upload-artifact@v1 with: diff --git a/utils/webassembly/ci-linux.sh b/utils/webassembly/ci-linux.sh deleted file mode 100755 index 1f68dd2cd13b2..0000000000000 --- a/utils/webassembly/ci-linux.sh +++ /dev/null @@ -1,15 +0,0 @@ -#/bin/bash - -set -ex - -SOURCE_PATH="$( cd "$(dirname $0)/../../.." && pwd )" -SWIFT_PATH=$SOURCE_PATH/swift -UTILS_PATH=$SWIFT_PATH/utils/webassembly -BUILD_SCRIPT=$UTILS_PATH/build-linux.sh -DEPENDENCIES_SCRIPT=$UTILS_PATH/linux/install-dependencies.sh - -$DEPENDENCIES_SCRIPT - -$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 deleted file mode 100755 index 8d1cdf9f73920..0000000000000 --- a/utils/webassembly/ci-mac.sh +++ /dev/null @@ -1,15 +0,0 @@ -#/bin/bash - -set -ex - -SOURCE_PATH="$( cd "$(dirname $0)/../../.." && pwd )" -SWIFT_PATH=$SOURCE_PATH/swift -UTILS_PATH=$SWIFT_PATH/utils/webassembly -BUILD_SCRIPT=$UTILS_PATH/build-macos.sh -DEPENDENCIES_SCRIPT=$UTILS_PATH/macos/install-dependencies.sh - -$DEPENDENCIES_SCRIPT - -$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.sh b/utils/webassembly/ci.sh new file mode 100755 index 0000000000000..7b573f3755a0c --- /dev/null +++ b/utils/webassembly/ci.sh @@ -0,0 +1,26 @@ +#/bin/bash + +set -ex + +SOURCE_PATH="$( cd "$(dirname $0)/../../.." && pwd )" +SWIFT_PATH=$SOURCE_PATH/swift +UTILS_PATH=$SWIFT_PATH/utils/webassembly +if [[ "$(uname)" == "Linux" ]]; then + BUILD_SCRIPT=$UTILS_PATH/build-linux.sh + DEPENDENCIES_SCRIPT=$UTILS_PATH/linux/install-dependencies.sh +else + BUILD_SCRIPT=$UTILS_PATH/build-macos.sh + DEPENDENCIES_SCRIPT=$UTILS_PATH/macos/install-dependencies.sh +fi + +$DEPENDENCIES_SCRIPT + +export SCCACHE_CACHE_SIZE="50G" +export SCCACHE_DIR="$SOURCE_PATH/cache" + +CACHE_FLAGS="--cmake-c-launcher $(which sccache) --cmake-cxx-launcher $(which sccache)" +FLAGS="--release --debug-swift-stdlib $CACHE_FLAGS --verbose" + +$BUILD_SCRIPT $FLAGS +# Run test but ignore failure temporarily +$BUILD_SCRIPT $FLAGS -t || true diff --git a/utils/webassembly/linux/install-dependencies.sh b/utils/webassembly/linux/install-dependencies.sh index a3b78e34b890a..a8bc2f3fb19d4 100755 --- a/utils/webassembly/linux/install-dependencies.sh +++ b/utils/webassembly/linux/install-dependencies.sh @@ -46,3 +46,10 @@ 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 + +# Install sccache + +sudo mkdir /opt/sccache && cd /opt/sccache +wget -O - "https://github.com/mozilla/sccache/releases/download/0.2.12/sccache-0.2.12-x86_64-unknown-linux-musl.tar.gz" | \ + sudo tar xz --strip-components 1 +sudo ln -sf /opt/sccache/sccache /usr/local/bin diff --git a/utils/webassembly/macos/install-dependencies.sh b/utils/webassembly/macos/install-dependencies.sh index a1e4728cf086e..643410148f8d7 100755 --- a/utils/webassembly/macos/install-dependencies.sh +++ b/utils/webassembly/macos/install-dependencies.sh @@ -37,3 +37,10 @@ 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 + +# Install sccache + +sudo mkdir /opt/sccache && cd /opt/sccache +wget -O - "https://github.com/mozilla/sccache/releases/download/0.2.12/sccache-0.2.12-x86_64-apple-darwin.tar.gz" | \ + sudo tar xz --strip-components 1 +sudo ln -sf /opt/sccache/sccache /usr/local/bin From ea63437105dc3893cdcd59936453c7052d537195 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Sun, 23 Feb 2020 10:40:38 +0000 Subject: [PATCH 107/838] Fix syntax error for yaml --- .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 f8d28f3bfaaed..f15506b6bb311 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -20,7 +20,7 @@ jobs: - uses: actions/cache@v1 with: path: ../build-cache - key: ${{ runner.os }}-sccache-v1 }} + key: ${{ runner.os }}-sccache-v1 - name: Build Linux installable archive run: ./utils/webassembly/ci.sh - name: Upload Linux installable archive @@ -47,7 +47,7 @@ jobs: - uses: actions/cache@v1 with: path: ../build-cache - key: ${{ runner.os }}-sccache-v1 }} + key: ${{ runner.os }}-sccache-v1 - name: Build macOS installable archive run: ./utils/webassembly/ci.sh - name: Upload macOS installable archive From 6fbaa61f40aedf71f6db9700f6f602a8226f24a8 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Sun, 23 Feb 2020 10:44:11 +0000 Subject: [PATCH 108/838] Fix SOURCE_PATH --- utils/webassembly/macos/install-dependencies.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/webassembly/macos/install-dependencies.sh b/utils/webassembly/macos/install-dependencies.sh index 254f5f696b4e6..3e6ac6eee94c2 100755 --- a/utils/webassembly/macos/install-dependencies.sh +++ b/utils/webassembly/macos/install-dependencies.sh @@ -5,7 +5,7 @@ set -ex brew uninstall python@2 || true brew install cmake ninja llvm -SOURCE_PATH="$( cd "$(dirname $0)/../../.." && pwd )" +SOURCE_PATH="$( cd "$(dirname $0)/../../../../" && pwd )" SWIFT_PATH=$SOURCE_PATH/swift cd $SWIFT_PATH From 17f1bbc9948e85d6b4a8ff6f895f498a0035bf17 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Sun, 23 Feb 2020 10:57:04 +0000 Subject: [PATCH 109/838] fix build script path --- utils/webassembly/ci.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/webassembly/ci.sh b/utils/webassembly/ci.sh index 7b573f3755a0c..5efc689e7a8c4 100755 --- a/utils/webassembly/ci.sh +++ b/utils/webassembly/ci.sh @@ -9,7 +9,7 @@ if [[ "$(uname)" == "Linux" ]]; then BUILD_SCRIPT=$UTILS_PATH/build-linux.sh DEPENDENCIES_SCRIPT=$UTILS_PATH/linux/install-dependencies.sh else - BUILD_SCRIPT=$UTILS_PATH/build-macos.sh + BUILD_SCRIPT=$UTILS_PATH/build-mac.sh DEPENDENCIES_SCRIPT=$UTILS_PATH/macos/install-dependencies.sh fi From 11000ab6f23c9883586df2cb58460a930a80c200 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Mon, 24 Feb 2020 00:02:15 +0000 Subject: [PATCH 110/838] Skip to run test case on Linux --- utils/webassembly/ci.sh | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/utils/webassembly/ci.sh b/utils/webassembly/ci.sh index 5efc689e7a8c4..6cc28b36247fe 100755 --- a/utils/webassembly/ci.sh +++ b/utils/webassembly/ci.sh @@ -22,5 +22,10 @@ CACHE_FLAGS="--cmake-c-launcher $(which sccache) --cmake-cxx-launcher $(which sc FLAGS="--release --debug-swift-stdlib $CACHE_FLAGS --verbose" $BUILD_SCRIPT $FLAGS -# Run test but ignore failure temporarily -$BUILD_SCRIPT $FLAGS -t || true + +if [[ "$(uname)" == "Linux" ]]; then + echo "Skip running test suites for Linux" +else + # Run test but ignore failure temporarily + $BUILD_SCRIPT $FLAGS -t || true +fi From c401792988eb7d2728ec574a2d8d5c127870c037 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Mon, 24 Feb 2020 00:08:34 +0000 Subject: [PATCH 111/838] Fix build cache dir --- utils/webassembly/ci.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/webassembly/ci.sh b/utils/webassembly/ci.sh index 6cc28b36247fe..ca7193df2d82c 100755 --- a/utils/webassembly/ci.sh +++ b/utils/webassembly/ci.sh @@ -16,7 +16,7 @@ fi $DEPENDENCIES_SCRIPT export SCCACHE_CACHE_SIZE="50G" -export SCCACHE_DIR="$SOURCE_PATH/cache" +export SCCACHE_DIR="$SOURCE_PATH/build-cache" CACHE_FLAGS="--cmake-c-launcher $(which sccache) --cmake-cxx-launcher $(which sccache)" FLAGS="--release --debug-swift-stdlib $CACHE_FLAGS --verbose" From 0eb28207d355e83493f2115581c877fb900bc512 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Mon, 24 Feb 2020 08:00:38 +0000 Subject: [PATCH 112/838] Disable test artifact uploading --- .github/workflows/main.yml | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index f15506b6bb311..7111d2f13f2c8 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -28,13 +28,13 @@ jobs: 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 +# - 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 From 4d1a571c026165e1323dc31d468a56e192eb8ea0 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Tue, 25 Feb 2020 07:30:31 +0000 Subject: [PATCH 113/838] Trigger CI From d498cb2cd03c7d285abd4ab057c196c76402a823 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Tue, 25 Feb 2020 16:18:11 +0000 Subject: [PATCH 114/838] [WASM] Disable unsupported test sutie on WASM This change disables these kind of test suite and add ifdef condition for wasm32 and WASI 1. Dynamic linking 2. Dynamic Replacement 3. Address Sanitizer of clang 4. Expecting crash of process --- test/AutolinkExtract/empty.swift | 1 + test/AutolinkExtract/empty_archive.swift | 1 + test/AutolinkExtract/import.swift | 3 +++ test/ClangImporter/clang_builtins.swift | 2 +- test/IRGen/builtin_math.swift | 2 +- test/IRGen/sanitize_coverage.swift | 1 + .../dynamicReplacement_property_observer.swift | 1 + test/Interpreter/dynamic_replacement.swift | 1 + .../dynamic_replacement_chaining.swift | 1 + ...amic_replacement_without_previous_calls.swift | 1 + test/lit.cfg | 16 ++++++---------- test/stdlib/Character.swift | 2 ++ test/stdlib/CharacterTraps.swift | 1 + test/stdlib/DictionaryTraps.swift | 1 + test/stdlib/FloatingPointIR.swift | 3 +++ test/stdlib/IntervalTraps.swift | 1 + test/stdlib/KeyPath.swift | 2 ++ test/stdlib/NumericParsing.swift.gyb | 2 ++ test/stdlib/Optional.swift | 4 ++++ test/stdlib/OptionalTraps.swift | 1 + test/stdlib/POSIX.swift | 3 ++- test/stdlib/PrintFloat.swift.gyb | 2 +- test/stdlib/RangeTraps.swift | 1 + test/stdlib/Repeat.swift | 2 ++ test/stdlib/Runtime.swift.gyb | 3 +++ test/stdlib/SetTraps.swift | 1 + test/stdlib/StaticString.swift | 4 ++++ test/stdlib/StringTraps.swift | 1 + test/stdlib/UnsafePointer.swift.gyb | 8 ++++++-- test/stdlib/UnsafeRawBufferPointer.swift | 6 ++++++ test/stdlib/UnsafeRawPointer.swift | 2 ++ 31 files changed, 64 insertions(+), 16 deletions(-) diff --git a/test/AutolinkExtract/empty.swift b/test/AutolinkExtract/empty.swift index b9ae0068c07d6..c5b612927092a 100644 --- a/test/AutolinkExtract/empty.swift +++ b/test/AutolinkExtract/empty.swift @@ -5,3 +5,4 @@ // CHECK-elf: -lswiftCore // CHECK-coff: -lswiftCore +// CHECK-wasm: -lswiftCore diff --git a/test/AutolinkExtract/empty_archive.swift b/test/AutolinkExtract/empty_archive.swift index ee66d6762610c..a94f40ad68c1b 100644 --- a/test/AutolinkExtract/empty_archive.swift +++ b/test/AutolinkExtract/empty_archive.swift @@ -7,3 +7,4 @@ // CHECK-elf: -lswiftCore // CHECK-coff: -lswiftCore +// CHECK-wasm: -lswiftCore diff --git a/test/AutolinkExtract/import.swift b/test/AutolinkExtract/import.swift index ae8b929c7e132..0d3a09a8eecf0 100644 --- a/test/AutolinkExtract/import.swift +++ b/test/AutolinkExtract/import.swift @@ -11,4 +11,7 @@ // CHECK-coff-DAG: -lswiftCore // CHECK-coff-DAG: -lempty +// CHECK-wasm-DAG: -lswiftCore +// CHECK-wasm-DAG: -lempty + import empty diff --git a/test/ClangImporter/clang_builtins.swift b/test/ClangImporter/clang_builtins.swift index a9c6ae820fa50..720bb637cb909 100644 --- a/test/ClangImporter/clang_builtins.swift +++ b/test/ClangImporter/clang_builtins.swift @@ -2,7 +2,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(WASI) import Glibc #elseif os(Windows) import MSVCRT diff --git a/test/IRGen/builtin_math.swift b/test/IRGen/builtin_math.swift index 93a7938f69389..d20ebe4a21cc3 100644 --- a/test/IRGen/builtin_math.swift +++ b/test/IRGen/builtin_math.swift @@ -2,7 +2,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(WASI) import Glibc #elseif os(Windows) import MSVCRT diff --git a/test/IRGen/sanitize_coverage.swift b/test/IRGen/sanitize_coverage.swift index 556cc3af1b848..4401f09471acb 100644 --- a/test/IRGen/sanitize_coverage.swift +++ b/test/IRGen/sanitize_coverage.swift @@ -7,6 +7,7 @@ // RUN: %target-swift-frontend -emit-ir -sanitize=address -sanitize-coverage=edge,indirect-calls %s | %FileCheck %s -check-prefix=SANCOV -check-prefix=SANCOV_INDIRECT_CALLS // RUN: %target-swift-frontend -emit-ir -sanitize=address -sanitize-coverage=edge,8bit-counters %s | %FileCheck %s -check-prefix=SANCOV -check-prefix=SANCOV_8BIT_COUNTERS // RUN: %target-swift-frontend -emit-ir -sanitize=fuzzer %s | %FileCheck %s -check-prefix=SANCOV -check-prefix=SANCOV_TRACE_CMP +// UNSUPPORTED: wasm #if os(macOS) || os(iOS) || os(tvOS) || os(watchOS) import Darwin diff --git a/test/Interpreter/dynamicReplacement_property_observer.swift b/test/Interpreter/dynamicReplacement_property_observer.swift index a09b0b0845c1c..1b8d27252fd80 100644 --- a/test/Interpreter/dynamicReplacement_property_observer.swift +++ b/test/Interpreter/dynamicReplacement_property_observer.swift @@ -8,6 +8,7 @@ // REQUIRES: executable_test // UNSUPPORTED: swift_test_mode_optimize_none_with_implicit_dynamic +// UNSUPPORTED: wasm @_private(sourceFile: "dynamic_replacement_property_observer_orig.swift") import TestDidWillSet diff --git a/test/Interpreter/dynamic_replacement.swift b/test/Interpreter/dynamic_replacement.swift index 193c2a9d096cd..f68f240339322 100644 --- a/test/Interpreter/dynamic_replacement.swift +++ b/test/Interpreter/dynamic_replacement.swift @@ -48,6 +48,7 @@ // REQUIRES: executable_test +// UNSUPPORTED: wasm import Module1 diff --git a/test/Interpreter/dynamic_replacement_chaining.swift b/test/Interpreter/dynamic_replacement_chaining.swift index fc92c0309b6aa..d95779f0e4e37 100644 --- a/test/Interpreter/dynamic_replacement_chaining.swift +++ b/test/Interpreter/dynamic_replacement_chaining.swift @@ -20,6 +20,7 @@ // This test flips the chaining flag. // UNSUPPORTED: swift_test_mode_optimize_none_with_implicit_dynamic +// UNSUPPORTED: wasm import A diff --git a/test/Interpreter/dynamic_replacement_without_previous_calls.swift b/test/Interpreter/dynamic_replacement_without_previous_calls.swift index 092103d9d4bb7..760847a951fec 100644 --- a/test/Interpreter/dynamic_replacement_without_previous_calls.swift +++ b/test/Interpreter/dynamic_replacement_without_previous_calls.swift @@ -6,6 +6,7 @@ // RUN: %target-run %t/main %t/%target-library-name(Module1) %t/%target-library-name(Module2) // REQUIRES: executable_test +// UNSUPPORTED: wasm import Module1 diff --git a/test/lit.cfg b/test/lit.cfg index ef7f199f2da00..c44613644959a 100644 --- a/test/lit.cfg +++ b/test/lit.cfg @@ -1320,8 +1320,6 @@ elif run_os == 'wasi': # 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() @@ -1329,18 +1327,16 @@ elif run_os == 'wasi': with open(path) as f: return 'lldb-moduleimport-test' in f.read() - import fnmatch - def disabled_filenames(path, filename_pat): + def disabled_filenames(path): matches = [] for root, dirnames, filenames in os.walk(path): - for filename in fnmatch.filter(filenames, filename_pat): + for filename in filenames: filepath = os.path.join(root, filename) - if not use_objc_interop(filepath): continue - if not lldb_related_test(filepath): continue - matches.append(filename) + if use_objc_interop(filepath) or lldb_related_test(filepath): + matches.append(os.path.basename(filepath)) return matches - config.excludes += disabled_filenames(config.test_source_root, "*.swift") + config.excludes += disabled_filenames(config.test_source_root) config.target_swift_autolink_extract = inferSwiftBinary("swift-autolink-extract") @@ -1369,7 +1365,7 @@ elif run_os == 'wasi': 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' + config.target_run = 'wasmtime --' if 'interpret' in lit_config.params: use_interpreter_for_simple_runs() config.target_sil_opt = ( diff --git a/test/stdlib/Character.swift b/test/stdlib/Character.swift index 19ecd73256f0e..bd632593e2e2c 100644 --- a/test/stdlib/Character.swift +++ b/test/stdlib/Character.swift @@ -369,6 +369,7 @@ UnicodeScalarTests.test("UInt8(ascii: UnicodeScalar)") { } } +#if !os(WASI) UnicodeScalarTests.test("UInt8(ascii: UnicodeScalar)/non-ASCII should trap") .skip(.custom( { _isFastAssertConfiguration() }, @@ -378,6 +379,7 @@ UnicodeScalarTests.test("UInt8(ascii: UnicodeScalar)/non-ASCII should trap") expectCrashLater() _blackHole(UInt8(ascii: us)) } +#endif UnicodeScalarTests.test("UInt32(_: UnicodeScalar),UInt64(_: UnicodeScalar)") { for us in baseScalars { diff --git a/test/stdlib/CharacterTraps.swift b/test/stdlib/CharacterTraps.swift index 6b981290300a2..d7182ee2d9819 100644 --- a/test/stdlib/CharacterTraps.swift +++ b/test/stdlib/CharacterTraps.swift @@ -7,6 +7,7 @@ // RUN: %target-run %t/a.out_Debug // RUN: %target-run %t/a.out_Release // REQUIRES: executable_test +// UNSUPPORTED: OS=wasi import StdlibUnittest diff --git a/test/stdlib/DictionaryTraps.swift b/test/stdlib/DictionaryTraps.swift index 021c01bd524ad..07772984d6071 100644 --- a/test/stdlib/DictionaryTraps.swift +++ b/test/stdlib/DictionaryTraps.swift @@ -7,6 +7,7 @@ // RUN: %target-run %t/a.out_Debug // RUN: %target-run %t/a.out_Release // REQUIRES: executable_test +// UNSUPPORTED: OS=wasi import StdlibUnittest diff --git a/test/stdlib/FloatingPointIR.swift b/test/stdlib/FloatingPointIR.swift index 9fadab623f92e..73ae3eaeea4e9 100644 --- a/test/stdlib/FloatingPointIR.swift +++ b/test/stdlib/FloatingPointIR.swift @@ -49,3 +49,6 @@ func testConstantFoldFloatLiterals() { // s390x: call swiftcc void @"$s15FloatingPointIR13acceptFloat32yySfF{{.*}}"(float 1.000000e+00) // s390x: call swiftcc void @"$s15FloatingPointIR13acceptFloat64yySdF{{.*}}"(double 1.000000e+00) + +// wasm32: call swiftcc void @"$s15FloatingPointIR13acceptFloat32yySfF{{.*}}"(float 1.000000e+00) +// wasm32: call swiftcc void @"$s15FloatingPointIR13acceptFloat64yySdF{{.*}}"(double 1.000000e+00) diff --git a/test/stdlib/IntervalTraps.swift b/test/stdlib/IntervalTraps.swift index 1b982ae39d69e..c6105d03ddba5 100644 --- a/test/stdlib/IntervalTraps.swift +++ b/test/stdlib/IntervalTraps.swift @@ -18,6 +18,7 @@ // RUN: %target-run %t/a.out_Debug // RUN: %target-run %t/a.out_Release // REQUIRES: executable_test +// UNSUPPORTED: OS=wasi import StdlibUnittest diff --git a/test/stdlib/KeyPath.swift b/test/stdlib/KeyPath.swift index 1b4a1270a8a38..9c279a5590f6d 100644 --- a/test/stdlib/KeyPath.swift +++ b/test/stdlib/KeyPath.swift @@ -422,6 +422,7 @@ keyPath.test("optional force-unwrapping") { expectTrue(value.questionableCanary === newCanary) } +#if !os(WASI) keyPath.test("optional force-unwrapping trap") { let origin_x = \TestOptional.origin!.x var value = TestOptional(origin: nil) @@ -429,6 +430,7 @@ keyPath.test("optional force-unwrapping trap") { expectCrashLater() _ = value[keyPath: origin_x] } +#endif struct TestOptional2 { var optional: TestOptional? diff --git a/test/stdlib/NumericParsing.swift.gyb b/test/stdlib/NumericParsing.swift.gyb index fe0239efdb32d..04e17cba47175 100644 --- a/test/stdlib/NumericParsing.swift.gyb +++ b/test/stdlib/NumericParsing.swift.gyb @@ -107,6 +107,7 @@ tests.test("${Self}/success") { % end } +#if !os(WASI) tests.test("${Self}/radixTooLow") { ${Self}("0", radix: 2) expectCrashLater() @@ -119,6 +120,7 @@ tests.test("${Self}/radixTooHigh") { expectCrashLater() let y = ${Self}("0", radix: maxRadix + 1) } +#endif % end diff --git a/test/stdlib/Optional.swift b/test/stdlib/Optional.swift index b8c42fff21c5c..df3e59a040435 100644 --- a/test/stdlib/Optional.swift +++ b/test/stdlib/Optional.swift @@ -338,6 +338,7 @@ OptionalTests.test("Casting Optional") { expectTrue(anyToAnyIsOptional(Optional<(String, String)>.none, Bool.self)) } +#if !os(WASI) OptionalTests.test("Casting Optional Traps") { let nx: C? = nil expectCrash { _blackHole(anyToAny(nx, Int.self)) } @@ -346,6 +347,7 @@ OptionalTests.test("Casting Optional Any Traps") { let nx: X? = X() expectCrash { _blackHole(anyToAny(nx as Any, Optional.self)) } } +#endif class TestNoString {} class TestString : CustomStringConvertible, CustomDebugStringConvertible { @@ -407,6 +409,7 @@ OptionalTests.test("unsafelyUnwrapped") { expectEqual(3, nonEmpty.unsafelyUnwrapped) } +#if !os(WASI) OptionalTests.test("unsafelyUnwrapped nil") .xfail(.custom( { !_isDebugAssertConfiguration() }, @@ -416,5 +419,6 @@ OptionalTests.test("unsafelyUnwrapped nil") expectCrashLater() _blackHole(empty.unsafelyUnwrapped) } +#endif runAllTests() diff --git a/test/stdlib/OptionalTraps.swift b/test/stdlib/OptionalTraps.swift index 0539f2dc6ca33..9c489e9a021e5 100644 --- a/test/stdlib/OptionalTraps.swift +++ b/test/stdlib/OptionalTraps.swift @@ -10,6 +10,7 @@ // RUN: %target-run %t/Assert_Release // RUN: %target-run %t/Assert_Unchecked // REQUIRES: executable_test +// UNSUPPORTED: OS=wasi import StdlibUnittest diff --git a/test/stdlib/POSIX.swift b/test/stdlib/POSIX.swift index c01589a36f135..09bccb15f57b6 100644 --- a/test/stdlib/POSIX.swift +++ b/test/stdlib/POSIX.swift @@ -1,11 +1,12 @@ // 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) import Darwin -#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku) || os(WASI) +#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku) import Glibc #else #error("Unsupported platform") diff --git a/test/stdlib/PrintFloat.swift.gyb b/test/stdlib/PrintFloat.swift.gyb index ce798ba623021..a7bf257fcc09a 100644 --- a/test/stdlib/PrintFloat.swift.gyb +++ b/test/stdlib/PrintFloat.swift.gyb @@ -12,7 +12,7 @@ import StdlibUnittest #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(WASI) import Glibc #elseif os(Windows) import MSVCRT diff --git a/test/stdlib/RangeTraps.swift b/test/stdlib/RangeTraps.swift index bf9e5e06e6291..9f02d983e7620 100644 --- a/test/stdlib/RangeTraps.swift +++ b/test/stdlib/RangeTraps.swift @@ -18,6 +18,7 @@ // RUN: %target-run %t/a.out_Debug // RUN: %target-run %t/a.out_Release // REQUIRES: executable_test +// UNSUPPORTED: OS=wasi import StdlibUnittest diff --git a/test/stdlib/Repeat.swift b/test/stdlib/Repeat.swift index a86a412069470..9c58a4baee913 100644 --- a/test/stdlib/Repeat.swift +++ b/test/stdlib/Repeat.swift @@ -23,11 +23,13 @@ RepeatTests.test("associated-types") { indicesType: CountableRange.self) } +#if !os(WASI) RepeatTests.test("out-of-bounds") { let sequence = repeatElement(0, count: 1) expectCrashLater() _ = sequence[sequence.count] } +#endif runAllTests() diff --git a/test/stdlib/Runtime.swift.gyb b/test/stdlib/Runtime.swift.gyb index 876e5890bfbff..7f659e3a9dcfc 100644 --- a/test/stdlib/Runtime.swift.gyb +++ b/test/stdlib/Runtime.swift.gyb @@ -591,6 +591,8 @@ Runtime.test("Struct layout with reference storage types") { print(malkovich) } +// WebAssembly/WASI doesn't support dynamic symbol lookup +#if !os(WASI) Runtime.test("SwiftError layout constants for LLDB") { let offsetof_SwiftError_typeMetadata = pointerToSwiftCoreSymbol(name: "_swift_lldb_offsetof_SwiftError_typeMetadata")! let sizeof_SwiftError = pointerToSwiftCoreSymbol(name: "_swift_lldb_sizeof_SwiftError")! @@ -610,6 +612,7 @@ Runtime.test("SwiftError layout constants for LLDB") { _UnimplementedError() #endif } +#endif var Reflection = TestSuite("Reflection") diff --git a/test/stdlib/SetTraps.swift b/test/stdlib/SetTraps.swift index d9662285ec80e..00ccf5925afee 100644 --- a/test/stdlib/SetTraps.swift +++ b/test/stdlib/SetTraps.swift @@ -7,6 +7,7 @@ // RUN: %target-run %t/a.out_Debug // RUN: %target-run %t/a.out_Release // REQUIRES: executable_test +// UNSUPPORTED: OS=wasi import StdlibUnittest diff --git a/test/stdlib/StaticString.swift b/test/stdlib/StaticString.swift index cbf2b9f4d1f93..30aba3e413d0b 100644 --- a/test/stdlib/StaticString.swift +++ b/test/stdlib/StaticString.swift @@ -69,6 +69,7 @@ StaticStringTestSuite.test("PointerRepresentation/NonASCII") { expectDebugPrinted("\"абв\"", str) } +#if !os(WASI) StaticStringTestSuite.test("PointerRepresentation/unicodeScalar") .skip(.custom( { _isFastAssertConfiguration() }, @@ -81,6 +82,7 @@ StaticStringTestSuite.test("PointerRepresentation/unicodeScalar") expectCrashLater() strOpaque.unicodeScalar } +#endif StaticStringTestSuite.test("UnicodeScalarRepresentation/ASCII") { // The type checker does not call the UnicodeScalar initializer even if @@ -119,6 +121,7 @@ StaticStringTestSuite.test("UnicodeScalarRepresentation/NonASCII") { expectDebugPrinted("\"Ы\"", str) } +#if !os(WASI) StaticStringTestSuite.test("UnicodeScalarRepresentation/utf8Start") .skip(.custom( { _isFastAssertConfiguration() }, @@ -144,6 +147,7 @@ StaticStringTestSuite.test("UnicodeScalarRepresentation/utf8CodeUnitCount") expectCrashLater() strOpaque.utf8CodeUnitCount } +#endif runAllTests() diff --git a/test/stdlib/StringTraps.swift b/test/stdlib/StringTraps.swift index 729eea0353a3a..5d43833f3dff0 100644 --- a/test/stdlib/StringTraps.swift +++ b/test/stdlib/StringTraps.swift @@ -7,6 +7,7 @@ // RUN: %target-run %t/a.out_Debug // RUN: %target-run %t/a.out_Release // REQUIRES: executable_test +// UNSUPPORTED: OS=wasi import StdlibUnittest diff --git a/test/stdlib/UnsafePointer.swift.gyb b/test/stdlib/UnsafePointer.swift.gyb index 568f34fb22732..59f11426326b2 100644 --- a/test/stdlib/UnsafePointer.swift.gyb +++ b/test/stdlib/UnsafePointer.swift.gyb @@ -304,6 +304,7 @@ func checkPtr( } } +#if !os(WASI) UnsafeMutablePointerTestSuite.test("moveAssign:from:") { let check = checkPtr(UnsafeMutablePointer.moveAssign, true) check(Check.Disjoint) @@ -322,6 +323,7 @@ UnsafeMutablePointerTestSuite.test("moveAssign:from:.Right") { check(Check.RightOverlap) } } +#endif UnsafeMutablePointerTestSuite.test("assign:from:") { let check = checkPtr(UnsafeMutablePointer.assign, true) @@ -337,6 +339,7 @@ UnsafeMutablePointerTestSuite.test("moveInitialize:from:") { check(Check.RightOverlap) } +#if !os(WASI) UnsafeMutablePointerTestSuite.test("initialize:from:") { let check = checkPtr(UnsafeMutablePointer.initialize(from:count:), false) check(Check.Disjoint) @@ -355,6 +358,7 @@ UnsafeMutablePointerTestSuite.test("initialize:from:.Right") { check(Check.RightOverlap) } } +#endif UnsafeMutablePointerTestSuite.test("initialize:from:/immutable") { var ptr = UnsafeMutablePointer.allocate(capacity: 3) @@ -400,7 +404,7 @@ ${SelfName}TestSuite.test("customMirror") { let ptr = ${SelfType}(bitPattern: reallyBigInt)! let mirror = ptr.customMirror expectEqual(1, mirror.children.count) -#if arch(i386) || arch(arm) +#if arch(i386) || arch(arm) || arch(wasm32) expectEqual("18446744071562067968", String(describing: mirror.children.first!.1)) #elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x) expectEqual("9223372036854775808", String(describing: mirror.children.first!.1)) @@ -415,7 +419,7 @@ ${SelfName}TestSuite.test("customPlaygroundQuickLook") { let reallyBigInt: UInt = UInt(Int.max) + 1 let ptr = ${SelfType}(bitPattern: reallyBigInt)! if case let .text(desc) = ptr.customPlaygroundQuickLook { -#if arch(i386) || arch(arm) +#if arch(i386) || arch(arm) || arch(wasm32) expectEqual("${SelfName}(0xFFFFFFFF80000000)", desc) #elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x) expectEqual("${SelfName}(0x8000000000000000)", desc) diff --git a/test/stdlib/UnsafeRawBufferPointer.swift b/test/stdlib/UnsafeRawBufferPointer.swift index cdc1bde447b69..72cc72b216243 100644 --- a/test/stdlib/UnsafeRawBufferPointer.swift +++ b/test/stdlib/UnsafeRawBufferPointer.swift @@ -128,6 +128,7 @@ UnsafeRawBufferPointerTestSuite.test("initFromArray") { expectEqual(array2, array1) } +#if !os(WASI) UnsafeRawBufferPointerTestSuite.test("initializeMemory(as:from:).underflow") { let buffer = UnsafeMutableRawBufferPointer.allocate(byteCount: 30, alignment: MemoryLayout.alignment) defer { buffer.deallocate() } @@ -159,6 +160,7 @@ UnsafeRawBufferPointerTestSuite.test("initializeMemory(as:from:).overflow") { expected.withUnsafeBytes { expectEqualSequence($0,buffer[0...alignment) @@ -172,12 +174,14 @@ UnsafeRawBufferPointerTestSuite.test("initializeMemory(as:from:).exact") { expectEqualSequence([5, 4, 3],bound) } +#if !os(WASI) UnsafeRawBufferPointerTestSuite.test("initializeMemory(as:from:).invalidNilPtr") { let buffer = UnsafeMutableRawBufferPointer(start: nil, count: 0) let source: [Int64] = [5, 4, 3, 2, 1] expectCrashLater() _ = buffer.initializeMemory(as: Int64.self, from: source) } +#endif UnsafeRawBufferPointerTestSuite.test("initializeMemory(as:from:).validNilPtr") { let buffer = UnsafeMutableRawBufferPointer(start: nil, count: 0) @@ -280,6 +284,7 @@ UnsafeRawBufferPointerTestSuite.test("inBounds") { expectEqualSequence(firstHalf, secondHalf) } +#if !os(WASI) UnsafeRawBufferPointerTestSuite.test("subscript.get.underflow") { let buffer = UnsafeMutableRawBufferPointer.allocate(byteCount: 2, alignment: MemoryLayout.alignment) defer { buffer.deallocate() } @@ -512,6 +517,7 @@ UnsafeRawBufferPointerTestSuite.test("copy.sequence.overflow") } } } +#endif UnsafeRawBufferPointerTestSuite.test("copy.overlap") { let bytes = UnsafeMutableRawBufferPointer.allocate(byteCount: 4, alignment: MemoryLayout.alignment) diff --git a/test/stdlib/UnsafeRawPointer.swift b/test/stdlib/UnsafeRawPointer.swift index 6f98ce9a0e561..a71e5595ca14d 100644 --- a/test/stdlib/UnsafeRawPointer.swift +++ b/test/stdlib/UnsafeRawPointer.swift @@ -200,6 +200,7 @@ func checkPtr( } } +#if !os(WASI) UnsafeMutableRawPointerExtraTestSuite.test("initializeMemory:as:from:count:") { let check = checkPtr(UnsafeMutableRawPointer.initializeMemory(as:from:count:)) check(Check.Disjoint) @@ -220,6 +221,7 @@ UnsafeMutableRawPointerExtraTestSuite.test("initializeMemory:as:from:count:.Righ check(Check.RightOverlap) } } +#endif UnsafeMutableRawPointerExtraTestSuite.test("moveInitialize:from:") { let check = From 60d5dbd97ad04a963a3f7a5ede51b21daea66f03 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Tue, 25 Feb 2020 16:33:40 +0000 Subject: [PATCH 115/838] [WASM] Always build static library for WASM --- lib/Driver/Driver.cpp | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/Driver/Driver.cpp b/lib/Driver/Driver.cpp index 7fa6a9d671bd2..711247add3b80 100644 --- a/lib/Driver/Driver.cpp +++ b/lib/Driver/Driver.cpp @@ -1432,13 +1432,18 @@ void Driver::buildOutputInfo(const ToolChain &TC, const DerivedArgList &Args, OI.CompilerOutputType = file_types::TY_Object; break; - case options::OPT_emit_library: - OI.LinkAction = Args.hasArg(options::OPT_static) ? - LinkKind::StaticLibrary : - LinkKind::DynamicLibrary; + case options::OPT_emit_library: { + // WebAssembly only support static library + if (TC.getTriple().isOSBinFormatWasm()) { + OI.LinkAction = LinkKind::StaticLibrary; + } else { + OI.LinkAction = Args.hasArg(options::OPT_static) ? + LinkKind::StaticLibrary : + LinkKind::DynamicLibrary; + } OI.CompilerOutputType = file_types::TY_Object; break; - + } case options::OPT_static: break; From 4c745a88e10e0ee7c9b3129a7c4250d7f295ea9e Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Wed, 26 Feb 2020 01:40:53 +0000 Subject: [PATCH 116/838] [WASM] Avoid to emit GOT for WASM WebAssembly doesn't support dynamic linking, so GOT is not necessary now. LLVM produces virtual GOT when compiler want to use GOT, but it takes overhead on runtime. Discussion: https://forums.swift.org/t/wasm-support/16087/19 --- include/swift/IRGen/Linking.h | 6 +++++- lib/IRGen/GenDecl.cpp | 5 +++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/include/swift/IRGen/Linking.h b/include/swift/IRGen/Linking.h index 041b97e6b02f8..892bfe876ba3a 100644 --- a/include/swift/IRGen/Linking.h +++ b/include/swift/IRGen/Linking.h @@ -1177,8 +1177,12 @@ class ApplyIRLinkage { // 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()) + // And avoid to create GOT because wasm32 doesn't support + // dynamic linking yet + if (Triple.isOSBinFormatWasm()) { + GV->setDSOLocal(true); return; + } if (IRL.Linkage == llvm::GlobalValue::LinkOnceODRLinkage || IRL.Linkage == llvm::GlobalValue::WeakODRLinkage) diff --git a/lib/IRGen/GenDecl.cpp b/lib/IRGen/GenDecl.cpp index 38975526a6555..4fb036dd18ff0 100644 --- a/lib/IRGen/GenDecl.cpp +++ b/lib/IRGen/GenDecl.cpp @@ -2910,6 +2910,11 @@ IRGenModule::getAddrOfLLVMVariableOrGOTEquivalent(LinkEntity entity) { return {gotEquivalent, ConstantReference::Indirect}; }; + // Avoid to create GOT because wasm32 doesn't support + // dynamic linking yet + if (TargetInfo.OutputObjectFormat == llvm::Triple::Wasm) { + return direct(); + } // The integrated REPL incrementally adds new definitions, so always use // indirect references in this mode. if (IRGen.Opts.IntegratedREPL) From e95e714f5ed83dfb0b2eff6a045dceb859c1137b Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Wed, 26 Feb 2020 03:10:40 +0000 Subject: [PATCH 117/838] [WASM] Resolve KeyPath address absolutely instead of relatively --- stdlib/public/core/KeyPath.swift | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/stdlib/public/core/KeyPath.swift b/stdlib/public/core/KeyPath.swift index 414d3e09d290e..b343535c2b963 100644 --- a/stdlib/public/core/KeyPath.swift +++ b/stdlib/public/core/KeyPath.swift @@ -2543,9 +2543,19 @@ internal protocol KeyPathPatternVisitor { internal func _resolveRelativeAddress(_ base: UnsafeRawPointer, _ offset: Int32) -> UnsafeRawPointer { + #if arch(wasm32) + // FIXME: If offset is 0, it means the pointer is null. + // For real relative pointer, it always calculate valid non-null address + // because the base address is always valid. + // But hacked absolute pointer can be null pointer. + // The return type doesn't allow nil, so return given base temporarily. + if offset == 0 { return base } + return UnsafeRawPointer(bitPattern: Int(offset)).unsafelyUnwrapped + #else // Sign-extend the offset to pointer width and add with wrap on overflow. return UnsafeRawPointer(bitPattern: Int(bitPattern: base) &+ Int(offset)) .unsafelyUnwrapped + #endif } internal func _resolveRelativeIndirectableAddress(_ base: UnsafeRawPointer, _ offset: Int32) @@ -3189,7 +3199,7 @@ internal struct InstantiateKeyPathBuffer: KeyPathPatternVisitor { case .pointer: // Resolve the sign-extended relative reference. - var absoluteID: UnsafeRawPointer? = idValueBase + Int(idValue) + var absoluteID: UnsafeRawPointer? = _resolveRelativeAddress(idValueBase, idValue) // If the pointer ID is unresolved, then it needs work to get to // the final value. @@ -3287,7 +3297,7 @@ internal struct InstantiateKeyPathBuffer: KeyPathPatternVisitor { for i in externalArgs.indices { let base = externalArgs.baseAddress.unsafelyUnwrapped + i let offset = base.pointee - let metadataRef = UnsafeRawPointer(base) + Int(offset) + let metadataRef = _resolveRelativeAddress(UnsafeRawPointer(base), offset) let result = _resolveKeyPathGenericArgReference( metadataRef, genericEnvironment: genericEnvironment, From 59cc02c0b0a82ae91eb8beabaab5312012819fe9 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Wed, 26 Feb 2020 03:12:23 +0000 Subject: [PATCH 118/838] [WASM] Always emit extra argument for indexing and generic param This is necessary to match callee and caller signature on WebAssembly --- lib/IRGen/GenKeyPath.cpp | 3 ++- lib/SIL/SILVerifier.cpp | 4 ++-- lib/SILGen/SILGenExpr.cpp | 39 +++++++++++++++++++++++++++++++++++---- 3 files changed, 39 insertions(+), 7 deletions(-) diff --git a/lib/IRGen/GenKeyPath.cpp b/lib/IRGen/GenKeyPath.cpp index 7667185c20b1f..6b021bd1c9f04 100644 --- a/lib/IRGen/GenKeyPath.cpp +++ b/lib/IRGen/GenKeyPath.cpp @@ -222,7 +222,8 @@ getAccessorForComputedComponent(IRGenModule &IGM, componentArgsBuf = params.claimNext(); // Pass the argument pointer down to the underlying function, if it // wants it. - if (hasSubscriptIndices) { + // Always forward extra argument to match callee and caller signature on WebAssembly + if (hasSubscriptIndices || IGM.TargetInfo.OutputObjectFormat == llvm::Triple::Wasm) { forwardedArgs.add(componentArgsBuf); } break; diff --git a/lib/SIL/SILVerifier.cpp b/lib/SIL/SILVerifier.cpp index 276612e871a3a..eca9f83baa818 100644 --- a/lib/SIL/SILVerifier.cpp +++ b/lib/SIL/SILVerifier.cpp @@ -277,7 +277,7 @@ void verifyKeyPathComponent(SILModule &M, SILFunctionTypeRepresentation::Thin, "getter should be a thin function"); - require(substGetterType->getNumParameters() == 1 + hasIndices, + require(substGetterType->getNumParameters() == 1 + (hasIndices || C.LangOpts.Target.isOSBinFormatWasm()), "getter should have one parameter"); auto baseParam = substGetterType->getParameters()[0]; require(baseParam.getConvention() == normalArgConvention, @@ -325,7 +325,7 @@ void verifyKeyPathComponent(SILModule &M, SILFunctionTypeRepresentation::Thin, "setter should be a thin function"); - require(substSetterType->getNumParameters() == 2 + hasIndices, + require(substSetterType->getNumParameters() == 2 + (hasIndices || C.LangOpts.Target.isOSBinFormatWasm()), "setter should have two parameters"); auto newValueParam = substSetterType->getParameters()[0]; diff --git a/lib/SILGen/SILGenExpr.cpp b/lib/SILGen/SILGenExpr.cpp index a7e254f5d797b..37426f0718fb7 100644 --- a/lib/SILGen/SILGenExpr.cpp +++ b/lib/SILGen/SILGenExpr.cpp @@ -2680,6 +2680,7 @@ static SILFunction *getOrCreateKeyPathGetter(SILGenModule &SGM, } } + auto Target = SGM.getASTContext().LangOpts.Target; auto genericSig = genericEnv ? genericEnv->getGenericSignature().getCanonicalSignature() : nullptr; @@ -2688,6 +2689,14 @@ static SILFunction *getOrCreateKeyPathGetter(SILGenModule &SGM, genericEnv = nullptr; } + // Add empty generic type parameter to match function signature on WebAssembly + if (!genericSig && Target.isOSBinFormatWasm()) { + auto param = GenericTypeParamType::get(0, 0, SGM.getASTContext()); + auto sig = GenericSignature::get(param, { }); + genericSig = CanGenericSignature(sig); + genericEnv = sig->getGenericEnvironment(); + } + // Build the signature of the thunk as expected by the keypath runtime. auto signature = [&]() { CanType loweredBaseTy, loweredPropTy; @@ -2703,7 +2712,8 @@ static SILFunction *getOrCreateKeyPathGetter(SILGenModule &SGM, SmallVector params; params.push_back({loweredBaseTy, paramConvention}); auto &C = SGM.getASTContext(); - if (!indexes.empty()) + // Always take indexes parameter to match callee and caller signature on WebAssembly + if (!indexes.empty() || C.LangOpts.Target.isOSBinFormatWasm()) params.push_back({C.getUnsafeRawPointerDecl()->getDeclaredType() ->getCanonicalType(), ParameterConvention::Direct_Unowned}); @@ -2759,7 +2769,8 @@ static SILFunction *getOrCreateKeyPathGetter(SILGenModule &SGM, auto resultArg = entry->createFunctionArgument(resultArgTy); auto baseArg = entry->createFunctionArgument(baseArgTy); SILValue indexPtrArg; - if (!indexes.empty()) { + // Always take indexes parameter to match callee and caller signature on WebAssembly + if (!indexes.empty() || Target.isOSBinFormatWasm()) { auto indexArgTy = signature->getParameters()[1].getSILStorageType( SGM.M, signature); indexPtrArg = entry->createFunctionArgument(indexArgTy); @@ -2821,6 +2832,7 @@ static SILFunction *getOrCreateKeyPathSetter(SILGenModule &SGM, } } + auto Target = SGM.getASTContext().LangOpts.Target; auto genericSig = genericEnv ? genericEnv->getGenericSignature().getCanonicalSignature() : nullptr; @@ -2829,6 +2841,14 @@ static SILFunction *getOrCreateKeyPathSetter(SILGenModule &SGM, genericEnv = nullptr; } + // Add empty generic type parameter to match function signature on WebAssembly + if (!genericSig && Target.isOSBinFormatWasm()) { + auto param = GenericTypeParamType::get(0, 0, SGM.getASTContext()); + auto sig = GenericSignature::get(param, { }); + genericSig = CanGenericSignature(sig); + genericEnv = sig->getGenericEnvironment(); + } + // Build the signature of the thunk as expected by the keypath runtime. auto signature = [&]() { CanType loweredBaseTy, loweredPropTy; @@ -2854,7 +2874,8 @@ static SILFunction *getOrCreateKeyPathSetter(SILGenModule &SGM, ? ParameterConvention::Indirect_Inout : paramConvention}); // indexes - if (!indexes.empty()) + // Always take indexes parameter to match callee and caller signature on WebAssembly + if (!indexes.empty() || C.LangOpts.Target.isOSBinFormatWasm()) params.push_back({C.getUnsafeRawPointerDecl()->getDeclaredType() ->getCanonicalType(), ParameterConvention::Direct_Unowned}); @@ -2910,7 +2931,8 @@ static SILFunction *getOrCreateKeyPathSetter(SILGenModule &SGM, auto baseArg = entry->createFunctionArgument(baseArgTy); SILValue indexPtrArg; - if (!indexes.empty()) { + // Always take indexes parameter to match callee and caller signature on WebAssembly + if (!indexes.empty() || Target.isOSBinFormatWasm()) { auto indexArgTy = signature->getParameters()[2].getSILStorageType( SGM.M, signature); indexPtrArg = entry->createFunctionArgument(indexArgTy); @@ -2992,6 +3014,7 @@ getOrCreateKeyPathEqualsAndHash(SILGenModule &SGM, return; } + auto Target = SGM.getASTContext().LangOpts.Target; auto genericSig = genericEnv ? genericEnv->getGenericSignature().getCanonicalSignature() : nullptr; @@ -3001,6 +3024,14 @@ getOrCreateKeyPathEqualsAndHash(SILGenModule &SGM, genericEnv = nullptr; } + // Add empty generic type parameter to match function signature on WebAssembly + if (!genericSig && Target.isOSBinFormatWasm()) { + auto param = GenericTypeParamType::get(0, 0, SGM.getASTContext()); + auto sig = GenericSignature::get(param, { }); + genericSig = CanGenericSignature(sig); + genericEnv = sig->getGenericEnvironment(); + } + auto &C = SGM.getASTContext(); auto unsafeRawPointerTy = C.getUnsafeRawPointerDecl()->getDeclaredType() ->getCanonicalType(); From b425d02e0076a91143606b8da6bd790509497197 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Wed, 26 Feb 2020 03:27:56 +0000 Subject: [PATCH 119/838] [WASM] Disable KeyPath crash test for WASM --- test/stdlib/KeyPath.swift | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/stdlib/KeyPath.swift b/test/stdlib/KeyPath.swift index 1b4a1270a8a38..9c279a5590f6d 100644 --- a/test/stdlib/KeyPath.swift +++ b/test/stdlib/KeyPath.swift @@ -422,6 +422,7 @@ keyPath.test("optional force-unwrapping") { expectTrue(value.questionableCanary === newCanary) } +#if !os(WASI) keyPath.test("optional force-unwrapping trap") { let origin_x = \TestOptional.origin!.x var value = TestOptional(origin: nil) @@ -429,6 +430,7 @@ keyPath.test("optional force-unwrapping trap") { expectCrashLater() _ = value[keyPath: origin_x] } +#endif struct TestOptional2 { var optional: TestOptional? From a4c2dd71c4148feb232996404412116e25f95e8f Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Wed, 26 Feb 2020 06:12:09 +0000 Subject: [PATCH 120/838] [WASM] Skip thunk generation for throws func --- lib/IRGen/IRGenSIL.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/IRGen/IRGenSIL.cpp b/lib/IRGen/IRGenSIL.cpp index 50086ec069aa7..b458fee7dbc84 100644 --- a/lib/IRGen/IRGenSIL.cpp +++ b/lib/IRGen/IRGenSIL.cpp @@ -4756,10 +4756,9 @@ getLoweredFunctionPointer(IRGenSILFunction &IGF, SILValue v) { void IRGenSILFunction::visitThinToThickFunctionInst( swift::ThinToThickFunctionInst *i) { - - if (IGM.TargetInfo.OutputObjectFormat == llvm::Triple::Wasm) { - auto fn = getLoweredFunctionPointer(*this, i->getCallee()); - auto fnTy = i->getCallee()->getType().castTo(); + auto fn = getLoweredFunctionPointer(*this, i->getCallee()); + auto fnTy = i->getCallee()->getType().castTo(); + if (IGM.TargetInfo.OutputObjectFormat == llvm::Triple::Wasm && !fnTy->hasErrorResult()) { Optional staticFn; if (fn.isConstant()) staticFn = fn; auto thunkFn = getThinToThickForwarder(IGM, staticFn, fnTy); From 60186b6644c26a32f300418236ae9da2bef14aa7 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Wed, 26 Feb 2020 20:11:49 +0900 Subject: [PATCH 121/838] Update stdlib/public/core/KeyPath.swift Co-Authored-By: Max Desiatov --- stdlib/public/core/KeyPath.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/public/core/KeyPath.swift b/stdlib/public/core/KeyPath.swift index b343535c2b963..169aae54c9f03 100644 --- a/stdlib/public/core/KeyPath.swift +++ b/stdlib/public/core/KeyPath.swift @@ -2545,7 +2545,7 @@ internal func _resolveRelativeAddress(_ base: UnsafeRawPointer, _ offset: Int32) -> UnsafeRawPointer { #if arch(wasm32) // FIXME: If offset is 0, it means the pointer is null. - // For real relative pointer, it always calculate valid non-null address + // For real relative pointer, it always calculates valid non-null address // because the base address is always valid. // But hacked absolute pointer can be null pointer. // The return type doesn't allow nil, so return given base temporarily. From e900e8c69094aeae6f3f95be1fe56dcf63968d89 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Wed, 26 Feb 2020 23:01:36 +0000 Subject: [PATCH 122/838] Install latest sccache and install from brew on macOS --- utils/webassembly/linux/install-dependencies.sh | 2 +- utils/webassembly/macos/install-dependencies.sh | 9 +-------- 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/utils/webassembly/linux/install-dependencies.sh b/utils/webassembly/linux/install-dependencies.sh index f1690f462bb4a..784eef14135ea 100755 --- a/utils/webassembly/linux/install-dependencies.sh +++ b/utils/webassembly/linux/install-dependencies.sh @@ -50,6 +50,6 @@ tar xf icu.tar.xz # Install sccache sudo mkdir /opt/sccache && cd /opt/sccache -wget -O - "https://github.com/mozilla/sccache/releases/download/0.2.12/sccache-0.2.12-x86_64-unknown-linux-musl.tar.gz" | \ +wget -O - "https://github.com/mozilla/sccache/releases/download/0.2.13/sccache-0.2.13-x86_64-unknown-linux-musl.tar.gz" | \ sudo tar xz --strip-components 1 sudo ln -sf /opt/sccache/sccache /usr/local/bin diff --git a/utils/webassembly/macos/install-dependencies.sh b/utils/webassembly/macos/install-dependencies.sh index 3e6ac6eee94c2..81fb5e5993f02 100755 --- a/utils/webassembly/macos/install-dependencies.sh +++ b/utils/webassembly/macos/install-dependencies.sh @@ -3,7 +3,7 @@ set -ex brew uninstall python@2 || true -brew install cmake ninja llvm +brew install cmake ninja llvm sccache SOURCE_PATH="$( cd "$(dirname $0)/../../../../" && pwd )" SWIFT_PATH=$SOURCE_PATH/swift @@ -37,10 +37,3 @@ 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/0.3.0/icu4c-wasi.tar.xz" tar xf icu.tar.xz - -# Install sccache - -sudo mkdir /opt/sccache && cd /opt/sccache -wget -O - "https://github.com/mozilla/sccache/releases/download/0.2.12/sccache-0.2.12-x86_64-apple-darwin.tar.gz" | \ - sudo tar xz --strip-components 1 -sudo ln -sf /opt/sccache/sccache /usr/local/bin From 91063c8c7ac2657686daa3715c0d5252facb0135 Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Thu, 27 Feb 2020 10:18:18 +0000 Subject: [PATCH 123/838] Attempt to resolve conflict in AddSwift.cmake --- cmake/modules/AddSwift.cmake | 1920 --------------------- cmake/modules/SwiftSource.cmake | 114 ++ stdlib/cmake/modules/AddSwiftStdlib.cmake | 1806 +++++++++++++++++++ 3 files changed, 1920 insertions(+), 1920 deletions(-) diff --git a/cmake/modules/AddSwift.cmake b/cmake/modules/AddSwift.cmake index 7dba3c02ed2d1..a2e7f263991df 100644 --- a/cmake/modules/AddSwift.cmake +++ b/cmake/modules/AddSwift.cmake @@ -365,88 +365,6 @@ function(_add_variant_c_compile_flags) set("${CFLAGS_RESULT_VAR_NAME}" "${result}" PARENT_SCOPE) endfunction() -function(_add_variant_swift_compile_flags - sdk arch build_type enable_assertions result_var_name) - set(result ${${result_var_name}}) - - cmake_parse_arguments( - VARIANT # prefix - "" # options - "MACCATALYST_BUILD_FLAVOR" # single-value args - "" # multi-value args - ${ARGN}) - - # On Windows, we don't set SWIFT_SDK_WINDOWS_PATH_ARCH_{ARCH}_PATH, so don't include it. - # On Android the sdk is split to two different paths for includes and libs, so these - # need to be set manually. - if (NOT "${sdk}" STREQUAL "WINDOWS" AND NOT "${sdk}" STREQUAL "ANDROID") - list(APPEND result "-sdk" "${SWIFT_SDK_${sdk}_ARCH_${arch}_PATH}") - endif() - - is_darwin_based_sdk("${sdk}" IS_DARWIN) - if(IS_DARWIN) - set(sdk_deployment_version "${SWIFT_SDK_${sdk}_DEPLOYMENT_VERSION}") - get_target_triple(target target_variant "${sdk}" "${arch}" - MACCATALYST_BUILD_FLAVOR "${VARIANT_MACCATALYST_BUILD_FLAVOR}" - DEPLOYMENT_VERSION "${sdk_deployment_version}") - - list(APPEND result "-target" "${target}") - if(target_variant) - list(APPEND result "-target-variant" "${target_variant}") - endif() - else() - list(APPEND result - "-target" "${SWIFT_SDK_${sdk}_ARCH_${arch}_TRIPLE}") - endif() - - if("${sdk}" STREQUAL "ANDROID") - swift_android_include_for_arch(${arch} ${arch}_swift_include) - 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) - list(APPEND result "-resource-dir" "${SWIFTLIB_DIR}") - endif() - - if(IS_DARWIN) - # We collate -F with the framework path to avoid unwanted deduplication - # of options by target_compile_options -- this way no undesired - # side effects are introduced should a new search path be added. - list(APPEND result - "-F${SWIFT_SDK_${sdk}_ARCH_${arch}_PATH}/../../../Developer/Library/Frameworks") - endif() - - is_build_type_optimized("${build_type}" optimized) - if(optimized) - list(APPEND result "-O") - else() - list(APPEND result "-Onone") - endif() - - is_build_type_with_debuginfo("${build_type}" debuginfo) - if(debuginfo) - list(APPEND result "-g") - endif() - - if(enable_assertions) - list(APPEND result "-D" "INTERNAL_CHECKS_ENABLED") - endif() - - if(SWIFT_ENABLE_RUNTIME_FUNCTION_COUNTERS) - list(APPEND result "-D" "SWIFT_ENABLE_RUNTIME_FUNCTION_COUNTERS") - endif() - - if(SWIFT_ENABLE_EXPERIMENTAL_DIFFERENTIABLE_PROGRAMMING) - list(APPEND result "-D" "SWIFT_ENABLE_EXPERIMENTAL_DIFFERENTIABLE_PROGRAMMING") - endif() - - set("${result_var_name}" "${result}" PARENT_SCOPE) -endfunction() - function(_add_variant_link_flags) set(oneValueArgs SDK ARCH BUILD_TYPE ENABLE_ASSERTIONS ANALYZE_CODE_COVERAGE DEPLOYMENT_VERSION_OSX DEPLOYMENT_VERSION_MACCATALYST DEPLOYMENT_VERSION_IOS DEPLOYMENT_VERSION_TVOS DEPLOYMENT_VERSION_WATCHOS @@ -588,92 +506,6 @@ function(_add_variant_link_flags) set("${LFLAGS_LIBRARY_SEARCH_DIRECTORIES_VAR_NAME}" "${library_search_directories}" PARENT_SCOPE) endfunction() -# Look up extra flags for a module that matches a regexp. -function(_add_extra_swift_flags_for_module module_name result_var_name) - set(result_list) - list(LENGTH SWIFT_EXPERIMENTAL_EXTRA_REGEXP_FLAGS listlen) - if (${listlen} GREATER 0) - math(EXPR listlen "${listlen}-1") - foreach(i RANGE 0 ${listlen} 2) - list(GET SWIFT_EXPERIMENTAL_EXTRA_REGEXP_FLAGS ${i} regex) - if (module_name MATCHES "${regex}") - math(EXPR ip1 "${i}+1") - list(GET SWIFT_EXPERIMENTAL_EXTRA_REGEXP_FLAGS ${ip1} flags) - list(APPEND result_list ${flags}) - message(STATUS "Matched '${regex}' to module '${module_name}'. Compiling ${module_name} with special flags: ${flags}") - endif() - endforeach() - endif() - list(LENGTH SWIFT_EXPERIMENTAL_EXTRA_NEGATIVE_REGEXP_FLAGS listlen) - if (${listlen} GREATER 0) - math(EXPR listlen "${listlen}-1") - foreach(i RANGE 0 ${listlen} 2) - list(GET SWIFT_EXPERIMENTAL_EXTRA_NEGATIVE_REGEXP_FLAGS ${i} regex) - if (NOT module_name MATCHES "${regex}") - math(EXPR ip1 "${i}+1") - list(GET SWIFT_EXPERIMENTAL_EXTRA_NEGATIVE_REGEXP_FLAGS ${ip1} flags) - list(APPEND result_list ${flags}) - message(STATUS "Matched NEGATIVE '${regex}' to module '${module_name}'. Compiling ${module_name} with special flags: ${flags}") - endif() - endforeach() - endif() - set("${result_var_name}" ${result_list} PARENT_SCOPE) -endfunction() - -# Add a universal binary target created from the output of the given -# set of targets by running 'lipo'. -# -# Usage: -# _add_swift_lipo_target( -# sdk # The name of the SDK the target was created for. -# # Examples include "OSX", "IOS", "ANDROID", etc. -# target # The name of the target to create -# output # The file to be created by this target -# source_targets... # The source targets whose outputs will be -# # lipo'd into the output. -# ) -function(_add_swift_lipo_target) - cmake_parse_arguments( - LIPO # prefix - "CODESIGN" # options - "SDK;TARGET;OUTPUT" # single-value args - "" # multi-value args - ${ARGN}) - - precondition(LIPO_SDK MESSAGE "sdk is required") - precondition(LIPO_TARGET MESSAGE "target is required") - precondition(LIPO_OUTPUT MESSAGE "output is required") - precondition(LIPO_UNPARSED_ARGUMENTS MESSAGE "one or more inputs are required") - - set(source_targets ${LIPO_UNPARSED_ARGUMENTS}) - - # Gather the source binaries. - set(source_binaries) - foreach(source_target ${source_targets}) - list(APPEND source_binaries $) - endforeach() - - if(${LIPO_SDK} IN_LIST SWIFT_APPLE_PLATFORMS) - if(LIPO_CODESIGN) - set(codesign_command COMMAND "codesign" "-f" "-s" "-" "${LIPO_OUTPUT}") - endif() - # Use lipo to create the final binary. - add_custom_command_target(unused_var - COMMAND "${SWIFT_LIPO}" "-create" "-output" "${LIPO_OUTPUT}" ${source_binaries} - ${codesign_command} - CUSTOM_TARGET_NAME "${LIPO_TARGET}" - OUTPUT "${LIPO_OUTPUT}" - DEPENDS ${source_targets}) - else() - # We don't know how to create fat binaries for other platforms. - add_custom_command_target(unused_var - COMMAND "${CMAKE_COMMAND}" "-E" "copy" "${source_binaries}" "${LIPO_OUTPUT}" - CUSTOM_TARGET_NAME "${LIPO_TARGET}" - OUTPUT "${LIPO_OUTPUT}" - DEPENDS ${source_targets}) - endif() -endfunction() - # Add a single variant of a new Swift library. # # Usage: @@ -1010,1758 +842,6 @@ function(add_swift_host_library name) endif() endfunction() -# Add a single variant of a new Swift library. -# -# Usage: -# _add_swift_target_library_single( -# target -# name -# [MODULE_TARGETS] -# [SHARED] -# [STATIC] -# [SDK sdk] -# [ARCHITECTURE architecture] -# [DEPENDS dep1 ...] -# [LINK_LIBRARIES dep1 ...] -# [FRAMEWORK_DEPENDS dep1 ...] -# [FRAMEWORK_DEPENDS_WEAK dep1 ...] -# [LLVM_LINK_COMPONENTS comp1 ...] -# [C_COMPILE_FLAGS flag1...] -# [SWIFT_COMPILE_FLAGS flag1...] -# [LINK_FLAGS flag1...] -# [FILE_DEPENDS target1 ...] -# [DONT_EMBED_BITCODE] -# [IS_STDLIB] -# [IS_STDLIB_CORE] -# [IS_SDK_OVERLAY] -# INSTALL_IN_COMPONENT comp -# MACCATALYST_BUILD_FLAVOR flavor -# source1 [source2 source3 ...]) -# -# target -# Name of the target (e.g., swiftParse-IOS-armv7). -# -# name -# Name of the library (e.g., swiftParse). -# -# MODULE_TARGETS -# Names of the module target (e.g., swiftParse-swiftmodule-IOS-armv7). -# -# SHARED -# Build a shared library. -# -# STATIC -# Build a static library. -# -# SDK sdk -# SDK to build for. -# -# ARCHITECTURE -# Architecture to build for. -# -# DEPENDS -# Targets that this library depends on. -# -# LINK_LIBRARIES -# Libraries this library depends on. -# -# FRAMEWORK_DEPENDS -# System frameworks this library depends on. -# -# FRAMEWORK_DEPENDS_WEAK -# System frameworks this library depends on that should be weakly-linked. -# -# LLVM_LINK_COMPONENTS -# LLVM components this library depends on. -# -# C_COMPILE_FLAGS -# Extra compile flags (C, C++, ObjC). -# -# SWIFT_COMPILE_FLAGS -# Extra compile flags (Swift). -# -# LINK_FLAGS -# Extra linker flags. -# -# FILE_DEPENDS -# Additional files this library depends on. -# -# DONT_EMBED_BITCODE -# Don't embed LLVM bitcode in this target, even if it is enabled globally. -# -# IS_STDLIB -# Install library dylib and swift module files to lib/swift. -# -# IS_STDLIB_CORE -# Compile as the standard library core. -# -# IS_SDK_OVERLAY -# Treat the library as a part of the Swift SDK overlay. -# -# INSTALL_IN_COMPONENT comp -# The Swift installation component that this library belongs to. -# -# MACCATALYST_BUILD_FLAVOR -# Possible values are 'ios-like', 'macos-like', 'zippered', 'unzippered-twin' -# -# source1 ... -# Sources to add into this library -function(_add_swift_target_library_single target name) - set(SWIFTLIB_SINGLE_options - DONT_EMBED_BITCODE - IS_SDK_OVERLAY - IS_STDLIB - IS_STDLIB_CORE - NOSWIFTRT - OBJECT_LIBRARY - SHARED - STATIC - TARGET_LIBRARY - INSTALL_WITH_SHARED) - set(SWIFTLIB_SINGLE_single_parameter_options - ARCHITECTURE - DEPLOYMENT_VERSION_IOS - DEPLOYMENT_VERSION_OSX - DEPLOYMENT_VERSION_TVOS - DEPLOYMENT_VERSION_WATCHOS - INSTALL_IN_COMPONENT - DARWIN_INSTALL_NAME_DIR - SDK - DEPLOYMENT_VERSION_MACCATALYST - MACCATALYST_BUILD_FLAVOR) - set(SWIFTLIB_SINGLE_multiple_parameter_options - C_COMPILE_FLAGS - DEPENDS - FILE_DEPENDS - FRAMEWORK_DEPENDS - FRAMEWORK_DEPENDS_WEAK - GYB_SOURCES - INCORPORATE_OBJECT_LIBRARIES - INCORPORATE_OBJECT_LIBRARIES_SHARED_ONLY - LINK_FLAGS - LINK_LIBRARIES - LLVM_LINK_COMPONENTS - PRIVATE_LINK_LIBRARIES - SWIFT_COMPILE_FLAGS - MODULE_TARGETS) - - cmake_parse_arguments(SWIFTLIB_SINGLE - "${SWIFTLIB_SINGLE_options}" - "${SWIFTLIB_SINGLE_single_parameter_options}" - "${SWIFTLIB_SINGLE_multiple_parameter_options}" - ${ARGN}) - - # Determine macCatalyst build flavor - get_maccatalyst_build_flavor(maccatalyst_build_flavor - "${SWIFTLIB_SINGLE_SDK}" "${SWIFTLIB_SINGLE_MACCATALYST_BUILD_FLAVOR}") - - set(SWIFTLIB_SINGLE_SOURCES ${SWIFTLIB_SINGLE_UNPARSED_ARGUMENTS}) - - translate_flags(SWIFTLIB_SINGLE "${SWIFTLIB_SINGLE_options}") - - # Check arguments. - precondition(SWIFTLIB_SINGLE_SDK MESSAGE "Should specify an SDK") - precondition(SWIFTLIB_SINGLE_ARCHITECTURE MESSAGE "Should specify an architecture") - precondition(SWIFTLIB_SINGLE_INSTALL_IN_COMPONENT MESSAGE "INSTALL_IN_COMPONENT is required") - - if(NOT SWIFTLIB_SINGLE_SHARED AND - NOT SWIFTLIB_SINGLE_STATIC AND - NOT SWIFTLIB_SINGLE_OBJECT_LIBRARY) - message(FATAL_ERROR - "Either SHARED, STATIC, or OBJECT_LIBRARY must be specified") - endif() - - # Determine the subdirectory where this library will be installed. - set(SWIFTLIB_SINGLE_SUBDIR - "${SWIFT_SDK_${SWIFTLIB_SINGLE_SDK}_LIB_SUBDIR}/${SWIFTLIB_SINGLE_ARCHITECTURE}") - - # macCatalyst ios-like builds are installed in the maccatalyst/x86_64 directory - if(maccatalyst_build_flavor STREQUAL "ios-like") - set(SWIFTLIB_SINGLE_SUBDIR - "${SWIFT_SDK_MACCATALYST_LIB_SUBDIR}/${SWIFTLIB_SINGLE_ARCHITECTURE}") - endif() - - # Include LLVM Bitcode slices for iOS, Watch OS, and Apple TV OS device libraries. - set(embed_bitcode_arg) - if(SWIFT_EMBED_BITCODE_SECTION AND NOT SWIFTLIB_SINGLE_DONT_EMBED_BITCODE) - if("${SWIFTLIB_SINGLE_SDK}" STREQUAL "IOS" OR "${SWIFTLIB_SINGLE_SDK}" STREQUAL "TVOS" OR "${SWIFTLIB_SINGLE_SDK}" STREQUAL "WATCHOS") - list(APPEND SWIFTLIB_SINGLE_C_COMPILE_FLAGS "-fembed-bitcode") - set(embed_bitcode_arg EMBED_BITCODE) - endif() - endif() - - if(XCODE) - string(REGEX MATCHALL "/[^/]+" split_path ${CMAKE_CURRENT_SOURCE_DIR}) - list(GET split_path -1 dir) - file(GLOB_RECURSE SWIFTLIB_SINGLE_HEADERS - ${SWIFT_SOURCE_DIR}/include/swift${dir}/*.h - ${SWIFT_SOURCE_DIR}/include/swift${dir}/*.def - ${CMAKE_CURRENT_SOURCE_DIR}/*.def) - - file(GLOB_RECURSE SWIFTLIB_SINGLE_TDS - ${SWIFT_SOURCE_DIR}/include/swift${dir}/*.td) - - set_source_files_properties(${SWIFTLIB_SINGLE_HEADERS} ${SWIFTLIB_SINGLE_TDS} - PROPERTIES - HEADER_FILE_ONLY true) - source_group("TableGen descriptions" FILES ${SWIFTLIB_SINGLE_TDS}) - - set(SWIFTLIB_SINGLE_SOURCES ${SWIFTLIB_SINGLE_SOURCES} ${SWIFTLIB_SINGLE_HEADERS} ${SWIFTLIB_SINGLE_TDS}) - endif() - - if(MODULE) - set(libkind MODULE) - elseif(SWIFTLIB_SINGLE_OBJECT_LIBRARY) - set(libkind OBJECT) - # If both SHARED and STATIC are specified, we add the SHARED library first. - # The STATIC library is handled further below. - elseif(SWIFTLIB_SINGLE_SHARED) - set(libkind SHARED) - elseif(SWIFTLIB_SINGLE_STATIC) - set(libkind STATIC) - else() - message(FATAL_ERROR - "Either SHARED, STATIC, or OBJECT_LIBRARY must be specified") - endif() - - if(SWIFTLIB_SINGLE_GYB_SOURCES) - handle_gyb_sources( - gyb_dependency_targets - SWIFTLIB_SINGLE_GYB_SOURCES - "${SWIFTLIB_SINGLE_ARCHITECTURE}") - set(SWIFTLIB_SINGLE_SOURCES ${SWIFTLIB_SINGLE_SOURCES} - ${SWIFTLIB_SINGLE_GYB_SOURCES}) - endif() - - # Remove the "swift" prefix from the name to determine the module name. - if(SWIFTLIB_IS_STDLIB_CORE) - set(module_name "Swift") - else() - string(REPLACE swift "" module_name "${name}") - endif() - - if("${SWIFTLIB_SINGLE_SDK}" STREQUAL "WINDOWS") - if(NOT "${CMAKE_C_COMPILER_ID}" STREQUAL "MSVC") - swift_windows_get_sdk_vfs_overlay(SWIFTLIB_SINGLE_VFS_OVERLAY) - list(APPEND SWIFTLIB_SINGLE_SWIFT_COMPILE_FLAGS - -Xcc;-Xclang;-Xcc;-ivfsoverlay;-Xcc;-Xclang;-Xcc;${SWIFTLIB_SINGLE_VFS_OVERLAY}) - endif() - swift_windows_include_for_arch(${SWIFTLIB_SINGLE_ARCHITECTURE} SWIFTLIB_INCLUDE) - foreach(directory ${SWIFTLIB_INCLUDE}) - list(APPEND SWIFTLIB_SINGLE_SWIFT_COMPILE_FLAGS -Xcc;-isystem;-Xcc;${directory}) - endforeach() - if("${SWIFTLIB_SINGLE_ARCHITECTURE}" MATCHES arm) - list(APPEND SWIFTLIB_SINGLE_SWIFT_COMPILE_FLAGS -Xcc;-D_ARM_WINAPI_PARTITION_DESKTOP_SDK_AVAILABLE) - endif() - list(APPEND SWIFTLIB_SINGLE_SWIFT_COMPILE_FLAGS - -libc;${SWIFT_STDLIB_MSVC_RUNTIME_LIBRARY}) - endif() - - # FIXME: don't actually depend on the libraries in SWIFTLIB_SINGLE_LINK_LIBRARIES, - # just any swiftmodule files that are associated with them. - handle_swift_sources( - swift_object_dependency_target - swift_module_dependency_target - swift_sib_dependency_target - swift_sibopt_dependency_target - swift_sibgen_dependency_target - SWIFTLIB_SINGLE_SOURCES - SWIFTLIB_SINGLE_EXTERNAL_SOURCES ${name} - DEPENDS - ${gyb_dependency_targets} - ${SWIFTLIB_SINGLE_DEPENDS} - ${SWIFTLIB_SINGLE_FILE_DEPENDS} - ${SWIFTLIB_SINGLE_LINK_LIBRARIES} - SDK ${SWIFTLIB_SINGLE_SDK} - ARCHITECTURE ${SWIFTLIB_SINGLE_ARCHITECTURE} - MODULE_NAME ${module_name} - COMPILE_FLAGS ${SWIFTLIB_SINGLE_SWIFT_COMPILE_FLAGS} - ${SWIFTLIB_SINGLE_IS_STDLIB_keyword} - ${SWIFTLIB_SINGLE_IS_STDLIB_CORE_keyword} - ${SWIFTLIB_SINGLE_IS_SDK_OVERLAY_keyword} - ${embed_bitcode_arg} - INSTALL_IN_COMPONENT "${SWIFTLIB_SINGLE_INSTALL_IN_COMPONENT}" - MACCATALYST_BUILD_FLAVOR "${SWIFTLIB_SINGLE_MACCATALYST_BUILD_FLAVOR}") - add_swift_source_group("${SWIFTLIB_SINGLE_EXTERNAL_SOURCES}") - - # If there were any swift sources, then a .swiftmodule may have been created. - # If that is the case, then add a target which is an alias of the module files. - set(VARIANT_SUFFIX "-${SWIFT_SDK_${SWIFTLIB_SINGLE_SDK}_LIB_SUBDIR}-${SWIFTLIB_SINGLE_ARCHITECTURE}") - if(maccatalyst_build_flavor STREQUAL "ios-like") - set(VARIANT_SUFFIX "-${SWIFT_SDK_MACCATALYST_LIB_SUBDIR}-${SWIFTLIB_SINGLE_ARCHITECTURE}") - endif() - - if(NOT "${SWIFTLIB_SINGLE_MODULE_TARGETS}" STREQUAL "" AND NOT "${swift_module_dependency_target}" STREQUAL "") - foreach(module_target ${SWIFTLIB_SINGLE_MODULE_TARGETS}) - add_custom_target("${module_target}" - DEPENDS ${swift_module_dependency_target}) - set_target_properties("${module_target}" PROPERTIES - FOLDER "Swift libraries/Modules") - endforeach() - endif() - - # For standalone overlay builds to work - if(NOT BUILD_STANDALONE) - if (EXISTS swift_sib_dependency_target AND NOT "${swift_sib_dependency_target}" STREQUAL "") - add_dependencies(swift-stdlib${VARIANT_SUFFIX}-sib ${swift_sib_dependency_target}) - endif() - - if (EXISTS swift_sibopt_dependency_target AND NOT "${swift_sibopt_dependency_target}" STREQUAL "") - add_dependencies(swift-stdlib${VARIANT_SUFFIX}-sibopt ${swift_sibopt_dependency_target}) - endif() - - if (EXISTS swift_sibgen_dependency_target AND NOT "${swift_sibgen_dependency_target}" STREQUAL "") - add_dependencies(swift-stdlib${VARIANT_SUFFIX}-sibgen ${swift_sibgen_dependency_target}) - endif() - endif() - - # Only build the modules for any arch listed in the *_MODULE_ARCHITECTURES. - if(SWIFTLIB_SINGLE_SDK IN_LIST SWIFT_APPLE_PLATFORMS - AND SWIFTLIB_SINGLE_ARCHITECTURE IN_LIST SWIFT_SDK_${SWIFTLIB_SINGLE_SDK}_MODULE_ARCHITECTURES) - # Create dummy target to hook up the module target dependency. - add_custom_target("${target}" - DEPENDS - "${swift_module_dependency_target}") - - return() - endif() - - set(SWIFTLIB_INCORPORATED_OBJECT_LIBRARIES_EXPRESSIONS) - foreach(object_library ${SWIFTLIB_SINGLE_INCORPORATE_OBJECT_LIBRARIES}) - list(APPEND SWIFTLIB_INCORPORATED_OBJECT_LIBRARIES_EXPRESSIONS - $) - endforeach() - - set(SWIFTLIB_INCORPORATED_OBJECT_LIBRARIES_EXPRESSIONS_SHARED_ONLY) - foreach(object_library ${SWIFTLIB_SINGLE_INCORPORATE_OBJECT_LIBRARIES_SHARED_ONLY}) - list(APPEND SWIFTLIB_INCORPORATED_OBJECT_LIBRARIES_EXPRESSIONS_SHARED_ONLY - $) - endforeach() - - set(SWIFTLIB_SINGLE_XCODE_WORKAROUND_SOURCES) - if(XCODE AND SWIFTLIB_SINGLE_TARGET_LIBRARY) - set(SWIFTLIB_SINGLE_XCODE_WORKAROUND_SOURCES - # Note: the dummy.cpp source file provides no definitions. However, - # it forces Xcode to properly link the static library. - ${SWIFT_SOURCE_DIR}/cmake/dummy.cpp) - endif() - - set(INCORPORATED_OBJECT_LIBRARIES_EXPRESSIONS ${SWIFTLIB_INCORPORATED_OBJECT_LIBRARIES_EXPRESSIONS}) - if(${libkind} STREQUAL "SHARED") - list(APPEND INCORPORATED_OBJECT_LIBRARIES_EXPRESSIONS - ${SWIFTLIB_INCORPORATED_OBJECT_LIBRARIES_EXPRESSIONS_SHARED_ONLY}) - endif() - - add_library("${target}" ${libkind} - ${SWIFTLIB_SINGLE_SOURCES} - ${SWIFTLIB_SINGLE_EXTERNAL_SOURCES} - ${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 - SWIFTLIB_SINGLE_TARGET_LIBRARY) - if("${libkind}" STREQUAL "SHARED" AND NOT SWIFTLIB_SINGLE_NOSWIFTRT) - # TODO(compnerd) switch to the generator expression when cmake is upgraded - # to a version which supports it. - # target_sources(${target} - # PRIVATE - # $) - if(SWIFTLIB_SINGLE_SDK STREQUAL WINDOWS) - set(extension .obj) - else() - set(extension .o) - endif() - target_sources(${target} - PRIVATE - "${SWIFTLIB_DIR}/${SWIFTLIB_SINGLE_SUBDIR}/swiftrt${extension}") - set_source_files_properties("${SWIFTLIB_DIR}/${SWIFTLIB_SINGLE_SUBDIR}/swiftrt${extension}" - PROPERTIES - GENERATED 1) - endif() - endif() - _set_target_prefix_and_suffix("${target}" "${libkind}" "${SWIFTLIB_SINGLE_SDK}") - - if("${SWIFTLIB_SINGLE_SDK}" STREQUAL "WINDOWS") - swift_windows_include_for_arch(${SWIFTLIB_SINGLE_ARCHITECTURE} SWIFTLIB_INCLUDE) - target_include_directories("${target}" SYSTEM PRIVATE ${SWIFTLIB_INCLUDE}) - set_target_properties(${target} - PROPERTIES - CXX_STANDARD 14) - endif() - - if("${SWIFTLIB_SINGLE_SDK}" STREQUAL "WINDOWS" AND NOT "${CMAKE_SYSTEM_NAME}" STREQUAL "Windows") - if("${libkind}" STREQUAL "SHARED") - # Each dll has an associated .lib (import library); since we may be - # building on a non-DLL platform (not windows), create an imported target - # for the library which created implicitly by the dll. - add_custom_command_target(${target}_IMPORT_LIBRARY - OUTPUT "${SWIFTLIB_DIR}/${SWIFTLIB_SINGLE_SUBDIR}/${name}.lib" - DEPENDS "${target}") - add_library(${target}_IMPLIB SHARED IMPORTED GLOBAL) - set_property(TARGET "${target}_IMPLIB" PROPERTY - IMPORTED_LOCATION "${SWIFTLIB_DIR}/${SWIFTLIB_SINGLE_SUBDIR}/${name}.lib") - add_dependencies(${target}_IMPLIB ${${target}_IMPORT_LIBRARY}) - endif() - set_property(TARGET "${target}" PROPERTY NO_SONAME ON) - endif() - - llvm_update_compile_flags(${target}) - - set_output_directory(${target} - BINARY_DIR ${SWIFT_RUNTIME_OUTPUT_INTDIR} - LIBRARY_DIR ${SWIFT_LIBRARY_OUTPUT_INTDIR}) - - if(MODULE) - set_target_properties("${target}" PROPERTIES - PREFIX "" - SUFFIX ${LLVM_PLUGIN_EXT}) - endif() - - if(SWIFTLIB_SINGLE_TARGET_LIBRARY) - # Install runtime libraries to lib/swift instead of lib. This works around - # the fact that -isysroot prevents linking to libraries in the system - # /usr/lib if Swift is installed in /usr. - set_target_properties("${target}" PROPERTIES - LIBRARY_OUTPUT_DIRECTORY ${SWIFTLIB_DIR}/${SWIFTLIB_SINGLE_SUBDIR} - ARCHIVE_OUTPUT_DIRECTORY ${SWIFTLIB_DIR}/${SWIFTLIB_SINGLE_SUBDIR}) - if(SWIFTLIB_SINGLE_SDK STREQUAL WINDOWS AND SWIFTLIB_SINGLE_IS_STDLIB_CORE - AND libkind STREQUAL SHARED) - add_custom_command(TARGET ${target} POST_BUILD - COMMAND ${CMAKE_COMMAND} -E copy_if_different $ ${SWIFTLIB_DIR}/${SWIFTLIB_SINGLE_SUBDIR}) - endif() - - foreach(config ${CMAKE_CONFIGURATION_TYPES}) - string(TOUPPER ${config} config_upper) - escape_path_for_xcode("${config}" "${SWIFTLIB_DIR}" config_lib_dir) - set_target_properties(${target} PROPERTIES - LIBRARY_OUTPUT_DIRECTORY_${config_upper} ${config_lib_dir}/${SWIFTLIB_SINGLE_SUBDIR} - ARCHIVE_OUTPUT_DIRECTORY_${config_upper} ${config_lib_dir}/${SWIFTLIB_SINGLE_SUBDIR}) - endforeach() - endif() - - if(SWIFTLIB_SINGLE_SDK IN_LIST SWIFT_APPLE_PLATFORMS) - set(install_name_dir "@rpath") - - if(SWIFTLIB_SINGLE_IS_STDLIB) - set(install_name_dir "${SWIFT_DARWIN_STDLIB_INSTALL_NAME_DIR}") - - # iOS-like overlays are installed in a separate directory so that - # unzippered twins do not conflict. - if(maccatalyst_build_flavor STREQUAL "ios-like" - AND DEFINED SWIFT_DARWIN_MACCATALYST_STDLIB_INSTALL_NAME_DIR) - set(install_name_dir "${SWIFT_DARWIN_MACCATALYST_STDLIB_INSTALL_NAME_DIR}") - endif() - endif() - - # Always use @rpath for XCTest - if(module_name STREQUAL "XCTest") - set(install_name_dir "@rpath") - endif() - - if(SWIFTLIB_SINGLE_DARWIN_INSTALL_NAME_DIR) - set(install_name_dir "${SWIFTLIB_SINGLE_DARWIN_INSTALL_NAME_DIR}") - endif() - - set_target_properties("${target}" - PROPERTIES - INSTALL_NAME_DIR "${install_name_dir}") - elseif("${SWIFTLIB_SINGLE_SDK}" STREQUAL "LINUX") - set_target_properties("${target}" - PROPERTIES - INSTALL_RPATH "$ORIGIN:/usr/lib/swift/linux") - elseif("${SWIFTLIB_SINGLE_SDK}" STREQUAL "CYGWIN") - set_target_properties("${target}" - PROPERTIES - INSTALL_RPATH "$ORIGIN:/usr/lib/swift/cygwin") - elseif("${SWIFTLIB_SINGLE_SDK}" STREQUAL "ANDROID") - # CMake generates an incorrect rule `$SONAME_FLAG $INSTALLNAME_DIR$SONAME` - # for an Android cross-build from a macOS host. Construct the proper linker - # flags manually in add_swift_target_library instead, see there with - # variable `swiftlib_link_flags_all`. - if(SWIFTLIB_SINGLE_TARGET_LIBRARY) - set_target_properties("${target}" PROPERTIES NO_SONAME TRUE) - endif() - # Only set the install RPATH if cross-compiling the host tools, in which - # case both the NDK and Sysroot paths must be set. - if(NOT "${SWIFT_ANDROID_NDK_PATH}" STREQUAL "" AND - NOT "${SWIFT_ANDROID_NATIVE_SYSROOT}" STREQUAL "") - set_target_properties("${target}" - PROPERTIES - INSTALL_RPATH "$ORIGIN") - endif() - endif() - - set_target_properties("${target}" PROPERTIES BUILD_WITH_INSTALL_RPATH YES) - set_target_properties("${target}" PROPERTIES FOLDER "Swift libraries") - - # Configure the static library target. - # Set compile and link flags for the non-static target. - # Do these LAST. - set(target_static) - if(SWIFTLIB_SINGLE_IS_STDLIB AND SWIFTLIB_SINGLE_STATIC) - set(target_static "${target}-static") - - # We have already compiled Swift sources. Link everything into a static - # library. - add_library(${target_static} STATIC - ${SWIFTLIB_SINGLE_SOURCES} - ${SWIFTLIB_INCORPORATED_OBJECT_LIBRARIES_EXPRESSIONS} - ${SWIFTLIB_SINGLE_XCODE_WORKAROUND_SOURCES}) - - set_output_directory(${target_static} - BINARY_DIR ${SWIFT_RUNTIME_OUTPUT_INTDIR} - LIBRARY_DIR ${SWIFT_LIBRARY_OUTPUT_INTDIR}) - - if(SWIFTLIB_INSTALL_WITH_SHARED) - set(swift_lib_dir ${SWIFTLIB_DIR}) - else() - set(swift_lib_dir ${SWIFTSTATICLIB_DIR}) - endif() - - foreach(config ${CMAKE_CONFIGURATION_TYPES}) - string(TOUPPER ${config} config_upper) - escape_path_for_xcode( - "${config}" "${swift_lib_dir}" config_lib_dir) - set_target_properties(${target_static} PROPERTIES - LIBRARY_OUTPUT_DIRECTORY_${config_upper} ${config_lib_dir}/${SWIFTLIB_SINGLE_SUBDIR} - ARCHIVE_OUTPUT_DIRECTORY_${config_upper} ${config_lib_dir}/${SWIFTLIB_SINGLE_SUBDIR}) - endforeach() - - set_target_properties(${target_static} PROPERTIES - LIBRARY_OUTPUT_DIRECTORY ${swift_lib_dir}/${SWIFTLIB_SINGLE_SUBDIR} - ARCHIVE_OUTPUT_DIRECTORY ${swift_lib_dir}/${SWIFTLIB_SINGLE_SUBDIR}) - endif() - - set_target_properties(${target} - PROPERTIES - # Library name (without the variant information) - OUTPUT_NAME ${name}) - if(target_static) - set_target_properties(${target_static} - PROPERTIES - OUTPUT_NAME ${name}) - endif() - - # Don't build standard libraries by default. We will enable building - # standard libraries that the user requested; the rest can be built on-demand. - if(SWIFTLIB_SINGLE_TARGET_LIBRARY) - foreach(t "${target}" ${target_static}) - set_target_properties(${t} PROPERTIES EXCLUDE_FROM_ALL TRUE) - endforeach() - endif() - - # Handle linking and dependencies. - add_dependencies_multiple_targets( - TARGETS "${target}" ${target_static} - DEPENDS - ${SWIFTLIB_SINGLE_DEPENDS} - ${gyb_dependency_targets} - "${swift_object_dependency_target}" - "${swift_module_dependency_target}" - ${LLVM_COMMON_DEPENDS}) - - if("${libkind}" STREQUAL "SHARED") - target_link_libraries("${target}" PRIVATE ${SWIFTLIB_SINGLE_LINK_LIBRARIES}) - elseif("${libkind}" STREQUAL "OBJECT") - precondition_list_empty( - "${SWIFTLIB_SINGLE_LINK_LIBRARIES}" - "OBJECT_LIBRARY may not link to anything") - else() - target_link_libraries("${target}" INTERFACE ${SWIFTLIB_SINGLE_LINK_LIBRARIES}) - endif() - - # Don't add the icucore target. - set(SWIFTLIB_SINGLE_LINK_LIBRARIES_WITHOUT_ICU) - foreach(item ${SWIFTLIB_SINGLE_LINK_LIBRARIES}) - if(NOT "${item}" STREQUAL "icucore") - list(APPEND SWIFTLIB_SINGLE_LINK_LIBRARIES_WITHOUT_ICU "${item}") - endif() - endforeach() - - if(target_static) - _list_add_string_suffix( - "${SWIFTLIB_SINGLE_LINK_LIBRARIES_WITHOUT_ICU}" - "-static" - target_static_depends) - # FIXME: should this be target_link_libraries? - add_dependencies_multiple_targets( - TARGETS "${target_static}" - DEPENDS ${target_static_depends}) - endif() - - # Link against system frameworks. - foreach(FRAMEWORK ${SWIFTLIB_SINGLE_FRAMEWORK_DEPENDS}) - foreach(t "${target}" ${target_static}) - target_link_libraries("${t}" PUBLIC "-framework ${FRAMEWORK}") - endforeach() - endforeach() - foreach(FRAMEWORK ${SWIFTLIB_SINGLE_FRAMEWORK_DEPENDS_WEAK}) - foreach(t "${target}" ${target_static}) - target_link_libraries("${t}" PUBLIC "-weak_framework ${FRAMEWORK}") - endforeach() - endforeach() - - if(NOT SWIFTLIB_SINGLE_TARGET_LIBRARY) - # Call llvm_config() only for libraries that are part of the compiler. - swift_common_llvm_config("${target}" ${SWIFTLIB_SINGLE_LLVM_LINK_COMPONENTS}) - endif() - - # Collect compile and link flags for the static and non-static targets. - # Don't set PROPERTY COMPILE_FLAGS or LINK_FLAGS directly. - set(c_compile_flags ${SWIFTLIB_SINGLE_C_COMPILE_FLAGS}) - set(link_flags ${SWIFTLIB_SINGLE_LINK_FLAGS}) - - set(library_search_subdir "${SWIFT_SDK_${SWIFTLIB_SINGLE_SDK}_LIB_SUBDIR}") - set(library_search_directories - "${SWIFTLIB_DIR}/${SWIFTLIB_SINGLE_SUBDIR}" - "${SWIFT_NATIVE_SWIFT_TOOLS_PATH}/../lib/swift/${SWIFTLIB_SINGLE_SUBDIR}" - "${SWIFT_NATIVE_SWIFT_TOOLS_PATH}/../lib/swift/${SWIFT_SDK_${SWIFTLIB_SINGLE_SDK}_LIB_SUBDIR}") - - # In certain cases when building, the environment variable SDKROOT is set to override - # where the sdk root is located in the system. If that environment variable has been - # set by the user, respect it and add the specified SDKROOT directory to the - # library_search_directories so we are able to link against those libraries - if(DEFINED ENV{SDKROOT} AND EXISTS "$ENV{SDKROOT}/usr/lib/swift") - list(APPEND library_search_directories "$ENV{SDKROOT}/usr/lib/swift") - endif() - - # Add variant-specific flags. - if(SWIFTLIB_SINGLE_TARGET_LIBRARY) - set(build_type "${SWIFT_STDLIB_BUILD_TYPE}") - set(enable_assertions "${SWIFT_STDLIB_ASSERTIONS}") - else() - set(build_type "${CMAKE_BUILD_TYPE}") - set(enable_assertions "${LLVM_ENABLE_ASSERTIONS}") - set(analyze_code_coverage "${SWIFT_ANALYZE_CODE_COVERAGE}") - endif() - - if (NOT SWIFTLIB_SINGLE_TARGET_LIBRARY) - set(lto_type "${SWIFT_TOOLS_ENABLE_LTO}") - endif() - - _add_variant_c_compile_flags( - SDK "${SWIFTLIB_SINGLE_SDK}" - ARCH "${SWIFTLIB_SINGLE_ARCHITECTURE}" - BUILD_TYPE "${build_type}" - ENABLE_ASSERTIONS "${enable_assertions}" - ANALYZE_CODE_COVERAGE "${analyze_code_coverage}" - ENABLE_LTO "${lto_type}" - DEPLOYMENT_VERSION_OSX "${SWIFTLIB_DEPLOYMENT_VERSION_OSX}" - DEPLOYMENT_VERSION_MACCATALYST "${SWIFTLIB_DEPLOYMENT_VERSION_MACCATALYST}" - DEPLOYMENT_VERSION_IOS "${SWIFTLIB_DEPLOYMENT_VERSION_IOS}" - DEPLOYMENT_VERSION_TVOS "${SWIFTLIB_DEPLOYMENT_VERSION_TVOS}" - DEPLOYMENT_VERSION_WATCHOS "${SWIFTLIB_DEPLOYMENT_VERSION_WATCHOS}" - RESULT_VAR_NAME c_compile_flags - MACCATALYST_BUILD_FLAVOR "${SWIFTLIB_SINGLE_MACCATALYST_BUILD_FLAVOR}" - ) - - if(SWIFTLIB_IS_STDLIB) - # We don't ever want to link against the ABI-breakage checking symbols - # in the standard library, runtime, or overlays because they only rely - # on the header parts of LLVM's ADT. - list(APPEND c_compile_flags - "-DLLVM_DISABLE_ABI_BREAKING_CHECKS_ENFORCING=1") - endif() - - if(SWIFTLIB_SINGLE_SDK STREQUAL WINDOWS) - if(libkind STREQUAL SHARED) - list(APPEND c_compile_flags -D_WINDLL) - endif() - endif() - _add_variant_link_flags( - SDK "${SWIFTLIB_SINGLE_SDK}" - ARCH "${SWIFTLIB_SINGLE_ARCHITECTURE}" - BUILD_TYPE "${build_type}" - ENABLE_ASSERTIONS "${enable_assertions}" - ANALYZE_CODE_COVERAGE "${analyze_code_coverage}" - ENABLE_LTO "${lto_type}" - LTO_OBJECT_NAME "${target}-${SWIFTLIB_SINGLE_SDK}-${SWIFTLIB_SINGLE_ARCHITECTURE}" - DEPLOYMENT_VERSION_OSX "${SWIFTLIB_DEPLOYMENT_VERSION_OSX}" - DEPLOYMENT_VERSION_MACCATALYST "${SWIFTLIB_DEPLOYMENT_VERSION_MACCATALYST}" - DEPLOYMENT_VERSION_IOS "${SWIFTLIB_DEPLOYMENT_VERSION_IOS}" - DEPLOYMENT_VERSION_TVOS "${SWIFTLIB_DEPLOYMENT_VERSION_TVOS}" - DEPLOYMENT_VERSION_WATCHOS "${SWIFTLIB_DEPLOYMENT_VERSION_WATCHOS}" - RESULT_VAR_NAME link_flags - LINK_LIBRARIES_VAR_NAME link_libraries - LIBRARY_SEARCH_DIRECTORIES_VAR_NAME library_search_directories - MACCATALYST_BUILD_FLAVOR "${SWIFTLIB_SINGLE_MACCATALYST_BUILD_FLAVOR}" - ) - - # Configure plist creation for OS X. - set(PLIST_INFO_PLIST "Info.plist" CACHE STRING "Plist name") - if("${SWIFTLIB_SINGLE_SDK}" IN_LIST SWIFT_APPLE_PLATFORMS AND SWIFTLIB_SINGLE_IS_STDLIB) - set(PLIST_INFO_NAME ${name}) - set(PLIST_INFO_UTI "com.apple.dt.runtime.${name}") - set(PLIST_INFO_VERSION "${SWIFT_VERSION}") - if (SWIFT_COMPILER_VERSION) - set(PLIST_INFO_BUILD_VERSION - "${SWIFT_COMPILER_VERSION}") - endif() - - set(PLIST_INFO_PLIST_OUT "${PLIST_INFO_PLIST}") - list(APPEND link_flags - "-Wl,-sectcreate,__TEXT,__info_plist,${CMAKE_CURRENT_BINARY_DIR}/${PLIST_INFO_PLIST_OUT}") - configure_file( - "${SWIFT_SOURCE_DIR}/stdlib/${PLIST_INFO_PLIST}.in" - "${PLIST_INFO_PLIST_OUT}" - @ONLY - NEWLINE_STYLE UNIX) - - # If Application Extensions are enabled, pass the linker flag marking - # the dylib as safe. - if (CXX_SUPPORTS_FAPPLICATION_EXTENSION AND (NOT DISABLE_APPLICATION_EXTENSION)) - list(APPEND link_flags "-Wl,-application_extension") - endif() - - set(PLIST_INFO_UTI) - set(PLIST_INFO_NAME) - set(PLIST_INFO_VERSION) - set(PLIST_INFO_BUILD_VERSION) - endif() - - # Set compilation and link flags. - if(SWIFTLIB_SINGLE_SDK STREQUAL WINDOWS) - swift_windows_include_for_arch(${SWIFTLIB_SINGLE_ARCHITECTURE} - ${SWIFTLIB_SINGLE_ARCHITECTURE}_INCLUDE) - target_include_directories(${target} SYSTEM PRIVATE - ${${SWIFTLIB_SINGLE_ARCHITECTURE}_INCLUDE}) - - if(NOT ${CMAKE_C_COMPILER_ID} STREQUAL MSVC) - swift_windows_get_sdk_vfs_overlay(SWIFTLIB_SINGLE_VFS_OVERLAY) - target_compile_options(${target} PRIVATE - "SHELL:-Xclang -ivfsoverlay -Xclang ${SWIFTLIB_SINGLE_VFS_OVERLAY}") - - # MSVC doesn't support -Xclang. We don't need to manually specify - # the dependent libraries as `cl` does so. - target_compile_options(${target} PRIVATE - "SHELL:-Xclang --dependent-lib=oldnames" - # TODO(compnerd) handle /MT, /MTd - "SHELL:-Xclang --dependent-lib=msvcrt$<$:d>") - endif() - endif() - target_include_directories(${target} SYSTEM PRIVATE - ${SWIFT_${SWIFTLIB_SINGLE_SDK}_${SWIFTLIB_SINGLE_ARCHITECTURE}_ICU_UC_INCLUDE} - ${SWIFT_${SWIFTLIB_SINGLE_SDK}_${SWIFTLIB_SINGLE_ARCHITECTURE}_ICU_I18N_INCLUDE}) - target_compile_options(${target} PRIVATE - ${c_compile_flags}) - target_link_options(${target} PRIVATE - ${link_flags}) - if(${SWIFTLIB_SINGLE_SDK} IN_LIST SWIFT_APPLE_PLATFORMS) - target_link_options(${target} PRIVATE - "LINKER:-compatibility_version,1") - if(SWIFT_COMPILER_VERSION) - target_link_options(${target} PRIVATE - "LINKER:-current_version,${SWIFT_COMPILER_VERSION}") - endif() - # Include LLVM Bitcode slices for iOS, Watch OS, and Apple TV OS device libraries. - if(SWIFT_EMBED_BITCODE_SECTION AND NOT SWIFTLIB_SINGLE_DONT_EMBED_BITCODE) - if(${SWIFTLIB_SINGLE_SDK} MATCHES "(I|TV|WATCH)OS") - # The two branches of this if statement accomplish the same end result - # We are simply accounting for the fact that on CMake < 3.16 - # using a generator expression to - # specify a LINKER: argument does not work, - # since that seems not to allow the LINKER: prefix to be - # evaluated (i.e. it will be added as-is to the linker parameters) - if(CMAKE_VERSION VERSION_LESS 3.16) - target_link_options(${target} PRIVATE - "LINKER:-bitcode_bundle" - "LINKER:-lto_library,${LLVM_LIBRARY_DIR}/libLTO.dylib") - - if(SWIFT_EMBED_BITCODE_SECTION_HIDE_SYMBOLS) - target_link_options(${target} PRIVATE - "LINKER:-bitcode_hide_symbols") - endif() - else() - target_link_options(${target} PRIVATE - "LINKER:-bitcode_bundle" - $<$:"LINKER:-bitcode_hide_symbols"> - "LINKER:-lto_library,${LLVM_LIBRARY_DIR}/libLTO.dylib") - endif() - endif() - endif() - endif() - target_link_libraries(${target} PRIVATE - ${link_libraries}) - target_link_directories(${target} PRIVATE - ${library_search_directories}) - - # Adjust the linked libraries for windows targets. On Windows, the link is - # performed against the import library, and the runtime uses the dll. Not - # doing so will result in incorrect symbol resolution and linkage. We created - # import library targets when the library was added. Use that to adjust the - # link libraries. - if(SWIFTLIB_SINGLE_SDK STREQUAL WINDOWS AND NOT CMAKE_SYSTEM_NAME STREQUAL Windows) - foreach(library_list LINK_LIBRARIES PRIVATE_LINK_LIBRARIES) - set(import_libraries) - foreach(library ${SWIFTLIB_SINGLE_${library_list}}) - # Ensure that the library is a target. If an absolute path was given, - # then we do not have an import library associated with it. This occurs - # primarily with ICU (which will be an import library). Import - # libraries are only associated with shared libraries, so add an - # additional check for that as well. - set(import_library ${library}) - if(TARGET ${library}) - get_target_property(type ${library} TYPE) - if(${type} STREQUAL "SHARED_LIBRARY") - set(import_library ${library}_IMPLIB) - endif() - endif() - list(APPEND import_libraries ${import_library}) - endforeach() - set(SWIFTLIB_SINGLE_${library_list} ${import_libraries}) - endforeach() - endif() - - if("${libkind}" STREQUAL "OBJECT") - precondition_list_empty( - "${SWIFTLIB_SINGLE_PRIVATE_LINK_LIBRARIES}" - "OBJECT_LIBRARY may not link to anything") - else() - target_link_libraries("${target}" PRIVATE - ${SWIFTLIB_SINGLE_PRIVATE_LINK_LIBRARIES}) - endif() - - # NOTE(compnerd) use the C linker language to invoke `clang` rather than - # `clang++` as we explicitly link against the C++ runtime. We were previously - # actually passing `-nostdlib++` to avoid the C++ runtime linkage. - if("${SWIFTLIB_SINGLE_SDK}" STREQUAL "ANDROID") - set_property(TARGET "${target}" PROPERTY - LINKER_LANGUAGE "C") - else() - set_property(TARGET "${target}" PROPERTY - LINKER_LANGUAGE "CXX") - endif() - - if(target_static) - target_compile_options(${target_static} PRIVATE - ${c_compile_flags}) - # FIXME: The fallback paths here are going to be dynamic libraries. - - if(SWIFTLIB_INSTALL_WITH_SHARED) - set(search_base_dir ${SWIFTLIB_DIR}) - else() - set(search_base_dir ${SWIFTSTATICLIB_DIR}) - endif() - set(library_search_directories - "${search_base_dir}/${SWIFTLIB_SINGLE_SUBDIR}" - "${SWIFT_NATIVE_SWIFT_TOOLS_PATH}/../lib/swift/${SWIFTLIB_SINGLE_SUBDIR}" - "${SWIFT_NATIVE_SWIFT_TOOLS_PATH}/../lib/swift/${SWIFT_SDK_${SWIFTLIB_SINGLE_SDK}_LIB_SUBDIR}") - target_link_directories(${target_static} PRIVATE - ${library_search_directories}) - target_link_libraries("${target_static}" PRIVATE - ${SWIFTLIB_SINGLE_PRIVATE_LINK_LIBRARIES}) - endif() - - # Do not add code here. -endfunction() - -# Add a new Swift target library. -# -# NOTE: This has not had the swift host code debrided from it yet. That will be -# in a forthcoming commit. -# -# Usage: -# add_swift_target_library(name -# [SHARED] -# [STATIC] -# [DEPENDS dep1 ...] -# [LINK_LIBRARIES dep1 ...] -# [SWIFT_MODULE_DEPENDS dep1 ...] -# [FRAMEWORK_DEPENDS dep1 ...] -# [FRAMEWORK_DEPENDS_WEAK dep1 ...] -# [LLVM_LINK_COMPONENTS comp1 ...] -# [FILE_DEPENDS target1 ...] -# [TARGET_SDKS sdk1...] -# [C_COMPILE_FLAGS flag1...] -# [SWIFT_COMPILE_FLAGS flag1...] -# [LINK_FLAGS flag1...] -# [DONT_EMBED_BITCODE] -# [INSTALL] -# [IS_STDLIB] -# [IS_STDLIB_CORE] -# [INSTALL_WITH_SHARED] -# INSTALL_IN_COMPONENT comp -# DEPLOYMENT_VERSION_OSX version -# DEPLOYMENT_VERSION_MACCATALYST version -# DEPLOYMENT_VERSION_IOS version -# DEPLOYMENT_VERSION_TVOS version -# DEPLOYMENT_VERSION_WATCHOS version -# MACCATALYST_BUILD_FLAVOR flavor -# source1 [source2 source3 ...]) -# -# name -# Name of the library (e.g., swiftParse). -# -# SHARED -# Build a shared library. -# -# STATIC -# Build a static library. -# -# DEPENDS -# Targets that this library depends on. -# -# LINK_LIBRARIES -# Libraries this library depends on. -# -# SWIFT_MODULE_DEPENDS -# Swift modules this library depends on. -# -# SWIFT_MODULE_DEPENDS_OSX -# Swift modules this library depends on when built for OS X. -# -# SWIFT_MODULE_DEPENDS_MACCATALYST -# Zippered Swift modules this library depends on when built for macCatalyst. -# For example, Foundation. -# -# SWIFT_MODULE_DEPENDS_MACCATALYST_UNZIPPERED -# Unzippered Swift modules this library depends on when built for macCatalyst. -# For example, UIKit -# -# SWIFT_MODULE_DEPENDS_IOS -# Swift modules this library depends on when built for iOS. -# -# SWIFT_MODULE_DEPENDS_TVOS -# Swift modules this library depends on when built for tvOS. -# -# SWIFT_MODULE_DEPENDS_WATCHOS -# Swift modules this library depends on when built for watchOS. -# -# SWIFT_MODULE_DEPENDS_FREEBSD -# Swift modules this library depends on when built for FreeBSD. -# -# SWIFT_MODULE_DEPENDS_LINUX -# Swift modules this library depends on when built for Linux. -# -# SWIFT_MODULE_DEPENDS_CYGWIN -# Swift modules this library depends on when built for Cygwin. -# -# SWIFT_MODULE_DEPENDS_HAIKU -# Swift modules this library depends on when built for Haiku. -# -# SWIFT_MODULE_DEPENDS_WASI -# Swift modules this library depends on when built for WASI. -# -# FRAMEWORK_DEPENDS -# System frameworks this library depends on. -# -# FRAMEWORK_DEPENDS_WEAK -# System frameworks this library depends on that should be weak-linked -# -# LLVM_LINK_COMPONENTS -# LLVM components this library depends on. -# -# FILE_DEPENDS -# Additional files this library depends on. -# -# TARGET_SDKS -# The set of SDKs in which this library is included. If empty, the library -# is included in all SDKs. -# -# C_COMPILE_FLAGS -# Extra compiler flags (C, C++, ObjC). -# -# SWIFT_COMPILE_FLAGS -# Extra compiler flags (Swift). -# -# LINK_FLAGS -# Extra linker flags. -# -# DONT_EMBED_BITCODE -# Don't embed LLVM bitcode in this target, even if it is enabled globally. -# -# IS_STDLIB -# Treat the library as a part of the Swift standard library. -# -# IS_STDLIB_CORE -# Compile as the Swift standard library core. -# -# IS_SDK_OVERLAY -# Treat the library as a part of the Swift SDK overlay. -# -# INSTALL_IN_COMPONENT comp -# The Swift installation component that this library belongs to. -# -# DEPLOYMENT_VERSION_OSX -# The minimum deployment version to build for if this is an OSX library. -# -# DEPLOYMENT_VERSION_MACCATALYST -# The minimum deployment version to build for if this is an macCatalyst library. -# -# DEPLOYMENT_VERSION_IOS -# The minimum deployment version to build for if this is an iOS library. -# -# DEPLOYMENT_VERSION_TVOS -# The minimum deployment version to build for if this is an TVOS library. -# -# DEPLOYMENT_VERSION_WATCHOS -# The minimum deployment version to build for if this is an WATCHOS library. -# -# INSTALL_WITH_SHARED -# Install a static library target alongside shared libraries -# -# MACCATALYST_BUILD_FLAVOR -# Possible values are 'ios-like', 'macos-like', 'zippered', 'unzippered-twin' -# Presence of a build flavor requires SWIFT_MODULE_DEPENDS_MACCATALYST to be -# defined and have values. -# -# source1 ... -# Sources to add into this library. -function(add_swift_target_library name) - set(SWIFTLIB_options - DONT_EMBED_BITCODE - HAS_SWIFT_CONTENT - IS_SDK_OVERLAY - IS_STDLIB - IS_STDLIB_CORE - NOSWIFTRT - OBJECT_LIBRARY - SHARED - STATIC - INSTALL_WITH_SHARED) - set(SWIFTLIB_single_parameter_options - DEPLOYMENT_VERSION_IOS - DEPLOYMENT_VERSION_OSX - DEPLOYMENT_VERSION_TVOS - DEPLOYMENT_VERSION_WATCHOS - INSTALL_IN_COMPONENT - DARWIN_INSTALL_NAME_DIR - DEPLOYMENT_VERSION_MACCATALYST - MACCATALYST_BUILD_FLAVOR) - set(SWIFTLIB_multiple_parameter_options - C_COMPILE_FLAGS - DEPENDS - FILE_DEPENDS - FRAMEWORK_DEPENDS - FRAMEWORK_DEPENDS_IOS_TVOS - FRAMEWORK_DEPENDS_OSX - FRAMEWORK_DEPENDS_WEAK - GYB_SOURCES - INCORPORATE_OBJECT_LIBRARIES - INCORPORATE_OBJECT_LIBRARIES_SHARED_ONLY - LINK_FLAGS - LINK_LIBRARIES - LLVM_LINK_COMPONENTS - PRIVATE_LINK_LIBRARIES - SWIFT_COMPILE_FLAGS - SWIFT_COMPILE_FLAGS_IOS - SWIFT_COMPILE_FLAGS_OSX - SWIFT_COMPILE_FLAGS_TVOS - SWIFT_COMPILE_FLAGS_WATCHOS - SWIFT_COMPILE_FLAGS_LINUX - SWIFT_MODULE_DEPENDS - SWIFT_MODULE_DEPENDS_CYGWIN - SWIFT_MODULE_DEPENDS_FREEBSD - SWIFT_MODULE_DEPENDS_HAIKU - SWIFT_MODULE_DEPENDS_IOS - SWIFT_MODULE_DEPENDS_LINUX - SWIFT_MODULE_DEPENDS_OSX - SWIFT_MODULE_DEPENDS_TVOS - SWIFT_MODULE_DEPENDS_WATCHOS - SWIFT_MODULE_DEPENDS_WASI - SWIFT_MODULE_DEPENDS_WINDOWS - SWIFT_MODULE_DEPENDS_FROM_SDK - TARGET_SDKS - SWIFT_COMPILE_FLAGS_MACCATALYST - SWIFT_MODULE_DEPENDS_MACCATALYST - SWIFT_MODULE_DEPENDS_MACCATALYST_UNZIPPERED) - - cmake_parse_arguments(SWIFTLIB - "${SWIFTLIB_options}" - "${SWIFTLIB_single_parameter_options}" - "${SWIFTLIB_multiple_parameter_options}" - ${ARGN}) - set(SWIFTLIB_SOURCES ${SWIFTLIB_UNPARSED_ARGUMENTS}) - - # Ensure it's impossible to build for macCatalyst without module dependencies - if(SWIFT_ENABLE_MACCATALYST AND SWIFTLIB_MACCATALYST_BUILD_FLAVOR) - if((NOT SWIFTLIB_MACCATALYST_BUILD_FLAVOR STREQUAL "zippered") OR - SWIFTLIB_SWIFT_MODULE_DEPENDS_OSX) - precondition(SWIFTLIB_SWIFT_MODULE_DEPENDS_MACCATALYST - MESSAGE "SWIFT_MODULE_DEPENDS_MACCATALYST is required when building for macCatalyst") - endif() - endif() - - # Infer arguments. - - if(SWIFTLIB_IS_SDK_OVERLAY) - set(SWIFTLIB_HAS_SWIFT_CONTENT TRUE) - set(SWIFTLIB_IS_STDLIB TRUE) - endif() - - # Standard library is always a target library. - if(SWIFTLIB_IS_STDLIB) - set(SWIFTLIB_HAS_SWIFT_CONTENT TRUE) - endif() - - # If target SDKs are not specified, build for all known SDKs. - if("${SWIFTLIB_TARGET_SDKS}" STREQUAL "") - set(SWIFTLIB_TARGET_SDKS ${SWIFT_SDKS}) - endif() - list_replace(SWIFTLIB_TARGET_SDKS ALL_APPLE_PLATFORMS "${SWIFT_APPLE_PLATFORMS}") - - # All Swift code depends on the standard library, except for the standard - # library itself. - if(SWIFTLIB_HAS_SWIFT_CONTENT AND NOT SWIFTLIB_IS_STDLIB_CORE) - list(APPEND SWIFTLIB_SWIFT_MODULE_DEPENDS Core) - - # swiftSwiftOnoneSupport does not depend on itself, obviously. - if(NOT ${name} STREQUAL swiftSwiftOnoneSupport) - # All Swift code depends on the SwiftOnoneSupport in non-optimized mode, - # except for the standard library itself. - is_build_type_optimized("${SWIFT_STDLIB_BUILD_TYPE}" optimized) - if(NOT optimized) - list(APPEND SWIFTLIB_SWIFT_MODULE_DEPENDS SwiftOnoneSupport) - endif() - endif() - endif() - - if((NOT "${SWIFT_BUILD_STDLIB}") AND - (NOT "${SWIFTLIB_SWIFT_MODULE_DEPENDS}" STREQUAL "")) - list(REMOVE_ITEM SWIFTLIB_SWIFT_MODULE_DEPENDS Core SwiftOnoneSupport) - endif() - - translate_flags(SWIFTLIB "${SWIFTLIB_options}") - precondition(SWIFTLIB_INSTALL_IN_COMPONENT MESSAGE "INSTALL_IN_COMPONENT is required") - - if(NOT SWIFTLIB_SHARED AND - NOT SWIFTLIB_STATIC AND - NOT SWIFTLIB_OBJECT_LIBRARY) - message(FATAL_ERROR - "Either SHARED, STATIC, or OBJECT_LIBRARY must be specified") - endif() - - # In the standard library and overlays, warn about implicit overrides - # as a reminder to consider when inherited protocols need different - # behavior for their requirements. - if (SWIFTLIB_IS_STDLIB) - list(APPEND SWIFTLIB_SWIFT_COMPILE_FLAGS "-warn-implicit-overrides") - endif() - - if(NOT SWIFT_BUILD_RUNTIME_WITH_HOST_COMPILER AND NOT BUILD_STANDALONE) - list(APPEND SWIFTLIB_DEPENDS clang) - endif() - - # If we are building this library for targets, loop through the various - # SDKs building the variants of this library. - list_intersect( - "${SWIFTLIB_TARGET_SDKS}" "${SWIFT_SDKS}" SWIFTLIB_TARGET_SDKS) - - foreach(sdk ${SWIFTLIB_TARGET_SDKS}) - if(NOT SWIFT_SDK_${sdk}_ARCHITECTURES) - # SWIFT_SDK_${sdk}_ARCHITECTURES is empty, so just continue - continue() - endif() - - # Skip building library for macOS if macCatalyst support is not enabled and the - # library only builds for macOS when macCatalyst is enabled. - if(NOT SWIFT_ENABLE_MACCATALYST AND - sdk STREQUAL "OSX" AND - SWIFTLIB_MACCATALYST_BUILD_FLAVOR STREQUAL "ios-like") - message(STATUS "Skipping OSX SDK for module ${name}") - continue() - endif() - - # Determine if/what macCatalyst build flavor we are - get_maccatalyst_build_flavor(maccatalyst_build_flavor - "${sdk}" "${SWIFTLIB_MACCATALYST_BUILD_FLAVOR}") - - set(maccatalyst_build_flavors) - if(NOT DEFINED maccatalyst_build_flavor) - list(APPEND maccatalyst_build_flavors "none") - elseif(maccatalyst_build_flavor STREQUAL "unzippered-twin") - list(APPEND maccatalyst_build_flavors "macos-like" "ios-like") - else() - list(APPEND maccatalyst_build_flavors "${maccatalyst_build_flavor}") - endif() - - # Loop over the build flavors for the this library. If it is an unzippered - # twin we'll build it twice: once for "macos-like" and once for "ios-like" - # flavors. - foreach(maccatalyst_build_flavor ${maccatalyst_build_flavors}) - if(maccatalyst_build_flavor STREQUAL "none") - unset(maccatalyst_build_flavor) - endif() - - set(THIN_INPUT_TARGETS) - - # Collect architecture agnostic SDK module dependencies - set(swiftlib_module_depends_flattened ${SWIFTLIB_SWIFT_MODULE_DEPENDS}) - if(${sdk} STREQUAL OSX) - if(DEFINED maccatalyst_build_flavor AND NOT maccatalyst_build_flavor STREQUAL "macos-like") - list(APPEND swiftlib_module_depends_flattened - ${SWIFTLIB_SWIFT_MODULE_DEPENDS_MACCATALYST}) - list(APPEND swiftlib_module_depends_flattened - ${SWIFTLIB_SWIFT_MODULE_DEPENDS_MACCATALYST_UNZIPPERED}) - else() - list(APPEND swiftlib_module_depends_flattened - ${SWIFTLIB_SWIFT_MODULE_DEPENDS_OSX}) - endif() - list(APPEND swiftlib_module_depends_flattened - ${SWIFTLIB_SWIFT_MODULE_DEPENDS_OSX}) - elseif(${sdk} STREQUAL IOS OR ${sdk} STREQUAL IOS_SIMULATOR) - list(APPEND swiftlib_module_depends_flattened - ${SWIFTLIB_SWIFT_MODULE_DEPENDS_IOS}) - elseif(${sdk} STREQUAL TVOS OR ${sdk} STREQUAL TVOS_SIMULATOR) - list(APPEND swiftlib_module_depends_flattened - ${SWIFTLIB_SWIFT_MODULE_DEPENDS_TVOS}) - elseif(${sdk} STREQUAL WATCHOS OR ${sdk} STREQUAL WATCHOS_SIMULATOR) - list(APPEND swiftlib_module_depends_flattened - ${SWIFTLIB_SWIFT_MODULE_DEPENDS_WATCHOS}) - elseif(${sdk} STREQUAL FREEBSD) - list(APPEND swiftlib_module_depends_flattened - ${SWIFTLIB_SWIFT_MODULE_DEPENDS_FREEBSD}) - elseif(${sdk} STREQUAL LINUX OR ${sdk} STREQUAL ANDROID) - list(APPEND swiftlib_module_depends_flattened - ${SWIFTLIB_SWIFT_MODULE_DEPENDS_LINUX}) - elseif(${sdk} STREQUAL CYGWIN) - list(APPEND swiftlib_module_depends_flattened - ${SWIFTLIB_SWIFT_MODULE_DEPENDS_CYGWIN}) - elseif(${sdk} STREQUAL HAIKU) - list(APPEND swiftlib_module_depends_flattened - ${SWIFTLIB_SWIFT_MODULE_DEPENDS_HAIKU}) - elseif(${sdk} STREQUAL WASI) - list(APPEND swiftlib_module_depends_flattened - ${SWIFTLIB_SWIFT_MODULE_DEPENDS_WASI}) - elseif(${sdk} STREQUAL WINDOWS) - list(APPEND swiftlib_module_depends_flattened - ${SWIFTLIB_SWIFT_MODULE_DEPENDS_WINDOWS}) - endif() - - # Collect architecture agnostic SDK framework dependencies - set(swiftlib_framework_depends_flattened ${SWIFTLIB_FRAMEWORK_DEPENDS}) - if(${sdk} STREQUAL OSX) - list(APPEND swiftlib_framework_depends_flattened - ${SWIFTLIB_FRAMEWORK_DEPENDS_OSX}) - elseif(${sdk} STREQUAL IOS OR ${sdk} STREQUAL IOS_SIMULATOR OR - ${sdk} STREQUAL TVOS OR ${sdk} STREQUAL TVOS_SIMULATOR) - list(APPEND swiftlib_framework_depends_flattened - ${SWIFTLIB_FRAMEWORK_DEPENDS_IOS_TVOS}) - endif() - - # Collect architecutre agnostic compiler flags - set(swiftlib_swift_compile_flags_all ${SWIFTLIB_SWIFT_COMPILE_FLAGS}) - if(${sdk} STREQUAL OSX) - list(APPEND swiftlib_swift_compile_flags_all - ${SWIFTLIB_SWIFT_COMPILE_FLAGS_OSX}) - elseif(${sdk} STREQUAL IOS OR ${sdk} STREQUAL IOS_SIMULATOR) - list(APPEND swiftlib_swift_compile_flags_all - ${SWIFTLIB_SWIFT_COMPILE_FLAGS_IOS}) - elseif(${sdk} STREQUAL TVOS OR ${sdk} STREQUAL TVOS_SIMULATOR) - list(APPEND swiftlib_swift_compile_flags_all - ${SWIFTLIB_SWIFT_COMPILE_FLAGS_TVOS}) - elseif(${sdk} STREQUAL WATCHOS OR ${sdk} STREQUAL WATCHOS_SIMULATOR) - list(APPEND swiftlib_swift_compile_flags_all - ${SWIFTLIB_SWIFT_COMPILE_FLAGS_WATCHOS}) - elseif(${sdk} STREQUAL LINUX) - list(APPEND swiftlib_swift_compile_flags_all - ${SWIFTLIB_SWIFT_COMPILE_FLAGS_LINUX}) - elseif(${sdk} STREQUAL WINDOWS) - # FIXME(SR2005) static and shared are not mutually exclusive; however - # since we do a single build of the sources, this doesn't work for - # building both simultaneously. Effectively, only shared builds are - # supported on windows currently. - if(SWIFTLIB_SHARED) - list(APPEND swiftlib_swift_compile_flags_all -D_WINDLL) - if(SWIFTLIB_IS_STDLIB_CORE) - list(APPEND swiftlib_swift_compile_flags_all -DswiftCore_EXPORTS) - endif() - elseif(SWIFTLIB_STATIC) - list(APPEND swiftlib_swift_compile_flags_all -D_LIB) - endif() - endif() - - - # Collect architecture agnostic SDK linker flags - set(swiftlib_link_flags_all ${SWIFTLIB_LINK_FLAGS}) - if(${sdk} STREQUAL IOS_SIMULATOR AND ${name} STREQUAL swiftMediaPlayer) - # message("DISABLING AUTOLINK FOR swiftMediaPlayer") - list(APPEND swiftlib_link_flags_all "-Xlinker" "-ignore_auto_link") - endif() - - # We unconditionally removed "-z,defs" from CMAKE_SHARED_LINKER_FLAGS in - # swift_common_standalone_build_config_llvm within - # SwiftSharedCMakeConfig.cmake, where it was added by a call to - # HandleLLVMOptions. - # - # Rather than applying it to all targets and libraries, we here add it - # back to supported targets and libraries only. This is needed for ELF - # targets only; however, RemoteMirror needs to build with undefined - # symbols. - if(${SWIFT_SDK_${sdk}_OBJECT_FORMAT} STREQUAL ELF AND - NOT ${name} STREQUAL swiftRemoteMirror) - list(APPEND swiftlib_link_flags_all "-Wl,-z,defs") - endif() - # Setting back linker flags which are not supported when making Android build on macOS cross-compile host. - if(SWIFTLIB_SHARED) - if(sdk IN_LIST SWIFT_APPLE_PLATFORMS) - list(APPEND swiftlib_link_flags_all "-dynamiclib -Wl,-headerpad_max_install_names") - elseif(${sdk} STREQUAL ANDROID) - list(APPEND swiftlib_link_flags_all "-shared") - # TODO: Instead of `lib${name}.so` find variable or target property which already have this value. - list(APPEND swiftlib_link_flags_all "-Wl,-soname,lib${name}.so") - endif() - endif() - - set(sdk_supported_archs - ${SWIFT_SDK_${sdk}_ARCHITECTURES} - ${SWIFT_SDK_${sdk}_MODULE_ARCHITECTURES}) - list(REMOVE_DUPLICATES sdk_supported_archs) - - # For each architecture supported by this SDK - foreach(arch ${sdk_supported_archs}) - # Configure variables for this subdirectory. - set(VARIANT_SUFFIX "-${SWIFT_SDK_${sdk}_LIB_SUBDIR}-${arch}") - set(VARIANT_NAME "${name}${VARIANT_SUFFIX}") - set(MODULE_VARIANT_SUFFIX "-swiftmodule${VARIANT_SUFFIX}") - set(MODULE_VARIANT_NAME "${name}${MODULE_VARIANT_SUFFIX}") - - # Configure macCatalyst flavor variables - if(DEFINED maccatalyst_build_flavor) - set(maccatalyst_variant_suffix "-${SWIFT_SDK_MACCATALYST_LIB_SUBDIR}-${arch}") - set(maccatalyst_variant_name "${name}${maccatalyst_variant_suffix}") - - set(maccatalyst_module_variant_suffix "-swiftmodule${maccatalyst_variant_suffix}") - set(maccatalyst_module_variant_name "${name}${maccatalyst_module_variant_suffix}") - endif() - - # Map dependencies over to the appropriate variants. - set(swiftlib_link_libraries) - foreach(lib ${SWIFTLIB_LINK_LIBRARIES}) - if(TARGET "${lib}${VARIANT_SUFFIX}") - list(APPEND swiftlib_link_libraries "${lib}${VARIANT_SUFFIX}") - else() - list(APPEND swiftlib_link_libraries "${lib}") - endif() - endforeach() - - # Swift compiles depend on swift modules, while links depend on - # linked libraries. Find targets for both of these here. - set(swiftlib_module_dependency_targets) - set(swiftlib_private_link_libraries_targets) - - if(NOT BUILD_STANDALONE) - foreach(mod ${swiftlib_module_depends_flattened}) - if(DEFINED maccatalyst_build_flavor) - if(maccatalyst_build_flavor STREQUAL "zippered") - # Zippered libraries are dependent on both the macCatalyst and normal macOS - # modules of their dependencies (which themselves must be zippered). - list(APPEND swiftlib_module_dependency_targets - "swift${mod}${maccatalyst_module_variant_suffix}") - list(APPEND swiftlib_module_dependency_targets - "swift${mod}${MODULE_VARIANT_SUFFIX}") - - # Zippered libraries link against their zippered library targets, which - # live (and are built in) the same location as normal macOS libraries. - list(APPEND swiftlib_private_link_libraries_targets - "swift${mod}${VARIANT_SUFFIX}") - elseif(maccatalyst_build_flavor STREQUAL "ios-like") - # iOS-like libraries depend on the macCatalyst modules of their dependencies - # regardless of whether the target is zippered or macCatalyst only. - list(APPEND swiftlib_module_dependency_targets - "swift${mod}${maccatalyst_module_variant_suffix}") - - # iOS-like libraries can link against either iOS-like library targets - # or zippered targets. - if(mod IN_LIST SWIFTLIB_SWIFT_MODULE_DEPENDS_MACCATALYST_UNZIPPERED) - list(APPEND swiftlib_private_link_libraries_targets - "swift${mod}${maccatalyst_variant_suffix}") - else() - list(APPEND swiftlib_private_link_libraries_targets - "swift${mod}${VARIANT_SUFFIX}") - endif() - else() - list(APPEND swiftlib_module_dependency_targets - "swift${mod}${MODULE_VARIANT_SUFFIX}") - - list(APPEND swiftlib_private_link_libraries_targets - "swift${mod}${VARIANT_SUFFIX}") - endif() - continue() - endif() - - list(APPEND swiftlib_module_dependency_targets - "swift${mod}${MODULE_VARIANT_SUFFIX}") - - list(APPEND swiftlib_private_link_libraries_targets - "swift${mod}${VARIANT_SUFFIX}") - endforeach() - endif() - - foreach(lib ${SWIFTLIB_PRIVATE_LINK_LIBRARIES}) - if(TARGET "${lib}${VARIANT_SUFFIX}") - list(APPEND swiftlib_private_link_libraries_targets - "${lib}${VARIANT_SUFFIX}") - else() - list(APPEND swiftlib_private_link_libraries_targets "${lib}") - endif() - endforeach() - - # Add PrivateFrameworks, rdar://28466433 - set(swiftlib_c_compile_flags_all ${SWIFTLIB_C_COMPILE_FLAGS}) - set(swiftlib_link_flags_all ${SWIFTLIB_LINK_FLAGS}) - - # Add flags to prepend framework search paths for the parallel framework - # hierarchy rooted at /System/iOSSupport/... - # These paths must come before their normal counterparts so that when compiling - # macCatalyst-only or unzippered-twin overlays the macCatalyst version - # of a framework is found and not the Mac version. - if(maccatalyst_build_flavor STREQUAL "ios-like" - OR (name STREQUAL "swiftXCTest" - AND maccatalyst_build_flavor STREQUAL "zippered")) - - # The path to find iOS-only frameworks (such as UIKit) under macCatalyst. - set(ios_support_frameworks_path "${SWIFT_SDK_${sdk}_PATH}/System/iOSSupport/System/Library/Frameworks/") - - list(APPEND swiftlib_swift_compile_flags_all "-Fsystem" "${ios_support_frameworks_path}") - list(APPEND swiftlib_c_compile_flags_all "-iframework" "${ios_support_frameworks_path}") - # We collate -F with the framework path to avoid unwanted deduplication - # of options by target_compile_options -- this way no undesired - # side effects are introduced should a new search path be added. - list(APPEND swiftlib_link_flags_all "-F${ios_support_frameworks_path}") - endif() - - if(sdk IN_LIST SWIFT_APPLE_PLATFORMS AND SWIFTLIB_IS_SDK_OVERLAY) - set(swiftlib_swift_compile_private_frameworks_flag "-Fsystem" "${SWIFT_SDK_${sdk}_ARCH_${arch}_PATH}/System/Library/PrivateFrameworks/") - foreach(tbd_lib ${SWIFTLIB_SWIFT_MODULE_DEPENDS_FROM_SDK}) - list(APPEND swiftlib_link_flags_all "${SWIFT_SDK_${sdk}_ARCH_${arch}_PATH}/usr/lib/swift/libswift${tbd_lib}.tbd") - endforeach() - endif() - - set(variant_name "${VARIANT_NAME}") - set(module_variant_names "${MODULE_VARIANT_NAME}") - if(maccatalyst_build_flavor STREQUAL "ios-like") - set(variant_name "${maccatalyst_variant_name}") - set(module_variant_names "${maccatalyst_module_variant_name}") - elseif(maccatalyst_build_flavor STREQUAL "zippered") - # Zippered libraries produce two modules: one for macCatalyst and one for macOS - # and so need two module targets. - list(APPEND module_variant_names "${maccatalyst_module_variant_name}") - endif() - - list(APPEND swiftlib_c_compile_flags_all "-DSWIFT_TARGET_LIBRARY_NAME=${name}") - - # Add this library variant. - _add_swift_target_library_single( - ${variant_name} - ${name} - ${SWIFTLIB_SHARED_keyword} - ${SWIFTLIB_STATIC_keyword} - ${SWIFTLIB_OBJECT_LIBRARY_keyword} - ${SWIFTLIB_INSTALL_WITH_SHARED_keyword} - ${SWIFTLIB_SOURCES} - TARGET_LIBRARY - MODULE_TARGETS ${module_variant_names} - SDK ${sdk} - ARCHITECTURE ${arch} - DEPENDS ${SWIFTLIB_DEPENDS} - LINK_LIBRARIES ${swiftlib_link_libraries} - FRAMEWORK_DEPENDS ${swiftlib_framework_depends_flattened} - FRAMEWORK_DEPENDS_WEAK ${SWIFTLIB_FRAMEWORK_DEPENDS_WEAK} - LLVM_LINK_COMPONENTS ${SWIFTLIB_LLVM_LINK_COMPONENTS} - FILE_DEPENDS ${SWIFTLIB_FILE_DEPENDS} ${swiftlib_module_dependency_targets} - C_COMPILE_FLAGS ${swiftlib_c_compile_flags_all} - SWIFT_COMPILE_FLAGS ${swiftlib_swift_compile_flags_all} ${swiftlib_swift_compile_flags_arch} ${swiftlib_swift_compile_private_frameworks_flag} - LINK_FLAGS ${swiftlib_link_flags_all} - PRIVATE_LINK_LIBRARIES ${swiftlib_private_link_libraries_targets} - INCORPORATE_OBJECT_LIBRARIES ${SWIFTLIB_INCORPORATE_OBJECT_LIBRARIES} - INCORPORATE_OBJECT_LIBRARIES_SHARED_ONLY ${SWIFTLIB_INCORPORATE_OBJECT_LIBRARIES_SHARED_ONLY} - ${SWIFTLIB_DONT_EMBED_BITCODE_keyword} - ${SWIFTLIB_IS_STDLIB_keyword} - ${SWIFTLIB_IS_STDLIB_CORE_keyword} - ${SWIFTLIB_IS_SDK_OVERLAY_keyword} - ${SWIFTLIB_NOSWIFTRT_keyword} - DARWIN_INSTALL_NAME_DIR "${SWIFTLIB_DARWIN_INSTALL_NAME_DIR}" - INSTALL_IN_COMPONENT "${SWIFTLIB_INSTALL_IN_COMPONENT}" - DEPLOYMENT_VERSION_OSX "${SWIFTLIB_DEPLOYMENT_VERSION_OSX}" - DEPLOYMENT_VERSION_MACCATALYST "${SWIFTLIB_DEPLOYMENT_VERSION_MACCATALYST}" - DEPLOYMENT_VERSION_IOS "${SWIFTLIB_DEPLOYMENT_VERSION_IOS}" - DEPLOYMENT_VERSION_TVOS "${SWIFTLIB_DEPLOYMENT_VERSION_TVOS}" - DEPLOYMENT_VERSION_WATCHOS "${SWIFTLIB_DEPLOYMENT_VERSION_WATCHOS}" - MACCATALYST_BUILD_FLAVOR "${maccatalyst_build_flavor}" - - GYB_SOURCES ${SWIFTLIB_GYB_SOURCES} - ) - if(NOT SWIFT_BUILT_STANDALONE AND NOT "${CMAKE_C_COMPILER_ID}" MATCHES "Clang") - add_dependencies(${VARIANT_NAME} clang) - endif() - - if(sdk STREQUAL WINDOWS) - if(SWIFT_COMPILER_IS_MSVC_LIKE) - if (SWIFT_STDLIB_MSVC_RUNTIME_LIBRARY MATCHES MultiThreadedDebugDLL) - target_compile_options(${VARIANT_NAME} PRIVATE /MDd /D_DLL /D_DEBUG) - elseif (SWIFT_STDLIB_MSVC_RUNTIME_LIBRARY MATCHES MultiThreadedDebug) - target_compile_options(${VARIANT_NAME} PRIVATE /MTd /U_DLL /D_DEBUG) - elseif (SWIFT_STDLIB_MSVC_RUNTIME_LIBRARY MATCHES MultiThreadedDLL) - target_compile_options(${VARIANT_NAME} PRIVATE /MD /D_DLL /U_DEBUG) - elseif (SWIFT_STDLIB_MSVC_RUNTIME_LIBRARY MATCHES MultiThreaded) - target_compile_options(${VARIANT_NAME} PRIVATE /MT /U_DLL /U_DEBUG) - endif() - endif() - endif() - - if(NOT SWIFTLIB_OBJECT_LIBRARY) - # Add dependencies on the (not-yet-created) custom lipo target. - foreach(DEP ${SWIFTLIB_LINK_LIBRARIES}) - if (NOT "${DEP}" STREQUAL "icucore") - add_dependencies(${VARIANT_NAME} - "${DEP}-${SWIFT_SDK_${sdk}_LIB_SUBDIR}") - endif() - endforeach() - - if (SWIFTLIB_IS_STDLIB AND SWIFTLIB_STATIC) - # Add dependencies on the (not-yet-created) custom lipo target. - foreach(DEP ${SWIFTLIB_LINK_LIBRARIES}) - if (NOT "${DEP}" STREQUAL "icucore") - add_dependencies("${VARIANT_NAME}-static" - "${DEP}-${SWIFT_SDK_${sdk}_LIB_SUBDIR}-static") - endif() - endforeach() - endif() - - if(arch IN_LIST SWIFT_SDK_${sdk}_ARCHITECTURES) - # Note this thin library. - list(APPEND THIN_INPUT_TARGETS ${VARIANT_NAME}) - endif() - endif() - endforeach() - - # Configure module-only targets - if(NOT SWIFT_SDK_${sdk}_ARCHITECTURES - AND SWIFT_SDK_${sdk}_MODULE_ARCHITECTURES) - set(_target "${name}-${SWIFT_SDK_${sdk}_LIB_SUBDIR}") - - # Create unified sdk target - add_custom_target("${_target}") - - foreach(_arch ${SWIFT_SDK_${sdk}_MODULE_ARCHITECTURES}) - set(_variant_suffix "-${SWIFT_SDK_${sdk}_LIB_SUBDIR}-${_arch}") - set(_module_variant_name "${name}-swiftmodule-${_variant_suffix}") - - add_dependencies("${_target}" ${_module_variant_name}) - - # Add Swift standard library targets as dependencies to the top-level - # convenience target. - if(TARGET "swift-stdlib${_variant_suffix}") - add_dependencies("swift-stdlib${_variant_suffix}" - "${_target}") - endif() - endforeach() - - return() - endif() - - set(library_subdir "${SWIFT_SDK_${sdk}_LIB_SUBDIR}") - if(maccatalyst_build_flavor STREQUAL "ios-like") - set(library_subdir "${SWIFT_SDK_MACCATALYST_LIB_SUBDIR}") - endif() - - if(NOT SWIFTLIB_OBJECT_LIBRARY) - # Determine the name of the universal library. - if(SWIFTLIB_SHARED) - if("${sdk}" STREQUAL "WINDOWS") - set(UNIVERSAL_LIBRARY_NAME - "${SWIFTLIB_DIR}/${library_subdir}/${name}.dll") - else() - set(UNIVERSAL_LIBRARY_NAME - "${SWIFTLIB_DIR}/${library_subdir}/${CMAKE_SHARED_LIBRARY_PREFIX}${name}${CMAKE_SHARED_LIBRARY_SUFFIX}") - endif() - else() - if("${sdk}" STREQUAL "WINDOWS") - set(UNIVERSAL_LIBRARY_NAME - "${SWIFTLIB_DIR}/${library_subdir}/${name}.lib") - else() - set(UNIVERSAL_LIBRARY_NAME - "${SWIFTLIB_DIR}/${library_subdir}/${CMAKE_STATIC_LIBRARY_PREFIX}${name}${CMAKE_STATIC_LIBRARY_SUFFIX}") - endif() - endif() - - set(lipo_target "${name}-${library_subdir}") - if("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin" AND SWIFTLIB_SHARED) - set(codesign_arg CODESIGN) - endif() - precondition(THIN_INPUT_TARGETS) - _add_swift_lipo_target(SDK - ${sdk} - TARGET - ${lipo_target} - OUTPUT - ${UNIVERSAL_LIBRARY_NAME} - ${codesign_arg} - ${THIN_INPUT_TARGETS}) - - # Cache universal libraries for dependency purposes - set(UNIVERSAL_LIBRARY_NAMES_${library_subdir} - ${UNIVERSAL_LIBRARY_NAMES_${library_subdir}} - ${lipo_target} - CACHE INTERNAL "UNIVERSAL_LIBRARY_NAMES_${library_subdir}") - - # Determine the subdirectory where this library will be installed. - set(resource_dir_sdk_subdir "${SWIFT_SDK_${sdk}_LIB_SUBDIR}") - if(maccatalyst_build_flavor STREQUAL "ios-like") - set(resource_dir_sdk_subdir "${SWIFT_SDK_MACCATALYST_LIB_SUBDIR}") - endif() - - precondition(resource_dir_sdk_subdir) - - if(SWIFTLIB_SHARED OR SWIFTLIB_INSTALL_WITH_SHARED) - set(resource_dir "swift") - set(file_permissions - OWNER_READ OWNER_WRITE OWNER_EXECUTE - GROUP_READ GROUP_EXECUTE - WORLD_READ WORLD_EXECUTE) - else() - set(resource_dir "swift_static") - set(file_permissions - OWNER_READ OWNER_WRITE - GROUP_READ - WORLD_READ) - endif() - - set(optional_arg) - if(sdk IN_LIST SWIFT_APPLE_PLATFORMS) - # Allow installation of stdlib without building all variants on Darwin. - set(optional_arg "OPTIONAL") - endif() - - if(sdk STREQUAL WINDOWS AND CMAKE_SYSTEM_NAME STREQUAL Windows) - add_dependencies(${SWIFTLIB_INSTALL_IN_COMPONENT} ${name}-windows-${SWIFT_PRIMARY_VARIANT_ARCH}) - swift_install_in_component(TARGETS ${name}-windows-${SWIFT_PRIMARY_VARIANT_ARCH} - RUNTIME - DESTINATION "bin" - COMPONENT "${SWIFTLIB_INSTALL_IN_COMPONENT}" - LIBRARY - DESTINATION "lib${LLVM_LIBDIR_SUFFIX}/${resource_dir}/${resource_dir_sdk_subdir}/${SWIFT_PRIMARY_VARIANT_ARCH}" - COMPONENT "${SWIFTLIB_INSTALL_IN_COMPONENT}" - ARCHIVE - DESTINATION "lib${LLVM_LIBDIR_SUFFIX}/${resource_dir}/${resource_dir_sdk_subdir}/${SWIFT_PRIMARY_VARIANT_ARCH}" - COMPONENT "${SWIFTLIB_INSTALL_IN_COMPONENT}" - PERMISSIONS ${file_permissions}) - else() - # NOTE: ${UNIVERSAL_LIBRARY_NAME} is the output associated with the target - # ${lipo_target} - add_dependencies(${SWIFTLIB_INSTALL_IN_COMPONENT} ${lipo_target}) - swift_install_in_component(FILES "${UNIVERSAL_LIBRARY_NAME}" - DESTINATION "lib${LLVM_LIBDIR_SUFFIX}/${resource_dir}/${resource_dir_sdk_subdir}" - COMPONENT "${SWIFTLIB_INSTALL_IN_COMPONENT}" - PERMISSIONS ${file_permissions} - "${optional_arg}") - endif() - if(sdk STREQUAL WINDOWS) - foreach(arch ${SWIFT_SDK_WINDOWS_ARCHITECTURES}) - if(TARGET ${name}-windows-${arch}_IMPLIB) - get_target_property(import_library ${name}-windows-${arch}_IMPLIB IMPORTED_LOCATION) - add_dependencies(${SWIFTLIB_INSTALL_IN_COMPONENT} ${name}-windows-${arch}_IMPLIB) - swift_install_in_component(FILES ${import_library} - DESTINATION "lib${LLVM_LIBDIR_SUFFIX}/${resource_dir}/${resource_dir_sdk_subdir}/${arch}" - COMPONENT ${SWIFTLIB_INSTALL_IN_COMPONENT} - PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ) - endif() - endforeach() - endif() - - swift_is_installing_component( - "${SWIFTLIB_INSTALL_IN_COMPONENT}" - is_installing) - - # Add the arch-specific library targets to the global exports. - foreach(arch ${SWIFT_SDK_${sdk}_ARCHITECTURES}) - set(_variant_name "${name}-${SWIFT_SDK_${sdk}_LIB_SUBDIR}-${arch}") - if(maccatalyst_build_flavor STREQUAL "ios-like") - set(_variant_name "${name}-${SWIFT_SDK_MACCATALYST_LIB_SUBDIR}-${arch}") - endif() - - if(NOT TARGET "${_variant_name}") - continue() - endif() - - if(is_installing) - set_property(GLOBAL APPEND - PROPERTY SWIFT_EXPORTS ${_variant_name}) - else() - set_property(GLOBAL APPEND - PROPERTY SWIFT_BUILDTREE_EXPORTS ${_variant_name}) - endif() - endforeach() - - # Add the swiftmodule-only targets to the lipo target depdencies. - foreach(arch ${SWIFT_SDK_${sdk}_MODULE_ARCHITECTURES}) - set(_variant_name "${name}-${SWIFT_SDK_${sdk}_LIB_SUBDIR}-${arch}") - if(maccatalyst_build_flavor STREQUAL "ios-like") - set(_variant_name "${name}-${SWIFT_SDK_MACCATALYST_LIB_SUBDIR}-${arch}") - endif() - - if(NOT TARGET "${_variant_name}") - continue() - endif() - - add_dependencies("${lipo_target}" "${_variant_name}") - endforeach() - - # If we built static variants of the library, create a lipo target for - # them. - set(lipo_target_static) - if (SWIFTLIB_IS_STDLIB AND SWIFTLIB_STATIC) - set(THIN_INPUT_TARGETS_STATIC) - foreach(TARGET ${THIN_INPUT_TARGETS}) - list(APPEND THIN_INPUT_TARGETS_STATIC "${TARGET}-static") - endforeach() - - if(SWIFTLIB_INSTALL_WITH_SHARED) - set(install_subdir "swift") - set(universal_subdir ${SWIFTLIB_DIR}) - else() - set(install_subdir "swift_static") - set(universal_subdir ${SWIFTSTATICLIB_DIR}) - endif() - - set(lipo_target_static - "${name}-${library_subdir}-static") - set(UNIVERSAL_LIBRARY_NAME - "${universal_subdir}/${library_subdir}/${CMAKE_STATIC_LIBRARY_PREFIX}${name}${CMAKE_STATIC_LIBRARY_SUFFIX}") - _add_swift_lipo_target(SDK - ${sdk} - TARGET - ${lipo_target_static} - OUTPUT - "${UNIVERSAL_LIBRARY_NAME}" - ${THIN_INPUT_TARGETS_STATIC}) - add_dependencies(${SWIFTLIB_INSTALL_IN_COMPONENT} ${lipo_target_static}) - swift_install_in_component(FILES "${UNIVERSAL_LIBRARY_NAME}" - DESTINATION "lib${LLVM_LIBDIR_SUFFIX}/${install_subdir}/${resource_dir_sdk_subdir}" - PERMISSIONS - OWNER_READ OWNER_WRITE - GROUP_READ - WORLD_READ - COMPONENT "${SWIFTLIB_INSTALL_IN_COMPONENT}" - "${optional_arg}") - endif() - - # Add Swift standard library targets as dependencies to the top-level - # convenience target. - set(FILTERED_UNITTESTS - swiftStdlibCollectionUnittest - swiftStdlibUnicodeUnittest) - - foreach(arch ${SWIFT_SDK_${sdk}_ARCHITECTURES}) - set(VARIANT_SUFFIX "-${SWIFT_SDK_${sdk}_LIB_SUBDIR}-${arch}") - if(TARGET "swift-stdlib${VARIANT_SUFFIX}" AND - TARGET "swift-test-stdlib${VARIANT_SUFFIX}") - add_dependencies("swift-stdlib${VARIANT_SUFFIX}" - ${lipo_target} - ${lipo_target_static}) - if(NOT "${name}" IN_LIST FILTERED_UNITTESTS) - add_dependencies("swift-test-stdlib${VARIANT_SUFFIX}" - ${lipo_target} - ${lipo_target_static}) - endif() - endif() - endforeach() - endif() - endforeach() # maccatalyst_build_flavors - endforeach() -endfunction() - # Add an executable compiled for a given variant. # # Don't use directly, use add_swift_executable and add_swift_target_executable diff --git a/cmake/modules/SwiftSource.cmake b/cmake/modules/SwiftSource.cmake index a68f9ef423140..9da9b4ffd9161 100644 --- a/cmake/modules/SwiftSource.cmake +++ b/cmake/modules/SwiftSource.cmake @@ -140,6 +140,120 @@ function(add_swift_source_group sources) HEADER_FILE_ONLY true) endfunction() +# Look up extra flags for a module that matches a regexp. +function(_add_extra_swift_flags_for_module module_name result_var_name) + set(result_list) + list(LENGTH SWIFT_EXPERIMENTAL_EXTRA_REGEXP_FLAGS listlen) + if (${listlen} GREATER 0) + math(EXPR listlen "${listlen}-1") + foreach(i RANGE 0 ${listlen} 2) + list(GET SWIFT_EXPERIMENTAL_EXTRA_REGEXP_FLAGS ${i} regex) + if (module_name MATCHES "${regex}") + math(EXPR ip1 "${i}+1") + list(GET SWIFT_EXPERIMENTAL_EXTRA_REGEXP_FLAGS ${ip1} flags) + list(APPEND result_list ${flags}) + message(STATUS "Matched '${regex}' to module '${module_name}'. Compiling ${module_name} with special flags: ${flags}") + endif() + endforeach() + endif() + list(LENGTH SWIFT_EXPERIMENTAL_EXTRA_NEGATIVE_REGEXP_FLAGS listlen) + if (${listlen} GREATER 0) + math(EXPR listlen "${listlen}-1") + foreach(i RANGE 0 ${listlen} 2) + list(GET SWIFT_EXPERIMENTAL_EXTRA_NEGATIVE_REGEXP_FLAGS ${i} regex) + if (NOT module_name MATCHES "${regex}") + math(EXPR ip1 "${i}+1") + list(GET SWIFT_EXPERIMENTAL_EXTRA_NEGATIVE_REGEXP_FLAGS ${ip1} flags) + list(APPEND result_list ${flags}) + message(STATUS "Matched NEGATIVE '${regex}' to module '${module_name}'. Compiling ${module_name} with special flags: ${flags}") + endif() + endforeach() + endif() + set("${result_var_name}" ${result_list} PARENT_SCOPE) +endfunction() + +function(_add_variant_swift_compile_flags + sdk arch build_type enable_assertions result_var_name) + set(result ${${result_var_name}}) + + cmake_parse_arguments( + VARIANT # prefix + "" # options + "MACCATALYST_BUILD_FLAVOR" # single-value args + "" # multi-value args + ${ARGN}) + + # On Windows, we don't set SWIFT_SDK_WINDOWS_PATH_ARCH_{ARCH}_PATH, so don't include it. + # On Android the sdk is split to two different paths for includes and libs, so these + # need to be set manually. + if (NOT "${sdk}" STREQUAL "WINDOWS" AND NOT "${sdk}" STREQUAL "ANDROID") + list(APPEND result "-sdk" "${SWIFT_SDK_${sdk}_ARCH_${arch}_PATH}") + endif() + + is_darwin_based_sdk("${sdk}" IS_DARWIN) + if(IS_DARWIN) + set(sdk_deployment_version "${SWIFT_SDK_${sdk}_DEPLOYMENT_VERSION}") + get_target_triple(target target_variant "${sdk}" "${arch}" + MACCATALYST_BUILD_FLAVOR "${VARIANT_MACCATALYST_BUILD_FLAVOR}" + DEPLOYMENT_VERSION "${sdk_deployment_version}") + + list(APPEND result "-target" "${target}") + if(target_variant) + list(APPEND result "-target-variant" "${target_variant}") + endif() + else() + list(APPEND result + "-target" "${SWIFT_SDK_${sdk}_ARCH_${arch}_TRIPLE}") + endif() + + if("${sdk}" STREQUAL "ANDROID") + swift_android_include_for_arch(${arch} ${arch}_swift_include) + 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) + list(APPEND result "-resource-dir" "${SWIFTLIB_DIR}") + endif() + + if(IS_DARWIN) + # We collate -F with the framework path to avoid unwanted deduplication + # of options by target_compile_options -- this way no undesired + # side effects are introduced should a new search path be added. + list(APPEND result + "-F${SWIFT_SDK_${sdk}_ARCH_${arch}_PATH}/../../../Developer/Library/Frameworks") + endif() + + is_build_type_optimized("${build_type}" optimized) + if(optimized) + list(APPEND result "-O") + else() + list(APPEND result "-Onone") + endif() + + is_build_type_with_debuginfo("${build_type}" debuginfo) + if(debuginfo) + list(APPEND result "-g") + endif() + + if(enable_assertions) + list(APPEND result "-D" "INTERNAL_CHECKS_ENABLED") + endif() + + if(SWIFT_ENABLE_RUNTIME_FUNCTION_COUNTERS) + list(APPEND result "-D" "SWIFT_ENABLE_RUNTIME_FUNCTION_COUNTERS") + endif() + + if(SWIFT_ENABLE_EXPERIMENTAL_DIFFERENTIABLE_PROGRAMMING) + list(APPEND result "-D" "SWIFT_ENABLE_EXPERIMENTAL_DIFFERENTIABLE_PROGRAMMING") + endif() + + set("${result_var_name}" "${result}" PARENT_SCOPE) +endfunction() + # Compile a swift file into an object file (as a library). # # Usage: diff --git a/stdlib/cmake/modules/AddSwiftStdlib.cmake b/stdlib/cmake/modules/AddSwiftStdlib.cmake index f2cacacab89c1..3d02d27cf52f3 100644 --- a/stdlib/cmake/modules/AddSwiftStdlib.cmake +++ b/stdlib/cmake/modules/AddSwiftStdlib.cmake @@ -1,6 +1,1812 @@ include(AddSwift) +# Add a universal binary target created from the output of the given +# set of targets by running 'lipo'. +# +# Usage: +# _add_swift_lipo_target( +# sdk # The name of the SDK the target was created for. +# # Examples include "OSX", "IOS", "ANDROID", etc. +# target # The name of the target to create +# output # The file to be created by this target +# source_targets... # The source targets whose outputs will be +# # lipo'd into the output. +# ) +function(_add_swift_lipo_target) + cmake_parse_arguments( + LIPO # prefix + "CODESIGN" # options + "SDK;TARGET;OUTPUT" # single-value args + "" # multi-value args + ${ARGN}) + + precondition(LIPO_SDK MESSAGE "sdk is required") + precondition(LIPO_TARGET MESSAGE "target is required") + precondition(LIPO_OUTPUT MESSAGE "output is required") + precondition(LIPO_UNPARSED_ARGUMENTS MESSAGE "one or more inputs are required") + + set(source_targets ${LIPO_UNPARSED_ARGUMENTS}) + + # Gather the source binaries. + set(source_binaries) + foreach(source_target ${source_targets}) + list(APPEND source_binaries $) + endforeach() + + if(${LIPO_SDK} IN_LIST SWIFT_APPLE_PLATFORMS) + if(LIPO_CODESIGN) + set(codesign_command COMMAND "codesign" "-f" "-s" "-" "${LIPO_OUTPUT}") + endif() + # Use lipo to create the final binary. + add_custom_command_target(unused_var + COMMAND "${SWIFT_LIPO}" "-create" "-output" "${LIPO_OUTPUT}" ${source_binaries} + ${codesign_command} + CUSTOM_TARGET_NAME "${LIPO_TARGET}" + OUTPUT "${LIPO_OUTPUT}" + DEPENDS ${source_targets}) + else() + # We don't know how to create fat binaries for other platforms. + add_custom_command_target(unused_var + COMMAND "${CMAKE_COMMAND}" "-E" "copy" "${source_binaries}" "${LIPO_OUTPUT}" + CUSTOM_TARGET_NAME "${LIPO_TARGET}" + OUTPUT "${LIPO_OUTPUT}" + DEPENDS ${source_targets}) + endif() +endfunction() + +# Add a single variant of a new Swift library. +# +# Usage: +# _add_swift_target_library_single( +# target +# name +# [MODULE_TARGETS] +# [SHARED] +# [STATIC] +# [SDK sdk] +# [ARCHITECTURE architecture] +# [DEPENDS dep1 ...] +# [LINK_LIBRARIES dep1 ...] +# [FRAMEWORK_DEPENDS dep1 ...] +# [FRAMEWORK_DEPENDS_WEAK dep1 ...] +# [LLVM_LINK_COMPONENTS comp1 ...] +# [C_COMPILE_FLAGS flag1...] +# [SWIFT_COMPILE_FLAGS flag1...] +# [LINK_FLAGS flag1...] +# [FILE_DEPENDS target1 ...] +# [DONT_EMBED_BITCODE] +# [IS_STDLIB] +# [IS_STDLIB_CORE] +# [IS_SDK_OVERLAY] +# INSTALL_IN_COMPONENT comp +# MACCATALYST_BUILD_FLAVOR flavor +# source1 [source2 source3 ...]) +# +# target +# Name of the target (e.g., swiftParse-IOS-armv7). +# +# name +# Name of the library (e.g., swiftParse). +# +# MODULE_TARGETS +# Names of the module target (e.g., swiftParse-swiftmodule-IOS-armv7). +# +# SHARED +# Build a shared library. +# +# STATIC +# Build a static library. +# +# SDK sdk +# SDK to build for. +# +# ARCHITECTURE +# Architecture to build for. +# +# DEPENDS +# Targets that this library depends on. +# +# LINK_LIBRARIES +# Libraries this library depends on. +# +# FRAMEWORK_DEPENDS +# System frameworks this library depends on. +# +# FRAMEWORK_DEPENDS_WEAK +# System frameworks this library depends on that should be weakly-linked. +# +# LLVM_LINK_COMPONENTS +# LLVM components this library depends on. +# +# C_COMPILE_FLAGS +# Extra compile flags (C, C++, ObjC). +# +# SWIFT_COMPILE_FLAGS +# Extra compile flags (Swift). +# +# LINK_FLAGS +# Extra linker flags. +# +# FILE_DEPENDS +# Additional files this library depends on. +# +# DONT_EMBED_BITCODE +# Don't embed LLVM bitcode in this target, even if it is enabled globally. +# +# IS_STDLIB +# Install library dylib and swift module files to lib/swift. +# +# IS_STDLIB_CORE +# Compile as the standard library core. +# +# IS_SDK_OVERLAY +# Treat the library as a part of the Swift SDK overlay. +# +# INSTALL_IN_COMPONENT comp +# The Swift installation component that this library belongs to. +# +# MACCATALYST_BUILD_FLAVOR +# Possible values are 'ios-like', 'macos-like', 'zippered', 'unzippered-twin' +# +# source1 ... +# Sources to add into this library +function(_add_swift_target_library_single target name) + set(SWIFTLIB_SINGLE_options + DONT_EMBED_BITCODE + IS_SDK_OVERLAY + IS_STDLIB + IS_STDLIB_CORE + NOSWIFTRT + OBJECT_LIBRARY + SHARED + STATIC + TARGET_LIBRARY + INSTALL_WITH_SHARED) + set(SWIFTLIB_SINGLE_single_parameter_options + ARCHITECTURE + DEPLOYMENT_VERSION_IOS + DEPLOYMENT_VERSION_OSX + DEPLOYMENT_VERSION_TVOS + DEPLOYMENT_VERSION_WATCHOS + INSTALL_IN_COMPONENT + DARWIN_INSTALL_NAME_DIR + SDK + DEPLOYMENT_VERSION_MACCATALYST + MACCATALYST_BUILD_FLAVOR) + set(SWIFTLIB_SINGLE_multiple_parameter_options + C_COMPILE_FLAGS + DEPENDS + FILE_DEPENDS + FRAMEWORK_DEPENDS + FRAMEWORK_DEPENDS_WEAK + GYB_SOURCES + INCORPORATE_OBJECT_LIBRARIES + INCORPORATE_OBJECT_LIBRARIES_SHARED_ONLY + LINK_FLAGS + LINK_LIBRARIES + LLVM_LINK_COMPONENTS + PRIVATE_LINK_LIBRARIES + SWIFT_COMPILE_FLAGS + MODULE_TARGETS) + + cmake_parse_arguments(SWIFTLIB_SINGLE + "${SWIFTLIB_SINGLE_options}" + "${SWIFTLIB_SINGLE_single_parameter_options}" + "${SWIFTLIB_SINGLE_multiple_parameter_options}" + ${ARGN}) + + # Determine macCatalyst build flavor + get_maccatalyst_build_flavor(maccatalyst_build_flavor + "${SWIFTLIB_SINGLE_SDK}" "${SWIFTLIB_SINGLE_MACCATALYST_BUILD_FLAVOR}") + + set(SWIFTLIB_SINGLE_SOURCES ${SWIFTLIB_SINGLE_UNPARSED_ARGUMENTS}) + + translate_flags(SWIFTLIB_SINGLE "${SWIFTLIB_SINGLE_options}") + + # Check arguments. + precondition(SWIFTLIB_SINGLE_SDK MESSAGE "Should specify an SDK") + precondition(SWIFTLIB_SINGLE_ARCHITECTURE MESSAGE "Should specify an architecture") + precondition(SWIFTLIB_SINGLE_INSTALL_IN_COMPONENT MESSAGE "INSTALL_IN_COMPONENT is required") + + if(NOT SWIFTLIB_SINGLE_SHARED AND + NOT SWIFTLIB_SINGLE_STATIC AND + NOT SWIFTLIB_SINGLE_OBJECT_LIBRARY) + message(FATAL_ERROR + "Either SHARED, STATIC, or OBJECT_LIBRARY must be specified") + endif() + + # Determine the subdirectory where this library will be installed. + set(SWIFTLIB_SINGLE_SUBDIR + "${SWIFT_SDK_${SWIFTLIB_SINGLE_SDK}_LIB_SUBDIR}/${SWIFTLIB_SINGLE_ARCHITECTURE}") + + # macCatalyst ios-like builds are installed in the maccatalyst/x86_64 directory + if(maccatalyst_build_flavor STREQUAL "ios-like") + set(SWIFTLIB_SINGLE_SUBDIR + "${SWIFT_SDK_MACCATALYST_LIB_SUBDIR}/${SWIFTLIB_SINGLE_ARCHITECTURE}") + endif() + + # Include LLVM Bitcode slices for iOS, Watch OS, and Apple TV OS device libraries. + set(embed_bitcode_arg) + if(SWIFT_EMBED_BITCODE_SECTION AND NOT SWIFTLIB_SINGLE_DONT_EMBED_BITCODE) + if("${SWIFTLIB_SINGLE_SDK}" STREQUAL "IOS" OR "${SWIFTLIB_SINGLE_SDK}" STREQUAL "TVOS" OR "${SWIFTLIB_SINGLE_SDK}" STREQUAL "WATCHOS") + list(APPEND SWIFTLIB_SINGLE_C_COMPILE_FLAGS "-fembed-bitcode") + set(embed_bitcode_arg EMBED_BITCODE) + endif() + endif() + + if(XCODE) + string(REGEX MATCHALL "/[^/]+" split_path ${CMAKE_CURRENT_SOURCE_DIR}) + list(GET split_path -1 dir) + file(GLOB_RECURSE SWIFTLIB_SINGLE_HEADERS + ${SWIFT_SOURCE_DIR}/include/swift${dir}/*.h + ${SWIFT_SOURCE_DIR}/include/swift${dir}/*.def + ${CMAKE_CURRENT_SOURCE_DIR}/*.def) + + file(GLOB_RECURSE SWIFTLIB_SINGLE_TDS + ${SWIFT_SOURCE_DIR}/include/swift${dir}/*.td) + + set_source_files_properties(${SWIFTLIB_SINGLE_HEADERS} ${SWIFTLIB_SINGLE_TDS} + PROPERTIES + HEADER_FILE_ONLY true) + source_group("TableGen descriptions" FILES ${SWIFTLIB_SINGLE_TDS}) + + set(SWIFTLIB_SINGLE_SOURCES ${SWIFTLIB_SINGLE_SOURCES} ${SWIFTLIB_SINGLE_HEADERS} ${SWIFTLIB_SINGLE_TDS}) + endif() + + if(MODULE) + set(libkind MODULE) + elseif(SWIFTLIB_SINGLE_OBJECT_LIBRARY) + set(libkind OBJECT) + # If both SHARED and STATIC are specified, we add the SHARED library first. + # The STATIC library is handled further below. + elseif(SWIFTLIB_SINGLE_SHARED) + set(libkind SHARED) + elseif(SWIFTLIB_SINGLE_STATIC) + set(libkind STATIC) + else() + message(FATAL_ERROR + "Either SHARED, STATIC, or OBJECT_LIBRARY must be specified") + endif() + + if(SWIFTLIB_SINGLE_GYB_SOURCES) + handle_gyb_sources( + gyb_dependency_targets + SWIFTLIB_SINGLE_GYB_SOURCES + "${SWIFTLIB_SINGLE_ARCHITECTURE}") + set(SWIFTLIB_SINGLE_SOURCES ${SWIFTLIB_SINGLE_SOURCES} + ${SWIFTLIB_SINGLE_GYB_SOURCES}) + endif() + + # Remove the "swift" prefix from the name to determine the module name. + if(SWIFTLIB_IS_STDLIB_CORE) + set(module_name "Swift") + else() + string(REPLACE swift "" module_name "${name}") + endif() + + if("${SWIFTLIB_SINGLE_SDK}" STREQUAL "WINDOWS") + if(NOT "${CMAKE_C_COMPILER_ID}" STREQUAL "MSVC") + swift_windows_get_sdk_vfs_overlay(SWIFTLIB_SINGLE_VFS_OVERLAY) + list(APPEND SWIFTLIB_SINGLE_SWIFT_COMPILE_FLAGS + -Xcc;-Xclang;-Xcc;-ivfsoverlay;-Xcc;-Xclang;-Xcc;${SWIFTLIB_SINGLE_VFS_OVERLAY}) + endif() + swift_windows_include_for_arch(${SWIFTLIB_SINGLE_ARCHITECTURE} SWIFTLIB_INCLUDE) + foreach(directory ${SWIFTLIB_INCLUDE}) + list(APPEND SWIFTLIB_SINGLE_SWIFT_COMPILE_FLAGS -Xcc;-isystem;-Xcc;${directory}) + endforeach() + if("${SWIFTLIB_SINGLE_ARCHITECTURE}" MATCHES arm) + list(APPEND SWIFTLIB_SINGLE_SWIFT_COMPILE_FLAGS -Xcc;-D_ARM_WINAPI_PARTITION_DESKTOP_SDK_AVAILABLE) + endif() + list(APPEND SWIFTLIB_SINGLE_SWIFT_COMPILE_FLAGS + -libc;${SWIFT_STDLIB_MSVC_RUNTIME_LIBRARY}) + endif() + + # FIXME: don't actually depend on the libraries in SWIFTLIB_SINGLE_LINK_LIBRARIES, + # just any swiftmodule files that are associated with them. + handle_swift_sources( + swift_object_dependency_target + swift_module_dependency_target + swift_sib_dependency_target + swift_sibopt_dependency_target + swift_sibgen_dependency_target + SWIFTLIB_SINGLE_SOURCES + SWIFTLIB_SINGLE_EXTERNAL_SOURCES ${name} + DEPENDS + ${gyb_dependency_targets} + ${SWIFTLIB_SINGLE_DEPENDS} + ${SWIFTLIB_SINGLE_FILE_DEPENDS} + ${SWIFTLIB_SINGLE_LINK_LIBRARIES} + SDK ${SWIFTLIB_SINGLE_SDK} + ARCHITECTURE ${SWIFTLIB_SINGLE_ARCHITECTURE} + MODULE_NAME ${module_name} + COMPILE_FLAGS ${SWIFTLIB_SINGLE_SWIFT_COMPILE_FLAGS} + ${SWIFTLIB_SINGLE_IS_STDLIB_keyword} + ${SWIFTLIB_SINGLE_IS_STDLIB_CORE_keyword} + ${SWIFTLIB_SINGLE_IS_SDK_OVERLAY_keyword} + ${embed_bitcode_arg} + INSTALL_IN_COMPONENT "${SWIFTLIB_SINGLE_INSTALL_IN_COMPONENT}" + MACCATALYST_BUILD_FLAVOR "${SWIFTLIB_SINGLE_MACCATALYST_BUILD_FLAVOR}") + add_swift_source_group("${SWIFTLIB_SINGLE_EXTERNAL_SOURCES}") + + # If there were any swift sources, then a .swiftmodule may have been created. + # If that is the case, then add a target which is an alias of the module files. + set(VARIANT_SUFFIX "-${SWIFT_SDK_${SWIFTLIB_SINGLE_SDK}_LIB_SUBDIR}-${SWIFTLIB_SINGLE_ARCHITECTURE}") + if(maccatalyst_build_flavor STREQUAL "ios-like") + set(VARIANT_SUFFIX "-${SWIFT_SDK_MACCATALYST_LIB_SUBDIR}-${SWIFTLIB_SINGLE_ARCHITECTURE}") + endif() + + if(NOT "${SWIFTLIB_SINGLE_MODULE_TARGETS}" STREQUAL "" AND NOT "${swift_module_dependency_target}" STREQUAL "") + foreach(module_target ${SWIFTLIB_SINGLE_MODULE_TARGETS}) + add_custom_target("${module_target}" + DEPENDS ${swift_module_dependency_target}) + set_target_properties("${module_target}" PROPERTIES + FOLDER "Swift libraries/Modules") + endforeach() + endif() + + # For standalone overlay builds to work + if(NOT BUILD_STANDALONE) + if (EXISTS swift_sib_dependency_target AND NOT "${swift_sib_dependency_target}" STREQUAL "") + add_dependencies(swift-stdlib${VARIANT_SUFFIX}-sib ${swift_sib_dependency_target}) + endif() + + if (EXISTS swift_sibopt_dependency_target AND NOT "${swift_sibopt_dependency_target}" STREQUAL "") + add_dependencies(swift-stdlib${VARIANT_SUFFIX}-sibopt ${swift_sibopt_dependency_target}) + endif() + + if (EXISTS swift_sibgen_dependency_target AND NOT "${swift_sibgen_dependency_target}" STREQUAL "") + add_dependencies(swift-stdlib${VARIANT_SUFFIX}-sibgen ${swift_sibgen_dependency_target}) + endif() + endif() + + # Only build the modules for any arch listed in the *_MODULE_ARCHITECTURES. + if(SWIFTLIB_SINGLE_SDK IN_LIST SWIFT_APPLE_PLATFORMS + AND SWIFTLIB_SINGLE_ARCHITECTURE IN_LIST SWIFT_SDK_${SWIFTLIB_SINGLE_SDK}_MODULE_ARCHITECTURES) + # Create dummy target to hook up the module target dependency. + add_custom_target("${target}" + DEPENDS + "${swift_module_dependency_target}") + + return() + endif() + + set(SWIFTLIB_INCORPORATED_OBJECT_LIBRARIES_EXPRESSIONS) + foreach(object_library ${SWIFTLIB_SINGLE_INCORPORATE_OBJECT_LIBRARIES}) + list(APPEND SWIFTLIB_INCORPORATED_OBJECT_LIBRARIES_EXPRESSIONS + $) + endforeach() + + set(SWIFTLIB_INCORPORATED_OBJECT_LIBRARIES_EXPRESSIONS_SHARED_ONLY) + foreach(object_library ${SWIFTLIB_SINGLE_INCORPORATE_OBJECT_LIBRARIES_SHARED_ONLY}) + list(APPEND SWIFTLIB_INCORPORATED_OBJECT_LIBRARIES_EXPRESSIONS_SHARED_ONLY + $) + endforeach() + + set(SWIFTLIB_SINGLE_XCODE_WORKAROUND_SOURCES) + if(XCODE AND SWIFTLIB_SINGLE_TARGET_LIBRARY) + set(SWIFTLIB_SINGLE_XCODE_WORKAROUND_SOURCES + # Note: the dummy.cpp source file provides no definitions. However, + # it forces Xcode to properly link the static library. + ${SWIFT_SOURCE_DIR}/cmake/dummy.cpp) + endif() + + set(INCORPORATED_OBJECT_LIBRARIES_EXPRESSIONS ${SWIFTLIB_INCORPORATED_OBJECT_LIBRARIES_EXPRESSIONS}) + if(${libkind} STREQUAL "SHARED") + list(APPEND INCORPORATED_OBJECT_LIBRARIES_EXPRESSIONS + ${SWIFTLIB_INCORPORATED_OBJECT_LIBRARIES_EXPRESSIONS_SHARED_ONLY}) + endif() + + add_library("${target}" ${libkind} + ${SWIFTLIB_SINGLE_SOURCES} + ${SWIFTLIB_SINGLE_EXTERNAL_SOURCES} + ${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 + SWIFTLIB_SINGLE_TARGET_LIBRARY) + if("${libkind}" STREQUAL "SHARED" AND NOT SWIFTLIB_SINGLE_NOSWIFTRT) + # TODO(compnerd) switch to the generator expression when cmake is upgraded + # to a version which supports it. + # target_sources(${target} + # PRIVATE + # $) + if(SWIFTLIB_SINGLE_SDK STREQUAL WINDOWS) + set(extension .obj) + else() + set(extension .o) + endif() + target_sources(${target} + PRIVATE + "${SWIFTLIB_DIR}/${SWIFTLIB_SINGLE_SUBDIR}/swiftrt${extension}") + set_source_files_properties("${SWIFTLIB_DIR}/${SWIFTLIB_SINGLE_SUBDIR}/swiftrt${extension}" + PROPERTIES + GENERATED 1) + endif() + endif() + _set_target_prefix_and_suffix("${target}" "${libkind}" "${SWIFTLIB_SINGLE_SDK}") + + if("${SWIFTLIB_SINGLE_SDK}" STREQUAL "WINDOWS") + swift_windows_include_for_arch(${SWIFTLIB_SINGLE_ARCHITECTURE} SWIFTLIB_INCLUDE) + target_include_directories("${target}" SYSTEM PRIVATE ${SWIFTLIB_INCLUDE}) + set_target_properties(${target} + PROPERTIES + CXX_STANDARD 14) + endif() + + if("${SWIFTLIB_SINGLE_SDK}" STREQUAL "WINDOWS" AND NOT "${CMAKE_SYSTEM_NAME}" STREQUAL "Windows") + if("${libkind}" STREQUAL "SHARED") + # Each dll has an associated .lib (import library); since we may be + # building on a non-DLL platform (not windows), create an imported target + # for the library which created implicitly by the dll. + add_custom_command_target(${target}_IMPORT_LIBRARY + OUTPUT "${SWIFTLIB_DIR}/${SWIFTLIB_SINGLE_SUBDIR}/${name}.lib" + DEPENDS "${target}") + add_library(${target}_IMPLIB SHARED IMPORTED GLOBAL) + set_property(TARGET "${target}_IMPLIB" PROPERTY + IMPORTED_LOCATION "${SWIFTLIB_DIR}/${SWIFTLIB_SINGLE_SUBDIR}/${name}.lib") + add_dependencies(${target}_IMPLIB ${${target}_IMPORT_LIBRARY}) + endif() + set_property(TARGET "${target}" PROPERTY NO_SONAME ON) + endif() + + llvm_update_compile_flags(${target}) + + set_output_directory(${target} + BINARY_DIR ${SWIFT_RUNTIME_OUTPUT_INTDIR} + LIBRARY_DIR ${SWIFT_LIBRARY_OUTPUT_INTDIR}) + + if(MODULE) + set_target_properties("${target}" PROPERTIES + PREFIX "" + SUFFIX ${LLVM_PLUGIN_EXT}) + endif() + + if(SWIFTLIB_SINGLE_TARGET_LIBRARY) + # Install runtime libraries to lib/swift instead of lib. This works around + # the fact that -isysroot prevents linking to libraries in the system + # /usr/lib if Swift is installed in /usr. + set_target_properties("${target}" PROPERTIES + LIBRARY_OUTPUT_DIRECTORY ${SWIFTLIB_DIR}/${SWIFTLIB_SINGLE_SUBDIR} + ARCHIVE_OUTPUT_DIRECTORY ${SWIFTLIB_DIR}/${SWIFTLIB_SINGLE_SUBDIR}) + if(SWIFTLIB_SINGLE_SDK STREQUAL WINDOWS AND SWIFTLIB_SINGLE_IS_STDLIB_CORE + AND libkind STREQUAL SHARED) + add_custom_command(TARGET ${target} POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy_if_different $ ${SWIFTLIB_DIR}/${SWIFTLIB_SINGLE_SUBDIR}) + endif() + + foreach(config ${CMAKE_CONFIGURATION_TYPES}) + string(TOUPPER ${config} config_upper) + escape_path_for_xcode("${config}" "${SWIFTLIB_DIR}" config_lib_dir) + set_target_properties(${target} PROPERTIES + LIBRARY_OUTPUT_DIRECTORY_${config_upper} ${config_lib_dir}/${SWIFTLIB_SINGLE_SUBDIR} + ARCHIVE_OUTPUT_DIRECTORY_${config_upper} ${config_lib_dir}/${SWIFTLIB_SINGLE_SUBDIR}) + endforeach() + endif() + + if(SWIFTLIB_SINGLE_SDK IN_LIST SWIFT_APPLE_PLATFORMS) + set(install_name_dir "@rpath") + + if(SWIFTLIB_SINGLE_IS_STDLIB) + set(install_name_dir "${SWIFT_DARWIN_STDLIB_INSTALL_NAME_DIR}") + + # iOS-like overlays are installed in a separate directory so that + # unzippered twins do not conflict. + if(maccatalyst_build_flavor STREQUAL "ios-like" + AND DEFINED SWIFT_DARWIN_MACCATALYST_STDLIB_INSTALL_NAME_DIR) + set(install_name_dir "${SWIFT_DARWIN_MACCATALYST_STDLIB_INSTALL_NAME_DIR}") + endif() + endif() + + # Always use @rpath for XCTest + if(module_name STREQUAL "XCTest") + set(install_name_dir "@rpath") + endif() + + if(SWIFTLIB_SINGLE_DARWIN_INSTALL_NAME_DIR) + set(install_name_dir "${SWIFTLIB_SINGLE_DARWIN_INSTALL_NAME_DIR}") + endif() + + set_target_properties("${target}" + PROPERTIES + INSTALL_NAME_DIR "${install_name_dir}") + elseif("${SWIFTLIB_SINGLE_SDK}" STREQUAL "LINUX") + set_target_properties("${target}" + PROPERTIES + INSTALL_RPATH "$ORIGIN:/usr/lib/swift/linux") + elseif("${SWIFTLIB_SINGLE_SDK}" STREQUAL "CYGWIN") + set_target_properties("${target}" + PROPERTIES + INSTALL_RPATH "$ORIGIN:/usr/lib/swift/cygwin") + elseif("${SWIFTLIB_SINGLE_SDK}" STREQUAL "ANDROID") + # CMake generates an incorrect rule `$SONAME_FLAG $INSTALLNAME_DIR$SONAME` + # for an Android cross-build from a macOS host. Construct the proper linker + # flags manually in add_swift_target_library instead, see there with + # variable `swiftlib_link_flags_all`. + if(SWIFTLIB_SINGLE_TARGET_LIBRARY) + set_target_properties("${target}" PROPERTIES NO_SONAME TRUE) + endif() + # Only set the install RPATH if cross-compiling the host tools, in which + # case both the NDK and Sysroot paths must be set. + if(NOT "${SWIFT_ANDROID_NDK_PATH}" STREQUAL "" AND + NOT "${SWIFT_ANDROID_NATIVE_SYSROOT}" STREQUAL "") + set_target_properties("${target}" + PROPERTIES + INSTALL_RPATH "$ORIGIN") + endif() + endif() + + set_target_properties("${target}" PROPERTIES BUILD_WITH_INSTALL_RPATH YES) + set_target_properties("${target}" PROPERTIES FOLDER "Swift libraries") + + # Configure the static library target. + # Set compile and link flags for the non-static target. + # Do these LAST. + set(target_static) + if(SWIFTLIB_SINGLE_IS_STDLIB AND SWIFTLIB_SINGLE_STATIC) + set(target_static "${target}-static") + + # We have already compiled Swift sources. Link everything into a static + # library. + add_library(${target_static} STATIC + ${SWIFTLIB_SINGLE_SOURCES} + ${SWIFTLIB_INCORPORATED_OBJECT_LIBRARIES_EXPRESSIONS} + ${SWIFTLIB_SINGLE_XCODE_WORKAROUND_SOURCES}) + + set_output_directory(${target_static} + BINARY_DIR ${SWIFT_RUNTIME_OUTPUT_INTDIR} + LIBRARY_DIR ${SWIFT_LIBRARY_OUTPUT_INTDIR}) + + if(SWIFTLIB_INSTALL_WITH_SHARED) + set(swift_lib_dir ${SWIFTLIB_DIR}) + else() + set(swift_lib_dir ${SWIFTSTATICLIB_DIR}) + endif() + + foreach(config ${CMAKE_CONFIGURATION_TYPES}) + string(TOUPPER ${config} config_upper) + escape_path_for_xcode( + "${config}" "${swift_lib_dir}" config_lib_dir) + set_target_properties(${target_static} PROPERTIES + LIBRARY_OUTPUT_DIRECTORY_${config_upper} ${config_lib_dir}/${SWIFTLIB_SINGLE_SUBDIR} + ARCHIVE_OUTPUT_DIRECTORY_${config_upper} ${config_lib_dir}/${SWIFTLIB_SINGLE_SUBDIR}) + endforeach() + + set_target_properties(${target_static} PROPERTIES + LIBRARY_OUTPUT_DIRECTORY ${swift_lib_dir}/${SWIFTLIB_SINGLE_SUBDIR} + ARCHIVE_OUTPUT_DIRECTORY ${swift_lib_dir}/${SWIFTLIB_SINGLE_SUBDIR}) + endif() + + set_target_properties(${target} + PROPERTIES + # Library name (without the variant information) + OUTPUT_NAME ${name}) + if(target_static) + set_target_properties(${target_static} + PROPERTIES + OUTPUT_NAME ${name}) + endif() + + # Don't build standard libraries by default. We will enable building + # standard libraries that the user requested; the rest can be built on-demand. + if(SWIFTLIB_SINGLE_TARGET_LIBRARY) + foreach(t "${target}" ${target_static}) + set_target_properties(${t} PROPERTIES EXCLUDE_FROM_ALL TRUE) + endforeach() + endif() + + # Handle linking and dependencies. + add_dependencies_multiple_targets( + TARGETS "${target}" ${target_static} + DEPENDS + ${SWIFTLIB_SINGLE_DEPENDS} + ${gyb_dependency_targets} + "${swift_object_dependency_target}" + "${swift_module_dependency_target}" + ${LLVM_COMMON_DEPENDS}) + + if("${libkind}" STREQUAL "SHARED") + target_link_libraries("${target}" PRIVATE ${SWIFTLIB_SINGLE_LINK_LIBRARIES}) + elseif("${libkind}" STREQUAL "OBJECT") + precondition_list_empty( + "${SWIFTLIB_SINGLE_LINK_LIBRARIES}" + "OBJECT_LIBRARY may not link to anything") + else() + target_link_libraries("${target}" INTERFACE ${SWIFTLIB_SINGLE_LINK_LIBRARIES}) + endif() + + # Don't add the icucore target. + set(SWIFTLIB_SINGLE_LINK_LIBRARIES_WITHOUT_ICU) + foreach(item ${SWIFTLIB_SINGLE_LINK_LIBRARIES}) + if(NOT "${item}" STREQUAL "icucore") + list(APPEND SWIFTLIB_SINGLE_LINK_LIBRARIES_WITHOUT_ICU "${item}") + endif() + endforeach() + + if(target_static) + _list_add_string_suffix( + "${SWIFTLIB_SINGLE_LINK_LIBRARIES_WITHOUT_ICU}" + "-static" + target_static_depends) + # FIXME: should this be target_link_libraries? + add_dependencies_multiple_targets( + TARGETS "${target_static}" + DEPENDS ${target_static_depends}) + endif() + + # Link against system frameworks. + foreach(FRAMEWORK ${SWIFTLIB_SINGLE_FRAMEWORK_DEPENDS}) + foreach(t "${target}" ${target_static}) + target_link_libraries("${t}" PUBLIC "-framework ${FRAMEWORK}") + endforeach() + endforeach() + foreach(FRAMEWORK ${SWIFTLIB_SINGLE_FRAMEWORK_DEPENDS_WEAK}) + foreach(t "${target}" ${target_static}) + target_link_libraries("${t}" PUBLIC "-weak_framework ${FRAMEWORK}") + endforeach() + endforeach() + + if(NOT SWIFTLIB_SINGLE_TARGET_LIBRARY) + # Call llvm_config() only for libraries that are part of the compiler. + swift_common_llvm_config("${target}" ${SWIFTLIB_SINGLE_LLVM_LINK_COMPONENTS}) + endif() + + # Collect compile and link flags for the static and non-static targets. + # Don't set PROPERTY COMPILE_FLAGS or LINK_FLAGS directly. + set(c_compile_flags ${SWIFTLIB_SINGLE_C_COMPILE_FLAGS}) + set(link_flags ${SWIFTLIB_SINGLE_LINK_FLAGS}) + + set(library_search_subdir "${SWIFT_SDK_${SWIFTLIB_SINGLE_SDK}_LIB_SUBDIR}") + set(library_search_directories + "${SWIFTLIB_DIR}/${SWIFTLIB_SINGLE_SUBDIR}" + "${SWIFT_NATIVE_SWIFT_TOOLS_PATH}/../lib/swift/${SWIFTLIB_SINGLE_SUBDIR}" + "${SWIFT_NATIVE_SWIFT_TOOLS_PATH}/../lib/swift/${SWIFT_SDK_${SWIFTLIB_SINGLE_SDK}_LIB_SUBDIR}") + + # In certain cases when building, the environment variable SDKROOT is set to override + # where the sdk root is located in the system. If that environment variable has been + # set by the user, respect it and add the specified SDKROOT directory to the + # library_search_directories so we are able to link against those libraries + if(DEFINED ENV{SDKROOT} AND EXISTS "$ENV{SDKROOT}/usr/lib/swift") + list(APPEND library_search_directories "$ENV{SDKROOT}/usr/lib/swift") + endif() + + # Add variant-specific flags. + if(SWIFTLIB_SINGLE_TARGET_LIBRARY) + set(build_type "${SWIFT_STDLIB_BUILD_TYPE}") + set(enable_assertions "${SWIFT_STDLIB_ASSERTIONS}") + else() + set(build_type "${CMAKE_BUILD_TYPE}") + set(enable_assertions "${LLVM_ENABLE_ASSERTIONS}") + set(analyze_code_coverage "${SWIFT_ANALYZE_CODE_COVERAGE}") + endif() + + if (NOT SWIFTLIB_SINGLE_TARGET_LIBRARY) + set(lto_type "${SWIFT_TOOLS_ENABLE_LTO}") + endif() + + _add_variant_c_compile_flags( + SDK "${SWIFTLIB_SINGLE_SDK}" + ARCH "${SWIFTLIB_SINGLE_ARCHITECTURE}" + BUILD_TYPE "${build_type}" + ENABLE_ASSERTIONS "${enable_assertions}" + ANALYZE_CODE_COVERAGE "${analyze_code_coverage}" + ENABLE_LTO "${lto_type}" + DEPLOYMENT_VERSION_OSX "${SWIFTLIB_DEPLOYMENT_VERSION_OSX}" + DEPLOYMENT_VERSION_MACCATALYST "${SWIFTLIB_DEPLOYMENT_VERSION_MACCATALYST}" + DEPLOYMENT_VERSION_IOS "${SWIFTLIB_DEPLOYMENT_VERSION_IOS}" + DEPLOYMENT_VERSION_TVOS "${SWIFTLIB_DEPLOYMENT_VERSION_TVOS}" + DEPLOYMENT_VERSION_WATCHOS "${SWIFTLIB_DEPLOYMENT_VERSION_WATCHOS}" + RESULT_VAR_NAME c_compile_flags + MACCATALYST_BUILD_FLAVOR "${SWIFTLIB_SINGLE_MACCATALYST_BUILD_FLAVOR}" + ) + + if(SWIFTLIB_IS_STDLIB) + # We don't ever want to link against the ABI-breakage checking symbols + # in the standard library, runtime, or overlays because they only rely + # on the header parts of LLVM's ADT. + list(APPEND c_compile_flags + "-DLLVM_DISABLE_ABI_BREAKING_CHECKS_ENFORCING=1") + endif() + + if(SWIFTLIB_SINGLE_SDK STREQUAL WINDOWS) + if(libkind STREQUAL SHARED) + list(APPEND c_compile_flags -D_WINDLL) + endif() + endif() + _add_variant_link_flags( + SDK "${SWIFTLIB_SINGLE_SDK}" + ARCH "${SWIFTLIB_SINGLE_ARCHITECTURE}" + BUILD_TYPE "${build_type}" + ENABLE_ASSERTIONS "${enable_assertions}" + ANALYZE_CODE_COVERAGE "${analyze_code_coverage}" + ENABLE_LTO "${lto_type}" + LTO_OBJECT_NAME "${target}-${SWIFTLIB_SINGLE_SDK}-${SWIFTLIB_SINGLE_ARCHITECTURE}" + DEPLOYMENT_VERSION_OSX "${SWIFTLIB_DEPLOYMENT_VERSION_OSX}" + DEPLOYMENT_VERSION_MACCATALYST "${SWIFTLIB_DEPLOYMENT_VERSION_MACCATALYST}" + DEPLOYMENT_VERSION_IOS "${SWIFTLIB_DEPLOYMENT_VERSION_IOS}" + DEPLOYMENT_VERSION_TVOS "${SWIFTLIB_DEPLOYMENT_VERSION_TVOS}" + DEPLOYMENT_VERSION_WATCHOS "${SWIFTLIB_DEPLOYMENT_VERSION_WATCHOS}" + RESULT_VAR_NAME link_flags + LINK_LIBRARIES_VAR_NAME link_libraries + LIBRARY_SEARCH_DIRECTORIES_VAR_NAME library_search_directories + MACCATALYST_BUILD_FLAVOR "${SWIFTLIB_SINGLE_MACCATALYST_BUILD_FLAVOR}" + ) + + # Configure plist creation for OS X. + set(PLIST_INFO_PLIST "Info.plist" CACHE STRING "Plist name") + if("${SWIFTLIB_SINGLE_SDK}" IN_LIST SWIFT_APPLE_PLATFORMS AND SWIFTLIB_SINGLE_IS_STDLIB) + set(PLIST_INFO_NAME ${name}) + set(PLIST_INFO_UTI "com.apple.dt.runtime.${name}") + set(PLIST_INFO_VERSION "${SWIFT_VERSION}") + if (SWIFT_COMPILER_VERSION) + set(PLIST_INFO_BUILD_VERSION + "${SWIFT_COMPILER_VERSION}") + endif() + + set(PLIST_INFO_PLIST_OUT "${PLIST_INFO_PLIST}") + list(APPEND link_flags + "-Wl,-sectcreate,__TEXT,__info_plist,${CMAKE_CURRENT_BINARY_DIR}/${PLIST_INFO_PLIST_OUT}") + configure_file( + "${SWIFT_SOURCE_DIR}/stdlib/${PLIST_INFO_PLIST}.in" + "${PLIST_INFO_PLIST_OUT}" + @ONLY + NEWLINE_STYLE UNIX) + + # If Application Extensions are enabled, pass the linker flag marking + # the dylib as safe. + if (CXX_SUPPORTS_FAPPLICATION_EXTENSION AND (NOT DISABLE_APPLICATION_EXTENSION)) + list(APPEND link_flags "-Wl,-application_extension") + endif() + + set(PLIST_INFO_UTI) + set(PLIST_INFO_NAME) + set(PLIST_INFO_VERSION) + set(PLIST_INFO_BUILD_VERSION) + endif() + + # Set compilation and link flags. + if(SWIFTLIB_SINGLE_SDK STREQUAL WINDOWS) + swift_windows_include_for_arch(${SWIFTLIB_SINGLE_ARCHITECTURE} + ${SWIFTLIB_SINGLE_ARCHITECTURE}_INCLUDE) + target_include_directories(${target} SYSTEM PRIVATE + ${${SWIFTLIB_SINGLE_ARCHITECTURE}_INCLUDE}) + + if(NOT ${CMAKE_C_COMPILER_ID} STREQUAL MSVC) + swift_windows_get_sdk_vfs_overlay(SWIFTLIB_SINGLE_VFS_OVERLAY) + target_compile_options(${target} PRIVATE + "SHELL:-Xclang -ivfsoverlay -Xclang ${SWIFTLIB_SINGLE_VFS_OVERLAY}") + + # MSVC doesn't support -Xclang. We don't need to manually specify + # the dependent libraries as `cl` does so. + target_compile_options(${target} PRIVATE + "SHELL:-Xclang --dependent-lib=oldnames" + # TODO(compnerd) handle /MT, /MTd + "SHELL:-Xclang --dependent-lib=msvcrt$<$:d>") + endif() + endif() + target_include_directories(${target} SYSTEM PRIVATE + ${SWIFT_${SWIFTLIB_SINGLE_SDK}_${SWIFTLIB_SINGLE_ARCHITECTURE}_ICU_UC_INCLUDE} + ${SWIFT_${SWIFTLIB_SINGLE_SDK}_${SWIFTLIB_SINGLE_ARCHITECTURE}_ICU_I18N_INCLUDE}) + target_compile_options(${target} PRIVATE + ${c_compile_flags}) + target_link_options(${target} PRIVATE + ${link_flags}) + if(${SWIFTLIB_SINGLE_SDK} IN_LIST SWIFT_APPLE_PLATFORMS) + target_link_options(${target} PRIVATE + "LINKER:-compatibility_version,1") + if(SWIFT_COMPILER_VERSION) + target_link_options(${target} PRIVATE + "LINKER:-current_version,${SWIFT_COMPILER_VERSION}") + endif() + # Include LLVM Bitcode slices for iOS, Watch OS, and Apple TV OS device libraries. + if(SWIFT_EMBED_BITCODE_SECTION AND NOT SWIFTLIB_SINGLE_DONT_EMBED_BITCODE) + if(${SWIFTLIB_SINGLE_SDK} MATCHES "(I|TV|WATCH)OS") + # The two branches of this if statement accomplish the same end result + # We are simply accounting for the fact that on CMake < 3.16 + # using a generator expression to + # specify a LINKER: argument does not work, + # since that seems not to allow the LINKER: prefix to be + # evaluated (i.e. it will be added as-is to the linker parameters) + if(CMAKE_VERSION VERSION_LESS 3.16) + target_link_options(${target} PRIVATE + "LINKER:-bitcode_bundle" + "LINKER:-lto_library,${LLVM_LIBRARY_DIR}/libLTO.dylib") + + if(SWIFT_EMBED_BITCODE_SECTION_HIDE_SYMBOLS) + target_link_options(${target} PRIVATE + "LINKER:-bitcode_hide_symbols") + endif() + else() + target_link_options(${target} PRIVATE + "LINKER:-bitcode_bundle" + $<$:"LINKER:-bitcode_hide_symbols"> + "LINKER:-lto_library,${LLVM_LIBRARY_DIR}/libLTO.dylib") + endif() + endif() + endif() + endif() + target_link_libraries(${target} PRIVATE + ${link_libraries}) + target_link_directories(${target} PRIVATE + ${library_search_directories}) + + # Adjust the linked libraries for windows targets. On Windows, the link is + # performed against the import library, and the runtime uses the dll. Not + # doing so will result in incorrect symbol resolution and linkage. We created + # import library targets when the library was added. Use that to adjust the + # link libraries. + if(SWIFTLIB_SINGLE_SDK STREQUAL WINDOWS AND NOT CMAKE_SYSTEM_NAME STREQUAL Windows) + foreach(library_list LINK_LIBRARIES PRIVATE_LINK_LIBRARIES) + set(import_libraries) + foreach(library ${SWIFTLIB_SINGLE_${library_list}}) + # Ensure that the library is a target. If an absolute path was given, + # then we do not have an import library associated with it. This occurs + # primarily with ICU (which will be an import library). Import + # libraries are only associated with shared libraries, so add an + # additional check for that as well. + set(import_library ${library}) + if(TARGET ${library}) + get_target_property(type ${library} TYPE) + if(${type} STREQUAL "SHARED_LIBRARY") + set(import_library ${library}_IMPLIB) + endif() + endif() + list(APPEND import_libraries ${import_library}) + endforeach() + set(SWIFTLIB_SINGLE_${library_list} ${import_libraries}) + endforeach() + endif() + + if("${libkind}" STREQUAL "OBJECT") + precondition_list_empty( + "${SWIFTLIB_SINGLE_PRIVATE_LINK_LIBRARIES}" + "OBJECT_LIBRARY may not link to anything") + else() + target_link_libraries("${target}" PRIVATE + ${SWIFTLIB_SINGLE_PRIVATE_LINK_LIBRARIES}) + endif() + + # NOTE(compnerd) use the C linker language to invoke `clang` rather than + # `clang++` as we explicitly link against the C++ runtime. We were previously + # actually passing `-nostdlib++` to avoid the C++ runtime linkage. + if("${SWIFTLIB_SINGLE_SDK}" STREQUAL "ANDROID") + set_property(TARGET "${target}" PROPERTY + LINKER_LANGUAGE "C") + else() + set_property(TARGET "${target}" PROPERTY + LINKER_LANGUAGE "CXX") + endif() + + if(target_static) + target_compile_options(${target_static} PRIVATE + ${c_compile_flags}) + # FIXME: The fallback paths here are going to be dynamic libraries. + + if(SWIFTLIB_INSTALL_WITH_SHARED) + set(search_base_dir ${SWIFTLIB_DIR}) + else() + set(search_base_dir ${SWIFTSTATICLIB_DIR}) + endif() + set(library_search_directories + "${search_base_dir}/${SWIFTLIB_SINGLE_SUBDIR}" + "${SWIFT_NATIVE_SWIFT_TOOLS_PATH}/../lib/swift/${SWIFTLIB_SINGLE_SUBDIR}" + "${SWIFT_NATIVE_SWIFT_TOOLS_PATH}/../lib/swift/${SWIFT_SDK_${SWIFTLIB_SINGLE_SDK}_LIB_SUBDIR}") + target_link_directories(${target_static} PRIVATE + ${library_search_directories}) + target_link_libraries("${target_static}" PRIVATE + ${SWIFTLIB_SINGLE_PRIVATE_LINK_LIBRARIES}) + endif() + + # Do not add code here. +endfunction() + +# Add a new Swift target library. +# +# NOTE: This has not had the swift host code debrided from it yet. That will be +# in a forthcoming commit. +# +# Usage: +# add_swift_target_library(name +# [SHARED] +# [STATIC] +# [DEPENDS dep1 ...] +# [LINK_LIBRARIES dep1 ...] +# [SWIFT_MODULE_DEPENDS dep1 ...] +# [FRAMEWORK_DEPENDS dep1 ...] +# [FRAMEWORK_DEPENDS_WEAK dep1 ...] +# [LLVM_LINK_COMPONENTS comp1 ...] +# [FILE_DEPENDS target1 ...] +# [TARGET_SDKS sdk1...] +# [C_COMPILE_FLAGS flag1...] +# [SWIFT_COMPILE_FLAGS flag1...] +# [LINK_FLAGS flag1...] +# [DONT_EMBED_BITCODE] +# [INSTALL] +# [IS_STDLIB] +# [IS_STDLIB_CORE] +# [INSTALL_WITH_SHARED] +# INSTALL_IN_COMPONENT comp +# DEPLOYMENT_VERSION_OSX version +# DEPLOYMENT_VERSION_MACCATALYST version +# DEPLOYMENT_VERSION_IOS version +# DEPLOYMENT_VERSION_TVOS version +# DEPLOYMENT_VERSION_WATCHOS version +# MACCATALYST_BUILD_FLAVOR flavor +# source1 [source2 source3 ...]) +# +# name +# Name of the library (e.g., swiftParse). +# +# SHARED +# Build a shared library. +# +# STATIC +# Build a static library. +# +# DEPENDS +# Targets that this library depends on. +# +# LINK_LIBRARIES +# Libraries this library depends on. +# +# SWIFT_MODULE_DEPENDS +# Swift modules this library depends on. +# +# SWIFT_MODULE_DEPENDS_OSX +# Swift modules this library depends on when built for OS X. +# +# SWIFT_MODULE_DEPENDS_MACCATALYST +# Zippered Swift modules this library depends on when built for macCatalyst. +# For example, Foundation. +# +# SWIFT_MODULE_DEPENDS_MACCATALYST_UNZIPPERED +# Unzippered Swift modules this library depends on when built for macCatalyst. +# For example, UIKit +# +# SWIFT_MODULE_DEPENDS_IOS +# Swift modules this library depends on when built for iOS. +# +# SWIFT_MODULE_DEPENDS_TVOS +# Swift modules this library depends on when built for tvOS. +# +# SWIFT_MODULE_DEPENDS_WATCHOS +# Swift modules this library depends on when built for watchOS. +# +# SWIFT_MODULE_DEPENDS_FREEBSD +# Swift modules this library depends on when built for FreeBSD. +# +# SWIFT_MODULE_DEPENDS_LINUX +# Swift modules this library depends on when built for Linux. +# +# SWIFT_MODULE_DEPENDS_CYGWIN +# Swift modules this library depends on when built for Cygwin. +# +# SWIFT_MODULE_DEPENDS_HAIKU +# Swift modules this library depends on when built for Haiku. +# +# SWIFT_MODULE_DEPENDS_WASI +# Swift modules this library depends on when built for WASI. +# +# FRAMEWORK_DEPENDS +# System frameworks this library depends on. +# +# FRAMEWORK_DEPENDS_WEAK +# System frameworks this library depends on that should be weak-linked +# +# LLVM_LINK_COMPONENTS +# LLVM components this library depends on. +# +# FILE_DEPENDS +# Additional files this library depends on. +# +# TARGET_SDKS +# The set of SDKs in which this library is included. If empty, the library +# is included in all SDKs. +# +# C_COMPILE_FLAGS +# Extra compiler flags (C, C++, ObjC). +# +# SWIFT_COMPILE_FLAGS +# Extra compiler flags (Swift). +# +# LINK_FLAGS +# Extra linker flags. +# +# DONT_EMBED_BITCODE +# Don't embed LLVM bitcode in this target, even if it is enabled globally. +# +# IS_STDLIB +# Treat the library as a part of the Swift standard library. +# +# IS_STDLIB_CORE +# Compile as the Swift standard library core. +# +# IS_SDK_OVERLAY +# Treat the library as a part of the Swift SDK overlay. +# +# INSTALL_IN_COMPONENT comp +# The Swift installation component that this library belongs to. +# +# DEPLOYMENT_VERSION_OSX +# The minimum deployment version to build for if this is an OSX library. +# +# DEPLOYMENT_VERSION_MACCATALYST +# The minimum deployment version to build for if this is an macCatalyst library. +# +# DEPLOYMENT_VERSION_IOS +# The minimum deployment version to build for if this is an iOS library. +# +# DEPLOYMENT_VERSION_TVOS +# The minimum deployment version to build for if this is an TVOS library. +# +# DEPLOYMENT_VERSION_WATCHOS +# The minimum deployment version to build for if this is an WATCHOS library. +# +# INSTALL_WITH_SHARED +# Install a static library target alongside shared libraries +# +# MACCATALYST_BUILD_FLAVOR +# Possible values are 'ios-like', 'macos-like', 'zippered', 'unzippered-twin' +# Presence of a build flavor requires SWIFT_MODULE_DEPENDS_MACCATALYST to be +# defined and have values. +# +# source1 ... +# Sources to add into this library. +function(add_swift_target_library name) + set(SWIFTLIB_options + DONT_EMBED_BITCODE + HAS_SWIFT_CONTENT + IS_SDK_OVERLAY + IS_STDLIB + IS_STDLIB_CORE + NOSWIFTRT + OBJECT_LIBRARY + SHARED + STATIC + INSTALL_WITH_SHARED) + set(SWIFTLIB_single_parameter_options + DEPLOYMENT_VERSION_IOS + DEPLOYMENT_VERSION_OSX + DEPLOYMENT_VERSION_TVOS + DEPLOYMENT_VERSION_WATCHOS + INSTALL_IN_COMPONENT + DARWIN_INSTALL_NAME_DIR + DEPLOYMENT_VERSION_MACCATALYST + MACCATALYST_BUILD_FLAVOR) + set(SWIFTLIB_multiple_parameter_options + C_COMPILE_FLAGS + DEPENDS + FILE_DEPENDS + FRAMEWORK_DEPENDS + FRAMEWORK_DEPENDS_IOS_TVOS + FRAMEWORK_DEPENDS_OSX + FRAMEWORK_DEPENDS_WEAK + GYB_SOURCES + INCORPORATE_OBJECT_LIBRARIES + INCORPORATE_OBJECT_LIBRARIES_SHARED_ONLY + LINK_FLAGS + LINK_LIBRARIES + LLVM_LINK_COMPONENTS + PRIVATE_LINK_LIBRARIES + SWIFT_COMPILE_FLAGS + SWIFT_COMPILE_FLAGS_IOS + SWIFT_COMPILE_FLAGS_OSX + SWIFT_COMPILE_FLAGS_TVOS + SWIFT_COMPILE_FLAGS_WATCHOS + SWIFT_COMPILE_FLAGS_LINUX + SWIFT_MODULE_DEPENDS + SWIFT_MODULE_DEPENDS_CYGWIN + SWIFT_MODULE_DEPENDS_FREEBSD + SWIFT_MODULE_DEPENDS_HAIKU + SWIFT_MODULE_DEPENDS_IOS + SWIFT_MODULE_DEPENDS_LINUX + SWIFT_MODULE_DEPENDS_OSX + SWIFT_MODULE_DEPENDS_TVOS + SWIFT_MODULE_DEPENDS_WATCHOS + SWIFT_MODULE_DEPENDS_WASI + SWIFT_MODULE_DEPENDS_WINDOWS + SWIFT_MODULE_DEPENDS_FROM_SDK + TARGET_SDKS + SWIFT_COMPILE_FLAGS_MACCATALYST + SWIFT_MODULE_DEPENDS_MACCATALYST + SWIFT_MODULE_DEPENDS_MACCATALYST_UNZIPPERED) + + cmake_parse_arguments(SWIFTLIB + "${SWIFTLIB_options}" + "${SWIFTLIB_single_parameter_options}" + "${SWIFTLIB_multiple_parameter_options}" + ${ARGN}) + set(SWIFTLIB_SOURCES ${SWIFTLIB_UNPARSED_ARGUMENTS}) + + # Ensure it's impossible to build for macCatalyst without module dependencies + if(SWIFT_ENABLE_MACCATALYST AND SWIFTLIB_MACCATALYST_BUILD_FLAVOR) + if((NOT SWIFTLIB_MACCATALYST_BUILD_FLAVOR STREQUAL "zippered") OR + SWIFTLIB_SWIFT_MODULE_DEPENDS_OSX) + precondition(SWIFTLIB_SWIFT_MODULE_DEPENDS_MACCATALYST + MESSAGE "SWIFT_MODULE_DEPENDS_MACCATALYST is required when building for macCatalyst") + endif() + endif() + + # Infer arguments. + + if(SWIFTLIB_IS_SDK_OVERLAY) + set(SWIFTLIB_HAS_SWIFT_CONTENT TRUE) + set(SWIFTLIB_IS_STDLIB TRUE) + endif() + + # Standard library is always a target library. + if(SWIFTLIB_IS_STDLIB) + set(SWIFTLIB_HAS_SWIFT_CONTENT TRUE) + endif() + + # If target SDKs are not specified, build for all known SDKs. + if("${SWIFTLIB_TARGET_SDKS}" STREQUAL "") + set(SWIFTLIB_TARGET_SDKS ${SWIFT_SDKS}) + endif() + list_replace(SWIFTLIB_TARGET_SDKS ALL_APPLE_PLATFORMS "${SWIFT_APPLE_PLATFORMS}") + + # All Swift code depends on the standard library, except for the standard + # library itself. + if(SWIFTLIB_HAS_SWIFT_CONTENT AND NOT SWIFTLIB_IS_STDLIB_CORE) + list(APPEND SWIFTLIB_SWIFT_MODULE_DEPENDS Core) + + # swiftSwiftOnoneSupport does not depend on itself, obviously. + if(NOT ${name} STREQUAL swiftSwiftOnoneSupport) + # All Swift code depends on the SwiftOnoneSupport in non-optimized mode, + # except for the standard library itself. + is_build_type_optimized("${SWIFT_STDLIB_BUILD_TYPE}" optimized) + if(NOT optimized) + list(APPEND SWIFTLIB_SWIFT_MODULE_DEPENDS SwiftOnoneSupport) + endif() + endif() + endif() + + if((NOT "${SWIFT_BUILD_STDLIB}") AND + (NOT "${SWIFTLIB_SWIFT_MODULE_DEPENDS}" STREQUAL "")) + list(REMOVE_ITEM SWIFTLIB_SWIFT_MODULE_DEPENDS Core SwiftOnoneSupport) + endif() + + translate_flags(SWIFTLIB "${SWIFTLIB_options}") + precondition(SWIFTLIB_INSTALL_IN_COMPONENT MESSAGE "INSTALL_IN_COMPONENT is required") + + if(NOT SWIFTLIB_SHARED AND + NOT SWIFTLIB_STATIC AND + NOT SWIFTLIB_OBJECT_LIBRARY) + message(FATAL_ERROR + "Either SHARED, STATIC, or OBJECT_LIBRARY must be specified") + endif() + + # In the standard library and overlays, warn about implicit overrides + # as a reminder to consider when inherited protocols need different + # behavior for their requirements. + if (SWIFTLIB_IS_STDLIB) + list(APPEND SWIFTLIB_SWIFT_COMPILE_FLAGS "-warn-implicit-overrides") + endif() + + if(NOT SWIFT_BUILD_RUNTIME_WITH_HOST_COMPILER AND NOT BUILD_STANDALONE) + list(APPEND SWIFTLIB_DEPENDS clang) + endif() + + # If we are building this library for targets, loop through the various + # SDKs building the variants of this library. + list_intersect( + "${SWIFTLIB_TARGET_SDKS}" "${SWIFT_SDKS}" SWIFTLIB_TARGET_SDKS) + + foreach(sdk ${SWIFTLIB_TARGET_SDKS}) + if(NOT SWIFT_SDK_${sdk}_ARCHITECTURES) + # SWIFT_SDK_${sdk}_ARCHITECTURES is empty, so just continue + continue() + endif() + + # Skip building library for macOS if macCatalyst support is not enabled and the + # library only builds for macOS when macCatalyst is enabled. + if(NOT SWIFT_ENABLE_MACCATALYST AND + sdk STREQUAL "OSX" AND + SWIFTLIB_MACCATALYST_BUILD_FLAVOR STREQUAL "ios-like") + message(STATUS "Skipping OSX SDK for module ${name}") + continue() + endif() + + # Determine if/what macCatalyst build flavor we are + get_maccatalyst_build_flavor(maccatalyst_build_flavor + "${sdk}" "${SWIFTLIB_MACCATALYST_BUILD_FLAVOR}") + + set(maccatalyst_build_flavors) + if(NOT DEFINED maccatalyst_build_flavor) + list(APPEND maccatalyst_build_flavors "none") + elseif(maccatalyst_build_flavor STREQUAL "unzippered-twin") + list(APPEND maccatalyst_build_flavors "macos-like" "ios-like") + else() + list(APPEND maccatalyst_build_flavors "${maccatalyst_build_flavor}") + endif() + + # Loop over the build flavors for the this library. If it is an unzippered + # twin we'll build it twice: once for "macos-like" and once for "ios-like" + # flavors. + foreach(maccatalyst_build_flavor ${maccatalyst_build_flavors}) + if(maccatalyst_build_flavor STREQUAL "none") + unset(maccatalyst_build_flavor) + endif() + + set(THIN_INPUT_TARGETS) + + # Collect architecture agnostic SDK module dependencies + set(swiftlib_module_depends_flattened ${SWIFTLIB_SWIFT_MODULE_DEPENDS}) + if(${sdk} STREQUAL OSX) + if(DEFINED maccatalyst_build_flavor AND NOT maccatalyst_build_flavor STREQUAL "macos-like") + list(APPEND swiftlib_module_depends_flattened + ${SWIFTLIB_SWIFT_MODULE_DEPENDS_MACCATALYST}) + list(APPEND swiftlib_module_depends_flattened + ${SWIFTLIB_SWIFT_MODULE_DEPENDS_MACCATALYST_UNZIPPERED}) + else() + list(APPEND swiftlib_module_depends_flattened + ${SWIFTLIB_SWIFT_MODULE_DEPENDS_OSX}) + endif() + list(APPEND swiftlib_module_depends_flattened + ${SWIFTLIB_SWIFT_MODULE_DEPENDS_OSX}) + elseif(${sdk} STREQUAL IOS OR ${sdk} STREQUAL IOS_SIMULATOR) + list(APPEND swiftlib_module_depends_flattened + ${SWIFTLIB_SWIFT_MODULE_DEPENDS_IOS}) + elseif(${sdk} STREQUAL TVOS OR ${sdk} STREQUAL TVOS_SIMULATOR) + list(APPEND swiftlib_module_depends_flattened + ${SWIFTLIB_SWIFT_MODULE_DEPENDS_TVOS}) + elseif(${sdk} STREQUAL WATCHOS OR ${sdk} STREQUAL WATCHOS_SIMULATOR) + list(APPEND swiftlib_module_depends_flattened + ${SWIFTLIB_SWIFT_MODULE_DEPENDS_WATCHOS}) + elseif(${sdk} STREQUAL FREEBSD) + list(APPEND swiftlib_module_depends_flattened + ${SWIFTLIB_SWIFT_MODULE_DEPENDS_FREEBSD}) + elseif(${sdk} STREQUAL LINUX OR ${sdk} STREQUAL ANDROID) + list(APPEND swiftlib_module_depends_flattened + ${SWIFTLIB_SWIFT_MODULE_DEPENDS_LINUX}) + elseif(${sdk} STREQUAL CYGWIN) + list(APPEND swiftlib_module_depends_flattened + ${SWIFTLIB_SWIFT_MODULE_DEPENDS_CYGWIN}) + elseif(${sdk} STREQUAL HAIKU) + list(APPEND swiftlib_module_depends_flattened + ${SWIFTLIB_SWIFT_MODULE_DEPENDS_HAIKU}) + elseif(${sdk} STREQUAL WASI) + list(APPEND swiftlib_module_depends_flattened + ${SWIFTLIB_SWIFT_MODULE_DEPENDS_WASI}) + elseif(${sdk} STREQUAL WINDOWS) + list(APPEND swiftlib_module_depends_flattened + ${SWIFTLIB_SWIFT_MODULE_DEPENDS_WINDOWS}) + endif() + + # Collect architecture agnostic SDK framework dependencies + set(swiftlib_framework_depends_flattened ${SWIFTLIB_FRAMEWORK_DEPENDS}) + if(${sdk} STREQUAL OSX) + list(APPEND swiftlib_framework_depends_flattened + ${SWIFTLIB_FRAMEWORK_DEPENDS_OSX}) + elseif(${sdk} STREQUAL IOS OR ${sdk} STREQUAL IOS_SIMULATOR OR + ${sdk} STREQUAL TVOS OR ${sdk} STREQUAL TVOS_SIMULATOR) + list(APPEND swiftlib_framework_depends_flattened + ${SWIFTLIB_FRAMEWORK_DEPENDS_IOS_TVOS}) + endif() + + # Collect architecutre agnostic compiler flags + set(swiftlib_swift_compile_flags_all ${SWIFTLIB_SWIFT_COMPILE_FLAGS}) + if(${sdk} STREQUAL OSX) + list(APPEND swiftlib_swift_compile_flags_all + ${SWIFTLIB_SWIFT_COMPILE_FLAGS_OSX}) + elseif(${sdk} STREQUAL IOS OR ${sdk} STREQUAL IOS_SIMULATOR) + list(APPEND swiftlib_swift_compile_flags_all + ${SWIFTLIB_SWIFT_COMPILE_FLAGS_IOS}) + elseif(${sdk} STREQUAL TVOS OR ${sdk} STREQUAL TVOS_SIMULATOR) + list(APPEND swiftlib_swift_compile_flags_all + ${SWIFTLIB_SWIFT_COMPILE_FLAGS_TVOS}) + elseif(${sdk} STREQUAL WATCHOS OR ${sdk} STREQUAL WATCHOS_SIMULATOR) + list(APPEND swiftlib_swift_compile_flags_all + ${SWIFTLIB_SWIFT_COMPILE_FLAGS_WATCHOS}) + elseif(${sdk} STREQUAL LINUX) + list(APPEND swiftlib_swift_compile_flags_all + ${SWIFTLIB_SWIFT_COMPILE_FLAGS_LINUX}) + elseif(${sdk} STREQUAL WINDOWS) + # FIXME(SR2005) static and shared are not mutually exclusive; however + # since we do a single build of the sources, this doesn't work for + # building both simultaneously. Effectively, only shared builds are + # supported on windows currently. + if(SWIFTLIB_SHARED) + list(APPEND swiftlib_swift_compile_flags_all -D_WINDLL) + if(SWIFTLIB_IS_STDLIB_CORE) + list(APPEND swiftlib_swift_compile_flags_all -DswiftCore_EXPORTS) + endif() + elseif(SWIFTLIB_STATIC) + list(APPEND swiftlib_swift_compile_flags_all -D_LIB) + endif() + endif() + + + # Collect architecture agnostic SDK linker flags + set(swiftlib_link_flags_all ${SWIFTLIB_LINK_FLAGS}) + if(${sdk} STREQUAL IOS_SIMULATOR AND ${name} STREQUAL swiftMediaPlayer) + # message("DISABLING AUTOLINK FOR swiftMediaPlayer") + list(APPEND swiftlib_link_flags_all "-Xlinker" "-ignore_auto_link") + endif() + + # We unconditionally removed "-z,defs" from CMAKE_SHARED_LINKER_FLAGS in + # swift_common_standalone_build_config_llvm within + # SwiftSharedCMakeConfig.cmake, where it was added by a call to + # HandleLLVMOptions. + # + # Rather than applying it to all targets and libraries, we here add it + # back to supported targets and libraries only. This is needed for ELF + # targets only; however, RemoteMirror needs to build with undefined + # symbols. + if(${SWIFT_SDK_${sdk}_OBJECT_FORMAT} STREQUAL ELF AND + NOT ${name} STREQUAL swiftRemoteMirror) + list(APPEND swiftlib_link_flags_all "-Wl,-z,defs") + endif() + # Setting back linker flags which are not supported when making Android build on macOS cross-compile host. + if(SWIFTLIB_SHARED) + if(sdk IN_LIST SWIFT_APPLE_PLATFORMS) + list(APPEND swiftlib_link_flags_all "-dynamiclib -Wl,-headerpad_max_install_names") + elseif(${sdk} STREQUAL ANDROID) + list(APPEND swiftlib_link_flags_all "-shared") + # TODO: Instead of `lib${name}.so` find variable or target property which already have this value. + list(APPEND swiftlib_link_flags_all "-Wl,-soname,lib${name}.so") + endif() + endif() + + set(sdk_supported_archs + ${SWIFT_SDK_${sdk}_ARCHITECTURES} + ${SWIFT_SDK_${sdk}_MODULE_ARCHITECTURES}) + list(REMOVE_DUPLICATES sdk_supported_archs) + + # For each architecture supported by this SDK + foreach(arch ${sdk_supported_archs}) + # Configure variables for this subdirectory. + set(VARIANT_SUFFIX "-${SWIFT_SDK_${sdk}_LIB_SUBDIR}-${arch}") + set(VARIANT_NAME "${name}${VARIANT_SUFFIX}") + set(MODULE_VARIANT_SUFFIX "-swiftmodule${VARIANT_SUFFIX}") + set(MODULE_VARIANT_NAME "${name}${MODULE_VARIANT_SUFFIX}") + + # Configure macCatalyst flavor variables + if(DEFINED maccatalyst_build_flavor) + set(maccatalyst_variant_suffix "-${SWIFT_SDK_MACCATALYST_LIB_SUBDIR}-${arch}") + set(maccatalyst_variant_name "${name}${maccatalyst_variant_suffix}") + + set(maccatalyst_module_variant_suffix "-swiftmodule${maccatalyst_variant_suffix}") + set(maccatalyst_module_variant_name "${name}${maccatalyst_module_variant_suffix}") + endif() + + # Map dependencies over to the appropriate variants. + set(swiftlib_link_libraries) + foreach(lib ${SWIFTLIB_LINK_LIBRARIES}) + if(TARGET "${lib}${VARIANT_SUFFIX}") + list(APPEND swiftlib_link_libraries "${lib}${VARIANT_SUFFIX}") + else() + list(APPEND swiftlib_link_libraries "${lib}") + endif() + endforeach() + + # Swift compiles depend on swift modules, while links depend on + # linked libraries. Find targets for both of these here. + set(swiftlib_module_dependency_targets) + set(swiftlib_private_link_libraries_targets) + + if(NOT BUILD_STANDALONE) + foreach(mod ${swiftlib_module_depends_flattened}) + if(DEFINED maccatalyst_build_flavor) + if(maccatalyst_build_flavor STREQUAL "zippered") + # Zippered libraries are dependent on both the macCatalyst and normal macOS + # modules of their dependencies (which themselves must be zippered). + list(APPEND swiftlib_module_dependency_targets + "swift${mod}${maccatalyst_module_variant_suffix}") + list(APPEND swiftlib_module_dependency_targets + "swift${mod}${MODULE_VARIANT_SUFFIX}") + + # Zippered libraries link against their zippered library targets, which + # live (and are built in) the same location as normal macOS libraries. + list(APPEND swiftlib_private_link_libraries_targets + "swift${mod}${VARIANT_SUFFIX}") + elseif(maccatalyst_build_flavor STREQUAL "ios-like") + # iOS-like libraries depend on the macCatalyst modules of their dependencies + # regardless of whether the target is zippered or macCatalyst only. + list(APPEND swiftlib_module_dependency_targets + "swift${mod}${maccatalyst_module_variant_suffix}") + + # iOS-like libraries can link against either iOS-like library targets + # or zippered targets. + if(mod IN_LIST SWIFTLIB_SWIFT_MODULE_DEPENDS_MACCATALYST_UNZIPPERED) + list(APPEND swiftlib_private_link_libraries_targets + "swift${mod}${maccatalyst_variant_suffix}") + else() + list(APPEND swiftlib_private_link_libraries_targets + "swift${mod}${VARIANT_SUFFIX}") + endif() + else() + list(APPEND swiftlib_module_dependency_targets + "swift${mod}${MODULE_VARIANT_SUFFIX}") + + list(APPEND swiftlib_private_link_libraries_targets + "swift${mod}${VARIANT_SUFFIX}") + endif() + continue() + endif() + + list(APPEND swiftlib_module_dependency_targets + "swift${mod}${MODULE_VARIANT_SUFFIX}") + + list(APPEND swiftlib_private_link_libraries_targets + "swift${mod}${VARIANT_SUFFIX}") + endforeach() + endif() + + foreach(lib ${SWIFTLIB_PRIVATE_LINK_LIBRARIES}) + if(TARGET "${lib}${VARIANT_SUFFIX}") + list(APPEND swiftlib_private_link_libraries_targets + "${lib}${VARIANT_SUFFIX}") + else() + list(APPEND swiftlib_private_link_libraries_targets "${lib}") + endif() + endforeach() + + # Add PrivateFrameworks, rdar://28466433 + set(swiftlib_c_compile_flags_all ${SWIFTLIB_C_COMPILE_FLAGS}) + set(swiftlib_link_flags_all ${SWIFTLIB_LINK_FLAGS}) + + # Add flags to prepend framework search paths for the parallel framework + # hierarchy rooted at /System/iOSSupport/... + # These paths must come before their normal counterparts so that when compiling + # macCatalyst-only or unzippered-twin overlays the macCatalyst version + # of a framework is found and not the Mac version. + if(maccatalyst_build_flavor STREQUAL "ios-like" + OR (name STREQUAL "swiftXCTest" + AND maccatalyst_build_flavor STREQUAL "zippered")) + + # The path to find iOS-only frameworks (such as UIKit) under macCatalyst. + set(ios_support_frameworks_path "${SWIFT_SDK_${sdk}_PATH}/System/iOSSupport/System/Library/Frameworks/") + + list(APPEND swiftlib_swift_compile_flags_all "-Fsystem" "${ios_support_frameworks_path}") + list(APPEND swiftlib_c_compile_flags_all "-iframework" "${ios_support_frameworks_path}") + # We collate -F with the framework path to avoid unwanted deduplication + # of options by target_compile_options -- this way no undesired + # side effects are introduced should a new search path be added. + list(APPEND swiftlib_link_flags_all "-F${ios_support_frameworks_path}") + endif() + + if(sdk IN_LIST SWIFT_APPLE_PLATFORMS AND SWIFTLIB_IS_SDK_OVERLAY) + set(swiftlib_swift_compile_private_frameworks_flag "-Fsystem" "${SWIFT_SDK_${sdk}_ARCH_${arch}_PATH}/System/Library/PrivateFrameworks/") + foreach(tbd_lib ${SWIFTLIB_SWIFT_MODULE_DEPENDS_FROM_SDK}) + list(APPEND swiftlib_link_flags_all "${SWIFT_SDK_${sdk}_ARCH_${arch}_PATH}/usr/lib/swift/libswift${tbd_lib}.tbd") + endforeach() + endif() + + set(variant_name "${VARIANT_NAME}") + set(module_variant_names "${MODULE_VARIANT_NAME}") + if(maccatalyst_build_flavor STREQUAL "ios-like") + set(variant_name "${maccatalyst_variant_name}") + set(module_variant_names "${maccatalyst_module_variant_name}") + elseif(maccatalyst_build_flavor STREQUAL "zippered") + # Zippered libraries produce two modules: one for macCatalyst and one for macOS + # and so need two module targets. + list(APPEND module_variant_names "${maccatalyst_module_variant_name}") + endif() + + list(APPEND swiftlib_c_compile_flags_all "-DSWIFT_TARGET_LIBRARY_NAME=${name}") + + # Add this library variant. + _add_swift_target_library_single( + ${variant_name} + ${name} + ${SWIFTLIB_SHARED_keyword} + ${SWIFTLIB_STATIC_keyword} + ${SWIFTLIB_OBJECT_LIBRARY_keyword} + ${SWIFTLIB_INSTALL_WITH_SHARED_keyword} + ${SWIFTLIB_SOURCES} + TARGET_LIBRARY + MODULE_TARGETS ${module_variant_names} + SDK ${sdk} + ARCHITECTURE ${arch} + DEPENDS ${SWIFTLIB_DEPENDS} + LINK_LIBRARIES ${swiftlib_link_libraries} + FRAMEWORK_DEPENDS ${swiftlib_framework_depends_flattened} + FRAMEWORK_DEPENDS_WEAK ${SWIFTLIB_FRAMEWORK_DEPENDS_WEAK} + LLVM_LINK_COMPONENTS ${SWIFTLIB_LLVM_LINK_COMPONENTS} + FILE_DEPENDS ${SWIFTLIB_FILE_DEPENDS} ${swiftlib_module_dependency_targets} + C_COMPILE_FLAGS ${swiftlib_c_compile_flags_all} + SWIFT_COMPILE_FLAGS ${swiftlib_swift_compile_flags_all} ${swiftlib_swift_compile_flags_arch} ${swiftlib_swift_compile_private_frameworks_flag} + LINK_FLAGS ${swiftlib_link_flags_all} + PRIVATE_LINK_LIBRARIES ${swiftlib_private_link_libraries_targets} + INCORPORATE_OBJECT_LIBRARIES ${SWIFTLIB_INCORPORATE_OBJECT_LIBRARIES} + INCORPORATE_OBJECT_LIBRARIES_SHARED_ONLY ${SWIFTLIB_INCORPORATE_OBJECT_LIBRARIES_SHARED_ONLY} + ${SWIFTLIB_DONT_EMBED_BITCODE_keyword} + ${SWIFTLIB_IS_STDLIB_keyword} + ${SWIFTLIB_IS_STDLIB_CORE_keyword} + ${SWIFTLIB_IS_SDK_OVERLAY_keyword} + ${SWIFTLIB_NOSWIFTRT_keyword} + DARWIN_INSTALL_NAME_DIR "${SWIFTLIB_DARWIN_INSTALL_NAME_DIR}" + INSTALL_IN_COMPONENT "${SWIFTLIB_INSTALL_IN_COMPONENT}" + DEPLOYMENT_VERSION_OSX "${SWIFTLIB_DEPLOYMENT_VERSION_OSX}" + DEPLOYMENT_VERSION_MACCATALYST "${SWIFTLIB_DEPLOYMENT_VERSION_MACCATALYST}" + DEPLOYMENT_VERSION_IOS "${SWIFTLIB_DEPLOYMENT_VERSION_IOS}" + DEPLOYMENT_VERSION_TVOS "${SWIFTLIB_DEPLOYMENT_VERSION_TVOS}" + DEPLOYMENT_VERSION_WATCHOS "${SWIFTLIB_DEPLOYMENT_VERSION_WATCHOS}" + MACCATALYST_BUILD_FLAVOR "${maccatalyst_build_flavor}" + + GYB_SOURCES ${SWIFTLIB_GYB_SOURCES} + ) + if(NOT SWIFT_BUILT_STANDALONE AND NOT "${CMAKE_C_COMPILER_ID}" MATCHES "Clang") + add_dependencies(${VARIANT_NAME} clang) + endif() + + if(sdk STREQUAL WINDOWS) + if(SWIFT_COMPILER_IS_MSVC_LIKE) + if (SWIFT_STDLIB_MSVC_RUNTIME_LIBRARY MATCHES MultiThreadedDebugDLL) + target_compile_options(${VARIANT_NAME} PRIVATE /MDd /D_DLL /D_DEBUG) + elseif (SWIFT_STDLIB_MSVC_RUNTIME_LIBRARY MATCHES MultiThreadedDebug) + target_compile_options(${VARIANT_NAME} PRIVATE /MTd /U_DLL /D_DEBUG) + elseif (SWIFT_STDLIB_MSVC_RUNTIME_LIBRARY MATCHES MultiThreadedDLL) + target_compile_options(${VARIANT_NAME} PRIVATE /MD /D_DLL /U_DEBUG) + elseif (SWIFT_STDLIB_MSVC_RUNTIME_LIBRARY MATCHES MultiThreaded) + target_compile_options(${VARIANT_NAME} PRIVATE /MT /U_DLL /U_DEBUG) + endif() + endif() + endif() + + if(NOT SWIFTLIB_OBJECT_LIBRARY) + # Add dependencies on the (not-yet-created) custom lipo target. + foreach(DEP ${SWIFTLIB_LINK_LIBRARIES}) + if (NOT "${DEP}" STREQUAL "icucore") + add_dependencies(${VARIANT_NAME} + "${DEP}-${SWIFT_SDK_${sdk}_LIB_SUBDIR}") + endif() + endforeach() + + if (SWIFTLIB_IS_STDLIB AND SWIFTLIB_STATIC) + # Add dependencies on the (not-yet-created) custom lipo target. + foreach(DEP ${SWIFTLIB_LINK_LIBRARIES}) + if (NOT "${DEP}" STREQUAL "icucore") + add_dependencies("${VARIANT_NAME}-static" + "${DEP}-${SWIFT_SDK_${sdk}_LIB_SUBDIR}-static") + endif() + endforeach() + endif() + + if(arch IN_LIST SWIFT_SDK_${sdk}_ARCHITECTURES) + # Note this thin library. + list(APPEND THIN_INPUT_TARGETS ${VARIANT_NAME}) + endif() + endif() + endforeach() + + # Configure module-only targets + if(NOT SWIFT_SDK_${sdk}_ARCHITECTURES + AND SWIFT_SDK_${sdk}_MODULE_ARCHITECTURES) + set(_target "${name}-${SWIFT_SDK_${sdk}_LIB_SUBDIR}") + + # Create unified sdk target + add_custom_target("${_target}") + + foreach(_arch ${SWIFT_SDK_${sdk}_MODULE_ARCHITECTURES}) + set(_variant_suffix "-${SWIFT_SDK_${sdk}_LIB_SUBDIR}-${_arch}") + set(_module_variant_name "${name}-swiftmodule-${_variant_suffix}") + + add_dependencies("${_target}" ${_module_variant_name}) + + # Add Swift standard library targets as dependencies to the top-level + # convenience target. + if(TARGET "swift-stdlib${_variant_suffix}") + add_dependencies("swift-stdlib${_variant_suffix}" + "${_target}") + endif() + endforeach() + + return() + endif() + + set(library_subdir "${SWIFT_SDK_${sdk}_LIB_SUBDIR}") + if(maccatalyst_build_flavor STREQUAL "ios-like") + set(library_subdir "${SWIFT_SDK_MACCATALYST_LIB_SUBDIR}") + endif() + + if(NOT SWIFTLIB_OBJECT_LIBRARY) + # Determine the name of the universal library. + if(SWIFTLIB_SHARED) + if("${sdk}" STREQUAL "WINDOWS") + set(UNIVERSAL_LIBRARY_NAME + "${SWIFTLIB_DIR}/${library_subdir}/${name}.dll") + else() + set(UNIVERSAL_LIBRARY_NAME + "${SWIFTLIB_DIR}/${library_subdir}/${CMAKE_SHARED_LIBRARY_PREFIX}${name}${CMAKE_SHARED_LIBRARY_SUFFIX}") + endif() + else() + if("${sdk}" STREQUAL "WINDOWS") + set(UNIVERSAL_LIBRARY_NAME + "${SWIFTLIB_DIR}/${library_subdir}/${name}.lib") + else() + set(UNIVERSAL_LIBRARY_NAME + "${SWIFTLIB_DIR}/${library_subdir}/${CMAKE_STATIC_LIBRARY_PREFIX}${name}${CMAKE_STATIC_LIBRARY_SUFFIX}") + endif() + endif() + + set(lipo_target "${name}-${library_subdir}") + if("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin" AND SWIFTLIB_SHARED) + set(codesign_arg CODESIGN) + endif() + precondition(THIN_INPUT_TARGETS) + _add_swift_lipo_target(SDK + ${sdk} + TARGET + ${lipo_target} + OUTPUT + ${UNIVERSAL_LIBRARY_NAME} + ${codesign_arg} + ${THIN_INPUT_TARGETS}) + + # Cache universal libraries for dependency purposes + set(UNIVERSAL_LIBRARY_NAMES_${library_subdir} + ${UNIVERSAL_LIBRARY_NAMES_${library_subdir}} + ${lipo_target} + CACHE INTERNAL "UNIVERSAL_LIBRARY_NAMES_${library_subdir}") + + # Determine the subdirectory where this library will be installed. + set(resource_dir_sdk_subdir "${SWIFT_SDK_${sdk}_LIB_SUBDIR}") + if(maccatalyst_build_flavor STREQUAL "ios-like") + set(resource_dir_sdk_subdir "${SWIFT_SDK_MACCATALYST_LIB_SUBDIR}") + endif() + + precondition(resource_dir_sdk_subdir) + + if(SWIFTLIB_SHARED OR SWIFTLIB_INSTALL_WITH_SHARED) + set(resource_dir "swift") + set(file_permissions + OWNER_READ OWNER_WRITE OWNER_EXECUTE + GROUP_READ GROUP_EXECUTE + WORLD_READ WORLD_EXECUTE) + else() + set(resource_dir "swift_static") + set(file_permissions + OWNER_READ OWNER_WRITE + GROUP_READ + WORLD_READ) + endif() + + set(optional_arg) + if(sdk IN_LIST SWIFT_APPLE_PLATFORMS) + # Allow installation of stdlib without building all variants on Darwin. + set(optional_arg "OPTIONAL") + endif() + + if(sdk STREQUAL WINDOWS AND CMAKE_SYSTEM_NAME STREQUAL Windows) + add_dependencies(${SWIFTLIB_INSTALL_IN_COMPONENT} ${name}-windows-${SWIFT_PRIMARY_VARIANT_ARCH}) + swift_install_in_component(TARGETS ${name}-windows-${SWIFT_PRIMARY_VARIANT_ARCH} + RUNTIME + DESTINATION "bin" + COMPONENT "${SWIFTLIB_INSTALL_IN_COMPONENT}" + LIBRARY + DESTINATION "lib${LLVM_LIBDIR_SUFFIX}/${resource_dir}/${resource_dir_sdk_subdir}/${SWIFT_PRIMARY_VARIANT_ARCH}" + COMPONENT "${SWIFTLIB_INSTALL_IN_COMPONENT}" + ARCHIVE + DESTINATION "lib${LLVM_LIBDIR_SUFFIX}/${resource_dir}/${resource_dir_sdk_subdir}/${SWIFT_PRIMARY_VARIANT_ARCH}" + COMPONENT "${SWIFTLIB_INSTALL_IN_COMPONENT}" + PERMISSIONS ${file_permissions}) + else() + # NOTE: ${UNIVERSAL_LIBRARY_NAME} is the output associated with the target + # ${lipo_target} + add_dependencies(${SWIFTLIB_INSTALL_IN_COMPONENT} ${lipo_target}) + swift_install_in_component(FILES "${UNIVERSAL_LIBRARY_NAME}" + DESTINATION "lib${LLVM_LIBDIR_SUFFIX}/${resource_dir}/${resource_dir_sdk_subdir}" + COMPONENT "${SWIFTLIB_INSTALL_IN_COMPONENT}" + PERMISSIONS ${file_permissions} + "${optional_arg}") + endif() + if(sdk STREQUAL WINDOWS) + foreach(arch ${SWIFT_SDK_WINDOWS_ARCHITECTURES}) + if(TARGET ${name}-windows-${arch}_IMPLIB) + get_target_property(import_library ${name}-windows-${arch}_IMPLIB IMPORTED_LOCATION) + add_dependencies(${SWIFTLIB_INSTALL_IN_COMPONENT} ${name}-windows-${arch}_IMPLIB) + swift_install_in_component(FILES ${import_library} + DESTINATION "lib${LLVM_LIBDIR_SUFFIX}/${resource_dir}/${resource_dir_sdk_subdir}/${arch}" + COMPONENT ${SWIFTLIB_INSTALL_IN_COMPONENT} + PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ) + endif() + endforeach() + endif() + + swift_is_installing_component( + "${SWIFTLIB_INSTALL_IN_COMPONENT}" + is_installing) + + # Add the arch-specific library targets to the global exports. + foreach(arch ${SWIFT_SDK_${sdk}_ARCHITECTURES}) + set(_variant_name "${name}-${SWIFT_SDK_${sdk}_LIB_SUBDIR}-${arch}") + if(maccatalyst_build_flavor STREQUAL "ios-like") + set(_variant_name "${name}-${SWIFT_SDK_MACCATALYST_LIB_SUBDIR}-${arch}") + endif() + + if(NOT TARGET "${_variant_name}") + continue() + endif() + + if(is_installing) + set_property(GLOBAL APPEND + PROPERTY SWIFT_EXPORTS ${_variant_name}) + else() + set_property(GLOBAL APPEND + PROPERTY SWIFT_BUILDTREE_EXPORTS ${_variant_name}) + endif() + endforeach() + + # Add the swiftmodule-only targets to the lipo target depdencies. + foreach(arch ${SWIFT_SDK_${sdk}_MODULE_ARCHITECTURES}) + set(_variant_name "${name}-${SWIFT_SDK_${sdk}_LIB_SUBDIR}-${arch}") + if(maccatalyst_build_flavor STREQUAL "ios-like") + set(_variant_name "${name}-${SWIFT_SDK_MACCATALYST_LIB_SUBDIR}-${arch}") + endif() + + if(NOT TARGET "${_variant_name}") + continue() + endif() + + add_dependencies("${lipo_target}" "${_variant_name}") + endforeach() + + # If we built static variants of the library, create a lipo target for + # them. + set(lipo_target_static) + if (SWIFTLIB_IS_STDLIB AND SWIFTLIB_STATIC) + set(THIN_INPUT_TARGETS_STATIC) + foreach(TARGET ${THIN_INPUT_TARGETS}) + list(APPEND THIN_INPUT_TARGETS_STATIC "${TARGET}-static") + endforeach() + + if(SWIFTLIB_INSTALL_WITH_SHARED) + set(install_subdir "swift") + set(universal_subdir ${SWIFTLIB_DIR}) + else() + set(install_subdir "swift_static") + set(universal_subdir ${SWIFTSTATICLIB_DIR}) + endif() + + set(lipo_target_static + "${name}-${library_subdir}-static") + set(UNIVERSAL_LIBRARY_NAME + "${universal_subdir}/${library_subdir}/${CMAKE_STATIC_LIBRARY_PREFIX}${name}${CMAKE_STATIC_LIBRARY_SUFFIX}") + _add_swift_lipo_target(SDK + ${sdk} + TARGET + ${lipo_target_static} + OUTPUT + "${UNIVERSAL_LIBRARY_NAME}" + ${THIN_INPUT_TARGETS_STATIC}) + add_dependencies(${SWIFTLIB_INSTALL_IN_COMPONENT} ${lipo_target_static}) + swift_install_in_component(FILES "${UNIVERSAL_LIBRARY_NAME}" + DESTINATION "lib${LLVM_LIBDIR_SUFFIX}/${install_subdir}/${resource_dir_sdk_subdir}" + PERMISSIONS + OWNER_READ OWNER_WRITE + GROUP_READ + WORLD_READ + COMPONENT "${SWIFTLIB_INSTALL_IN_COMPONENT}" + "${optional_arg}") + endif() + + # Add Swift standard library targets as dependencies to the top-level + # convenience target. + set(FILTERED_UNITTESTS + swiftStdlibCollectionUnittest + swiftStdlibUnicodeUnittest) + + foreach(arch ${SWIFT_SDK_${sdk}_ARCHITECTURES}) + set(VARIANT_SUFFIX "-${SWIFT_SDK_${sdk}_LIB_SUBDIR}-${arch}") + if(TARGET "swift-stdlib${VARIANT_SUFFIX}" AND + TARGET "swift-test-stdlib${VARIANT_SUFFIX}") + add_dependencies("swift-stdlib${VARIANT_SUFFIX}" + ${lipo_target} + ${lipo_target_static}) + if(NOT "${name}" IN_LIST FILTERED_UNITTESTS) + add_dependencies("swift-test-stdlib${VARIANT_SUFFIX}" + ${lipo_target} + ${lipo_target_static}) + endif() + endif() + endforeach() + endif() + endforeach() # maccatalyst_build_flavors + endforeach() +endfunction() + # Add an executable compiled for a given variant. # # Don't use directly, use add_swift_executable and add_swift_target_executable From efc78a76887b8bad5b29dfcb5108dbf101b7ca72 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Tue, 3 Mar 2020 01:03:22 +0000 Subject: [PATCH 124/838] [WASM] Build installable toolchain --- .github/workflows/main.yml | 4 +-- utils/webassembly/build-linux.sh | 4 --- utils/webassembly/build-mac.sh | 4 --- utils/webassembly/build-toolchain.sh | 43 ++++++++++++++++++++++++++++ utils/webassembly/ci.sh | 4 +-- 5 files changed, 47 insertions(+), 12 deletions(-) create mode 100755 utils/webassembly/build-toolchain.sh diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 7111d2f13f2c8..d50b6192dd39e 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -27,7 +27,7 @@ jobs: uses: actions/upload-artifact@v1 with: name: linux-installable - path: ../swiftwasm-linux.tar.gz + path: ../swift-wasm-5.2-DEVELOPMENT-SNAPSHOT-*-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 @@ -54,7 +54,7 @@ jobs: uses: actions/upload-artifact@v1 with: name: macos-installable - path: ../swiftwasm-macos.tar.gz + path: ../swift-wasm-5.2-DEVELOPMENT-SNAPSHOT-*-osx.tar.gz - name: Upload packaging scripts uses: actions/upload-artifact@v1 with: diff --git a/utils/webassembly/build-linux.sh b/utils/webassembly/build-linux.sh index 7aa8b90e26cfe..9050c2bed100a 100755 --- a/utils/webassembly/build-linux.sh +++ b/utils/webassembly/build-linux.sh @@ -20,10 +20,6 @@ $SWIFT_PATH/utils/build-script --wasm \ --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" \ diff --git a/utils/webassembly/build-mac.sh b/utils/webassembly/build-mac.sh index 1fef9d76ac8d6..43824be3bc073 100755 --- a/utils/webassembly/build-mac.sh +++ b/utils/webassembly/build-mac.sh @@ -28,8 +28,4 @@ $SWIFT_PATH/utils/build-script --wasm \ --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="$SOURCE_PATH/install" \ - --installable-package="$SOURCE_PATH/swiftwasm-macos.tar.gz" \ "$@" diff --git a/utils/webassembly/build-toolchain.sh b/utils/webassembly/build-toolchain.sh new file mode 100755 index 0000000000000..6d90d0ff2faa9 --- /dev/null +++ b/utils/webassembly/build-toolchain.sh @@ -0,0 +1,43 @@ +#/bin/bash + +set -x +SOURCE_PATH="$(cd "$(dirname $0)/../../.." && pwd)" +UTILS_PATH="$(cd "$(dirname $0)" && pwd)" + +case $(uname -s) in + Darwin) + OS_SUFFIX=osx + BUILD_SCRIPT=$UTILS_PATH/build-mac.sh + ;; + Linux) + OS_SUFFIX=linux + BUILD_SCRIPT=$UTILS_PATH/build-linux.sh + ;; + *) + echo "Unrecognised platform $(uname -s)" + exit 1 + ;; +esac + +YEAR=$(date +"%Y") +MONTH=$(date +"%m") +DAY=$(date +"%d") +TOOLCHAIN_VERSION="5.2.${YEAR}${MONTH}${DAY}" +TOOLCHAIN_NAME="swift-wasm-5.2-DEVELOPMENT-SNAPSHOT-${YEAR}-${MONTH}-${DAY}-a" +ARCHIVE="${TOOLCHAIN_NAME}-${OS_SUFFIX}.tar.gz" + +BUNDLE_IDENTIFIER="${BUNDLE_PREFIX}.${YEAR}${MONTH}${DAY}" +DISPLAY_NAME_SHORT="Swift for WebAssembly Development Snapshot" +DISPLAY_NAME="${DISPLAY_NAME_SHORT} ${YEAR}-${MONTH}-${DAY}" + +$BUILD_SCRIPT \ + --install_destdir="$SOURCE_PATH/install" \ + --installable_package="$SOURCE_PATH/$ARCHIVE" \ + --install-prefix=/$TOOLCHAIN_NAME/usr \ + --install-swift \ + --darwin-toolchain-bundle-identifier="${BUNDLE_IDENTIFIER}" \ + --darwin-toolchain-display-name="${DISPLAY_NAME}" \ + --darwin-toolchain-display-name-short="${DISPLAY_NAME_SHORT}" \ + --darwin-toolchain-name="${TOOLCHAIN_NAME}" \ + --darwin-toolchain-version="${TOOLCHAIN_VERSION}" \ + "$@" diff --git a/utils/webassembly/ci.sh b/utils/webassembly/ci.sh index ca7193df2d82c..f5336bf429533 100755 --- a/utils/webassembly/ci.sh +++ b/utils/webassembly/ci.sh @@ -6,13 +6,13 @@ SOURCE_PATH="$( cd "$(dirname $0)/../../.." && pwd )" SWIFT_PATH=$SOURCE_PATH/swift UTILS_PATH=$SWIFT_PATH/utils/webassembly if [[ "$(uname)" == "Linux" ]]; then - BUILD_SCRIPT=$UTILS_PATH/build-linux.sh DEPENDENCIES_SCRIPT=$UTILS_PATH/linux/install-dependencies.sh else - BUILD_SCRIPT=$UTILS_PATH/build-mac.sh DEPENDENCIES_SCRIPT=$UTILS_PATH/macos/install-dependencies.sh fi +BUILD_SCRIPT=$UTILS_PATH/build-toolchain.sh + $DEPENDENCIES_SCRIPT export SCCACHE_CACHE_SIZE="50G" From 2cee201a6243473ee6e53d4eb3717bd41a422f82 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Tue, 3 Mar 2020 02:01:11 +0000 Subject: [PATCH 125/838] [WASM] Merge wasi-sdk and toolchain --- .github/workflows/main.yml | 4 ++-- utils/webassembly/build-toolchain.sh | 20 +++++++++++++++++++- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index d50b6192dd39e..ae762d74b7cfe 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -27,7 +27,7 @@ jobs: uses: actions/upload-artifact@v1 with: name: linux-installable - path: ../swift-wasm-5.2-DEVELOPMENT-SNAPSHOT-*-linux.tar.gz + path: ../swift-wasm-5.2-DEVELOPMENT-SNAPSHOT-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 @@ -54,7 +54,7 @@ jobs: uses: actions/upload-artifact@v1 with: name: macos-installable - path: ../swift-wasm-5.2-DEVELOPMENT-SNAPSHOT-*-osx.tar.gz + path: ../swift-wasm-5.2-DEVELOPMENT-SNAPSHOT-osx.tar.gz - name: Upload packaging scripts uses: actions/upload-artifact@v1 with: diff --git a/utils/webassembly/build-toolchain.sh b/utils/webassembly/build-toolchain.sh index 6d90d0ff2faa9..975a150d26135 100755 --- a/utils/webassembly/build-toolchain.sh +++ b/utils/webassembly/build-toolchain.sh @@ -4,6 +4,8 @@ set -x SOURCE_PATH="$(cd "$(dirname $0)/../../.." && pwd)" UTILS_PATH="$(cd "$(dirname $0)" && pwd)" +WASI_SDK_PATH=$SOURCE_PATH/wasi-sdk + case $(uname -s) in Darwin) OS_SUFFIX=osx @@ -25,6 +27,9 @@ DAY=$(date +"%d") TOOLCHAIN_VERSION="5.2.${YEAR}${MONTH}${DAY}" TOOLCHAIN_NAME="swift-wasm-5.2-DEVELOPMENT-SNAPSHOT-${YEAR}-${MONTH}-${DAY}-a" ARCHIVE="${TOOLCHAIN_NAME}-${OS_SUFFIX}.tar.gz" +INSTALLABLE_PACKAGE=$SOURCE_PATH/$ARCHIVE + +PACKAGE_ARTIFACT="$SOURCE_PATH/swift-wasm-5.2-DEVELOPMENT-SNAPSHOT-${OS_SUFFIX}.tar.gz" BUNDLE_IDENTIFIER="${BUNDLE_PREFIX}.${YEAR}${MONTH}${DAY}" DISPLAY_NAME_SHORT="Swift for WebAssembly Development Snapshot" @@ -32,7 +37,7 @@ DISPLAY_NAME="${DISPLAY_NAME_SHORT} ${YEAR}-${MONTH}-${DAY}" $BUILD_SCRIPT \ --install_destdir="$SOURCE_PATH/install" \ - --installable_package="$SOURCE_PATH/$ARCHIVE" \ + --installable_package="$INSTALLABLE_PACKAGE" \ --install-prefix=/$TOOLCHAIN_NAME/usr \ --install-swift \ --darwin-toolchain-bundle-identifier="${BUNDLE_IDENTIFIER}" \ @@ -41,3 +46,16 @@ $BUILD_SCRIPT \ --darwin-toolchain-name="${TOOLCHAIN_NAME}" \ --darwin-toolchain-version="${TOOLCHAIN_VERSION}" \ "$@" + +TMP_DIR=$(mktemp -d) +cd $TMP_DIR +tar xfz $INSTALLABLE_PACKAGE $TOOLCHAIN_NAME +cd $TMP_DIR/$TOOLCHAIN_NAME + +# Merge wasi-sdk and toolchain +cp -r $WASI_SDK_PATH/lib/clang usr/lib +cp $WASI_SDK_PATH/bin/* usr/bin +cp -r $WASI_SDK_PATH/share/wasi-sysroot usr/share + +cd $TMP_DIR +tar cfz $PACKAGE_ARTIFACT $TOOLCHAIN_NAME From 078f4a8cbb7e620914f2db985f1f76c01f8ca5a7 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Thu, 5 Mar 2020 10:19:46 +0000 Subject: [PATCH 126/838] Remove packing job from CI --- .github/workflows/main.yml | 78 +++--------------- utils/webassembly/build-linux-package.sh | 9 -- utils/webassembly/build-mac-package.sh | 9 -- utils/webassembly/build-packages.sh | 5 -- .../download-installable-prebuilts.sh | 7 -- utils/webassembly/download-prebuilts.sh | 8 -- .../sdkroot/extra_objs/fakelocaltime.o | Bin 423 -> 0 bytes .../sdkroot/extra_objs/fakepthread.o | Bin 4061 -> 0 bytes 8 files changed, 12 insertions(+), 104 deletions(-) delete mode 100755 utils/webassembly/build-linux-package.sh delete mode 100755 utils/webassembly/build-mac-package.sh delete mode 100755 utils/webassembly/build-packages.sh delete mode 100755 utils/webassembly/download-installable-prebuilts.sh delete mode 100755 utils/webassembly/download-prebuilts.sh delete mode 100644 utils/webassembly/sdkroot/extra_objs/fakelocaltime.o delete mode 100644 utils/webassembly/sdkroot/extra_objs/fakepthread.o diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index ae762d74b7cfe..a9a144757974a 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -67,83 +67,29 @@ jobs: with: name: macos-test-results path: ./swift-test-results.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: Download packaging scripts - uses: actions/download-artifact@v1 - with: - name: packaging-scripts - - name: Build the packages - shell: bash - run: | - cd packaging-scripts - find . -name '*.sh' -exec chmod +x {} \; - chmod +x sdkroot/swiftwasm - ./download-prebuilts.sh - - 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 && echo "Successfully unpacked Linux SDK" - - cd swiftwasm-sdk - ./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: packaging-scripts/output/swiftwasm-sdk-macos.tar.xz - - - name: Upload Linux package - uses: actions/upload-artifact@v1 - with: - name: linux-package - 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: packaging-scripts/output/swiftwasm-sdk/hello.wasm - macos_smoke_test: name: Compile hello.swift on macOS runs-on: macos-latest - needs: package + needs: macos_build steps: - - name: Download SwiftWasm macOS package + - name: Download installable macOS archive uses: actions/download-artifact@v1 with: - name: macos-package - + name: macos-installable - 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" + set -x + tar xf $(find -name "swift-wasm-*.tar.gz" -type f) + TOOLCHAIN_PATH=$(find -name "swift-wasm-*" -type d) + echo 'print("Hello, world!")' > hello.swift + $TOOLCHAIN_PATH/usr/bin/swiftc \ + -target wasm32-unknown-wasi \ + hello.swift -o 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 + path: hello.wasm diff --git a/utils/webassembly/build-linux-package.sh b/utils/webassembly/build-linux-package.sh deleted file mode 100755 index 685d5c247a18e..0000000000000 --- a/utils/webassembly/build-linux-package.sh +++ /dev/null @@ -1,9 +0,0 @@ -#!/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 deleted file mode 100755 index cd3c2602e8b5d..0000000000000 --- a/utils/webassembly/build-mac-package.sh +++ /dev/null @@ -1,9 +0,0 @@ -#!/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 deleted file mode 100755 index e83fc38beb5d6..0000000000000 --- a/utils/webassembly/build-packages.sh +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/bash -set -e -rm -rf output || true -./build-linux-package.sh -./build-mac-package.sh diff --git a/utils/webassembly/download-installable-prebuilts.sh b/utils/webassembly/download-installable-prebuilts.sh deleted file mode 100755 index c407580547fc4..0000000000000 --- a/utils/webassembly/download-installable-prebuilts.sh +++ /dev/null @@ -1,7 +0,0 @@ -#!/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 deleted file mode 100755 index 5ee1c8b3bf756..0000000000000 --- a/utils/webassembly/download-prebuilts.sh +++ /dev/null @@ -1,8 +0,0 @@ -#!/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/sdkroot/extra_objs/fakelocaltime.o b/utils/webassembly/sdkroot/extra_objs/fakelocaltime.o deleted file mode 100644 index 904d3a6dedc6108030be87a4d3012e63e153ed9a..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 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 From dbaf868b73c2177df147123d008c08954853f213 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Thu, 5 Mar 2020 13:35:09 +0000 Subject: [PATCH 127/838] Omit swift version and add toolchain alias --- .github/workflows/main.yml | 4 ++-- utils/webassembly/build-swiftpm.sh | 1 + utils/webassembly/build-toolchain.sh | 7 ++++--- 3 files changed, 7 insertions(+), 5 deletions(-) create mode 100644 utils/webassembly/build-swiftpm.sh diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index a9a144757974a..c72b1f0c49176 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -27,7 +27,7 @@ jobs: uses: actions/upload-artifact@v1 with: name: linux-installable - path: ../swift-wasm-5.2-DEVELOPMENT-SNAPSHOT-linux.tar.gz + path: ../swift-wasm-DEVELOPMENT-SNAPSHOT-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 @@ -54,7 +54,7 @@ jobs: uses: actions/upload-artifact@v1 with: name: macos-installable - path: ../swift-wasm-5.2-DEVELOPMENT-SNAPSHOT-osx.tar.gz + path: ../swift-wasm-DEVELOPMENT-SNAPSHOT-osx.tar.gz - name: Upload packaging scripts uses: actions/upload-artifact@v1 with: diff --git a/utils/webassembly/build-swiftpm.sh b/utils/webassembly/build-swiftpm.sh new file mode 100644 index 0000000000000..01058050e15ab --- /dev/null +++ b/utils/webassembly/build-swiftpm.sh @@ -0,0 +1 @@ +SOURCE_PATH="$(cd "$(dirname $0)/../../.." && pwd)" diff --git a/utils/webassembly/build-toolchain.sh b/utils/webassembly/build-toolchain.sh index 975a150d26135..6cf721eb725e2 100755 --- a/utils/webassembly/build-toolchain.sh +++ b/utils/webassembly/build-toolchain.sh @@ -24,12 +24,12 @@ esac YEAR=$(date +"%Y") MONTH=$(date +"%m") DAY=$(date +"%d") -TOOLCHAIN_VERSION="5.2.${YEAR}${MONTH}${DAY}" -TOOLCHAIN_NAME="swift-wasm-5.2-DEVELOPMENT-SNAPSHOT-${YEAR}-${MONTH}-${DAY}-a" +TOOLCHAIN_VERSION="${YEAR}${MONTH}${DAY}" +TOOLCHAIN_NAME="swift-wasm-DEVELOPMENT-SNAPSHOT-${YEAR}-${MONTH}-${DAY}-a" ARCHIVE="${TOOLCHAIN_NAME}-${OS_SUFFIX}.tar.gz" INSTALLABLE_PACKAGE=$SOURCE_PATH/$ARCHIVE -PACKAGE_ARTIFACT="$SOURCE_PATH/swift-wasm-5.2-DEVELOPMENT-SNAPSHOT-${OS_SUFFIX}.tar.gz" +PACKAGE_ARTIFACT="$SOURCE_PATH/swift-wasm-DEVELOPMENT-SNAPSHOT-${OS_SUFFIX}.tar.gz" BUNDLE_IDENTIFIER="${BUNDLE_PREFIX}.${YEAR}${MONTH}${DAY}" DISPLAY_NAME_SHORT="Swift for WebAssembly Development Snapshot" @@ -45,6 +45,7 @@ $BUILD_SCRIPT \ --darwin-toolchain-display-name-short="${DISPLAY_NAME_SHORT}" \ --darwin-toolchain-name="${TOOLCHAIN_NAME}" \ --darwin-toolchain-version="${TOOLCHAIN_VERSION}" \ + --darwin-toolchain-alias="swift" \ "$@" TMP_DIR=$(mktemp -d) From db1ee080972837123bbaeac4b356d05e2b7090c9 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Thu, 5 Mar 2020 23:08:15 +0000 Subject: [PATCH 128/838] Fix find command args --- .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 c72b1f0c49176..46653cc122812 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -80,8 +80,8 @@ jobs: shell: bash run: | set -x - tar xf $(find -name "swift-wasm-*.tar.gz" -type f) - TOOLCHAIN_PATH=$(find -name "swift-wasm-*" -type d) + tar xf $(find . -name "swift-wasm-*.tar.gz" -type f) + TOOLCHAIN_PATH=$(find . -name "swift-wasm-*" -type d) echo 'print("Hello, world!")' > hello.swift $TOOLCHAIN_PATH/usr/bin/swiftc \ -target wasm32-unknown-wasi \ From 0cc2fb1abf3750bfa43c8401de20e0ce78a09fc3 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Thu, 5 Mar 2020 23:30:40 +0000 Subject: [PATCH 129/838] Install SwiftPM in toolchain --- utils/webassembly/build-swiftpm.sh | 53 +++++++++++++++++++ utils/webassembly/build-toolchain.sh | 7 +++ utils/webassembly/ci.sh | 2 +- .../webassembly/install-nightly-toolchain.sh | 50 +++++++++++++++++ 4 files changed, 111 insertions(+), 1 deletion(-) mode change 100644 => 100755 utils/webassembly/build-swiftpm.sh create mode 100755 utils/webassembly/install-nightly-toolchain.sh diff --git a/utils/webassembly/build-swiftpm.sh b/utils/webassembly/build-swiftpm.sh old mode 100644 new mode 100755 index 01058050e15ab..f258f4ea664fb --- a/utils/webassembly/build-swiftpm.sh +++ b/utils/webassembly/build-swiftpm.sh @@ -1 +1,54 @@ +#!/bin/bash +set -e +DESTINATION_TOOLCHAIN=$1 SOURCE_PATH="$(cd "$(dirname $0)/../../.." && pwd)" +NIGHTLY_TOOLCHAIN=${SOURCE_PATH}/swift-nightly-toolchain + +export PATH=$NIGHTLY_TOOLCHAIN/usr/bin:$PATH + +SWIFT_BUILD_FLAGS="-c release" +SWIFT_BUILD=${NIGHTLY_TOOLCHAIN}/usr/bin/swift-build + +build_swiftpm() { + cd ${SOURCE_PATH}/swiftpm + ${SWIFT_BUILD} ${SWIFT_BUILD_FLAGS} +} + +install_binary() { + local src=$1 + local dest=${DESTINATION_TOOLCHAIN}/usr/bin + echo "Installing ${src} to ${dest}" + cp ${src} ${dest} +} + +install_runtime_file() { + local src=$1 + local dest=${DESTINATION_TOOLCHAIN}/usr/lib/swift/pm + echo "Installing ${src} to ${dest}/{4,4_2}" + for runtime in "4" "4_2" + do + mkdir -p ${dest}/${runtime} + cp ${src} ${dest}/${runtime} + done +} + +install_swiftpm() { + cd ${SOURCE_PATH}/swiftpm + local bin_path=$(${SWIFT_BUILD} ${SWIFT_BUILD_FLAGS} --show-bin-path) + for binary in ${bin_path}/{swift-build,swift-test,swift-run,swift-package} + do + install_binary ${binary} + done + + if [[ "$(uname)" == "Linux" ]]; then + install_runtime_file "${bin_path}/libPackageDescription.so" + install_runtime_file "${bin_path}/PackageDescription.swiftmodule" + install_runtime_file "${bin_path}/PackageDescription.swiftdoc" + else + install_runtime_file "${bin_path}/libPackageDescription.dylib" + install_runtime_file "${bin_path}/PackageDescription.swiftinterface" + fi +} + +build_swiftpm +install_swiftpm diff --git a/utils/webassembly/build-toolchain.sh b/utils/webassembly/build-toolchain.sh index 6cf721eb725e2..b7d3aa7c1fc62 100755 --- a/utils/webassembly/build-toolchain.sh +++ b/utils/webassembly/build-toolchain.sh @@ -48,6 +48,11 @@ $BUILD_SCRIPT \ --darwin-toolchain-alias="swift" \ "$@" + +if [ ! -e $SOURCE_PATH/swift-nightly-toolchain ]; then + $UTILS_PATH/install-nightly-toolchain.sh +fi + TMP_DIR=$(mktemp -d) cd $TMP_DIR tar xfz $INSTALLABLE_PACKAGE $TOOLCHAIN_NAME @@ -58,5 +63,7 @@ cp -r $WASI_SDK_PATH/lib/clang usr/lib cp $WASI_SDK_PATH/bin/* usr/bin cp -r $WASI_SDK_PATH/share/wasi-sysroot usr/share +$UTILS_PATH/build-swiftpm.sh $TMP_DIR/$TOOLCHAIN_NAME + cd $TMP_DIR tar cfz $PACKAGE_ARTIFACT $TOOLCHAIN_NAME diff --git a/utils/webassembly/ci.sh b/utils/webassembly/ci.sh index f5336bf429533..359c085749e2d 100755 --- a/utils/webassembly/ci.sh +++ b/utils/webassembly/ci.sh @@ -19,7 +19,7 @@ export SCCACHE_CACHE_SIZE="50G" export SCCACHE_DIR="$SOURCE_PATH/build-cache" CACHE_FLAGS="--cmake-c-launcher $(which sccache) --cmake-cxx-launcher $(which sccache)" -FLAGS="--release --debug-swift-stdlib $CACHE_FLAGS --verbose" +FLAGS="--release $CACHE_FLAGS --verbose" $BUILD_SCRIPT $FLAGS diff --git a/utils/webassembly/install-nightly-toolchain.sh b/utils/webassembly/install-nightly-toolchain.sh new file mode 100755 index 0000000000000..cb0b814aad75b --- /dev/null +++ b/utils/webassembly/install-nightly-toolchain.sh @@ -0,0 +1,50 @@ +set -x + +SOURCE_DIR="$(cd "$(dirname $0)/../../.." && pwd)" +DESTINATION="${SOURCE_DIR}/swift-nightly-toolchain" + +mkdir -p $DESTINATION + +install_linux() { + WORKSPACE=$(mktemp -d) + + pushd ${WORKSPACE} + + BASE_URL=https://swift.org/builds/development/ubuntu1804 + export $(/usr/bin/curl ${BASE_URL}/latest-build.yml | grep 'download:' | sed 's/:[^:\/\/]/=/g') + + DOWNLOAD_DIR=$(echo $download | sed "s/-ubuntu18.04.tar.gz//g") + DOWNLOAD_URL=${BASE_URL}/${DOWNLOAD_DIR}/${download} + /usr/bin/curl ${BASE_URL}/${DOWNLOAD_DIR}/${download} > ${WORKSPACE}/latest_toolchain.tar.gz + + mkdir -p ${WORKSPACE}/latest_toolchain + tar xzf ${WORKSPACE}/latest_toolchain.tar.gz -C ${WORKSPACE}/latest_toolchain + mv ${WORKSPACE}/latest_toolchain/${DOWNLOAD_DIR}-ubuntu18.04/usr ${DESTINATION}/usr + + popd +} + +install_macos() { + WORKSPACE=$(mktemp -d) + + pushd ${WORKSPACE} + + BASE_URL=https://swift.org/builds/development/ + export $(/usr/bin/curl --silent ${BASE_URL}/xcode/latest-build.yml | grep 'download:' | sed 's/:[^:\/\/]/=/g') + + DOWNLOAD_DIR=$(echo $download | sed "s/-osx.pkg//g") + DOWNLOAD_URL=${BASE_URL}/xcode/${DOWNLOAD_DIR}/${download} + + /usr/bin/curl --silent ${DOWNLOAD_URL} > ${WORKSPACE}/latest_xcode_toolchain.pkg + pkgutil --expand ${WORKSPACE}/latest_xcode_toolchain.pkg ${WORKSPACE}/latest_xcode_toolchain + tar xf latest_xcode_toolchain/${DOWNLOAD_DIR}-osx-package.pkg/Payload + mv ${WORKSPACE}/usr ${DESTINATION}/usr + + popd +} + +if [[ "$(uname)" == "Linux" ]]; then + install_linux +else + install_macos +fi From e11342bfb96aac828c76e970f314cf551b1f51cb Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Thu, 5 Mar 2020 23:30:40 +0000 Subject: [PATCH 130/838] Install SwiftPM in toolchain --- utils/webassembly/build-toolchain.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/utils/webassembly/build-toolchain.sh b/utils/webassembly/build-toolchain.sh index b7d3aa7c1fc62..eb25dd85bc4c4 100755 --- a/utils/webassembly/build-toolchain.sh +++ b/utils/webassembly/build-toolchain.sh @@ -1,6 +1,6 @@ #/bin/bash -set -x +set -ex SOURCE_PATH="$(cd "$(dirname $0)/../../.." && pwd)" UTILS_PATH="$(cd "$(dirname $0)" && pwd)" @@ -31,7 +31,7 @@ INSTALLABLE_PACKAGE=$SOURCE_PATH/$ARCHIVE PACKAGE_ARTIFACT="$SOURCE_PATH/swift-wasm-DEVELOPMENT-SNAPSHOT-${OS_SUFFIX}.tar.gz" -BUNDLE_IDENTIFIER="${BUNDLE_PREFIX}.${YEAR}${MONTH}${DAY}" +BUNDLE_IDENTIFIER="swiftwasm.${YEAR}${MONTH}${DAY}" DISPLAY_NAME_SHORT="Swift for WebAssembly Development Snapshot" DISPLAY_NAME="${DISPLAY_NAME_SHORT} ${YEAR}-${MONTH}-${DAY}" From b076de43614a545963a004deb710a2c9cebe538d Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Thu, 5 Mar 2020 21:26:09 -0800 Subject: [PATCH 131/838] Copy stdlib of nightly-toolchain into toolchain --- utils/webassembly/build-swiftpm.sh | 5 ++--- utils/webassembly/build-toolchain.sh | 8 +++++++- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/utils/webassembly/build-swiftpm.sh b/utils/webassembly/build-swiftpm.sh index f258f4ea664fb..5ad8c44f9a496 100755 --- a/utils/webassembly/build-swiftpm.sh +++ b/utils/webassembly/build-swiftpm.sh @@ -42,12 +42,11 @@ install_swiftpm() { if [[ "$(uname)" == "Linux" ]]; then install_runtime_file "${bin_path}/libPackageDescription.so" - install_runtime_file "${bin_path}/PackageDescription.swiftmodule" - install_runtime_file "${bin_path}/PackageDescription.swiftdoc" else install_runtime_file "${bin_path}/libPackageDescription.dylib" - install_runtime_file "${bin_path}/PackageDescription.swiftinterface" fi + install_runtime_file "${bin_path}/PackageDescription.swiftmodule" + install_runtime_file "${bin_path}/PackageDescription.swiftdoc" } build_swiftpm diff --git a/utils/webassembly/build-toolchain.sh b/utils/webassembly/build-toolchain.sh index eb25dd85bc4c4..437e79fb424b1 100755 --- a/utils/webassembly/build-toolchain.sh +++ b/utils/webassembly/build-toolchain.sh @@ -49,7 +49,8 @@ $BUILD_SCRIPT \ "$@" -if [ ! -e $SOURCE_PATH/swift-nightly-toolchain ]; then +NIGHTLY_TOOLCHAIN=$SOURCE_PATH/swift-nightly-toolchain +if [ ! -e $NIGHTLY_TOOLCHAIN ]; then $UTILS_PATH/install-nightly-toolchain.sh fi @@ -63,7 +64,12 @@ cp -r $WASI_SDK_PATH/lib/clang usr/lib cp $WASI_SDK_PATH/bin/* usr/bin cp -r $WASI_SDK_PATH/share/wasi-sysroot usr/share +# Build SwiftPM and install it into toolchain $UTILS_PATH/build-swiftpm.sh $TMP_DIR/$TOOLCHAIN_NAME +# Copy nightly-toolchain's host environment stdlib into toolchain + +cp -r $NIGHTLY_TOOLCHAIN/usr/lib/swift/macosx $TMP_DIR/$TOOLCHAIN_NAME/usr/lib/swift + cd $TMP_DIR tar cfz $PACKAGE_ARTIFACT $TOOLCHAIN_NAME From 23c0b17db8df7f6b18aa341c9f2b153bb2898c1c Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Thu, 5 Mar 2020 21:26:23 -0800 Subject: [PATCH 132/838] Build forked swift-package-manager --- utils/update_checkout/update-checkout-config.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/utils/update_checkout/update-checkout-config.json b/utils/update_checkout/update-checkout-config.json index 382ef93c5887e..c688eaf852432 100644 --- a/utils/update_checkout/update-checkout-config.json +++ b/utils/update_checkout/update-checkout-config.json @@ -11,7 +11,7 @@ "swift-tools-support-core": { "remote": { "id": "apple/swift-tools-support-core" } }, "swiftpm": { - "remote": { "id": "apple/swift-package-manager" } }, + "remote": { "id": "swiftwasm/swift-package-manager" } }, "swift-syntax": { "remote": { "id": "apple/swift-syntax" } }, "swift-stress-tester": { @@ -59,7 +59,7 @@ "swift": "swiftwasm", "cmark": "master", "llbuild": "master", - "swiftpm": "master", + "swiftpm": "swiftwasm", "swift-syntax": "master", "swift-stress-tester": "master", "swift-corelibs-xctest": "master", From 387172e2c97ae4386421872a90bea3e348e2bdb1 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Fri, 6 Mar 2020 10:31:39 +0000 Subject: [PATCH 133/838] Find resource from relative to swiftc bin instead of given sdk --- .github/workflows/main.yml | 1 + lib/Driver/ToolChains.cpp | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 46653cc122812..5aa7c0ac078d4 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -85,6 +85,7 @@ jobs: echo 'print("Hello, world!")' > hello.swift $TOOLCHAIN_PATH/usr/bin/swiftc \ -target wasm32-unknown-wasi \ + -sdk $TOOLCHAIN_PATH/usr/share/wasi-sysroot \ hello.swift -o hello.wasm && \ echo "Successfully linked hello.wasm" diff --git a/lib/Driver/ToolChains.cpp b/lib/Driver/ToolChains.cpp index ad85f1fc1efc6..95557b375f6e6 100644 --- a/lib/Driver/ToolChains.cpp +++ b/lib/Driver/ToolChains.cpp @@ -1255,7 +1255,8 @@ void ToolChain::getResourceDirPath(SmallVectorImpl &resourceDirPath, if (const Arg *A = args.getLastArg(options::OPT_resource_dir)) { StringRef value = A->getValue(); resourceDirPath.append(value.begin(), value.end()); - } else if (!getTriple().isOSDarwin() && args.hasArg(options::OPT_sdk)) { + } else if (!getTriple().isOSDarwin() && !getTriple().isOSWASI() && args.hasArg(options::OPT_sdk)) { + // for WASI, sdk option points wasi-sysroot which doesn't have Swift toolchain StringRef value = args.getLastArg(options::OPT_sdk)->getValue(); resourceDirPath.append(value.begin(), value.end()); llvm::sys::path::append(resourceDirPath, "usr", "lib", From 1a429fba2ea54eb39e78f7e8e461e1dc4d62ea0e Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Fri, 6 Mar 2020 13:26:43 +0000 Subject: [PATCH 134/838] Support Linux toolchain build --- utils/webassembly/build-toolchain.sh | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/utils/webassembly/build-toolchain.sh b/utils/webassembly/build-toolchain.sh index 437e79fb424b1..6167419466fad 100755 --- a/utils/webassembly/build-toolchain.sh +++ b/utils/webassembly/build-toolchain.sh @@ -69,7 +69,11 @@ $UTILS_PATH/build-swiftpm.sh $TMP_DIR/$TOOLCHAIN_NAME # Copy nightly-toolchain's host environment stdlib into toolchain -cp -r $NIGHTLY_TOOLCHAIN/usr/lib/swift/macosx $TMP_DIR/$TOOLCHAIN_NAME/usr/lib/swift +if [[ "$(uname)" == "Linux" ]]; then + cp -r $NIGHTLY_TOOLCHAIN/usr/lib/swift/linux $TMP_DIR/$TOOLCHAIN_NAME/usr/lib/swift +else + cp -r $NIGHTLY_TOOLCHAIN/usr/lib/swift/macosx $TMP_DIR/$TOOLCHAIN_NAME/usr/lib/swift +fi cd $TMP_DIR tar cfz $PACKAGE_ARTIFACT $TOOLCHAIN_NAME From fa1eb9bbead2140712938fe923a7240611e0f9fb Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Sat, 7 Mar 2020 03:09:28 +0000 Subject: [PATCH 135/838] Bump cache version --- .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 5aa7c0ac078d4..b689fca09a72d 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -20,7 +20,7 @@ jobs: - uses: actions/cache@v1 with: path: ../build-cache - key: ${{ runner.os }}-sccache-v1 + key: ${{ runner.os }}-sccache-v2 - name: Build Linux installable archive run: ./utils/webassembly/ci.sh - name: Upload Linux installable archive @@ -47,7 +47,7 @@ jobs: - uses: actions/cache@v1 with: path: ../build-cache - key: ${{ runner.os }}-sccache-v1 + key: ${{ runner.os }}-sccache-v2 - name: Build macOS installable archive run: ./utils/webassembly/ci.sh - name: Upload macOS installable archive From 60341ee17842fdc19b0b6d31f3262176e6ddcf9e Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Sun, 8 Mar 2020 09:58:59 +0900 Subject: [PATCH 136/838] Update lib/Driver/ToolChains.cpp Co-Authored-By: Max Desiatov --- lib/Driver/ToolChains.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Driver/ToolChains.cpp b/lib/Driver/ToolChains.cpp index 95557b375f6e6..79ccb05f95284 100644 --- a/lib/Driver/ToolChains.cpp +++ b/lib/Driver/ToolChains.cpp @@ -1256,7 +1256,7 @@ void ToolChain::getResourceDirPath(SmallVectorImpl &resourceDirPath, StringRef value = A->getValue(); resourceDirPath.append(value.begin(), value.end()); } else if (!getTriple().isOSDarwin() && !getTriple().isOSWASI() && args.hasArg(options::OPT_sdk)) { - // for WASI, sdk option points wasi-sysroot which doesn't have Swift toolchain + // for WASI, sdk option points to wasi-sysroot which doesn't have Swift toolchain StringRef value = args.getLastArg(options::OPT_sdk)->getValue(); resourceDirPath.append(value.begin(), value.end()); llvm::sys::path::append(resourceDirPath, "usr", "lib", From 957a7828c86281a6518c76d0fb05f0f305edb11a Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Thu, 12 Mar 2020 06:17:29 +0000 Subject: [PATCH 137/838] [WASM] Pass -rpath flag to find dynamic library from relative to it's executable --- utils/webassembly/build-swiftpm.sh | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/utils/webassembly/build-swiftpm.sh b/utils/webassembly/build-swiftpm.sh index 5ad8c44f9a496..cb00f0e96def9 100755 --- a/utils/webassembly/build-swiftpm.sh +++ b/utils/webassembly/build-swiftpm.sh @@ -10,8 +10,15 @@ SWIFT_BUILD_FLAGS="-c release" SWIFT_BUILD=${NIGHTLY_TOOLCHAIN}/usr/bin/swift-build build_swiftpm() { + local build_flags=$SWIFT_BUILD_FLAGS + if [[ "$(uname)" == "Darwin" ]]; then + rpath_prefix='@executable_path/../' + else + rpath_prefix='$ORIGIN/../' + fi + build_flags="${build_flags} -Xlinker -rpath -Xlinker ${rpath_prefix}" cd ${SOURCE_PATH}/swiftpm - ${SWIFT_BUILD} ${SWIFT_BUILD_FLAGS} + ${SWIFT_BUILD} ${build_flags} } install_binary() { From fe80b6e4510807a0835d2ce9c0e4c137a97f7c01 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Fri, 13 Mar 2020 06:05:05 +0000 Subject: [PATCH 138/838] Use SWIFT_INCLUDE_TESTS flag to avoid building Block --- utils/webassembly/build-linux.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/utils/webassembly/build-linux.sh b/utils/webassembly/build-linux.sh index 9050c2bed100a..3f79a0413aa5a 100755 --- a/utils/webassembly/build-linux.sh +++ b/utils/webassembly/build-linux.sh @@ -12,6 +12,7 @@ $SWIFT_PATH/utils/build-script --wasm \ -DSWIFT_BUILD_SOURCEKIT=FALSE \ -DSWIFT_ENABLE_SOURCEKIT_TESTS=FALSE \ -DSWIFT_BUILD_SYNTAXPARSERLIB=FALSE \ + -DSWIFT_INCLUDE_TESTS=FALSE \ -DCMAKE_AR='$SOURCE_PATH/wasi-sdk/bin/llvm-ar' \ -DCMAKE_RANLIB='$SOURCE_PATH/wasi-sdk/bin/llvm-ranlib' \ " \ From 5e2f22e41160037d61f23c24a4796e117f8fb5c8 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Fri, 13 Mar 2020 06:22:17 +0000 Subject: [PATCH 139/838] Bump cache version --- .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 b689fca09a72d..552970b2a461c 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -20,7 +20,7 @@ jobs: - uses: actions/cache@v1 with: path: ../build-cache - key: ${{ runner.os }}-sccache-v2 + key: ${{ runner.os }}-sccache-v3 - name: Build Linux installable archive run: ./utils/webassembly/ci.sh - name: Upload Linux installable archive @@ -47,7 +47,7 @@ jobs: - uses: actions/cache@v1 with: path: ../build-cache - key: ${{ runner.os }}-sccache-v2 + key: ${{ runner.os }}-sccache-v3 - name: Build macOS installable archive run: ./utils/webassembly/ci.sh - name: Upload macOS installable archive From abfa2f1fc1ae44b8116d380501d53c51da7aba58 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Fri, 13 Mar 2020 08:57:40 +0000 Subject: [PATCH 140/838] Fix rpath to point to platform specific lib path --- utils/webassembly/build-swiftpm.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/utils/webassembly/build-swiftpm.sh b/utils/webassembly/build-swiftpm.sh index cb00f0e96def9..5a7899166c7d7 100755 --- a/utils/webassembly/build-swiftpm.sh +++ b/utils/webassembly/build-swiftpm.sh @@ -12,9 +12,9 @@ SWIFT_BUILD=${NIGHTLY_TOOLCHAIN}/usr/bin/swift-build build_swiftpm() { local build_flags=$SWIFT_BUILD_FLAGS if [[ "$(uname)" == "Darwin" ]]; then - rpath_prefix='@executable_path/../' + rpath_prefix='@executable_path/../lib/swift/macosx' else - rpath_prefix='$ORIGIN/../' + rpath_prefix='$ORIGIN/../lib/swift/linux' fi build_flags="${build_flags} -Xlinker -rpath -Xlinker ${rpath_prefix}" cd ${SOURCE_PATH}/swiftpm From 9256629cc70dbe8697b211f35fb32aaa83f8e7de Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Fri, 13 Mar 2020 02:56:52 -0700 Subject: [PATCH 141/838] Add Missing CMake flag to avoid building Block for wasm on macOS --- utils/webassembly/build-mac.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/utils/webassembly/build-mac.sh b/utils/webassembly/build-mac.sh index 43824be3bc073..bf11a7a1e8115 100755 --- a/utils/webassembly/build-mac.sh +++ b/utils/webassembly/build-mac.sh @@ -12,6 +12,7 @@ $SWIFT_PATH/utils/build-script --wasm \ -DSWIFT_BUILD_SOURCEKIT=FALSE \ -DSWIFT_ENABLE_SOURCEKIT_TESTS=FALSE \ -DSWIFT_BUILD_SYNTAXPARSERLIB=FALSE \ + -DSWIFT_INCLUDE_TESTS=FALSE \ -DCMAKE_AR='/usr/local/opt/llvm/bin/llvm-ar' \ -DCMAKE_RANLIB='/usr/local/opt/llvm/bin/llvm-ranlib' \ " \ From 2f6e0284854fe7f0e40130068bd293f18cfa1f3c Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Fri, 13 Mar 2020 15:10:34 +0000 Subject: [PATCH 142/838] Disable only building BlocksRuntimeStub when wasm --- test/CMakeLists.txt | 2 +- utils/webassembly/build-linux.sh | 1 - utils/webassembly/build-mac.sh | 1 - 3 files changed, 1 insertion(+), 3 deletions(-) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index d8065bed763aa..8e272ed0d0912 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -242,7 +242,7 @@ foreach(SDK ${SWIFT_SDKS}) set(test_dependencies) get_test_dependencies("${SDK}" test_dependencies) - if(SWIFT_BUILD_STDLIB AND SWIFT_INCLUDE_TESTS) + if(SWIFT_BUILD_STDLIB AND SWIFT_INCLUDE_TESTS AND NOT ${SDK} STREQUAL "WASI") # NOTE create a stub BlocksRuntime library that can be used for the # reflection tests file(WRITE ${test_bin_dir}/Inputs/BlocksRuntime.c diff --git a/utils/webassembly/build-linux.sh b/utils/webassembly/build-linux.sh index 3f79a0413aa5a..9050c2bed100a 100755 --- a/utils/webassembly/build-linux.sh +++ b/utils/webassembly/build-linux.sh @@ -12,7 +12,6 @@ $SWIFT_PATH/utils/build-script --wasm \ -DSWIFT_BUILD_SOURCEKIT=FALSE \ -DSWIFT_ENABLE_SOURCEKIT_TESTS=FALSE \ -DSWIFT_BUILD_SYNTAXPARSERLIB=FALSE \ - -DSWIFT_INCLUDE_TESTS=FALSE \ -DCMAKE_AR='$SOURCE_PATH/wasi-sdk/bin/llvm-ar' \ -DCMAKE_RANLIB='$SOURCE_PATH/wasi-sdk/bin/llvm-ranlib' \ " \ diff --git a/utils/webassembly/build-mac.sh b/utils/webassembly/build-mac.sh index bf11a7a1e8115..43824be3bc073 100755 --- a/utils/webassembly/build-mac.sh +++ b/utils/webassembly/build-mac.sh @@ -12,7 +12,6 @@ $SWIFT_PATH/utils/build-script --wasm \ -DSWIFT_BUILD_SOURCEKIT=FALSE \ -DSWIFT_ENABLE_SOURCEKIT_TESTS=FALSE \ -DSWIFT_BUILD_SYNTAXPARSERLIB=FALSE \ - -DSWIFT_INCLUDE_TESTS=FALSE \ -DCMAKE_AR='/usr/local/opt/llvm/bin/llvm-ar' \ -DCMAKE_RANLIB='/usr/local/opt/llvm/bin/llvm-ranlib' \ " \ From f75817a166fc12dbee95c8d0e26975aa6db3d8c6 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Sat, 14 Mar 2020 01:23:21 +0000 Subject: [PATCH 143/838] Debug showing disk free --- .github/workflows/main.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 552970b2a461c..0cd6e023f0194 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -22,7 +22,9 @@ jobs: path: ../build-cache key: ${{ runner.os }}-sccache-v3 - name: Build Linux installable archive - run: ./utils/webassembly/ci.sh + run: | + ./utils/webassembly/ci.sh + df -h - name: Upload Linux installable archive uses: actions/upload-artifact@v1 with: From a5ae3d74effed9410eb0b52b37c2d5bb977f8d4c Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Sat, 14 Mar 2020 01:23:35 +0000 Subject: [PATCH 144/838] Revert "Debug showing disk free" This reverts commit f75817a166fc12dbee95c8d0e26975aa6db3d8c6. --- .github/workflows/main.yml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 0cd6e023f0194..552970b2a461c 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -22,9 +22,7 @@ jobs: path: ../build-cache key: ${{ runner.os }}-sccache-v3 - name: Build Linux installable archive - run: | - ./utils/webassembly/ci.sh - df -h + run: ./utils/webassembly/ci.sh - name: Upload Linux installable archive uses: actions/upload-artifact@v1 with: From cd734d1a2bdf5566a7075b62fbd43942b045283e Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Sat, 14 Mar 2020 04:15:45 +0000 Subject: [PATCH 145/838] Cleanup build directory before uploading artifacts --- .github/workflows/main.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 552970b2a461c..0b5f322eed1be 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -22,7 +22,10 @@ jobs: path: ../build-cache key: ${{ runner.os }}-sccache-v3 - name: Build Linux installable archive - run: ./utils/webassembly/ci.sh + run: | + ./utils/webassembly/ci.sh + echo "Cleanup build directory to free disk space" + rm -rf ../build - name: Upload Linux installable archive uses: actions/upload-artifact@v1 with: From b8e40c044091057f463c2d877738031d5b18d668 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Mon, 9 Mar 2020 00:26:24 +0900 Subject: [PATCH 146/838] Revert thinToThick lowering from IRGen --- include/swift/Demangling/DemangleNodes.def | 1 - lib/Demangling/Context.cpp | 2 - lib/Demangling/Demangler.cpp | 5 +- lib/Demangling/NodePrinter.cpp | 9 ---- lib/Demangling/OldDemangler.cpp | 9 ---- lib/Demangling/OldRemangler.cpp | 5 -- lib/Demangling/Remangler.cpp | 5 -- lib/IRGen/GenFunc.cpp | 56 ---------------------- lib/IRGen/IRGenMangler.cpp | 15 ------ lib/IRGen/IRGenMangler.h | 1 - lib/IRGen/IRGenSIL.cpp | 52 -------------------- test/IRGen/indirect_func_call_wasm.sil | 33 ------------- 12 files changed, 1 insertion(+), 192 deletions(-) delete mode 100644 test/IRGen/indirect_func_call_wasm.sil diff --git a/include/swift/Demangling/DemangleNodes.def b/include/swift/Demangling/DemangleNodes.def index f13e126b4628e..4f11bec456290 100644 --- a/include/swift/Demangling/DemangleNodes.def +++ b/include/swift/Demangling/DemangleNodes.def @@ -201,7 +201,6 @@ CONTEXT_NODE(Structure) CONTEXT_NODE(Subscript) NODE(Suffix) NODE(ThinFunctionType) -NODE(ThinToThickForwarder) NODE(Tuple) NODE(TupleElement) NODE(TupleElementName) diff --git a/lib/Demangling/Context.cpp b/lib/Demangling/Context.cpp index 23f771ae22af4..7c2e986b3985f 100644 --- a/lib/Demangling/Context.cpp +++ b/lib/Demangling/Context.cpp @@ -88,7 +88,6 @@ bool Context::isThunkSymbol(llvm::StringRef MangledName) { MangledName = stripSuffix(MangledName); // First do a quick check if (MangledName.endswith("TA") || // partial application forwarder - MangledName.endswith("Tu")|| // thin-to-thick forwarder MangledName.endswith("Ta") || // ObjC partial application forwarder MangledName.endswith("To") || // swift-as-ObjC thunk MangledName.endswith("TO") || // ObjC-as-swift thunk @@ -108,7 +107,6 @@ bool Context::isThunkSymbol(llvm::StringRef MangledName) { case Node::Kind::NonObjCAttribute: case Node::Kind::PartialApplyObjCForwarder: case Node::Kind::PartialApplyForwarder: - case Node::Kind::ThinToThickForwarder: case Node::Kind::ReabstractionThunkHelper: case Node::Kind::ReabstractionThunk: case Node::Kind::ProtocolWitness: diff --git a/lib/Demangling/Demangler.cpp b/lib/Demangling/Demangler.cpp index e65fe4a4b924b..42dc1b29685a3 100644 --- a/lib/Demangling/Demangler.cpp +++ b/lib/Demangling/Demangler.cpp @@ -113,7 +113,6 @@ bool swift::Demangle::isFunctionAttr(Node::Kind kind) { case Node::Kind::DirectMethodReferenceAttribute: case Node::Kind::VTableAttribute: case Node::Kind::PartialApplyForwarder: - case Node::Kind::ThinToThickForwarder: case Node::Kind::PartialApplyObjCForwarder: case Node::Kind::OutlinedVariable: case Node::Kind::OutlinedBridgedMethod: @@ -550,8 +549,7 @@ NodePointer Demangler::demangleSymbol(StringRef MangledName, while (NodePointer FuncAttr = popNode(isFunctionAttr)) { Parent->addChild(FuncAttr, *this); if (FuncAttr->getKind() == Node::Kind::PartialApplyForwarder || - FuncAttr->getKind() == Node::Kind::PartialApplyObjCForwarder || - FuncAttr->getKind() == Node::Kind::ThinToThickForwarder) + FuncAttr->getKind() == Node::Kind::PartialApplyObjCForwarder) Parent = FuncAttr; } for (Node *Nd : NodeStack) { @@ -2180,7 +2178,6 @@ NodePointer Demangler::demangleThunkOrSpecialization() { case 'd': return createNode(Node::Kind::DirectMethodReferenceAttribute); case 'a': return createNode(Node::Kind::PartialApplyObjCForwarder); case 'A': return createNode(Node::Kind::PartialApplyForwarder); - case 'u': return createNode(Node::Kind::ThinToThickForwarder); case 'm': return createNode(Node::Kind::MergedFunction); case 'X': return createNode(Node::Kind::DynamicallyReplaceableFunctionVar); case 'x': return createNode(Node::Kind::DynamicallyReplaceableFunctionKey); diff --git a/lib/Demangling/NodePrinter.cpp b/lib/Demangling/NodePrinter.cpp index d7113bdddaa8d..f45a5bd2de6f2 100644 --- a/lib/Demangling/NodePrinter.cpp +++ b/lib/Demangling/NodePrinter.cpp @@ -467,7 +467,6 @@ class NodePrinter { case Node::Kind::Subscript: case Node::Kind::Suffix: case Node::Kind::ThinFunctionType: - case Node::Kind::ThinToThickForwarder: case Node::Kind::TupleElement: case Node::Kind::TypeMangling: case Node::Kind::TypeMetadata: @@ -1234,14 +1233,6 @@ NodePointer NodePrinter::print(NodePointer Node, bool asPrefixContext) { Printer << "@convention(thin) "; printFunctionType(nullptr, Node); return nullptr; - case Node::Kind::ThinToThickForwarder: - Printer << "thin-to-thick forwarder"; - - if (Node->hasChildren()) { - Printer << " for "; - printChildren(Node); - } - return nullptr; case Node::Kind::FunctionType: case Node::Kind::UncurriedFunctionType: printFunctionType(nullptr, Node); diff --git a/lib/Demangling/OldDemangler.cpp b/lib/Demangling/OldDemangler.cpp index a505241ccec8d..ebe2bec4e5823 100644 --- a/lib/Demangling/OldDemangler.cpp +++ b/lib/Demangling/OldDemangler.cpp @@ -364,15 +364,6 @@ class OldDemangler { DEMANGLE_CHILD_OR_RETURN(forwarder, Global); return forwarder; } - - // thin-to-thick thunks. - if (Mangled.nextIf("Pu")) { - Node::Kind kind = Node::Kind::ThinToThickForwarder; - auto forwarder = Factory.createNode(kind); - if (Mangled.nextIf("__T")) - DEMANGLE_CHILD_OR_RETURN(forwarder, Global); - return forwarder; - } // Top-level types, for various consumers. if (Mangled.nextIf('t')) { diff --git a/lib/Demangling/OldRemangler.cpp b/lib/Demangling/OldRemangler.cpp index f1f57c374ea6a..9f1fda4a99f90 100644 --- a/lib/Demangling/OldRemangler.cpp +++ b/lib/Demangling/OldRemangler.cpp @@ -574,11 +574,6 @@ void Remangler::mangleProtocolSelfConformanceDescriptor(Node *node) { mangleProtocol(node->begin()[0]); } -void Remangler::mangleThinToThickForwarder(Node *node) { - Buffer << "Pu__T"; - mangleSingleChildNode(node); // global -} - void Remangler::manglePartialApplyForwarder(Node *node) { Buffer << "PA__T"; mangleSingleChildNode(node); // global diff --git a/lib/Demangling/Remangler.cpp b/lib/Demangling/Remangler.cpp index 56f2ba18bb748..cb9b6e97e27e2 100644 --- a/lib/Demangling/Remangler.cpp +++ b/lib/Demangling/Remangler.cpp @@ -1738,11 +1738,6 @@ void Remangler::mangleOwningMutableAddressor(Node *node) { mangleAbstractStorage(node->getFirstChild(), "aO"); } -void Remangler::mangleThinToThickForwarder(Node *node) { - mangleChildNodesReversed(node); - Buffer << "Tu"; -} - void Remangler::manglePartialApplyForwarder(Node *node) { mangleChildNodesReversed(node); Buffer << "TA"; diff --git a/lib/IRGen/GenFunc.cpp b/lib/IRGen/GenFunc.cpp index 407fa80015ffe..5888b9b65a874 100644 --- a/lib/IRGen/GenFunc.cpp +++ b/lib/IRGen/GenFunc.cpp @@ -700,62 +700,6 @@ static unsigned findSinglePartiallyAppliedParameterIndexIgnoringEmptyTypes( return firstNonEmpty; } - -llvm::Function *irgen::getThinToThickForwarder(IRGenModule &IGM, - const Optional &staticFnPtr, - const CanSILFunctionType origType) { - auto origSig = IGM.getSignature(origType); - llvm::FunctionType *origFnTy = origSig.getType(); - auto origTy = origSig.getType()->getPointerTo(); - - llvm::SmallVector thunkParams; - - for (unsigned i = 0; i < origFnTy->getNumParams(); ++i) - thunkParams.push_back(origFnTy->getParamType(i)); - - thunkParams.push_back(IGM.RefCountedPtrTy); - - auto thunkType = llvm::FunctionType::get(origFnTy->getReturnType(), - thunkParams, - /*vararg*/ false); - - StringRef FnName; - if (staticFnPtr) - FnName = staticFnPtr->getPointer()->getName(); - - IRGenMangler Mangler; - std::string thunkName = Mangler.mangleThinToThickForwarder(FnName); - - - // FIXME: Maybe cache the thunk by function and closure types?. - llvm::Function *fwd = - llvm::Function::Create(thunkType, llvm::Function::InternalLinkage, - llvm::StringRef(thunkName), &IGM.Module); - - fwd->setAttributes(origSig.getAttributes()); - fwd->addAttribute(llvm::AttributeList::FirstArgIndex + origFnTy->getNumParams(), llvm::Attribute::SwiftSelf); - IRGenFunction IGF(IGM, fwd); - if (IGM.DebugInfo) - IGM.DebugInfo->emitArtificialFunction(IGF, fwd); - auto args = IGF.collectParameters(); - auto rawFnPtr = args.takeLast(); - - // It comes out of the context as an i8*. Cast to the function type. - rawFnPtr = IGF.Builder.CreateBitCast(rawFnPtr, origTy); - - auto fnPtr = FunctionPointer(rawFnPtr, origSig); - - auto result = IGF.Builder.CreateCall(fnPtr, args.claimAll()); - - // Return the result, if we have one. - if (result->getType()->isVoidTy()) - IGF.Builder.CreateRetVoid(); - else - IGF.Builder.CreateRet(result); - return fwd; -} - - /// Emit the forwarding stub function for a partial application. /// /// If 'layout' is null, there is a single captured value of diff --git a/lib/IRGen/IRGenMangler.cpp b/lib/IRGen/IRGenMangler.cpp index b231b588726a1..7dbac2eaf2f82 100644 --- a/lib/IRGen/IRGenMangler.cpp +++ b/lib/IRGen/IRGenMangler.cpp @@ -78,21 +78,6 @@ std::string IRGenMangler::manglePartialApplyForwarder(StringRef FuncName) { return finalize(); } -std::string IRGenMangler::mangleThinToThickForwarder(StringRef FuncName) { - if (FuncName.empty()) { - beginMangling(); - } else { - if (FuncName.startswith(MANGLING_PREFIX_STR)) { - Buffer << FuncName; - } else { - beginMangling(); - appendIdentifier(FuncName); - } - } - appendOperator("Tu"); - return finalize(); -} - SymbolicMangling IRGenMangler::withSymbolicReferences(IRGenModule &IGM, llvm::function_ref body) { diff --git a/lib/IRGen/IRGenMangler.h b/lib/IRGen/IRGenMangler.h index f972d4c7eedc4..2f79d355aa437 100644 --- a/lib/IRGen/IRGenMangler.h +++ b/lib/IRGen/IRGenMangler.h @@ -512,7 +512,6 @@ class IRGenMangler : public Mangle::ASTMangler { } std::string manglePartialApplyForwarder(StringRef FuncName); - std::string mangleThinToThickForwarder(StringRef FuncName); std::string mangleTypeForForeignMetadataUniquing(Type type) { return mangleTypeWithoutPrefix(type); diff --git a/lib/IRGen/IRGenSIL.cpp b/lib/IRGen/IRGenSIL.cpp index 77d7d3aee18dc..38577f4c8c4c7 100644 --- a/lib/IRGen/IRGenSIL.cpp +++ b/lib/IRGen/IRGenSIL.cpp @@ -4732,60 +4732,8 @@ void IRGenSILFunction::visit##KIND##Inst(swift::KIND##Inst *i) { \ #include "swift/AST/ReferenceStorage.def" #undef NOOP_CONVERSION - -static FunctionPointer -getLoweredFunctionPointer(IRGenSILFunction &IGF, SILValue v) { - LoweredValue &lv = IGF.getLoweredValue(v); - auto fnType = v->getType().castTo(); - - switch (lv.kind) { - case LoweredValue::Kind::ContainedAddress: - case LoweredValue::Kind::StackAddress: - case LoweredValue::Kind::DynamicallyEnforcedAddress: - case LoweredValue::Kind::OwnedAddress: - case LoweredValue::Kind::EmptyExplosion: - case LoweredValue::Kind::CoroutineState: - case LoweredValue::Kind::ObjCMethod: - llvm_unreachable("not a valid function"); - - case LoweredValue::Kind::FunctionPointer: { - return lv.getFunctionPointer(); - } - case LoweredValue::Kind::SingletonExplosion: { - llvm::Value *fnPtr = lv.getKnownSingletonExplosion(); - return FunctionPointer::forExplosionValue(IGF, fnPtr, fnType); - } - case LoweredValue::Kind::ExplosionVector: { - Explosion ex = lv.getExplosion(IGF, v->getType()); - llvm::Value *fnPtr = ex.claimNext(); - auto fn = FunctionPointer::forExplosionValue(IGF, fnPtr, fnType); - return fn; - } - } - llvm_unreachable("bad kind"); -} - void IRGenSILFunction::visitThinToThickFunctionInst( swift::ThinToThickFunctionInst *i) { - auto fn = getLoweredFunctionPointer(*this, i->getCallee()); - auto fnTy = i->getCallee()->getType().castTo(); - if (IGM.TargetInfo.OutputObjectFormat == llvm::Triple::Wasm && !fnTy->hasErrorResult()) { - Optional staticFn; - if (fn.isConstant()) staticFn = fn; - auto thunkFn = getThinToThickForwarder(IGM, staticFn, fnTy); - Explosion from = getLoweredExplosion(i->getOperand()); - Explosion to; - auto fnPtr = Builder.CreateBitCast(thunkFn, IGM.Int8PtrTy); - to.add(fnPtr); - llvm::Value *ctx = from.claimNext(); - if (fnTy->isNoEscape()) - ctx = Builder.CreateBitCast(ctx, IGM.OpaquePtrTy); - else - ctx = Builder.CreateBitCast(ctx, IGM.RefCountedPtrTy); - to.add(ctx); - setLoweredExplosion(i, to); - return; - } // Take the incoming function pointer and add a null context pointer to it. Explosion from = getLoweredExplosion(i->getOperand()); Explosion to; diff --git a/test/IRGen/indirect_func_call_wasm.sil b/test/IRGen/indirect_func_call_wasm.sil deleted file mode 100644 index f64cae7fcaa12..0000000000000 --- a/test/IRGen/indirect_func_call_wasm.sil +++ /dev/null @@ -1,33 +0,0 @@ -// RUN: %target-swift-frontend %s -emit-ir | %FileCheck %s - -// REQUIRES: CPU=wasm32 - -// CHECK-LABEL: define swiftcc void @closureToConvert() -// CHECK: entry: -// CHECK: ret void -// CHECK: } -sil @closureToConvert : $@convention(thin) () -> () { - %99 = tuple () - return %99 : $() -} -// CHECK-LABEL: define swiftcc void @testConvertFunc() -// CHECK: entry: -// CHECK: call swiftcc void @"$s{{.*}}Tu"(%swift.refcounted* swiftself bitcast (void ()* @closureToConvert to %swift.refcounted*)) -// CHECK: ret void -// CHECK-LABEL: } - -sil @testConvertFunc : $@convention(thin) () -> () { -bb0: - %f = function_ref @closureToConvert : $@convention(thin) () -> () - %cf = convert_function %f : $@convention(thin) () -> () to $@noescape @convention(thin) () -> () - %thick = thin_to_thick_function %cf : $@noescape @convention(thin) () -> () to $@noescape @callee_owned () -> () - %apply = apply %thick() : $@noescape @callee_owned () -> () - %99 = tuple () - return %99 : $() -} - -// CHECK-LABEL: define internal void @"$s{{.*}}Tu"(%swift.refcounted* swiftself %0) -// CHECK: entry: -// CHECK: %1 = bitcast %swift.refcounted* %0 to void ()* -// CHECK: call swiftcc void %1() -// CHECK: ret void From a4814df193095a76ed07dc120b424e88c244b6c8 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Thu, 12 Mar 2020 01:18:56 +0000 Subject: [PATCH 147/838] Add swiftcc attribute for runtime functions called from Swift --- include/swift/Demangling/Demangle.h | 1 + include/swift/Runtime/HeapObject.h | 3 +++ lib/IRGen/GenBuiltin.cpp | 15 +++++++++++---- lib/IRGen/GenKeyPath.cpp | 7 ++++++- stdlib/public/SwiftShims/HeapObject.h | 8 ++++++++ stdlib/public/runtime/Demangle.cpp | 1 + stdlib/public/runtime/ErrorObject.h | 12 ++++++++++++ stdlib/public/runtime/ErrorObjectCommon.cpp | 10 +++++++++- stdlib/public/runtime/HeapObject.cpp | 5 +++++ stdlib/public/runtime/Numeric.cpp | 2 ++ .../runtime/RuntimeInvocationsTracking.cpp | 18 +++++++++--------- .../runtime/RuntimeInvocationsTracking.h | 18 +++++++++--------- 12 files changed, 76 insertions(+), 24 deletions(-) diff --git a/include/swift/Demangling/Demangle.h b/include/swift/Demangling/Demangle.h index ce3a4b5d439c3..30c31f55c2689 100644 --- a/include/swift/Demangling/Demangle.h +++ b/include/swift/Demangling/Demangle.h @@ -637,6 +637,7 @@ llvm::StringRef makeSymbolicMangledNameStringRef(const char *base); //// define what these will be. /// \returns the demangled name. Returns nullptr if the input String is not a /// Swift mangled name. +SWIFT_CC(swift) SWIFT_RUNTIME_EXPORT char *swift_demangle(const char *mangledName, size_t mangledNameLength, diff --git a/include/swift/Runtime/HeapObject.h b/include/swift/Runtime/HeapObject.h index 72998fbbf6193..53f4448d7c666 100644 --- a/include/swift/Runtime/HeapObject.h +++ b/include/swift/Runtime/HeapObject.h @@ -205,10 +205,13 @@ void swift_nonatomic_release_n(HeapObject *object, uint32_t n); // Refcounting observation hooks for memory tools. Don't use these. SWIFT_RUNTIME_EXPORT +SWIFT_CC(swift) size_t swift_retainCount(HeapObject *object); SWIFT_RUNTIME_EXPORT +SWIFT_CC(swift) size_t swift_unownedRetainCount(HeapObject *object); SWIFT_RUNTIME_EXPORT +SWIFT_CC(swift) size_t swift_weakRetainCount(HeapObject *object); /// Is this pointer a non-null unique reference to an object diff --git a/lib/IRGen/GenBuiltin.cpp b/lib/IRGen/GenBuiltin.cpp index c0f771259286b..d1f4a92a2dab2 100644 --- a/lib/IRGen/GenBuiltin.cpp +++ b/lib/IRGen/GenBuiltin.cpp @@ -398,10 +398,17 @@ if (Builtin.ID == BuiltinValueKind::id) { \ call->addAttribute(llvm::AttributeList::FirstArgIndex + 1, llvm::Attribute::ReadOnly); - auto attrs = call->getAttributes(); - IGF.IGM.addSwiftSelfAttributes(attrs, 0); - IGF.IGM.addSwiftErrorAttributes(attrs, 1); - call->setAttributes(attrs); + // Remove swiftself and swifterror attribute to match signature generated from + // RuntimeFunctions.def. These two parameters are passed using swifterror and swiftself, + // but the definition of swift_willThrow generated from the def file doesn't has those + // attributes due to the def file limitation. In WebAssembly context, these attributes are + // lowered as usual parameters, so this doesn't have any side effects. + if (IGF.IGM.TargetInfo.OutputObjectFormat != llvm::Triple::Wasm) { + auto attrs = call->getAttributes(); + IGF.IGM.addSwiftSelfAttributes(attrs, 0); + IGF.IGM.addSwiftErrorAttributes(attrs, 1); + call->setAttributes(attrs); + } IGF.Builder.CreateStore(llvm::ConstantPointerNull::get(IGF.IGM.ErrorPtrTy), errorBuffer); diff --git a/lib/IRGen/GenKeyPath.cpp b/lib/IRGen/GenKeyPath.cpp index 2f40aae09c7d2..da6db5cb0739e 100644 --- a/lib/IRGen/GenKeyPath.cpp +++ b/lib/IRGen/GenKeyPath.cpp @@ -278,7 +278,8 @@ getLayoutFunctionForComputedComponent(IRGenModule &IGM, auto layoutFn = llvm::Function::Create(fnTy, llvm::GlobalValue::PrivateLinkage, "keypath_get_arg_layout", IGM.getModule()); - + layoutFn->setCallingConv(IGM.SwiftCC); + { IRGenFunction IGF(IGM, layoutFn); if (IGM.DebugInfo) @@ -378,6 +379,7 @@ getWitnessTableForComputedComponent(IRGenModule &IGM, /*vararg*/ false); auto destroyFn = llvm::Function::Create(destroyType, llvm::GlobalValue::PrivateLinkage, "keypath_destroy", IGM.getModule()); + destroyFn->setCallingConv(IGM.SwiftCC); destroy = destroyFn; IRGenFunction IGF(IGM, destroyFn); @@ -426,6 +428,7 @@ getWitnessTableForComputedComponent(IRGenModule &IGM, /*vararg*/ false); auto copyFn = llvm::Function::Create(copyType, llvm::GlobalValue::PrivateLinkage, "keypath_copy", IGM.getModule()); + copyFn->setCallingConv(IGM.SwiftCC); copy = copyFn; IRGenFunction IGF(IGM, copyFn); @@ -539,6 +542,8 @@ getInitializerForComputedComponent(IRGenModule &IGM, auto initFn = llvm::Function::Create(fnTy, llvm::GlobalValue::PrivateLinkage, "keypath_arg_init", IGM.getModule()); + initFn->setCallingConv(IGM.SwiftCC); + { IRGenFunction IGF(IGM, initFn); diff --git a/stdlib/public/SwiftShims/HeapObject.h b/stdlib/public/SwiftShims/HeapObject.h index bc7a8637b5f19..e69d1072ce6e5 100644 --- a/stdlib/public/SwiftShims/HeapObject.h +++ b/stdlib/public/SwiftShims/HeapObject.h @@ -74,17 +74,25 @@ struct HeapObject { #ifdef __cplusplus extern "C" { #endif +#if __has_attribute(swiftcall) +#define SWIFT_CC_swift __attribute__((swiftcall)) +#else +#define SWIFT_CC_swift +#endif SWIFT_RUNTIME_STDLIB_API void _swift_instantiateInertHeapObject(void *address, const HeapMetadata *metadata); +SWIFT_CC_swift SWIFT_RUNTIME_STDLIB_API __swift_size_t swift_retainCount(HeapObject *obj); +SWIFT_CC_swift SWIFT_RUNTIME_STDLIB_API __swift_size_t swift_unownedRetainCount(HeapObject *obj); +SWIFT_CC_swift SWIFT_RUNTIME_STDLIB_API __swift_size_t swift_weakRetainCount(HeapObject *obj); diff --git a/stdlib/public/runtime/Demangle.cpp b/stdlib/public/runtime/Demangle.cpp index f082e47e015aa..ea33eb6839fcb 100644 --- a/stdlib/public/runtime/Demangle.cpp +++ b/stdlib/public/runtime/Demangle.cpp @@ -638,6 +638,7 @@ swift::_swift_buildDemanglingForMetadata(const Metadata *type, // NB: This function is not used directly in the Swift codebase, but is // exported for Xcode support and is used by the sanitizers. Please coordinate // before changing. +SWIFT_CC(swift) char *swift_demangle(const char *mangledName, size_t mangledNameLength, char *outputBuffer, diff --git a/stdlib/public/runtime/ErrorObject.h b/stdlib/public/runtime/ErrorObject.h index 5e0b702cad5d2..b39b1d3e8c0df 100644 --- a/stdlib/public/runtime/ErrorObject.h +++ b/stdlib/public/runtime/ErrorObject.h @@ -206,9 +206,21 @@ SWIFT_RUNTIME_STDLIB_API void swift_errorRelease(SwiftError *object); /// Breakpoint hook for debuggers. +#ifdef __wasm__ +// Notes: +// Remove swiftself and swifterror attribute to match signature generated from +// RuntimeFunctions.def. These two parameters are passed using swifterror and swiftself, +// but the definition of swift_willThrow generated from the def file doesn't has those +// attributes due to the def file limitation. In WebAssembly context, these attributes are +// lowered as usual parameters, so this doesn't have any side effects. +SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_API +void swift_willThrow(void *unused, + SwiftError **object); +#else SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_API void swift_willThrow(SWIFT_CONTEXT void *unused, SWIFT_ERROR_RESULT SwiftError **object); +#endif /// Halt in response to an error. SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_API LLVM_ATTRIBUTE_NORETURN diff --git a/stdlib/public/runtime/ErrorObjectCommon.cpp b/stdlib/public/runtime/ErrorObjectCommon.cpp index e000603431aba..7f445db18fe40 100644 --- a/stdlib/public/runtime/ErrorObjectCommon.cpp +++ b/stdlib/public/runtime/ErrorObjectCommon.cpp @@ -27,9 +27,17 @@ using namespace swift; void (*swift::_swift_willThrow)(SwiftError *error); /// Breakpoint hook for debuggers, and calls _swift_willThrow if set. +#ifdef __wasm__ +// Notes: +// The reason of this ifdef is described in header file. +SWIFT_CC(swift) void +swift::swift_willThrow(void *unused, SwiftError **error) +#else SWIFT_CC(swift) void swift::swift_willThrow(SWIFT_CONTEXT void *unused, - SWIFT_ERROR_RESULT SwiftError **error) { + SWIFT_ERROR_RESULT SwiftError **error) +#endif +{ // Cheap check to bail out early, since we expect there to be no callbacks // the vast majority of the time. if (SWIFT_LIKELY(!_swift_willThrow)) diff --git a/stdlib/public/runtime/HeapObject.cpp b/stdlib/public/runtime/HeapObject.cpp index 2517f0349c1fd..5e6b522fb0c27 100644 --- a/stdlib/public/runtime/HeapObject.cpp +++ b/stdlib/public/runtime/HeapObject.cpp @@ -253,6 +253,7 @@ class BoxCacheEntry { static SimpleGlobalCache Boxes; +SWIFT_CC(swift) BoxPair swift::swift_makeBoxUnique(OpaqueValue *buffer, const Metadata *type, size_t alignMask) { auto *inlineBuffer = reinterpret_cast(buffer); @@ -278,6 +279,7 @@ BoxPair swift::swift_makeBoxUnique(OpaqueValue *buffer, const Metadata *type, } } +SWIFT_CC(swift) BoxPair swift::swift_allocBox(const Metadata *type) { // Get the heap metadata for the box. auto metadata = &Boxes.getOrInsert(type).first->Data; @@ -420,16 +422,19 @@ void swift::swift_nonatomic_release_n(HeapObject *object, uint32_t n) { object->refCounts.decrementAndMaybeDeinitNonAtomic(n); } +SWIFT_CC(swift) size_t swift::swift_retainCount(HeapObject *object) { if (isValidPointerForNativeRetain(object)) return object->refCounts.getCount(); return 0; } +SWIFT_CC(swift) size_t swift::swift_unownedRetainCount(HeapObject *object) { return object->refCounts.getUnownedCount(); } +SWIFT_CC(swift) size_t swift::swift_weakRetainCount(HeapObject *object) { return object->refCounts.getWeakCount(); } diff --git a/stdlib/public/runtime/Numeric.cpp b/stdlib/public/runtime/Numeric.cpp index ac3933e6b62c6..40421d0336ef4 100644 --- a/stdlib/public/runtime/Numeric.cpp +++ b/stdlib/public/runtime/Numeric.cpp @@ -50,10 +50,12 @@ static T convert(IntegerLiteral value) { return result; } +SWIFT_CC(swift) float swift::swift_intToFloat32(IntegerLiteral value) { return convert(value); } +SWIFT_CC(swift) double swift::swift_intToFloat64(IntegerLiteral value) { return convert(value); } diff --git a/stdlib/public/runtime/RuntimeInvocationsTracking.cpp b/stdlib/public/runtime/RuntimeInvocationsTracking.cpp index 959577589c4ba..7cceb8d6dc9b5 100644 --- a/stdlib/public/runtime/RuntimeInvocationsTracking.cpp +++ b/stdlib/public/runtime/RuntimeInvocationsTracking.cpp @@ -128,7 +128,7 @@ static uint16_t RuntimeFunctionCountersOffsets[] = { /// Public APIs /// Get the runtime object state associated with an object. -void _swift_getObjectRuntimeFunctionCounters( +SWIFT_CC(swift) void _swift_getObjectRuntimeFunctionCounters( HeapObject *object, RuntimeFunctionCountersState *result) { auto &theSentinel = RuntimeObjectStateCache.get(); StaticScopedReadLock lock(theSentinel.Lock); @@ -137,7 +137,7 @@ void _swift_getObjectRuntimeFunctionCounters( /// Set the runtime object state associated with an object from a provided /// state. -void _swift_setObjectRuntimeFunctionCounters( +SWIFT_CC(swift) void _swift_setObjectRuntimeFunctionCounters( HeapObject *object, RuntimeFunctionCountersState *state) { auto &theSentinel = RuntimeObjectStateCache.get(); StaticScopedWriteLock lock(theSentinel.Lock); @@ -146,14 +146,14 @@ void _swift_setObjectRuntimeFunctionCounters( /// Get the global runtime state containing the total numbers of invocations for /// each runtime function of interest. -void _swift_getGlobalRuntimeFunctionCounters( +SWIFT_CC(swift) void _swift_getGlobalRuntimeFunctionCounters( RuntimeFunctionCountersState *result) { StaticScopedReadLock lock(RuntimeGlobalFunctionCountersState.Lock); *result = RuntimeGlobalFunctionCountersState.State; } /// Set the global runtime state of function pointers from a provided state. -void _swift_setGlobalRuntimeFunctionCounters( +SWIFT_CC(swift) void _swift_setGlobalRuntimeFunctionCounters( RuntimeFunctionCountersState *state) { StaticScopedWriteLock lock(RuntimeGlobalFunctionCountersState.Lock); RuntimeGlobalFunctionCountersState.State = *state; @@ -162,19 +162,19 @@ void _swift_setGlobalRuntimeFunctionCounters( /// Return the names of the runtime functions being tracked. /// Their order is the same as the order of the counters in the /// RuntimeObjectState structure. All these strings are null terminated. -const char **_swift_getRuntimeFunctionNames() { +SWIFT_CC(swift) const char **_swift_getRuntimeFunctionNames() { return RuntimeFunctionNames; } /// Return the offsets of the runtime function counters being tracked. /// Their order is the same as the order of the counters in the /// RuntimeObjectState structure. -const uint16_t *_swift_getRuntimeFunctionCountersOffsets() { +SWIFT_CC(swift) const uint16_t *_swift_getRuntimeFunctionCountersOffsets() { return RuntimeFunctionCountersOffsets; } /// Return the number of runtime functions being tracked. -uint64_t _swift_getNumRuntimeFunctionCounters() { +SWIFT_CC(swift) uint64_t _swift_getNumRuntimeFunctionCounters() { return ID_LastRuntimeFunctionName; } @@ -202,7 +202,7 @@ void _swift_dumpObjectsRuntimeFunctionPointers() { /// Set mode for global runtime function counters. /// Return the old value of this flag. -int _swift_setGlobalRuntimeFunctionCountersMode(int mode) { +SWIFT_CC(swift) int _swift_setGlobalRuntimeFunctionCountersMode(int mode) { int oldMode = UpdateGlobalRuntimeFunctionCounters; UpdateGlobalRuntimeFunctionCounters = mode ? 1 : 0; return oldMode; @@ -210,7 +210,7 @@ int _swift_setGlobalRuntimeFunctionCountersMode(int mode) { /// Set mode for per object runtime function counters. /// Return the old value of this flag. -int _swift_setPerObjectRuntimeFunctionCountersMode(int mode) { +SWIFT_CC(swift) int _swift_setPerObjectRuntimeFunctionCountersMode(int mode) { int oldMode = UpdatePerObjectRuntimeFunctionCounters; UpdatePerObjectRuntimeFunctionCounters = mode ? 1 : 0; return oldMode; diff --git a/stdlib/public/runtime/RuntimeInvocationsTracking.h b/stdlib/public/runtime/RuntimeInvocationsTracking.h index 74d609176563e..3005df2b79c7c 100644 --- a/stdlib/public/runtime/RuntimeInvocationsTracking.h +++ b/stdlib/public/runtime/RuntimeInvocationsTracking.h @@ -61,47 +61,47 @@ using RuntimeFunctionCountersUpdateHandler = /// Get the runtime object state associated with an object and store it /// into the result. -SWIFT_RUNTIME_EXPORT void +SWIFT_CC(swift) SWIFT_RUNTIME_EXPORT void _swift_getObjectRuntimeFunctionCounters(HeapObject *object, RuntimeFunctionCountersState *result); /// Get the global runtime state containing the total numbers of invocations for /// each runtime function of interest and store it into the result. -SWIFT_RUNTIME_EXPORT void _swift_getGlobalRuntimeFunctionCounters( +SWIFT_CC(swift) SWIFT_RUNTIME_EXPORT void _swift_getGlobalRuntimeFunctionCounters( swift::RuntimeFunctionCountersState *result); /// Return the names of the runtime functions being tracked. /// Their order is the same as the order of the counters in the /// RuntimeObjectState structure. -SWIFT_RUNTIME_EXPORT const char **_swift_getRuntimeFunctionNames(); +SWIFT_CC(swift) SWIFT_RUNTIME_EXPORT const char **_swift_getRuntimeFunctionNames(); /// Return the offsets of the runtime function counters being tracked. /// Their order is the same as the order of the counters in the /// RuntimeFunctionCountersState structure. -SWIFT_RUNTIME_EXPORT const uint16_t *_swift_getRuntimeFunctionCountersOffsets(); +SWIFT_CC(swift) SWIFT_RUNTIME_EXPORT const uint16_t *_swift_getRuntimeFunctionCountersOffsets(); /// Return the number of runtime functions being tracked. -SWIFT_RUNTIME_EXPORT uint64_t _swift_getNumRuntimeFunctionCounters(); +SWIFT_CC(swift) SWIFT_RUNTIME_EXPORT uint64_t _swift_getNumRuntimeFunctionCounters(); /// Dump all per-object runtime function pointers. SWIFT_RUNTIME_EXPORT void _swift_dumpObjectsRuntimeFunctionPointers(); /// Set mode for global runtime function counters. /// Return the old value of this flag. -SWIFT_RUNTIME_EXPORT int +SWIFT_CC(swift) SWIFT_RUNTIME_EXPORT int _swift_setPerObjectRuntimeFunctionCountersMode(int mode); /// Set mode for per object runtime function counters. /// Return the old value of this flag. -SWIFT_RUNTIME_EXPORT int _swift_setGlobalRuntimeFunctionCountersMode(int mode); +SWIFT_CC(swift) SWIFT_RUNTIME_EXPORT int _swift_setGlobalRuntimeFunctionCountersMode(int mode); /// Set the global runtime state of function pointers from a provided state. -SWIFT_RUNTIME_EXPORT void _swift_setGlobalRuntimeFunctionCounters( +SWIFT_CC(swift) SWIFT_RUNTIME_EXPORT void _swift_setGlobalRuntimeFunctionCounters( swift::RuntimeFunctionCountersState *state); /// Set the runtime object state associated with an object from a provided /// state. -SWIFT_RUNTIME_EXPORT void +SWIFT_CC(swift) SWIFT_RUNTIME_EXPORT void _swift_setObjectRuntimeFunctionCounters(HeapObject *object, RuntimeFunctionCountersState *state); From 942e0f1f1f49c26e4700a704e89b8f98a99f7e00 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Sat, 14 Mar 2020 17:23:00 -0700 Subject: [PATCH 148/838] Fix typo --- lib/IRGen/GenBuiltin.cpp | 2 +- stdlib/public/runtime/ErrorObject.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/IRGen/GenBuiltin.cpp b/lib/IRGen/GenBuiltin.cpp index d1f4a92a2dab2..6223decbd9762 100644 --- a/lib/IRGen/GenBuiltin.cpp +++ b/lib/IRGen/GenBuiltin.cpp @@ -400,7 +400,7 @@ if (Builtin.ID == BuiltinValueKind::id) { \ // Remove swiftself and swifterror attribute to match signature generated from // RuntimeFunctions.def. These two parameters are passed using swifterror and swiftself, - // but the definition of swift_willThrow generated from the def file doesn't has those + // but the definition of swift_willThrow generated from the def file doesn't have those // attributes due to the def file limitation. In WebAssembly context, these attributes are // lowered as usual parameters, so this doesn't have any side effects. if (IGF.IGM.TargetInfo.OutputObjectFormat != llvm::Triple::Wasm) { diff --git a/stdlib/public/runtime/ErrorObject.h b/stdlib/public/runtime/ErrorObject.h index b39b1d3e8c0df..b67a80f8d1ed3 100644 --- a/stdlib/public/runtime/ErrorObject.h +++ b/stdlib/public/runtime/ErrorObject.h @@ -210,7 +210,7 @@ void swift_errorRelease(SwiftError *object); // Notes: // Remove swiftself and swifterror attribute to match signature generated from // RuntimeFunctions.def. These two parameters are passed using swifterror and swiftself, -// but the definition of swift_willThrow generated from the def file doesn't has those +// but the definition of swift_willThrow generated from the def file doesn't have those // attributes due to the def file limitation. In WebAssembly context, these attributes are // lowered as usual parameters, so this doesn't have any side effects. SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_API From 82bfeb45f7709e4595fc40555bb9bda6b12bed13 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Mon, 16 Mar 2020 19:17:38 -0700 Subject: [PATCH 149/838] Update ICU version --- utils/webassembly/linux/install-dependencies.sh | 2 +- utils/webassembly/macos/install-dependencies.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/utils/webassembly/linux/install-dependencies.sh b/utils/webassembly/linux/install-dependencies.sh index 784eef14135ea..6cfc4cf87eb02 100755 --- a/utils/webassembly/linux/install-dependencies.sh +++ b/utils/webassembly/linux/install-dependencies.sh @@ -44,7 +44,7 @@ mv $WASI_SDK_FULL_NAME ./wasi-sdk # 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/0.3.0/icu4c-wasi.tar.xz" +wget -O icu.tar.xz "https://github.com/swiftwasm/icu4c-wasi/releases/download/0.4.0/icu4c-wasi.tar.xz" tar xf icu.tar.xz # Install sccache diff --git a/utils/webassembly/macos/install-dependencies.sh b/utils/webassembly/macos/install-dependencies.sh index 81fb5e5993f02..d46098ff791fd 100755 --- a/utils/webassembly/macos/install-dependencies.sh +++ b/utils/webassembly/macos/install-dependencies.sh @@ -35,5 +35,5 @@ ln -s ../include wasi-sdk/share/wasi-sysroot/usr/include # 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/0.3.0/icu4c-wasi.tar.xz" +wget -O icu.tar.xz "https://github.com/swiftwasm/icu4c-wasi/releases/download/0.4.0/icu4c-wasi.tar.xz" tar xf icu.tar.xz From 76355b0ee58fadae66ff8c00748bea08a1f7c9b2 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Mon, 16 Mar 2020 19:59:09 -0700 Subject: [PATCH 150/838] Don't link icui18n --- utils/webassembly/static-executable-args.lnk | 1 - utils/webassembly/static-stdlib-args.lnk | 1 - 2 files changed, 2 deletions(-) diff --git a/utils/webassembly/static-executable-args.lnk b/utils/webassembly/static-executable-args.lnk index d80b4a58a272c..2d8c872154c5f 100644 --- a/utils/webassembly/static-executable-args.lnk +++ b/utils/webassembly/static-executable-args.lnk @@ -3,7 +3,6 @@ -lswiftImageInspectionShared -lswiftSwiftOnoneSupport -lswiftWasiPthread --licui18n -licuuc -licudata -ldl diff --git a/utils/webassembly/static-stdlib-args.lnk b/utils/webassembly/static-stdlib-args.lnk index af390c6413bd2..e4cdb427d7fdd 100644 --- a/utils/webassembly/static-stdlib-args.lnk +++ b/utils/webassembly/static-stdlib-args.lnk @@ -4,7 +4,6 @@ -lswiftCore -latomic -lswiftImageInspectionShared --licui18n -licuuc -licudata -lstdc++ From 1354a78372e63490f07f579385a0eacc53d86857 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Tue, 17 Mar 2020 16:23:24 +0900 Subject: [PATCH 151/838] Cronize nightly toolchain distribution --- .github/workflows/nightly-distribution.yml | 13 +++ .../distribute-latest-toolchain.sh | 107 ++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 .github/workflows/nightly-distribution.yml create mode 100755 utils/webassembly/distribute-latest-toolchain.sh diff --git a/.github/workflows/nightly-distribution.yml b/.github/workflows/nightly-distribution.yml new file mode 100644 index 0000000000000..e6d23873097a1 --- /dev/null +++ b/.github/workflows/nightly-distribution.yml @@ -0,0 +1,13 @@ +name: Nightly distribution +on: + schedule: + - cron: '0 0 * * *' +jobs: + nightly-distribution: + runs-on: macos-latest + steps: + - uses: actions/checkout@v1 + - run: ./utils/webassembly/distribute-latest-toolchain.sh + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + if: github.ref == 'refs/heads/swiftwasm' diff --git a/utils/webassembly/distribute-latest-toolchain.sh b/utils/webassembly/distribute-latest-toolchain.sh new file mode 100755 index 0000000000000..5ad9754e1c396 --- /dev/null +++ b/utils/webassembly/distribute-latest-toolchain.sh @@ -0,0 +1,107 @@ +#!/bin/bash + +set -xe +repository='swiftwasm/swift' +workflow_name='main.yml' +branch='swiftwasm' + +gh_api=https://api.github.com + +github() { + curl --header "authorization: Bearer $GITHUB_TOKEN" "$@" +} + +latest_run=$(github "${gh_api}/repos/${repository}/actions/workflows/${workflow_name}/runs?branch=${branch}&status=completed" | jq '.workflow_runs | sort_by(.run_number) | last') +artifacts_url=$(echo $latest_run | jq .artifacts_url --raw-output) +head_sha=$(echo $latest_run | jq .head_sha --raw-output) + + +get_artifact_url() { + local name=$1 + github $artifacts_url --fail | jq ".artifacts[] | select(.name == \"$name\") | .archive_download_url" | sed 's/\"//g' +} + +download_artifact() { + local name=$1 + github -L "$(get_artifact_url $name)" --fail -o "$name.zip" +} + +is_released() { + local name=$1 + local code=$(github "$gh_api/repos/$repository/releases/tags/$name" -o /dev/null -w '%{http_code}') + test $code = 200 +} + +create_tag() { + local name=$1 + local sha=$2 + local body=$(cat < Date: Wed, 18 Mar 2020 11:54:24 +0000 Subject: [PATCH 152/838] [WASM] Fix test/stdlib/Mirror.swift --- test/stdlib/Runtime.swift.gyb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/stdlib/Runtime.swift.gyb b/test/stdlib/Runtime.swift.gyb index 7f659e3a9dcfc..9a5208c335c88 100644 --- a/test/stdlib/Runtime.swift.gyb +++ b/test/stdlib/Runtime.swift.gyb @@ -706,7 +706,7 @@ Reflection.test("multiprotocolTypes") { var BitTwiddlingTestSuite = TestSuite("BitTwiddling") BitTwiddlingTestSuite.test("_pointerSize") { -#if arch(i386) || arch(arm) +#if arch(i386) || arch(arm) || arch(wasm32) expectEqual(4, MemoryLayout>.size) #elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x) expectEqual(8, MemoryLayout>.size) @@ -730,7 +730,7 @@ BitTwiddlingTestSuite.test("_isPowerOf2/Int") { expectTrue(_isPowerOf2(asInt(2))) expectFalse(_isPowerOf2(asInt(3))) expectTrue(_isPowerOf2(asInt(1024))) -#if arch(i386) || arch(arm) +#if arch(i386) || arch(arm) || arch(wasm32) // Not applicable to 32-bit architectures. #elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x) expectTrue(_isPowerOf2(asInt(0x8000_0000))) From 29a89d759827508cd0d0e156994004207181023f Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Wed, 18 Mar 2020 11:58:07 +0000 Subject: [PATCH 153/838] [WASM] test/stdlib/Error.swift --- test/stdlib/Error.swift | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/stdlib/Error.swift b/test/stdlib/Error.swift index 1d095e80c9100..2aea1c2a0cb8c 100644 --- a/test/stdlib/Error.swift +++ b/test/stdlib/Error.swift @@ -105,6 +105,7 @@ ErrorTests.test("default domain and code") { enum SillyError: Error { case JazzHands } +#if !os(WASI) ErrorTests.test("try!") .skip(.custom({ _isFastAssertConfiguration() }, reason: "trap is not guaranteed to happen in -Ounchecked")) @@ -127,6 +128,7 @@ ErrorTests.test("try!/location") expectCrashLater() let _: () = try! { throw SillyError.JazzHands }() } +#endif ErrorTests.test("try?") { var value = try? { () throws -> Int in return 1 }() @@ -191,6 +193,7 @@ ErrorTests.test("test dealloc empty error box") { } } +#if !os(WASI) var errors: [Error] = [] ErrorTests.test("willThrow") { if #available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *) { @@ -215,6 +218,7 @@ ErrorTests.test("willThrow") { expectEqual(SillyError.self, type(of: errors.last!)) } } +#endif runAllTests() From a0f48aa21b88b65fdb40c30e1fc9bf58d6390bf8 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Fri, 20 Mar 2020 09:41:01 +0000 Subject: [PATCH 154/838] Try to use wasmer instead of wasmtime to reduce exec time --- test/lit.cfg | 2 +- utils/webassembly/linux/install-dependencies.sh | 7 ++----- utils/webassembly/macos/install-dependencies.sh | 9 +-------- 3 files changed, 4 insertions(+), 14 deletions(-) diff --git a/test/lit.cfg b/test/lit.cfg index cabbca8f0c90f..8fdd05b7bd838 100644 --- a/test/lit.cfg +++ b/test/lit.cfg @@ -1376,7 +1376,7 @@ elif run_os == 'wasi': 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 --' + config.target_run = 'wasmer run --' if 'interpret' in lit_config.params: use_interpreter_for_simple_runs() config.target_sil_opt = ( diff --git a/utils/webassembly/linux/install-dependencies.sh b/utils/webassembly/linux/install-dependencies.sh index 6cfc4cf87eb02..ae5dffeef3817 100755 --- a/utils/webassembly/linux/install-dependencies.sh +++ b/utils/webassembly/linux/install-dependencies.sh @@ -17,12 +17,9 @@ cd $SWIFT_PATH ./utils/update-checkout --clone --scheme wasm --skip-repository swift -# Install wasmtime +# Install wasmer -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 Jx --strip-components 1 -sudo ln -sf /opt/wasmtime/* /usr/local/bin +curl https://get.wasmer.io -sSfL | sh cd $SOURCE_PATH diff --git a/utils/webassembly/macos/install-dependencies.sh b/utils/webassembly/macos/install-dependencies.sh index d46098ff791fd..16ae070c2e709 100755 --- a/utils/webassembly/macos/install-dependencies.sh +++ b/utils/webassembly/macos/install-dependencies.sh @@ -3,7 +3,7 @@ set -ex brew uninstall python@2 || true -brew install cmake ninja llvm sccache +brew install cmake ninja llvm sccache wasmer SOURCE_PATH="$( cd "$(dirname $0)/../../../../" && pwd )" SWIFT_PATH=$SOURCE_PATH/swift @@ -11,13 +11,6 @@ cd $SWIFT_PATH ./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 Jx --strip-components 1 -sudo ln -sf /opt/wasmtime/* /usr/local/bin - cd $SOURCE_PATH wget -O dist-wasi-sdk.tgz.zip "https://github.com/swiftwasm/wasi-sdk/releases/download/0.2.0-swiftwasm/dist-macos-latest.tgz.zip" From f371b792a2d6e49a57b9a8f6d59b46384dec6b3f Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Fri, 20 Mar 2020 12:49:54 +0000 Subject: [PATCH 155/838] [WASM] Disable always failing test case because it crashes --- test/stdlib/StringAPI.swift | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/stdlib/StringAPI.swift b/test/stdlib/StringAPI.swift index 47ee8c50667ea..e65ec357b6f33 100644 --- a/test/stdlib/StringAPI.swift +++ b/test/stdlib/StringAPI.swift @@ -319,6 +319,7 @@ StringTests.test("SameTypeComparisons") { expectFalse(xs != xs) } +#if !os(WASI) StringTests.test("CompareStringsWithUnpairedSurrogates") .xfail( .always(" Strings referring to underlying " + @@ -334,6 +335,7 @@ StringTests.test("CompareStringsWithUnpairedSurrogates") ] ) } +#endif StringTests.test("[String].joined() -> String") { let s = ["hello", "world"].joined() From e5252082e7424edb20afdfc1b8471b273271d4f5 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Fri, 20 Mar 2020 12:51:10 +0000 Subject: [PATCH 156/838] [WASM] Disable crash test case for Mirror --- test/stdlib/Mirror.swift | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/stdlib/Mirror.swift b/test/stdlib/Mirror.swift index 60b45e7e91539..c83e0d4576b17 100644 --- a/test/stdlib/Mirror.swift +++ b/test/stdlib/Mirror.swift @@ -957,6 +957,7 @@ mirrors.test("Addressing") { expectNil(m.descendant(1, 1, "bork")) } +#if !os(WASI) mirrors.test("Invalid Path Type") .skip(.custom( { _isFastAssertConfiguration() }, @@ -968,6 +969,7 @@ mirrors.test("Invalid Path Type") expectCrashLater() _ = m.descendant(X()) } +#endif mirrors.test("PlaygroundQuickLook") { // Customization works. From ea7ca463f55382174e771bacf2f35a7af58a8c10 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Thu, 26 Mar 2020 11:26:36 +0000 Subject: [PATCH 157/838] [CI] Free disk space to avoid IOExceptions https://github.community/t5/GitHub-Actions/BUG-Strange-quot-No-space-left-on-device-quot-IOExceptions-on/m-p/47691/highlight/true#M6920 --- .github/workflows/main.yml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 0b5f322eed1be..d78f26e89a43b 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -14,6 +14,14 @@ jobs: runs-on: ubuntu-18.04 steps: + - name: Free disk space + run: | + df -h + sudo swapoff -a + sudo rm -f /swapfile + sudo apt clean + docker rmi $(docker image ls -aq) + df -h - uses: actions/checkout@v1 with: path: swift From cf2b7ea427fb83445d03a57d24ebf98bd7616216 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Wed, 1 Apr 2020 02:03:37 +0000 Subject: [PATCH 158/838] [WASM] Fix test/stdlib/FlatMapDeprecation.swift --- test/stdlib/FlatMapDeprecation.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/stdlib/FlatMapDeprecation.swift b/test/stdlib/FlatMapDeprecation.swift index ee5545b8ca498..14202e45c82c2 100644 --- a/test/stdlib/FlatMapDeprecation.swift +++ b/test/stdlib/FlatMapDeprecation.swift @@ -1,4 +1,4 @@ -// RUN: %swift -swift-version 4 -typecheck -verify %s +// RUN: %target-typecheck-verify-swift -swift-version 4 func flatMapOnSequence< S : Sequence From ba7116f93a623e49d9da4e8a4f72b18b67efc3f1 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Wed, 1 Apr 2020 02:50:57 +0000 Subject: [PATCH 159/838] Use cranelift as the backend of wasmer instead of single-pass --- test/lit.cfg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/lit.cfg b/test/lit.cfg index 8fdd05b7bd838..448fceefb0a71 100644 --- a/test/lit.cfg +++ b/test/lit.cfg @@ -1376,7 +1376,7 @@ elif run_os == 'wasi': 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 = 'wasmer run --' + config.target_run = 'wasmer run --backend cranelift --' if 'interpret' in lit_config.params: use_interpreter_for_simple_runs() config.target_sil_opt = ( From 74275405b3fddb71d21721fbae065ed95c14516b Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Wed, 1 Apr 2020 02:53:26 +0000 Subject: [PATCH 160/838] [WASM] Disable test/stdlib/InputStream.swift.gyb because of single process --- test/stdlib/InputStream.swift.gyb | 1 + 1 file changed, 1 insertion(+) diff --git a/test/stdlib/InputStream.swift.gyb b/test/stdlib/InputStream.swift.gyb index ffe24359b7d3a..445e654d44e1f 100644 --- a/test/stdlib/InputStream.swift.gyb +++ b/test/stdlib/InputStream.swift.gyb @@ -12,6 +12,7 @@ // -*- swift -*- // RUN: %target-run-simple-swiftgyb // REQUIRES: executable_test +// UNSUPPORTED: OS=wasi import StdlibUnittest From c74876ab8f79d3a9defe1a3aac55cb9b5f8fc0f6 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Thu, 2 Apr 2020 03:04:19 +0000 Subject: [PATCH 161/838] [WASM] Fix test/stdlib/VarArgs.swift --- lib/ClangImporter/ImportType.cpp | 2 +- lib/ClangImporter/MappedTypes.def | 1 + stdlib/public/core/VarArgs.swift | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/ClangImporter/ImportType.cpp b/lib/ClangImporter/ImportType.cpp index 3d067ef2fdab5..16d8063d3f5a3 100644 --- a/lib/ClangImporter/ImportType.cpp +++ b/lib/ClangImporter/ImportType.cpp @@ -711,7 +711,7 @@ namespace { } static const llvm::StringLiteral vaListNames[] = { - "va_list", "__gnuc_va_list", "__va_list" + "va_list", "__gnuc_va_list", "__isoc_va_list", "__va_list" }; ImportHint hint = ImportHint::None; diff --git a/lib/ClangImporter/MappedTypes.def b/lib/ClangImporter/MappedTypes.def index c1a8c8039dcd1..7f11008619a99 100644 --- a/lib/ClangImporter/MappedTypes.def +++ b/lib/ClangImporter/MappedTypes.def @@ -128,6 +128,7 @@ MAP_STDLIB_TYPE("u_int64_t", UnsignedInt, 64, "UInt64", false, DoNothing) // There's an explicit workaround in ImportType.cpp's VisitDecayedType for that. MAP_STDLIB_TYPE("va_list", VaList, 0, "CVaListPointer", false, DoNothing) MAP_STDLIB_TYPE("__gnuc_va_list", VaList, 0, "CVaListPointer", false, DoNothing) +MAP_STDLIB_TYPE("__isoc_va_list", VaList, 0, "CVaListPointer", false, DoNothing) MAP_STDLIB_TYPE("__va_list", VaList, 0, "CVaListPointer", false, DoNothing) // libkern/OSTypes.h types. diff --git a/stdlib/public/core/VarArgs.swift b/stdlib/public/core/VarArgs.swift index 8576a86568b01..d6d9458e96f4a 100644 --- a/stdlib/public/core/VarArgs.swift +++ b/stdlib/public/core/VarArgs.swift @@ -565,7 +565,7 @@ final internal class __VaListBuilder { // supported vararg type is greater than the alignment of Int, such // as non-iOS ARM. Note that we can't use alignof because it // differs from ABI alignment on some architectures. -#if arch(arm) && !os(iOS) +#if (arch(arm) && !os(iOS)) || arch(wasm32) if let arg = arg as? _CVarArgAligned { let alignmentInWords = arg._cVarArgAlignment / MemoryLayout.size let misalignmentInWords = count % alignmentInWords From 524bec885a55980d0570cd4e93d58333bd92a549 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Mon, 6 Apr 2020 08:48:53 +0900 Subject: [PATCH 162/838] [CI] Run primary test cases on CI (#572) * [CI] Run primary test cases on CI * [WASM] Disable simd module tests * Bump cache version * Update ICU --- .github/workflows/main.yml | 4 ++-- test/stdlib/simd.swift.gyb | 1 + test/stdlib/simd_diagnostics.swift | 1 + utils/webassembly/ci.sh | 11 +++++++++++ utils/webassembly/linux/install-dependencies.sh | 2 +- utils/webassembly/macos/install-dependencies.sh | 2 +- 6 files changed, 17 insertions(+), 4 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index d78f26e89a43b..d2bb4d8949a4a 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -28,7 +28,7 @@ jobs: - uses: actions/cache@v1 with: path: ../build-cache - key: ${{ runner.os }}-sccache-v3 + key: ${{ runner.os }}-sccache-v4 - name: Build Linux installable archive run: | ./utils/webassembly/ci.sh @@ -58,7 +58,7 @@ jobs: - uses: actions/cache@v1 with: path: ../build-cache - key: ${{ runner.os }}-sccache-v3 + key: ${{ runner.os }}-sccache-v4 - name: Build macOS installable archive run: ./utils/webassembly/ci.sh - name: Upload macOS installable archive diff --git a/test/stdlib/simd.swift.gyb b/test/stdlib/simd.swift.gyb index 19953ae6598c8..e948ecbf19729 100644 --- a/test/stdlib/simd.swift.gyb +++ b/test/stdlib/simd.swift.gyb @@ -3,6 +3,7 @@ // FIXME: No simd module on linux rdar://problem/20795411 // XFAIL: linux, windows +// XFAIL: OS=wasi import simd import StdlibUnittest diff --git a/test/stdlib/simd_diagnostics.swift b/test/stdlib/simd_diagnostics.swift index d2230cc2a0970..5ffcb5897239d 100644 --- a/test/stdlib/simd_diagnostics.swift +++ b/test/stdlib/simd_diagnostics.swift @@ -2,6 +2,7 @@ // FIXME: No simd module on linux rdar://problem/20795411 // XFAIL: linux, windows, wasm +// XFAIL: OS=wasi import simd diff --git a/utils/webassembly/ci.sh b/utils/webassembly/ci.sh index 359c085749e2d..75c9adb37ff9e 100755 --- a/utils/webassembly/ci.sh +++ b/utils/webassembly/ci.sh @@ -12,9 +12,13 @@ else fi BUILD_SCRIPT=$UTILS_PATH/build-toolchain.sh +RUN_TEST_BIN=$SWIFT_PATH/utils/run-test +BUILD_DIR=$SOURCE_PATH/build/Ninja-ReleaseAssert $DEPENDENCIES_SCRIPT +export PATH="$HOME/.wasmer/bin:$PATH" + export SCCACHE_CACHE_SIZE="50G" export SCCACHE_DIR="$SOURCE_PATH/build-cache" @@ -23,6 +27,13 @@ FLAGS="--release $CACHE_FLAGS --verbose" $BUILD_SCRIPT $FLAGS +if [[ "$(uname)" == "Darwin" ]]; then + # workaround: host target test directory is necessary to use run-test + mkdir -p $BUILD_DIR/swift-macosx-x86_64/test-macosx-x86_64 +fi + +$RUN_TEST_BIN --build-dir $BUILD_DIR --target wasi-wasm32 test/stdlib/ + if [[ "$(uname)" == "Linux" ]]; then echo "Skip running test suites for Linux" else diff --git a/utils/webassembly/linux/install-dependencies.sh b/utils/webassembly/linux/install-dependencies.sh index ae5dffeef3817..edb8bc3daa262 100755 --- a/utils/webassembly/linux/install-dependencies.sh +++ b/utils/webassembly/linux/install-dependencies.sh @@ -41,7 +41,7 @@ mv $WASI_SDK_FULL_NAME ./wasi-sdk # 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/0.4.0/icu4c-wasi.tar.xz" +wget -O icu.tar.xz "https://github.com/swiftwasm/icu4c-wasi/releases/download/0.5.0/icu4c-wasi.tar.xz" tar xf icu.tar.xz # Install sccache diff --git a/utils/webassembly/macos/install-dependencies.sh b/utils/webassembly/macos/install-dependencies.sh index 16ae070c2e709..01e270c29f4b5 100755 --- a/utils/webassembly/macos/install-dependencies.sh +++ b/utils/webassembly/macos/install-dependencies.sh @@ -28,5 +28,5 @@ ln -s ../include wasi-sdk/share/wasi-sysroot/usr/include # 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/0.4.0/icu4c-wasi.tar.xz" +wget -O icu.tar.xz "https://github.com/swiftwasm/icu4c-wasi/releases/download/0.5.0/icu4c-wasi.tar.xz" tar xf icu.tar.xz From 64ea425bd61a8e8b96386e2962fe3900d29ce041 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Tue, 7 Apr 2020 16:19:43 +0900 Subject: [PATCH 163/838] [WASM] Use embeded wasi-sysroot headers for Glibc module (#590) --- utils/webassembly/build-toolchain.sh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/utils/webassembly/build-toolchain.sh b/utils/webassembly/build-toolchain.sh index 6167419466fad..5c53ac98acee3 100755 --- a/utils/webassembly/build-toolchain.sh +++ b/utils/webassembly/build-toolchain.sh @@ -67,6 +67,9 @@ cp -r $WASI_SDK_PATH/share/wasi-sysroot usr/share # Build SwiftPM and install it into toolchain $UTILS_PATH/build-swiftpm.sh $TMP_DIR/$TOOLCHAIN_NAME +# Replace absolute sysroot path with relative path +sed -i -e "s@\".*/include@\"../../../../share/wasi-sysroot/include@g" $TMP_DIR/$TOOLCHAIN_NAME/usr/lib/swift/wasi/wasm32/glibc.modulemap + # Copy nightly-toolchain's host environment stdlib into toolchain if [[ "$(uname)" == "Linux" ]]; then From d3a1034747ea399802923d8fdb15749f9895ef8d Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Thu, 9 Apr 2020 16:28:43 +0100 Subject: [PATCH 164/838] Add os(WASI) check to TgmathDerivatives.swift.gyb --- stdlib/public/Differentiation/TgmathDerivatives.swift.gyb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/public/Differentiation/TgmathDerivatives.swift.gyb b/stdlib/public/Differentiation/TgmathDerivatives.swift.gyb index a6294247ade73..d57700f93b0ff 100644 --- a/stdlib/public/Differentiation/TgmathDerivatives.swift.gyb +++ b/stdlib/public/Differentiation/TgmathDerivatives.swift.gyb @@ -16,7 +16,7 @@ import Swift #if os(macOS) || os(iOS) || os(tvOS) || os(watchOS) import Darwin.C.tgmath -#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(WASI) import Glibc #elseif os(Windows) import MSVCRT From f61d46b89f7de67e64685efbf8ead3be92682054 Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Fri, 10 Apr 2020 01:13:31 +0100 Subject: [PATCH 165/838] Fix Glibc dependency in CMakeLists.txt --- stdlib/public/Differentiation/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/stdlib/public/Differentiation/CMakeLists.txt b/stdlib/public/Differentiation/CMakeLists.txt index 14f881517674f..02848c76597fc 100644 --- a/stdlib/public/Differentiation/CMakeLists.txt +++ b/stdlib/public/Differentiation/CMakeLists.txt @@ -29,6 +29,7 @@ add_swift_target_library(swift_Differentiation ${SWIFT_STDLIB_LIBRARY_BUILD_TYPE SWIFT_MODULE_DEPENDS_FREEBSD Glibc SWIFT_MODULE_DEPENDS_CYGWIN Glibc SWIFT_MODULE_DEPENDS_HAIKU Glibc + SWIFT_MODULE_DEPENDS_WASI Glibc SWIFT_MODULE_DEPENDS_WINDOWS MSVCRT SWIFT_COMPILE_FLAGS From 320d89cb79b4f37caeb129bc925a49747a8dec44 Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Mon, 13 Apr 2020 09:07:56 +0100 Subject: [PATCH 166/838] Use latest CMake when building for Linux (#648) According to the upstream `README.md` at least version 3.16.5 is required. --- utils/webassembly/linux/install-dependencies.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/webassembly/linux/install-dependencies.sh b/utils/webassembly/linux/install-dependencies.sh index edb8bc3daa262..ea2fe17a4a12a 100755 --- a/utils/webassembly/linux/install-dependencies.sh +++ b/utils/webassembly/linux/install-dependencies.sh @@ -23,7 +23,7 @@ curl https://get.wasmer.io -sSfL | sh 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" +wget -O install_cmake.sh "https://github.com/Kitware/CMake/releases/download/v3.17.1/cmake-3.17.1-Linux-x86_64.sh" chmod +x install_cmake.sh sudo mkdir -p /opt/cmake sudo ./install_cmake.sh --skip-license --prefix=/opt/cmake From 606aacc746562d34055dccb7621daf1ab516325f Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Tue, 14 Apr 2020 16:13:11 +0100 Subject: [PATCH 167/838] Move _WASI_EMULATED_MMAN flag from AddSwift.cmake to AddSwiftStdlib.cmake --- cmake/modules/AddSwift.cmake | 2 -- stdlib/cmake/modules/AddSwiftStdlib.cmake | 4 ++++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/cmake/modules/AddSwift.cmake b/cmake/modules/AddSwift.cmake index 83de5ef399e62..554a3ce832fbc 100644 --- a/cmake/modules/AddSwift.cmake +++ b/cmake/modules/AddSwift.cmake @@ -351,8 +351,6 @@ function(_add_host_variant_c_compile_flags) list(APPEND result "SHELL:${CMAKE_INCLUDE_SYSTEM_FLAG_C}${path}") endforeach() list(APPEND result "-D__ANDROID_API__=${SWIFT_ANDROID_API_LEVEL}") - elseif("${CFLAGS_SDK}" STREQUAL "WASI") - list(APPEND result "-D_WASI_EMULATED_MMAN") endif() if("${CFLAGS_SDK}" STREQUAL "LINUX") diff --git a/stdlib/cmake/modules/AddSwiftStdlib.cmake b/stdlib/cmake/modules/AddSwiftStdlib.cmake index ccc0edf0768e4..8bd70a034116c 100644 --- a/stdlib/cmake/modules/AddSwiftStdlib.cmake +++ b/stdlib/cmake/modules/AddSwiftStdlib.cmake @@ -284,6 +284,10 @@ function(_add_target_variant_c_compile_flags) list(APPEND result "-D__ANDROID_API__=${SWIFT_ANDROID_API_LEVEL}") endif() + if("${CFLAGS_SDK}" STREQUAL "WASI") + list(APPEND result "-D_WASI_EMULATED_MMAN") + endif() + if("${CFLAGS_SDK}" STREQUAL "LINUX") if(${CFLAGS_ARCH} STREQUAL x86_64) # this is the minimum architecture that supports 16 byte CAS, which is necessary to avoid a dependency to libatomic From 6484af8ed49f6d48c0f4e36ce398a69f61d28707 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Thu, 16 Apr 2020 15:24:56 +0900 Subject: [PATCH 168/838] [WASM] Revert NeedsThunk patch for thin to thick --- lib/SIL/IR/TypeLowering.cpp | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/lib/SIL/IR/TypeLowering.cpp b/lib/SIL/IR/TypeLowering.cpp index 91eb09dd18d45..4fd63d39a7db8 100644 --- a/lib/SIL/IR/TypeLowering.cpp +++ b/lib/SIL/IR/TypeLowering.cpp @@ -2885,23 +2885,10 @@ 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 && 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 From 834952db8ca01f38938bbdc597be6b264a8c24c0 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Thu, 16 Apr 2020 22:14:09 +0900 Subject: [PATCH 169/838] [WASM] Revert swift_willThrow patch and change to use swifterror --- lib/IRGen/GenBuiltin.cpp | 15 ++++---------- lib/IRGen/GenFunc.h | 4 ---- lib/IRGen/IRGenModule.cpp | 23 ++++++++++++++++++++- stdlib/public/runtime/ErrorObject.h | 12 ----------- stdlib/public/runtime/ErrorObjectCommon.cpp | 10 +-------- 5 files changed, 27 insertions(+), 37 deletions(-) diff --git a/lib/IRGen/GenBuiltin.cpp b/lib/IRGen/GenBuiltin.cpp index e6ae58e74d640..f8ff93a6aacab 100644 --- a/lib/IRGen/GenBuiltin.cpp +++ b/lib/IRGen/GenBuiltin.cpp @@ -398,17 +398,10 @@ if (Builtin.ID == BuiltinValueKind::id) { \ call->addAttribute(llvm::AttributeList::FirstArgIndex + 1, llvm::Attribute::ReadOnly); - // Remove swiftself and swifterror attribute to match signature generated from - // RuntimeFunctions.def. These two parameters are passed using swifterror and swiftself, - // but the definition of swift_willThrow generated from the def file doesn't have those - // attributes due to the def file limitation. In WebAssembly context, these attributes are - // lowered as usual parameters, so this doesn't have any side effects. - if (IGF.IGM.TargetInfo.OutputObjectFormat != llvm::Triple::Wasm) { - auto attrs = call->getAttributes(); - IGF.IGM.addSwiftSelfAttributes(attrs, 0); - IGF.IGM.addSwiftErrorAttributes(attrs, 1); - call->setAttributes(attrs); - } + auto attrs = call->getAttributes(); + IGF.IGM.addSwiftSelfAttributes(attrs, 0); + IGF.IGM.addSwiftErrorAttributes(attrs, 1); + call->setAttributes(attrs); IGF.Builder.CreateStore(llvm::ConstantPointerNull::get(IGF.IGM.ErrorPtrTy), errorBuffer); diff --git a/lib/IRGen/GenFunc.h b/lib/IRGen/GenFunc.h index 620b5ebeb54c0..04bc2e4732182 100644 --- a/lib/IRGen/GenFunc.h +++ b/lib/IRGen/GenFunc.h @@ -54,10 +54,6 @@ namespace irgen { CanSILFunctionType origType, CanSILFunctionType substType, CanSILFunctionType outType, Explosion &out, bool isOutlined); - - llvm::Function *getThinToThickForwarder(IRGenModule &IGM, - const Optional &staticFnPtr, - const CanSILFunctionType origType); } // end namespace irgen } // end namespace swift diff --git a/lib/IRGen/IRGenModule.cpp b/lib/IRGen/IRGenModule.cpp index d63d5db3dcf7b..01024882cd919 100644 --- a/lib/IRGen/IRGenModule.cpp +++ b/lib/IRGen/IRGenModule.cpp @@ -553,9 +553,17 @@ IRGenModule::IRGenModule(IRGenerator &irgen, AtomicBoolAlign = Alignment(ClangASTContext->getTypeSize(atomicBoolTy)); } + // On WebAssembly, tail optional arguments are not allowed because wasm requires + // callee and caller signature should be same. So LLVM adds dummy arguments for + // swiftself and swifterror. If there is swiftself but there isn't swifterror in + // a swiftcc function or invocation, then LLVM adds dummy swifterror parameter or + // argument. To count up how many dummy arguments should be added, we need to mark + // it as swifterror even though it's not in register. + // + // TODO: Before sending patch, please rename `IsSwiftErrorInRegister` to `ShouldUseSwiftError` IsSwiftErrorInRegister = clang::CodeGen::swiftcall::isSwiftErrorLoweredInRegister( - ClangCodeGen->CGM()); + ClangCodeGen->CGM()) || TargetInfo.OutputObjectFormat == llvm::Triple::Wasm; #ifndef NDEBUG sanityCheckStdlib(*this); @@ -761,6 +769,19 @@ llvm::Constant *swift::getRuntimeFn(llvm::Module &Module, fn->addAttributes(llvm::AttributeList::FunctionIndex, buildFnAttr); fn->addAttributes(llvm::AttributeList::ReturnIndex, buildRetAttr); fn->addParamAttrs(0, buildFirstParamAttr); + + // Add swiftself and swifterror attributes only when swift_willThrow + // swift_willThrow is defined in RuntimeFunctions.def, but due to the + // DSL limitation, arguments attributes are not set. + // On the other hand, caller of swift_willThrow assumes that it's attributed + // with swiftself and swifterror. + // This mismatch of attributes would be issue when lowering to WebAssembly. + // While lowering, LLVM count up how many dummy params are necssary to match + // callee and caller signature. So we need to add them correctly. + if (functionName == "swift_willThrow") { + fn->addParamAttr(0, Attribute::AttrKind::SwiftSelf); + fn->addParamAttr(1, Attribute::AttrKind::SwiftError); + } } return cache; diff --git a/stdlib/public/runtime/ErrorObject.h b/stdlib/public/runtime/ErrorObject.h index b67a80f8d1ed3..5e0b702cad5d2 100644 --- a/stdlib/public/runtime/ErrorObject.h +++ b/stdlib/public/runtime/ErrorObject.h @@ -206,21 +206,9 @@ SWIFT_RUNTIME_STDLIB_API void swift_errorRelease(SwiftError *object); /// Breakpoint hook for debuggers. -#ifdef __wasm__ -// Notes: -// Remove swiftself and swifterror attribute to match signature generated from -// RuntimeFunctions.def. These two parameters are passed using swifterror and swiftself, -// but the definition of swift_willThrow generated from the def file doesn't have those -// attributes due to the def file limitation. In WebAssembly context, these attributes are -// lowered as usual parameters, so this doesn't have any side effects. -SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_API -void swift_willThrow(void *unused, - SwiftError **object); -#else SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_API void swift_willThrow(SWIFT_CONTEXT void *unused, SWIFT_ERROR_RESULT SwiftError **object); -#endif /// Halt in response to an error. SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_API LLVM_ATTRIBUTE_NORETURN diff --git a/stdlib/public/runtime/ErrorObjectCommon.cpp b/stdlib/public/runtime/ErrorObjectCommon.cpp index 7f445db18fe40..e000603431aba 100644 --- a/stdlib/public/runtime/ErrorObjectCommon.cpp +++ b/stdlib/public/runtime/ErrorObjectCommon.cpp @@ -27,17 +27,9 @@ using namespace swift; void (*swift::_swift_willThrow)(SwiftError *error); /// Breakpoint hook for debuggers, and calls _swift_willThrow if set. -#ifdef __wasm__ -// Notes: -// The reason of this ifdef is described in header file. -SWIFT_CC(swift) void -swift::swift_willThrow(void *unused, SwiftError **error) -#else SWIFT_CC(swift) void swift::swift_willThrow(SWIFT_CONTEXT void *unused, - SWIFT_ERROR_RESULT SwiftError **error) -#endif -{ + SWIFT_ERROR_RESULT SwiftError **error) { // Cheap check to bail out early, since we expect there to be no callbacks // the vast majority of the time. if (SWIFT_LIKELY(!_swift_willThrow)) From 7fab66c9688f663c863b516fdf18a48b273946c3 Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Sat, 18 Apr 2020 23:21:13 +0100 Subject: [PATCH 170/838] Attempt to reduce disk space used during builds (#706) * Avoid building tools on Linux * Run `apt clean` in linux/install-dependencies.sh * Update build-linux.sh --- utils/webassembly/linux/install-dependencies.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/utils/webassembly/linux/install-dependencies.sh b/utils/webassembly/linux/install-dependencies.sh index ea2fe17a4a12a..3218bd4448a6e 100755 --- a/utils/webassembly/linux/install-dependencies.sh +++ b/utils/webassembly/linux/install-dependencies.sh @@ -10,6 +10,7 @@ sudo apt install -y \ libpython-dev libncurses5-dev pkg-config \ libblocksruntime-dev libcurl4-openssl-dev \ systemtap-sdt-dev tzdata rsync wget llvm zip unzip +sudo apt clean SOURCE_PATH="$( cd "$(dirname $0)/../../../.." && pwd )" SWIFT_PATH=$SOURCE_PATH/swift From 8060135cbcfbbdb7855842015b005d1520b1cd21 Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Sun, 19 Apr 2020 09:35:37 +0100 Subject: [PATCH 171/838] Fix help text in driver_arguments.py (#707) Current `in_group` argument is misleading as mentions Android for the WebAssembly arguments group. --- utils/build_swift/build_swift/driver_arguments.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/utils/build_swift/build_swift/driver_arguments.py b/utils/build_swift/build_swift/driver_arguments.py index 2f9324da2afd3..2f8ff95ceff3e 100644 --- a/utils/build_swift/build_swift/driver_arguments.py +++ b/utils/build_swift/build_swift/driver_arguments.py @@ -1088,7 +1088,8 @@ def create_argument_parser(): 'Currently only armv7 and aarch64 are supported. ' '%(default)s is the default.') - in_group('Build settings for Android') + # ------------------------------------------------------------------------- + in_group('Build settings for WebAssembly') option('--wasi-sdk', store_path, help='An absolute path to WASI SDK that will be used as a libc ' From caf026f3aa995da1f778a41135c80fea4f9dadf8 Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Mon, 20 Apr 2020 19:40:08 +0100 Subject: [PATCH 172/838] Resolve conflicts with master (#727) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Yet another conflict with OpenBSD ifdefs 🙂 --- include/swift/AST/DiagnosticsFrontend.def | 4 - include/swift/Basic/Platform.h | 6 +- include/swift/Frontend/Frontend.h | 22 ++- include/swift/Option/Options.td | 4 + lib/Basic/LangOptions.cpp | 2 +- lib/Basic/Platform.cpp | 52 ++++-- lib/Driver/DarwinToolChains.cpp | 2 +- lib/Driver/Driver.cpp | 27 +-- lib/Driver/ToolChains.cpp | 3 +- lib/Frontend/CompilerInvocation.cpp | 47 +++-- lib/Frontend/Frontend.cpp | 5 +- lib/FrontendTool/FrontendTool.cpp | 171 ++++++++++-------- lib/IDE/REPLCodeCompletion.cpp | 3 +- lib/IRGen/SwiftTargetInfo.cpp | 2 +- lib/SILOptimizer/Utils/Devirtualize.cpp | 18 +- lib/Serialization/ModuleFile.cpp | 3 - lib/Serialization/SerializedModuleLoader.cpp | 2 +- stdlib/cmake/modules/AddSwiftStdlib.cmake | 7 + stdlib/public/Differentiation/CMakeLists.txt | 1 + .../TgmathDerivatives.swift.gyb | 2 +- test/Driver/infer-simulator.swift | 13 -- test/Driver/linker-arclite.swift | 6 +- test/Driver/linker.swift | 8 +- test/Driver/print_target_info.swift | 10 - test/Driver/profiling.swift | 12 +- test/Driver/sanitize_scudo.swift | 12 +- test/Driver/sanitizers.swift | 24 +-- .../simulatorTargetEnv.swift | 4 +- .../devirtualize_class_method.swift | 37 ++++ .../load-target-normalization.swift | 9 + test/diagnostics/no_warnings_as_errors.swift | 12 ++ .../swift-api-digester/swift-api-digester.cpp | 8 +- 32 files changed, 319 insertions(+), 219 deletions(-) delete mode 100644 test/Driver/infer-simulator.swift create mode 100644 test/SILOptimizer/devirtualize_class_method.swift create mode 100644 test/diagnostics/no_warnings_as_errors.swift diff --git a/include/swift/AST/DiagnosticsFrontend.def b/include/swift/AST/DiagnosticsFrontend.def index e59e17bf751f6..4ed1d7a62d92e 100644 --- a/include/swift/AST/DiagnosticsFrontend.def +++ b/include/swift/AST/DiagnosticsFrontend.def @@ -39,10 +39,6 @@ ERROR(error_unsupported_target_arch, none, ERROR(error_unsupported_opt_for_target, none, "unsupported option '%0' for target '%1'", (StringRef, StringRef)) -WARNING(warning_inferred_simulator_target,none, - "inferring simulator environment for target '%0'; " - "use '-target %1' instead", (StringRef, StringRef)) - ERROR(error_argument_not_allowed_with, none, "argument '%0' is not allowed with '%1'", (StringRef, StringRef)) diff --git a/include/swift/Basic/Platform.h b/include/swift/Basic/Platform.h index c0cd0fa832eef..eb21ea7a29714 100644 --- a/include/swift/Basic/Platform.h +++ b/include/swift/Basic/Platform.h @@ -43,12 +43,12 @@ namespace swift { /// Returns true if the given triple represents watchOS running in a simulator. bool tripleIsWatchSimulator(const llvm::Triple &triple); + /// Return true if the given triple represents any simulator. + bool tripleIsAnySimulator(const llvm::Triple &triple); + /// Returns true if the given triple represents a macCatalyst environment. bool tripleIsMacCatalystEnvironment(const llvm::Triple &triple); - /// Determine whether the triple infers the "simulator" environment. - bool tripleInfersSimulatorEnvironment(const llvm::Triple &triple); - /// Returns true if the given -target triple and -target-variant triple /// can be zippered. bool triplesAreValidForZippering(const llvm::Triple &target, diff --git a/include/swift/Frontend/Frontend.h b/include/swift/Frontend/Frontend.h index fa5fa5652ac7d..7718407233f5d 100644 --- a/include/swift/Frontend/Frontend.h +++ b/include/swift/Frontend/Frontend.h @@ -386,9 +386,25 @@ class CompilerInvocation { std::string getLdAddCFileOutputPathForWholeModule() const; +public: + /// Given the current configuration of this frontend invocation, a set of + /// supplementary output paths, and a module, compute the appropriate set of + /// serialization options. + /// + /// FIXME: The \p module parameter supports the + /// \c SerializeOptionsForDebugging hack. SerializationOptions computeSerializationOptions(const SupplementaryOutputPaths &outs, - bool moduleIsPublic) const; + const ModuleDecl *module) const; + + /// Returns an approximation of whether the given module could be + /// redistributed and consumed by external clients. + /// + /// FIXME: The scope of this computation should be limited entirely to + /// PrintAsObjC. Unfortunately, it has been co-opted to support the + /// \c SerializeOptionsForDebugging hack. Once this information can be + /// transferred from module files to the dSYMs, remove this. + bool isModuleExternallyConsumed(const ModuleDecl *mod) const; }; /// A class which manages the state and execution of the compiler. @@ -555,9 +571,7 @@ class CompilerInstance { /// Returns true if there was an error during setup. bool setup(const CompilerInvocation &Invocation); - const CompilerInvocation &getInvocation() { - return Invocation; - } + const CompilerInvocation &getInvocation() const { return Invocation; } /// If a code completion buffer has been set, returns the corresponding source /// file. diff --git a/include/swift/Option/Options.td b/include/swift/Option/Options.td index d79b131a9eea0..8aa8775751a59 100644 --- a/include/swift/Option/Options.td +++ b/include/swift/Option/Options.td @@ -528,6 +528,10 @@ def warnings_as_errors : Flag<["-"], "warnings-as-errors">, Flags<[FrontendOption]>, HelpText<"Treat warnings as errors">; +def no_warnings_as_errors : Flag<["-"], "no-warnings-as-errors">, + Flags<[FrontendOption]>, + HelpText<"Don't treat warnings as errors">; + def continue_building_after_errors : Flag<["-"], "continue-building-after-errors">, Flags<[FrontendOption, DoesNotAffectIncrementalBuild]>, HelpText<"Continue building, even after errors are encountered">; diff --git a/lib/Basic/LangOptions.cpp b/lib/Basic/LangOptions.cpp index cc2bf058d14f2..682f6e1010c61 100644 --- a/lib/Basic/LangOptions.cpp +++ b/lib/Basic/LangOptions.cpp @@ -361,7 +361,7 @@ std::pair LangOptions::setTarget(llvm::Triple triple) { // Set the "targetEnvironment" platform condition if targeting a simulator // environment. Otherwise _no_ value is present for targetEnvironment; it's // an optional disambiguating refinement of the triple. - if (Target.isSimulatorEnvironment()) + if (swift::tripleIsAnySimulator(Target)) addPlatformConditionValue(PlatformConditionKind::TargetEnvironment, "simulator"); diff --git a/lib/Basic/Platform.cpp b/lib/Basic/Platform.cpp index 9f46b1785a548..3722a88062d41 100644 --- a/lib/Basic/Platform.cpp +++ b/lib/Basic/Platform.cpp @@ -19,17 +19,40 @@ using namespace swift; bool swift::tripleIsiOSSimulator(const llvm::Triple &triple) { + llvm::Triple::ArchType arch = triple.getArch(); return (triple.isiOS() && !tripleIsMacCatalystEnvironment(triple) && - triple.isSimulatorEnvironment()); + // FIXME: transitional, this should eventually stop testing arch, and + // switch to only checking the -environment field. + (triple.isSimulatorEnvironment() || + arch == llvm::Triple::x86 || arch == llvm::Triple::x86_64)); } bool swift::tripleIsAppleTVSimulator(const llvm::Triple &triple) { - return (triple.isTvOS() && triple.isSimulatorEnvironment()); + llvm::Triple::ArchType arch = triple.getArch(); + return (triple.isTvOS() && + // FIXME: transitional, this should eventually stop testing arch, and + // switch to only checking the -environment field. + (triple.isSimulatorEnvironment() || + arch == llvm::Triple::x86 || arch == llvm::Triple::x86_64)); } bool swift::tripleIsWatchSimulator(const llvm::Triple &triple) { - return (triple.isWatchOS() && triple.isSimulatorEnvironment()); + llvm::Triple::ArchType arch = triple.getArch(); + return (triple.isWatchOS() && + // FIXME: transitional, this should eventually stop testing arch, and + // switch to only checking the -environment field. + (triple.isSimulatorEnvironment() || + arch == llvm::Triple::x86 || arch == llvm::Triple::x86_64)); +} + +bool swift::tripleIsAnySimulator(const llvm::Triple &triple) { + // FIXME: transitional, this should eventually just use the -environment + // field. + return triple.isSimulatorEnvironment() || + tripleIsiOSSimulator(triple) || + tripleIsWatchSimulator(triple) || + tripleIsAppleTVSimulator(triple); } bool swift::tripleIsMacCatalystEnvironment(const llvm::Triple &triple) { @@ -37,21 +60,6 @@ bool swift::tripleIsMacCatalystEnvironment(const llvm::Triple &triple) { triple.getEnvironment() == llvm::Triple::MacABI; } -bool swift::tripleInfersSimulatorEnvironment(const llvm::Triple &triple) { - switch (triple.getOS()) { - case llvm::Triple::IOS: - case llvm::Triple::TvOS: - case llvm::Triple::WatchOS: - return !triple.hasEnvironment() && - (triple.getArch() == llvm::Triple::x86 || - triple.getArch() == llvm::Triple::x86_64) && - !tripleIsMacCatalystEnvironment(triple); - - default: - return false; - } -} - bool swift::triplesAreValidForZippering(const llvm::Triple &target, const llvm::Triple &targetVariant) { // The arch and vendor must match. @@ -319,6 +327,14 @@ getOSForAppleTargetSpecificModuleTriple(const llvm::Triple &triple) { static Optional getEnvironmentForAppleTargetSpecificModuleTriple(const llvm::Triple &triple) { auto tripleEnvironment = triple.getEnvironmentName(); + + // If the environment is empty, infer a "simulator" environment based on the + // OS and architecture combination. This feature is deprecated and exists for + // backwards compatibility only; build systems should pass the "simulator" + // environment explicitly if they know they're building for a simulator. + if (tripleEnvironment == "" && swift::tripleIsAnySimulator(triple)) + return StringRef("simulator"); + return llvm::StringSwitch>(tripleEnvironment) .Cases("unknown", "", None) // These values are also supported, but are handled by the default case below: diff --git a/lib/Driver/DarwinToolChains.cpp b/lib/Driver/DarwinToolChains.cpp index bfd3bd442d3cc..7bf80dd5748af 100644 --- a/lib/Driver/DarwinToolChains.cpp +++ b/lib/Driver/DarwinToolChains.cpp @@ -479,7 +479,7 @@ toolchains::Darwin::addProfileGenerationArgs(ArgStringList &Arguments, } StringRef Sim; - if (Triple.isSimulatorEnvironment()) { + if (tripleIsAnySimulator(Triple)) { Sim = "sim"; } diff --git a/lib/Driver/Driver.cpp b/lib/Driver/Driver.cpp index be7149e797b0c..2392865ce5acb 100644 --- a/lib/Driver/Driver.cpp +++ b/lib/Driver/Driver.cpp @@ -140,7 +140,8 @@ static void validateBridgingHeaderArgs(DiagnosticEngine &diags, static void validateWarningControlArgs(DiagnosticEngine &diags, const ArgList &args) { if (args.hasArg(options::OPT_suppress_warnings) && - args.hasArg(options::OPT_warnings_as_errors)) { + args.hasFlag(options::OPT_warnings_as_errors, + options::OPT_no_warnings_as_errors, false)) { diags.diagnose(SourceLoc(), diag::error_conflicting_options, "-warnings-as-errors", "-suppress-warnings"); } @@ -264,31 +265,17 @@ static void validateArgs(DiagnosticEngine &diags, const ArgList &args, std::unique_ptr Driver::buildToolChain(const llvm::opt::InputArgList &ArgList) { - if (const Arg *A = ArgList.getLastArg(options::OPT_target)) { + if (const Arg *A = ArgList.getLastArg(options::OPT_target)) DefaultTargetTriple = llvm::Triple::normalize(A->getValue()); - } - - llvm::Triple target(DefaultTargetTriple); - // Backward compatibility hack: infer "simulator" environment for x86 - // iOS/tvOS/watchOS. - if (tripleInfersSimulatorEnvironment(target)) { - // Set the simulator environment. - target.setEnvironment(llvm::Triple::EnvironmentType::Simulator); - - auto newTargetTriple = target.normalize(); - Diags.diagnose(SourceLoc(), diag::warning_inferred_simulator_target, - DefaultTargetTriple, newTargetTriple); - - DefaultTargetTriple = newTargetTriple; - } + const llvm::Triple target(DefaultTargetTriple); switch (target.getOS()) { + case llvm::Triple::Darwin: + case llvm::Triple::MacOSX: case llvm::Triple::IOS: case llvm::Triple::TvOS: - case llvm::Triple::WatchOS: - case llvm::Triple::Darwin: - case llvm::Triple::MacOSX: { + case llvm::Triple::WatchOS: { Optional targetVariant; if (const Arg *A = ArgList.getLastArg(options::OPT_target_variant)) targetVariant = llvm::Triple(llvm::Triple::normalize(A->getValue())); diff --git a/lib/Driver/ToolChains.cpp b/lib/Driver/ToolChains.cpp index 5777683e62a61..3336c432cad20 100644 --- a/lib/Driver/ToolChains.cpp +++ b/lib/Driver/ToolChains.cpp @@ -216,7 +216,8 @@ void ToolChain::addCommonFrontendArgs(const OutputInfo &OI, inputArgs.AddLastArg(arguments, options::OPT_profile_generate); inputArgs.AddLastArg(arguments, options::OPT_profile_use); inputArgs.AddLastArg(arguments, options::OPT_profile_coverage_mapping); - inputArgs.AddLastArg(arguments, options::OPT_warnings_as_errors); + inputArgs.AddAllArgs(arguments, options::OPT_warnings_as_errors, + options::OPT_no_warnings_as_errors); inputArgs.AddLastArg(arguments, options::OPT_sanitize_EQ); inputArgs.AddLastArg(arguments, options::OPT_sanitize_recover_EQ); inputArgs.AddLastArg(arguments, options::OPT_sanitize_coverage_EQ); diff --git a/lib/Frontend/CompilerInvocation.cpp b/lib/Frontend/CompilerInvocation.cpp index cf873456b1cb5..79a220109f765 100644 --- a/lib/Frontend/CompilerInvocation.cpp +++ b/lib/Frontend/CompilerInvocation.cpp @@ -553,22 +553,9 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args, llvm::Triple Target = Opts.Target; StringRef TargetArg; - std::string TargetArgScratch; - if (const Arg *A = Args.getLastArg(OPT_target)) { Target = llvm::Triple(A->getValue()); TargetArg = A->getValue(); - - // Backward compatibility hack: infer "simulator" environment for x86 - // iOS/tvOS/watchOS. The driver takes care of this for the frontend - // most of the time, but loading of old .swiftinterface files goes - // directly to the frontend. - if (tripleInfersSimulatorEnvironment(Target)) { - // Set the simulator environment. - Target.setEnvironment(llvm::Triple::EnvironmentType::Simulator); - TargetArgScratch = Target.str(); - TargetArg = TargetArgScratch; - } } if (const Arg *A = Args.getLastArg(OPT_target_variant)) { @@ -773,7 +760,8 @@ static bool ParseClangImporterArgs(ClangImporterOptions &Opts, Opts.PCHDisableValidation |= Args.hasArg(OPT_pch_disable_validation); } - if (Args.hasArg(OPT_warnings_as_errors)) + if (Args.hasFlag(options::OPT_warnings_as_errors, + options::OPT_no_warnings_as_errors, false)) Opts.ExtraArgs.push_back("-Werror"); Opts.DebuggerSupport |= Args.hasArg(OPT_debugger_support); @@ -858,7 +846,9 @@ static bool ParseDiagnosticArgs(DiagnosticOptions &Opts, ArgList &Args, /*Default=*/llvm::sys::Process::StandardErrHasColors()); Opts.FixitCodeForAllDiagnostics |= Args.hasArg(OPT_fixit_all); Opts.SuppressWarnings |= Args.hasArg(OPT_suppress_warnings); - Opts.WarningsAsErrors |= Args.hasArg(OPT_warnings_as_errors); + Opts.WarningsAsErrors = Args.hasFlag(options::OPT_warnings_as_errors, + options::OPT_no_warnings_as_errors, + false); Opts.PrintDiagnosticNames |= Args.hasArg(OPT_debug_diagnostic_names); Opts.PrintEducationalNotes |= Args.hasArg(OPT_print_educational_notes); Opts.EnableExperimentalFormatting |= @@ -1714,3 +1704,30 @@ CompilerInvocation::setUpInputForSILTool( } return fileBufOrErr; } + +bool CompilerInvocation::isModuleExternallyConsumed( + const ModuleDecl *mod) const { + // Modules for executables aren't expected to be consumed by other modules. + // This picks up all kinds of entrypoints, including script mode, + // @UIApplicationMain and @NSApplicationMain. + if (mod->hasEntryPoint()) { + return false; + } + + // If an implicit Objective-C header was needed to construct this module, it + // must be the product of a library target. + if (!getFrontendOptions().ImplicitObjCHeaderPath.empty()) { + return false; + } + + // App extensions are special beasts because they build without entrypoints + // like library targets, but they behave like executable targets because + // their associated modules are not suitable for distribution. + if (mod->getASTContext().LangOpts.EnableAppExtensionRestrictions) { + return false; + } + + // FIXME: This is still a lousy approximation of whether the module file will + // be externally consumed. + return true; +} diff --git a/lib/Frontend/Frontend.cpp b/lib/Frontend/Frontend.cpp index d1ec3481d4121..b162e549d05fe 100644 --- a/lib/Frontend/Frontend.cpp +++ b/lib/Frontend/Frontend.cpp @@ -152,7 +152,7 @@ CompilerInvocation::getPrivateModuleInterfaceOutputPathForWholeModule() const { } SerializationOptions CompilerInvocation::computeSerializationOptions( - const SupplementaryOutputPaths &outs, bool moduleIsPublic) const { + const SupplementaryOutputPaths &outs, const ModuleDecl *module) const { const FrontendOptions &opts = getFrontendOptions(); SerializationOptions serializationOpts; @@ -171,7 +171,8 @@ SerializationOptions CompilerInvocation::computeSerializationOptions( // so only serialize them if the module isn't going to be shipped to // the public. serializationOpts.SerializeOptionsForDebugging = - opts.SerializeOptionsForDebugging.getValueOr(!moduleIsPublic); + opts.SerializeOptionsForDebugging.getValueOr( + !isModuleExternallyConsumed(module)); return serializationOpts; } diff --git a/lib/FrontendTool/FrontendTool.cpp b/lib/FrontendTool/FrontendTool.cpp index 677af2ef1bab5..e78174844befa 100644 --- a/lib/FrontendTool/FrontendTool.cpp +++ b/lib/FrontendTool/FrontendTool.cpp @@ -1069,45 +1069,37 @@ static bool writeLdAddCFileIfNeeded(const CompilerInvocation &Invocation, return false; } -static bool performCompileStepsPostSILGen( - CompilerInstance &Instance, const CompilerInvocation &Invocation, - std::unique_ptr SM, bool astGuaranteedToCorrespondToSIL, - ModuleOrSourceFile MSF, const PrimarySpecificPaths &PSPs, - bool moduleIsPublic, int &ReturnValue, FrontendObserver *observer); - -static bool -performCompileStepsPostSema(const CompilerInvocation &Invocation, - CompilerInstance &Instance, - bool moduleIsPublic, int &ReturnValue, - FrontendObserver *observer) { +static bool performCompileStepsPostSILGen(CompilerInstance &Instance, + const CompilerInvocation &Invocation, + std::unique_ptr SM, + ModuleOrSourceFile MSF, + const PrimarySpecificPaths &PSPs, + int &ReturnValue, + FrontendObserver *observer); + +static bool performCompileStepsPostSema(const CompilerInvocation &Invocation, + CompilerInstance &Instance, + int &ReturnValue, + FrontendObserver *observer) { auto mod = Instance.getMainModule(); if (auto SM = Instance.takeSILModule()) { const PrimarySpecificPaths PSPs = Instance.getPrimarySpecificPathsForAtMostOnePrimary(); return performCompileStepsPostSILGen(Instance, Invocation, std::move(SM), - /*ASTGuaranteedToCorrespondToSIL=*/false, - mod, PSPs, moduleIsPublic, + mod, PSPs, ReturnValue, observer); } const SILOptions &SILOpts = Invocation.getSILOptions(); const FrontendOptions &opts = Invocation.getFrontendOptions(); - auto fileIsSIB = [](const FileUnit *File) -> bool { - auto SASTF = dyn_cast(File); - return SASTF && SASTF->isSIB(); - }; - if (!opts.InputsAndOutputs.hasPrimaryInputs()) { // If there are no primary inputs the compiler is in WMO mode and builds one // SILModule for the entire module. auto SM = performSILGeneration(mod, Instance.getSILTypes(), SILOpts); const PrimarySpecificPaths PSPs = Instance.getPrimarySpecificPathsForWholeModuleOptimizationMode(); - bool astGuaranteedToCorrespondToSIL = - llvm::none_of(mod->getFiles(), fileIsSIB); return performCompileStepsPostSILGen(Instance, Invocation, std::move(SM), - astGuaranteedToCorrespondToSIL, - mod, PSPs, moduleIsPublic, + mod, PSPs, ReturnValue, observer); } // If there are primary source files, build a separate SILModule for @@ -1120,8 +1112,7 @@ performCompileStepsPostSema(const CompilerInvocation &Invocation, const PrimarySpecificPaths PSPs = Instance.getPrimarySpecificPathsForSourceFile(*PrimaryFile); result |= performCompileStepsPostSILGen(Instance, Invocation, std::move(SM), - /*ASTGuaranteedToCorrespondToSIL*/true, - PrimaryFile, PSPs, moduleIsPublic, + PrimaryFile, PSPs, ReturnValue, observer); } @@ -1139,8 +1130,7 @@ performCompileStepsPostSema(const CompilerInvocation &Invocation, const PrimarySpecificPaths &PSPs = Instance.getPrimarySpecificPathsForPrimary(SASTF->getFilename()); result |= performCompileStepsPostSILGen(Instance, Invocation, std::move(SM), - !fileIsSIB(SASTF), - mod, PSPs, moduleIsPublic, + mod, PSPs, ReturnValue, observer); } } @@ -1167,8 +1157,7 @@ emitIndexData(const CompilerInvocation &Invocation, const CompilerInstance &Inst /// `-typecheck`, but skipped for any mode that runs SIL diagnostics if there's /// an error found there (to get those diagnostics back to the user faster). static bool emitAnyWholeModulePostTypeCheckSupplementaryOutputs( - CompilerInstance &Instance, const CompilerInvocation &Invocation, - bool moduleIsPublic) { + CompilerInstance &Instance, const CompilerInvocation &Invocation) { const FrontendOptions &opts = Invocation.getFrontendOptions(); // Record whether we failed to emit any of these outputs, but keep going; one @@ -1191,7 +1180,8 @@ static bool emitAnyWholeModulePostTypeCheckSupplementaryOutputs( } hadAnyError |= printAsObjCIfNeeded( Invocation.getObjCHeaderOutputPathForAtMostOnePrimary(), - Instance.getMainModule(), BridgingHeaderPathForPrint, moduleIsPublic); + Instance.getMainModule(), BridgingHeaderPathForPrint, + Invocation.isModuleExternallyConsumed(Instance.getMainModule())); } if (opts.InputsAndOutputs.hasModuleInterfaceOutputPath()) { @@ -1324,13 +1314,6 @@ static bool performCompile(CompilerInstance &Instance, (void)emitLoadedModuleTraceForAllPrimariesIfNeeded( Instance.getMainModule(), Instance.getDependencyTracker(), opts); - // FIXME: This is still a lousy approximation of whether the module file will - // be externally consumed. - bool moduleIsPublic = - !Instance.getMainModule()->hasEntryPoint() && - opts.ImplicitObjCHeaderPath.empty() && - !Context.LangOpts.EnableAppExtensionRestrictions; - // We've just been told to perform a typecheck, so we can return now. if (Action == FrontendOptions::ActionType::Typecheck) { if (emitIndexData(Invocation, Instance)) @@ -1345,8 +1328,7 @@ static bool performCompile(CompilerInstance &Instance, // guarding the emission of whole-module supplementary outputs. if (opts.InputsAndOutputs.isWholeModule()) { if (emitAnyWholeModulePostTypeCheckSupplementaryOutputs(Instance, - Invocation, - moduleIsPublic)) { + Invocation)) { return true; } } @@ -1356,8 +1338,8 @@ static bool performCompile(CompilerInstance &Instance, assert(FrontendOptions::doesActionGenerateSIL(Action) && "All actions not requiring SILGen must have been handled!"); - return performCompileStepsPostSema(Invocation, Instance, moduleIsPublic, - ReturnValue, observer); + return performCompileStepsPostSema(Invocation, Instance, ReturnValue, + observer); } static bool serializeSIB(SILModule *SM, const PrimarySpecificPaths &PSPs, @@ -1422,44 +1404,76 @@ static bool processCommandLineAndRunImmediately(const CompilerInvocation &Invoca static bool validateTBDIfNeeded(const CompilerInvocation &Invocation, ModuleOrSourceFile MSF, - bool astGuaranteedToCorrespondToSIL, const llvm::Module &IRModule) { - if (!astGuaranteedToCorrespondToSIL || - !inputFileKindCanHaveTBDValidated(Invocation.getInputKind())) - return false; - - if (Invocation.getSILOptions().CrossModuleOptimization) - return false; + const auto mode = Invocation.getFrontendOptions().ValidateTBDAgainstIR; + const bool canPerformTBDValidation = [&]() { + // If the user has requested we skip validation, honor it. + if (mode == FrontendOptions::TBDValidationMode::None) { + return false; + } - const auto &frontendOpts = Invocation.getFrontendOptions(); - auto mode = frontendOpts.ValidateTBDAgainstIR; - // Ensure all cases are covered by using a switch here. - switch (mode) { - case FrontendOptions::TBDValidationMode::Default: + // Cross-module optimization does not yet support TBD validation. + if (Invocation.getSILOptions().CrossModuleOptimization) { + return false; + } + + // If we can't validate the given input file, bail early. This covers cases + // like passing raw SIL as a primary file. + if (!inputFileKindCanHaveTBDValidated(Invocation.getInputKind())) { + return false; + } + + // Modules with SIB files cannot be validated. This is because SIB files + // may have serialized hand-crafted SIL definitions that are invisible to + // TBDGen as it is an AST-only traversal. + if (auto *mod = MSF.dyn_cast()) { + return llvm::none_of(mod->getFiles(), [](const FileUnit *File) -> bool { + auto SASTF = dyn_cast(File); + return SASTF && SASTF->isSIB(); + }); + } + + // "Default" mode's behavior varies if using a debug compiler. + if (mode == FrontendOptions::TBDValidationMode::Default) { #ifndef NDEBUG - // With a debug compiler, we do some validation by default. - mode = FrontendOptions::TBDValidationMode::MissingFromTBD; - break; + // With a debug compiler, we do some validation by default. + return true; #else - // Otherwise, the default is to do nothing. - LLVM_FALLTHROUGH; + // Otherwise, the default is to do nothing. + return false; #endif - case FrontendOptions::TBDValidationMode::None: + } + + + return true; + }(); + + if (!canPerformTBDValidation) { return false; - case FrontendOptions::TBDValidationMode::All: - case FrontendOptions::TBDValidationMode::MissingFromTBD: - break; } - const bool allSymbols = mode == FrontendOptions::TBDValidationMode::All; - // We should ignore embeded symbols from external modules for validation. + const bool diagnoseExtraSymbolsInTBD = [mode]() { + switch (mode) { + case FrontendOptions::TBDValidationMode::None: + llvm_unreachable("Handled Above!"); + case FrontendOptions::TBDValidationMode::Default: + case FrontendOptions::TBDValidationMode::MissingFromTBD: + return false; + case FrontendOptions::TBDValidationMode::All: + return true; + } + }(); + TBDGenOptions Opts = Invocation.getTBDGenOptions(); + // Ignore embedded symbols from external modules for validation to remove + // noise from e.g. statically-linked libraries. Opts.embedSymbolsFromModules.clear(); - return MSF.is() - ? validateTBD(MSF.get(), IRModule, - Opts, allSymbols) - : validateTBD(MSF.get(), IRModule, - Opts, allSymbols); + if (auto *SF = MSF.dyn_cast()) { + return validateTBD(SF, IRModule, Opts, diagnoseExtraSymbolsInTBD); + } else { + return validateTBD(MSF.get(), IRModule, Opts, + diagnoseExtraSymbolsInTBD); + } } enum class DeallocatableResources { @@ -1547,12 +1561,13 @@ static void collectLinkerDirectives(const CompilerInvocation &Invocation, enumeratePublicSymbols(MSF.get(), Symbols, tbdOpts); } -static bool performCompileStepsPostSILGen( - CompilerInstance &Instance, const CompilerInvocation &Invocation, - std::unique_ptr SM, bool astGuaranteedToCorrespondToSIL, - ModuleOrSourceFile MSF, const PrimarySpecificPaths &PSPs, - bool moduleIsPublic, int &ReturnValue, FrontendObserver *observer) { - +static bool performCompileStepsPostSILGen(CompilerInstance &Instance, + const CompilerInvocation &Invocation, + std::unique_ptr SM, + ModuleOrSourceFile MSF, + const PrimarySpecificPaths &PSPs, + int &ReturnValue, + FrontendObserver *observer) { FrontendOptions opts = Invocation.getFrontendOptions(); FrontendOptions::ActionType Action = opts.RequestedAction; const ASTContext &Context = Instance.getASTContext(); @@ -1593,7 +1608,7 @@ static bool performCompileStepsPostSILGen( return; SerializationOptions serializationOpts = - Invocation.computeSerializationOptions(outs, moduleIsPublic); + Invocation.computeSerializationOptions(outs, Instance.getMainModule()); serialize(MSF, serializationOpts, SM.get()); }; @@ -1608,8 +1623,7 @@ static bool performCompileStepsPostSILGen( if (observer) observer->performedSILProcessing(*SM); - emitAnyWholeModulePostTypeCheckSupplementaryOutputs(Instance, Invocation, - moduleIsPublic); + emitAnyWholeModulePostTypeCheckSupplementaryOutputs(Instance, Invocation); if (Action == FrontendOptions::ActionType::EmitSIB) return serializeSIB(SM.get(), PSPs, Context, MSF); @@ -1690,8 +1704,7 @@ static bool performCompileStepsPostSILGen( if (!IRModule) return HadError; - if (validateTBDIfNeeded(Invocation, MSF, astGuaranteedToCorrespondToSIL, - *IRModule.getModule())) + if (validateTBDIfNeeded(Invocation, MSF, *IRModule.getModule())) return true; return generateCode(Invocation, Instance, OutputFilename, diff --git a/lib/IDE/REPLCodeCompletion.cpp b/lib/IDE/REPLCodeCompletion.cpp index 0ee8a3a3a0c7d..79e49dcc7dca1 100644 --- a/lib/IDE/REPLCodeCompletion.cpp +++ b/lib/IDE/REPLCodeCompletion.cpp @@ -238,7 +238,8 @@ doCodeCompletion(SourceFile &SF, StringRef EnteredCode, unsigned *BufferID, newSF.addImports(importsWithOptions); } - performTypeChecking(newSF); + performImportResolution(newSF); + bindExtensions(newSF); performCodeCompletionSecondPass(newSF, *CompletionCallbacksFactory); diff --git a/lib/IRGen/SwiftTargetInfo.cpp b/lib/IRGen/SwiftTargetInfo.cpp index a5e435d433973..a7b6f84bf06c0 100644 --- a/lib/IRGen/SwiftTargetInfo.cpp +++ b/lib/IRGen/SwiftTargetInfo.cpp @@ -69,7 +69,7 @@ static void configureX86_64(IRGenModule &IGM, const llvm::Triple &triple, SWIFT_ABI_X86_64_SWIFT_SPARE_BITS_MASK); setToMask(target.IsObjCPointerBit, 64, SWIFT_ABI_X86_64_IS_OBJC_BIT); - if (triple.isSimulatorEnvironment()) { + if (tripleIsAnySimulator(triple)) { setToMask(target.ObjCPointerReservedBits, 64, SWIFT_ABI_X86_64_SIMULATOR_OBJC_RESERVED_BITS_MASK); } else { diff --git a/lib/SILOptimizer/Utils/Devirtualize.cpp b/lib/SILOptimizer/Utils/Devirtualize.cpp index 32d5c20e1fd52..cb5226de09012 100644 --- a/lib/SILOptimizer/Utils/Devirtualize.cpp +++ b/lib/SILOptimizer/Utils/Devirtualize.cpp @@ -1147,8 +1147,15 @@ swift::tryDevirtualizeApply(ApplySite applySite, ClassHierarchyAnalysis *cha, // Try to check if the exact dynamic type of the instance is statically // known. - if (auto instance = getInstanceWithExactDynamicType(cmi->getOperand(), cha)) - return tryDevirtualizeClassMethod(fas, instance, cd, ore); + if (auto instance = getInstanceWithExactDynamicType(cmi->getOperand(), cha)) { + // Update the classDecl, because we are stripping casts more aggressively + // in getInstanceWithExactDynamicType than in stripUpCasts. + CanType classType = getSelfInstanceType(instance->getType().getASTType()); + // This should never be null - make the check just to be on the safe side. + if (ClassDecl *cd = classType.getClassOrBoundGenericClass()) + return tryDevirtualizeClassMethod(fas, instance, cd, ore); + return {ApplySite(), false}; + } if (auto exactTy = getExactDynamicType(cmi->getOperand(), cha)) { if (exactTy == cmi->getOperand()->getType()) @@ -1207,8 +1214,11 @@ bool swift::canDevirtualizeApply(FullApplySite applySite, // Try to check if the exact dynamic type of the instance is statically // known. - if (auto instance = getInstanceWithExactDynamicType(cmi->getOperand(), cha)) - return canDevirtualizeClassMethod(applySite, cd); + if (auto instance = getInstanceWithExactDynamicType(cmi->getOperand(), cha)) { + CanType classType = getSelfInstanceType(instance->getType().getASTType()); + ClassDecl *cd = classType.getClassOrBoundGenericClass(); + return cd && canDevirtualizeClassMethod(applySite, cd); + } if (auto exactTy = getExactDynamicType(cmi->getOperand(), cha)) { if (exactTy == cmi->getOperand()->getType()) diff --git a/lib/Serialization/ModuleFile.cpp b/lib/Serialization/ModuleFile.cpp index 21e640548149f..0dbe8b3730a72 100644 --- a/lib/Serialization/ModuleFile.cpp +++ b/lib/Serialization/ModuleFile.cpp @@ -1341,9 +1341,6 @@ static bool areCompatibleArchitectures(const llvm::Triple &moduleTarget, static bool areCompatibleOSs(const llvm::Triple &moduleTarget, const llvm::Triple &ctxTarget) { - if (moduleTarget.getEnvironment() != ctxTarget.getEnvironment()) - return false; - if (moduleTarget.getOS() == ctxTarget.getOS()) return true; diff --git a/lib/Serialization/SerializedModuleLoader.cpp b/lib/Serialization/SerializedModuleLoader.cpp index 59e2d019faa1c..00ed32305ecfb 100644 --- a/lib/Serialization/SerializedModuleLoader.cpp +++ b/lib/Serialization/SerializedModuleLoader.cpp @@ -35,7 +35,7 @@ using swift::version::Version; namespace { /// Apply \c body for each target-specific module file base name to search from -/// most to least desiable. +/// most to least desirable. void forEachTargetModuleBasename(const ASTContext &Ctx, llvm::function_ref body) { auto normalizedTarget = getTargetSpecificModuleTriple(Ctx.LangOpts.Target); diff --git a/stdlib/cmake/modules/AddSwiftStdlib.cmake b/stdlib/cmake/modules/AddSwiftStdlib.cmake index 9fcfb341265ac..53ec587be0873 100644 --- a/stdlib/cmake/modules/AddSwiftStdlib.cmake +++ b/stdlib/cmake/modules/AddSwiftStdlib.cmake @@ -1402,6 +1402,9 @@ endfunction() # SWIFT_MODULE_DEPENDS_FREEBSD # Swift modules this library depends on when built for FreeBSD. # +# SWIFT_MODULE_DEPENDS_OPENBSD +# Swift modules this library depends on when built for OpenBSD. +# # SWIFT_MODULE_DEPENDS_LINUX # Swift modules this library depends on when built for Linux. # @@ -1524,6 +1527,7 @@ function(add_swift_target_library name) SWIFT_MODULE_DEPENDS SWIFT_MODULE_DEPENDS_CYGWIN SWIFT_MODULE_DEPENDS_FREEBSD + SWIFT_MODULE_DEPENDS_OPENBSD SWIFT_MODULE_DEPENDS_HAIKU SWIFT_MODULE_DEPENDS_IOS SWIFT_MODULE_DEPENDS_LINUX @@ -1683,6 +1687,9 @@ function(add_swift_target_library name) elseif(${sdk} STREQUAL FREEBSD) list(APPEND swiftlib_module_depends_flattened ${SWIFTLIB_SWIFT_MODULE_DEPENDS_FREEBSD}) + elseif(${sdk} STREQUAL OPENBSD) + list(APPEND swiftlib_module_depends_flattened + ${SWIFTLIB_SWIFT_MODULE_DEPENDS_OPENBSD}) elseif(${sdk} STREQUAL LINUX OR ${sdk} STREQUAL ANDROID) list(APPEND swiftlib_module_depends_flattened ${SWIFTLIB_SWIFT_MODULE_DEPENDS_LINUX}) diff --git a/stdlib/public/Differentiation/CMakeLists.txt b/stdlib/public/Differentiation/CMakeLists.txt index d1b64e5e498d4..cceb6fc6f2560 100644 --- a/stdlib/public/Differentiation/CMakeLists.txt +++ b/stdlib/public/Differentiation/CMakeLists.txt @@ -28,6 +28,7 @@ add_swift_target_library(swift_Differentiation ${SWIFT_STDLIB_LIBRARY_BUILD_TYPE SWIFT_MODULE_DEPENDS_WATCHOS Darwin SWIFT_MODULE_DEPENDS_LINUX Glibc SWIFT_MODULE_DEPENDS_FREEBSD Glibc + SWIFT_MODULE_DEPENDS_OPENBSD Glibc SWIFT_MODULE_DEPENDS_CYGWIN Glibc SWIFT_MODULE_DEPENDS_HAIKU Glibc SWIFT_MODULE_DEPENDS_WASI Glibc diff --git a/stdlib/public/Differentiation/TgmathDerivatives.swift.gyb b/stdlib/public/Differentiation/TgmathDerivatives.swift.gyb index d57700f93b0ff..df807256e15cf 100644 --- a/stdlib/public/Differentiation/TgmathDerivatives.swift.gyb +++ b/stdlib/public/Differentiation/TgmathDerivatives.swift.gyb @@ -16,7 +16,7 @@ import Swift #if os(macOS) || os(iOS) || os(tvOS) || os(watchOS) import Darwin.C.tgmath -#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku) || os(WASI) +#elseif os(Linux) || os(FreeBSD) || os(OpenBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku) || os(WASI) import Glibc #elseif os(Windows) import MSVCRT diff --git a/test/Driver/infer-simulator.swift b/test/Driver/infer-simulator.swift deleted file mode 100644 index 8cdf9f82725c8..0000000000000 --- a/test/Driver/infer-simulator.swift +++ /dev/null @@ -1,13 +0,0 @@ -// Test the inference of the "simulator" environment within the driver. - -// RUN: %swiftc_driver -driver-print-jobs -target x86_64-apple-ios11.0 %s > %t.simple.txt 2>&1 -// RUN: %FileCheck -check-prefix WARN_IOS_SIMULATOR %s < %t.simple.txt -// WARN_IOS_SIMULATOR: warning: inferring simulator environment for target 'x86_64-apple-ios11.0'; use '-target x86_64-apple-ios11.0-simulator' instead - -// RUN: %swiftc_driver -driver-print-jobs -target x86_64-apple-tvos10.0 %s > %t.simple.txt 2>&1 -// RUN: %FileCheck -check-prefix WARN_TVOS_SIMULATOR %s < %t.simple.txt -// WARN_TVOS_SIMULATOR: inferring simulator environment for target 'x86_64-apple-tvos10.0'; use '-target x86_64-apple-tvos10.0-simulator' instead - -// RUN: %swiftc_driver -driver-print-jobs -target i386-apple-watchos4.0 %s > %t.simple.txt 2>&1 -// RUN: %FileCheck -check-prefix WARN_WATCHOS_SIMULATOR %s < %t.simple.txt -// WARN_WATCHOS_SIMULATOR: warning: inferring simulator environment for target 'i386-apple-watchos4.0'; use '-target i386-apple-watchos4.0-simulator' instead diff --git a/test/Driver/linker-arclite.swift b/test/Driver/linker-arclite.swift index 1ffc63c46ebe3..c320f9b871de5 100644 --- a/test/Driver/linker-arclite.swift +++ b/test/Driver/linker-arclite.swift @@ -9,7 +9,7 @@ // CHECK-SAME: -o {{[^ ]+}} -// RUN: %swiftc_driver -driver-print-jobs -target x86_64-apple-ios8.0-simulator %S/../Inputs/empty.swift | %FileCheck -check-prefix IOS_ARCLITE %s +// RUN: %swiftc_driver -driver-print-jobs -target x86_64-apple-ios8.0 %S/../Inputs/empty.swift | %FileCheck -check-prefix IOS_ARCLITE %s // IOS_ARCLITE: bin/ld{{"? }} // IOS_ARCLITE: -force_load {{[^ ]+/lib/arc/libarclite_iphonesimulator.a}} @@ -18,8 +18,8 @@ // RUN: %swiftc_driver -driver-print-jobs -target x86_64-apple-macosx10.11 %S/../Inputs/empty.swift | %FileCheck -check-prefix NO_ARCLITE %s // RUN: %swiftc_driver -driver-print-jobs -target x86_64-apple-macosx10.10 %S/../Inputs/empty.swift | %FileCheck -check-prefix ANY_ARCLITE %s -// RUN: %swiftc_driver -driver-print-jobs -target x86_64-apple-ios9-simulator %S/../Inputs/empty.swift | %FileCheck -check-prefix NO_ARCLITE %s -// RUN: %swiftc_driver -driver-print-jobs -target x86_64-apple-ios8-simulator %S/../Inputs/empty.swift | %FileCheck -check-prefix ANY_ARCLITE %s +// RUN: %swiftc_driver -driver-print-jobs -target x86_64-apple-ios9 %S/../Inputs/empty.swift | %FileCheck -check-prefix NO_ARCLITE %s +// RUN: %swiftc_driver -driver-print-jobs -target x86_64-apple-ios8 %S/../Inputs/empty.swift | %FileCheck -check-prefix ANY_ARCLITE %s // RUN: %swiftc_driver -driver-print-jobs -target arm64-apple-tvos9 %S/../Inputs/empty.swift | %FileCheck -check-prefix NO_ARCLITE %s // RUN: %swiftc_driver -driver-print-jobs -target armv7k-apple-watchos2 %S/../Inputs/empty.swift | %FileCheck -check-prefix NO_ARCLITE %s diff --git a/test/Driver/linker.swift b/test/Driver/linker.swift index 1c47aa6f420cd..8e5017922f1d2 100644 --- a/test/Driver/linker.swift +++ b/test/Driver/linker.swift @@ -6,13 +6,13 @@ // RUN: not %swiftc_driver -driver-print-jobs -target x86_64-apple-macosx10.9 -static-stdlib %s 2>&1 | %FileCheck -check-prefix=SIMPLE_STATIC %s -// RUN: %swiftc_driver -driver-print-jobs -target x86_64-apple-ios7.1-simulator %s 2>&1 > %t.simple.txt +// RUN: %swiftc_driver -driver-print-jobs -target x86_64-apple-ios7.1 %s 2>&1 > %t.simple.txt // RUN: %FileCheck -check-prefix IOS_SIMPLE %s < %t.simple.txt -// RUN: %swiftc_driver -driver-print-jobs -target x86_64-apple-tvos9.0-simulator %s 2>&1 > %t.simple.txt +// RUN: %swiftc_driver -driver-print-jobs -target x86_64-apple-tvos9.0 %s 2>&1 > %t.simple.txt // RUN: %FileCheck -check-prefix tvOS_SIMPLE %s < %t.simple.txt -// RUN: %swiftc_driver -driver-print-jobs -target i386-apple-watchos2.0-simulator %s 2>&1 > %t.simple.txt +// RUN: %swiftc_driver -driver-print-jobs -target i386-apple-watchos2.0 %s 2>&1 > %t.simple.txt // RUN: %FileCheck -check-prefix watchOS_SIMPLE %s < %t.simple.txt // RUN: %swiftc_driver -driver-print-jobs -target x86_64-unknown-linux-gnu -Ffoo -Fsystem car -F cdr -framework bar -Lbaz -lboo -Xlinker -undefined %s 2>&1 > %t.linux.txt @@ -47,7 +47,7 @@ // RUN: %FileCheck %s < %t.complex.txt // RUN: %FileCheck -check-prefix COMPLEX %s < %t.complex.txt -// RUN: %swiftc_driver -driver-print-jobs -target x86_64-apple-ios7.1-simulator -Xlinker -rpath -Xlinker customrpath -L foo %s 2>&1 > %t.simple.txt +// RUN: %swiftc_driver -driver-print-jobs -target x86_64-apple-ios7.1 -Xlinker -rpath -Xlinker customrpath -L foo %s 2>&1 > %t.simple.txt // RUN: %FileCheck -check-prefix IOS-linker-order %s < %t.simple.txt // RUN: %swiftc_driver -driver-print-jobs -target armv7-unknown-linux-gnueabihf -Xlinker -rpath -Xlinker customrpath -L foo %s 2>&1 > %t.linux.txt diff --git a/test/Driver/print_target_info.swift b/test/Driver/print_target_info.swift index fdb636dcf1f5d..301473f984747 100644 --- a/test/Driver/print_target_info.swift +++ b/test/Driver/print_target_info.swift @@ -7,8 +7,6 @@ // RUN: %swift_driver -print-target-info -target x86_64-apple-macosx10.15 -target-variant x86_64-apple-ios13-macabi | %FileCheck -check-prefix CHECK-ZIPPERED %s // RUN: %target-swift-frontend -print-target-info -target x86_64-apple-macosx10.15 -target-variant x86_64-apple-ios13-macabi | %FileCheck -check-prefix CHECK-ZIPPERED %s -// RUN: %swift_driver -print-target-info -target x86_64-apple-ios12.0 | %FileCheck -check-prefix CHECK-IOS-SIM %s - // CHECK-IOS: "target": { // CHECK-IOS: "triple": "arm64-apple-ios12.0", // CHECK-IOS: "unversionedTriple": "arm64-apple-ios", @@ -52,11 +50,3 @@ // CHECK-ZIPPERED: "swiftRuntimeCompatibilityVersion": "5.1" // CHECK-ZIPPERED: "librariesRequireRPath": false // CHECK-ZIPPERED: } - -// CHECK-IOS-SIM: "target": { -// CHECK-IOS-SIM: "triple": "x86_64-apple-ios12.0-simulator", -// CHECK-IOS-SIM: "unversionedTriple": "x86_64-apple-ios-simulator", -// CHECK-IOS-SIM: "moduleTriple": "x86_64-apple-ios-simulator", -// CHECK-IOS-SIM: "swiftRuntimeCompatibilityVersion": "5.0", -// CHECK-IOS-SIM: "librariesRequireRPath": true -// CHECK-IOS-SIM: } diff --git a/test/Driver/profiling.swift b/test/Driver/profiling.swift index 678e2d4e6abdd..f70ec0fe63fae 100644 --- a/test/Driver/profiling.swift +++ b/test/Driver/profiling.swift @@ -1,20 +1,20 @@ // RUN: %swiftc_driver -driver-print-jobs -profile-generate -target x86_64-apple-macosx10.9 %s | %FileCheck -check-prefix=CHECK -check-prefix=OSX %s -// RUN: %swiftc_driver -driver-print-jobs -profile-generate -target x86_64-apple-ios7.1-simulator -resource-dir %S/Inputs/fake-resource-dir-old/lib/swift/ %s | %FileCheck -check-prefix=CHECK -check-prefix=IOS %s +// RUN: %swiftc_driver -driver-print-jobs -profile-generate -target x86_64-apple-ios7.1 -resource-dir %S/Inputs/fake-resource-dir-old/lib/swift/ %s | %FileCheck -check-prefix=CHECK -check-prefix=IOS %s // RUN: %swiftc_driver -driver-print-jobs -profile-generate -target arm64-apple-ios7.1 -resource-dir %S/Inputs/fake-resource-dir-old/lib/swift/ %s | %FileCheck -check-prefix=CHECK -check-prefix=IOS %s -// RUN: %swiftc_driver -driver-print-jobs -profile-generate -target x86_64-apple-ios7.1-simulator -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ %s | %FileCheck -check-prefix=CHECK -check-prefix=IOSSIM %s +// RUN: %swiftc_driver -driver-print-jobs -profile-generate -target x86_64-apple-ios7.1 -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ %s | %FileCheck -check-prefix=CHECK -check-prefix=IOSSIM %s // RUN: %swiftc_driver -driver-print-jobs -profile-generate -target arm64-apple-ios7.1 -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ %s | %FileCheck -check-prefix=CHECK -check-prefix=IOS %s -// RUN: %swiftc_driver -driver-print-jobs -profile-generate -target x86_64-apple-tvos9.0-simulator -resource-dir %S/Inputs/fake-resource-dir-old/lib/swift/ %s | %FileCheck -check-prefix=CHECK -check-prefix=tvOS %s +// RUN: %swiftc_driver -driver-print-jobs -profile-generate -target x86_64-apple-tvos9.0 -resource-dir %S/Inputs/fake-resource-dir-old/lib/swift/ %s | %FileCheck -check-prefix=CHECK -check-prefix=tvOS %s // RUN: %swiftc_driver -driver-print-jobs -profile-generate -target arm64-apple-tvos9.0 -resource-dir %S/Inputs/fake-resource-dir-old/lib/swift/ %s | %FileCheck -check-prefix=CHECK -check-prefix=tvOS %s -// RUN: %swiftc_driver -driver-print-jobs -profile-generate -target x86_64-apple-tvos9.0-simulator -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ %s | %FileCheck -check-prefix=CHECK -check-prefix=tvOS_SIM %s +// RUN: %swiftc_driver -driver-print-jobs -profile-generate -target x86_64-apple-tvos9.0 -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ %s | %FileCheck -check-prefix=CHECK -check-prefix=tvOS_SIM %s // RUN: %swiftc_driver -driver-print-jobs -profile-generate -target arm64-apple-tvos9.0 -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ %s | %FileCheck -check-prefix=CHECK -check-prefix=tvOS %s -// RUN: %swiftc_driver -driver-print-jobs -profile-generate -target i386-apple-watchos2.0-simulator -resource-dir %S/Inputs/fake-resource-dir-old/lib/swift/ %s | %FileCheck -check-prefix=CHECK -check-prefix=watchOS %s +// RUN: %swiftc_driver -driver-print-jobs -profile-generate -target i386-apple-watchos2.0 -resource-dir %S/Inputs/fake-resource-dir-old/lib/swift/ %s | %FileCheck -check-prefix=CHECK -check-prefix=watchOS %s // RUN: %swiftc_driver -driver-print-jobs -profile-generate -target armv7k-apple-watchos2.0 -resource-dir %S/Inputs/fake-resource-dir-old/lib/swift/ %s | %FileCheck -check-prefix=CHECK -check-prefix=watchOS %s -// RUN: %swiftc_driver -driver-print-jobs -profile-generate -target i386-apple-watchos2.0-simulator -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ %s | %FileCheck -check-prefix=CHECK -check-prefix=watchOS_SIM %s +// RUN: %swiftc_driver -driver-print-jobs -profile-generate -target i386-apple-watchos2.0 -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ %s | %FileCheck -check-prefix=CHECK -check-prefix=watchOS_SIM %s // RUN: %swiftc_driver -driver-print-jobs -profile-generate -target armv7k-apple-watchos2.0 -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ %s | %FileCheck -check-prefix=CHECK -check-prefix=watchOS %s // RUN: %swiftc_driver -driver-print-jobs -profile-generate -target x86_64-unknown-linux-gnu %s | %FileCheck -check-prefix=CHECK -check-prefix=LINUX %s diff --git a/test/Driver/sanitize_scudo.swift b/test/Driver/sanitize_scudo.swift index cbc3051be024e..6f4e66f2612b3 100644 --- a/test/Driver/sanitize_scudo.swift +++ b/test/Driver/sanitize_scudo.swift @@ -2,11 +2,11 @@ // RUN: not %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=scudo -target arm64-apple-ios7.1 %s 2>&1 | %FileCheck -check-prefix=SCUDO_IOS %s // RUN: not %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=scudo -target arm64-apple-tvos9.0 %s 2>&1 | %FileCheck -check-prefix=SCUDO_tvOS %s // RUN: not %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=scudo -target armv7k-apple-watchos2.0 %s 2>&1 | %FileCheck -check-prefix=SCUDO_watchOS %s -// RUN: not %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=scudo -target i386-apple-watchos2.0-simulator %s 2>&1 | %FileCheck -check-prefix=SCUDO_watchOS_SIM %s +// RUN: not %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=scudo -target i386-apple-watchos2.0 %s 2>&1 | %FileCheck -check-prefix=SCUDO_watchOS_SIM %s // RUN: not %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=scudo -target i386-apple-macosx10.9 %s 2>&1 | %FileCheck -check-prefix=SCUDO_OSX_32 %s -// RUN: not %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=scudo -target x86_64-apple-ios7.1-simulator %s 2>&1 | %FileCheck -check-prefix=SCUDO_IOSSIM %s +// RUN: not %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=scudo -target x86_64-apple-ios7.1 %s 2>&1 | %FileCheck -check-prefix=SCUDO_IOSSIM %s // RUN: not %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=scudo -target x86_64-apple-macosx10.9 %s 2>&1 | %FileCheck -check-prefix=SCUDO_OSX_64 %s -// RUN: not %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=scudo -target x86_64-apple-tvos9.0-simulator %s 2>&1 | %FileCheck -check-prefix=SCUDO_tvOS_SIM %s +// RUN: not %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=scudo -target x86_64-apple-tvos9.0 %s 2>&1 | %FileCheck -check-prefix=SCUDO_tvOS_SIM %s // RUN: not %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=scudo -target x86_64-unknown-windows-msvc %s 2>&1 | %FileCheck -check-prefix=SCUDO_WINDOWS %s // RUN: not %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=scudo,address -target x86_64-unknown-linux-gnu %s 2>&1 | %FileCheck -check-prefix=SCUDO_ASAN %s @@ -24,11 +24,11 @@ // SCUDO_LINUX-SAME: -fsanitize=scudo // SCUDO_OSX_32: unsupported option '-sanitize=scudo' for target 'i386-apple-macosx10.9' // SCUDO_OSX_64: unsupported option '-sanitize=scudo' for target 'x86_64-apple-macosx10.9' -// SCUDO_IOSSIM: unsupported option '-sanitize=scudo' for target 'x86_64-apple-ios7.1-simulator' +// SCUDO_IOSSIM: unsupported option '-sanitize=scudo' for target 'x86_64-apple-ios7.1' // SCUDO_IOS: unsupported option '-sanitize=scudo' for target 'arm64-apple-ios7.1' -// SCUDO_tvOS_SIM: unsupported option '-sanitize=scudo' for target 'x86_64-apple-tvos9.0-simulator' +// SCUDO_tvOS_SIM: unsupported option '-sanitize=scudo' for target 'x86_64-apple-tvos9.0' // SCUDO_tvOS: unsupported option '-sanitize=scudo' for target 'arm64-apple-tvos9.0' -// SCUDO_watchOS_SIM: unsupported option '-sanitize=scudo' for target 'i386-apple-watchos2.0-simulator' +// SCUDO_watchOS_SIM: unsupported option '-sanitize=scudo' for target 'i386-apple-watchos2.0' // SCUDO_watchOS: unsupported option '-sanitize=scudo' for target 'armv7k-apple-watchos2.0' // SCUDO_WINDOWS: unsupported option '-sanitize=scudo' for target 'x86_64-unknown-windows-msvc' diff --git a/test/Driver/sanitizers.swift b/test/Driver/sanitizers.swift index d4dc5cea6de30..54935b7f7772e 100644 --- a/test/Driver/sanitizers.swift +++ b/test/Driver/sanitizers.swift @@ -3,11 +3,11 @@ */ // RUN: %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=address -target x86_64-apple-macosx10.9 %s | %FileCheck -check-prefix=ASAN -check-prefix=ASAN_OSX %s // RUN: not %swiftc_driver -driver-print-jobs -sanitize=fuzzer -target x86_64-apple-macosx10.9 -resource-dir %S/Inputs/nonexistent-resource-dir %s 2>&1 | %FileCheck -check-prefix=FUZZER_NONEXISTENT %s -// RUN: %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=address -target x86_64-apple-ios7.1-simulator %s | %FileCheck -check-prefix=ASAN -check-prefix=ASAN_IOSSIM %s +// RUN: %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=address -target x86_64-apple-ios7.1 %s | %FileCheck -check-prefix=ASAN -check-prefix=ASAN_IOSSIM %s // RUN: %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=address -target arm64-apple-ios7.1 %s | %FileCheck -check-prefix=ASAN -check-prefix=ASAN_IOS %s -// RUN: %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=address -target x86_64-apple-tvos9.0-simulator %s | %FileCheck -check-prefix=ASAN -check-prefix=ASAN_tvOS_SIM %s +// RUN: %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=address -target x86_64-apple-tvos9.0 %s | %FileCheck -check-prefix=ASAN -check-prefix=ASAN_tvOS_SIM %s // RUN: %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=address -target arm64-apple-tvos9.0 %s | %FileCheck -check-prefix=ASAN -check-prefix=ASAN_tvOS %s -// RUN: %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=address -target i386-apple-watchos2.0-simulator %s | %FileCheck -check-prefix=ASAN -check-prefix=ASAN_watchOS_SIM %s +// RUN: %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=address -target i386-apple-watchos2.0 %s | %FileCheck -check-prefix=ASAN -check-prefix=ASAN_watchOS_SIM %s // RUN: %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=address -target armv7k-apple-watchos2.0 %s | %FileCheck -check-prefix=ASAN -check-prefix=ASAN_watchOS %s // RUN: %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=address -target x86_64-unknown-linux-gnu %s 2>&1 | %FileCheck -check-prefix=ASAN_LINUX %s // RUN: %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=address -target x86_64-unknown-windows-msvc %s 2>&1 | %FileCheck -check-prefix=ASAN_WINDOWS %s @@ -17,11 +17,11 @@ */ // RUN: %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=thread -target x86_64-apple-macosx10.9 %s | %FileCheck -check-prefix=TSAN -check-prefix=TSAN_OSX %s // RUN: not %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=thread -target x86-apple-macosx10.9 %s 2>&1 | %FileCheck -check-prefix=TSAN_OSX_32 %s -// RUN: not %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=thread -target x86_64-apple-ios7.1-simulator %s 2>&1 | %FileCheck -check-prefix=TSAN_IOSSIM %s +// RUN: not %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=thread -target x86_64-apple-ios7.1 %s 2>&1 | %FileCheck -check-prefix=TSAN_IOSSIM %s // RUN: not %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=thread -target arm64-apple-ios7.1 %s 2>&1 | %FileCheck -check-prefix=TSAN_IOS %s -// RUN: not %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=thread -target x86_64-apple-tvos9.0-simulator %s 2>&1 | %FileCheck -check-prefix=TSAN_tvOS_SIM %s +// RUN: not %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=thread -target x86_64-apple-tvos9.0 %s 2>&1 | %FileCheck -check-prefix=TSAN_tvOS_SIM %s // RUN: not %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=thread -target arm64-apple-tvos9.0 %s 2>&1 | %FileCheck -check-prefix=TSAN_tvOS %s -// RUN: not %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=thread -target i386-apple-watchos2.0-simulator %s 2>&1 | %FileCheck -check-prefix=TSAN_watchOS_SIM %s +// RUN: not %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=thread -target i386-apple-watchos2.0 %s 2>&1 | %FileCheck -check-prefix=TSAN_watchOS_SIM %s // RUN: not %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=thread -target armv7k-apple-watchos2.0 %s 2>&1 | %FileCheck -check-prefix=TSAN_watchOS %s // RUN: not %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=thread -target x86_64-unknown-windows-msvc %s 2>&1 | %FileCheck -check-prefix=TSAN_WINDOWS %s // RUN: %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=thread -target x86_64-unknown-linux-gnu %s 2>&1 | %FileCheck -check-prefix=TSAN_LINUX %s @@ -30,11 +30,11 @@ * Undefined Behavior Sanitizer Tests (ubsan) */ // RUN: %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=undefined -target x86_64-apple-macosx10.9 %s | %FileCheck -check-prefix=UBSAN -check-prefix=UBSAN_OSX %s -// RUN: %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=undefined -target x86_64-apple-ios7.1-simulator %s | %FileCheck -check-prefix=UBSAN -check-prefix=UBSAN_IOSSIM %s +// RUN: %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=undefined -target x86_64-apple-ios7.1 %s | %FileCheck -check-prefix=UBSAN -check-prefix=UBSAN_IOSSIM %s // RUN: %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=undefined -target arm64-apple-ios7.1 %s | %FileCheck -check-prefix=UBSAN -check-prefix=UBSAN_IOS %s -// RUN: %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=undefined -target x86_64-apple-tvos9.0-simulator %s | %FileCheck -check-prefix=UBSAN -check-prefix=UBSAN_tvOS_SIM %s +// RUN: %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=undefined -target x86_64-apple-tvos9.0 %s | %FileCheck -check-prefix=UBSAN -check-prefix=UBSAN_tvOS_SIM %s // RUN: %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=undefined -target arm64-apple-tvos9.0 %s | %FileCheck -check-prefix=UBSAN -check-prefix=UBSAN_tvOS %s -// RUN: %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=undefined -target i386-apple-watchos2.0-simulator %s | %FileCheck -check-prefix=UBSAN -check-prefix=UBSAN_watchOS_SIM %s +// RUN: %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=undefined -target i386-apple-watchos2.0 %s | %FileCheck -check-prefix=UBSAN -check-prefix=UBSAN_watchOS_SIM %s // RUN: %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=undefined -target armv7k-apple-watchos2.0 %s | %FileCheck -check-prefix=UBSAN -check-prefix=UBSAN_watchOS %s // RUN: %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=undefined -target x86_64-unknown-linux-gnu %s 2>&1 | %FileCheck -check-prefix=UBSAN_LINUX %s // RUN: %swiftc_driver -resource-dir %S/Inputs/fake-resource-dir/lib/swift/ -driver-print-jobs -sanitize=undefined -target x86_64-unknown-windows-msvc %s 2>&1 | %FileCheck -check-prefix=UBSAN_WINDOWS %s @@ -79,11 +79,11 @@ // TSAN_OSX: lib{{/|\\\\}}swift{{/|\\\\}}clang{{/|\\\\}}lib{{/|\\\\}}darwin{{/|\\\\}}libclang_rt.tsan_osx_dynamic.dylib // TSAN_OSX_32: unsupported option '-sanitize=thread' for target 'x86-apple-macosx10.9' -// TSAN_IOSSIM: unsupported option '-sanitize=thread' for target 'x86_64-apple-ios7.1-simulator' +// TSAN_IOSSIM: unsupported option '-sanitize=thread' for target 'x86_64-apple-ios7.1' // TSAN_IOS: unsupported option '-sanitize=thread' for target 'arm64-apple-ios7.1' -// TSAN_tvOS_SIM: unsupported option '-sanitize=thread' for target 'x86_64-apple-tvos9.0-simulator' +// TSAN_tvOS_SIM: unsupported option '-sanitize=thread' for target 'x86_64-apple-tvos9.0' // TSAN_tvOS: unsupported option '-sanitize=thread' for target 'arm64-apple-tvos9.0' -// TSAN_watchOS_SIM: unsupported option '-sanitize=thread' for target 'i386-apple-watchos2.0-simulator' +// TSAN_watchOS_SIM: unsupported option '-sanitize=thread' for target 'i386-apple-watchos2.0' // TSAN_watchOS: unsupported option '-sanitize=thread' for target 'armv7k-apple-watchos2.0' // FUZZER_NONEXISTENT: unsupported option '-sanitize=fuzzer' for target 'x86_64-apple-macosx10.9' // TSAN_LINUX: -fsanitize=thread -lBlocksRuntime -ldispatch diff --git a/test/Parse/ConditionalCompilation/simulatorTargetEnv.swift b/test/Parse/ConditionalCompilation/simulatorTargetEnv.swift index 232bc41fdd0b4..63e54ed24f7f9 100644 --- a/test/Parse/ConditionalCompilation/simulatorTargetEnv.swift +++ b/test/Parse/ConditionalCompilation/simulatorTargetEnv.swift @@ -1,6 +1,6 @@ -// RUN: %swift -swift-version 4 -typecheck %s -verify -target x86_64-apple-ios7.0-simulator -parse-stdlib +// RUN: %swift -swift-version 4 -typecheck %s -verify -target x86_64-apple-ios7.0 -parse-stdlib // RUN: %swift -swift-version 4 -typecheck %s -verify -target x86_64-unknown-linux-simulator -parse-stdlib -// RUN: %swift-ide-test -swift-version 4 -test-input-complete -source-filename=%s -target x86_64-apple-ios7.0-simulator +// RUN: %swift-ide-test -swift-version 4 -test-input-complete -source-filename=%s -target x86_64-apple-ios7.0 #if !targetEnvironment(simulator) // This block should not parse. diff --git a/test/SILOptimizer/devirtualize_class_method.swift b/test/SILOptimizer/devirtualize_class_method.swift new file mode 100644 index 0000000000000..97a819da0bc66 --- /dev/null +++ b/test/SILOptimizer/devirtualize_class_method.swift @@ -0,0 +1,37 @@ +// RUN: %empty-directory(%t) +// RUN: %target-build-swift -O -module-name=test %s -o %t/a.out +// RUN: %target-run %t/a.out | %FileCheck %s +// REQUIRES: executable_test + +class Base { + required init() { } + + class func instance() -> Base { + return self.init() + } +} + +class Middle: Base { + override class func instance() -> Middle { + return self.init() + } +} + +class Derived: Middle { + required init() { + super.init() + print("init Derived") + } +} + +struct Maker { + @inline(never) + static func create() -> Base { + return C.instance() + } +} + +// CHECK: init Derived +// CHECK: test.Derived +print(Maker.create()) + diff --git a/test/Serialization/load-target-normalization.swift b/test/Serialization/load-target-normalization.swift index e1120d75ac3ff..21040c0c2aab8 100644 --- a/test/Serialization/load-target-normalization.swift +++ b/test/Serialization/load-target-normalization.swift @@ -109,6 +109,15 @@ import ForeignModule // RUN: not %target-swift-frontend %s -typecheck -I %t -parse-stdlib -Xcc -arch -Xcc i386 -target i386-apple-tvos40-simulator 2>&1 | %FileCheck -DNORM=i386-apple-tvos-simulator %s // RUN: not %target-swift-frontend %s -typecheck -I %t -parse-stdlib -Xcc -arch -Xcc i386 -target i386-apple-watchos9.1.1-simulator 2>&1 | %FileCheck -DNORM=i386-apple-watchos-simulator %s +// simulator should be inferred when an Intel architecture is used with iOS, tvOS, or watchOS. + +// RUN: not %target-swift-frontend %s -typecheck -I %t -parse-stdlib -Xcc -arch -Xcc i386 -target x86_64-apple-ios40.0 2>&1 | %FileCheck -DNORM=x86_64-apple-ios-simulator %s +// RUN: not %target-swift-frontend %s -typecheck -I %t -parse-stdlib -Xcc -arch -Xcc i386 -target x86_64-apple-tvos40 2>&1 | %FileCheck -DNORM=x86_64-apple-tvos-simulator %s +// RUN: not %target-swift-frontend %s -typecheck -I %t -parse-stdlib -Xcc -arch -Xcc i386 -target x86_64-apple-watchos9.1.1 2>&1 | %FileCheck -DNORM=x86_64-apple-watchos-simulator %s +// RUN: not %target-swift-frontend %s -typecheck -I %t -parse-stdlib -Xcc -arch -Xcc i386 -target i386-apple-ios40.0 2>&1 | %FileCheck -DNORM=i386-apple-ios-simulator %s +// RUN: not %target-swift-frontend %s -typecheck -I %t -parse-stdlib -Xcc -arch -Xcc i386 -target i386-apple-tvos40 2>&1 | %FileCheck -DNORM=i386-apple-tvos-simulator %s +// RUN: not %target-swift-frontend %s -typecheck -I %t -parse-stdlib -Xcc -arch -Xcc i386 -target i386-apple-watchos9.1.1 2>&1 | %FileCheck -DNORM=i386-apple-watchos-simulator %s + // Other environments should be passed through. // RUN: not %target-swift-frontend %s -typecheck -I %t -parse-stdlib -Xcc -arch -Xcc i386 -target i386-apple-ios40.0-in_spaaaaaaace 2>&1 | %FileCheck -DNORM=i386-apple-ios-in_spaaaaaaace %s diff --git a/test/diagnostics/no_warnings_as_errors.swift b/test/diagnostics/no_warnings_as_errors.swift new file mode 100644 index 0000000000000..38dcfd8686b90 --- /dev/null +++ b/test/diagnostics/no_warnings_as_errors.swift @@ -0,0 +1,12 @@ +// RUN: %target-swift-frontend -typecheck -warnings-as-errors -no-warnings-as-errors %s 2>&1 | %FileCheck %s --check-prefix=CHECK-WARNING +// RUN: not %target-swift-frontend -typecheck -no-warnings-as-errors -warnings-as-errors %s 2>&1 | %FileCheck %s --check-prefix=CHECK-ERROR + + +// This test verifies that the -no-warnings-as-errors option nullifies the effect of the -warnings-as-errors option +// CHECK-WARNING-NOT: error: initialization of immutable value 'c' was never used; +// CHECK-WARNING: warning: initialization of immutable value 'c' was never used; +// CHECK-ERROR-NOT: warning: initialization of immutable value 'c' was never used; +// CHECK-ERROR: error: initialization of immutable value 'c' was never used; +func b() { + let c = 2 +} diff --git a/tools/swift-api-digester/swift-api-digester.cpp b/tools/swift-api-digester/swift-api-digester.cpp index 5ce91e36b96b2..bb31a5217d403 100644 --- a/tools/swift-api-digester/swift-api-digester.cpp +++ b/tools/swift-api-digester/swift-api-digester.cpp @@ -2676,13 +2676,13 @@ static std::string getDefaultBaselineDir(const char *Main) { llvm::sys::path::remove_filename(BaselineDir); // Remove /swift-api-digester llvm::sys::path::remove_filename(BaselineDir); // Remove /bin llvm::sys::path::append(BaselineDir, "lib", "swift", "FrameworkABIBaseline"); - return BaselineDir.str(); + return BaselineDir.str().str(); } static std::string getEmptyBaselinePath(const char *Main) { llvm::SmallString<128> BaselinePath(getDefaultBaselineDir(Main)); llvm::sys::path::append(BaselinePath, "nil.json"); - return BaselinePath.str(); + return BaselinePath.str().str(); } static StringRef getBaselineFilename(llvm::Triple Triple) { @@ -2710,13 +2710,13 @@ static std::string getDefaultBaselinePath(const char *Main, StringRef Module, // Look for ABI or API baseline llvm::sys::path::append(BaselinePath, ABI? "ABI": "API"); llvm::sys::path::append(BaselinePath, getBaselineFilename(Triple)); - return BaselinePath.str(); + return BaselinePath.str().str(); } static std::string getCustomBaselinePath(llvm::Triple Triple) { llvm::SmallString<128> BaselinePath(options::BaselineDirPath); llvm::sys::path::append(BaselinePath, getBaselineFilename(Triple)); - return BaselinePath.str(); + return BaselinePath.str().str(); } static SDKNodeRoot *getBaselineFromJson(const char *Main, SDKContext &Ctx) { From f3c7423b6c83d4de2818dde4b03798b82c5a6e2d Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Mon, 20 Apr 2020 21:06:07 +0100 Subject: [PATCH 173/838] Resolve master conflicts (#730) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * [build-script] Add the installation prefix to the toolchain path In several places, it was assumed to be /usr and hard-coded to that. This makes sure the installation prefix actually passed in is used. * [stdlib] StaticString: extension per conformance * Fix @available(macCatalyst N, iOS N+M) bug Previously, availability checking computed a declaration’s availability as the intersection of all @available attributes with active platforms. This meant that Swift would not allow you to use an API that was available earlier in a child platform than in its parent until it was also available in the parent platform. That was incorrect. This PR corrects availability checking to find the most specific @available attribute with an introduced version and use that instead. Fixes rdar://60892534. * [stdlib] Buildfix differentiation for OpenBSD. New files were added in #30875 which did not include os(OpenBSD), so add this. add_swift_target_library in AddSwiftStdlib subsequently required modification. _add_target_variant_link_flags likely needs modification as well, but this is better suited to a separate PR. * Handle multiple @available attributes correctly The previous commit broke a promise made by the implementation it replaced: if there were two or more @available(introduced:) attributes for the same platform, the greatest version wins. This commit restores that property while still completely overriding the parent platform’s availability with a child platform’s. * [Diagnostics] Provide anchors on demand instead of storing in `FailureDiagnostic` This decouples `FailureDiagnostic` from expression. * [Diagnostics] Switch `get{Raw}Anchor` to return `TypedNode` In preparation to anchor `ConstraintLocator` from `TypedNode` let's refactor diagnostics (which is the biggest user of locators) to support `TypedNode` instead of `Expr *`. * [Diagnostics] Switch `getConstraintLocator` variants to accept `TypedNode` * [Diagnostics] Switch `getContextualType*` to use `TypedNode` * [Diagnostics] Switch `getType` to use `TypedNode` * [Diagnostics] Resolve const-ness problems introduced by `TypedNode` Since `TypedNode` elements are all marked as `const` diagnostics need to get some of the APIs adjusted to support passing `const Expr *`. * Add compiler option to *disable* warnings-as-errors `-no-warnings-as-errors` This functionality is required for build systems to be able to overload/disable a given Swift project's preference of treating warnings as errors. Resolves rdar://problem/35699776 * [Diagnostics] Add helper functions to work with anchors - `{castTo, getAs, is}Expr` * [Diagnostics] Adjust `getCallInfo` to accept `TypedNode` instead of expression * Avoid unnecessary performTypeChecking() in REPL * [SIL Diagnostics] Improve diagnostics for capture of inout values within escaping autoclosures. * [NFC] Refactor validateTBDIfNeeded Inline the conditions for the astGuaranteedToCorrespondToSIL parameter and clean up the logic here to be more const-friendly. Also try to centralize and document the reasons we skip TBD validation. * Revert "[Darwin] Further restrict inference of the simulator environment" * [Gardening] Const-qualify CompilerInstance::getInvocation() * [NFC] Formalize isModuleExternallyConsumed Yank this parameter and document the historical scope creep of this value. * More StringRef -> std::string conversion fixes. This is needed for the master-next branch. * Devirtualizer: fix a miscompile due to handling of cast instructions. getInstanceWithExactDynamicType returns a new instance and for this the class decl has to be updated. https://bugs.swift.org/browse/SR-12538 rdar://problem/61911112 * [NFC] Fix typo in SerializedModuleLoader.cpp Co-authored-by: Butta Co-authored-by: Ben Rimmington Co-authored-by: Brent Royal-Gordon Co-authored-by: 3405691582 Co-authored-by: Pavel Yaskevich Co-authored-by: Artem Chikin Co-authored-by: Rintaro Ishizaki Co-authored-by: Ravi Kandhadai Co-authored-by: Robert Widmann Co-authored-by: Saleem Abdulrasool Co-authored-by: Mishal Shah Co-authored-by: Michael Forster Co-authored-by: swift-ci Co-authored-by: Erik Eckstein Co-authored-by: Ravi Kandhadai <37849400+ravikandhadai@users.noreply.github.com> Co-authored-by: Pavel Yaskevich --- include/swift/AST/DiagnosticsSIL.def | 6 +- include/swift/AST/Expr.h | 7 +- lib/AST/Availability.cpp | 42 +- .../DiagnoseInvalidEscapingCaptures.cpp | 23 +- lib/Sema/CSApply.cpp | 11 +- lib/Sema/CSDiagnostics.cpp | 677 +++++++++--------- lib/Sema/CSDiagnostics.h | 125 ++-- lib/Sema/ConstraintSystem.h | 6 +- stdlib/public/core/StaticString.swift | 42 +- .../OSLogCompilerDiagnosticsTest.swift | 6 +- .../invalid_escaping_captures.swift | 58 ++ test/attr/attr_availability_maccatalyst.swift | 17 + .../swift_build_support/products/pythonkit.py | 4 +- .../swift_build_support/products/swiftpm.py | 2 +- .../products/tensorflow.py | 4 +- .../swift_build_support/targets.py | 4 +- 16 files changed, 615 insertions(+), 419 deletions(-) diff --git a/include/swift/AST/DiagnosticsSIL.def b/include/swift/AST/DiagnosticsSIL.def index 39535c27c34ef..34ca3c7b08c86 100644 --- a/include/swift/AST/DiagnosticsSIL.def +++ b/include/swift/AST/DiagnosticsSIL.def @@ -108,7 +108,7 @@ ERROR(capture_before_declaration_defer,none, NOTE(captured_value_declared_here,none, "captured value declared here", ()) -#define SELECT_ESCAPING_CLOSURE_KIND "escaping %select{local function|closure}0" +#define SELECT_ESCAPING_CLOSURE_KIND "escaping %select{local function|closure|autoclosure}0" // Invalid escaping capture diagnostics. ERROR(escaping_inout_capture,none, @@ -133,6 +133,10 @@ ERROR(escaping_noescape_var_capture,none, NOTE(value_captured_here,none,"captured here", ()) +NOTE(copy_inout_captured_by_autoclosure,none, "pass a copy of %0", (Identifier)) + +NOTE(copy_self_captured_by_autoclosure,none, "pass a copy of 'self'", ()) + #undef SELECT_ESCAPING_CLOSURE_KIND NOTE(value_captured_transitively,none, diff --git a/include/swift/AST/Expr.h b/include/swift/AST/Expr.h index 90a11c96eff43..7d394ee03b65e 100644 --- a/include/swift/AST/Expr.h +++ b/include/swift/AST/Expr.h @@ -3759,7 +3759,12 @@ class ClosureExpr : public AbstractClosureExpr { assert(hasExplicitResultType() && "No explicit result type"); return ExplicitResultType; } - + + TypeRepr *getExplicitResultTypeRepr() const { + assert(hasExplicitResultType() && "No explicit result type"); + return ExplicitResultType.getTypeRepr(); + } + void setExplicitResultType(SourceLoc arrowLoc, TypeLoc resultType) { ArrowLoc = arrowLoc; ExplicitResultType = resultType; diff --git a/lib/AST/Availability.cpp b/lib/AST/Availability.cpp index 4512b15047735..7eacce69db72e 100644 --- a/lib/AST/Availability.cpp +++ b/lib/AST/Availability.cpp @@ -132,9 +132,29 @@ void AvailabilityInference::applyInferredAvailableAttrs( } } +/// Returns true if the introduced version in \p newAttr should be used instead +/// of the introduced version in \p prevAttr when both are attached to the same +/// declaration and refer to the active platform. +static bool isBetterThan(const AvailableAttr *newAttr, + const AvailableAttr *prevAttr) { + assert(newAttr); + + // If there is no prevAttr, newAttr of course wins. + if (!prevAttr) + return true; + + // If they belong to the same platform, the one that introduces later wins. + if (prevAttr->Platform == newAttr->Platform) + return prevAttr->Introduced.getValue() < newAttr->Introduced.getValue(); + + // If the new attribute's platform inherits from the old one, it wins. + return inheritsAvailabilityFromPlatform(newAttr->Platform, + prevAttr->Platform); +} + Optional AvailabilityInference::annotatedAvailableRange(const Decl *D, ASTContext &Ctx) { - Optional AnnotatedRange; + const AvailableAttr *bestAvailAttr = nullptr; for (auto Attr : D->getAttrs()) { auto *AvailAttr = dyn_cast(Attr); @@ -145,21 +165,15 @@ AvailabilityInference::annotatedAvailableRange(const Decl *D, ASTContext &Ctx) { continue; } - AvailabilityContext AttrRange{ - VersionRange::allGTE(AvailAttr->Introduced.getValue())}; - - // If we have multiple introduction versions, we will conservatively - // assume the worst case scenario. We may want to be more precise here - // in the future or emit a diagnostic. - - if (AnnotatedRange.hasValue()) { - AnnotatedRange.getValue().intersectWith(AttrRange); - } else { - AnnotatedRange = AttrRange; - } + if (isBetterThan(AvailAttr, bestAvailAttr)) + bestAvailAttr = AvailAttr; } - return AnnotatedRange; + if (!bestAvailAttr) + return None; + + return AvailabilityContext{ + VersionRange::allGTE(bestAvailAttr->Introduced.getValue())}; } AvailabilityContext AvailabilityInference::availableRange(const Decl *D, diff --git a/lib/SILOptimizer/Mandatory/DiagnoseInvalidEscapingCaptures.cpp b/lib/SILOptimizer/Mandatory/DiagnoseInvalidEscapingCaptures.cpp index 0166fc1c9f1ba..5a1fc3a58f14d 100644 --- a/lib/SILOptimizer/Mandatory/DiagnoseInvalidEscapingCaptures.cpp +++ b/lib/SILOptimizer/Mandatory/DiagnoseInvalidEscapingCaptures.cpp @@ -17,6 +17,7 @@ #include "swift/AST/ASTContext.h" #include "swift/AST/DiagnosticsSIL.h" +#include "swift/AST/Expr.h" #include "swift/AST/Types.h" #include "swift/SIL/ApplySite.h" #include "swift/SIL/InstructionUtils.h" @@ -332,17 +333,22 @@ static void checkPartialApply(ASTContext &Context, DeclContext *DC, // Should match SELECT_ESCAPING_CLOSURE_KIND in DiagnosticsSIL.def. enum { EscapingLocalFunction, - EscapingClosure + EscapingClosure, + EscapingAutoClosure, } functionKind = EscapingClosure; if (auto *F = PAI->getReferencedFunctionOrNull()) { if (auto loc = F->getLocation()) { - if (loc.isASTNode()) + if (loc.isASTNode()) { functionKind = EscapingLocalFunction; + } else if (loc.isASTNode()) { + functionKind = EscapingAutoClosure; + } } } // First, diagnose the inout captures, if any. for (auto inoutCapture : inoutCaptures) { + Optional paramName = None; if (isUseOfSelfInInitializer(inoutCapture)) { diagnose(Context, PAI->getLoc(), diag::escaping_mutable_self_capture, functionKind); @@ -352,14 +358,23 @@ static void checkPartialApply(ASTContext &Context, DeclContext *DC, diagnose(Context, PAI->getLoc(), diag::escaping_mutable_self_capture, functionKind); else { + paramName = param->getName(); diagnose(Context, PAI->getLoc(), diag::escaping_inout_capture, functionKind, param->getName()); diagnose(Context, param->getLoc(), diag::inout_param_defined_here, param->getName()); } } - - diagnoseCaptureLoc(Context, DC, PAI, inoutCapture); + if (functionKind != EscapingAutoClosure) { + diagnoseCaptureLoc(Context, DC, PAI, inoutCapture); + continue; + } + // For an autoclosure capture, present a way to fix the problem. + if (paramName) + diagnose(Context, PAI->getLoc(), diag::copy_inout_captured_by_autoclosure, + paramName.getValue()); + else + diagnose(Context, PAI->getLoc(), diag::copy_self_captured_by_autoclosure); } // Finally, diagnose captures of values with noescape type. diff --git a/lib/Sema/CSApply.cpp b/lib/Sema/CSApply.cpp index 9d13caa396e1f..4bfd9ef7b399d 100644 --- a/lib/Sema/CSApply.cpp +++ b/lib/Sema/CSApply.cpp @@ -166,10 +166,17 @@ ConstraintLocator *Solution::getCalleeLocator(ConstraintLocator *locator, } ConstraintLocator * -Solution::getConstraintLocator(Expr *anchor, +Solution::getConstraintLocator(const Expr *anchor, ArrayRef path) const { auto &cs = getConstraintSystem(); - return cs.getConstraintLocator(anchor, path); + return cs.getConstraintLocator(const_cast(anchor), path); +} + +ConstraintLocator * +Solution::getConstraintLocator(ConstraintLocator *base, + ArrayRef path) const { + auto &cs = getConstraintSystem(); + return cs.getConstraintLocator(base, path); } /// Return the implicit access kind for a MemberRefExpr with the diff --git a/lib/Sema/CSDiagnostics.cpp b/lib/Sema/CSDiagnostics.cpp index afdefb654c000..3916799e41405 100644 --- a/lib/Sema/CSDiagnostics.cpp +++ b/lib/Sema/CSDiagnostics.cpp @@ -34,6 +34,7 @@ #include "swift/Basic/SourceLoc.h" #include "swift/Parse/Lexer.h" #include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/PointerUnion.h" #include "llvm/ADT/SmallString.h" #include @@ -50,7 +51,7 @@ bool FailureDiagnostic::diagnoseAsNote() { return false; } -Expr *FailureDiagnostic::computeAnchor() const { +TypedNode FailureDiagnostic::getAnchor() const { auto &cs = getConstraintSystem(); auto *locator = getLocator(); @@ -71,12 +72,32 @@ Expr *FailureDiagnostic::computeAnchor() const { return anchor; } -Type FailureDiagnostic::getType(Expr *expr, bool wantRValue) const { - return resolveType(S.getType(expr), /*reconstituteSugar=*/false, wantRValue); +SourceLoc FailureDiagnostic::getLoc(TypedNode anchor) { + if (auto *E = anchor.dyn_cast()) { + return E->getLoc(); + } else if (auto *T = anchor.dyn_cast()) { + return T->getLoc(); + } else if (auto *V = anchor.dyn_cast()) { + return V->getNameLoc(); + } else { + return anchor.get()->getLoc(); + } +} + +SourceRange FailureDiagnostic::getSourceRange(TypedNode anchor) { + if (auto *E = anchor.dyn_cast()) { + return E->getSourceRange(); + } else if (auto *T = anchor.dyn_cast()) { + return T->getSourceRange(); + } else if (auto *V = anchor.dyn_cast()) { + return V->getSourceRange(); + } else { + return anchor.get()->getSourceRange(); + } } -Type FailureDiagnostic::getType(const TypeLoc &loc, bool wantRValue) const { - return resolveType(S.getType(&loc), /*reconstituteSugar=*/false, wantRValue); +Type FailureDiagnostic::getType(TypedNode node, bool wantRValue) const { + return resolveType(S.getType(node), /*reconstituteSugar=*/false, wantRValue); } template @@ -92,9 +113,9 @@ FailureDiagnostic::emitDiagnosticAt(ArgTypes &&... Args) const { return DE.diagnose(std::forward(Args)...); } -Expr *FailureDiagnostic::findParentExpr(Expr *subExpr) const { +Expr *FailureDiagnostic::findParentExpr(const Expr *subExpr) const { auto &cs = getConstraintSystem(); - return cs.getParentExpr(subExpr); + return cs.getParentExpr(const_cast(subExpr)); } Expr * @@ -107,12 +128,11 @@ FailureDiagnostic::getArgumentListExprFor(ConstraintLocator *locator) const { // Form a new locator that ends at the ApplyArgument element, then simplify // to get the argument list. auto newPath = ArrayRef(path.begin(), iter + 1); - auto &cs = getConstraintSystem(); - auto argListLoc = cs.getConstraintLocator(locator->getAnchor(), newPath); + auto argListLoc = getConstraintLocator(locator->getAnchor(), newPath); return simplifyLocatorToAnchor(argListLoc); } -Expr *FailureDiagnostic::getBaseExprFor(Expr *anchor) const { +Expr *FailureDiagnostic::getBaseExprFor(const Expr *anchor) const { if (!anchor) return nullptr; @@ -151,14 +171,14 @@ Type FailureDiagnostic::restoreGenericParameters( } Type RequirementFailure::getOwnerType() const { - auto *anchor = getRawAnchor(); + auto anchor = getRawAnchor(); // If diagnostic is anchored at assignment expression // it means that requirement failure happend while trying // to convert source to destination, which means that // owner type is actually not an assignment expression // itself but its source. - if (auto *assignment = dyn_cast(anchor)) + if (auto *assignment = getAsExpr(anchor)) anchor = assignment->getSrc(); return getType(anchor)->getInOutObjectType()->getMetatypeInstanceType(); @@ -225,8 +245,7 @@ ValueDecl *RequirementFailure::getDeclRef() const { }; if (isFromContextualType()) - return getAffectedDeclFromType( - getContextualType(getLocator()->getAnchor())); + return getAffectedDeclFromType(getContextualType(getRawAnchor())); if (auto overload = getCalleeOverloadChoiceIfAvailable(getLocator())) { // If there is a declaration associated with this @@ -399,7 +418,7 @@ void RequirementFailure::emitRequirementNote(const Decl *anchor, Type lhs, } bool MissingConformanceFailure::diagnoseAsError() { - auto *anchor = getAnchor(); + auto *anchor = getAnchor().get(); auto nonConformingType = getLHS(); auto protocolType = getRHS(); @@ -408,7 +427,7 @@ bool MissingConformanceFailure::diagnoseAsError() { // with it and if so skip conformance error, otherwise we'd // produce an unrelated ` doesn't conform to Equatable protocol` // diagnostic. - if (isPatternMatchingOperator(anchor)) { + if (isPatternMatchingOperator(const_cast(anchor))) { if (auto *binaryOp = dyn_cast_or_null(findParentExpr(anchor))) { auto *caseExpr = binaryOp->getArg()->getElement(0); @@ -501,8 +520,8 @@ bool MissingConformanceFailure::diagnoseTypeCannotConform( } bool MissingConformanceFailure::diagnoseAsAmbiguousOperatorRef() { - auto *anchor = getRawAnchor(); - auto *ODRE = dyn_cast(anchor); + auto anchor = getRawAnchor(); + auto *ODRE = getAsExpr(anchor); if (!ODRE) return false; @@ -605,7 +624,7 @@ void GenericArgumentsMismatchFailure::emitNoteForMismatch(int position) { } bool GenericArgumentsMismatchFailure::diagnoseAsError() { - auto *anchor = getAnchor(); + auto anchor = getAnchor(); auto path = getLocator()->getPath(); auto fromType = getFromType(); @@ -613,9 +632,9 @@ bool GenericArgumentsMismatchFailure::diagnoseAsError() { Optional> diagnostic; if (path.empty()) { - if (isa(anchor)) { + if (isExpr(anchor)) { diagnostic = getDiagnosticFor(CTP_AssignSource); - } else if (isa(anchor)) { + } else if (isExpr(anchor)) { diagnostic = getDiagnosticFor(CTP_CoerceOperand); } else { return false; @@ -629,7 +648,7 @@ bool GenericArgumentsMismatchFailure::diagnoseAsError() { // If this is call to a closure e.g. `let _: A = { B() }()` // let's point diagnostic to its result. - if (auto *call = dyn_cast(anchor)) { + if (auto *call = getAsExpr(anchor)) { auto *fnExpr = call->getFn(); if (auto *closure = dyn_cast(fnExpr)) { purpose = CTP_ClosureResult; @@ -667,7 +686,7 @@ bool GenericArgumentsMismatchFailure::diagnoseAsError() { case ConstraintLocator::OptionalPayload: { // If we have an inout expression, this comes from an // InoutToPointer argument mismatch failure. - if (isa(anchor)) { + if (isExpr(anchor)) { diagnostic = diag::cannot_convert_argument_value; auto applyInfo = getFunctionArgApplyInfo(getLocator()); if (applyInfo) @@ -677,11 +696,11 @@ bool GenericArgumentsMismatchFailure::diagnoseAsError() { } case ConstraintLocator::TupleElement: { - auto *anchor = getRawAnchor(); + auto rawAnchor = getRawAnchor(); - if (isa(anchor)) { + if (isExpr(rawAnchor)) { diagnostic = getDiagnosticFor(CTP_ArrayElement); - } else if (isa(anchor)) { + } else if (isExpr(rawAnchor)) { auto eltLoc = last.castTo(); diagnostic = getDiagnosticFor( eltLoc.getIndex() == 0 ? CTP_DictionaryKey : CTP_DictionaryValue); @@ -696,7 +715,7 @@ bool GenericArgumentsMismatchFailure::diagnoseAsError() { if (!diagnostic) { // Handle all mismatches involving an `AssignExpr` - if (auto *assignExpr = dyn_cast(anchor)) { + if (auto *assignExpr = getAsExpr(anchor)) { diagnostic = getDiagnosticFor(CTP_AssignSource); fromType = getType(assignExpr->getSrc()); toType = getType(assignExpr->getDest()); @@ -715,7 +734,7 @@ bool GenericArgumentsMismatchFailure::diagnoseAsError() { if (!diagnostic) return false; - emitDiagnosticAt(anchor->getLoc(), *diagnostic, fromType, toType); + emitDiagnosticAt(getLoc(anchor), *diagnostic, fromType, toType); emitNotesForMismatches(); return true; } @@ -725,10 +744,8 @@ bool LabelingFailure::diagnoseAsError() { if (!argExpr) return false; - auto &cs = getConstraintSystem(); - auto *anchor = getRawAnchor(); - return diagnoseArgumentLabelError(cs.getASTContext(), argExpr, CorrectLabels, - isa(anchor)); + return diagnoseArgumentLabelError(getASTContext(), argExpr, CorrectLabels, + isExpr(getRawAnchor())); } bool LabelingFailure::diagnoseAsNote() { @@ -792,11 +809,11 @@ bool NoEscapeFuncToTypeConversionFailure::diagnoseParameterUse() const { if (!convertTo->is()) return false; - auto *anchor = getAnchor(); + auto anchor = getAnchor(); auto diagnostic = diag::general_noescape_to_escaping; ParamDecl *PD = nullptr; - if (auto *DRE = dyn_cast(anchor)) { + if (auto *DRE = getAsExpr(anchor)) { PD = dyn_cast(DRE->getDecl()); // If anchor is not a parameter declaration there @@ -838,7 +855,7 @@ bool NoEscapeFuncToTypeConversionFailure::diagnoseParameterUse() const { // only mean that parameter is expecting @escaping function type. diagnostic = diag::passing_noescape_to_escaping; } - } else if (auto *AE = dyn_cast(getRawAnchor())) { + } else if (auto *AE = getAsExpr(getRawAnchor())) { if (auto *DRE = dyn_cast(AE->getSrc())) { PD = dyn_cast(DRE->getDecl()); diagnostic = diag::assigning_noescape_to_escaping; @@ -863,13 +880,11 @@ bool NoEscapeFuncToTypeConversionFailure::diagnoseParameterUse() const { return true; } -Expr *MissingForcedDowncastFailure::getAnchor() const { - auto *expr = FailureDiagnostic::getAnchor(); - - if (auto *assignExpr = dyn_cast(expr)) +TypedNode MissingForcedDowncastFailure::getAnchor() const { + auto anchor = FailureDiagnostic::getAnchor(); + if (auto *assignExpr = getAsExpr(anchor)) return assignExpr->getSrc(); - - return expr; + return anchor; } bool MissingForcedDowncastFailure::diagnoseAsError() { @@ -896,13 +911,13 @@ bool MissingAddressOfFailure::diagnoseAsError() { return true; } -Expr *MissingExplicitConversionFailure::getAnchor() const { - auto *anchor = FailureDiagnostic::getAnchor(); +TypedNode MissingExplicitConversionFailure::getAnchor() const { + auto anchor = FailureDiagnostic::getAnchor(); - if (auto *assign = dyn_cast(anchor)) + if (auto *assign = getAsExpr(anchor)) return assign->getSrc(); - if (auto *paren = dyn_cast(anchor)) + if (auto *paren = getAsExpr(anchor)) return paren->getSubExpr(); return anchor; @@ -910,7 +925,7 @@ Expr *MissingExplicitConversionFailure::getAnchor() const { bool MissingExplicitConversionFailure::diagnoseAsError() { auto *DC = getDC(); - auto *anchor = getAnchor(); + auto *anchor = getAnchor().get(); auto fromType = getFromType(); auto toType = getToType(); @@ -924,7 +939,7 @@ bool MissingExplicitConversionFailure::diagnoseAsError() { auto *expr = findParentExpr(anchor); if (!expr) - expr = anchor; + expr = const_cast(anchor); // If we're performing pattern matching, // "as" means something completely different... @@ -966,7 +981,7 @@ bool MissingExplicitConversionFailure::diagnoseAsError() { } bool MemberAccessOnOptionalBaseFailure::diagnoseAsError() { - auto *anchor = getAnchor(); + auto anchor = getAnchor(); auto baseType = getType(anchor); bool resultIsOptional = ResultTypeIsOptional; @@ -1002,10 +1017,10 @@ bool MemberAccessOnOptionalBaseFailure::diagnoseAsError() { } void MissingOptionalUnwrapFailure::offerDefaultValueUnwrapFixIt( - DeclContext *DC, Expr *expr) const { + DeclContext *DC, const Expr *expr) const { assert(expr); - auto *anchor = getAnchor(); + auto *anchor = getAnchor().dyn_cast(); // If anchor is n explicit address-of, or expression which produces // an l-value (e.g. first argument of `+=` operator), let's not // suggest default value here because that would produce r-value type. @@ -1020,10 +1035,10 @@ void MissingOptionalUnwrapFailure::offerDefaultValueUnwrapFixIt( // Figure out what we need to parenthesize. bool needsParensInside = - exprNeedsParensBeforeAddingNilCoalescing(DC, expr); + exprNeedsParensBeforeAddingNilCoalescing(DC, const_cast(expr)); auto parentExpr = findParentExpr(anchor); - bool needsParensOutside = - exprNeedsParensAfterAddingNilCoalescing(DC, expr, parentExpr); + bool needsParensOutside = exprNeedsParensAfterAddingNilCoalescing( + DC, const_cast(expr), parentExpr); llvm::SmallString<2> insertBefore; llvm::SmallString<32> insertAfter; @@ -1045,7 +1060,8 @@ void MissingOptionalUnwrapFailure::offerDefaultValueUnwrapFixIt( } // Suggest a force-unwrap. -void MissingOptionalUnwrapFailure::offerForceUnwrapFixIt(Expr *expr) const { +void MissingOptionalUnwrapFailure::offerForceUnwrapFixIt( + const Expr *expr) const { auto diag = emitDiagnosticAt(expr->getLoc(), diag::unwrap_with_force_value); // If expr is optional as the result of an optional chain and this last @@ -1093,7 +1109,7 @@ bool MissingOptionalUnwrapFailure::diagnoseAsError() { return true; } - auto *anchor = getAnchor(); + auto *anchor = getAnchor().get(); // If this is an unresolved member expr e.g. `.foo` its // base type is going to be the same as result type minus @@ -1201,7 +1217,7 @@ bool MissingOptionalUnwrapFailure::diagnoseAsError() { bool RValueTreatedAsLValueFailure::diagnoseAsError() { Diag subElementDiagID; Diag rvalueDiagID = diag::assignment_lhs_not_lvalue; - Expr *diagExpr = getRawAnchor(); + auto diagExpr = getRawAnchor().get(); SourceLoc loc = diagExpr->getLoc(); // Assignment is not allowed inside of a condition, @@ -1402,13 +1418,14 @@ bool TypeChecker::diagnoseSelfAssignment(const Expr *expr) { } bool TrailingClosureAmbiguityFailure::diagnoseAsNote() { - const auto *expr = findParentExpr(getAnchor()); + auto *anchor = getAnchor().get(); + const auto *expr = findParentExpr(anchor); auto *callExpr = dyn_cast_or_null(expr); if (!callExpr) return false; if (!callExpr->hasTrailingClosure()) return false; - if (callExpr->getFn() != getAnchor()) + if (callExpr->getFn() != anchor) return false; llvm::SmallMapVector choicesByLabel; @@ -1454,7 +1471,8 @@ bool TrailingClosureAmbiguityFailure::diagnoseAsNote() { return true; } -AssignmentFailure::AssignmentFailure(Expr *destExpr, const Solution &solution, +AssignmentFailure::AssignmentFailure(const Expr *destExpr, + const Solution &solution, SourceLoc diagnosticLoc) : FailureDiagnostic(solution, destExpr), DestExpr(destExpr), Loc(diagnosticLoc), @@ -1701,7 +1719,6 @@ bool AssignmentFailure::diagnoseAsError() { std::pair> AssignmentFailure::resolveImmutableBase(Expr *expr) const { - auto &cs = getConstraintSystem(); auto *DC = getDC(); expr = expr->getValueProvidingExpr(); @@ -1731,7 +1748,7 @@ AssignmentFailure::resolveImmutableBase(Expr *expr) const { } Optional member = getMemberRef( - cs.getConstraintLocator(SE, ConstraintLocator::SubscriptMember)); + getConstraintLocator(SE, ConstraintLocator::SubscriptMember)); // If it isn't settable, return it. if (member) { @@ -1766,7 +1783,7 @@ AssignmentFailure::resolveImmutableBase(Expr *expr) const { // Look through property references. if (auto *UDE = dyn_cast(expr)) { // If we found a decl for the UDE, check it. - auto loc = cs.getConstraintLocator(UDE, ConstraintLocator::Member); + auto loc = getConstraintLocator(UDE, ConstraintLocator::Member); auto member = getMemberRef(loc); @@ -1832,7 +1849,6 @@ AssignmentFailure::getMemberRef(ConstraintLocator *locator) const { // If this is a keypath dynamic member lookup, we have to // adjust the locator to find member referred by it. if (isValidKeyPathDynamicMemberLookup(subscript)) { - auto &cs = getConstraintSystem(); // Type has a following format: // `(Self) -> (dynamicMember: {Writable}KeyPath) -> U` auto *fullType = member->openedFullType->castTo(); @@ -1840,7 +1856,7 @@ AssignmentFailure::getMemberRef(ConstraintLocator *locator) const { auto paramTy = fnType->getParams()[0].getPlainType(); auto keyPath = paramTy->getAnyNominal(); - auto memberLoc = cs.getConstraintLocator( + auto memberLoc = getConstraintLocator( locator, LocatorPathElt::KeyPathDynamicMember(keyPath)); auto memberRef = getOverloadChoiceIfAvailable(memberLoc); @@ -1855,7 +1871,7 @@ AssignmentFailure::getMemberRef(ConstraintLocator *locator) const { } Diag AssignmentFailure::findDeclDiagonstic(ASTContext &ctx, - Expr *destExpr) { + const Expr *destExpr) { if (isa(destExpr) || isa(destExpr)) return diag::assignment_lhs_is_apply_expression; @@ -1879,7 +1895,7 @@ Diag AssignmentFailure::findDeclDiagonstic(ASTContext &ctx, } bool ContextualFailure::diagnoseAsError() { - auto *anchor = getAnchor(); + auto anchor = getAnchor(); auto path = getLocator()->getPath(); if (CTP == CTP_ReturnSingleExpr || CTP == CTP_ReturnStmt) { @@ -1895,23 +1911,23 @@ bool ContextualFailure::diagnoseAsError() { return true; if (path.empty()) { - if (auto *KPE = dyn_cast(anchor)) { + if (auto *KPE = getAsExpr(anchor)) { emitDiagnosticAt(KPE->getLoc(), diag::expr_smart_keypath_value_covert_to_contextual_type, getFromType(), getToType()); return true; } - + if (diagnoseCoercionToUnrelatedType()) return true; - if (isa(anchor)) { + if (isExpr(anchor)) { emitDiagnostic(diag::cannot_convert_initializer_value, getFromType(), getToType()); return true; } - if (isa(anchor)) { + if (isExpr(anchor)) { emitDiagnostic(diag::cannot_convert_assign, getFromType(), getToType()); return true; } @@ -1938,10 +1954,10 @@ bool ContextualFailure::diagnoseAsError() { switch (path.back().getKind()) { case ConstraintLocator::ClosureBody: case ConstraintLocator::ClosureResult: { - auto *closure = cast(getRawAnchor()); + auto *closure = castToExpr(getRawAnchor()); if (closure->hasExplicitResultType() && - closure->getExplicitResultTypeLoc().getTypeRepr()) { - auto resultRepr = closure->getExplicitResultTypeLoc().getTypeRepr(); + closure->getExplicitResultTypeRepr()) { + auto resultRepr = closure->getExplicitResultTypeRepr(); emitDiagnosticAt(resultRepr->getStartLoc(), diag::incorrect_explicit_closure_result, fromType, toType) @@ -1970,7 +1986,7 @@ bool ContextualFailure::diagnoseAsError() { } case ConstraintLocator::TernaryBranch: { - auto *ifExpr = cast(getRawAnchor()); + auto *ifExpr = castToExpr(getRawAnchor()); fromType = getType(ifExpr->getThenExpr()); toType = getType(ifExpr->getElseExpr()); diagnostic = diag::if_expr_cases_mismatch; @@ -1987,13 +2003,13 @@ bool ContextualFailure::diagnoseAsError() { if (diagnoseYieldByReferenceMismatch()) return true; - if (isa(anchor) || isa(anchor)) { + if (isExpr(anchor) || + isExpr(anchor)) { auto objectType = fromType->getOptionalObjectType(); if (objectType->isEqual(toType)) { - auto &cs = getConstraintSystem(); MissingOptionalUnwrapFailure failure(getSolution(), getType(anchor), toType, - cs.getConstraintLocator(anchor)); + getConstraintLocator(anchor)); if (failure.diagnoseAsError()) return true; } @@ -2013,7 +2029,7 @@ bool ContextualFailure::diagnoseAsError() { return true; } - if (auto *call = dyn_cast(anchor)) { + if (auto *call = getAsExpr(anchor)) { if (isa(call->getFn())) CTP = CTP_ClosureResult; } @@ -2149,9 +2165,9 @@ getContextualNilDiagnostic(ContextualTypePurpose CTP) { } bool ContextualFailure::diagnoseConversionToNil() const { - auto *anchor = getAnchor(); + auto anchor = getAnchor(); - if (!isa(anchor)) + if (!isExpr(anchor)) return false; auto *locator = getLocator(); @@ -2165,7 +2181,7 @@ bool ContextualFailure::diagnoseConversionToNil() const { // It could be e.g. an argument to a subscript/call, assignment // source like `s[0] = nil` or an array element like `[nil]` or // `[nil: 42]` as a sub-expression to a larger one. - auto *parentExpr = findParentExpr(anchor); + auto *parentExpr = findParentExpr(anchor.dyn_cast()); // Looks like it's something similar to `let _ = nil`. if (!parentExpr) { @@ -2198,8 +2214,9 @@ bool ContextualFailure::diagnoseConversionToNil() const { // out whether nil is a "key" or a "value". if (isa(enclosingExpr)) { assert(TE->getNumElements() == 2); - CTP = TE->getElement(0) == anchor ? CTP_DictionaryKey - : CTP_DictionaryValue; + CTP = TE->getElement(0) == anchor.get() + ? CTP_DictionaryKey + : CTP_DictionaryValue; } else { // Can't initialize one of the tuple elements with `nil`. CTP = CTP_Initialization; @@ -2321,11 +2338,11 @@ bool ContextualFailure::diagnoseMissingFunctionCall() const { } bool ContextualFailure::diagnoseCoercionToUnrelatedType() const { - auto *anchor = getAnchor(); - - if (auto *coerceExpr = dyn_cast(anchor)) { + auto anchor = getAnchor(); + + if (auto *coerceExpr = getAsExpr(anchor)) { auto fromType = getType(coerceExpr->getSubExpr()); - auto toType = getType(coerceExpr->getCastTypeLoc()); + auto toType = getType(&coerceExpr->getCastTypeLoc()); auto diagnostic = getDiagnosticFor(CTP_CoerceOperand, toType); @@ -2345,9 +2362,9 @@ bool ContextualFailure::diagnoseConversionToBool() const { if (!toType->isBool()) return false; - const auto *expr = getAnchor(); + auto *anchor = getAnchor().get(); // Check for "=" converting to Bool. The user probably meant ==. - if (auto *AE = dyn_cast(expr->getValueProvidingExpr())) { + if (auto *AE = dyn_cast(anchor->getValueProvidingExpr())) { emitDiagnosticAt(AE->getEqualLoc(), diag::use_of_equal_instead_of_equality) .fixItReplace(AE->getEqualLoc(), "==") .highlight(AE->getDest()->getLoc()) @@ -2360,7 +2377,7 @@ bool ContextualFailure::diagnoseConversionToBool() const { // diagnostics if someone attempts to use an optional or integer as a boolean // condition. SourceLoc notOperatorLoc; - if (Expr *parent = findParentExpr(getAnchor())) { + if (Expr *parent = findParentExpr(anchor)) { if (isa(parent) && parent->isImplicit()) { if ((parent = findParentExpr(parent))) { auto parentOperatorApplication = dyn_cast(parent); @@ -2391,7 +2408,7 @@ bool ContextualFailure::diagnoseConversionToBool() const { // Technically we only need them if there's something in 'expr' with // lower precedence than '!=', but the code actually comes out nicer // in most cases with parens on anything non-trivial. - if (expr->canAppendPostfixExpression()) { + if (anchor->canAppendPostfixExpression()) { prefix = prefix.drop_back(); suffix = suffix.drop_front(); } @@ -2422,7 +2439,7 @@ bool ContextualFailure::diagnoseConversionToBool() const { // Technically we only need them if there's something in 'expr' with // lower precedence than '!=', but the code actually comes out nicer // in most cases with parens on anything non-trivial. - if (expr->canAppendPostfixExpression()) { + if (anchor->canAppendPostfixExpression()) { prefix = prefix.drop_back(); suffix = suffix.drop_front(); } @@ -2446,7 +2463,7 @@ bool ContextualFailure::diagnoseThrowsTypeMismatch() const { if (CTP != CTP_ThrowStmt) return false; - auto *anchor = getAnchor(); + auto anchor = getAnchor(); // If we tried to throw the error code of an error type, suggest object // construction. @@ -2465,7 +2482,7 @@ bool ContextualFailure::diagnoseThrowsTypeMismatch() const { if (errorType) { auto diagnostic = emitDiagnostic(diag::cannot_throw_error_code, errorCodeType, errorType); - if (auto *UDE = dyn_cast(anchor)) { + if (auto *UDE = getAsExpr(anchor)) { diagnostic.fixItInsert(UDE->getDotLoc(), "("); diagnostic.fixItInsertAfter(UDE->getEndLoc(), ")"); } @@ -2486,7 +2503,7 @@ bool ContextualFailure::diagnoseYieldByReferenceMismatch() const { if (CTP != CTP_YieldByReference) return false; - auto *anchor = getAnchor(); + auto anchor = getAnchor(); auto exprType = getType(anchor, /*wantRValue=*/false); auto contextualType = getToType(); @@ -2506,7 +2523,7 @@ bool ContextualFailure::tryRawRepresentableFixIts( InFlightDiagnostic &diagnostic, KnownProtocolKind rawRepresentableProtocol) const { auto &CS = getConstraintSystem(); - auto *expr = getAnchor(); + auto anchor = getAnchor(); auto fromType = getFromType(); auto toType = getToType(); @@ -2519,7 +2536,8 @@ bool ContextualFailure::tryRawRepresentableFixIts( if (fromTypeIsOptional) fromType = fromTypeUnwrapped; - auto fixIt = [&](StringRef convWrapBefore, StringRef convWrapAfter) { + auto fixIt = [&](StringRef convWrapBefore, StringRef convWrapAfter, + const Expr *expr) { SourceRange exprRange = expr->getSourceRange(); if (fromTypeIsOptional && toTypeIsOptional) { // Use optional's map function to convert conditionally, like so: @@ -2561,9 +2579,9 @@ bool ContextualFailure::tryRawRepresentableFixIts( if (conformsToKnownProtocol(CS, fromType, rawRepresentableProtocol)) { if (conformsToKnownProtocol(CS, fromType, KnownProtocolKind::OptionSet) && - isa(expr) && - cast(expr)->getDigitsText() == "0") { - diagnostic.fixItReplace(expr->getSourceRange(), "[]"); + isExpr(anchor) && + castToExpr(anchor)->getDigitsText() == "0") { + diagnostic.fixItReplace(getSourceRange(anchor), "[]"); return true; } if (auto rawTy = isRawRepresentable(CS, toType, rawRepresentableProtocol)) { @@ -2572,7 +2590,7 @@ bool ContextualFailure::tryRawRepresentableFixIts( std::string convWrapBefore = toType.getString(); convWrapBefore += "(rawValue: "; std::string convWrapAfter = ")"; - if (!isa(expr) && + if (!isExpr(anchor) && !TypeChecker::isConvertibleTo(fromType, rawTy, getDC())) { // Only try to insert a converting construction if the protocol is a // literal protocol and not some other known protocol. @@ -2589,7 +2607,9 @@ bool ContextualFailure::tryRawRepresentableFixIts( convWrapBefore += "("; convWrapAfter += ")"; } - fixIt(convWrapBefore, convWrapAfter); + + if (auto *E = anchor.dyn_cast()) + fixIt(convWrapBefore, convWrapAfter, E); return true; } } @@ -2614,7 +2634,9 @@ bool ContextualFailure::tryRawRepresentableFixIts( convWrapBefore += "("; convWrapAfter += ")"; } - fixIt(convWrapBefore, convWrapAfter); + + if (auto *E = anchor.dyn_cast()) + fixIt(convWrapBefore, convWrapAfter, E); return true; } } @@ -2630,7 +2652,7 @@ bool ContextualFailure::tryIntegerCastFixIts( if (!isIntegerType(fromType) || !isIntegerType(toType)) return false; - auto getInnerCastedExpr = [&](Expr *expr) -> Expr * { + auto getInnerCastedExpr = [&](const Expr *expr) -> Expr * { if (auto *CE = dyn_cast(expr)) return CE->getSubExpr(); @@ -2645,14 +2667,15 @@ bool ContextualFailure::tryIntegerCastFixIts( return parenE->getSubExpr(); }; - auto *anchor = getAnchor(); - if (Expr *innerE = getInnerCastedExpr(anchor)) { - Type innerTy = getType(innerE); - if (TypeChecker::isConvertibleTo(innerTy, toType, getDC())) { - // Remove the unnecessary cast. - diagnostic.fixItRemoveChars(getLoc(), innerE->getStartLoc()) - .fixItRemove(getSourceRange().End); - return true; + if (auto *anchor = getAnchor().dyn_cast()) { + if (Expr *innerE = getInnerCastedExpr(anchor)) { + Type innerTy = getType(innerE); + if (TypeChecker::isConvertibleTo(innerTy, toType, getDC())) { + // Remove the unnecessary cast. + diagnostic.fixItRemoveChars(getLoc(), innerE->getStartLoc()) + .fixItRemove(getSourceRange().End); + return true; + } } } @@ -2681,7 +2704,8 @@ bool ContextualFailure::trySequenceSubsequenceFixIts( // Wrap in String.init if (getFromType()->isEqual(Substring)) { if (getToType()->isEqual(String)) { - auto *anchor = getAnchor()->getSemanticsProvidingExpr(); + auto anchor = + getAnchor().get()->getSemanticsProvidingExpr(); if (auto *CE = dyn_cast(anchor)) { anchor = CE->getSubExpr(); } @@ -2824,7 +2848,7 @@ bool ContextualFailure::tryProtocolConformanceFixIt( } void ContextualFailure::tryComputedPropertyFixIts() const { - if (!isa(getAnchor())) + if (!isExpr(getAnchor())) return; // It is possible that we're looking at a stored property being @@ -2985,15 +3009,10 @@ bool FunctionTypeMismatch::diagnoseAsError() { } bool AutoClosureForwardingFailure::diagnoseAsError() { - auto *loc = getLocator(); - auto last = loc->castLastElementTo(); - - // We need a raw anchor here because `getAnchor()` is simplified - // to the argument expression. - auto *argExpr = getArgumentExpr(getRawAnchor(), last.getArgIdx()); - emitDiagnosticAt(argExpr->getLoc(), diag::invalid_autoclosure_forwarding) - .highlight(argExpr->getSourceRange()) - .fixItInsertAfter(argExpr->getEndLoc(), "()"); + auto argRange = getSourceRange(); + emitDiagnostic(diag::invalid_autoclosure_forwarding) + .highlight(argRange) + .fixItInsertAfter(argRange.End, "()"); return true; } @@ -3005,10 +3024,10 @@ bool AutoClosurePointerConversionFailure::diagnoseAsError() { } bool NonOptionalUnwrapFailure::diagnoseAsError() { - auto *anchor = getAnchor(); + auto anchor = getAnchor(); auto diagnostic = diag::invalid_optional_chain; - if (isa(anchor)) + if (isExpr(anchor)) diagnostic = diag::invalid_force_unwrap; auto range = getSourceRange(); @@ -3016,22 +3035,24 @@ bool NonOptionalUnwrapFailure::diagnoseAsError() { return true; } -Expr *MissingCallFailure::getAnchor() const { - auto *anchor = FailureDiagnostic::getAnchor(); - if (auto *FVE = dyn_cast(anchor)) +TypedNode MissingCallFailure::getAnchor() const { + auto anchor = FailureDiagnostic::getAnchor(); + + if (auto *FVE = getAsExpr(anchor)) return FVE->getSubExpr(); + return anchor; } bool MissingCallFailure::diagnoseAsError() { - auto *baseExpr = getAnchor(); - SourceLoc insertLoc = baseExpr->getEndLoc(); + auto anchor = getAnchor(); + SourceLoc insertLoc = getSourceRange(anchor).End; // Calls are not yet supported by key path, but it // is useful to record this fix to diagnose chaining // where one of the key path components is a method // reference. - if (isa(baseExpr)) + if (isExpr(anchor)) return false; auto path = getLocator()->getPath(); @@ -3041,7 +3062,7 @@ bool MissingCallFailure::diagnoseAsError() { switch (last.getKind()) { case ConstraintLocator::ContextualType: case ConstraintLocator::ApplyArgToParam: { - auto fnType = getType(baseExpr)->castTo(); + auto fnType = getType(anchor)->castTo(); emitDiagnostic(diag::missing_nullary_call, fnType->getResult()) .fixItInsertAfter(getSourceRange().End, "()"); return true; @@ -3065,21 +3086,21 @@ bool MissingCallFailure::diagnoseAsError() { } } - if (auto *DRE = dyn_cast(baseExpr)) { + if (auto *DRE = getAsExpr(anchor)) { emitDiagnostic(diag::did_not_call_function, DRE->getDecl()->getBaseIdentifier()) .fixItInsertAfter(insertLoc, "()"); return true; } - if (auto *UDE = dyn_cast(baseExpr)) { + if (auto *UDE = getAsExpr(anchor)) { emitDiagnostic(diag::did_not_call_method, UDE->getName().getBaseIdentifier()) .fixItInsertAfter(insertLoc, "()"); return true; } - if (auto *DSCE = dyn_cast(baseExpr)) { + if (auto *DSCE = getAsExpr(anchor)) { if (auto *DRE = dyn_cast(DSCE->getFn())) { emitDiagnostic(diag::did_not_call_method, DRE->getDecl()->getBaseIdentifier()) @@ -3088,7 +3109,7 @@ bool MissingCallFailure::diagnoseAsError() { } } - if (auto *AE = dyn_cast(baseExpr)) { + if (auto *AE = getAsExpr(anchor)) { auto *srcExpr = AE->getSrc(); if (auto *fnType = getType(srcExpr)->getAs()) { emitDiagnosticAt(srcExpr->getLoc(), diag::missing_nullary_call, @@ -3122,7 +3143,7 @@ bool ExtraneousPropertyWrapperUnwrapFailure::diagnoseAsError() { } bool MissingPropertyWrapperUnwrapFailure::diagnoseAsError() { - auto endLoc = getAnchor()->getLoc().getAdvancedLoc(1); + auto endLoc = getLoc(getAnchor()).getAdvancedLoc(1); if (auto *member = getReferencedMember()) { emitDiagnostic(diag::incorrect_property_wrapper_reference_member, @@ -3141,8 +3162,7 @@ bool MissingPropertyWrapperUnwrapFailure::diagnoseAsError() { bool SubscriptMisuseFailure::diagnoseAsError() { auto &sourceMgr = getASTContext().SourceMgr; - auto *memberExpr = cast(getRawAnchor()); - auto *baseExpr = getAnchor(); + auto *memberExpr = castToExpr(getRawAnchor()); auto memberRange = getSourceRange(); (void)simplifyLocator(getConstraintSystem(), getLocator(), memberRange); @@ -3150,7 +3170,7 @@ bool SubscriptMisuseFailure::diagnoseAsError() { auto nameLoc = DeclNameLoc(memberRange.Start); auto diag = emitDiagnostic(diag::could_not_find_subscript_member_did_you_mean, - getType(baseExpr)); + getType(getAnchor())); diag.highlight(memberRange).highlight(nameLoc.getSourceRange()); @@ -3211,21 +3231,18 @@ DeclName MissingMemberFailure::findCorrectEnumCaseName( } bool MissingMemberFailure::diagnoseAsError() { - auto *anchor = getRawAnchor(); - auto *baseExpr = getAnchor(); - - if (!anchor || !baseExpr) - return false; + auto anchor = getRawAnchor(); + auto memberBase = getAnchor(); if (diagnoseForDynamicCallable()) return true; auto baseType = resolveType(getBaseType())->getWithoutSpecifierType(); - DeclNameLoc nameLoc(anchor->getStartLoc()); - if (auto *UDE = dyn_cast(anchor)) { + DeclNameLoc nameLoc(FailureDiagnostic::getLoc(anchor)); + if (auto *UDE = getAsExpr(anchor)) { nameLoc = UDE->getNameLoc(); - } else if (auto *UME = dyn_cast(anchor)) { + } else if (auto *UME = getAsExpr(anchor)) { nameLoc = UME->getNameLoc(); } @@ -3241,10 +3258,13 @@ bool MissingMemberFailure::diagnoseAsError() { diagnostic = diag::could_not_find_tuple_member; bool hasUnresolvedPattern = false; - anchor->forEachChildExpr([&](Expr *expr) { - hasUnresolvedPattern |= isa(expr); - return hasUnresolvedPattern ? nullptr : expr; - }); + if (auto *E = anchor.dyn_cast()) { + const_cast(E)->forEachChildExpr([&](Expr *expr) { + hasUnresolvedPattern |= isa(expr); + return hasUnresolvedPattern ? nullptr : expr; + }); + } + if (hasUnresolvedPattern && !baseType->getAs()) { emitDiagnostic(diag::cannot_match_unresolved_expr_pattern_with_value, baseType); @@ -3317,7 +3337,8 @@ bool MissingMemberFailure::diagnoseAsError() { emitBasicError(baseType); } } else if (auto moduleTy = baseType->getAs()) { - emitDiagnosticAt(baseExpr->getLoc(), diag::no_member_of_module, + emitDiagnosticAt(FailureDiagnostic::getLoc(memberBase), + diag::no_member_of_module, moduleTy->getModule()->getName(), getName()) .highlight(getSourceRange()) .highlight(nameLoc.getSourceRange()); @@ -3347,13 +3368,13 @@ bool MissingMemberFailure::diagnoseAsError() { // If locator points to the member found via key path dynamic member lookup, // we provide a custom diagnostic and emit typo corrections for the wrapper type too. if (getLocator()->isForKeyPathDynamicMemberLookup()) { - auto baseExprType = getType(baseExpr)->getWithoutSpecifierType(); - - tryTypoCorrection(baseExprType); - + auto memberBaseType = getType(memberBase)->getWithoutSpecifierType(); + + tryTypoCorrection(memberBaseType); + if (auto correction = corrections.claimUniqueCorrection()) { auto diagnostic = emitDiagnostic( - diag::could_not_find_value_dynamic_member_corrected, baseExprType, + diag::could_not_find_value_dynamic_member_corrected, memberBaseType, baseType, getName(), correction->CorrectedName); diagnostic.highlight(getSourceRange()) .highlight(nameLoc.getSourceRange()); @@ -3361,7 +3382,7 @@ bool MissingMemberFailure::diagnoseAsError() { } else { auto diagnostic = emitDiagnostic(diag::could_not_find_value_dynamic_member, - baseExprType, baseType, getName()); + memberBaseType, baseType, getName()); diagnostic.highlight(getSourceRange()) .highlight(nameLoc.getSourceRange()); } @@ -3404,19 +3425,13 @@ bool MissingMemberFailure::diagnoseForDynamicCallable() const { } bool InvalidMemberRefOnExistential::diagnoseAsError() { - auto *anchor = getRawAnchor(); + auto anchor = getRawAnchor(); - Expr *baseExpr = getAnchor(); DeclNameLoc nameLoc; - if (auto *UDE = dyn_cast(anchor)) { - baseExpr = UDE->getBase(); + if (auto *UDE = getAsExpr(anchor)) { nameLoc = UDE->getNameLoc(); - } else if (auto *UME = dyn_cast(anchor)) { + } else if (auto *UME = getAsExpr(anchor)) { nameLoc = UME->getNameLoc(); - } else if (auto *SE = dyn_cast(anchor)) { - baseExpr = SE->getBase(); - } else if (auto *call = dyn_cast(anchor)) { - baseExpr = call->getFn(); } emitDiagnostic(diag::could_not_use_member_on_existential, getBaseType(), @@ -3428,7 +3443,6 @@ bool InvalidMemberRefOnExistential::diagnoseAsError() { bool AllowTypeOrInstanceMemberFailure::diagnoseAsError() { auto loc = getLoc(); - auto &cs = getConstraintSystem(); auto *DC = getDC(); auto locator = getLocator(); @@ -3436,13 +3450,19 @@ bool AllowTypeOrInstanceMemberFailure::diagnoseAsError() { return true; } - auto getRootExpr = [&cs](Expr *expr) { - while (auto parent = cs.getParentExpr(expr)) - expr = parent; - return expr; + auto getRootExpr = [this](const Expr *childExpr) { + auto *currExpr = const_cast(childExpr); + while (auto parent = findParentExpr(currExpr)) + currExpr = parent; + return currExpr; }; - Expr *expr = findParentExpr(getAnchor()); + auto anchor = getAnchor(); + + if (!anchor.is()) + return false; + + Expr *expr = findParentExpr(anchor.get()); SourceRange baseRange = expr ? expr->getSourceRange() : SourceRange(); // If the base is an implicit self type reference, and we're in a @@ -3468,22 +3488,21 @@ bool AllowTypeOrInstanceMemberFailure::diagnoseAsError() { if (Name.isSimpleName(DeclBaseName::createConstructor()) && !BaseType->is()) { - if (auto ctorRef = dyn_cast(getRawAnchor())) { + if (auto *ctorRef = getAsExpr(getRawAnchor())) { if (isa(ctorRef->getBase())) { emitDiagnostic(diag::super_initializer_not_in_initializer); return true; } auto isCallArgument = [this](Expr *expr) { - auto &cs = getConstraintSystem(); - auto argExpr = cs.getParentExpr(expr); + auto argExpr = findParentExpr(expr); if (!argExpr) return false; - auto possibleApplyExpr = cs.getParentExpr(expr); + auto possibleApplyExpr = findParentExpr(expr); return possibleApplyExpr && isa(possibleApplyExpr); }; - auto *initCall = cs.getParentExpr(cs.getParentExpr(ctorRef)); + auto *initCall = findParentExpr(findParentExpr(ctorRef)); auto isMutable = [&DC](ValueDecl *decl) { if (auto *storage = dyn_cast(decl)) @@ -3492,7 +3511,7 @@ bool AllowTypeOrInstanceMemberFailure::diagnoseAsError() { return true; }; - auto *baseLoc = cs.getConstraintLocator(ctorRef->getBase()); + auto *baseLoc = getConstraintLocator(ctorRef->getBase()); if (auto selection = getCalleeOverloadChoiceIfAvailable(baseLoc)) { OverloadChoice choice = selection->choice; if (choice.isDecl() && isMutable(choice.getDecl()) && @@ -3520,9 +3539,9 @@ bool AllowTypeOrInstanceMemberFailure::diagnoseAsError() { instanceTy = AMT->getInstanceType(); } - if (getRawAnchor() && - cs.DC->getContextKind() == DeclContextKind::Initializer) { - auto *TypeDC = cs.DC->getParent(); + auto *DC = getDC(); + if (DC->getContextKind() == DeclContextKind::Initializer) { + auto *TypeDC = DC->getParent(); bool propertyInitializer = true; // If the parent context is not a type context, we expect it // to be a defaulted parameter in a function declaration. @@ -3547,22 +3566,22 @@ bool AllowTypeOrInstanceMemberFailure::diagnoseAsError() { } } - auto maybeCallExpr = getRawAnchor(); - - if (auto UDE = dyn_cast(maybeCallExpr)) { - maybeCallExpr = UDE->getBase(); - } + if (auto *maybeCallExpr = getRawAnchor().dyn_cast()) { + if (auto *UDE = dyn_cast(maybeCallExpr)) { + maybeCallExpr = UDE->getBase(); + } - if (auto callExpr = dyn_cast(maybeCallExpr)) { - auto fnExpr = callExpr->getFn(); - auto fnType = cs.getType(fnExpr)->getRValueType(); - auto arg = callExpr->getArg(); + if (auto callExpr = dyn_cast(maybeCallExpr)) { + auto fnExpr = callExpr->getFn(); + auto fnType = getType(fnExpr)->getRValueType(); + auto arg = callExpr->getArg(); - if (fnType->is()) { - emitDiagnosticAt(arg->getStartLoc(), - diag::missing_init_on_metatype_initialization) - .highlight(fnExpr->getSourceRange()); - return true; + if (fnType->is()) { + emitDiagnosticAt(arg->getStartLoc(), + diag::missing_init_on_metatype_initialization) + .highlight(fnExpr->getSourceRange()); + return true; + } } } @@ -3570,8 +3589,8 @@ bool AllowTypeOrInstanceMemberFailure::diagnoseAsError() { // provide more specialized message. auto memberTypeContext = Member->getDeclContext()->getInnermostTypeContext(); - auto currentTypeContext = cs.DC->getInnermostTypeContext(); - + auto currentTypeContext = getDC()->getInnermostTypeContext(); + if (memberTypeContext && currentTypeContext && memberTypeContext->getSemanticDepth() < currentTypeContext->getSemanticDepth()) { @@ -3583,7 +3602,7 @@ bool AllowTypeOrInstanceMemberFailure::diagnoseAsError() { return true; } - if (auto *UDE = dyn_cast(getRawAnchor())) { + if (auto *UDE = getAsExpr(getRawAnchor())) { auto *baseExpr = UDE->getBase(); if (isa(baseExpr)) { emitDiagnostic(diag::instance_member_use_on_type, instanceTy, Name) @@ -3637,7 +3656,7 @@ bool AllowTypeOrInstanceMemberFailure::diagnoseAsError() { Diag->highlight(baseRange).highlight(getSourceRange()); // See through function decl context - if (auto parent = cs.DC->getInnermostTypeContext()) { + if (auto parent = getDC()->getInnermostTypeContext()) { // If we are in a protocol extension of 'Proto' and we see // 'Proto.static', suggest 'Self.static' if (auto extensionContext = parent->getExtendedProtocolDecl()) { @@ -3667,11 +3686,11 @@ bool AllowTypeOrInstanceMemberFailure::diagnoseAsError() { baseTy, Name)); } - Diag->highlight(getAnchor()->getSourceRange()); + Diag->highlight(getSourceRange()); if (Name.isSimpleName(DeclBaseName::createConstructor()) && !baseTy->is()) { - if (auto ctorRef = dyn_cast(getRawAnchor())) { + if (auto ctorRef = getAsExpr(getRawAnchor())) { SourceRange fixItRng = ctorRef->getNameLoc().getSourceRange(); Diag->fixItInsert(fixItRng.Start, "type(of: "); Diag->fixItInsertAfter(fixItRng.End, ")"); @@ -3689,7 +3708,8 @@ bool AllowTypeOrInstanceMemberFailure::diagnoseAsError() { // Check if the expression is the matching operator ~=, most often used in // case statements. If so, try to provide a single dot fix-it - const Expr *contextualTypeNode = getRootExpr(getAnchor()); + const Expr *contextualTypeNode = + getRootExpr(getAnchor().dyn_cast()); // The '~=' operator is an overloaded decl ref inside a binaryExpr if (auto binaryExpr = dyn_cast(contextualTypeNode)) { @@ -3704,8 +3724,7 @@ bool AllowTypeOrInstanceMemberFailure::diagnoseAsError() { // If the rhs of '~=' is the enum type, a single dot suffixes // since the type can be inferred - Type secondArgType = - cs.getType(binaryExpr->getArg()->getElement(1)); + Type secondArgType = getType(binaryExpr->getArg()->getElement(1)); if (secondArgType->isEqual(baseTy)) { Diag->fixItInsert(loc, "."); return true; @@ -3717,9 +3736,9 @@ bool AllowTypeOrInstanceMemberFailure::diagnoseAsError() { // Fall back to a fix-it with a full type qualifier const Expr *baseExpr = nullptr; - if (const auto SE = dyn_cast(getRawAnchor())) + if (const auto *SE = getAsExpr(getRawAnchor())) baseExpr = SE->getBase(); - else if (const auto UDE = dyn_cast(getRawAnchor())) + else if (const auto UDE = getAsExpr(getRawAnchor())) baseExpr = UDE->getBase(); // An implicit 'self' reference base expression means we should @@ -3738,14 +3757,13 @@ bool AllowTypeOrInstanceMemberFailure::diagnoseAsError() { } bool PartialApplicationFailure::diagnoseAsError() { - auto &cs = getConstraintSystem(); - auto *anchor = cast(getRawAnchor()); + auto *anchor = castToExpr(getRawAnchor()); RefKind kind = RefKind::MutatingMethod; // If this is initializer delegation chain, we have a tailored message. - if (getOverloadChoiceIfAvailable(cs.getConstraintLocator( - anchor, ConstraintLocator::ConstructorMember))) { + if (getOverloadChoiceIfAvailable( + getConstraintLocator(anchor, ConstraintLocator::ConstructorMember))) { kind = anchor->getBase()->isSuperExpr() ? RefKind::SuperInit : RefKind::SelfInit; } else if (anchor->getBase()->isSuperExpr()) { @@ -3783,8 +3801,10 @@ bool InitOnProtocolMetatypeFailure::diagnoseAsError() { } SourceLoc ImplicitInitOnNonConstMetatypeFailure::getLoc() const { - auto *apply = cast(getRawAnchor()); - return apply->getArg()->getStartLoc(); + if (auto *apply = getAsExpr(getRawAnchor())) + return apply->getArg()->getStartLoc(); + + return FailureDiagnostic::getLoc(); } bool ImplicitInitOnNonConstMetatypeFailure::diagnoseAsError() { @@ -3793,10 +3813,12 @@ bool ImplicitInitOnNonConstMetatypeFailure::diagnoseAsError() { return true; } -Expr *MissingArgumentsFailure::getAnchor() const { - auto *anchor = FailureDiagnostic::getAnchor(); - if (auto *captureList = dyn_cast(anchor)) +TypedNode MissingArgumentsFailure::getAnchor() const { + auto anchor = FailureDiagnostic::getAnchor(); + + if (auto *captureList = getAsExpr(anchor)) return captureList->getClosureBody(); + return anchor; } @@ -3815,9 +3837,9 @@ bool MissingArgumentsFailure::diagnoseAsError() { if (isMisplacedMissingArgument(getSolution(), locator)) return false; - auto *anchor = getAnchor(); + auto anchor = getAnchor(); - if (auto *closure = dyn_cast(anchor)) + if (auto *closure = getAsExpr(anchor)) return diagnoseClosure(closure); // This is a situation where function type is passed as an argument @@ -3937,9 +3959,10 @@ bool MissingArgumentsFailure::diagnoseAsNote() { bool MissingArgumentsFailure::diagnoseSingleMissingArgument() const { auto &ctx = getASTContext(); - auto *anchor = getRawAnchor(); - if (!(isa(anchor) || isa(anchor) || - isa(anchor) || isa(anchor))) + auto anchor = getRawAnchor(); + if (!(isExpr(anchor) || isExpr(anchor) || + isExpr(anchor) || + isExpr(anchor))) return false; if (SynthesizedArgs.size() != 1) @@ -4017,7 +4040,7 @@ bool MissingArgumentsFailure::diagnoseSingleMissingArgument() const { // fn { closure }: // fn[(argMissing)] { closure } // fn[(closureLabel:] { closure }[, missingArg)] // Not impl. - assert(!isa(anchor) && "bracket less subscript"); + assert(!isExpr(anchor) && "bracket less subscript"); assert(PE->hasTrailingClosure() && "paren less ParenExpr without trailing closure"); insertBuf.insert(insertBuf.begin(), '('); @@ -4052,7 +4075,7 @@ bool MissingArgumentsFailure::diagnoseSingleMissingArgument() const { return true; } -bool MissingArgumentsFailure::diagnoseClosure(ClosureExpr *closure) { +bool MissingArgumentsFailure::diagnoseClosure(const ClosureExpr *closure) { FunctionType *funcType = nullptr; auto *locator = getLocator(); @@ -4149,15 +4172,15 @@ bool MissingArgumentsFailure::diagnoseInvalidTupleDestructuring() const { if (SynthesizedArgs.size() < 2) return false; - auto *anchor = getAnchor(); + auto anchor = getAnchor(); Expr *argExpr = nullptr; // Something like `foo(x: (1, 2))` - if (auto *TE = dyn_cast(anchor)) { + if (auto *TE = getAsExpr(anchor)) { if (TE->getNumElements() == 1) argExpr = TE->getElement(0); } else { // or `foo((1, 2))` - argExpr = cast(anchor)->getSubExpr(); + argExpr = castToExpr(anchor)->getSubExpr(); } if (!(argExpr && getType(argExpr)->getRValueType()->is())) @@ -4190,7 +4213,7 @@ bool MissingArgumentsFailure::diagnoseInvalidTupleDestructuring() const { } bool MissingArgumentsFailure::isPropertyWrapperInitialization() const { - auto *call = dyn_cast(getRawAnchor()); + auto *call = getAsExpr(getRawAnchor()); if (!(call && call->isImplicit())) return false; @@ -4269,17 +4292,17 @@ bool MissingArgumentsFailure::isMisplacedMissingArgument( } std::tuple -MissingArgumentsFailure::getCallInfo(Expr *anchor) const { - if (auto *call = dyn_cast(anchor)) { +MissingArgumentsFailure::getCallInfo(TypedNode anchor) const { + if (auto *call = getAsExpr(anchor)) { return std::make_tuple(call->getFn(), call->getArg(), call->getNumArguments(), call->hasTrailingClosure()); - } else if (auto *UME = dyn_cast(anchor)) { + } else if (auto *UME = getAsExpr(anchor)) { return std::make_tuple(UME, UME->getArgument(), UME->getNumArguments(), UME->hasTrailingClosure()); - } else if (auto *SE = dyn_cast(anchor)) { + } else if (auto *SE = getAsExpr(anchor)) { return std::make_tuple(SE, SE->getIndex(), SE->getNumArguments(), SE->hasTrailingClosure()); - } else if (auto *OLE = dyn_cast(anchor)) { + } else if (auto *OLE = getAsExpr(anchor)) { return std::make_tuple(OLE, OLE->getArg(), OLE->getNumArguments(), OLE->hasTrailingClosure()); } @@ -4306,19 +4329,19 @@ void MissingArgumentsFailure::forFixIt( } SourceLoc ClosureParamDestructuringFailure::getLoc() const { - auto *closure = cast(getAnchor()); + auto *closure = castToExpr(getAnchor()); auto paramList = closure->getParameters(); return paramList->getStartLoc(); } SourceRange ClosureParamDestructuringFailure::getSourceRange() const { - auto *closure = cast(getAnchor()); + auto *closure = castToExpr(getAnchor()); auto paramList = closure->getParameters(); return paramList->getSourceRange(); } bool ClosureParamDestructuringFailure::diagnoseAsError() { - auto *closure = cast(getAnchor()); + auto *closure = castToExpr(getAnchor()); auto params = closure->getParameters(); // In case of implicit parameters e.g. $0, $1 we @@ -4448,9 +4471,10 @@ bool ClosureParamDestructuringFailure::diagnoseAsError() { } bool OutOfOrderArgumentFailure::diagnoseAsError() { - auto *anchor = getRawAnchor(); - auto *argExpr = isa(anchor) ? anchor - : getArgumentListExprFor(getLocator()); + auto anchor = getRawAnchor(); + auto *argExpr = isExpr(anchor) + ? castToExpr(anchor) + : getArgumentListExprFor(getLocator()); if (!argExpr) return false; @@ -4509,17 +4533,18 @@ bool OutOfOrderArgumentFailure::diagnoseAsError() { firstRange.End); diag.fixItRemove(removalRange); diag.fixItInsert(secondRange.Start, - text.str() + (isa(anchor) ? "" : ", ")); + text.str() + (isExpr(anchor) ? "" : ", ")); }; // There are 4 diagnostic messages variations depending on // labeled/unlabeled arguments. if (first.empty() && second.empty()) { - addFixIts(emitDiagnosticAt( - diagLoc, - isa(anchor) ? diag::argument_out_of_order_binary_op - : diag::argument_out_of_order_unnamed_unnamed, - ArgIdx + 1, PrevArgIdx + 1)); + addFixIts( + emitDiagnosticAt(diagLoc, + isExpr(anchor) + ? diag::argument_out_of_order_binary_op + : diag::argument_out_of_order_unnamed_unnamed, + ArgIdx + 1, PrevArgIdx + 1)); } else if (first.empty() && !second.empty()) { addFixIts(emitDiagnosticAt(diagLoc, diag::argument_out_of_order_unnamed_named, @@ -4538,8 +4563,9 @@ bool OutOfOrderArgumentFailure::diagnoseAsError() { bool ExtraneousArgumentsFailure::diagnoseAsError() { // Simplified anchor would point directly to the // argument in case of contextual mismatch. - auto *anchor = getAnchor(); - if (auto *closure = dyn_cast(anchor)) { + auto anchor = getAnchor(); + + if (auto *closure = getAsExpr(anchor)) { auto fnType = ContextualType; auto params = closure->getParameters(); @@ -4616,11 +4642,10 @@ bool ExtraneousArgumentsFailure::diagnoseAsNote() { return false; auto *decl = overload->choice.getDecl(); - auto *anchor = getAnchor(); auto numArgs = getTotalNumArguments(); emitDiagnosticAt(decl, diag::candidate_with_extraneous_args, ContextualType, ContextualType->getNumParams(), numArgs, (numArgs == 1), - isa(anchor)); + isExpr(getAnchor())); return true; } @@ -4630,7 +4655,7 @@ bool ExtraneousArgumentsFailure::diagnoseSingleExtraArgument() const { // This specifically handles a case of `Void(...)` which generates // constraints differently from other constructor invocations and // wouldn't have `ApplyArgument` as a last element in the locator. - if (auto *call = dyn_cast(getRawAnchor())) { + if (auto *call = getAsExpr(getRawAnchor())) { auto *TE = dyn_cast(call->getFn()); if (TE && getType(TE)->getMetatypeInstanceType()->isVoid()) { emitDiagnosticAt(call->getLoc(), diag::extra_argument_to_nullary_call) @@ -4680,7 +4705,7 @@ bool ExtraneousArgumentsFailure::diagnoseSingleExtraArgument() const { } bool InaccessibleMemberFailure::diagnoseAsError() { - auto *anchor = getRawAnchor(); + auto anchor = getRawAnchor(); // Let's try to avoid over-diagnosing chains of inaccessible // members e.g.: // @@ -4696,26 +4721,27 @@ bool InaccessibleMemberFailure::diagnoseAsError() { // sense to diagnose only `B` and consider the rest hidden. Expr *baseExpr = nullptr; DeclNameLoc nameLoc; - if (auto *UDE = dyn_cast(anchor)) { + if (auto *UDE = getAsExpr(anchor)) { baseExpr = UDE->getBase(); nameLoc = UDE->getNameLoc(); - } else if (auto *UME = dyn_cast(anchor)) { + } else if (auto *UME = getAsExpr(anchor)) { nameLoc = UME->getNameLoc(); - } else if (auto *SE = dyn_cast(anchor)) { + } else if (auto *SE = getAsExpr(anchor)) { baseExpr = SE->getBase(); - } else if (auto *call = dyn_cast(anchor)) { + } else if (auto *call = getAsExpr(anchor)) { baseExpr = call->getFn(); } if (baseExpr) { - auto &cs = getConstraintSystem(); - auto *locator = - cs.getConstraintLocator(baseExpr, ConstraintLocator::Member); - if (cs.hasFixFor(locator)) + auto *locator = getConstraintLocator(baseExpr, ConstraintLocator::Member); + const auto &solution = getSolution(); + if (llvm::any_of(solution.Fixes, [&locator](const ConstraintFix *fix) { + return fix->getLocator() == locator; + })) return false; } - auto loc = nameLoc.isValid() ? nameLoc.getStartLoc() : anchor->getLoc(); + auto loc = nameLoc.isValid() ? nameLoc.getStartLoc() : getLoc(anchor); auto accessLevel = Member->getFormalAccessScope().accessLevelForDiagnostics(); if (auto *CD = dyn_cast(Member)) { emitDiagnosticAt(loc, diag::init_candidate_inaccessible, @@ -4732,21 +4758,25 @@ bool InaccessibleMemberFailure::diagnoseAsError() { } SourceLoc AnyObjectKeyPathRootFailure::getLoc() const { - if (auto KPE = dyn_cast(getAnchor())) { + auto anchor = getAnchor(); + + if (auto *KPE = getAsExpr(anchor)) { if (auto rootTyRepr = KPE->getRootType()) return rootTyRepr->getLoc(); } - return FailureDiagnostic::getLoc(); + return FailureDiagnostic::getLoc(anchor); } SourceRange AnyObjectKeyPathRootFailure::getSourceRange() const { - if (auto KPE = dyn_cast(getAnchor())) { + auto anchor = getAnchor(); + + if (auto *KPE = getAsExpr(anchor)) { if (auto rootTyRepr = KPE->getRootType()) return rootTyRepr->getSourceRange(); } - return FailureDiagnostic::getSourceRange(); + return FailureDiagnostic::getSourceRange(anchor); } bool AnyObjectKeyPathRootFailure::diagnoseAsError() { @@ -4760,7 +4790,7 @@ SourceLoc KeyPathSubscriptIndexHashableFailure::getLoc() const { auto *locator = getLocator(); if (locator->isKeyPathSubscriptComponent()) { - auto *KPE = cast(getAnchor()); + auto *KPE = castToExpr(getAnchor()); if (auto kpElt = locator->findFirst()) return KPE->getComponents()[kpElt->getIndex()].getLoc(); } @@ -4775,16 +4805,16 @@ bool KeyPathSubscriptIndexHashableFailure::diagnoseAsError() { } SourceLoc InvalidMemberRefInKeyPath::getLoc() const { - auto *anchor = getRawAnchor(); + auto anchor = getRawAnchor(); - if (auto *KPE = dyn_cast(anchor)) { + if (auto *KPE = getAsExpr(anchor)) { auto *locator = getLocator(); auto component = locator->findFirst(); assert(component); return KPE->getComponents()[component->getIndex()].getLoc(); } - return anchor->getLoc(); + return FailureDiagnostic::getLoc(anchor); } bool InvalidStaticMemberRefInKeyPath::diagnoseAsError() { @@ -4806,12 +4836,12 @@ bool InvalidMethodRefInKeyPath::diagnoseAsError() { } SourceLoc InvalidUseOfAddressOf::getLoc() const { - auto *anchor = getAnchor(); + auto anchor = getAnchor(); - if (auto *assign = dyn_cast(anchor)) - anchor = assign->getSrc(); + if (auto *assign = getAsExpr(anchor)) + return assign->getSrc()->getLoc(); - return anchor->getLoc(); + return FailureDiagnostic::getLoc(anchor); } bool InvalidUseOfAddressOf::diagnoseAsError() { @@ -4851,18 +4881,19 @@ bool ExtraneousReturnFailure::diagnoseAsError() { } bool CollectionElementContextualFailure::diagnoseAsError() { + auto anchor = getRawAnchor(); auto *locator = getLocator(); auto eltType = getFromType(); auto contextualType = getToType(); Optional diagnostic; - if (isa(getRawAnchor())) { + if (isExpr(anchor)) { diagnostic.emplace(emitDiagnostic(diag::cannot_convert_array_element, eltType, contextualType)); } - if (isa(getRawAnchor())) { + if (isExpr(anchor)) { auto eltLoc = locator->castLastElementTo(); switch (eltLoc.getIndex()) { case 0: // key @@ -4881,11 +4912,10 @@ bool CollectionElementContextualFailure::diagnoseAsError() { } if (locator->isForSequenceElementType()) { - auto &cs = getConstraintSystem(); // If this is a conversion failure related to binding of `for-each` // statement it has to be diagnosed as pattern match if there are // holes present in the contextual type. - if (cs.getContextualTypePurpose(getAnchor()) == + if (FailureDiagnostic::getContextualTypePurpose(getAnchor()) == ContextualTypePurpose::CTP_ForEachStmt && contextualType->hasHole()) { diagnostic.emplace(emitDiagnostic( @@ -4910,13 +4940,13 @@ bool CollectionElementContextualFailure::diagnoseAsError() { } bool MissingContextualConformanceFailure::diagnoseAsError() { - auto *anchor = getAnchor(); + auto anchor = getAnchor(); auto path = getLocator()->getPath(); Optional> diagnostic; if (path.empty()) { - assert(isa(anchor)); - if (isa(cast(anchor)->getDest())) { + assert(isExpr(anchor)); + if (isa(castToExpr(anchor)->getDest())) { diagnostic = getDiagnosticFor(CTP_SubscriptAssignSource, getToType()); } else { diagnostic = getDiagnosticFor(CTP_AssignSource, getToType()); @@ -4947,7 +4977,7 @@ bool MissingContextualConformanceFailure::diagnoseAsError() { emitDiagnostic(*diagnostic, srcType, dstType); - if (isa(anchor)) + if (isExpr(anchor)) return true; if (srcType->isAny() && dstType->isAnyObject()) { @@ -4972,7 +5002,7 @@ bool MissingGenericArgumentsFailure::diagnoseAsError() { }); if (!isScoped) - return diagnoseForAnchor(getAnchor(), Parameters); + return diagnoseForAnchor(getAnchor().get(), Parameters); bool diagnosed = false; for (const auto &scope : scopedParameters) @@ -5018,8 +5048,8 @@ bool MissingGenericArgumentsFailure::diagnoseParameter( Anchor anchor, GenericTypeParamType *GP) const { auto &cs = getConstraintSystem(); - auto loc = anchor.is() ? anchor.get()->getLoc() - : anchor.get()->getLoc(); + auto loc = anchor.is() ? anchor.get()->getLoc() + : anchor.get()->getLoc(); auto *locator = getLocator(); // Type variables associated with missing generic parameters are @@ -5033,8 +5063,8 @@ bool MissingGenericArgumentsFailure::diagnoseParameter( return false; } - if (auto *CE = dyn_cast(getRawAnchor())) { - auto castTo = getType(CE->getCastTypeLoc()); + if (auto *CE = getAsExpr(getRawAnchor())) { + auto castTo = getType(&CE->getCastTypeLoc()); auto *NTD = castTo->getAnyNominal(); emitDiagnosticAt(loc, diag::unbound_generic_parameter_cast, GP, NTD ? NTD->getDeclaredType() : castTo); @@ -5073,7 +5103,7 @@ void MissingGenericArgumentsFailure::emitGenericSignatureNote( return; auto *GTD = dyn_cast(paramDC); - if (!GTD || anchor.is()) + if (!GTD || anchor.is()) return; auto getParamDecl = @@ -5130,12 +5160,12 @@ bool MissingGenericArgumentsFailure::findArgumentLocations( llvm::function_ref callback) { using Callback = llvm::function_ref; - auto *anchor = getRawAnchor(); + auto anchor = getRawAnchor(); TypeLoc typeLoc; - if (auto *TE = dyn_cast(anchor)) + if (auto *TE = getAsExpr(anchor)) typeLoc = TE->getTypeLoc(); - else if (auto *ECE = dyn_cast(anchor)) + else if (auto *ECE = getAsExpr(anchor)) typeLoc = ECE->getCastTypeLoc(); if (!typeLoc.hasLocation()) @@ -5230,7 +5260,7 @@ bool SkipUnhandledConstructInFunctionBuilderFailure::diagnoseAsNote() { } bool MutatingMemberRefOnImmutableBase::diagnoseAsError() { - auto *anchor = getRawAnchor(); + auto *anchor = getRawAnchor().get(); auto baseExpr = getBaseExprFor(anchor); if (!baseExpr) return false; @@ -5338,7 +5368,6 @@ bool ThrowingFunctionConversionFailure::diagnoseAsError() { } bool InOutConversionFailure::diagnoseAsError() { - auto *anchor = getAnchor(); auto *locator = getLocator(); auto path = locator->getPath(); @@ -5349,6 +5378,7 @@ bool InOutConversionFailure::diagnoseAsError() { argApplyInfo->getArgType(), argApplyInfo->getParamType()); } else { assert(locator->findLast()); + auto anchor = getAnchor(); auto contextualType = getContextualType(anchor); auto purpose = getContextualTypePurpose(); auto diagnostic = getDiagnosticFor(purpose, contextualType); @@ -5369,7 +5399,7 @@ bool InOutConversionFailure::diagnoseAsError() { } void InOutConversionFailure::fixItChangeArgumentType() const { - auto *argExpr = getAnchor(); + auto *argExpr = getAnchor().get(); auto *DC = getDC(); if (auto *IOE = dyn_cast(argExpr)) @@ -5476,11 +5506,11 @@ bool ArgumentMismatchFailure::diagnoseAsError() { // If argument is an l-value type and parameter is a pointer type, // let's match up its element type to the argument to see whether // it would be appropriate to suggest adding `&`. - auto *argExpr = getAnchor(); - if (getType(argExpr, /*wantRValue=*/false)->is()) { + auto argument = getAnchor(); + if (getType(argument, /*wantRValue=*/false)->is()) { auto elementTy = paramType->getAnyPointerElementType(); if (elementTy && argType->isEqual(elementTy)) { - diag.fixItInsert(argExpr->getStartLoc(), "&"); + diag.fixItInsert(getSourceRange(argument).Start, "&"); return true; } } @@ -5508,7 +5538,7 @@ bool ArgumentMismatchFailure::diagnoseUseOfReferenceEqualityOperator() const { if (!isArgumentOfReferenceEqualityOperator(locator)) return false; - auto *binaryOp = cast(getRawAnchor()); + auto *binaryOp = castToExpr(getRawAnchor()); auto *lhs = binaryOp->getArg()->getElement(0); auto *rhs = binaryOp->getArg()->getElement(1); @@ -5520,9 +5550,10 @@ bool ArgumentMismatchFailure::diagnoseUseOfReferenceEqualityOperator() const { // If both arguments where incorrect e.g. both are function types, // let's avoid producing a diagnostic second time, because first // one would cover both arguments. - if (getAnchor() == rhs && rhsType->is()) { + if (getAnchor().dyn_cast() == rhs && + rhsType->is()) { auto &cs = getConstraintSystem(); - if (cs.hasFixFor(cs.getConstraintLocator( + if (cs.hasFixFor(getConstraintLocator( binaryOp, {ConstraintLocator::ApplyArgument, LocatorPathElt::ApplyArgToParam( 0, 0, getParameterFlagsAtIndex(0))}))) @@ -5572,7 +5603,7 @@ bool ArgumentMismatchFailure::diagnosePatternMatchingMismatch() const { if (!isArgumentOfPatternMatchingOperator(getLocator())) return false; - auto *op = cast(getRawAnchor()); + auto *op = castToExpr(getRawAnchor()); auto *lhsExpr = op->getArg()->getElement(0); auto *rhsExpr = op->getArg()->getElement(1); @@ -5652,7 +5683,7 @@ bool ArgumentMismatchFailure::diagnoseMisplacedMissingArgument() const { auto *fnType = getFnType(); const auto ¶m = fnType->getParams()[0]; - auto *anchor = getRawAnchor(); + auto anchor = getRawAnchor(); MissingArgumentsFailure failure( solution, {std::make_pair(0, param)}, @@ -5667,7 +5698,7 @@ bool ArgumentMismatchFailure::diagnosePropertyWrapperMismatch() const { // Verify that this is an implicit call to a property wrapper initializer // in a form of `init(wrappedValue:)` or deprecated `init(initialValue:)`. - auto *call = dyn_cast(getRawAnchor()); + auto *call = getAsExpr(getRawAnchor()); if (!(call && call->isImplicit() && isa(call->getFn()) && call->getNumArguments() == 1 && (call->getArgumentLabels().front() == getASTContext().Id_wrappedValue || @@ -5687,7 +5718,7 @@ bool ArgumentMismatchFailure::diagnosePropertyWrapperMismatch() const { } void ExpandArrayIntoVarargsFailure::tryDropArrayBracketsFixIt( - Expr *anchor) const { + const Expr *anchor) const { // If this is an array literal, offer to remove the brackets and pass the // elements directly as variadic arguments. if (auto *arrayExpr = dyn_cast(anchor)) { @@ -5702,7 +5733,7 @@ void ExpandArrayIntoVarargsFailure::tryDropArrayBracketsFixIt( } bool ExpandArrayIntoVarargsFailure::diagnoseAsError() { - if (auto anchor = getAnchor()) { + if (auto *anchor = getAnchor().dyn_cast()) { emitDiagnostic(diag::cannot_convert_array_to_variadic, getFromType(), getToType()); tryDropArrayBracketsFixIt(anchor); @@ -5714,7 +5745,7 @@ bool ExpandArrayIntoVarargsFailure::diagnoseAsError() { bool ExpandArrayIntoVarargsFailure::diagnoseAsNote() { auto overload = getCalleeOverloadChoiceIfAvailable(getLocator()); - auto anchor = getAnchor(); + auto *anchor = getAnchor().dyn_cast(); if (!overload || !anchor) return false; @@ -5728,16 +5759,14 @@ bool ExpandArrayIntoVarargsFailure::diagnoseAsNote() { } bool ExtraneousCallFailure::diagnoseAsError() { - auto &cs = getConstraintSystem(); - - auto *anchor = getAnchor(); + auto anchor = getAnchor(); auto *locator = getLocator(); // If this is something like `foo()` where `foo` is a variable // or a property, let's suggest dropping `()`. auto removeParensFixIt = [&](InFlightDiagnostic &diagnostic) { - auto *argLoc = cs.getConstraintLocator(getRawAnchor(), - ConstraintLocator::ApplyArgument); + auto *argLoc = + getConstraintLocator(getRawAnchor(), ConstraintLocator::ApplyArgument); if (auto *TE = dyn_cast_or_null(simplifyLocatorToAnchor(argLoc))) { @@ -5759,9 +5788,9 @@ bool ExtraneousCallFailure::diagnoseAsError() { } } - if (auto *UDE = dyn_cast(anchor)) { + if (auto *UDE = getAsExpr(anchor)) { auto *baseExpr = UDE->getBase(); - auto *call = cast(getRawAnchor()); + auto *call = castToExpr(getRawAnchor()); if (getType(baseExpr)->isAnyObject()) { emitDiagnostic(diag::cannot_call_with_params, @@ -5936,9 +5965,9 @@ bool NonEphemeralConversionFailure::diagnosePointerInit() const { ? diag::cannot_construct_dangling_pointer_warning : diag::cannot_construct_dangling_pointer; - auto *anchor = getRawAnchor(); - emitDiagnosticAt(anchor->getLoc(), diagID, constructedTy, constructorKind) - .highlight(anchor->getSourceRange()); + auto anchor = getRawAnchor(); + emitDiagnosticAt(getLoc(anchor), diagID, constructedTy, constructorKind) + .highlight(getSourceRange(anchor)); emitSuggestionNotes(); return true; @@ -6032,7 +6061,7 @@ bool AssignmentTypeMismatchFailure::diagnoseAsError() { } bool AssignmentTypeMismatchFailure::diagnoseAsNote() { - auto *anchor = getAnchor(); + auto anchor = getAnchor(); if (auto overload = getCalleeOverloadChoiceIfAvailable(getConstraintLocator(anchor))) { @@ -6048,7 +6077,7 @@ bool AssignmentTypeMismatchFailure::diagnoseAsNote() { } bool MissingContextualBaseInMemberRefFailure::diagnoseAsError() { - auto *anchor = getAnchor(); + auto *anchor = getAnchor().get(); // Member reference could be wrapped into a number of parens // e.g. `((.foo))`. auto *parentExpr = findParentExpr(anchor); @@ -6069,7 +6098,7 @@ bool MissingContextualBaseInMemberRefFailure::diagnoseAsError() { } bool UnableToInferClosureReturnType::diagnoseAsError() { - auto *closure = cast(getRawAnchor()); + auto *closure = castToExpr(getRawAnchor()); auto diagnostic = emitDiagnostic(diag::cannot_infer_closure_result_type, closure->hasSingleExpressionBody()); @@ -6095,9 +6124,10 @@ bool UnableToInferClosureReturnType::diagnoseAsError() { } static std::pair -getImportModuleAndDefaultType(const ASTContext &ctx, ObjectLiteralExpr *expr) { +getImportModuleAndDefaultType(const ASTContext &ctx, + const ObjectLiteralExpr *expr) { const auto &target = ctx.LangOpts.Target; - + switch (expr->getLiteralKind()) { case ObjectLiteralExpr::colorLiteral: { if (target.isMacOSX()) { @@ -6126,13 +6156,12 @@ getImportModuleAndDefaultType(const ASTContext &ctx, ObjectLiteralExpr *expr) { } SourceLoc UnableToInferProtocolLiteralType::getLoc() const { - return getRawAnchor()->getLoc(); + return FailureDiagnostic::getLoc(getRawAnchor()); } bool UnableToInferProtocolLiteralType::diagnoseAsError() { - auto &cs = getConstraintSystem(); - auto &ctx = cs.getASTContext(); - auto *expr = cast(getRawAnchor()); + auto &ctx = getASTContext(); + auto *expr = castToExpr(getRawAnchor()); StringRef importModule; StringRef importDefaultTypeName; @@ -6154,7 +6183,7 @@ bool MissingQuialifierInMemberRefFailure::diagnoseAsError() { if (!selectedOverload) return false; - auto *UDE = cast(getRawAnchor()); + auto *UDE = castToExpr(getRawAnchor()); auto baseType = getType(UDE->getBase()); diff --git a/lib/Sema/CSDiagnostics.h b/lib/Sema/CSDiagnostics.h index 85534ebffcb32..dd0fadb6f03b8 100644 --- a/lib/Sema/CSDiagnostics.h +++ b/lib/Sema/CSDiagnostics.h @@ -41,25 +41,19 @@ class FailureDiagnostic { const Solution &S; ConstraintLocator *Locator; - /// The original anchor before any simplification. - Expr *RawAnchor; - /// Simplified anchor associated with the given locator. - Expr *Anchor; - public: FailureDiagnostic(const Solution &solution, ConstraintLocator *locator) - : S(solution), Locator(locator), RawAnchor(locator->getAnchor()), - Anchor(computeAnchor()) {} + : S(solution), Locator(locator) {} - FailureDiagnostic(const Solution &solution, Expr *anchor) + FailureDiagnostic(const Solution &solution, const Expr *anchor) : FailureDiagnostic(solution, solution.getConstraintLocator(anchor)) {} virtual ~FailureDiagnostic(); - virtual SourceLoc getLoc() const { return getAnchor()->getLoc(); } + virtual SourceLoc getLoc() const { return getLoc(getAnchor()); } virtual SourceRange getSourceRange() const { - return getAnchor()->getSourceRange(); + return getSourceRange(getAnchor()); } /// Try to diagnose a problem given affected expression, @@ -83,14 +77,13 @@ class FailureDiagnostic { /// e.g. ambiguity error. virtual bool diagnoseAsNote(); - Expr *getRawAnchor() const { return RawAnchor; } + TypedNode getRawAnchor() const { return Locator->getAnchor(); } - virtual Expr *getAnchor() const { return Anchor; } + virtual TypedNode getAnchor() const; ConstraintLocator *getLocator() const { return Locator; } - Type getType(Expr *expr, bool wantRValue = true) const; - Type getType(const TypeLoc &loc, bool wantRValue = true) const; + Type getType(TypedNode node, bool wantRValue = true) const; /// Resolve type variables present in the raw type, if any. Type resolveType(Type rawType, bool reconstituteSugar = false, @@ -124,19 +117,19 @@ class FailureDiagnostic { return S.getConstraintSystem(); } - Type getContextualType(Expr *anchor) const { + Type getContextualType(TypedNode anchor) const { auto &cs = getConstraintSystem(); - return cs.getContextualType(anchor); + return cs.getContextualType(anchor.get()); } - TypeLoc getContextualTypeLoc(Expr *anchor) const { + TypeLoc getContextualTypeLoc(TypedNode anchor) const { auto &cs = getConstraintSystem(); - return cs.getContextualTypeLoc(anchor); + return cs.getContextualTypeLoc(anchor.get()); } - ContextualTypePurpose getContextualTypePurpose(Expr *anchor) const { + ContextualTypePurpose getContextualTypePurpose(TypedNode anchor) const { auto &cs = getConstraintSystem(); - return cs.getContextualTypePurpose(anchor); + return cs.getContextualTypePurpose(anchor.get()); } DeclContext *getDC() const { @@ -164,16 +157,23 @@ class FailureDiagnostic { } ConstraintLocator * - getConstraintLocator(Expr *anchor, + getConstraintLocator(TypedNode anchor, ConstraintLocator::PathElement element) const { - return S.getConstraintLocator(anchor, {element}); + return S.getConstraintLocator(anchor.get(), {element}); } /// Retrive the constraint locator for the given anchor and /// path, uniqued and automatically calculate the summary flags ConstraintLocator *getConstraintLocator( - Expr *anchor, ArrayRef path = {}) const { - return S.getConstraintLocator(anchor, path); + TypedNode anchor, + ArrayRef path = {}) const { + return S.getConstraintLocator(anchor.get(), path); + } + + ConstraintLocator * + getConstraintLocator(ConstraintLocator *baseLocator, + ConstraintLocator::PathElement element) const { + return S.getConstraintLocator(baseLocator, element); } Optional @@ -183,11 +183,11 @@ class FailureDiagnostic { /// \returns A parent expression if sub-expression is contained anywhere /// in the root expression or `nullptr` otherwise. - Expr *findParentExpr(Expr *subExpr) const; + Expr *findParentExpr(const Expr *subExpr) const; /// If given expression is some kind of a member reference e.g. /// `x.foo` or `x[0]` extract and return its base expression. - Expr *getBaseExprFor(Expr *anchor) const; + Expr *getBaseExprFor(const Expr *anchor) const; /// For a given locator describing an argument application, or a constraint /// within an argument application, returns the argument list for that @@ -202,9 +202,23 @@ class FailureDiagnostic { llvm::function_ref substitution = [](GenericTypeParamType *, Type) {}); -private: - /// Compute anchor expression associated with current diagnostic. - Expr *computeAnchor() const; + static SourceLoc getLoc(TypedNode node); + static SourceRange getSourceRange(TypedNode node); + + template static const T *castToExpr(TypedNode node) { + return cast(node.get()); + } + + template static T *getAsExpr(TypedNode node) { + if (const auto *E = node.dyn_cast()) + return dyn_cast(const_cast(E)); + return nullptr; + } + + template static bool isExpr(TypedNode node) { + auto *E = node.get(); + return isa(E); + } }; /// Base class for all of the diagnostics related to generic requirement @@ -251,7 +265,7 @@ class RequirementFailure : public FailureDiagnostic { assert(getGenericContext() && "Affected decl not within a generic context?"); - if (auto *parentExpr = findParentExpr(getRawAnchor())) + if (auto *parentExpr = findParentExpr(getRawAnchor().get())) Apply = dyn_cast(parentExpr); } @@ -507,16 +521,16 @@ class TrailingClosureAmbiguityFailure final : public FailureDiagnostic { /// trying to assign something to immutable value, or trying /// to access mutating member on immutable base. class AssignmentFailure final : public FailureDiagnostic { - Expr *DestExpr; + const Expr *DestExpr; SourceLoc Loc; Diag DeclDiagnostic; Diag TypeDiagnostic; public: - AssignmentFailure(Expr *destExpr, const Solution &solution, + AssignmentFailure(const Expr *destExpr, const Solution &solution, SourceLoc diagnosticLoc); - AssignmentFailure(Expr *destExpr, const Solution &solution, + AssignmentFailure(const Expr *destExpr, const Solution &solution, SourceLoc diagnosticLoc, Diag declDiag, Diag typeDiag) : FailureDiagnostic(solution, destExpr), DestExpr(destExpr), @@ -537,7 +551,13 @@ class AssignmentFailure final : public FailureDiagnostic { std::pair> resolveImmutableBase(Expr *expr) const; - static Diag findDeclDiagonstic(ASTContext &ctx, Expr *destExpr); + std::pair> + resolveImmutableBase(const Expr *expr) const { + return resolveImmutableBase(const_cast(expr)); + } + + static Diag findDeclDiagonstic(ASTContext &ctx, + const Expr *destExpr); /// Retrive an member reference associated with given member /// looking through dynamic member lookup on the way. @@ -708,9 +728,9 @@ class MissingOptionalUnwrapFailure final : public ContextualFailure { } /// Suggest a default value via `?? ` - void offerDefaultValueUnwrapFixIt(DeclContext *DC, Expr *expr) const; + void offerDefaultValueUnwrapFixIt(DeclContext *DC, const Expr *expr) const; /// Suggest a force optional unwrap via `!` - void offerForceUnwrapFixIt(Expr *expr) const; + void offerForceUnwrapFixIt(const Expr *expr) const; }; /// Diagnostics for mismatched generic arguments e.g @@ -786,28 +806,30 @@ class MissingExplicitConversionFailure final : public ContextualFailure { Type toType, ConstraintLocator *locator) : ContextualFailure(solution, fromType, toType, locator) {} - Expr *getAnchor() const override; + TypedNode getAnchor() const override; bool diagnoseAsError() override; private: - bool exprNeedsParensBeforeAddingAs(Expr *expr) { + bool exprNeedsParensBeforeAddingAs(const Expr *expr) { auto *DC = getDC(); auto asPG = TypeChecker::lookupPrecedenceGroup( DC, DC->getASTContext().Id_CastingPrecedence, SourceLoc()); if (!asPG) return true; - return exprNeedsParensInsideFollowingOperator(DC, expr, asPG); + return exprNeedsParensInsideFollowingOperator(DC, const_cast(expr), + asPG); } - bool exprNeedsParensAfterAddingAs(Expr *expr, Expr *rootExpr) { + bool exprNeedsParensAfterAddingAs(const Expr *expr, const Expr *rootExpr) { auto *DC = getDC(); auto asPG = TypeChecker::lookupPrecedenceGroup( DC, DC->getASTContext().Id_CastingPrecedence, SourceLoc()); if (!asPG) return true; - return exprNeedsParensOutsideFollowingOperator(DC, expr, rootExpr, asPG); + return exprNeedsParensOutsideFollowingOperator( + DC, const_cast(expr), const_cast(rootExpr), asPG); } }; @@ -945,7 +967,7 @@ class MissingCallFailure final : public FailureDiagnostic { MissingCallFailure(const Solution &solution, ConstraintLocator *locator) : FailureDiagnostic(solution, locator) {} - Expr *getAnchor() const override; + TypedNode getAnchor() const override; bool diagnoseAsError() override; }; @@ -1043,7 +1065,7 @@ class MissingMemberFailure final : public InvalidMemberRefFailure { SourceLoc getLoc() const override { // Diagnostic should point to the member instead of its base expression. - return getRawAnchor()->getLoc(); + return FailureDiagnostic::getLoc(getRawAnchor()); } bool diagnoseAsError() override; @@ -1140,7 +1162,7 @@ class InvalidInitRefFailure : public FailureDiagnostic { const ConstructorDecl *Init; SourceRange BaseRange; - Expr *getAnchor() const override { return getRawAnchor(); } + TypedNode getAnchor() const override { return getRawAnchor(); } InvalidInitRefFailure(const Solution &solution, Type baseTy, const ConstructorDecl *init, SourceRange baseRange, @@ -1234,7 +1256,7 @@ class MissingArgumentsFailure final : public FailureDiagnostic { assert(!SynthesizedArgs.empty() && "No missing arguments?!"); } - Expr *getAnchor() const override; + TypedNode getAnchor() const override; bool diagnoseAsError() override; @@ -1245,7 +1267,7 @@ class MissingArgumentsFailure final : public FailureDiagnostic { private: /// If missing arguments come from a closure, /// let's produce tailored diagnostics. - bool diagnoseClosure(ClosureExpr *closure); + bool diagnoseClosure(const ClosureExpr *closure); /// Diagnose cases when instead of multiple distinct arguments /// call got a single tuple argument with expected arity/types. @@ -1256,10 +1278,11 @@ class MissingArgumentsFailure final : public FailureDiagnostic { /// `@Foo(answer: 42) var question = "ultimate question"` bool isPropertyWrapperInitialization() const; - /// Gather informatioin associated with expression that represents + /// Gather information associated with expression that represents /// a call - function, arguments, # of arguments and whether it has /// a trailing closure. - std::tuple getCallInfo(Expr *anchor) const; + std::tuple + getCallInfo(TypedNode anchor) const; /// Transform given argument into format suitable for a fix-it /// text e.g. `[