From d1d6efba6a3906dfd1d871f0eb57716aa58beff5 Mon Sep 17 00:00:00 2001 From: Aleksa Sarai Date: Sat, 7 Sep 2024 03:10:54 +1000 Subject: [PATCH] capi: switch to cargo-c It seems cargo-c is the "recommended" way of building distro-friendly .so's for Rust crates and so switch to that. The lack of fine-grained SONAME control might be an issue in the future (when we do a 1.0 release, we probably don't want to do a full SONAME API breakage -- but maybe we can do a 1.0 release when pathrs is ready for packaging anyway). It also seems that the ability to do Version.map stuff is limited with cargo-c, but we can cross that bridge when we get to it (for the most part there aren't many cargo-c-specific changes). Signed-off-by: Aleksa Sarai --- .github/workflows/bindings.yml | 9 +++++++++ Cargo.toml | 18 ++++++++++++++---- Makefile | 10 +++++----- build.rs | 30 ------------------------------ 4 files changed, 28 insertions(+), 39 deletions(-) delete mode 100644 build.rs diff --git a/.github/workflows/bindings.yml b/.github/workflows/bindings.yml index 7210f471..5e0fd8ef 100644 --- a/.github/workflows/bindings.yml +++ b/.github/workflows/bindings.yml @@ -33,6 +33,9 @@ jobs: - uses: actions/checkout@v4 # Build and install libpathrs.so. - uses: dtolnay/rust-toolchain@stable + - uses: taiki-e/install-action@v2 + with: + tool: cargo-c - name: build libpathrs run: make release - name: install libpathrs @@ -50,6 +53,9 @@ jobs: - uses: actions/checkout@v4 # Build and install libpathrs.so. - uses: dtolnay/rust-toolchain@stable + - uses: taiki-e/install-action@v2 + with: + tool: cargo-c - name: build libpathrs run: make release - name: install libpathrs @@ -73,6 +79,9 @@ jobs: - uses: actions/checkout@v4 # Build and install libpathrs.so. - uses: dtolnay/rust-toolchain@stable + - uses: taiki-e/install-action@v2 + with: + tool: cargo-c - name: build libpathrs run: make release - name: install libpathrs diff --git a/Cargo.toml b/Cargo.toml index 520935f9..cc94de56 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,7 +33,20 @@ rust-version = "1.63" maintenance = { status = "experimental" } [lib] -crate-type = ["rlib", "cdylib", "staticlib"] +crate-type = ["rlib"] + +[package.metadata.capi.header] +subdirectory = false + +[package.metadata.capi.library] +name = "pathrs" +# TODO: In order to avoid breaking dependencies when we do our 1.0 release, we +# might need to fake the so version... +#version = "1.0.0" +#version_suffix_components = 1 +# Since we are cdylib, panic!s will cause aborts once they hit the FFI barrier +# anyway. We might as well reduce our code size if we're doing it. +rustflags = "-Cpanic=abort" [features] capi = ["dep:rand"] @@ -43,9 +56,6 @@ _test_as_root = [] [profile.release] # Enable link-time optimisations. lto = true -# Since we are cdylib, panic!s will cause aborts once they hit the FFI barrier -# anyway. We might as well reduce our code size if we're doing it. -panic = "abort" [dependencies] bitflags = "^2" diff --git a/Makefile b/Makefile index 773497c4..e821972b 100644 --- a/Makefile +++ b/Makefile @@ -18,19 +18,19 @@ CARGO ?= cargo CARGO_NIGHTLY ?= cargo +nightly SRC_FILES = $(shell find . -name '*.rs') -CAPI_FEATURES = --features=capi +DESTDIR ?= / .PHONY: debug debug: target/debug target/debug: $(SRC_FILES) - $(CARGO) build $(CAPI_FEATURES) + $(CARGO) cbuild .PHONY: release release: target/release target/release: $(SRC_FILES) - $(CARGO) build $(CAPI_FEATURES) --release + $(CARGO) cbuild --release .PHONY: smoke-test smoke-test: @@ -83,7 +83,7 @@ docs: .PHONY: install install: release - @echo "If you want to configure the install paths, use ./install.sh directly." + @echo "If you want to configure the install paths, use 'cargo cinstall' directly." @echo "[Sleeping for 3 seconds.]" @sleep 3s - ./install.sh + cargo cinstall --release --destdir=$(DESTDIR) --prefix=/usr diff --git a/build.rs b/build.rs deleted file mode 100644 index e3f0fe94..00000000 --- a/build.rs +++ /dev/null @@ -1,30 +0,0 @@ -/* - * libpathrs: safe path resolution on Linux - * Copyright (C) 2019-2021 Aleksa Sarai - * Copyright (C) 2019-2021 SUSE LLC - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or (at your - * option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see . - */ - -use std::env; - -fn main() { - // Add DT_SONAME to our cdylibs. - let name = "pathrs"; - let major = env::var("CARGO_PKG_VERSION_MAJOR").unwrap(); - println!( - "cargo:rustc-cdylib-link-arg=-Wl,-soname,lib{}.so.{}", - name, major - ); -}