From 68cd53e82d130f0cd07933761157d297766d13c3 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 7 Aug 2023 10:20:20 -0700 Subject: [PATCH 1/4] build: always add /usr/lib/{triple} to search path for Linux This is a common location for dynamic libraries. We already searched it for Flatpak but we need to do this more generally. --- build.zig | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/build.zig b/build.zig index 58ce683a6..3733a5534 100644 --- a/build.zig +++ b/build.zig @@ -644,6 +644,14 @@ fn addDeps( return static_libs; } + // On Linux, we need to add a couple common library paths that aren't + // on the standard search list. i.e. GTK is often in /usr/lib/x86_64-linux-gnu + // on x86_64. + if (step.target.isLinux()) { + const triple = try step.target.linuxTriple(b.allocator); + step.addLibraryPath(.{ .path = b.fmt("/usr/lib/{s}", .{triple}) }); + } + // If we're building a lib we have some different deps const lib = step.kind == .lib; @@ -800,14 +808,7 @@ fn addDeps( // When we're targeting flatpak we ALWAYS link GTK so we // get access to glib for dbus. - if (flatpak) { - step.linkSystemLibrary("gtk4"); - switch (step.target.getCpuArch()) { - .aarch64 => step.addLibraryPath(.{ .path = "/usr/lib/aarch64-linux-gnu" }), - .x86_64 => step.addLibraryPath(.{ .path = "/usr/lib/x86_64-linux-gnu" }), - else => @panic("unsupported flatpak target"), - } - } + if (flatpak) step.linkSystemLibrary("gtk4"); switch (app_runtime) { .none => {}, From d356d5ea38deebe895c2f20536e38b0f63e813e8 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 7 Aug 2023 10:33:00 -0700 Subject: [PATCH 2/4] build: add libxml2/libbuid for fontconfig dynamic link for CI --- build.zig | 9 ++++++++- nix/devshell.nix | 9 ++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/build.zig b/build.zig index 3733a5534..07056c0a1 100644 --- a/build.zig +++ b/build.zig @@ -718,7 +718,14 @@ fn addDeps( step.linkSystemLibrary("pixman-1"); step.linkSystemLibrary("zlib"); - if (font_backend.hasFontconfig()) step.linkSystemLibrary("fontconfig"); + if (font_backend.hasFontconfig()) { + step.linkSystemLibrary("fontconfig"); + + // Required on some systems, and pkg-config for fontconfig + // doesn't include it + step.linkSystemLibrary("libxml-2.0"); + step.linkSystemLibrary("uuid"); + } } // Other dependencies, we may dynamically link diff --git a/nix/devshell.nix b/nix/devshell.nix index b44a54f1d..815e4490b 100644 --- a/nix/devshell.nix +++ b/nix/devshell.nix @@ -30,7 +30,8 @@ , harfbuzz , libpng , libGL -, libuv +, libuuid +, libxml2 , libX11 , libXcursor , libXext @@ -51,7 +52,8 @@ let freetype harfbuzz libpng - libuv + libuuid + libxml2 zlib libX11 @@ -105,7 +107,8 @@ in mkShell rec { freetype harfbuzz libpng - libuv + libuuid + libxml2 pixman zlib From a348adf26b6a4b03cb6eb892e37eac0fabb1fc81 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 7 Aug 2023 10:49:50 -0700 Subject: [PATCH 3/4] build: dynamic link first (mode_first) --- build.zig | 31 +++++++++++++++++-------------- flake.lock | 6 +++--- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/build.zig b/build.zig index 07056c0a1..9e15ab773 100644 --- a/build.zig +++ b/build.zig @@ -644,6 +644,14 @@ fn addDeps( return static_libs; } + // For dynamic linking, we prefer dynamic linking and to search by + // mode first. Mode first will search all paths for a dynamic library + // before falling back to static. + const dynamic_link_opts: std.build.Step.Compile.LinkSystemLibraryOptions = .{ + .preferred_link_mode = .Dynamic, + .search_strategy = .mode_first, + }; + // On Linux, we need to add a couple common library paths that aren't // on the standard search list. i.e. GTK is often in /usr/lib/x86_64-linux-gnu // on x86_64. @@ -711,20 +719,15 @@ fn addDeps( // Dynamic link if (!static) { step.addIncludePath(.{ .path = freetype.include_path_self }); - step.linkSystemLibrary("bzip2"); - step.linkSystemLibrary("freetype2"); - step.linkSystemLibrary("harfbuzz"); - step.linkSystemLibrary("libpng"); - step.linkSystemLibrary("pixman-1"); - step.linkSystemLibrary("zlib"); + step.linkSystemLibrary2("bzip2", dynamic_link_opts); + step.linkSystemLibrary2("freetype2", dynamic_link_opts); + step.linkSystemLibrary2("harfbuzz", dynamic_link_opts); + step.linkSystemLibrary2("libpng", dynamic_link_opts); + step.linkSystemLibrary2("pixman-1", dynamic_link_opts); + step.linkSystemLibrary2("zlib", dynamic_link_opts); if (font_backend.hasFontconfig()) { - step.linkSystemLibrary("fontconfig"); - - // Required on some systems, and pkg-config for fontconfig - // doesn't include it - step.linkSystemLibrary("libxml-2.0"); - step.linkSystemLibrary("uuid"); + step.linkSystemLibrary2("fontconfig", dynamic_link_opts); } } @@ -815,7 +818,7 @@ fn addDeps( // When we're targeting flatpak we ALWAYS link GTK so we // get access to glib for dbus. - if (flatpak) step.linkSystemLibrary("gtk4"); + if (flatpak) step.linkSystemLibrary2("gtk4", dynamic_link_opts); switch (app_runtime) { .none => {}, @@ -842,7 +845,7 @@ fn addDeps( }; try glfw.link(b, step, glfw_opts); - step.linkSystemLibrary("gtk4"); + step.linkSystemLibrary2("gtk4", dynamic_link_opts); }, } } diff --git a/flake.lock b/flake.lock index 3c2350a0f..cb4b30c43 100644 --- a/flake.lock +++ b/flake.lock @@ -126,11 +126,11 @@ "nixpkgs": "nixpkgs_2" }, "locked": { - "lastModified": 1690978078, - "narHash": "sha256-+S9s6sjOGuGCtA9njTdduJ7KjZm2tskxKhwGRKBK/xM=", + "lastModified": 1691410097, + "narHash": "sha256-HoZ/JwddeysSKNYr7h3AqReCMXPcqgqW/eBVOekxhFM=", "owner": "mitchellh", "repo": "zig-overlay", - "rev": "c9f51464f2c2c9c75f3d7fdc58c157d252545dec", + "rev": "9eda53e9001c54c810e24efe256c28d57a75d20d", "type": "github" }, "original": { From c7865f0aad507fe3c741c115524ecc507bcada08 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 7 Aug 2023 10:55:56 -0700 Subject: [PATCH 4/4] nix: remove unused libs --- nix/devshell.nix | 6 ------ 1 file changed, 6 deletions(-) diff --git a/nix/devshell.nix b/nix/devshell.nix index 815e4490b..f146eb3aa 100644 --- a/nix/devshell.nix +++ b/nix/devshell.nix @@ -30,8 +30,6 @@ , harfbuzz , libpng , libGL -, libuuid -, libxml2 , libX11 , libXcursor , libXext @@ -52,8 +50,6 @@ let freetype harfbuzz libpng - libuuid - libxml2 zlib libX11 @@ -107,8 +103,6 @@ in mkShell rec { freetype harfbuzz libpng - libuuid - libxml2 pixman zlib