diamondburned / gotk4

Autogenerated GTK4 bindings for Go
GNU Affero General Public License v3.0
525 stars 20 forks source link

pkg-config not working from cgo in snap #119

Closed soumyaDghosh closed 10 months ago

soumyaDghosh commented 11 months ago

I have been trying to create a snap package for your gtkcord app, but it always complains about unable to find glib, gobject and other libraries. The pkg-config gives proper locations though.

snapcraft-gtkcord4-on-amd64-for-amd64-15306614 ../parts/gtkcord4/build# pkg-config --cflags --libs glib-2.0
-I/snap/gnome-42-2204-sdk/current/usr/include/glib-2.0 -I/snap/gnome-42-2204-sdk/current/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/snap/gnome-42-2204-sdk/current/usr/include -L/snap/gnome-42-2204-sdk/current/usr/lib/x86_64-linux-gnu -lglib-2.0

But, probably your code isn't looking there. Any way this can be helped? Thanks

diamondburned commented 11 months ago

What do you mean "probably"? What is the exact error? Does your go env contain anything weird?

soumyaDghosh commented 11 months ago
2023-12-16 21:01:22.242 Executing PosixPath('/root/parts/gtkcord4/run/build.sh')                                                                                            
2023-12-16 21:01:22.246 :: + go mod download all                                                                                                                            
2023-12-16 21:01:22.444 :: + go install -p 16 ./...                                                                                                                         
2023-12-16 21:01:24.368 :: go build github.com/diamondburned/gotk4/pkg/core/gextras:                                                                                        
2023-12-16 21:01:24.368 :: # pkg-config --cflags  -- glib-2.0                                                                                                               
2023-12-16 21:01:24.368 :: /snap/gnome-42-2204-sdk/current/usr/bin/pkg-config: error while loading shared libraries: libglib-2.0.so.0: cannot open shared object file: No such file or directory                                                                                                                                                         
2023-12-16 21:01:24.368 :: pkg-config: exit status 127                                                                                                                      
2023-12-16 21:01:24.368 :: go build github.com/diamondburned/gotk4/pkg/core/intern:                                                                                         
2023-12-16 21:01:24.368 :: # pkg-config --cflags  -- gobject-2.0                                                                                                            
2023-12-16 21:01:24.368 :: /snap/gnome-42-2204-sdk/current/usr/bin/pkg-config: error while loading shared libraries: libglib-2.0.so.0: cannot open shared object file: No such file or directory                                                                                                                                                         
2023-12-16 21:01:24.368 :: pkg-config: exit status 127                                                                                                                      
2023-12-16 21:01:24.368 :: go build github.com/diamondburned/gotk4/pkg/core/gbox:                                                                                           
2023-12-16 21:01:24.368 :: # pkg-config --cflags  -- glib-2.0                                                                                                               
2023-12-16 21:01:24.368 :: /snap/gnome-42-2204-sdk/current/usr/bin/pkg-config: error while loading shared libraries: libglib-2.0.so.0: cannot open shared object file: No such file or directory                                                                                                                                                         
2023-12-16 21:01:24.368 :: pkg-config: exit status 127                                                                                                                      
2023-12-16 21:01:24.417 Failed to run the build script for part 'gtkcord4'.                                                                                                 
2023-12-16 21:01:24.417 Launching shell on build environment...                                                                                                             
2023-12-16 21:01:24.417 Emitter: Pausing control of the terminal                                                                                                            
snapcraft-gtkcord4-on-amd64-for-amd64-15306614 ../project# 

This is the error that I am facing and this is go env as asked by you

GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/root/.cache/go-build'
GOENV='/root/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/root/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/root/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/snap/go/10455'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/snap/go/10455/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.21.5'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/dev/null'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build584678944=/tmp/go-build -gno-record-gcc-switches'
snapcraft-gtkcord4-on-amd64-for-amd64-15306614 ../project# 

The libraries are in the snap Gnome sdks folder. So, I need to add those flags here separately?

diamondburned commented 11 months ago

This might be an upstream problem. I'm not sure how Snap handles pkg-config with non-standard locations, but I didn't make cgo or pkg-config or snap, so I can't speak for any of them.

NixOS also does non-standard paths, and it uses $PKG_CONFIG_PATH_FOR_TARGET to create a list of pkg-config paths. This has always worked fine with that.

diamondburned commented 11 months ago

Also as a heads up, you ran this command:

pkg-config --cflags --libs glib-2.0

cgo wanted this command:

pkg-config --cflags  -- glib-2.0

What does running that exact command give you?

soumyaDghosh commented 11 months ago
snapcraft-gtkcord4-on-amd64-for-amd64-15306614 ../parts/gtkcord4/build# pkg-config --cflags  -- glib-2.0
-I/snap/gnome-42-2204-sdk/current/usr/include/glib-2.0 -I/snap/gnome-42-2204-sdk/current/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/snap/gnome-42-2204-sdk/current/usr/include

NixOS also does non-standard paths, and it uses $PKG_CONFIG_PATH_FOR_TARGET to create a list of pkg-config paths. This has always worked fine with that.

Here also we use the same, it's $PKG_CONFIG_PATH. This is the output of echo $PKG_CONFIG_PATH

/snap/gnome-42-2204-sdk/current/usr/lib/x86_64-linux-gnu/pkgconfig:/snap/gnome-42-2204-sdk/current/usr/lib/pkgconfig:/snap/gnome-42-2204-sdk/current/usr/share/pkgconfig

I am just not getting enough way to manually run those commands that are run by cgo itself. Also, there is nothing wrong with the sdk. I myself has published 50+ gnome apps, with this sdk. Things are often hardcoded, which make these issues.

diamondburned commented 11 months ago

Then this sounds more like a cgo issue. Again, I don't do anything to break pkg-config doing this, and it works perfectly fine on NixOS.

soumyaDghosh commented 10 months ago

Okay.. so, I created the snap using the prebuilt binary.