Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
164 changes: 164 additions & 0 deletions tur/hnefatafl-copenhagen/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
TERMUX_PKG_HOMEPAGE=https://hnefatafl.org/
# It's suspected that the discord URL being in the description of packages makes the upstream developer happy
# (speculatively, because it might increase their bug report coverage)
# is something like that acceptable to Termux?
TERMUX_PKG_DESCRIPTION="Copenhagen Hnefatafl client. Discord: https://discord.gg/h56CAHEBXd"
TERMUX_PKG_LICENSE="AGPL-V3"
TERMUX_PKG_MAINTAINER="@termux"
TERMUX_PKG_VERSION="5.6.1-2"
TERMUX_PKG_SRCURL="https://codeberg.org/dcampbell/hnefatafl/archive/v$TERMUX_PKG_VERSION.tar.gz"
TERMUX_PKG_SHA256=ce9f494eea0a3e92360efa96cec6251c1fa7a011864096804d073f8560e15ea0
TERMUX_PKG_DEPENDS="alsa-lib, libc++, hicolor-icon-theme, libxi, libxcursor, libxrandr, hicolor-icon-theme, openssl"
TERMUX_PKG_BUILD_IN_SRC=true
TERMUX_PKG_AUTO_UPDATE=true
TERMUX_PKG_HOSTBUILD=true

_install_ubuntu_packages() {
termux_download_ubuntu_packages "$@"

export HOSTBUILD_ROOTFS="${TERMUX_PKG_HOSTBUILD_DIR}/ubuntu_packages"

find "${HOSTBUILD_ROOTFS}" -type f -name '*.pc' | \
xargs -n 1 sed -i -e "s|/usr|${HOSTBUILD_ROOTFS}/usr|g"

find "${HOSTBUILD_ROOTFS}/usr/lib/x86_64-linux-gnu" -xtype l \
-exec sh -c "ln -snvf /usr/lib/x86_64-linux-gnu/\$(readlink \$1) \$1" sh {} \;

export LD_LIBRARY_PATH="${HOSTBUILD_ROOTFS}/usr/lib/x86_64-linux-gnu"
LD_LIBRARY_PATH+=":${HOSTBUILD_ROOTFS}/usr/lib"

export PKG_CONFIG_LIBDIR="${HOSTBUILD_ROOTFS}/usr/lib/x86_64-linux-gnu/pkgconfig"
PKG_CONFIG_LIBDIR+=":/usr/lib/x86_64-linux-gnu/pkgconfig"
}

termux_step_host_build() {
# build man page

if [[ "$TERMUX_ON_DEVICE_BUILD" == "true" ]]; then
return
fi

_install_ubuntu_packages libasound2-dev

termux_setup_rust
pushd "$TERMUX_PKG_SRCDIR"
rm -f .cargo/config.toml
cargo build \
--jobs "$TERMUX_PKG_MAKE_PROCESSES" \
--release
target/release/hnefatafl-ai --man --username ""
target/release/hnefatafl-client --man
target/release/hnefatafl-server --man
target/release/hnefatafl-server-full --man
target/release/hnefatafl-text-protocol --man
cp hnefatafl-ai.1 "$TERMUX_PKG_HOSTBUILD_DIR"/
cp hnefatafl-server.1 "$TERMUX_PKG_HOSTBUILD_DIR"/
cp hnefatafl-server-full.1 "$TERMUX_PKG_HOSTBUILD_DIR"/
cp hnefatafl-text-protocol.1 "$TERMUX_PKG_HOSTBUILD_DIR"/
cp hnefatafl-client.1 "$TERMUX_PKG_HOSTBUILD_DIR"/
popd
}

termux_step_pre_configure() {
termux_setup_rust

rm -f .cargo/config.toml

: "${CARGO_HOME:=$HOME/.cargo}"
export CARGO_HOME

cargo vendor
find ./vendor \
-mindepth 1 -maxdepth 1 -type d \
! -wholename ./vendor/cpal \
! -wholename ./vendor/smithay-client-toolkit \
! -wholename ./vendor/smithay-client-toolkit-0.19.2 \
! -wholename ./vendor/softbuffer \
! -wholename ./vendor/wayland-cursor \
! -wholename ./vendor/wgpu-hal \
! -wholename ./vendor/winit \
! -wholename ./vendor/x11rb-protocol \
! -wholename ./vendor/xkbcommon-dl \
-exec rm -rf '{}' \;

# currently, there is only one relevant version of "iced winit", but most likely,
# if this winit fork ever changes, the version necessary for hnefatafl-copenhagen
# will be connected to the version of hnefatafl-copenhagen, i.e. older versions of
# hnefatafl-copenhagen will require this commit, but future versions of hnefatafl-copenhagen
# could require a different commit.
local _ICED_WINIT_COMMIT=2e28820207080f4499382df3a4fedb0da81562d3
git clone https://github.com/iced-rs/winit.git vendor/winit-iced
git -C vendor/winit-iced checkout "$_ICED_WINIT_COMMIT"

find vendor/{cpal,smithay-client-toolkit,smithay-client-toolkit-0.19.2,softbuffer,wgpu-hal,winit,winit-iced,x11rb-protocol,xkbcommon-dl} -type f | \
xargs -n 1 sed -i \
-e 's|target_os = "android"|target_os = "disabling_this_because_it_is_for_building_an_apk"|g' \
-e 's|target_os = "linux"|target_os = "android"|g' \
-e "s|libxkbcommon.so.0|libxkbcommon.so|g" \
-e "s|libxkbcommon-x11.so.0|libxkbcommon-x11.so|g" \
-e "s|libxcb.so.1|libxcb.so|g" \
-e "s|/tmp|$TERMUX_PREFIX/tmp|g"

for crate in wayland-cursor softbuffer; do
local patch="$TERMUX_PKG_BUILDER_DIR/$crate-no-shm.diff"
local dir="vendor/$crate"
echo "Applying patch: $patch"
patch -p1 -d "$dir" < "${patch}"
done

local patch="$TERMUX_PKG_BUILDER_DIR/smithay-client-toolkit-0.19.2-no-shm.diff"
local dir="vendor/smithay-client-toolkit-0.19.2"
echo "Applying patch: $patch"
patch -p1 -d "$dir" < "${patch}"

local patch="$TERMUX_PKG_BUILDER_DIR/smithay-client-toolkit-0.20.0-no-shm.diff"
local dir="vendor/smithay-client-toolkit"
echo "Applying patch: $patch"
patch -p1 -d "$dir" < "${patch}"

echo "" >> Cargo.toml
echo '[patch.crates-io]' >> Cargo.toml
for crate in cpal smithay-client-toolkit softbuffer wayland-cursor wgpu-hal winit x11rb-protocol xkbcommon-dl; do
echo "$crate = { path = \"./vendor/$crate\" }" >> Cargo.toml
done
echo "smithay-client-toolkit2 = { package = \"smithay-client-toolkit\", path = \"./vendor/smithay-client-toolkit-0.19.2\" }" >> Cargo.toml
echo "" >> Cargo.toml
echo '[patch."git+https://github.com/iced-rs/winit.git"]' >> Cargo.toml
echo 'winit = { path = "./vendor/winit-iced" }' >> Cargo.toml
}

termux_step_make() {
cargo build \
--jobs "$TERMUX_PKG_MAKE_PROCESSES" \
--target "$CARGO_TARGET_NAME" \
--release

if [[ "$TERMUX_ON_DEVICE_BUILD" == "true" ]]; then
target/"$CARGO_TARGET_NAME"/release/hnefatafl-ai --man --username ""
target/"$CARGO_TARGET_NAME"/release/hnefatafl-client --man
target/"$CARGO_TARGET_NAME"/release/hnefatafl-server --man
target/"$CARGO_TARGET_NAME"/release/hnefatafl-server-full --man
target/"$CARGO_TARGET_NAME"/release/hnefatafl-text-protocol --man
else
cp "$TERMUX_PKG_HOSTBUILD_DIR"/hnefatafl-ai.1 "$TERMUX_PKG_BUILDDIR"/
cp "$TERMUX_PKG_HOSTBUILD_DIR"/hnefatafl-server.1 "$TERMUX_PKG_BUILDDIR"/
cp "$TERMUX_PKG_HOSTBUILD_DIR"/hnefatafl-server-full.1 "$TERMUX_PKG_BUILDDIR"/
cp "$TERMUX_PKG_HOSTBUILD_DIR"/hnefatafl-text-protocol.1 "$TERMUX_PKG_BUILDDIR"/
cp "$TERMUX_PKG_HOSTBUILD_DIR"/hnefatafl-client.1 "$TERMUX_PKG_BUILDDIR"/
fi
}

