From 4221ebd406592b8c439eaf56c6a6bed93a82ee3b Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Fri, 11 Apr 2025 20:43:13 +0200 Subject: [PATCH] wip 80k example --- Cargo.toml | 21 ++++++++- cbindgen.toml | 2 +- examples/lang_c/Makefile | 59 +++++++++++++++++++++----- examples/lang_c/include/servicepoint.h | 50 +++++++++++----------- examples/lang_c/src/main.c | 8 +++- flake.nix | 26 ++++++++---- src/bitmap.rs | 2 +- src/brightness_grid.rs | 2 +- 8 files changed, 121 insertions(+), 49 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 9e52832..7e5ed5d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,8 +17,17 @@ crate-type = ["staticlib", "cdylib", "rlib"] [build-dependencies] cbindgen = "0.28.0" -[dependencies] -servicepoint = { features = ["all_compressions"], git = "https://git.berlin.ccc.de/servicepoint/servicepoint/", branch = "next" } +[dependencies.servicepoint] +package = "servicepoint" +version = "0.13.2" +default-features = false +features = ["protocol_udp"] +git = "https://git.berlin.ccc.de/servicepoint/servicepoint/" +branch = "next" + +[features] +full = ["servicepoint/all_compressions", "servicepoint/default"] +default = ["full"] [lints.rust] missing-docs = "warn" @@ -26,3 +35,11 @@ unsafe_op_in_unsafe_fn = "warn" [package.metadata.docs.rs] all-features = true + +[profile.size-optimized] +inherits = "release" +opt-level = 'z' # Optimize for size +lto = true # Enable link-time optimization +codegen-units = 1 # Reduce number of codegen units to increase optimizations +panic = 'abort' # Abort on panic +strip = true # Strip symbols from binary diff --git a/cbindgen.toml b/cbindgen.toml index 7fc0fdf..bf2377c 100644 --- a/cbindgen.toml +++ b/cbindgen.toml @@ -26,7 +26,7 @@ sort_by = "Name" parse_deps = false [parse.expand] -all_features = true +features = ["full"] [export] include = [] diff --git a/examples/lang_c/Makefile b/examples/lang_c/Makefile index b28b9d3..1be4009 100644 --- a/examples/lang_c/Makefile +++ b/examples/lang_c/Makefile @@ -1,7 +1,47 @@ CC := gcc +CARGO := rustup run nightly cargo THIS_DIR := $(dir $(realpath $(lastword $(MAKEFILE_LIST)))) REPO_ROOT := $(THIS_DIR)/../../ +RUST_TARGET_DIR := $(REPO_ROOT)/target/x86_64-unknown-linux-musl/size-optimized + +RUSTFLAGS := -Zlocation-detail=none \ + -Zfmt-debug=none \ + -C linker=musl-gcc \ + -C link-arg=-s \ + -C link-arg=--gc-sections \ + -C link-arg=-z,norelro \ + -C link-arg=--hash-style=gnu \ + --crate-type=staticlib \ + -C panic=abort + +CARGOFLAGS := --manifest-path=$(REPO_ROOT)/Cargo.toml \ + --profile=size-optimized \ + --no-default-features \ + --target=x86_64-unknown-linux-musl \ + -Zbuild-std="core,std,alloc,proc_macro,panic_abort" \ + -Zbuild-std-features="panic_immediate_abort" \ + +CCFLAGS := -static -Os \ + -ffunction-sections -fdata-sections \ + -fwrapv -fomit-frame-pointer -fno-stack-protector\ + -fwhole-program \ + -nodefaultlibs -lservicepoint_binding_c -lc \ + -Wl,--gc-sections \ + -fno-unroll-loops \ + -fno-unwind-tables -fno-asynchronous-unwind-tables \ + -fmerge-all-constants \ + -Wl,-z,norelro \ + -Wl,--hash-style=gnu \ + -fvisibility=hidden \ + -Bsymbolic \ + -Wl,--exclude-libs,ALL \ + -fno-ident \ + #-fuse-ld=gold \ + -fno-exceptions + #-Wl,--icf=all \ + +export SERVICEPOINT_HEADER_OUT := $(THIS_DIR)/include build: out/lang_c @@ -15,20 +55,19 @@ run: out/lang_c PHONY: build clean dependencies run -out/lang_c: dependencies src/main.c +out/lang_c_unstripped: dependencies src/main.c mkdir -p out || true ${CC} src/main.c \ - -I include \ - -L $(REPO_ROOT)/target/release \ - -Wl,-Bstatic -lservicepoint_binding_c \ - -Wl,-Bdynamic -llzma \ - -o out/lang_c + -I $(SERVICEPOINT_HEADER_OUT) \ + -L $(RUST_TARGET_DIR)\ + $(CCFLAGS) \ + -o out/lang_c_unstripped +out/lang_c: out/lang_c_unstripped + strip -s -R .comment -R .gnu.version --strip-unneeded out/lang_c_unstripped -o out/lang_c +#strip -S --strip-unneeded --remove-section=.note.gnu.gold-version --remove-section=.comment --remove-section=.note --remove-section=.note.gnu.build-id --remove-section=.note.ABI-tag dependencies: FORCE mkdir -p include || true # generate servicepoint header and binary to link against - SERVICEPOINT_HEADER_OUT=$(THIS_DIR)/include cargo build \ - --manifest-path=$(REPO_ROOT)/Cargo.toml \ - --release - + ${CARGO} rustc $(CARGOFLAGS) -- $(RUSTFLAGS) FORCE: ; diff --git a/examples/lang_c/include/servicepoint.h b/examples/lang_c/include/servicepoint.h index e631bc0..ca235ca 100644 --- a/examples/lang_c/include/servicepoint.h +++ b/examples/lang_c/include/servicepoint.h @@ -168,6 +168,19 @@ typedef struct SPCharGrid SPCharGrid; */ typedef struct SPCommand SPCommand; +/** + * A connection to the display. + * + * # Examples + * + * ```C + * CConnection connection = sp_connection_open("172.23.42.29:2342"); + * if (connection != NULL) + * sp_connection_send_command(connection, sp_command_clear()); + * ``` + */ +typedef struct SPConnection SPConnection; + /** * A C-wrapper for grid containing codepage 437 characters. * @@ -189,19 +202,6 @@ typedef struct SPCp437Grid SPCp437Grid; */ typedef struct SPPacket SPPacket; -/** - * A connection to the display. - * - * # Examples - * - * ```C - * CConnection connection = sp_connection_open("172.23.42.29:2342"); - * if (connection != NULL) - * sp_connection_send_command(connection, sp_command_clear()); - * ``` - */ -typedef struct SPUdpConnection SPUdpConnection; - /** * Represents a span of memory (`&mut [u8]` ) as a struct usable by C code. * @@ -290,7 +290,7 @@ void sp_bitmap_fill(SPBitmap *bitmap, bool value); * * [SPCommand]: [crate::SPCommand] */ -void sp_bitmap_free(SPBitmap **bitmap); +void sp_bitmap_free(SPBitmap *bitmap); /** * Gets the current value at the specified position in the [SPBitmap]. @@ -1456,7 +1456,7 @@ SPCommand *sp_command_utf8_data(size_t x, SPCharGrid *grid); /** - * Closes and deallocates a [SPUdpConnection]. + * Closes and deallocates a [SPConnection]. * * # Panics * @@ -1466,13 +1466,13 @@ SPCommand *sp_command_utf8_data(size_t x, * * The caller has to make sure that: * - * - `connection` points to a valid [SPUdpConnection] + * - `connection` points to a valid [SPConnection] * - `connection` is not used concurrently or after this call */ -void sp_connection_free(SPUdpConnection *connection); +void sp_connection_free(SPConnection *connection); /** - * Creates a new instance of [SPUdpConnection]. + * Creates a new instance of [SPConnection]. * * returns: NULL if connection fails, or connected instance * @@ -1487,10 +1487,10 @@ void sp_connection_free(SPUdpConnection *connection); * - the returned instance is freed in some way, either by using a consuming function or * by explicitly calling `sp_connection_free`. */ -SPUdpConnection *sp_connection_open(const char *host); +SPConnection *sp_connection_open(const char *host); /** - * Sends a [SPCommand] to the display using the [SPUdpConnection]. + * Sends a [SPCommand] to the display using the [SPConnection]. * * The passed `command` gets consumed. * @@ -1505,15 +1505,15 @@ SPUdpConnection *sp_connection_open(const char *host); * * The caller has to make sure that: * - * - `connection` points to a valid instance of [SPUdpConnection] + * - `connection` points to a valid instance of [SPConnection] * - `command` points to a valid instance of [SPPacket] * - `command` is not used concurrently or after this call */ -bool sp_connection_send_command(const SPUdpConnection *connection, +bool sp_connection_send_command(const SPConnection *connection, SPCommand *command); /** - * Sends a [SPPacket] to the display using the [SPUdpConnection]. + * Sends a [SPPacket] to the display using the [SPConnection]. * * The passed `packet` gets consumed. * @@ -1528,11 +1528,11 @@ bool sp_connection_send_command(const SPUdpConnection *connection, * * The caller has to make sure that: * - * - `connection` points to a valid instance of [SPUdpConnection] + * - `connection` points to a valid instance of [SPConnection] * - `packet` points to a valid instance of [SPPacket] * - `packet` is not used concurrently or after this call */ -bool sp_connection_send_packet(const SPUdpConnection *connection, +bool sp_connection_send_packet(const SPConnection *connection, SPPacket *packet); /** diff --git a/examples/lang_c/src/main.c b/examples/lang_c/src/main.c index aeb811b..6a4b30e 100644 --- a/examples/lang_c/src/main.c +++ b/examples/lang_c/src/main.c @@ -2,14 +2,20 @@ #include "servicepoint.h" int main(void) { - SPUdpConnection *connection = sp_connection_open("localhost:2342"); + SPConnection *connection = sp_connection_open("localhost:2342"); if (connection == NULL) return 1; SPBitmap *pixels = sp_bitmap_new(SP_PIXEL_WIDTH, SP_PIXEL_HEIGHT); + if (pixels == NULL) + return 1; + sp_bitmap_fill(pixels, true); SPCommand *command = sp_command_bitmap_linear_win(0, 0, pixels, SP_COMPRESSION_CODE_UNCOMPRESSED); + if (command == NULL) + return 1; + sp_connection_send_command(connection, command); sp_connection_free(connection); diff --git a/flake.nix b/flake.nix index 9318237..1f0ac97 100644 --- a/flake.nix +++ b/flake.nix @@ -33,25 +33,35 @@ { pkgs, system }: { default = pkgs.mkShell rec { - packages = with pkgs; [ + buildInputs = with pkgs;[ + xe + xz + libgcc + #glibc.static + musl + libunwind + ]; + + nativeBuildInputs = with pkgs;[ (pkgs.symlinkJoin { name = "rust-toolchain"; paths = with pkgs; [ - rustc - cargo - rustPlatform.rustcSrc - rustfmt - clippy + #rustc + #cargo + #rustPlatform.rustcSrc + #rustfmt + #clippy cargo-expand cargo-tarpaulin + rustup ]; }) gcc gnumake - xe - xz pkg-config + ]; + RUST_SRC_PATH = "${pkgs.rust.packages.stable.rustPlatform.rustLibSrc}"; }; } diff --git a/src/bitmap.rs b/src/bitmap.rs index 45993d8..8ac5192 100644 --- a/src/bitmap.rs +++ b/src/bitmap.rs @@ -3,7 +3,7 @@ //! prefix `sp_bitmap_` use servicepoint::{DataRef, Grid}; -use std::ptr::{null_mut, NonNull}; +use std::ptr::{NonNull}; use crate::byte_slice::SPByteSlice; diff --git a/src/brightness_grid.rs b/src/brightness_grid.rs index cfd2589..6087db0 100644 --- a/src/brightness_grid.rs +++ b/src/brightness_grid.rs @@ -5,7 +5,7 @@ use crate::SPByteSlice; use servicepoint::{DataRef, Grid}; use std::convert::Into; -use std::intrinsics::transmute; +use std::mem::transmute; use std::ptr::NonNull; /// see [servicepoint::Brightness::MIN]