Skip to content

Build Foundation and add it to the distribution #1000

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 26, 2020

Conversation

MaxDesiatov
Copy link

@MaxDesiatov MaxDesiatov commented May 14, 2020

Bits of the dependency installation scripts were refactored to make the local development a bit easier.

Overall, Foundation is not fully working yet due to the OOB bug, and to link with it you need build with SwiftPM with swift build --destination destination.json where destination.json looks like this on Linux with root path (because of absolute paths, the destination file has to be adapted for every swiftenv installation, I think this should be automated in the future):

{
  "version": 1,
  "target": "wasm32-unknown-wasi",
  "sdk": "/usr/share/wasi-sysroot",
  "toolchain-bin-dir": "/usr/bin/",
  "extra-cc-flags": [],
  "extra-swiftc-flags": [
    "-I", "/usr/lib/swift/wasi/wasm32",
    "-Xlinker", "-lCoreFoundation",
    "-Xlinker", "-lBlocksRuntime",
    "-Xlinker", "-licui18n",
    "-Xlinker", "-luuid"
  ],
  "extra-cpp-flags": []
}

The reasoning for shipping Foundation anyway in the nightly distribution is to provide an easier way to reproduce the bug. And even after the bug is fixed, I think the Foundation build/installation steps would stay the same, at least as long as we continue to use build-script.

Related to #592.
Closes #658.

@MaxDesiatov MaxDesiatov added the continuous integration Improvements to continuous integration label May 14, 2020
MaxDesiatov added a commit that referenced this pull request May 20, 2020
Build host toolchain w/o copying it from snapshots

Currently the host toolchain is copied over to the target WASI toolchain from snapshots downloaded from [swift.org](https://swift.org/download/#snapshots). This is not ideal as snapshots on swift.org are not always fresh and don't always match what we have in the `swiftwasm` branch or even in the upstream `master` branch. This causes issues similar to what can be seen in #1000, where the host toolchain and SDK don't match the target toolchain with this error:

```
error: compiled module was created by an older version of the compiler; 
rebuild 'Swift' and try again: 
/usr/lib/swift/macosx/Swift.swiftmodule/x86_64-apple-macos.swiftmodule
```

The solution is to:
1) build the host toolchain together with the host SDK and SwiftPM as the first part of the CI process
2) install the host products into the `$SOURCE_PATH/host-toolchain-sdk` directory
3) clean up the build directory
4) build the target SwiftWasm SDK
5) install the target SwiftWasm SDK into the `$SOURCE_PATH/install` directory
6) copy over WASI SDK clang/llvm-ar/wasm-ld into the `$SOURCE_PATH/host-toolchain-sdk` directory
6) copy over the target SwiftWasm SDK into the `$SOURCE_PATH/host-toolchain-sdk` directory
7) create an archive from the `$SOURCE_PATH/host-toolchain-sdk` directory

Thus `build-swiftpm.sh` script is no longer needed as SwiftPM is already compiled as a part of the host toolchain, which also avoids similar issues where SwiftPM from upstream snapshots can also be out of sync from what we build here on CI.
@MaxDesiatov MaxDesiatov force-pushed the maxd/build-foundation branch 3 times, most recently from c3e3cc1 to 86bca51 Compare May 21, 2020 13:17
@MaxDesiatov MaxDesiatov force-pushed the maxd/build-foundation branch 4 times, most recently from a110d25 to cbf0614 Compare May 25, 2020 16:34
@MaxDesiatov MaxDesiatov force-pushed the maxd/build-foundation branch from cbf0614 to 688b6aa Compare May 25, 2020 19:49
@MaxDesiatov MaxDesiatov marked this pull request as ready for review May 25, 2020 21:18
@kateinoigakukun
Copy link
Member

Awesome!
I also succeed to compile using Foundation.

The produced binary is 26MB 😅 (If link only stdlib, it's 11MB)

$ cat main.swift
import Foundation

var str = "Hello, world!"
str = str.replacingOccurrences(of: "world", with: "SwiftWasm")
print(str)

$ swiftc main.swift -target wasm32-unknown-wasi \
   -sdk /Users/kateinoigakukun/.swiftenv/versions/wasm-DEVELOPMENT-SNAPSHOT-2020-05-25-a/usr/share/wasi-sysroot \
   -I /Users/kateinoigakukun/.swiftenv/versions/wasm-DEVELOPMENT-SNAPSHOT-2020-05-25-a/usr/lib/swift/wasi/wasm32 \
   -Xlinker -lCoreFoundation \
   -Xlinker -lBlocksRuntime \
   -Xlinker -luuid \
   -Xlinker -licui18n

@kateinoigakukun kateinoigakukun merged commit 1e8e77a into swiftwasm May 26, 2020
@kateinoigakukun kateinoigakukun deleted the maxd/build-foundation branch September 9, 2020 08:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
continuous integration Improvements to continuous integration
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Investigate if supporting BlocksRuntime is viable
2 participants