Skip to content
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

Automatically create destination JSON to allow linking Foundation #4

Closed
MaxDesiatov opened this issue Jun 9, 2020 · 20 comments · Fixed by #41
Closed

Automatically create destination JSON to allow linking Foundation #4

MaxDesiatov opened this issue Jun 9, 2020 · 20 comments · Fixed by #41
Labels
bug Something isn't working

Comments

@MaxDesiatov
Copy link
Collaborator

Currently Foundation can't be linked even when installed due to autolinking issues. Supplying an explicit destination JSON file to swift build should resolve the issue in the meantime.

@MaxDesiatov MaxDesiatov added the bug Something isn't working label Jun 9, 2020
@ratranqu
Copy link
Contributor

ratranqu commented Jul 1, 2020

@MaxDesiatov, first, thank for the hard work you are putting in SwiftWasm/*. What you are in the process of building is AWESOME.

I'm spending a bit of time trying it out with some of my libs to see what can be compile and what not and I am getting into the missing Foundation problem. So I crafted the following destination file:

{
  "version": 1,
  "sdk": "/Users/alex/.swiftenv/versions/wasm-DEVELOPMENT-SNAPSHOT-2020-06-12-a/usr/share/wasi-sysroot",
  "toolchain-bin-dir": "/Users/alex/.swiftenv/versions/wasm-DEVELOPMENT-SNAPSHOT-2020-06-12-a/usr/bin",
  "target": "wasm32-unknown-wasi",
  "extra-cc-flags": [
    ""
  ],
  "extra-cpp-flags": [
    ""
  ],
  "extra-swiftc-flags": [
    ""
  ]
}

and I am adding it as a flag to my swift build command, but I still get the missing required module 'CoreFoundation' error.

Do you have any pointers as to what I am doing wrong?

Thank!

PS:
I am using carton 0.2.0 on MacOS Catalina and I downloaded wasm-DEVELOPMENT-SNAPSHOT-2020-06-12-a with swiftenv.

@MaxDesiatov MaxDesiatov pinned this issue Jul 1, 2020
@MaxDesiatov
Copy link
Collaborator Author

MaxDesiatov commented Jul 1, 2020

Hi @ratranqu, thank you very much for your kind words!

You're missing a few flags here. Sorry about that, I should've documented it, and this issue is on my list to resolve very soon. In the meantime, here's how it should look:

{
  "version": 1,
  "sdk": "/Users/alex/.swiftenv/versions/wasm-DEVELOPMENT-SNAPSHOT-2020-06-12-a/usr/share/wasi-sysroot",
  "toolchain-bin-dir": "/Users/alex/.swiftenv/versions/wasm-DEVELOPMENT-SNAPSHOT-2020-06-12-a/usr/bin",
  "target": "wasm32-unknown-wasi",
  "extra-cc-flags": [
    ""
  ],
  "extra-cpp-flags": [
    ""
  ],
  "extra-swiftc-flags": [
    "-I", "/Users/alex/.swiftenv/versions/wasm-DEVELOPMENT-SNAPSHOT-2020-06-12-a/usr/lib/swift/wasi/wasm32",
    "-Xlinker", "-lCoreFoundation",
    "-Xlinker", "-lBlocksRuntime",
    "-Xlinker", "-licui18n",
    "-Xlinker", "-luuid"
  ]
}

Does that allow you to get rid of the "missing required module" error?

@ratranqu
Copy link
Contributor

ratranqu commented Jul 1, 2020

Yes! it solves the problem. Thx much! Now, I have issues with the building of CCryptoBoringSSL (I am linking to a lib using apple/swift-crypto), but I guess I need to add a few c flags to the destination.json file.

@MaxDesiatov
Copy link
Collaborator Author

MaxDesiatov commented Jul 1, 2020

Yes, I hope that passing additional flags is going to work, but on the other hand SwiftCrypto is a pretty complex project, some other issues could come up. Please post here about your experience if you can, that could be useful to other people. And if there are any bugs in either carton or SwiftWasm that prevent it from being built, those certainly should be fixed.

@ratranqu
Copy link
Contributor

ratranqu commented Jul 2, 2020

Will do. In the meantime, I have done the following:

  • moved to CryptoSwift for crypto primitives, and it allows compilation. However, when loading the wasm in the browser, I hit some sort of size limit (the main.wasm file is about 35 MB):

image

I need to clean a few things up before providing some code to reproduce the error, but in the meantime, I am open for any suggestions.
  • I have added a flag to carton dev so that you can call it as such: carton dev --destination destination.json. I've also updated the README.md to explain a bit. I've done this in my own fork, and if you feel that it's ok as a step to providing a fully automated destination.json file, I will send you a PR.

@MaxDesiatov
Copy link
Collaborator Author

Awesome! The issue with the number of locals is apparently caused by the lack of optimizations applied to the final binary. Did you try building it in the release mode with swift build -c release --destination destination.json?

I'd be happy to review the PR, please go ahead 👍

@ratranqu
Copy link
Contributor

ratranqu commented Jul 2, 2020

Interestingly, building with Foundation in release mode (for instance just add import Foundation in the TokamakDemo project) breaks as follows:

Arcadia:Tokamak $ ../carton/.build/debug/carton dev --destination destination.json --release
Polyfill integrity verified: /Users/alex/.carton/static/dev.js
Inferring basic settings...
- swift executable: /Users/alex/.swiftenv/versions/wasm-DEVELOPMENT-SNAPSHOT-2020-06-12-a/usr/bin/swift
Swift version 5.3-dev (LLVM b54b6b832e, Swift 4bb1458ec4)
Target: x86_64-apple-darwin19.5.0
Parsing package manifest: /Users/alex/.swiftenv/versions/wasm-DEVELOPMENT-SNAPSHOT-2020-06-12-a/usr/bin/swift package dump-package
- development binary to serve: /Users/alex/Documents/dev/wasm/Tokamak/.build/wasm32-unknown-wasi/debug/TokamakDemo

Building the project before spinning up a server...
/Users/alex/.swiftenv/versions/wasm-DEVELOPMENT-SNAPSHOT-2020-06-12-a/usr/bin/swift build -c release --triple wasm32-unknown-wasi --product TokamakDemo --destination destination.json
Assertion failed: ((Args.size() == FTy->getNumParams() || (FTy->isVarArg() && Args.size() > FTy->getNumParams())) && "Calling a function with bad signature!"), function init, file /Users/runner/runners/2.263.0/work/swift/llvm-project/llvm/lib/IR/Instructions.cpp, line 395.
Stack dump:
0.	Program arguments: /Users/alex/.swiftenv/versions/wasm-DEVELOPMENT-SNAPSHOT-2020-06-12-a/usr/bin/swift -frontend -c /Users/alex/Documents/dev/wasm/Tokamak/Sources/TokamakCore/Binding.swift /Users/alex/Documents/dev/wasm/Tokamak/Sources/TokamakCore/CGStubs.swift /Users/alex/Documents/dev/wasm/Tokamak/Sources/TokamakCore/Default.swift /Users/alex/Documents/dev/wasm/Tokamak/Sources/TokamakCore/Environment/Environment.swift /Users/alex/Documents/dev/wasm/Tokamak/Sources/TokamakCore/Environment/EnvironmentKey.swift /Users/alex/Documents/dev/wasm/Tokamak/Sources/TokamakCore/Environment/EnvironmentValues.swift /Users/alex/Documents/dev/wasm/Tokamak/Sources/TokamakCore/Modifiers/Effects/ClipEffect.swift /Users/alex/Documents/dev/wasm/Tokamak/Sources/TokamakCore/Modifiers/FlexFrameLayout.swift /Users/alex/Documents/dev/wasm/Tokamak/Sources/TokamakCore/Modifiers/FrameLayout.swift /Users/alex/Documents/dev/wasm/Tokamak/Sources/TokamakCore/Modifiers/ModifiedContent.swift /Users/alex/Documents/dev/wasm/Tokamak/Sources/TokamakCore/Modifiers/PaddingLayout.swift /Users/alex/Documents/dev/wasm/Tokamak/Sources/TokamakCore/Modifiers/StyleModifiers.swift /Users/alex/Documents/dev/wasm/Tokamak/Sources/TokamakCore/Modifiers/ViewModifier.swift /Users/alex/Documents/dev/wasm/Tokamak/Sources/TokamakCore/Modifiers/zIndex.swift /Users/alex/Documents/dev/wasm/Tokamak/Sources/TokamakCore/MountedComponents/MountedCompositeView.swift /Users/alex/Documents/dev/wasm/Tokamak/Sources/TokamakCore/MountedComponents/MountedHostView.swift /Users/alex/Documents/dev/wasm/Tokamak/Sources/TokamakCore/MountedComponents/MountedNull.swift /Users/alex/Documents/dev/wasm/Tokamak/Sources/TokamakCore/MountedComponents/MountedView.swift /Users/alex/Documents/dev/wasm/Tokamak/Sources/TokamakCore/Null.swift /Users/alex/Documents/dev/wasm/Tokamak/Sources/TokamakCore/Renderer.swift /Users/alex/Documents/dev/wasm/Tokamak/Sources/TokamakCore/Shapes/Ellipse.swift /Users/alex/Documents/dev/wasm/Tokamak/Sources/TokamakCore/Shapes/ModifiedShapes.swift /Users/alex/Documents/dev/wasm/Tokamak/Sources/TokamakCore/Shapes/Path.swift /Users/alex/Documents/dev/wasm/Tokamak/Sources/TokamakCore/Shapes/Rectangle.swift /Users/alex/Documents/dev/wasm/Tokamak/Sources/TokamakCore/Shapes/Shape.swift /Users/alex/Documents/dev/wasm/Tokamak/Sources/TokamakCore/Shapes/ShapeModifiers.swift /Users/alex/Documents/dev/wasm/Tokamak/Sources/TokamakCore/StackReconciler.swift /Users/alex/Documents/dev/wasm/Tokamak/Sources/TokamakCore/State.swift /Users/alex/Documents/dev/wasm/Tokamak/Sources/TokamakCore/Target.swift /Users/alex/Documents/dev/wasm/Tokamak/Sources/TokamakCore/Tokens/Angle.swift /Users/alex/Documents/dev/wasm/Tokamak/Sources/TokamakCore/Tokens/Axis.swift /Users/alex/Documents/dev/wasm/Tokamak/Sources/TokamakCore/Tokens/Color.swift /Users/alex/Documents/dev/wasm/Tokamak/Sources/TokamakCore/Tokens/Edge.swift /Users/alex/Documents/dev/wasm/Tokamak/Sources/TokamakCore/Tokens/Font.swift /Users/alex/Documents/dev/wasm/Tokamak/Sources/TokamakCore/Tokens/LineBreakMode.swift /Users/alex/Documents/dev/wasm/Tokamak/Sources/TokamakCore/Tokens/TextAlignment.swift /Users/alex/Documents/dev/wasm/Tokamak/Sources/TokamakCore/Tokens/UnitPoint.swift /Users/alex/Documents/dev/wasm/Tokamak/Sources/TokamakCore/Views/AnyView.swift /Users/alex/Documents/dev/wasm/Tokamak/Sources/TokamakCore/Views/Button.swift /Users/alex/Documents/dev/wasm/Tokamak/Sources/TokamakCore/Views/ForEach.swift /Users/alex/Documents/dev/wasm/Tokamak/Sources/TokamakCore/Views/HStack.swift /Users/alex/Documents/dev/wasm/Tokamak/Sources/TokamakCore/Views/ScrollView.swift /Users/alex/Documents/dev/wasm/Tokamak/Sources/TokamakCore/Views/SecureField.swift /Users/alex/acer.swift /Users/alex/Documents/dev/wasm/Tokamak/Sources/TokamakCore/Views/Text.swift /Users/alex/Documents/dev/wasm/Tokamak/Sources/TokamakCore/Views/TextField.swift /Users/alex/Documents/dev/wasm/Tokamak/Sources/TokamakCore/Views/TupleView.swift /Users/alex/Documents/dev/wasm/Tokamak/Sources/TokamakCore/Views/VStack.swift /Users/alex/Documents/dev/wasm/Tokamak/Sources/TokamakCore/Views/View.swift /Users/alex/Documents/dev/wasm/Tokamak/Sources/TokamakCore/Views/ViewBuilder.swift /Users/alex/Documents/dev/wasm/Tokamak/Sources/TokamakCore/Views/ZStack.swift -supplementary-output-file-map /var/folders/f5/4r6myvhn0dl1stfm524q64t80000gn/T/supplementaryOutputs-976673 -target wasm32-unknown-wasi -disable-objc-interop -sdk /Users/alex/.swiftenv/versions/wasm-DEVELOPMENT-SNAPSHOT-2020-06-12-a/usr/share/wasi-sysroot -I /Users/alex/Documents/dev/wasm/Tokamak/.build/wasm32-unknown-wasi/release -I /Users/alex/.swiftenv/versions/wasm-DEVELOPMENT-SNAPSHOT-2020-06-12-a/usr/lib/swift/wasi/wasm32 -I /Users/alex/Documents/dev/wasm/Tokamak/.build/checkouts/Runtime/Sources/CRuntime/include -g -module-cache-path /Users/alex/Documents/dev/wasm/Tokamak/.build/wasm32-unknown-wasi/release/ModuleCache -swift-version 5 -O -D SWIFT_PACKAGE -Xcc -fmodule-map-file=/Users/alex/Documents/dev/wasm/Tokamak/.build/checkouts/Runtime/Sources/CRuntime/include/module.modulemap -parse-as-library -module-name TokamakCore -num-threads 8 -o /Users/alex/Documents/dev/wasm/Tokamak/.build/wasm32-unknown-wasi/release/TokamakCore.build/Binding.swift.o -o /Users/alex/Documents/dev/wasm/Tokamak/.build/wasm32-unknown-wasi/release/TokamakCore.build/CGStubs.swift.o -o /Users/alex/Documents/dev/wasm/Tokamak/.build/wasm32-unknown-wasi/release/TokamakCore.build/Default.swift.o -o /Users/alex/Documents/dev/wasm/Tokamak/.build/wasm32-unknown-wasi/release/TokamakCore.build/Environment/Environment.swift.o -o /Users/alex/Documents/dev/wasm/Tokamak/.build/wasm32-unknown-wasi/release/TokamakCore.build/Environment/EnvironmentKey.swift.o -o /Users/alex/Documents/dev/wasm/Tokamak/.build/wasm32-unknown-wasi/release/TokamakCore.build/Environment/EnvironmentValues.swift.o -o /Users/alex/Documents/dev/wasm/Tokamak/.build/wasm32-unknown-wasi/release/TokamakCore.build/Modifiers/Effects/ClipEffect.swift.o -o /Users/alex/Documents/dev/wasm/Tokamak/.build/wasm32-unknown-wasi/release/TokamakCore.build/Modifiers/FlexFrameLayout.swift.o -o /Users/alex/Documents/dev/wasm/Tokamak/.build/wasm32-unknown-wasi/release/TokamakCore.build/Modifiers/FrameLayout.swift.o -o /Users/alex/Documents/dev/wasm/Tokamak/.build/wasm32-unknown-wasi/release/TokamakCore.build/Modifiers/ModifiedContent.swift.o -o /Users/alex/Documents/dev/wasm/Tokamak/.build/wasm32-unknown-wasi/release/TokamakCore.build/Modifiers/PaddingLayout.swift.o -o /Users/alex/Documents/dev/wasm/Tokamak/.build/wasm32-unknown-wasi/release/TokamakCore.build/Modifiers/StyleModifiers.swift.o -o /Users/alex/Documents/dev/wasm/Tokamak/.build/wasm32-unknown-wasi/release/TokamakCore.build/Modifiers/ViewModifier.swift.o -o /Users/alex/Documents/dev/wasm/Tokamak/.build/wasm32-unknown-wasi/release/TokamakCore.build/Modifiers/zIndex.swift.o -o /Users/alex/Documents/dev/wasm/Tokamak/.build/wasm32-unknown-wasi/release/TokamakCore.build/MountedComponents/MountedCompositeView.swift.o -o /Users/alex/Documents/dev/wasm/Tokamak/.build/wasm32-unknown-wasi/release/TokamakCore.build/MountedComponents/MountedHostView.swift.o -o /Users/alex/Documents/dev/wasm/Tokamak/.build/wasm32-unknown-wasi/release/TokamakCore.build/MountedComponents/MountedNull.swift.o -o /Users/alex/Documents/dev/wasm/Tokamak/.build/wasm32-unknown-wasi/release/TokamakCore.build/MountedComponents/MountedView.swift.o -o /Users/alex/Documents/dev/wasm/Tokamak/.build/wasm32-unknown-wasi/release/TokamakCore.build/Null.swift.o -o /Users/alex/Documents/dev/wasm/Tokamak/.build/wasm32-unknown-wasi/release/TokamakCore.build/Renderer.swift.o -o /Users/alex/Documents/dev/wasm/Tokamak/.build/wasm32-unknown-wasi/release/TokamakCore.build/Shapes/Ellipse.swift.o -o /ments/dev/wasm/Tokamak/.build/wasm32-unknown-wasi/release/TokamakCore.build/Shapes/ModifiedShapes.swift.o -o /Users/alex/Documents/dev/wasm/Tokamak/.build/wasm32-unknown-wasi/release/TokamakCore.build/Shapes/Path.swift.o -o /Users/alex/Documents/dev/wasm/Tokamak/.build/wasm32-unknown-wasi/release/TokamakCore.build/Shapes/Rectangle.swift.o -o /Users/alex/Documents/dev/wasm/Tokamak/.build/wasm32-unknown-wasi/release/TokamakCore.build/Shapes/Shape.swift.o -o /Users/alex/Documents/dev/wasm/Tokamak/.build/wasm32-unknown-wasi/release/TokamakCore.build/Shapes/ShapeModifiers.swift.o -o /Users/alex/Documents/dev/wasm/Tokamak/.build/wasm32-unknown-wasi/release/TokamakCore.build/StackReconciler.swift.o -o /Users/alex/Documents/dev/wasm/Tokamak/.build/wasm32-unknown-wasi/release/TokamakCore.build/State.swift.o -o /Users/alex/Documents/dev/wasm/Tokamak/.build/wasm32-unknown-wasi/release/TokamakCore.build/Target.swift.o -o /Users/alex/Documents/dev/wasm/Tokamak/.build/wasm32-unknown-wasi/release/TokamakCore.build/Tokens/Angle.swift.o -o /Users/alex/Documents/dev/wasm/Tokamak/.build/wasm32-unknown-wasi/release/TokamakCore.build/Tokens/Axis.swift.o -o /Users/alex/Documents/dev/wasm/Tokamak/.build/wasm32-unknown-wasi/release/TokamakCore.build/Tokens/Color.swift.o -o /Users/alex/Documents/dev/wasm/Tokamak/.build/wasm32-unknown-wasi/release/TokamakCore.build/Tokens/Edge.swift.o -o /Users/alex/Documents/dev/wasm/Tokamak/.build/wasm32-unknown-wasi/release/TokamakCore.build/Tokens/Font.swift.o -o /Users/alex/Documents/dev/wasm/Tokamak/.build/wasm32-unknown-wasi/release/TokamakCore.build/Tokens/LineBreakMode.swift.o -o /Users/alex/Documents/dev/wasm/Tokamak/.build/wasm32-unknown-wasi/release/TokamakCore.build/Tokens/TextAlignment.swift.o -o /Users/alex/Documents/dev/wasm/Tokamak/.build/wasm32-unknown-wasi/release/TokamakCore.build/Tokens/UnitPoint.swift.o -o /Users/alex/Documents/dev/wasm/Tokamak/.build/wasm32-unknown-wasi/release/TokamakCore.build/Views/AnyView.swift.o -o /Users/alex/Documents/dev/wasm/Tokamak/.build/wasm32-unknown-wasi/release/TokamakCore.build/Views/Button.swift.o -o /Users/alex/Documents/dev/wasm/Tokamak/.build/wasm32-unknown-wasi/release/TokamakCore.build/Views/ForEach.swift.o -o /Users/alex/Documents/dev/wasm/Tokamak/.build/wasm32-unknown-wasi/release/TokamakCore.build/Views/HStack.swift.o -o /Users/alex/Documents/dev/wasm/Tokamak/.build/wasm32-unknown-wasi/release/TokamakCore.build/Views/ScrollView.swift.o -o /Users/alex/Documents/dev/wasm/Tokamak/.build/wasm32-unknown-wasi/release/TokamakCore.build/Views/SecureField.swift.o -o /Users/alex/Documents/dev/wasm/Tokamak/.build/wasm32-unknown-wasi/release/TokamakCore.build/Views/Spacer.swift.o -o /Users/alex/Documents/dev/wasm/Tokamak/.build/wasm32-unknown-wasi/release/TokamakCore.build/Views/Text.swift.o -o /Users/alex/Documents/dev/wasm/Tokamak/.build/wasm32-unknown-wasi/release/TokamakCore.build/Views/TextField.swift.o -o /Users/alex/Documents/dev/wasm/Tokamak/.build/wasm32-unknown-wasi/release/TokamakCore.build/Views/TupleView.swift.o -o /Users/alex/Documents/dev/wasm/Tokamak/.build/wasm32-unknown-wasi/release/TokamakCore.build/Views/VStack.swift.o -o /Users/alex/Documents/dev/wasm/Tokamak/.build/wasm32-unknown-wasi/release/TokamakCore.build/Views/View.swift.o -o /Users/alex/Documents/dev/wasm/Tokamak/.build/wasm32-unknown-wasi/release/TokamakCore.build/Views/ViewBuilder.swift.o -o /Users/alex/Documents/dev/wasm/Tokamak/.build/wasm32-unknown-wasi/release/TokamakCore.build/Views/ZStack.swift.o
1.	Swift version 5.3-dev (LLVM b54b6b832e, Swift 4bb1458ec4)
2.	While evaluating request IRGenWholeModuleRequest(IR Generation for module TokamakCore)
3.	While emitting IR SIL function "@$s11TokamakCore4ViewPAAE4fontyQrAA4FontVSgF".
 for 'font(_:)' (at /Users/alex/Documents/dev/wasm/Tokamak/Sources/TokamakCore/Tokens/Font.swift:165:3)
0  swift                    0x00000001096fc7b8 llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 40
1  swift                    0x00000001096fb738 llvm::sys::RunSignalHandlers() + 248
096fcdad SignalHandler(int) + 285
3  libsystem_platform.dylib 0x00007fff6d5d45fd _sigtramp + 29
4  libsystem_platform.dylib 0x0000000000001110 _sigtramp + 18446603338681338672
5  libsystem_c.dylib        0x00007fff6d4aa808 abort + 120
6  libsystem_c.dylib        0x00007fff6d4a9ac6 err + 0
7  swift                    0x000000010a889143 llvm::CallInst::init(llvm::FunctionType*, llvm::Value*, llvm::ArrayRef<llvm::Value*>, llvm::ArrayRef<llvm::OperandBundleDefT<llvm::Value*> >, llvm::Twine const&) (.cold.4) + 35
8  swift                    0x0000000109566b2f llvm::CallInst::init(llvm::FunctionType*, llvm::Value*, llvm::ArrayRef<llvm::Value*>, llvm::ArrayRef<llvm::OperandBundleDefT<llvm::Value*> >, llvm::Twine const&) + 447
9  swift                    0x0000000108f48c4f llvm::CallInst::Create(llvm::FunctionType*, llvm::Value*, llvm::ArrayRef<llvm::Value*>, llvm::ArrayRef<llvm::OperandBundleDefT<llvm::Value*> >, llvm::Twine const&, llvm::Instruction*) + 319
10 swift                    0x0000000106d01255 llvm::IRBuilder<llvm::ConstantFolder, llvm::IRBuilderDefaultInserter>::CreateCall(llvm::FunctionType*, llvm::Value*, llvm::ArrayRef<llvm::Value*>, llvm::ArrayRef<llvm::OperandBundleDefT<llvm::Value*> >, llvm::Twine const&, llvm::MDNode*) + 69
11 swift                    0x0000000105a40b83 swift::irgen::IRBuilder::CreateCall(swift::irgen::FunctionPointer const&, llvm::ArrayRef<llvm::Value*>) + 307
12 swift                    0x0000000105ae95d1 getAccessorForComputedComponent(swift::irgen::IRGenModule&, swift::KeyPathPatternComponent const&, KeyPathAccessor, swift::GenericEnvironment*, llvm::ArrayRef<swift::irgen::GenericRequirement>, bool) + 1665
13 swift                    0x0000000105ae591f emitKeyPathComponent(swift::irgen::IRGenModule&, swift::irgen::ConstantStructBuilder&, swift::KeyPathPatternComponent const&, bool, swift::GenericEnvironment*, llvm::ArrayRef<swift::irgen::GenericRequirement>, swift::CanType, llvm::ArrayRef<KeyPathIndexOperand>, bool) + 6879
14 swift                    0x0000000105ae397b swift::irgen::IRGenModule::getAddrOfKeyPathPattern(swift::KeyPathPattern*, swift::SILLocation) + 1803
15 swift                    0x0000000105b85e01 swift::SILInstructionVisitor<(anonymous namespace)::IRGenSILFunction, void>::visit(swift::SILInstruction*) + 19985
16 swift                    0x0000000105b7d4bf swift::irgen::IRGenModule::emitSILFunction(swift::SILFunction*) + 7199
17 swift                    0x0000000105a7d3e2 swift::irgen::IRGenerator::emitGlobalTopLevel(llvm::StringSet<llvm::MallocAllocator>*) + 898
18 swift                    0x0000000105b52f71 swift::IRGenWholeModuleRequest::evaluate(swift::Evaluator&, swift::IRGenDescriptor) const + 993
19 swift                    0x0000000105b7b895 swift::SimpleRequest<swift::IRGenWholeModuleRequest, swift::GeneratedModule (swift::IRGenDescriptor), (swift::RequestFlags)1>::evaluateRequest(swift::IRGenWholeModuleRequest const&, swift::Evaluator&) + 37
20 swift                    0x0000000105b57603 llvm::Expected<swift::IRGenWholeModuleRequest::OutputType> swift::Evaluator::getResultUncached<swift::IRGenWholeModuleRequest>(swift::IRGenWholeModuleRequest const&) + 451
21 swift                    0x0000000105b529b5 swift::performIRGeneration(swift::IRGenOptions const&, swift::ModuleDecl*, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> >, llvm::StringRef, swift::PrimarySpecificPaths const&, llvm::ArrayRef<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, llvm::GlobalVariable**, llvm::StringSet<llvm::MallocAllocator>*) + 277
22 swift                    0x00000001058d1385 performCompileStepsPostSILGen(swift::CompilerInstance&, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> >, llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>, swift::PrimarySpecificPaths const&, int&, swift::FrontendObserver*) + 2277
23 swift                    0x00000001058c46e6 performCompile(swift::CompilerInstance&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*) + 7926
                    0x00000001058c17f5 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 3397
25 swift                    0x000000010585bc3d main + 861
26 libdyld.dylib            0x00007fff6d3dbcc9 start + 1
27 libdyld.dylib            0x00000000000000b8 start + 18446603338683401200Build failed, check the build process output above.

Watching this directory for changes: /Users/alex/Documents/dev/wasm/Tokamak/Sources

[ NOTICE ] Server starting on http://127.0.0.1:8080
[ INFO ] GET /watcher
[ INFO ] GET /watcher

To achieve this, I added a --release flag to carton dev, and also sent a PR for that :)

@MaxDesiatov
Copy link
Collaborator Author

Very interesting, I'll have to investigate that more closely, looks like a bug in SwiftWasm. Thanks for reporting it!

I'm currently trying to fix the most recent SwiftWasm development snapshots, and also more stable 5.3 snapshots, will let you know once those are ready to retest with those.

@MaxDesiatov
Copy link
Collaborator Author

Destination files are now automatically generated for each toolchain with carton dev in the main branch. You still can pass a --destination option with a path to your custom destination file, but it's no longer needed to link with Foundation. What's nice is that Foundation is not actually linked if you don't use it, so there's no downside to always building with destination files as we now do.

@MaxDesiatov
Copy link
Collaborator Author

This also unblocks further work on carton test and proper XCTest support! ᕕ( ᐛ )ᕗ

@MaxDesiatov MaxDesiatov unpinned this issue Jul 13, 2020
@ephemer
Copy link
Contributor

ephemer commented Aug 16, 2022

Hi @MaxDesiatov, I think I'm running into an issue with this: https://forums.swift.org/t/how-to-disable-implicit-foundation-imports/59678/4

I am desperately trying to stop Foundation from being linked because it is ballooning the bundle size. By writing some seemingly innocuous code like myString.trimmingCharacters(in: .whitespace) in a random file somewhere, Foundation is linked and the bundle size balloons by 800% even though none of our files contain an import Foundation statement.

I would really like to disable this behaviour – can you point me towards how I could do that? Do I need to create a custom destination.json that doesn't contain -lFoundation etc.?

@MaxDesiatov
Copy link
Collaborator Author

I don't think we're linking Foundation anywhere explicitly anymore, it's on done on the compiler side, specifically the driver I guess.

cc @kateinoigakukun

@ephemer
Copy link
Contributor

ephemer commented Aug 16, 2022

@MaxDesiatov this code is still in main – is that code stale and no longer used perhaps? Or is it possible that those linker flags are still being used after all?

edit: sorry, maybe that's not correct? GitHub was claiming the PR is a part of main and 0.16 but looking at main directly I can't see that code any more

@MaxDesiatov
Copy link
Collaborator Author

Ah, that's correct, thanks for looking it up. But we needed those flags for the code that does use Foundation to link. Now these flags are passed automatically, on the compiler driver side I guess, will need to investigate to confirm if needed. In my understanding, if your code does not reference any symbols in Foundation, it won't be linked even when -lFoundation is passed.

This still means that it can be linked when your code relies on Foundation transitively. We just need to understand what that code is, and why it doesn't require import Foundation to be present. If you provided some self-contained sample code, that would greatly help us in our investigation. Thanks!

@ephemer
Copy link
Contributor

ephemer commented Aug 16, 2022

I can reproduce this really easily, but interestingly only with JavaScriptKit right now. I will continue investigating a bit further but the following is super simple:

$ mkdir test
$ carton init

change Sources/test/main.swift to look like this:

import JavaScriptKit

print("Hello, world!".trimmingCharacters(in: .whitespaces))

-> Foundation is linked

Without the import JavaScriptKit statement, compilation fails (as it should)

edit: this is crazy though, because the entire JavaScriptKit repo does not contain the word Foundation anywhere

@ephemer
Copy link
Contributor

ephemer commented Aug 16, 2022

@MaxDesiatov I played around with some alternatives here, and it does seem to have something to do with JavaScriptKit, although I can't see why.

import AnotherTarget
import Numerics

print(fromAnotherTarget);
print("Hello, world!".trimmingCharacters(in: .whitespaces))

fails with the error

...test/Sources/foundation-test/main.swift:5:23: error: value of type 'String' has no member 'trimmingCharacters'
print("Hello, world!".trimmingCharacters(in: .whitespaces))
      ~~~~~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~
...test/Sources/foundation-test/main.swift:5:47: error: cannot infer contextual base in reference to member 'whitespaces'
print("Hello, world!".trimmingCharacters(in: .whitespaces))
                                             ~^~~~~~~~~~~

as soon as I add import JavaScriptKit even in another file then Foundation gets implicitly linked and the compilation works (which, to be clear, is what we want to avoid)

@kateinoigakukun
Copy link
Member

The reason why Foundation is automatically linked when importing JavaScriptKit is that SwiftPM generates resource_bundle_accessor.swift, which provides Bundle.module declaration per module, and it imports Foundation. The resource_bundle_accessor.swift is generated only when a package has resources and JSKit has runtime JS code as resources.
In theory, Foundation is apparently unnecessary because JSKit doesn't use Bundle.module, but it's linked for the presence of resources. So we have two options to avoid unnecessary Foundation link:

  1. Stop providing JS runtime code as SwiftPM resource and think other way to pack runtime code and Swift package
  2. Add a SwiftPM option to suppress implicit Bundle.module generation

@MaxDesiatov
Copy link
Collaborator Author

MaxDesiatov commented Aug 17, 2022

In my understanding, unused Bundle.module doesn't trigger linking of Foundation by itself. The problem is that it made symbols like trimmingCharacters(in:) without import Foundation available, which did trigger Foundation linkage after all. If that's the case, swiftlang/swift-package-manager#5728 should fix it.

@filip-sakel
Copy link

For anyone struggling with Foundation being implicitly imported, I've had success with the following carton invocation:

carton dev -Xswiftc -Xfrontend -Xswiftc -disable-autolink-framework -Xswiftc -Xfrontend -Xswiftc Foundation

Then, to just get the Bundle location, we have to create our own "Foundation" module. This as simple as creating a Foundation target (e.g. under ./Sources/Foundation), and writing the following. Note that this target should be declared as a dependency for the target that uses bundled resources.

// In `Sources/Foundation/MyFile.swift`
public final class Bundle {
  public init?(path: String) { /* Save the path */ }
}

With autolink extract disabled, we can use the custom Foundation module (to access the bundle) with no linking errors.

@ephemer
Copy link
Contributor

ephemer commented Sep 26, 2024

@filip-sakel does that still work for you with the Swift 6 compiler? I am struggling to find any way of stopping Foundation from being linked with the resources parameter set (e.g. as it is on JavaScriptKit)

edit: I'm not sure why your workaround didn't work for me at first, but it's working now also in the Swift 6 compiler. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants