Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print proper cargo:include values #139

Merged
merged 1 commit into from Mar 29, 2019
Merged

Conversation

@spl
Copy link
Contributor

spl commented Mar 28, 2019

In order to build a Rust crate with C/C++ that uses the HarfBuzz library referenced by the harfbuzz crate, I need to know where the header files are. I believe the standard way of doing that is the links key/value environment variables. In particular, I'm expecting DEP_HARFBUZZ_INCLUDE to tell me the include path for hb.h and other header files. Unfortunately, this doesn't seem to be working correctly now. This PR is an attempt to resolve that.

Summary of changes:

  • Prints proper values for cargo:include in the build script for both the pkg-config and vendored cases.
  • Changes a use of to_str().unwrap() to PathBuf::display(). I believe this is better, but I'm happy to be corrected.

As far as adding a useful feature goes, I think this PR is complete. However, there are a couple of potential improvements that would be nice, but I'm not sure how to go about them.

not(feature = "build-native-harfbuzz")

I'm not exactly sure what disabling the feature build-native-harfbuzz does. It doesn't use pkg-config and doesn't build the vendored library? Consequently I'm not sure what to print for cargo:include here.

Testing

It would be nice to test for the presence and correctness of DEP_HARFBUZZ_INCLUDE. I used the following files (in the repository's top-level directory for convenience) both with HARFBUZZ_SYS_NO_PKG_CONFIG=1 and without:

Cargo.toml:

[project]
name = "foo"
version = "0.5.0"
authors = []
build = "build.rs"

[dependencies]
harfbuzz-sys = { path = "harfbuzz-sys" }

[build-dependencies]
cc = "1.0"

build.rs:

use std::env;
fn main() {
    let mut cfg = cc::Build::new();
    for path in env::split_paths(&env::var_os("DEP_HARFBUZZ_INCLUDE").unwrap()) {
        cfg.include(path);
    }
    cfg.file("src/test.c").warnings(false).compile("test");
}

src/test.c:

#include <hb.h>
int test() {
    hb_font_t *hb_font;
    return 0;
}

src/lib.rs: empty

I'm not sure how to add such a test without simply writing a script and running it in CI. I did a quick search on GitHub and couldn't find any other projects that actually tested cargo:include in this way.


This change is Reviewable

This prints proper values for cargo:include in the build script for both
the pkg-config and vendored cases.
@spl
Copy link
Contributor Author

spl commented Mar 28, 2019

Oh, I forgot to mention one thing.

Currently, the cargo:include for a pkg-config build includes all of the harfbuzz dependencies, the vendored cargo:include includes only the vendored include directory. It seems like one would want them to be consistent. I saw other repositories with a similar story. I'm not sure what to do about it, if anything.

@spl
Copy link
Contributor Author

spl commented Mar 28, 2019

Well, I've now discovered ctest, which looks like it might be useful for testing harfbuzz-sys. I'm looking at it now. Would that be something appreciated by the maintainers?

@jdm
Copy link
Member

jdm commented Mar 28, 2019

That seems valuable.

@spl
Copy link
Contributor Author

spl commented Mar 29, 2019

I think #140 provides a satisfactory answer to the testing issue (for me, at least). Any thoughts on the other two issues (build-native-harfbuzz and consistent cargo:include values)?

BTW, I think this PR is a self-contained improvement and useful to me. If you wanted to postpone dealing with the other issues, merge this, and make a release, I wouldn't complain. 😉

@spl
Copy link
Contributor Author

spl commented Mar 29, 2019

Okay, my ignorance is gradually dissipating. For the not(feature = "build-native-harfbuzz") case (#129), I cannot think of any value that can reasonably be given to cargo:include. Without a vendored build or pkg-config, we do not know where the header files are located. So, it should stay undefined as it is.

@jdm
Copy link
Member

jdm commented Mar 29, 2019

@bors-servo
Copy link
Contributor

bors-servo commented Mar 29, 2019

📌 Commit 692048e has been approved by jdm

@bors-servo
Copy link
Contributor

bors-servo commented Mar 29, 2019

Testing commit 692048e with merge 62a0050...

bors-servo added a commit that referenced this pull request Mar 29, 2019
Print proper cargo:include values

In order to build a Rust crate with C/C++ that uses the HarfBuzz library referenced by the `harfbuzz` crate, I need to know where the header files are. I believe the standard way of doing that is the `links` key/value environment variables. In particular, I'm expecting `DEP_HARFBUZZ_INCLUDE` to tell me the include path for `hb.h` and other header files. Unfortunately, this doesn't seem to be working correctly now. This PR is an attempt to resolve that.

Summary of changes:
* Prints proper values for `cargo:include` in the build script for both the `pkg-config` and vendored cases.
* Changes a use of `to_str().unwrap()` to `PathBuf::display()`. I believe this is better, but I'm happy to be corrected.

As far as adding a useful feature goes, I think this PR is complete. However, there are a couple of potential improvements that would be nice, but I'm not sure how to go about them.

**`not(feature = "build-native-harfbuzz")`**

I'm not exactly sure what disabling the feature `build-native-harfbuzz` does. It doesn't use `pkg-config` and doesn't build the vendored library? Consequently I'm not sure what to print for `cargo:include` here.

**Testing**

It would be nice to test for the presence and correctness of `DEP_HARFBUZZ_INCLUDE`. I used the following files (in the repository's top-level directory for convenience) both with `HARFBUZZ_SYS_NO_PKG_CONFIG=1` and without:

`Cargo.toml`:

```toml
[project]
name = "foo"
version = "0.5.0"
authors = []
build = "build.rs"

[dependencies]
harfbuzz-sys = { path = "harfbuzz-sys" }

[build-dependencies]
cc = "1.0"
```

`build.rs`:

```rust
use std::env;
fn main() {
    let mut cfg = cc::Build::new();
    for path in env::split_paths(&env::var_os("DEP_HARFBUZZ_INCLUDE").unwrap()) {
        cfg.include(path);
    }
    cfg.file("src/test.c").warnings(false).compile("test");
}
```

`src/test.c`:

```c
#include <hb.h>
int test() {
    hb_font_t *hb_font;
    return 0;
}
```

`src/lib.rs`: empty

I'm not sure how to add such a test without simply writing a script and running it in CI. I did a quick search on GitHub and couldn't find any other projects that actually tested `cargo:include` in this way.

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/rust-harfbuzz/139)
<!-- Reviewable:end -->
@bors-servo
Copy link
Contributor

bors-servo commented Mar 29, 2019

☀️ Test successful - checks-travis
Approved by: jdm
Pushing 62a0050 to master...

@bors-servo bors-servo merged commit 692048e into servo:master Mar 29, 2019
3 checks passed
3 checks passed
Travis CI - Pull Request Build Passed
Details
continuous-integration/appveyor/pr AppVeyor build succeeded
Details
homu Test successful
Details
bors-servo added a commit that referenced this pull request Mar 29, 2019
Add harfbuzz-sys-test using ctest

This adds a crate to the workspace for testing `harfbuzz-sys` automatically using `ctest`.

I changed a few types in `harfbuzz-sys/src/lib.rs` to make the testing automatic (i.e. to avoid creating special cases). However, I suppose `lib.rs` is generated? If so, this might be problematic if it is regenerated later. The changes seem quite sensible to me, so maybe there's an alternative to tweak the generation to get something similar?

`harfbuzz-sys-test` currently only works with `HARFBUZZ_SYS_NO_PKG_CONFIG=1` because it doesn't get the `DEP_HARFBUZZ_INCLUDE` environment variable otherwise. The `.travis.yml` change reflects that. If #139 or something similar is merged, this special case can be removed, and the test should be run for both `HARFBUZZ_SYS_NO_PKG_CONFIG=1` and without `HARFBUZZ_SYS_NO_PKG_CONFIG`.

There's a `FIXME` in `harfbuzz-sys-test/build.rs` because I don't understand the errors when these functions are not skipped. I would appreciate it if someone else would take a look at that.

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/rust-harfbuzz/140)
<!-- Reviewable:end -->
@spl spl deleted the spl:dep-harfbuzz-include branch Mar 30, 2019
@spl
Copy link
Contributor Author

spl commented Mar 30, 2019

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

3 participants
You can’t perform that action at this time.