AparokshaUI / adwaita-swift

A framework for creating user interfaces for GNOME with an API similar to SwiftUI
https://aparokshaui.github.io/adwaita-swift/
MIT License
750 stars 16 forks source link

libadwaita not found on Windows #26

Open s-k opened 2 months ago

s-k commented 2 months ago

Describe the bug

I tried using the library on Windows. I installed GTK and libadwaita using gvsbuild, but when I run swift run in PowerShell, the following warning is shown:

warning: failed to retrieve search paths with pkg-config; maybe pkg-config is not installed
warning: couldn't find pc file for libadwaita-1

However, when running pkg-config manually in PowerShell, it finds libadwaita without problems:

> pkg-config --variable=includedir libadwaita-1
C:/gtk-build/gtk/release/bin/../include

To Reproduce

  1. Add the library as a dependency in Package.swift
  2. Run swift run in PowerShell

Expected behavior

I expect the library to compile without problems.

Additional context

No response

david-swift commented 2 months ago

Thanks for opening this issue! I haven't tried gvsbuild so far, but I will definitely at some point, so unfortunately, I can't help (I use it on Linux and libadwaita also works best on Linux).

Based on the information you provided, I'd also expect the library to compile.

I'm a bit irritated by the first code snippet saying png-config instead of pkg-config. Do you know what png-config is, or where there might be a mistake?

s-k commented 2 months ago

Thank you for your reply!

I'm a bit irritated by the first code snippet saying png-config instead of pkg-config. Do you know what png-config is, or where there might be a mistake?

I'm sorry! This was a typo by me. I edited the issue to correct the mistake.

alexmocanu commented 2 months ago

@s-k I advanced a little further than you. Maybe you are missing the path to the .pc files?

I downloaded the GTK4 binaries from here: https://github.com/wingtk/gvsbuild/actions/runs/8882441878 and extracted the archive into the C:\GTK4 (I was too lazy do build it from source):

I set the following powershell variables (I used Developer Powershell for VS 2022 by the way):

$env:Path = "C:\gtk4\bin;C:\gtk4\lib\pkgconfig;" + $env:Path
$env:LIB = "C:\gtk4\lib;" + $env:LIB
$env:INCLUDE = "C:\gtk4\include;C:\gtk4\include\cairo;C:\gtk4\include\glib-2.0;C:\gtk4\include\gobject-introspection-1.0;C:\gtk4\lib\glib-2.0\include;C:\gtk4\lib\pkgconfig;" + $env:INCLUDE

I basically copy-pasted the ones from https://github.com/wingtk/gvsbuild and added the path to the .pc files (C:\gtk4\lib\pkgconfig in my case). That made the pkg-config issues go away for me.

Now when I try to compile the AdwaitaTemplate project I get these errors:

error: 'adwaitatemplate': Invalid manifest (compiled with: ["C:\\Users\\alexm\\AppData\\Local\\Programs\\Swift\\Toolchains\\5.10.0+Asserts\\usr\\bin\\swiftc.exe", "-vfsoverlay", "C:\\Users\\alexm\\AppData\\Local\\Temp\\TemporaryDirectory.iH54k8\\vfs.yaml", "-L", "C:\\Users\\alexm\\AppData\\Local\\Programs\\Swift\\Toolchains\\5.10.0+Asserts\\usr\\lib\\swift\\pm\\ManifestAPI", "-lPackageDescription", "-sdk", "C:\\Users\\alexm\\AppData\\Local\\Programs\\Swift\\Platforms\\5.10.0\\Windows.platform\\Developer\\SDKs\\Windows.sdk", "-libc", "MD", "-I", "C:\\Users\\alexm\\AppData\\Local\\Programs\\Swift\\Platforms\\5.10.0\\Windows.platform\\Developer\\Library\\XCTest-development\\usr\\lib\\swift\\windows", "-I", "C:\\Users\\alexm\\AppData\\Local\\Programs\\Swift\\Platforms\\5.10.0\\Windows.platform\\Developer\\Library\\XCTest-development\\usr\\lib\\swift\\windows\\x86_64", "-L", "C:\\Users\\alexm\\AppData\\Local\\Programs\\Swift\\Platforms\\5.10.0\\Windows.platform\\Developer\\Library\\XCTest-development\\usr\\lib\\swift\\windows\\x86_64", "-use-ld=lld", "-swift-version", "5", "-I", "C:\\Users\\alexm\\AppData\\Local\\Programs\\Swift\\Toolchains\\5.10.0+Asserts\\usr\\lib\\swift\\pm\\ManifestAPI", "-package-description-version", "5.8.0", "C:\\Users\\alexm\\Desktop\\AdwaitaTemplate\\Package.swift", "-Xfrontend", "-disable-implicit-concurrency-module-import", "-Xfrontend", "-disable-implicit-string-processing-module-import", "-o", "C:\\Users\\alexm\\AppData\\Local\\Temp\\TemporaryDirectory.3CMITY\\adwaitatemplate-manifest.exe"])
error: fatalError
<module-includes>:1:10: note: in file included from <module-includes>:1:
#include "AssertionReporting.h"
         ^
C:\Users\alexm\AppData\Local\Programs\Swift\Platforms\5.10.0\Windows.platform\Developer\SDKs\Windows.sdk\usr\lib\swift\shims/AssertionReporting.h:16:10: note: in file included from C:\Users\alexm\AppData\Local\Programs\Swift\Platforms\5.10.0\Windows.platform\Developer\SDKs\Windows.sdk\usr\lib\swift\shims/AssertionReporting.h:16:
#include "SwiftStdint.h"
         ^
C:\Users\alexm\AppData\Local\Programs\Swift\Platforms\5.10.0\Windows.platform\Developer\SDKs\Windows.sdk\usr\lib\swift\shims/SwiftStdint.h:28:10: error: 'stdint.h' file not found
#include <stdint.h>
         ^
<unknown>:0: error: could not build C module 'SwiftShims'

When I installed swift, visual studio and the windows 11 sdk, I used these commands as described on swift.org:

winget install --id Microsoft.VisualStudio.2022.Community --exact --force --custom "--add Microsoft.VisualStudio.Component.Windows11SDK.22000 --add Microsoft.VisualStudio.Component.VC.Tools.x86.x64"
winget install --id Swift.Toolchain -e

I think I need to add some additional Visual Studio components but I can't figure out which yet. I tried adding the Universal CRT SDK and the universal windows runtime, but those broke swift.

s-k commented 1 month ago

@alexmocanu Thank you for chiming in. I am not sure that you advanced further than me. To me, it seems that Swift cannot even compile your Package.swift.

@david-swift I made a little progress myself. I had to include all include directories manually:

swift build `
    -Xcc -I -Xcc C:/gtk-build/gtk/x64/release/include/cairo `
    -Xcc -I -Xcc C:/gtk-build/gtk/x64/release/include/epoxy `
    -Xcc -I -Xcc C:/gtk-build/gtk/x64/release/include/freetype2 `
    -Xcc -I -Xcc C:/gtk-build/gtk/x64/release/include/fribidi `
    -Xcc -I -Xcc C:/gtk-build/gtk/x64/release/include/gdk-pixbuf-2.0 `
    -Xcc -I -Xcc C:/gtk-build/gtk/x64/release/include/gio-win32-2.0 `
    -Xcc -I -Xcc C:/gtk-build/gtk/x64/release/include/glib-2.0 `
    -Xcc -I -Xcc C:/gtk-build/gtk/x64/release/include/graphene-1.0 `
    -Xcc -I -Xcc C:/gtk-build/gtk/x64/release/include/gtk-4.0 `
    -Xcc -I -Xcc C:/gtk-build/gtk/x64/release/include/harfbuzz `
    -Xcc -I -Xcc C:/gtk-build/gtk/x64/release/include/libadwaita-1 `
    -Xcc -I -Xcc C:/gtk-build/gtk/x64/release/include/libpng16 `
    -Xcc -I -Xcc C:/gtk-build/gtk/x64/release/include/librsvg-2.0 `
    -Xcc -I -Xcc C:/gtk-build/gtk/x64/release/include/libxml2 `
    -Xcc -I -Xcc C:/gtk-build/gtk/x64/release/include/pango-1.0 `
    -Xcc -I -Xcc C:/gtk-build/gtk/x64/release/include/pixman-1 `
    -Xcc -I -Xcc C:/gtk-build/gtk/x64/release/lib/glib-2.0/include `
    -Xcc -I -Xcc C:/gtk-build/gtk/x64/release/lib/graphene-1.0/include

Now, swift build finds all headers. However, I get an new error:

Adwaita\Sources\Adwaita\Model\User Interface\Window\GTUIAboutWindow.swift:18:23: error: cannot find 'adw_about_window_new_from_appdata' in scope
            pointer = adw_about_window_new_from_appdata(filePath, nil)?.cast()
                      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
david-swift commented 1 month ago

To me, it seems that Swift cannot even compile your Package.swift.

Yes, I recently installed Windows next to Linux and I got the same error. It appears with Swift packages in general. I skipped the Visual Studio step in the installation process, hoping to be able to use Swift without having to install all that stuff, but that could maybe cause this.

Edit: I just saw you, @s-k, followed the exact instructions 🤔

I made a little progress myself.

Thanks for sharing! This doesn't look that beautiful, but it's better than nothing. It looks like you got pretty far!

However, I get an new error

What's the version of your Libadwaita installation? Version 1.5 is required.

alexmocanu commented 1 month ago

@s-k You're right, I must have messed up something so I started from scratch.

About that missing function error:

I noticed that wingtk seems to be patching out adw_about_window_new_from_appdata from /src/adw-about-window.c. Something about removing AppStream dependency https://github.com/wingtk/gvsbuild/blob/f5d04f9133eab7856ee8d21246bdf5a6267bf5f4/gvsbuild/patches/libadwaita/0001-remove-appstream-dependency.patch#L507

I think this patch is responsible for that last error.

To test things out I started with an empty swift package having only Adwaita-Swift as dependency.

I removed the reference to that function from GTUIAboutWindow.swift (I left only pointer = adw_about_window_new()?.cast() in there). The swift package seems to compile (with a lot of warnings) but then the linker stops with these errors:

