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

Can't get a custom SDK working with glib-2.0 #89

Open
t089 opened this issue Mar 18, 2024 · 3 comments
Open

Can't get a custom SDK working with glib-2.0 #89

t089 opened this issue Mar 18, 2024 · 3 comments
Assignees

Comments

@t089
Copy link

t089 commented Mar 18, 2024

I am trying to build a custom SDK that targets ubuntu on x86_64 and contains the package glib-2.0. My host is arm64-apple-macosx14.0, (swiftlang-5.10.0.13 clang-1500.3.9.4)

I created a Dockerfile which contains glib-2.0:

FROM swift:5.10-jammy
RUN apt update && apt -y install libglib2.0-dev && apt -y clean

Then build the image using the target platform

docker build -t swift-5.10-glib:amd64 --platform=linux/amd64 -f Dockerfile .

Then build the SDK (using my fix/workaround from #88):

swift run swift-sdk-generator make-linux-sdk --target x86_64-unknown-linux-gnu \
      --swift-version 5.10-RELEASE \
      --with-docker \
      --from-container-image swift-5.10-glib:amd64 \
      --sdk-name 5.10-RELEASE_ubuntu_jammy_x86_64_glib

Then I use swift experimental-sdk install to install the SDK.

Unfortunately, the build fails:

swift build -c release --experimental-swift-sdk 5.10-RELEASE_ubuntu_jammy_x86_64_glib

[...]

ld.lld: error: undefined symbol: g_list_alloc
>>> referenced by swift_glib_test.swift:14 (/Users/tobias/Developing/swift-glib-test/Sources/swift-glib-test/swift_glib_test.swift:14)
>>>               /Users/tobias/Developing/swift-glib-test/.build/x86_64-unknown-linux-gnu/release/swift_glib_test.build/swift_glib_test.swift.o:($s15swift_glib_testAAV14ArgumentParser15ParsableCommandAacDP3runyyKFTW)

The example repo is up here: https://github.com/t089/swift-glib-test

The generated SDK seems to contain the required lib:

$ ls -l /Users/tobias/Library/org.swift.swiftpm/swift-sdks/5.10-RELEASE_ubuntu_jammy_x86_64_glib.artifactbundle/5.10-RELEASE_ubuntu_jammy_x86_64_glib/x86_64-unknown-linux-gnu/ubuntu-jammy.sdk/lib/x86_64-linux-gnu | grep glib
glib-2.0
libglib-2.0.a
libglib-2.0.so -> libglib-2.0.so.0
libglib-2.0.so.0 -> libglib-2.0.so.0.7200.4
libglib-2.0.so.0.7200.4

Am I doing something wrong? What could I try to debug this further?


Interestingly, if I run the build inside Docker using the same image as used for building the SDK from above, the build succeeds:

docker run -it --rm --platform linux/amd64 -v (pwd):/app -w /app swift-5.10-glib:amd64   swift run
Building for debugging...
[57/57] Linking swift-glib-test
Build complete! (38.85s)
Hello, world!
@euanh euanh self-assigned this Mar 19, 2024
@t089
Copy link
Author

t089 commented Mar 19, 2024

After some investigation and help from @weissi and @MaxDesiatov:
turns out swift was picking up .pc files from the host which of course don't match in arch and os and should also not be used. This can be avoided by providing the --pkg-config-path flag which needs to point to the installed SDK, eg $HOME/Library/org.swift.swiftpm/swift-sdks/5.10-RELEASE_ubuntu_jammy_x86_64_glib.artifactbundle/5.10-RELEASE_ubuntu_jammy_x86_64_glib/x86_64-unknown-linux-gnu/ubuntu-jammy.sdk.

The next problem is that the -I flags generated from the glib-2.0.pc file do not include the SDK root path, but point to, eg /usr/lib/x86_64-linux-gnu/glib-2.0/include.

Setting PKG_CONFIG_SYSROOT_DIR to the SDK path has no effect unfortunately.

Manually patching the glib-2.0.pc to include the SDK path in the prefix variable, makes the build succeed.

@euanh
Copy link
Contributor

euanh commented Mar 19, 2024

After some investigation and help from @weissi and @MaxDesiatov: turns out swift was picking up .pc files from the host which of course don't match in arch and os and should also not be used. This can be avoided by providing the --pkg-config-path flag which needs to point to the installed SDK, eg $HOME/Library/org.swift.swiftpm/swift-sdks/5.10-RELEASE_ubuntu_jammy_x86_64_glib.artifactbundle/5.10-RELEASE_ubuntu_jammy_x86_64_glib/x86_64-unknown-linux-gnu/ubuntu-jammy.sdk.

Yes, I found the same. The giveaway was that it was picking up files in /opt/brew. 🤦

@euanh
Copy link
Contributor

euanh commented Apr 23, 2024

@t089 Could you please try your test with the latest trunk or 6.0 snapshot from https://www.swift.org/download/? Those should both now contain apple/swift-package-manager#7461.

  • You should only need to install the snapshot on your host machine - it should not be necessary to update your Swift SDK image.
  • You'll still need to set the PKG_CONFIG_PATH and PKG_CONFIG_SYSROOT_DIR environment variables. I'm working on a change to set these appropriately when the --experimental-swift-sdk flag is used.
  • Remove your manual patches to the .pc files.
% export TOOLCHAINS=$(plutil -extract CFBundleIdentifier raw $HOME/Library/Developer/Toolchains/swift-6.0-DEVELOPMENT-SNAPSHOT-2024-04-22-a.xctoolchain/Info.plist)
% export PKG_CONFIG_SYSROOT_DIR=$HOME/Library/org.swift.swiftpm/swift-sdks/5.10-RELEASE_ubuntu_jammy_x86_64_glib.artifactbundle/5.10-RELEASE_ubuntu_jammy_x86_64_glib/x86_64-unknown-linux-gnu/ubuntu-jammy.sdk
% export PKG_CONFIG_PATH=$PKG_CONFIG_SYSROOT_DIR/usr/lib/x86_64-linux-gnu/pkgconfig 

% swift --version
Apple Swift version 6.0-dev (LLVM 94447cf359772fc, Swift f36098a14a0d48b)                                                                                                                                 
Target: x86_64-apple-macosx14.0 
                                                                                                                                                                         
% swift package clean       # remove leftovers from previous builds to ensure we really build everything
...

% swift build --experimental-swift-sdk 5.10-RELEASE_ubuntu_jammy_x86_64_glib
...
[59/59] Linking swift-glib-test
Build complete! (10.09s)

Testing the resulting binary:

$ cat /etc/lsb-release 
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=22.04
DISTRIB_CODENAME=jammy
DISTRIB_DESCRIPTION="Ubuntu 22.04.4 LTS"

$ ldd swift-glib-test
        linux-vdso.so.1 (0x00007ffffa7d3000)
        libglib-2.0.so.0 => /lib/x86_64-linux-gnu/libglib-2.0.so.0 (0x00007f0d9d2a6000)
        libswiftSwiftOnoneSupport.so => /usr/lib/swift/linux/libswiftSwiftOnoneSupport.so (0x00007f0d9d264000)
        libswiftCore.so => /usr/lib/swift/linux/libswiftCore.so (0x00007f0d9cbf5000)
        libswift_Concurrency.so => /usr/lib/swift/linux/libswift_Concurrency.so (0x00007f0d9cb74000)
        libswift_StringProcessing.so => /usr/lib/swift/linux/libswift_StringProcessing.so (0x00007f0d9caad000)
        libswift_RegexParser.so => /usr/lib/swift/linux/libswift_RegexParser.so (0x00007f0d9c997000)
        libswiftGlibc.so => /usr/lib/swift/linux/libswiftGlibc.so (0x00007f0d9c984000)
        libBlocksRuntime.so => /usr/lib/swift/linux/libBlocksRuntime.so (0x00007f0d9c97f000)
        libdispatch.so => /usr/lib/swift/linux/libdispatch.so (0x00007f0d9c91f000)
        libswiftDispatch.so => /usr/lib/swift/linux/libswiftDispatch.so (0x00007f0d9c8ee000)
        libFoundation.so => /usr/lib/swift/linux/libFoundation.so (0x00007f0d9c061000)
        libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f0d9bf7a000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f0d9bd51000)
        libpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007f0d9bcdb000)
        libstdc++.so.6 => /lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f0d9baaf000)
        libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f0d9ba8f000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f0d9d607000)
        libicuucswift.so.69 => /usr/lib/swift/linux/libicuucswift.so.69 (0x00007f0d9b887000)
        libicui18nswift.so.69 => /usr/lib/swift/linux/libicui18nswift.so.69 (0x00007f0d9b55a000)
        libicudataswift.so.69 => /usr/lib/swift/linux/libicudataswift.so.69 (0x00007f0d99a01000)

$ ./swift-glib-test --help
USAGE: swift_glib_test

OPTIONS:
  -h, --help              Show help information.

$ ./swift-glib-test
Hello, world!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants