How to quick build and test Foundation on Linux

I usually build swift-corelibs-foundation on macos with Xcode. It's easy to edit and test. Now, I want to build and run the Foundation test on Linux (Ubuntu 18.04). I followed the guide GettingStarted.md, On Linux section.

It's not working.

I spent server days reading docs, searching on google and swift forums, then try on the Linux. However, I failed.

It's there anyone could tell how to do it? I would like to help to create a PR, so it will not block new beginners.

@John_McCall @jrose

Thanks
Danny

Being more specific about (1) your configuration and (2) the way it’s failing is going to be key here.

Hi @John_McCall, actually I followed the guide strictly. I will provide more detail here.

Platform Build Env:

LSB Version:	core-9.20170808ubuntu1-noarch:security-9.20170808ubuntu1-noarch
Distributor ID:	Ubuntu
Description:	Ubuntu 18.04.2 LTS
Release:	18.04
Codename:	bionic

Swift version

Latest master branch

Build steps:

  1. Update the repositories with ./swift-source/swift/utils/update-checkout
  2. Build with release mode: ./swift-source/swift/utils/build-script -R
  3. Build Foundation ./swift-source/swift/utils/build-script --foundation --xctest -R

All the above steps works. Then

cd swift-corelibs-foundation
ninja

It failed, raise an error ninja: error: loading 'build.ninja': No such file or directory

I though the GettingStarted.md is outdated, so I cd to the build directory, run ninja and ninja test

root@swift:/data/swift-source/build/Ninja-ReleaseAssert/foundation-linux-x86_64# ninja
[5/5] Running utility command for FoundationNetworking
root@swift:/data/swift-source/build/Ninja-ReleaseAssert/foundation-linux-x86_64# ninja test
[0/1] Running tests...
Test project /data/swift-source/build/Ninja-ReleaseAssert/foundation-linux-x86_64
No tests were found!!!

It shows No tests were found!!!.

Thanks, that's more helpful. @millenomi is an expert on this, although she may be out for the week.

The docs are unfortunately slightly off; Foundation tests are only available if you build for testing. Foundation is also not built by default by -R.

My recommendation is to use the build preset we use on CI, like this:

swift/utils/build-script --preset=buildbot_linux,foundation=debug install_destdir=SOME_DIRECTORY installable_package=SOMEWHERE.tar.gz

Replace SOME_DIRECTORY and SOMEWHERE with paths that will contain installed files for this copy of Swift — I generally use directories beside my build directory. The CI preset is the only one guaranteed to behave like our testing does (and then, only in Ubuntu — if you’re not on a supported Ubuntu version, use the Swift docker images for best results.)

3 Likes

@millenomi It's still not working(Ubuntu 18.04).
I think I can ask in an alternative way: How do you test on the Linux when you make some changes of foundation framework?

Precisely as I typed above. How is it not working?

@dannliu: I don’t know if this will be helpful, but the build-script builds Foundation twice for the test to work: it has to build Foundation because XCTest depends on Foundation, and then it builds it again with test which depend on XCTest. If you are running ninja manually in between those steps, it might be that the tests haven't been configured, and so the target is failing.

BTW, in my opinion it might be better to invoke cmake --build . -- and cmake --build . -- test instead of ninja and ninja test. The former will take care of re-running CMake in case the scripts have changed, while the later will not (the former invokes the later internally).

If you post the output of the error you are getting now, maybe we can see what's wrong. Good luck!

@millenomi I built with the command you provided. And then got the ./build/buildbot_linux/foundation-linux-x86_64 run ninja test. It still shows No tests were found!!!

@drodriguez Thank you. I will try and let you know the result.

1 Like

@drodriguez @millenomi I am sorry to say both ways get the same result

root@Swift:/data/swift-source/build/buildbot_linux/foundation-linux-x86_64# cmake --build .
[5/5] Running utility command for FoundationNetworking
root@Swift:/data/swift-source/build/buildbot_linux/foundation-linux-x86_64# cmake --build . -- test
[0/1] Running tests...
Test project /data/swift-source/build/buildbot_linux/foundation-linux-x86_64
No tests were found!!!
root@Swift:/data/swift-source/build/buildbot_linux/foundation-linux-x86_64# 

