Skip to content

Commit

Permalink
fix(x11/rnote): Fix compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
EDLLT committed Sep 20, 2024
1 parent 39ee37e commit 4caf872
Show file tree
Hide file tree
Showing 7 changed files with 1,107 additions and 14 deletions.
55 changes: 41 additions & 14 deletions x11-packages/rnote/build.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,33 @@
TERMUX_PKG_HOMEPAGE="https://github.com/flxzt/rnote"
TERMUX_PKG_DESCRIPTION="A simple drawing application to create handwritten notes"
TERMUX_PKG_DESCRIPTION="An infinite canvas vector-based drawing application for handwritten notes"
TERMUX_PKG_LICENSE="GPL-3.0"
TERMUX_PKG_MAINTAINER="@termux"
TERMUX_PKG_VERSION="0.11.0"
TERMUX_PKG_SRCURL="https://github.com/flxzt/rnote/archive/v${TERMUX_PKG_VERSION}/v${TERMUX_PKG_VERSION}.tar.gz"
TERMUX_PKG_SHA256=b133d4331963d3c09d3a7477f60fc4c5072471dcbf459379a593ca1724164af4
TERMUX_PKG_AUTO_UPDATE=true
TERMUX_PKG_DEPENDS="gdk-pixbuf, gettext, glib, graphene, gtk4, hicolor-icon-theme, libadwaita, libcairo, pipewire, pango, poppler"
TERMUX_PKG_BUILD_DEPENDS="libiconv"

__fetch_gettext_rs() {
# This version is relative to gettext-sys provided by the gettext-rs crate
local _version="0.21.4"
local _url="https://github.com/gettext-rs/gettext-rs/archive/refs/tags/gettext-sys-$_version.tar.gz"
local _path="$TERMUX_PKG_CACHEDIR/gettext-v$_version.tar.gz"
termux_download "$_url" "$_path" SKIP_CHECKSUM
tar xf "$_path" -C "$TERMUX_PKG_SRCDIR"
mv "gettext-rs-gettext-sys-$_version" "gettext-source"


# Fetch latest and greatest gettext from upstream and place it inside gettext-source/gettext-sys
local _upstream_source_version="0.22.5"
wget "https://mirrors.kernel.org/gnu/gettext/gettext-${_upstream_source_version}.tar.xz" -P "$TERMUX_PKG_SRCDIR/gettext-source/gettext-sys"
}

__patch_gettext_rs() {
# Patch gettext-rs crate to use a newer gettext version because the old one doesn't compile properly with clang
patch -p1 -d "$TERMUX_PKG_SRCDIR/gettext-source" < "$TERMUX_PKG_BUILDER_DIR"/gettext-rs-crate-patch.diff
}

