# How to build swift-corelibs-foundation?

**URL:** <https://forums.swift.org/t/how-to-build-swift-corelibs-foundation/79451>\
**Category:** Core Libraries\
**Created:** [April 22, 2025, 8:56pm UTC](https://forums.swift.org/t/how-to-build-swift-corelibs-foundation/79451 "2025-04-22T20:56:53Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![DarkDust](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/darkdust/32/38508_2.png) [@DarkDust](https://forums.swift.org/u/DarkDust)\
**Post date:** [April 22, 2025, 8:56pm UTC](https://forums.swift.org/t/how-to-build-swift-corelibs-foundation/79451/1 "2025-04-22T20:56:53Z")

</div>

I wanted to work on `swift-corelibs-foundation` but I found it to be a pretty frustrating experience. [Building it on macOS doesn't work](https://forums.swift.org/t/how-to-build-swift-corelibs-foundation-on-macos/78754), so I tried to set up a Linux VM instead.

The actual question I'm having is: is there some recommended setup, some tips or hints on how to build `swift-corelibs-foundation`?

People are making contributions so I feel I'm missing something and have taken the wrong road, again. Any guidance is appreciated.

* * *

After other attempts, here's my current setup:

- A VM of Ubuntu 24.04.2 LTS on ARM64 (for performance on my MacBook Pro).
- I have Swift 6.0.3 installed via `swiftly`.
- Current main branch of `swift-corelibs-foundation` (6a0dd7335e77dcf33e6ab4d73d8e8ab3d1d347d0 at the time of this writing).
- Buildung using `DISPATCH_INCLUDE_PATH=/home/user/.local/share/swiftly/toolchains/6.0.3/usr/lib/swift swift build`

I get several errors, for example:

- `Sources/Foundation/NSCalendar.swift:45:13: error: switch must be exhaustive` (lots of missing calendar enum cases)
- `Sources/Foundation/NSString.swift:1695:2: error: replaced function '_cfStringEncodingConvert(string:using:allowLossyConversion:)' could not be found`
- `Sources/Foundation/NSString.swift:1700:2: error: replaced function '_cfMakeStringFromBytes(_:encoding:)' could not be found`

(It's easy to work around the NSCalendar issue, but I'm lost about the "replaced function could not be found" errors.)

---

<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:** [April 24, 2025, 7:02pm UTC](https://forums.swift.org/t/how-to-build-swift-corelibs-foundation/79451/2 "2025-04-24T19:02:26Z")

</div>

> [@DarkDust](#):
>
> is there some recommended setup, some tips or hints on how to build `swift-corelibs-foundation`?

Not that I know of, but now that it switched to being a Swift package with Swift 6, it shouldn't be that hard.

> [@DarkDust](#):
>
> I have Swift 6.0.3 installed via `swiftly`.

I don't recommend using the Swiftly toolchain to build Swift toolchain repos, as others have reported issues with it. Instead, download [the latest trunk snapshot toolchain for your distro](https://www.swift.org/install/linux/) yourself, unpack it, and add it to your `PATH`.

> [@DarkDust](#):
>
> Current main branch of `swift-corelibs-foundation`

Check out the trunk source snapshot tag that matches your trunk toolchain, eg `git checkout swift-DEVELOPMENT-SNAPSHOT-2025-04-12-a`, and see if that works.

If not, try checking out the dependencies `swift-foundation`, `swift-foundation-icu`, and `swift-syntax` to the same trunk tag in the same directory, plus `swift-collections` to the 1.1.3 tag, then build with `SWIFTCI_USE_LOCAL_DEPS=1 swift build`, to replicate how the CI builds it.

---

<div class="post-metadata">

**Author:** ![jmschonfeld](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jmschonfeld/32/20922_2.png) [@jmschonfeld](https://forums.swift.org/u/jmschonfeld)\
**Post date:** [April 25, 2025, 3:40am UTC](https://forums.swift.org/t/how-to-build-swift-corelibs-foundation/79451/3 "2025-04-25T03:40:10Z")

</div>

> [@DarkDust](#):
>
> I get several errors, for example:
> 
> - `Sources/Foundation/NSCalendar.swift:45:13: error: switch must be exhaustive` (lots of missing calendar enum cases)
> - `Sources/Foundation/NSString.swift:1695:2: error: replaced function '_cfStringEncodingConvert(string:using:allowLossyConversion:)' could not be found`
> - `Sources/Foundation/NSString.swift:1700:2: error: replaced function '_cfMakeStringFromBytes(_:encoding:)' could not be found`

To me all of these errors sound like you might be building a commit of swift-corelibs-foundation that is newer than the commit of the swift-foundation dependency. This can happen when doing a `git pull` of the SCL-F repo without asking SwiftPM to pull dependencies as well. Could you try running `swift package update` in your SCL-F checkout to pull the latest commits for the swift-foundation dependency and try building again? Using a Linux VM on your Mac with a swift toolchain installed using the command you provided should be sufficient to work. For reference:

> _Note: Subsequent SwiftPM builds will not automatically update to newer commits of downstream Foundation projects like swift-foundation-icu. If you encounter build failures after checking out a new branch or pulling from the remote repo, you can run `swift package update` to update your local dependency checkouts to match top-of-tree to ensure you have the required versions of dependencies._

> <https://github.com/swiftlang/swift-foundation/blob/main/Foundation_Build_Process.md>

---

<div class="post-metadata">

**Author:** ![DarkDust](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/darkdust/32/38508_2.png) [@DarkDust](https://forums.swift.org/u/DarkDust)\
**Post date:** [April 27, 2025, 6:04pm UTC](https://forums.swift.org/t/how-to-build-swift-corelibs-foundation/79451/4 "2025-04-27T18:04:20Z")

</div>

Thanks for the feedback. After more experiments I now _almost_ succeed in build `swift-corelibs-foundation`. It builds but can't link because it exhausts the number of open files. I suspect it's [SwiftPM bug 4584](https://github.com/swiftlang/swift-package-manager/issues/4584):

```plaintext
[1/1] Planning build
Building for debugging...
error: link command failed with exit code 1 (use -v to see invocation)
/usr/bin/ld.gold: fatal error: out of file descriptors and couldn't close any
clang: error: linker command failed with exit code 1 (use -v to see invocation)
error: link command failed with exit code 1 (use -v to see invocation)
/usr/bin/ld.gold: fatal error: out of file descriptors and couldn't close any
clang: error: linker command failed with exit code 1 (use -v to see invocation)
[1/3] Linking plutil

```

I even raised the number of open files a process can have to 100 million (!) via sysctl and limits.conf but that's not enough.

* * *

So here's my current setup:

- Ubuntu 24.02.
- Downloaded and installed Swift 6.1-RELEASE from the ["Install Swift" page](https://www.swift.org/install/linux/).
- Then did the equivalent of the following script:

```sh
TAG=swift-6.1-RELEASE
git clone https://github.com/swiftlang/swift-collections
for repo in swift-corelibs-foundation swift-foundation swift-foundation-icu swift-syntax ; do
  git clone https://github.com/swiftlang/$repo
  cd $repo
  git checkout $TAG
  cd ..
done
cd swift-corelibs-foundation
DISPATCH_INCLUDE_PATH=/path/to/swift-6.1-RELEASE-ubuntu24.04-aarch64/usr/lib/swift/ SWIFTCI_USE_LOCAL_DEPS=1 swift build

```

That seems to work well, no more compile errors. But unfortunately, the aforementioned linker error.

```sh
$ cat /etc/sysctl.conf
fs.nr_open=102457600

$ cat /etc/security/limits.conf
* soft nofile 104857600
* hard nofile 104857600

$ ulimit -n
104857600

```

---

<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:** [April 27, 2025, 6:41pm UTC](https://forums.swift.org/t/how-to-build-swift-corelibs-foundation/79451/5 "2025-04-27T18:41:07Z")

</div>

Does using lld instead work? Try `<env vars> swift build -Xswiftc -use-ld=lld`.

---

<div class="post-metadata">

**Author:** ![DarkDust](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/darkdust/32/38508_2.png) [@DarkDust](https://forums.swift.org/u/DarkDust)\
**Post date:** [April 27, 2025, 7:37pm UTC](https://forums.swift.org/t/how-to-build-swift-corelibs-foundation/79451/6 "2025-04-27T19:37:30Z")

</div>

Not much better, unfortunately:

```plaintext
error: link command failed with exit code 1 (use -v to see invocation)
ld.lld: error: cannot open /home/marc/utm/swift-corelibs-foundation/.build/aarch64-unknown-linux-gnu/debug/CoreFoundation.build/CFStream.c.o: Too many open files
[about 20 similar lines omitted]
ld.lld: error: cannot open /home/marc/utm/swift-corelibs-foundation/.build/aarch64-unknown-linux-gnu/debug/CoreFoundation.build/CFUnicodePrecomposition.c.o: Too many open files
ld.lld: error: too many errors emitted, stopping now (use --error-limit=0 to see all errors)
clang: error: linker command failed with exit code 1 (use -v to see invocation)
[104/106] Linking plutil

```

---

<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:** [April 27, 2025, 7:56pm UTC](https://forums.swift.org/t/how-to-build-swift-corelibs-foundation/79451/7 "2025-04-27T19:56:24Z")

</div>

Unless you have many other builds going on simultaneously, there's no way you've got millions of file descriptors open when linking Foundation alone. Something else is going on: I suggest you use tools like `lsof` to investigate further, or search online for known bugs of whatever VM tool you are using.

---

<div class="post-metadata">

**Author:** ![DarkDust](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/darkdust/32/38508_2.png) [@DarkDust](https://forums.swift.org/u/DarkDust)\
**Post date:** [May 4, 2025, 11:20am UTC](https://forums.swift.org/t/how-to-build-swift-corelibs-foundation/79451/8 "2025-05-04T11:20:00Z")

</div>

I tried to find out what's going on but ultimately gave up and tried with a different VM: instead of Ubuntu 24.04 on my MacBook Pro using UTM (ARM64), I'm now using Ubuntu 22.04 on a professionally hosted VM (x86\_64, Hetzner). And that finally works! 🥳

No idea what makes the difference, whether it's the different Ubuntu version or CPU architecture. Anyway, I can finally start working on my PR so thank you for your help!