Hi @millenomi I noticed that I didn't find anything in the install_destdir. I am not sure it's expected. I can only find foundation in the build dir.

What is your full invocation for the build-script?

Hi @millenomi, this's my full command:

swift/utils/build-script --preset=buildbot_linux,foundation=debug install_destdir=/data/swift-source/tmp installable_package=/data/swift-source/tmp/swift-debug.tar.gz

I created a tmp dir in the swift-source.

@dannliu: I think there's nothing in the install_destdir because the build script tries to build and test all the projects before installing (except for a couple of exception, but they are not your case).

It definitely looks as if the test haven't been configured yet. I would recommend checking if XCTest is around (it should be to build the Foundation tests). Check for a directory ./build/buildbot_linux/xctest-linux-x86_64 (or something similar, I'm typing from memory). In the directory is there, I would recommend doing a cmake --build . -- in that directory to make sure XCTest has been built. If the directory is not there, or the CMake invocation fails, it would be great to know the error there.

Once XCTest is built, you can go back to foundation-linux-x86_64 and try to do cmake -DENABLE_TESTING:BOOL=YES -DFOUNDATION_PATH_TO_XCTEST_BUILD:PATH=<absolute path to your build of XCTest>. That would hopefully configure Foundation for testing (hopefully it finishes without failures). Then a final cmake --build -- test should do the trick.

Also, rerunning the build script invocation should go very fast through the steps that have already finished, so you should try that too. If that fails, maybe use some system like gist.github.com to paste the full output of the build-script invocation, to give us more clues of what's failing.

Good luck!

@millenomi @drodriguez

I copied the build process into a gist Swift build on Linux (Ubuntu 18.04) · GitHub

Hi @millenomi, When you make some changes and execute the command you mentions.

swift/utils/build-script --preset=buildbot_linux,foundation=debug install_destdir=SOME_DIRECTORY installable_package=SOMEWHERE.tar.gz

What's the command you use to run the foundation test?

Also, could you provide more detail about such a scenario:
We fix a bug for swift-corelibs-foundation (for example 3 lines changed). How do you verify whether the bug is fixed quickly?

The process on Linux seems so hard (or I did something wrong). I think the developer or swift lover deserve a better README. It should not spend us so much time on how to build and test. @John_McCall
Thanks
Danny

1 Like

The tests should run once as part of that command. After that, I iterate using (cd build/buildbot_linux/foundation-*; ninja). I often run the test binary (build/buildbot_linux/foundation-*/TestFoundation/TestFoundation) manually after setting up the environment as described in the CTest* file because I run it under lldb rather than using ninja test.

Note that if compilation fails earlier, Foundation won’t be built. Without more detail (say, logs) I’m not sure how to help further.

Hey, I've been building and testing on linux since earlier this year, specifically on Arch linux. This is the command I use to build and run the Foundation tests:

./swift/utils/build-script -R --no-assertions --llvm-targets-to-build="X86;ARM;AArch64" -j2 --xctest -T --skip-test-swift`

Note that only the --xctest -T are required- as others have noted XCTest has to be built first, because the Foundation tests run with it- everything else is optional. Take a look at ./swift/utils/build-test --help to get a better idea, I came up with this command after looking at that help info and experimenting a bit.

-R --no-assertions means a Release build without assertions, you may prefer otherwise. I don't bother building several uncommon targets in the LLVM backend, just to save some time on the interminable LLVM build. I skip the Swift tests after the first build, you can find more ways to just run the tests you want in the build-script flags.

This will run the entire test suite, you can iterate on a particular function or test after running this once, by going to the build directory and running ninja and ninja test or invoking the TestFoundation binary directly.

I think the problem with your current approach is that the build-script disables building the tests when you simply build Foundation, then circles back later and enables them before building and running the Foundation tests. That's different than how it works for other Swift repos with the build-script.