termux_step_make_install() {
install -Dm755 target/"$CARGO_TARGET_NAME"/release/hnefatafl-ai -t "$TERMUX_PREFIX"/bin
install -Dm755 target/"$CARGO_TARGET_NAME"/release/hnefatafl-client -t "$TERMUX_PREFIX"/bin
install -Dm755 target/"$CARGO_TARGET_NAME"/release/hnefatafl-server -t "$TERMUX_PREFIX"/bin
install -Dm755 target/"$CARGO_TARGET_NAME"/release/hnefatafl-server-full -t "$TERMUX_PREFIX"/bin
install -Dm755 target/"$CARGO_TARGET_NAME"/release/hnefatafl-text-protocol -t "$TERMUX_PREFIX"/bin
install -Dm644 website/src/images/helmet.svg "$TERMUX_PREFIX"/share/icons/hicolor/scalable/apps/org.hnefatafl.hnefatafl_client.svg
install -Dm644 hnefatafl-ai.1 "$TERMUX_PREFIX"/share/man/man1/hnefatafl-ai.1
install -Dm644 hnefatafl-client.1 "$TERMUX_PREFIX"/share/man/man1/hnefatafl-client.1
install -Dm644 hnefatafl-server.1 "$TERMUX_PREFIX"/share/man/man1/hnefatafl-server.1
install -Dm644 hnefatafl-server-full.1 "$TERMUX_PREFIX"/share/man/man1/hnefatafl-server-full.1
install -Dm644 hnefatafl-text-protocol.1 "$TERMUX_PREFIX"/share/man/man1/hnefatafl-text-protocol.1
install -Dm644 packages/hnefatafl-client.desktop "$TERMUX_PREFIX"/share/applications/hnefatafl-client.desktop
}
63 changes: 63 additions & 0 deletions tur/hnefatafl-copenhagen/smithay-client-toolkit-0.19.2-no-shm.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
diff --git a/src/shm/raw.rs b/src/shm/raw.rs
index 1a93b8b..8385829 100644
--- a/src/shm/raw.rs
+++ b/src/shm/raw.rs
@@ -5,7 +5,6 @@

use rustix::{
io::Errno,
- shm::{Mode, ShmOFlags},
};
use std::{
fs::File,
@@ -175,49 +174,11 @@ impl RawPool {
Ok(fd) => return Ok(fd),

// Not supported, use fallback.
- Err(Errno::NOSYS) => (),
+ Err(Errno::NOSYS) => return Err(Errno::NOSYS.into()),

Err(err) => return Err(Into::<io::Error>::into(err)),
};
}
-
- let time = SystemTime::now();
- let mut mem_file_handle = format!(
- "/smithay-client-toolkit-{}",
- time.duration_since(UNIX_EPOCH).unwrap().subsec_nanos()
- );
-
- loop {
- let flags = ShmOFlags::CREATE | ShmOFlags::EXCL | ShmOFlags::RDWR;
-
- let mode = Mode::RUSR | Mode::WUSR;
-
- match rustix::shm::shm_open(mem_file_handle.as_str(), flags, mode) {
- Ok(fd) => match rustix::shm::shm_unlink(mem_file_handle.as_str()) {
- Ok(_) => return Ok(fd),
-
- Err(errno) => {
- return Err(errno.into());
- }
- },
-
- Err(Errno::EXIST) => {
- // Change the handle if we happen to be duplicate.
- let time = SystemTime::now();
-
- mem_file_handle = format!(
- "/smithay-client-toolkit-{}",
- time.duration_since(UNIX_EPOCH).unwrap().subsec_nanos()
- );
-
- continue;
- }
-
- Err(Errno::INTR) => continue,
-
- Err(err) => return Err(err.into()),
- }
- }
}

#[cfg(target_os = "android")]
63 changes: 63 additions & 0 deletions tur/hnefatafl-copenhagen/smithay-client-toolkit-0.20.0-no-shm.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
diff --git a/src/shm/raw.rs b/src/shm/raw.rs
index 41ae652..686261d 100644
--- a/src/shm/raw.rs
+++ b/src/shm/raw.rs
@@ -5,7 +5,6 @@

use rustix::{
io::Errno,
- shm::{Mode, OFlags},
};
use std::{
fs::File,
@@ -188,49 +187,12 @@ impl RawPool {
Ok(fd) => return Ok(fd),

// Not supported, use fallback.
- Err(Errno::NOSYS) => (),
+ Err(Errno::NOSYS) => return Err(Errno::NOSYS.into()),

Err(err) => return Err(Into::<io::Error>::into(err)),
};
}

- let time = SystemTime::now();
- let mut mem_file_handle = format!(
- "/smithay-client-toolkit-{}",
- time.duration_since(UNIX_EPOCH).unwrap().subsec_nanos()
- );
-
- loop {
- let flags = OFlags::CREATE | OFlags::EXCL | OFlags::RDWR;
-
- let mode = Mode::RUSR | Mode::WUSR;
-
- match rustix::shm::open(mem_file_handle.as_str(), flags, mode) {
- Ok(fd) => match rustix::shm::unlink(mem_file_handle.as_str()) {
- Ok(_) => return Ok(fd),
-
- Err(errno) => {
- return Err(errno.into());
- }
- },
-
- Err(Errno::EXIST) => {
- // Change the handle if we happen to be duplicate.
- let time = SystemTime::now();
-
- mem_file_handle = format!(
- "/smithay-client-toolkit-{}",
- time.duration_since(UNIX_EPOCH).unwrap().subsec_nanos()
- );
-
- continue;
- }
-
- Err(Errno::INTR) => continue,
-
- Err(err) => return Err(err.into()),
- }
- }
}

#[cfg(target_os = "android")]
51 changes: 51 additions & 0 deletions tur/hnefatafl-copenhagen/softbuffer-no-shm.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
diff --git a/src/backends/x11.rs b/src/backends/x11.rs
index b01fb5f..2a30172 100644
--- a/src/backends/x11.rs
+++ b/src/backends/x11.rs
@@ -14,7 +14,7 @@ use raw_window_handle::{
};
use rustix::{
fd::{AsFd, BorrowedFd, OwnedFd},
- mm, shm as posix_shm,
+ mm,
};

use std::{
@@ -827,37 +827,6 @@ impl<D: ?Sized, W: ?Sized> Drop for X11Impl<D, W> {

/// Create a shared memory identifier.
fn create_shm_id() -> io::Result<OwnedFd> {
- use posix_shm::{Mode, OFlags};
-
- let mut rng = fastrand::Rng::new();
- let mut name = String::with_capacity(23);
-
- // Only try four times; the chances of a collision on this space is astronomically low, so if
- // we miss four times in a row we're probably under attack.
- for i in 0..4 {
- name.clear();
- name.push_str("softbuffer-x11-");
- name.extend(std::iter::repeat_with(|| rng.alphanumeric()).take(7));
-
- // Try to create the shared memory segment.
- match posix_shm::open(
- &name,
- OFlags::RDWR | OFlags::CREATE | OFlags::EXCL,
- Mode::RWXU,
- ) {
- Ok(id) => {
- posix_shm::unlink(&name).ok();
- return Ok(id);
- }
-
- Err(rustix::io::Errno::EXIST) => {
- tracing::warn!("x11: SHM ID collision at {} on try number {}", name, i);
- }
-
- Err(e) => return Err(e.into()),
- };
- }
-
Err(io::Error::new(
io::ErrorKind::Other,
"failed to generate a non-existent SHM name",
53 changes: 53 additions & 0 deletions tur/hnefatafl-copenhagen/wayland-cursor-no-shm.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
diff --git a/src/lib.rs b/src/lib.rs
index 9993801..ea64a47 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -59,7 +59,6 @@ use rustix::fs::Mode;
#[cfg(any(target_os = "linux", target_os = "android"))]
use rustix::fs::{memfd_create, MemfdFlags};
use rustix::io::Errno;
-use rustix::shm;
#[cfg(any(target_os = "linux", target_os = "android"))]
use std::ffi::CStr;

@@ -445,39 +444,10 @@ fn create_shm_fd() -> IoResult<OwnedFd> {
) {
Ok(fd) => return Ok(fd),
Err(Errno::INTR) => continue,
- Err(Errno::NOSYS) => break,
+ Err(Errno::NOSYS) => return Err(Errno::NOSYS.into()),
Err(errno) => return Err(errno.into()),
}
}
-
- // Fallback to using shm_open.
- let sys_time = SystemTime::now();
- let mut mem_file_handle = format!(
- "/wayland-cursor-rs-{}",
- sys_time.duration_since(UNIX_EPOCH).unwrap().subsec_nanos()
- );
- loop {
- match shm::open(
- mem_file_handle.as_str(),
- shm::OFlags::CREATE | shm::OFlags::EXCL | shm::OFlags::RDWR,
- Mode::RUSR | Mode::WUSR,
- ) {
- Ok(fd) => match shm::unlink(mem_file_handle.as_str()) {
- Ok(_) => return Ok(fd),
- Err(errno) => return Err(IoError::from(errno)),
- },
- Err(Errno::EXIST) => {
- // If a file with that handle exists then change the handle
- mem_file_handle = format!(
- "/wayland-cursor-rs-{}",
- sys_time.duration_since(UNIX_EPOCH).unwrap().subsec_nanos()
- );
- continue;
- }
- Err(Errno::INTR) => continue,
- Err(errno) => return Err(IoError::from(errno)),
- }
- }
}

struct IgnoreObjectData;