lld-link: error: could not open 'adwaita-1.lib': no such file or directory
lld-link: error: could not open 'gtk-4.lib': no such file or directory
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I added "-Xlinker -LC:\gtk4\lib" (that's the path to the .lib files on my machine) and now I'm getting more linker errors:

lld-link: error: undefined symbol: __declspec(dllimport) g_signal_connect_data
>>> referenced by C:\Users\user\Desktop\testswift\.build\x86_64-unknown-windows-msvc\debug\Adwaita.build\GTUIApp.swift.o:($s7Adwaita7GTUIAppC3run14automaticSetup06manualE0ySb_yyctF)
>>> referenced by C:\Users\user\Desktop\testswift\.build\x86_64-unknown-windows-msvc\debug\Adwaita.build\GTUIApp.swift.o:($s7Adwaita7GTUIAppC19addKeyboardShortcut_2id6window7handlerySS_SSAA21GTUIApplicationWindowCSgyyctF)
>>> referenced by C:\Users\user\Desktop\testswift\.build\x86_64-unknown-windows-msvc\debug\Adwaita.build\GTUIWindow.swift.o:($s7Adwaita10GTUIWindowC11observeHide8observeryyyc_tF)
>>> referenced 1 more times

lld-link: error: undefined symbol: __declspec(dllimport) g_application_run
>>> referenced by C:\Users\user\Desktop\testswift\.build\x86_64-unknown-windows-msvc\debug\Adwaita.build\GTUIApp.swift.o:($s7Adwaita7GTUIAppC3run14automaticSetup06manualE0ySb_yyctF)
...
alexmocanu commented 1 month ago

If it's of any help, I got the Adwaita-Template project to compile and run under Windows: adwaita_template

  1. I used a local version of Adwaita-Swift 0.2.5 where I removed the reference to adw_about_window_new_from_appdata

gtk_window

  1. I had to remove all references to the Localized package in Package.swift and the code itself. It was throwing some compilation errors that were getting in the way.

  2. I had to link every lib file separately (maybe there is a better way? I don't know much about these things)

swift run `
    -Xcc -I -Xcc C:\gtk4\include\cairo `
    -Xcc -I -Xcc C:\gtk4\include\epoxy `
    -Xcc -I -Xcc C:\gtk4\include\freetype2 `
    -Xcc -I -Xcc C:\gtk4\include\fribidi `
    -Xcc -I -Xcc C:\gtk4\include\gdk-pixbuf-2.0 `
    -Xcc -I -Xcc C:\gtk4\include\gio-win32-2.0 `
    -Xcc -I -Xcc C:\gtk4\include\glib-2.0 `
    -Xcc -I -Xcc C:\gtk4\include\graphene-1.0 `
    -Xcc -I -Xcc C:\gtk4\include\gtk-4.0 `
    -Xcc -I -Xcc C:\gtk4\include\harfbuzz `
    -Xcc -I -Xcc C:\gtk4\include\libadwaita-1 `
    -Xcc -I -Xcc C:\gtk4\include\libpng16 `
    -Xcc -I -Xcc C:\gtk4\include\librsvg-2.0 `
    -Xcc -I -Xcc C:\gtk4\include\libxml2 `
    -Xcc -I -Xcc C:\gtk4\include\pango-1.0 `
    -Xcc -I -Xcc C:\gtk4\include\pixman-1 `
    -Xcc -I -Xcc C:\gtk4\lib\glib-2.0\include `
    -Xcc -I -Xcc C:\gtk4\lib\graphene-1.0\include `
    -Xlinker C:\gtk4\lib\adwaita-1.lib `
    -Xlinker C:\gtk4\lib\asprintf.lib `
    -Xlinker C:\gtk4\lib\cairo-gobject.lib `
    -Xlinker C:\gtk4\lib\cairo-script-interpreter.lib `
    -Xlinker C:\gtk4\lib\cairo.lib `
    -Xlinker C:\gtk4\lib\epoxy.lib `
    -Xlinker C:\gtk4\lib\ffi.lib `
    -Xlinker C:\gtk4\lib\freetype.lib `
    -Xlinker C:\gtk4\lib\fribidi.lib `
    -Xlinker C:\gtk4\lib\gdk_pixbuf-2.0.lib `
    -Xlinker C:\gtk4\lib\gettextpo.lib `
    -Xlinker C:\gtk4\lib\gio-2.0.lib `
    -Xlinker C:\gtk4\lib\girepository-1.0.lib `
    -Xlinker C:\gtk4\lib\girepository-2.0.lib `
    -Xlinker C:\gtk4\lib\glib-2.0.lib `
    -Xlinker C:\gtk4\lib\gmodule-2.0.lib `
    -Xlinker C:\gtk4\lib\gobject-2.0.lib `
    -Xlinker C:\gtk4\lib\graphene-1.0.lib `
    -Xlinker C:\gtk4\lib\gthread-2.0.lib `
    -Xlinker C:\gtk4\lib\gtk-4.lib `
    -Xlinker C:\gtk4\lib\gtksourceview-5.lib `
    -Xlinker C:\gtk4\lib\harfbuzz-cairo.lib `
    -Xlinker C:\gtk4\lib\harfbuzz-gobject.lib `
    -Xlinker C:\gtk4\lib\harfbuzz-subset.lib `
    -Xlinker C:\gtk4\lib\harfbuzz.lib `
    -Xlinker C:\gtk4\lib\iconv.lib `
    -Xlinker C:\gtk4\lib\intl.lib `
    -Xlinker C:\gtk4\lib\jpeg-static.lib `
    -Xlinker C:\gtk4\lib\jpeg.lib `
    -Xlinker C:\gtk4\lib\libpng16.lib `
    -Xlinker C:\gtk4\lib\libpng16_static.lib `
    -Xlinker C:\gtk4\lib\libxml2.lib `
    -Xlinker C:\gtk4\lib\pango-1.0.lib `
    -Xlinker C:\gtk4\lib\pangocairo-1.0.lib `
    -Xlinker C:\gtk4\lib\pangowin32-1.0.lib `
    -Xlinker C:\gtk4\lib\pcre2-16.lib `
    -Xlinker C:\gtk4\lib\pcre2-32.lib `
    -Xlinker C:\gtk4\lib\pcre2-8.lib `
    -Xlinker C:\gtk4\lib\pcre2-posix.lib `
    -Xlinker C:\gtk4\lib\pixman-1.lib `
    -Xlinker C:\gtk4\lib\pkgconf.lib `
    -Xlinker C:\gtk4\lib\rsvg-2.0.lib `
    -Xlinker C:\gtk4\lib\tiff.lib `
    -Xlinker C:\gtk4\lib\tiffxx.lib `
    -Xlinker C:\gtk4\lib\turbojpeg-static.lib `
    -Xlinker C:\gtk4\lib\turbojpeg.lib `
    -Xlinker C:\gtk4\lib\zlib1.lib

If you get errors about missing adwaita, gtk, etc. dll files, you have to set the path to them like this (replace C:\gtk with your path):

$env:Path = "C:\gtk4\bin;C:\gtk4\lib\pkgconfig;" + $env:Path

If you get errors about permissions (like this: :0: error: failed writing record...), make sure to unset the read only attribute from the project's folder (and contents).

s-k commented 1 month ago

@alexmocanu Thank you so much! That did the trick!

david-swift commented 1 month ago

I fixed the Adwaita problem in https://github.com/AparokshaUI/adwaita-swift/commit/e9094b9247abab3bb59fb902d31d4a03f82fac14 so one can now run the template following @alexmocanu's detailed instructions without having to edit the Adwaita package (unfortunately including removing the Localized references, but I'll work on that), using the main branch of Adwaita until the next release.

litewrap commented 1 month ago

Wow! Great job! Thank you all for making Swift Adwaita on Windows a reality.