Skip to content

Commit

Permalink
v0.8.0 (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
yvan-sraka committed Jan 24, 2023
1 parent 7af148d commit a416a75
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 53 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
10 changes: 5 additions & 5 deletions extra/cargo-cabal.scenario
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ $ cd greetings
$ # Add `hs-bindgen` to the dependencies list:
$ 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 @@ -46,14 +46,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 @@ -86,7 +86,7 @@ $ cat -n Cargo.toml
6 # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7
8 [dependencies]
9 hs-bindgen = { version = "0.7.1", features = ["full"] }
9 hs-bindgen = { version = "0.8.0", features = ["full"] }
10
11 [lib]
12 crate-type = ["staticlib"]
Expand Down
18 changes: 9 additions & 9 deletions flake.lock

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

2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
};
toolchain = (pkgs.rustChannelOf {
rustToolchain = ./rust-toolchain;
sha256 = "sha256-DzNEaW724O8/B8844tt5AVHmSjSQ3cmzlU4BP90oRlY=";
sha256 = "sha256-Zk2rxv6vwKFkTTidgjPm6gDsseVmmljVt201H7zuDkk=";
}).rust;
in {
defaultPackage = naersk'.buildPackage ./.;
Expand Down
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 guarantee:
///
/// - 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 a416a75

Please sign in to comment.