termux_step_pre_configure() {
termux_setup_cmake
Expand All @@ -21,20 +42,26 @@ termux_step_pre_configure() {
export PKG_CONFIG="${_WRAPPER_BIN}/pkg-config"
fi
export PATH="${_WRAPPER_BIN}:${PATH}"

# Fetch and patch the crate to use a newer upstream gettext version
__fetch_gettext_rs
__patch_gettext_rs
}

termux_step_make() {
termux_setup_rust

cd "${TERMUX_PKG_SRCDIR}"

RUSTFLAGS="-C link-arg=-Wl,-rpath=${TERMUX_PREFIX}/lib -C link-arg=-L${TERMUX_PREFIX}/lib -C link-arg=-liconv" \
cargo build \
--jobs "${TERMUX_PKG_MAKE_PROCESSES}" \
--target "${CARGO_TARGET_NAME}" \
--package rnote \
--release
}

termux_step_post_make_install() {
(
cd "${TERMUX_PKG_SRCDIR}"

termux_setup_rust

GETTEXT_SYSTEM=1 \
GETTEXT_DIR="${TERMUX_PREFIX}" \
cargo build \
--jobs "${TERMUX_PKG_MAKE_PROCESSES}" \
--target "${CARGO_TARGET_NAME}" \
--package rnote \
--release
)
cd "${TERMUX_PKG_SRCDIR}"
install -Dm755 "target/$CARGO_TARGET_NAME/release/rnote" -t "$TERMUX_PREFIX/bin"
}
13 changes: 13 additions & 0 deletions x11-packages/rnote/custom-gettext-rs-crate-Cargo.toml.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
diff --git a/Cargo.toml b/Cargo.toml
index 6a6aa16..58b576a 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -34,7 +34,7 @@ flate2 = "1.0"
fs_extra = "1.3"
futures = "0.3.30"
geo = "0.28.0"
-gettext-rs = { version = "0.7.0", features = ["gettext-system"] }
+gettext-rs = { path = "./gettext-source/gettext-rs" }
gio = "0.19.5"
glib = "0.19.7"
glib-build-tools = "0.19.0"
File renamed without changes.
106 changes: 106 additions & 0 deletions x11-packages/rnote/fix-compile-gresource.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
diff --git a/crates/rnote-ui/build.rs b/crates/rnote-ui/build.rs
index 9e5c07e..cd7baa5 100644
--- a/crates/rnote-ui/build.rs
+++ b/crates/rnote-ui/build.rs
@@ -1,5 +1,11 @@
fn main() -> anyhow::Result<()> {
- compile_gresources()?;
+ let is_cross_compiling = detect_cross_compilation();
+
+ if !is_cross_compiling {
+ println!("cargo:rustc-cfg=feature=\"use_glib_build_tools\"");
+ }
+
+ compile_gresources(is_cross_compiling)?;

#[cfg(windows)]
compile_icon_winres()?;
@@ -18,11 +24,64 @@ fn compile_icon_winres() -> anyhow::Result<()> {
.context("Failed to compile winresource resource")
}

-fn compile_gresources() -> anyhow::Result<()> {
- glib_build_tools::compile_resources(
- &["data"],
- "data/resources.gresource.xml",
- "compiled.gresource",
- );
- Ok(())
+fn detect_cross_compilation() -> bool {
+ let host = std::env::var("HOST").unwrap_or_default();
+ let target = std::env::var("TARGET").unwrap_or_default();
+ host != target
+}
+
+fn compile_gresources(is_cross_compiling: bool) -> anyhow::Result<()> {
+ use std::env;
+ use std::path::PathBuf;
+ use std::process::Command;
+
+ let out_dir = env::var("OUT_DIR").expect("OUT_DIR not set");
+ let output_path = PathBuf::from(&out_dir).join("compiled.gresource");
+
+ // First, try using the system's glib-compile-resources
+ let system_result = Command::new("glib-compile-resources")
+ .args(&[
+ "--sourcedir=data",
+ "data/resources.gresource.xml",
+ &format!("--target={}", output_path.display()),
+ ])
+ .status();
+
+ match system_result {
+ Ok(status) if status.success() => return Ok(()),
+ Ok(_) => println!("glib-compile-resources command failed, trying fallback method..."),
+ Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
+ println!("glib-compile-resources not found, trying fallback method...")
+ }
+ Err(e) => println!(
+ "Error executing glib-compile-resources: {}, trying fallback method...",
+ e
+ ),
+ }
+
+ // If cross-compiling, don't use glib_build_tools
+ if is_cross_compiling {
+ return Err(anyhow::anyhow!(
+ "Failed to compile gresources: system glib-compile-resources failed and we're cross-compiling. \
+ Please ensure you have glib development tools installed on your target system."
+ ));
+ }
+
+ // If not cross-compiling and system command fails, fall back to glib_build_tools if available
+ #[cfg(feature = "use_glib_build_tools")]
+ {
+ println!("Attempting to use glib_build_tools::compile_resources...");
+ glib_build_tools::compile_resources(
+ &["data"],
+ "data/resources.gresource.xml",
+ output_path.to_str().unwrap(),
+ );
+ Ok(())
+ }
+
+ #[cfg(not(feature = "use_glib_build_tools"))]
+ Err(anyhow::anyhow!(
+ "Failed to compile gresources: system glib-compile-resources failed and glib_build_tools is not available. \
+ Please ensure you have glib development tools installed on your system."
+ ))
}

diff --git a/crates/rnote-ui/Cargo.toml b/crates/rnote-ui/Cargo.toml
index 426d8e9..55447fc 100644
--- a/crates/rnote-ui/Cargo.toml
+++ b/crates/rnote-ui/Cargo.toml
@@ -58,7 +58,10 @@ url = { workspace = true }

[build-dependencies]
anyhow = { workspace = true }
-glib-build-tools = { workspace = true }
+glib-build-tools = { workspace = true, optional = true }
+
+[features]
+use_glib_build_tools = ["glib-build-tools"]

[target.'cfg(windows)'.build-dependencies]
winresource = { workspace = true }
180 changes: 180 additions & 0 deletions x11-packages/rnote/gettext-rs-crate-patch.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
Patch gettext-rs crate to use the a custom gettext fetched from upstream instead
Patch systest crate to skip testing gettext binary when cross compiling

diff --git a/gettext-rs/Cargo.toml b/gettext-rs/Cargo.toml
index f98bba8..b374080 100644
--- a/gettext-rs/Cargo.toml
+++ b/gettext-rs/Cargo.toml
@@ -18,7 +18,7 @@ name = "gettextrs"
gettext-system = ["gettext-sys/gettext-system"]

[dependencies.gettext-sys]
-version = "0.21.0"
+version = "0.22.0"
path = "../gettext-sys"

[dependencies]
diff --git a/gettext-rs/tests/integration.rs b/gettext-rs/tests/integration.rs
index bae3cc1..8d52953 100644
--- a/gettext-rs/tests/integration.rs
+++ b/gettext-rs/tests/integration.rs
@@ -86,7 +86,7 @@ fn dcgettext_fn() {
);

assert_eq!(
- dcgettext("bound_domain", "Hello, World!", LocaleCategory::LcAll),
+ dcgettext("bound_domain", "Hello, World!", LocaleCategory::LcMessages),
"Hello, World!"
);
}
@@ -156,7 +156,7 @@ fn dcngettext_fn() {
"Hello, World!",
"Hello, Worlds!",
1,
- LocaleCategory::LcAll
+ LocaleCategory::LcMessages
),
"Hello, World!"
);
@@ -166,7 +166,7 @@ fn dcngettext_fn() {
"Hello, World!",
"Hello, Worlds!",
2,
- LocaleCategory::LcAll
+ LocaleCategory::LcMessages
),
"Hello, Worlds!"
);
diff --git a/gettext-sys/Cargo.toml b/gettext-sys/Cargo.toml
index b5f849c..3baf2fd 100644
--- a/gettext-sys/Cargo.toml
+++ b/gettext-sys/Cargo.toml
@@ -2,7 +2,7 @@
name = "gettext-sys"
description = "Raw FFI bindings for gettext"
license = "MIT"
-version = "0.21.4"
+version = "0.22.5"
authors = ["Brian Olsen <[email protected]>", "Alexander Batischev <[email protected]>"]
build = "build.rs"
links = "gettext"
diff --git a/gettext-sys/build.rs b/gettext-sys/build.rs
index 7772f74..42bc167 100644
--- a/gettext-sys/build.rs
+++ b/gettext-sys/build.rs
@@ -191,7 +191,7 @@ fn main() {
let mut cmd = Command::new("tar");
cmd.current_dir(&build_dir.join("gettext"))
.arg("xJf")
- .arg(&src.join("gettext-0.21.tar.xz"))
+ .arg(&src.join("gettext-0.22.5.tar.xz"))
.arg("--strip-components")
.arg("1");
if host.contains("windows") {
diff --git a/systest/Cargo.toml b/systest/Cargo.toml
index 61895bb..43462d8 100644
--- a/systest/Cargo.toml
+++ b/systest/Cargo.toml
@@ -10,3 +10,6 @@ gettext-sys = { path = "../gettext-sys" }

[build-dependencies]
ctest2 = "0.4"
+
+[features]
+cross_compiling = []
diff --git a/systest/build.rs b/systest/build.rs
index 42650d9..c8bbca1 100644
--- a/systest/build.rs
+++ b/systest/build.rs
@@ -5,9 +5,18 @@ use std::path::{Path, PathBuf};
use std::process::{self, Command};

fn main() {
+ let target = env::var("TARGET").unwrap();
+ let host = env::var("HOST").unwrap();
+ let is_cross = target != host;
+
+ // Set the cross_compiling flag before the tests
+ if is_cross {
+ println!("cargo:rustc-cfg=cross_compiling");
+ println!("cargo::rustc-check-cfg=cfg(cross_compiling)");
+ }
+
let mut cfg = ctest2::TestGenerator::new();

- let target = env::var("TARGET").unwrap();
if target.contains("freebsd") {
cfg.include("/usr/local/include");
}
@@ -22,24 +31,38 @@ fn main() {
// Skip ptr check because the symbol name is different between glibc
// implementation and static lib.
// eg. gettext is libintl_gettext in static lib
- if env::var_os("GETTEXT_SYSTEM").is_none() || env::var("TARGET").unwrap().contains("windows") {
+ if env::var_os("GETTEXT_SYSTEM").is_none() || target.contains("windows") {
println!("Skipping ptr check");
cfg.skip_fn_ptrcheck(|_| true);
}

cfg.generate("../gettext-sys/lib.rs", "all.rs");

- // Check that we can find and run gettext binary
+ // Check for the existence of the gettext binary
let cmd = if let Some(bin) = env::var_os("DEP_GETTEXT_BIN") {
Path::new(&bin).join("gettext")
} else {
PathBuf::from("gettext")
};
- let c = Command::new(&cmd).arg("--version").spawn();
- if let Ok(mut child) = c {
- assert!(child.wait().unwrap().success());
- } else {
- println!("Could not run {}", cmd.display());
+
+ if !cmd.exists() {
+ println!(
+ "cargo:warning=Gettext binary not found at {}",
+ cmd.display()
+ );
process::exit(1);
}
+
+ // Only run the binary if not cross-compiling
+ if !is_cross {
+ let c = Command::new(&cmd).arg("--version").spawn();
+ if let Ok(mut child) = c {
+ assert!(child.wait().unwrap().success());
+ } else {
+ println!("Could not run {}", cmd.display());
+ process::exit(1);
+ }
+ } else {
+ println!("cargo:warning=Cross-compiling detected. Gettext binary found but not executed.");
+ }
}
diff --git a/systest/src/main.rs b/systest/src/main.rs
index befa675..522275d 100644
--- a/systest/src/main.rs
+++ b/systest/src/main.rs
@@ -1,7 +1,20 @@
#![allow(bad_style)]
-
+#![allow(unused_imports)]
extern crate gettext_sys;

+#[cfg(not(feature = "cross_compiling"))]
use gettext_sys::*;

+#[cfg(not(cross_compiling))]
include!(concat!(env!("OUT_DIR"), "/all.rs"));
+
+#[cfg(feature = "cross_compiling")]
+fn main() {
+ println!("Cross-compilation detected. Skipping system tests.");
+}
+
+#[cfg(feature = "cross_compiling")]
+#[test]
+fn dummy_cross_compile_test() {
+ println!("Cross-compilation detected. Skipping system tests.");
+}
18 changes: 18 additions & 0 deletions x11-packages/rnote/no-debug-Cargo.toml.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
diff --git a/Cargo.toml b/Cargo.toml
index 6a6aa16..7043460 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -94,11 +94,11 @@ piet-cairo = { git = "https://github.com/linebender/piet", rev = "02eb5f0152e893

[profile.dev]
debug = true
-opt-level = 2
+opt-level = 0 # We want proper gdb debugging for dev builds

[profile.release]
codegen-units = 1
lto = "fat"
opt-level = 3
# We want to be able to debug in the release build as well
-debug = true
+#debug = true # Performance and size matters for termux's packaging
Loading

0 comments on commit 4caf872

Please sign in to comment.