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

add rule for bindgen (#102) #108

Merged
merged 30 commits into from
Feb 6, 2019
Merged

Conversation

mfarrugi
Copy link
Collaborator

@mfarrugi mfarrugi commented Jul 22, 2018

#102

This is what I got working for me, but I haven't tested it against anything else.

Main issues I can see,

  1. This is not really testable within this repo, given the implicit raze dependency
  2. the cc toolchain finagling seems rough at best, not sure it works w/ vanilla toolchain

I imagine I can setup a small raze environment based on the examples in this repo, but I would like to get comments on the cc_toolchain usage (and anything else) before continuing on this.

Copy link
Collaborator

@acmcarther acmcarther left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure why, but I'm really skeptical about this PR.

Unrelated, the variable names here are really spartan (e.g. "unformatted" "rustfmt", "bindgen" instead of "unformatted_out", "rustfmt_bin", "bindgen_bin"). I let some questionable variable names go through earlier, but during my refactoring I really regretted it every time I looked at them. Are you averse to being a bit more specific on these?

rust/bindgen.bzl Outdated Show resolved Hide resolved
rust/bindgen.bzl Outdated Show resolved Hide resolved
rust/bindgen.bzl Outdated Show resolved Hide resolved
rust/bindgen.bzl Outdated Show resolved Hide resolved
rust/bindgen.bzl Outdated Show resolved Hide resolved
rust/bindgen.bzl Outdated Show resolved Hide resolved
rust/bindgen.bzl Outdated Show resolved Hide resolved
rust/rust.bzl Outdated Show resolved Hide resolved
@mfarrugi
Copy link
Collaborator Author

I am happy to expand on the names. My thought process was something like bindgen = ctx.executable.bindgen was clear, and the rule was relatively short.

Do you think it is better to reference ctx.executable.bindgen every time or go with bindgen_bin etc?

@mfarrugi
Copy link
Collaborator Author

On the skeptical bit, I think the cc provider and toolchain usage is weird, and even the clang usage feels like it should maybe come from a clang cc_toolchain.

@mfarrugi
Copy link
Collaborator Author

I haven't come back to this yet because upstreaming this while requiring libstdcxx as a parameter is extremely suspect to me, though I haven't had any issues with the current incarnation.

@mfarrugi
Copy link
Collaborator Author

Shipping this requires either writing a clang_toolchain rule, or figuring out how to use the current cc toolchain api with a clang installation appropriately.

@damienmg can you comment on whether the latter is actually a good idea?

@damienmg
Copy link
Collaborator

I think the latter is wiser, especially since the c++ toolchain can now be written in Starlark. I'll try to talk a bit with @mhlopko a bit latter about it to see if he can help.

@mfarrugi
Copy link
Collaborator Author

mfarrugi commented Jan 22, 2019

Updated this to use a toolchain, which makes it a little cleaner. This "Works On My Machine"..

Things to do before pushing:

  • Re-address comments from before.
  • Add default toolchain + working example, similar to protobuf rules
  • Add/update docs

The example should involve a tarball of clang and a raze imported bindgen, which is hopefully straight forward.

bindgen/raze/crates.bzl Outdated Show resolved Hide resolved
rust/private/rustc.bzl Outdated Show resolved Hide resolved
@mfarrugi
Copy link
Collaborator Author

The Ubuntu 16.04 (local and rbe) CI failures are due to the builders having an older version of libstdc++ than libclang wants...

thread 'main' panicked at 'Unable to find libclang: "the `libclang` shared library at bazel-out/host/bin/_solib_k8/_U@clang_S_S_Clibclang.so___Uexternal_Sclang_Slib/libclang.so could not be opened: bazel-out/host/bin/_solib_k8/_U@local_Ulinux_S_S_Clibstdc++___Uexternal_Slocal_Ulinux/libstdc++.so.6: version `GLIBCXX_3.4.22\' not found (required by bazel-out/host/bin/_solib_k8/_U@clang_S_S_Clibclang.so___Uexternal_Sclang_Slib/libclang.so)"', libcore/result.rs:1009:5

Ubuntu 14.04 failure is probably caused by using the wrong clang tarball

Inconsistency detected by ld.so: dl-open.c: 689: _dl_open: Assertion `_dl_debug_initialize (0, args.nsid)->r_state == RT_CONSISTENT' failed!

Will look to fix for 16.04...
 

@mfarrugi
Copy link
Collaborator Author

Alright this looks like it works, ptal @damienmg @acmcarther

There is something to be said about how much boilerplate is required to support a ~150 line rule.

Copy link
Collaborator

@damienmg damienmg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First pass, I spotted a few bugs but API-wise it looks good to me.

It is still missing documentation though. Especially how to plug other dependencies (see the protobuf change).

macos:
osx_targets: &osx_targets
- "--" # Allows negative patterns; hack for https://github.com/bazelbuild/continuous-integration/pull/245
- "--"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why remove the comment?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved the comment up to the first occurence

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we duplicate the comment rather? or later the first one might goes away but not this one.

bindgen/bindgen.bzl Outdated Show resolved Hide resolved
bindgen/bindgen.bzl Outdated Show resolved Hide resolved
bindgen/bindgen.bzl Outdated Show resolved Hide resolved
tools = [clang_bin],
)

if rustfmt_bin:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is running rustfmt needed on generated sources? I meant, they are not made for being read anyway.

Copy link
Collaborator

@acmcarther acmcarther Jan 28, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've personally found it useful (in my own hacky version of this) when inspecting generated files. In my experience, bindgen can be a bit finnicky and require special params to configure generation. Its much easier to diagnose issues in a formatted file compared to a super long single line unformatted file.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Beyond being useful, this is what bindgen does by default. I think it is best to keep parity with other tools whenever possible.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, can you just add a comment about it?

bindgen/bindgen.bzl Show resolved Hide resolved
executable = True,
cfg = "host",
),
"libclang": attr.label(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am surprised you did not got hit by bazelbuild/bazel#6889. This will get shipped in the host configuration :(

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if @mfarrugi fully follows the issue, but I'm a bit confused.

Isn't host configuration the configuration we want to run bindgen in?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want to run this at build-time on the host, so I don't follow what "shipped" means. (I also don't understand the issue :))

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we want to run this in host configuration please add cfg = "host". The default value of cfg is "target" but due to bazelbuild/bazel#6889 it somehow becomes the host configuration. This is a bug in Bazel.

bindgen/repositories.bzl Outdated Show resolved Hide resolved
rust/private/rustc.bzl Outdated Show resolved Hide resolved
Copy link
Collaborator

@acmcarther acmcarther left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for following up on this (even after so long)! I know the code generation experience with cargo is super clunky and this is a good place for Bazel to stand out.

I second @damienmg's request for a bit more documentation but otherwise I'm happy with the implementation.

WORKSPACE Show resolved Hide resolved
bindgen/bindgen.bzl Outdated Show resolved Hide resolved
bindgen/repositories.bzl Outdated Show resolved Hide resolved
executable = True,
cfg = "host",
),
"libclang": attr.label(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if @mfarrugi fully follows the issue, but I'm a bit confused.

Isn't host configuration the configuration we want to run bindgen in?

bindgen/raze/crates.bzl Outdated Show resolved Hide resolved
@mfarrugi
Copy link
Collaborator Author

I'll look to finish this up soon.

Any ideas on skipping the local_linux setup on osx? Looks like it requires a repository rule.. but maybe one of you knows of something lighter?

@mfarrugi
Copy link
Collaborator Author

mfarrugi commented Feb 3, 2019

Now I just need to skip loading local_linux on osx...

@mfarrugi
Copy link
Collaborator Author

mfarrugi commented Feb 3, 2019

This probably is not far from working on osx, but I don't have a mac to test with and I don't want bounce diffs off of CI.

ptal!

Copy link
Collaborator

@damienmg damienmg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only some nits now. Since buildkite is green, I say it is a go for me :)

macos:
osx_targets: &osx_targets
- "--" # Allows negative patterns; hack for https://github.com/bazelbuild/continuous-integration/pull/245
- "--"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we duplicate the comment rather? or later the first one might goes away but not this one.

bindgen/README.md Outdated Show resolved Hide resolved
bindgen/README.md Outdated Show resolved Hide resolved
bindgen/README.md Outdated Show resolved Hide resolved
tools = [clang_bin],
)

if rustfmt_bin:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, can you just add a comment about it?

bindgen/bindgen.bzl Show resolved Hide resolved
executable = True,
cfg = "host",
),
"libclang": attr.label(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we want to run this in host configuration please add cfg = "host". The default value of cfg is "target" but due to bazelbuild/bazel#6889 it somehow becomes the host configuration. This is a bug in Bazel.

@mfarrugi
Copy link
Collaborator Author

mfarrugi commented Feb 5, 2019

Looks like there are some unrelated RBE failures..

ERROR: While resolving toolchains for target @examples//fibonacci:fibonacci_test: invalid registered toolchain '@bazel_toolchains//configs/ubuntu16_04_clang/1.1/bazel_0.22.0/cpp:cc-toolchain-clang-x86_64-default': no such package '@bazel_toolchains//configs/ubuntu16_04_clang/1.1/bazel_0.22.0/cpp': BUILD file not found on package path

other than that I think this looks good.

@acmcarther any other comments?

bindgen/README.md Outdated Show resolved Hide resolved
@mfarrugi
Copy link
Collaborator Author

mfarrugi commented Feb 5, 2019

@nlopezgi where's the right place to complain about rbe ci issues? (https://buildkite.com/bazel/rules-rust-rustlang?branch=master failing after a trivial change convinces me this is not our fault)

@mfarrugi mfarrugi merged commit 1ced2c2 into bazelbuild:master Feb 6, 2019
@mfarrugi mfarrugi deleted the marco-bindgen branch February 6, 2019 01:59
@nlopezgi
Copy link
Contributor

Hi @mfarrugi
Sorry for the super late reply on this.
The error you were getting related to @bazel_toolchains//configs/ubuntu16_04_clang/1.1/bazel_0.22.0/cpp:cc-toolchain-clang-x86_64-default not being found is likely because the WORKSPACE file pointer to bazel-toolchains needs to be updated regularly (https://releases.bazel.build/bazel-toolchains.html). This is the case because with each Bazel version there are changes to the toolchain configurations that need to be used for RBE builds. Apologies for the issue. We are working towards a better mechanism (https://github.com/bazelbuild/bazel-toolchains/blob/master/rules/rbe_repo.bzl) that will make it easier to keep things working with the fast pace of Bazel releases.

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

Successfully merging this pull request may close these issues.

5 participants