Skip to content

Commit

Permalink
v0.8.0
Browse files Browse the repository at this point in the history
- fix crate name requirements and clean command dispay message ;

- update `flake.lock`, now relying on `github:oxalica/rust-overlay` since
  `github:mozilla/nixpkgs-mozilla` was broken with Rust 1.67.0 ...
  • Loading branch information
yvan-sraka committed Jan 27, 2023
1 parent 7af148d commit 2ed1fc5
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 103 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ license = "MIT OR Apache-2.0"
name = "cargo-cabal"
repository = "https://github.com/yvan-sraka/cargo-cabal"
rust-version = "1.62.1"
version = "0.7.1"
version = "0.8.0"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Add `hs-bindgen` to the dependencies list:
```text
$ cargo add hs-bindgen --features full
Updating crates.io index
Adding hs-bindgen v0.7.1 to dependencies.
Adding hs-bindgen v0.8.0 to dependencies.
Features:
+ antlion
+ full
Expand Down Expand Up @@ -79,14 +79,14 @@ $ cargo build
Compiling semver v0.9.0
Compiling semver v1.0.14
Compiling lazy_static v1.4.0
Compiling hs-bindgen-traits v0.7.1
Compiling hs-bindgen-traits v0.8.0
Compiling rustc_version v0.2.3
Compiling hs-bindgen-attribute v0.7.2
Compiling thiserror-impl v1.0.37
Compiling displaydoc v0.2.3
Compiling hs-bindgen-types v0.7.1
Compiling hs-bindgen-types v0.8.0
Compiling toml v0.5.9
Compiling hs-bindgen v0.7.1
Compiling hs-bindgen v0.8.0
Compiling greetings v0.1.0 (/Users/yvan/demo/greetings)
error: custom attribute panicked
--> src/lib.rs:3:1
Expand Down Expand Up @@ -131,7 +131,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
hs-bindgen = { version = "0.7.1", features = ["full"] }
hs-bindgen = { version = "0.8.0", features = ["full"] }

[lib]
crate-type = ["staticlib"]
Expand Down
82 changes: 42 additions & 40 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 8 additions & 25 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -1,34 +1,17 @@
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
rust-overlay.url = "github:oxalica/rust-overlay";
flake-utils.url = "github:numtide/flake-utils";
naersk.url = "github:nix-community/naersk/master";
nixpkgs-mozilla = {
url = "github:mozilla/nixpkgs-mozilla";
flake = false;
};
};

outputs = { self, flake-utils, naersk, nixpkgs, nixpkgs-mozilla }:
outputs = { self, nixpkgs, rust-overlay, flake-utils, ... }:
flake-utils.lib.eachDefaultSystem (system:
let
naersk' = pkgs.callPackage naersk {
cargo = toolchain;
rustc = toolchain;
overlays = [ (import rust-overlay) ];
pkgs = import nixpkgs { inherit system overlays; };
in with pkgs; {
devShells.default = mkShell {
buildInputs = [ (rust-bin.fromRustupToolchainFile ./rust-toolchain) ];
};
pkgs = (import nixpkgs) {
inherit system;
overlays = [ (import nixpkgs-mozilla) ];
};
toolchain = (pkgs.rustChannelOf {
rustToolchain = ./rust-toolchain;
sha256 = "sha256-DzNEaW724O8/B8844tt5AVHmSjSQ3cmzlU4BP90oRlY=";
}).rust;
in {
defaultPackage = naersk'.buildPackage ./.;
devShell = with pkgs;
mkShell {
buildInputs = [ libiconv toolchain ];
RUST_SRC_PATH = rustPlatform.rustLibSrc;
};
});
}
9 changes: 3 additions & 6 deletions src/cabal.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
/// Generate user `.cabal`, taking `--enable-nix` option into account
pub(crate) fn generate(name: &str, module: &str, version: &str, enable_nix: bool) -> String {
let lib_name = name.replace('-', "_"); // When a lib is created, "-" is replaced by "_"
let package_name = name.replace('_', "-"); // cabal does not expect "_" for packages names

let build_type = if enable_nix {
"
build-type: Simple"
Expand All @@ -20,7 +17,7 @@ custom-setup
-- `haskell.nix` tell GHC linker where to find the `libNAME.a` by setting
-- automatically `extra-lib-dirs`:
-- https://input-output-hk.github.io/haskell.nix/tutorials/pkg-map.html
extra-libraries: {lib_name}
extra-libraries: {name}
-- Cross-compilation to target `x86_64-w64-mingw32-cc` thrown a lot of
-- `undefined reference to 'X'` errors during linking stage ...
Expand All @@ -35,7 +32,7 @@ custom-setup
format!(
"
-- Libraries that are bundled with the package.
extra-bundled-libraries: {lib_name}"
extra-bundled-libraries: {name}"
)
};

Expand All @@ -52,7 +49,7 @@ custom-setup
-- documentation, see: http://haskell.org/cabal/users-guide/
--
-- The name of the package.
name: {package_name}
name: {module}
-- The package version.
-- See the Haskell package versioning policy (PVP) for standards
Expand Down
5 changes: 5 additions & 0 deletions src/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ pub(crate) struct Lib {
pub(crate) crate_type: Option<Vec<String>>,
}

/// We allow `staticlib` and `cdylib` target only since `dylib` doesn't offer
/// the same ABI stability guarantees:
///
/// - https://users.rust-lang.org/t/what-is-the-difference-between-dylib-and-cdylib/28847
/// - https://users.rust-lang.org/t/abi-stability-guarantee-of-dylib-vs-cdylib/50879
#[derive(Debug, PartialEq)]
pub(crate) enum CrateType {
/// `staticlib` target, which is what you want (really)
Expand Down
2 changes: 0 additions & 2 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,4 @@ pub(crate) enum Error {
* re-running `cargo cabal init --overwrite` command
*/
FlakeFileExist,
/// `{0}` is an invalid crate name, it should be prefixed by `C` like `C{0}`
InvalidCrateName(String)
}
2 changes: 1 addition & 1 deletion src/hsbindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// under an `[hs-bindgen]` manifest key directly in `Cargo.toml` (even if this
// would trigger a `unused manifest key` warning at `cargo build`)?

const VERSION: &str = "0.7.1";
const VERSION: &str = "0.8.0";

/// Generate content of `hsbindgen.toml` file, a neat way to share config option
/// between `hs-bindgen` and `cargo-cabal`!
Expand Down
37 changes: 15 additions & 22 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
//! ```text
//! $ cargo add hs-bindgen --features full
//! Updating crates.io index
//! Adding hs-bindgen v0.7.1 to dependencies.
//! Adding hs-bindgen v0.8.0 to dependencies.
//! Features:
//! + antlion
//! + full
Expand Down Expand Up @@ -77,14 +77,14 @@
//! Compiling semver v0.9.0
//! Compiling semver v1.0.14
//! Compiling lazy_static v1.4.0
//! Compiling hs-bindgen-traits v0.7.1
//! Compiling hs-bindgen-traits v0.8.0
//! Compiling rustc_version v0.2.3
//! Compiling hs-bindgen-attribute v0.7.2
//! Compiling thiserror-impl v1.0.37
//! Compiling displaydoc v0.2.3
//! Compiling hs-bindgen-types v0.7.1
//! Compiling hs-bindgen-types v0.8.0
//! Compiling toml v0.5.9
//! Compiling hs-bindgen v0.7.1
//! Compiling hs-bindgen v0.8.0
//! Compiling greetings v0.1.0 (/Users/yvan/demo/greetings)
//! error: custom attribute panicked
//! --> src/lib.rs:3:1
Expand Down Expand Up @@ -129,7 +129,7 @@
//! # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
//!
//! [dependencies]
//! hs-bindgen = { version = "0.7.1", features = ["full"] }
//! hs-bindgen = { version = "0.8.0", features = ["full"] }
//!
//! [lib]
//! crate-type = ["staticlib"]
Expand Down Expand Up @@ -323,15 +323,6 @@ enum Commands {
fn main() {
if let Err(e) = routine() {
println!("{}{}", Colour::Red.bold().paint("Error: "), e);
} else {
println!(
"\
Cabal files generated!
**********************
You should now be able to compile your library with `cabal build` and should
add `hs-bindgen` to your crate dependencies list and decorate the Rust function
you want to expose with `#[hs_bindgen]` attribute macro."
);
}
}

Expand Down Expand Up @@ -373,12 +364,6 @@ fn init(
clean(name)?;
}

// Check that crate name is prefixed by `C` ...
// https://gitlab.haskell.org/ghc/ghc/-/issues/22564#note_469030
name.starts_with('C')
.then_some(())
.ok_or_else(|| Error::InvalidCrateName(name.to_owned()))?;

// Check that project have a `crate-type` target ...
let crate_type = get_crate_type(root).ok_or(Error::NoCargoLibTarget)?;

Expand Down Expand Up @@ -429,12 +414,20 @@ fn init(
.map_err(|_| Error::FailedToWriteFile("Setup.lhs".to_owned()))?;
}

println!(
"\
Cabal files generated!
**********************
You should now be able to compile your library with `cabal build` and should
add `hs-bindgen` to your crate dependencies list and decorate the Rust function
you want to expose with `#[hs_bindgen]` attribute macro."
);

Ok(())
}

fn clean(name: &str) -> Result<(), Error> {
let cabal = format!("{name}.cabal");
let _ = fs::remove_file(&cabal);
let _ = fs::remove_file(format!("{name}.cabal"));
let _ = fs::remove_file(".hsbindgen");
let _ = fs::remove_file("hsbindgen.toml");
let _ = fs::remove_file("Setup.hs");
Expand Down

0 comments on commit 2ed1fc5

Please sign in to comment.