Skip to content

Commit

Permalink
Switch to WASI
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuowei committed May 10, 2019
1 parent 2961563 commit a361bba
Show file tree
Hide file tree
Showing 16 changed files with 21 additions and 61 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,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 "")
Expand Down
17 changes: 1 addition & 16 deletions cmake/modules/AddSwift.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,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
Expand Down Expand Up @@ -316,12 +315,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})
Expand Down Expand Up @@ -376,11 +369,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)
Expand Down Expand Up @@ -493,10 +481,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
Expand Down
13 changes: 2 additions & 11 deletions cmake/modules/SwiftConfigureSDK.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -36,14 +35,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}")
Expand Down Expand Up @@ -247,8 +238,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}")
Expand Down
16 changes: 0 additions & 16 deletions cmake/modules/SwiftWasmSupport.cmake

This file was deleted.

2 changes: 1 addition & 1 deletion include/swift/Runtime/Mutex.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

#include <type_traits>

#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"
Expand Down
2 changes: 1 addition & 1 deletion include/swift/Runtime/MutexPThread.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/Driver/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ Driver::buildToolChain(const llvm::opt::InputArgList &ArgList) {
case llvm::Triple::Haiku:
return llvm::make_unique<toolchains::GenericUnix>(*this, target);
case llvm::Triple::UnknownOS:
// newer LLVM actually has an OS value for Emscripten; should we use it?
// WebAssembly: hack: WASI isn't defined in this LLVM version yet
if (target.isOSBinFormatWasm())
return llvm::make_unique<toolchains::GenericUnix>(*this, target);
LLVM_FALLTHROUGH;
Expand Down
4 changes: 2 additions & 2 deletions stdlib/public/SwiftShims/LibcShims.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 __ANDROID_API__ >= 17
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/runtime/Errors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
//
//===----------------------------------------------------------------------===//

#if defined(__CYGWIN__) || defined(__ANDROID__) || defined(__HAIKU__) || defined(__EMSCRIPTEN__)
#if defined(__CYGWIN__) || defined(__ANDROID__) || defined(__HAIKU__) || defined(__wasi__)
#define SWIFT_SUPPORTS_BACKTRACE_REPORTING 0
#else
#define SWIFT_SUPPORTS_BACKTRACE_REPORTING 1
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/runtime/Heap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/runtime/ThreadLocalStorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,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;
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/stubs/LibcShims.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

#include <stdio.h>
#include <sys/types.h>
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) || defined(__EMSCRIPTEN__)
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) || defined(__wasi__)
#include <unistd.h>
#endif

Expand Down
6 changes: 3 additions & 3 deletions utils/build-script
Original file line number Diff line number Diff line change
Expand Up @@ -345,14 +345,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 "
Expand Down Expand Up @@ -777,7 +777,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,
Expand Down
4 changes: 2 additions & 2 deletions utils/build-script-impl
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ KNOWN_SETTINGS=(
android-icu-data "" "Path to libicudata.so"
android-deploy-device-path "" "Path on an Android device to which built Swift stdlib products will be deployed"
android-arch "armv7" "The Android target architecture when building for Android"
wasm-emscripten "" "An absolute path to the Emscripten that will be used as a libc implementation for Wasm builds"
wasm-wasi-sdk "" "An absolute path to the WASI SDK that will be used as a libc implementation for Wasm builds"
wasm-icu-uc "" "Path to libicuuc.so"
wasm-icu-uc-include "" "Path to a directory containing headers for libicuuc"
wasm-icu-i18n "" "Path to libicui18n.so"
Expand Down Expand Up @@ -2292,7 +2292,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}"
Expand Down
4 changes: 2 additions & 2 deletions utils/build_swift/driver_arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -1002,8 +1002,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,
Expand Down
2 changes: 1 addition & 1 deletion vvv.sh
Original file line number Diff line number Diff line change
@@ -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" \
Expand Down

0 comments on commit a361bba

Please sign in to comment.