detached aeb1d45

Core: Separate paths per arch and alias some libs

commit
aeb1d45eccace6c88c9f371f0c2f646eb8131ada
author
jwijenbergh · · 2023-09-28 14:06 UTC
parent
6cfb8c3
signature
GPG Unverified1A2298F6C34F96E5 The key is unknown
1 file · +46 -15
M internal/core/core.go +46 -15
@@ -8,6 +8,7 @@ import (
88 "os"
99 "os/exec"
1010 "path/filepath"
11+ "runtime"
1112 "strings"
1213 "unsafe"
1314
@@ -24,34 +25,45 @@ func PuregoSafeRegister(fptr interface{}, handle uintptr, name string) error {
2425 }
2526
2627 // paths to where the shared object files should be located
27-// TODO: This needs to be split in architecture and more paths should be added
28-var paths = []string{
29- "/usr/lib/x86_64-linux-gnu/",
28+// this is unique per architecture
29+// Debian/Ubuntu has it split into specific arch folder, Fedora is just /usr/lib64
30+// see:
31+// https://fedora.pkgs.org/38/fedora-x86_64/gtk4-4.10.1-1.fc38.x86_64.rpm.html
32+// https://fedora.pkgs.org/38/fedora-aarch64/gtk4-4.10.1-1.fc38.aarch64.rpm.html
33+// https://ubuntu.pkgs.org/23.04/ubuntu-main-amd64/libgtk-4-1_4.10.1+ds-2ubuntu1_amd64.deb.html
34+// https://ubuntu.pkgs.org/23.04/ubuntu-main-arm64/libgtk-4-1_4.10.1+ds-2ubuntu1_arm64.deb.html
35+var paths = map[string][]string{
36+ "amd64": {"/usr/lib/x86_64-linux-gnu/", "/usr/lib64/"},
37+ "arm64": {"/usr/lib/aarch64-linux-gnu/", "/usr/lib64/"},
3038 }
3139
3240 // names is a lookup from library names to shared object filenames
3341 var names = map[string][]string{
3442 "ADW": {"libadwaita-1.so"},
3543 "CAIRO": {"libcairo.so"},
36- "GDK": {"libgtk-4.so"},
3744 "GDKPIXBUF": {"libgdk_pixbuf-2.0.so"},
3845 "GIO": {"libgio-2.0.so"},
3946 "GLIB": {"libglib-2.0.so"},
4047 "GMODULE": {"libgmodule-2.0.so"},
4148 "GOBJECT": {"libgobject-2.0.so"},
4249 "GRAPHENE": {"libgraphene-1.0.so"},
43- "GSK": {"libgtk-4.so"},
4450 "GTK": {"libgtk-4.so"},
4551 "PANGO": {"libpango-1.0.so"},
4652 "PANGOCAIRO": {"libpangocairo-1.0.so"},
4753 }
4854
55+// aliases are lib aliases
56+// for example when we load the GSK lib, we should get the functions from the GTK shared library
57+var aliases = map[string]string{
58+ "GSK": "GTK",
59+ "GDK": "GTK",
60+}
61+
4962 // pkgConfNames is a lookup from library names to pkg-config library names
5063 // TODO: Get this from the package name in the gir files with a fallback to gtk's package name
5164 var pkgConfNames = map[string]string{
5265 "ADW": "libadwaita-1",
5366 "CAIRO": "cairo",
54- "GDK": "gtk4",
5567 "GDKPIXBUF": "gdk-pixbuf-2.0",
5668 "GIO": "gio-2.0",
5769 "GLIB": "glib-2.0",
@@ -59,7 +71,6 @@ var pkgConfNames = map[string]string{
5971 "GOBJECT": "gobject-2.0",
6072 "GRAPHENE": "graphene-gobject-1.0",
6173 "GSK": "gtk4",
62- "GTK": "gtk4",
6374 "PANGO": "pango",
6475 "PANGOCAIRO": "pangocairo",
6576 }
@@ -83,7 +94,6 @@ func findSo(path string, name string) string {
8394 // it does this by running pkg-config --libs-only-L libname
8495 // and then it loops over the directories returned and finds a suitable one
8596 func findPkgConf(name string) string {
86- // try to get with pkg-config
8797 cmd := exec.Command("pkg-config", "--libs-only-L", pkgConfNames[name])
8898 var out, outerr bytes.Buffer
8999 cmd.Stdout = &out
@@ -109,33 +119,54 @@ func findPkgConf(name string) string {
109119
110120 // GetPath gets a shared object file from a library name
111121 // it does it in the following order
112-// see if PUREGOTK_LIBNAME_PATH is set
122+// see if PUREGOTK_LIBNAME_FOLDER is set (full path to the lib)
123+// - e.g. PUREGOTK_GTK_PATH
124+// see if PUREGOTK_LIB_FOLDER is set (root folder where to look for libs)
113125 // go over the hardcoded paths
114126 // find a library name with pkg-config
115127 // panic if failed
116128 // TODO: Hardcore a library shared object with linker -X flag
117129 // This is useful for packaging
118130 func GetPath(name string) string {
131+ // resolve alias
132+ if v, ok := aliases[name]; ok {
133+ name = v
134+ }
119135 // try to get from env var
120136 ev := fmt.Sprintf("PUREGOTK_%s_PATH", name)
121137 if v := os.Getenv(ev); v != "" {
122138 return v
123139 }
124140
125- // try to loop over paths
126- for _, p := range paths {
127- g := findSo(p, name)
128- if g != "" {
129- return g
141+ // Or if a general folder is set where everywhere is located, return that
142+ ep := os.Getenv("PUREGOTK_LIB_FOLDER")
143+ if ep != "" {
144+ g := findSo(ep, name)
145+ if g == "" {
146+ panic(fmt.Sprintf("Could not find lib: %s, at path: %s with env: %s", name, ep, "PUREGOTK_FOLDER"))
130147 }
148+ return g
149+ }
131150
151+ // fallback to lookup a path if no env var is found
152+ gp, ok := paths[runtime.GOARCH]
153+ if ok {
154+ // try to loop over paths
155+ for _, p := range gp {
156+ g := findSo(p, name)
157+ if g != "" {
158+ return g
159+ }
160+
161+ }
132162 }
163+ // last effort: pkg-config
133164 g := findPkgConf(name)
134165 if g != "" {
135166 return g
136167 }
137168
138- panic(fmt.Sprintf("Path for library: %s not found. Please set the path to this library shared object file manually with env variable: %s. Or make sure pkg-config is setup correctly", strings.ToLower(name), ev))
169+ panic(fmt.Sprintf("Path for library: %s not found. Please set the path to this library shared object file manually with env variable: %s or PUREGOTK_LIB_FOLDER. Or make sure pkg-config is setup correctly", strings.ToLower(name), ev))
139170 }
140171
141172 // GoString copies a char* to a Go string.
@@ -8,6 +8,7 @@ import (
8 "os"8 "os"
9 "os/exec"9 "os/exec"
10 "path/filepath"10 "path/filepath"
11+ "runtime"
11 "strings"12 "strings"
12 "unsafe"13 "unsafe"
13 14
@@ -24,34 +25,45 @@ func PuregoSafeRegister(fptr interface{}, handle uintptr, name string) error {
24 }25 }
25 26
26 // paths to where the shared object files should be located27 // paths to where the shared object files should be located
27-// TODO: This needs to be split in architecture and more paths should be added28+// this is unique per architecture
28-var paths = []string{29+// Debian/Ubuntu has it split into specific arch folder, Fedora is just /usr/lib64
29- "/usr/lib/x86_64-linux-gnu/",30+// see:
31+// https://fedora.pkgs.org/38/fedora-x86_64/gtk4-4.10.1-1.fc38.x86_64.rpm.html
32+// https://fedora.pkgs.org/38/fedora-aarch64/gtk4-4.10.1-1.fc38.aarch64.rpm.html
33+// https://ubuntu.pkgs.org/23.04/ubuntu-main-amd64/libgtk-4-1_4.10.1+ds-2ubuntu1_amd64.deb.html
34+// https://ubuntu.pkgs.org/23.04/ubuntu-main-arm64/libgtk-4-1_4.10.1+ds-2ubuntu1_arm64.deb.html
35+var paths = map[string][]string{
36+ "amd64": {"/usr/lib/x86_64-linux-gnu/", "/usr/lib64/"},
37+ "arm64": {"/usr/lib/aarch64-linux-gnu/", "/usr/lib64/"},
30 }38 }
31 39
32 // names is a lookup from library names to shared object filenames40 // names is a lookup from library names to shared object filenames
33 var names = map[string][]string{41 var names = map[string][]string{
34 "ADW": {"libadwaita-1.so"},42 "ADW": {"libadwaita-1.so"},
35 "CAIRO": {"libcairo.so"},43 "CAIRO": {"libcairo.so"},
36- "GDK": {"libgtk-4.so"},
37 "GDKPIXBUF": {"libgdk_pixbuf-2.0.so"},44 "GDKPIXBUF": {"libgdk_pixbuf-2.0.so"},
38 "GIO": {"libgio-2.0.so"},45 "GIO": {"libgio-2.0.so"},
39 "GLIB": {"libglib-2.0.so"},46 "GLIB": {"libglib-2.0.so"},
40 "GMODULE": {"libgmodule-2.0.so"},47 "GMODULE": {"libgmodule-2.0.so"},
41 "GOBJECT": {"libgobject-2.0.so"},48 "GOBJECT": {"libgobject-2.0.so"},
42 "GRAPHENE": {"libgraphene-1.0.so"},49 "GRAPHENE": {"libgraphene-1.0.so"},
43- "GSK": {"libgtk-4.so"},
44 "GTK": {"libgtk-4.so"},50 "GTK": {"libgtk-4.so"},
45 "PANGO": {"libpango-1.0.so"},51 "PANGO": {"libpango-1.0.so"},
46 "PANGOCAIRO": {"libpangocairo-1.0.so"},52 "PANGOCAIRO": {"libpangocairo-1.0.so"},
47 }53 }
48 54
55+// aliases are lib aliases
56+// for example when we load the GSK lib, we should get the functions from the GTK shared library
57+var aliases = map[string]string{
58+ "GSK": "GTK",
59+ "GDK": "GTK",
60+}
61+
49 // pkgConfNames is a lookup from library names to pkg-config library names62 // pkgConfNames is a lookup from library names to pkg-config library names
50 // TODO: Get this from the package name in the gir files with a fallback to gtk's package name63 // TODO: Get this from the package name in the gir files with a fallback to gtk's package name
51 var pkgConfNames = map[string]string{64 var pkgConfNames = map[string]string{
52 "ADW": "libadwaita-1",65 "ADW": "libadwaita-1",
53 "CAIRO": "cairo",66 "CAIRO": "cairo",
54- "GDK": "gtk4",
55 "GDKPIXBUF": "gdk-pixbuf-2.0",67 "GDKPIXBUF": "gdk-pixbuf-2.0",
56 "GIO": "gio-2.0",68 "GIO": "gio-2.0",
57 "GLIB": "glib-2.0",69 "GLIB": "glib-2.0",
@@ -59,7 +71,6 @@ var pkgConfNames = map[string]string{
59 "GOBJECT": "gobject-2.0",71 "GOBJECT": "gobject-2.0",
60 "GRAPHENE": "graphene-gobject-1.0",72 "GRAPHENE": "graphene-gobject-1.0",
61 "GSK": "gtk4",73 "GSK": "gtk4",
62- "GTK": "gtk4",
63 "PANGO": "pango",74 "PANGO": "pango",
64 "PANGOCAIRO": "pangocairo",75 "PANGOCAIRO": "pangocairo",
65 }76 }
@@ -83,7 +94,6 @@ func findSo(path string, name string) string {
83 // it does this by running pkg-config --libs-only-L libname94 // it does this by running pkg-config --libs-only-L libname
84 // and then it loops over the directories returned and finds a suitable one95 // and then it loops over the directories returned and finds a suitable one
85 func findPkgConf(name string) string {96 func findPkgConf(name string) string {
86- // try to get with pkg-config
87 cmd := exec.Command("pkg-config", "--libs-only-L", pkgConfNames[name])97 cmd := exec.Command("pkg-config", "--libs-only-L", pkgConfNames[name])
88 var out, outerr bytes.Buffer98 var out, outerr bytes.Buffer
89 cmd.Stdout = &out99 cmd.Stdout = &out
@@ -109,33 +119,54 @@ func findPkgConf(name string) string {
109 119
110 // GetPath gets a shared object file from a library name120 // GetPath gets a shared object file from a library name
111 // it does it in the following order121 // it does it in the following order
112-// see if PUREGOTK_LIBNAME_PATH is set122+// see if PUREGOTK_LIBNAME_FOLDER is set (full path to the lib)
123+// - e.g. PUREGOTK_GTK_PATH
124+// see if PUREGOTK_LIB_FOLDER is set (root folder where to look for libs)
113 // go over the hardcoded paths125 // go over the hardcoded paths
114 // find a library name with pkg-config126 // find a library name with pkg-config
115 // panic if failed127 // panic if failed
116 // TODO: Hardcore a library shared object with linker -X flag128 // TODO: Hardcore a library shared object with linker -X flag
117 // This is useful for packaging129 // This is useful for packaging
118 func GetPath(name string) string {130 func GetPath(name string) string {
131+ // resolve alias
132+ if v, ok := aliases[name]; ok {
133+ name = v
134+ }
119 // try to get from env var135 // try to get from env var
120 ev := fmt.Sprintf("PUREGOTK_%s_PATH", name)136 ev := fmt.Sprintf("PUREGOTK_%s_PATH", name)
121 if v := os.Getenv(ev); v != "" {137 if v := os.Getenv(ev); v != "" {
122 return v138 return v
123 }139 }
124 140
125- // try to loop over paths141+ // Or if a general folder is set where everywhere is located, return that
126- for _, p := range paths {142+ ep := os.Getenv("PUREGOTK_LIB_FOLDER")
127- g := findSo(p, name)143+ if ep != "" {
128- if g != "" {144+ g := findSo(ep, name)
129- return g145+ if g == "" {
146+ panic(fmt.Sprintf("Could not find lib: %s, at path: %s with env: %s", name, ep, "PUREGOTK_FOLDER"))
130 }147 }
148+ return g
149+ }
131 150
151+ // fallback to lookup a path if no env var is found
152+ gp, ok := paths[runtime.GOARCH]
153+ if ok {
154+ // try to loop over paths
155+ for _, p := range gp {
156+ g := findSo(p, name)
157+ if g != "" {
158+ return g
159+ }
160+
161+ }
132 }162 }
163+ // last effort: pkg-config
133 g := findPkgConf(name)164 g := findPkgConf(name)
134 if g != "" {165 if g != "" {
135 return g166 return g
136 }167 }
137 168
138- panic(fmt.Sprintf("Path for library: %s not found. Please set the path to this library shared object file manually with env variable: %s. Or make sure pkg-config is setup correctly", strings.ToLower(name), ev))169+ panic(fmt.Sprintf("Path for library: %s not found. Please set the path to this library shared object file manually with env variable: %s or PUREGOTK_LIB_FOLDER. Or make sure pkg-config is setup correctly", strings.ToLower(name), ev))
139 }170 }
140 171
141 // GoString copies a char* to a Go string.172 // GoString copies a char* to a Go string.

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 1

Tags 102

↑↓ moveenter openesc close