Skip to content

Commit

Permalink
build hunspell-sys also on Windows (#2)
Browse files Browse the repository at this point in the history
* build hunspell-sys also on Windows
- msvc does not support autotools &
  hunspell's build scripts itself do not play well with msvc
- just compile the hunspell-1.7.lib with cc "by hand"
- keep build without msvc as it is

* hide autotools vs cc behind features
- build-autotools to build with autotools
- build-cc to build with cc
- default is build-autotools
- added documentation into Readme.md to show
  how to enable build-cc

* updated build documentation in readme
- added information about target specific

* rewrote build hunspell
- by default hunspell is searched by pkg-config
- optionally - with feature bundled - hunspell is build from
  directory vendor by crate cc
- documented this in README.md
- extended .travis.yml to test building the bundled hunspell

* fixed typo in travis file
- for testing feature bundled

* rewrote travis config
- for testing all features in all rust versions

* Update README.md

Co-authored-by: Andy Russell <[email protected]>
  • Loading branch information
msuesskraut and euclio authored Jul 9, 2020
1 parent 8769fc9 commit d49c3bf
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 31 deletions.
18 changes: 10 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ rust:
- stable
- beta
- nightly
addons:
apt:
packages:
- libhunspell-dev
update: true
matrix:
allow_failures:
- rust: nightly
fast_finish: true
include:
- name: System Library
addons:
apt:
packages:
- libhunspell-dev
update: true
- name: Bundled Library
script:
- cargo build --verbose
- cargo test --verbose
- cargo build --verbose --features bundled
- cargo test --verbose --features bundled
12 changes: 9 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ travis-ci = { repository = "euclio/hunspell-sys" }
tempfile = "3.1.0"

[build-dependencies]
bindgen = "0.49.0"
pkg-config = "0.3.14"
autotools = "0.2.1"
bindgen = "0.54.1"
pkg-config = "0.3.17"
cc = { version = "1.0.58", optional = true }

[features]

default = []

bundled = ["cc"]
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,15 @@
Rust bindings for the [hunspell] C API.

[hunspell]: https://hunspell.github.io/

## Building

By default `hunspell-sys` searches for a hunspell library installation with `pkg-config`.

Optionally, the bundled code of hunspell can be compiled with the `cc` crate.
This behavior needs to activated by the `bundled` feature.

```toml
[dependencies]
hunspell-sys = { version = 0.1.3, features = ["bundled"] }
```
59 changes: 39 additions & 20 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ use std::env;
use std::error::Error;
use std::path::PathBuf;

use autotools;

fn main() -> Result<(), Box<dyn Error>> {
let mut builder = bindgen::Builder::default();

/// builds hunspell in the `vendor` git submodule with the
/// `cc` crate: ignore any hunspell's build-scripts and
/// just compile the source code to a static lib.
///
/// Note: list of *.cxx files might need to be updated,
/// if `vendor` git submodule is updated
#[cfg(feature = "bundled")]
fn build_or_find_hunspell() -> Result<bindgen::Builder, Box<dyn Error>> {
let libcpp = if cfg!(target_os = "macos") {
Some("dylib=c++")
} else if cfg!(target_os = "linux") {
Expand All @@ -15,24 +18,40 @@ fn main() -> Result<(), Box<dyn Error>> {
None
};

if pkg_config::probe_library("hunspell").is_err() {
let dst = autotools::Config::new("vendor")
.reconf("-ivf")
.cxxflag("-fPIC")
.build();
if let Some(link) = libcpp {
println!("cargo:rustc-link-lib={}", link);
}

println!(
"cargo:rustc-link-search=native={}",
dst.join("lib").display()
);
println!("cargo:rustc-link-lib=static=hunspell-1.7");
println!("cargo:rustc-link-lib=static=hunspell-1.7");

if let Some(link) = libcpp {
println!("cargo:rustc-link-lib={}", link);
}
cc::Build::new()
.file("vendor/src/hunspell/affentry.cxx")
.file("vendor/src/hunspell/affixmgr.cxx")
.file("vendor/src/hunspell/csutil.cxx")
.file("vendor/src/hunspell/filemgr.cxx")
.file("vendor/src/hunspell/hashmgr.cxx")
.file("vendor/src/hunspell/hunspell.cxx")
.file("vendor/src/hunspell/hunzip.cxx")
.file("vendor/src/hunspell/phonet.cxx")
.file("vendor/src/hunspell/replist.cxx")
.file("vendor/src/hunspell/suggestmgr.cxx")
.define("BUILDING_LIBHUNSPELL", "1")
.cpp(true)
.compile("hunspell-1.7");

builder = builder.clang_arg(format!("-I{}", dst.join("include").display()));
}
Ok(bindgen::Builder::default().clang_arg(format!("-I{}", "vendor/src")))
}

#[cfg(not(feature = "bundled"))]
fn build_or_find_hunspell() -> Result<bindgen::Builder, Box<dyn Error>> {
pkg_config::probe_library("hunspell")?;

Ok(bindgen::Builder::default())
}

fn main() -> Result<(), Box<dyn Error>> {

let builder = build_or_find_hunspell()?;

let bindings = builder
.header("wrapper.h")
Expand Down

0 comments on commit d49c3bf

Please sign in to comment.