# "Who" defines / influences the linker invocation in a SwiftPM build of a swift target?

**URL:** <https://forums.swift.org/t/who-defines-influences-the-linker-invocation-in-a-swiftpm-build-of-a-swift-target/61866>\
**Category:** Compiler\
**Tags:** beginner-task\
**Created:** [December 6, 2022, 12:46pm UTC](https://forums.swift.org/t/who-defines-influences-the-linker-invocation-in-a-swiftpm-build-of-a-swift-target/61866 "2022-12-06T12:46:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Geordie\_J](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/geordie_j/32/839_2.png) [@Geordie\_J](https://forums.swift.org/u/Geordie_J)\
**Post date:** [December 6, 2022, 12:46pm UTC](https://forums.swift.org/t/who-defines-influences-the-linker-invocation-in-a-swiftpm-build-of-a-swift-target/61866/1 "2022-12-06T12:46:33Z")

</div>

As described [here](https://forums.swift.org/t/android-link-failures-with-static-swift-stdlib/61853) I am hoping to get a statically-linked dynamic library of our Android app to reduce size and loading overhead.

Long story short, I've identified three things that I'd like to change in the Android linker invocation when running `swift build --static-swift-stdlib`, but I've never really worked on the compiler before so I don't know where to start:

1. Currently the wrong library search path is being set (we want `-L${SDK}/usr/lib/ **swift_static** /android`, not `-L${SDK}/usr/lib/swift/android`)
2. We need to always add `-lDispatchStubs` to static builds to avoid runtime link errors.
3. (Bonus) I noticed when running `swift build --static-swift-stdlib` on a lonesome C target, `libswiftCore.so` is still being dynamically linked. I'm pretty sure that makes no sense for two reasons. That may be more difficult to understand/fix, but I'd be interested to try.

Can anyone give me a pointer about where to look for this kind of thing? The Android build uses `ldd`, but I'm also not sure how/why that is set either.

---

<div class="post-metadata">

**Author:** ![Max\_Desiatov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/max_desiatov/32/20654_2.png) [@Max\_Desiatov](https://forums.swift.org/u/Max_Desiatov)\
**Post date:** [December 6, 2022, 1:54pm UTC](https://forums.swift.org/t/who-defines-influences-the-linker-invocation-in-a-swiftpm-build-of-a-swift-target/61866/2 "2022-12-06T13:54:07Z")

</div>

Have you tried passing `-v` flag to `swift build --static-swift-stdlib`? Looking at the sequence of commands exposed by that flag is where I'd start.

---

<div class="post-metadata">

**Author:** ![Geordie\_J](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/geordie_j/32/839_2.png) [@Geordie\_J](https://forums.swift.org/u/Geordie_J)\
**Post date:** [December 6, 2022, 1:55pm UTC](https://forums.swift.org/t/who-defines-influences-the-linker-invocation-in-a-swiftpm-build-of-a-swift-target/61866/3 "2022-12-06T13:55:50Z")

</div>

I did indeed :) That's how I established the changes I need to make. Now I need to figure out how to change the compiler/frontend/SwiftPM (I don't know which) so it produces the right invocation – that's what I'd need an initial pointer on (just knowing which one of those places to change would already be a big help) 🙏

---

<div class="post-metadata">

**Author:** ![Finagolfin](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/finagolfin/32/34388_2.png) [@Finagolfin](https://forums.swift.org/u/Finagolfin)\
**Post date:** [December 6, 2022, 5:42pm UTC](https://forums.swift.org/t/who-defines-influences-the-linker-invocation-in-a-swiftpm-build-of-a-swift-target/61866/4 "2022-12-06T17:42:39Z")

</div>

As Max points out, simply running SPM in verbose mode gives you a lot of info. I just tried your SPM command with some Swift packages, both compiling natively for linux and natively on my Android phone, and both produced the right `lib/swift_static/` directory. If you look at that output, [SPM simply passes the `-static-stdlib` flag to the Swift compiler](https://github.com/apple/swift-package-manager/blob/bd6487b756d41db6f9936938ec464dd18cea5542/Sources/Build/BuildPlan.swift#L1455), though it only does so for executables and snippets, not for dynamic libraries.

As for DispatchStubs, [this line added it automatically a couple years ago](https://github.com/apple/swift-corelibs-libdispatch/blob/b27f6f343ca8556f669bbcc1449099a19567a0ca/dispatch/generic_static/module.modulemap#L6), but if you stopped using Dispatch and Foundation, I guess you'll have to add it back manually, [probably here](https://github.com/apple/swift/blob/main/stdlib/public/runtime/CMakeLists.txt#L228).

---

<div class="post-metadata">

**Author:** ![Geordie\_J](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/geordie_j/32/839_2.png) [@Geordie\_J](https://forums.swift.org/u/Geordie_J)\
**Post date:** [December 9, 2022, 1:01am UTC](https://forums.swift.org/t/who-defines-influences-the-linker-invocation-in-a-swiftpm-build-of-a-swift-target/61866/5 "2022-12-09T01:01:32Z")

</div>

To answer my original question, "who" turns out to be "us": In our SwiftPM destination.json, we are setting the `-resource-dir` to `${TOOLCHAIN}/usr/lib/swift`.

From what I can tell, there is some machinery in the compiler to `-use-static-resource-dir` when the `-static-stdlib` option is applied (which FYI does appear to be sent to the compiler, even when building a shared library). That said, I couldn't immediately figure out what the former option actually does and whether it does anything at all wrt the linker invocation. It doesn't appear to be applied or be relevant here – I couldn't see it being passed on, and I couldn't figure out "whom" to pass it to to test the option manually (`swiftc` and `swift -frontend` both seem to reject the flag).

For our purposes, I found that it was enough to simply change the `-resource-dir` in destination.json to `.../usr/lib/swift_static`. **That alone fixes all three issues listed above** , including the seemingly unrelated one whereby libswiftCore.so was being linked for non-swift targets.

It would be really great to have some kind of automatic recognition that `-resource-dir` is just the base directory, and that `--static-swift-stdlib` would add `_static` to that path automatically. But I suspect doing so might break other workflows, so for now I will just leave this as is.
