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 support for depending on shared libraries on linux. #61

Merged
merged 19 commits into from
May 23, 2018

Conversation

mfarrugi
Copy link
Collaborator

This change allows rust_library and rust_binary to depend on native shared libraries.
This requires 2 additional steps, collecting shared libraries from cc_library and rust_library, and setting the rpath in executables.

I added an example of this, and tested more extensively on another project.

I am not sure how much work is left to support mac+windows or rust dylibs.

@bazel-io
Copy link
Member

Can one of the admins verify this patch?

@mfarrugi
Copy link
Collaborator Author

@damienmg can someone take a look at this? I'm not that confident this is how it should be done.

@damienmg
Copy link
Collaborator

damienmg commented Nov 15, 2017 via email

@mfarrugi
Copy link
Collaborator Author

@davidzchen?

Would love any expert bazel eyes on this.

rust/rust.bzl Outdated
@@ -49,11 +49,12 @@ rust_repositories()
[Cargo](https://crates.io/).
"""

load(":toolchain.bzl", "build_rustc_command", "build_rustdoc_command", "build_rustdoc_test_command")
load(":toolchain.bzl", "build_rustc_command", "build_rustdoc_command", "build_rustdoc_test_command", "relative_path")
Copy link
Member

Choose a reason for hiding this comment

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

Keep line length to 80 chars.

@@ -101,6 +106,19 @@ def build_rustdoc_test_command(ctx, toolchain, depinfo, lib_rs):
depinfo.search_flags +
depinfo.link_flags)

# @TODO(review) How should this be done? cc_* logic seems much more involved.
Copy link
Member

Choose a reason for hiding this comment

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

@dslomov Can you help with this portion of the review? Thanks.

Copy link
Member

Choose a reason for hiding this comment

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

@mhlopko Can you help take a look at this part of the review?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

In particular, I'm concerned about handling the placement of .so's emitted by setting crate_type=dylib (which is supported by another PR).

For depending on cc_* rule dylibs I don't think there is any concern, but the cc_* logic is suspiciously complex.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Did your concerns about this get resolved? Either way, we probably don't want to submit this with this TODO and below comment intact.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

No, but I haven't had issues and neither has anyone else, so that's good enough for now.

path_parts = path.split("/")
return [part for part in path_parts if part != "."]

def relative_path(src_path, dest_path):
Copy link
Member

Choose a reason for hiding this comment

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

If this function is internal to this bzl file, prefix with _

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's also used in rust/rust.bzl

Copy link
Member

Choose a reason for hiding this comment

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

Ah you're right.

@bazel-io
Copy link
Member

Can one of the admins verify this patch?

@acmcarther
Copy link
Collaborator

Added @dslomov and @mhlopko to get eyes on the toolchain.bzl changes

@mfarrugi
Copy link
Collaborator Author

How do I get CI to run on this? Maybe would have caught acmcarther/cargo-raze#31

@acmcarther
Copy link
Collaborator

ok to test

# Construct features flags
features_flags = _get_features_flags(ctx.attr.crate_features)

return " ".join(
["set -e;"] +
# If TMPDIR is set but not created, rustc will die.
["if [[ -v TMPDIR ]]; then mkdir -p $TMPDIR; fi;"] +
Copy link
Collaborator

Choose a reason for hiding this comment

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

"-v" is apparently only available since bash 4.2, and "[[ ]]" is apparently not portable.

Try "if [ ! -z "${TMPDIR+x}" ];" ?

@mfarrugi
Copy link
Collaborator Author

CI requires this to work for osx (but not windows?), which requires a very different approach to setting rpath. I would appreciate some bazel-er input before reinventing how cpp linking is done in skylark.

cc acmcarther/cargo-raze#27 I stand surprised :)

@mfarrugi
Copy link
Collaborator Author

mfarrugi commented Mar 3, 2018

Going to need to skip the osx tests for this, not sure how to do that yet.

@acmcarther
Copy link
Collaborator

I've been building from under a rock apparently. This change is what makes depending on system libraries work! I've been silently using this in my side project, and when I went to use bazelbuild/rules_rust:master, everything broke e.g:

error: could not find native static library `clang`, perhaps an -L flag is missing?

mfarrugi@: can you remind me what the next steps are on this PR? From what I can tell:

  1. Fix up merge conflicts
  2. Another pass of code review
  3. Ideally, a solution that makes builds work on Windows

I don't have the best background for (2) and (3), but I can read up enough for a passable code review and maybe help come up with something functional enough for now.

@acmcarther
Copy link
Collaborator

I'm mostly caught up at this point on the way this works. The dep tracking strikes me as sloppy (in particular, the way we're tracking [libs, transitive_libs, transitive_dylibs, and transitive_staticlibs]), but I suspect that might just be my unfamiliarity with the problem space.

I've got a couple of comments mostly around error handling and being a little more explicit about assumptions, and I'm curious what the resolution to the cc_* confusion was, but overall it looks good.

path_parts = path.split("/")
return [part for part in path_parts if part != "."]

def relative_path(src_path, dest_path):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: Does it make sense to visually separate this public function from the private functions by moving it to the top of the 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.

I think there are enough of these utility methods to pull out a utils.bzl, should I hold off on that?

return []

if toolchain.os != 'linux':
print("Runtime linking is not supported on {}, but found {}".format(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Perhaps this should fail instead of print.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This builds correctly, but run will fall. My thinking was that this technically works, and is easier to iterate on for users and contributors.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

CI says I'm wrong, so I'll fail here and ignore it in ci builds. (https://source.cloud.google.com/results/invocations/46ee1fc3-222d-491f-bbb7-24d208119f66/log)

rust/rust.bzl Outdated
def _get_lib_name(lib):
""" Returns the name of a library artifact, eg. libabc.a -> abc"""
libname, ext = lib.basename.split(".", 2)
return libname[3:]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is it worth checking to make sure that lib.startswith("lib")?

@@ -101,6 +106,19 @@ def build_rustdoc_test_command(ctx, toolchain, depinfo, lib_rs):
depinfo.search_flags +
depinfo.link_flags)

# @TODO(review) How should this be done? cc_* logic seems much more involved.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Did your concerns about this get resolved? Either way, we probably don't want to submit this with this TODO and below comment intact.

@bobsomers
Copy link
Contributor

The dep tracking strikes me as sloppy (in particular, the way we're tracking [libs, transitive_libs, transitive_dylibs, and transitive_staticlibs]), but I suspect that might just be my unfamiliarity with the problem space.

It is kinda sloppy, but a proper fix for #78 will likely end up rewriting a lot of that to clean it up (at least from some preliminary hacking I have in a local branch).

IMHO it probably makes sense to keep it this way for now, since it's a logical extension of how it already works, and then clean it all up in one go when a fix for #78 is tackled.

(Sorry for the drive-by review comments, when I get some free time I'm planning to take a whack at solving #78 and it conflicts enough with this that one of them needs to go first.)

@mfarrugi
Copy link
Collaborator Author

@acmcarther this is up to date again

@acmcarther
Copy link
Collaborator

rule @examples//hello_lib:hello_lib doesn't provide attribute transitive_libs. Its list of attributes is: ["actions", "crate_root", "crate_type", "data_runfiles", "default_runfiles", "files", "files_to_run", "label", "output_group", "output_groups", "rust_deps", "rust_lib", "rust_srcs", "transitive_dylibs", "transitive_rlibs", "transitive_staticlibs"]

@mfarrugi
Copy link
Collaborator Author

@acmcarther sorry about that, was running against @examples and not ....

Can I get added to the auto-ci whitelist to streamline changes a little bit more?

@mfarrugi
Copy link
Collaborator Author

@acmcarther do we want anything else here? If so, lmk and we can let #84 go ahead next while I work that out (cc @bobsomers ).

@acmcarther
Copy link
Collaborator

Regd #61 (comment)

I don't actually know how to add someone to the whitelist and https://github.com/bazelbuild/continuous-integration/blob/master/buildkite/README.md#pull-requests wasn't immediately enlightening.

@buchgr: What's the org-wide attitude toward adding people to the CI whitelist? Is there a suggested approach to doing that, or perhaps is "ci whitelist" considered roughly equivalent to maintainer status anyway?


Regd #61 (comment)

This looks good to me, but there is a merge conflict. Can you fix that and tag me again and I'll ship.

@buchgr
Copy link
Contributor

buchgr commented May 23, 2018

@acmcarther you can add someone to the whitelist by adding him as a collaborator of rules_rust on Github.

@buchgr
Copy link
Contributor

buchgr commented May 23, 2018

I have added @mfarrugi as a collaborator (Settings -> Teams & Collaborators). Need to accept the invite and then CI should just work.

@googlebot
Copy link

So there's good news and bad news.

👍 The good news is that everyone that needs to sign a CLA (the pull request submitter and all commit authors) have done so. Everything is all good there.

😕 The bad news is that it appears that one or more commits were authored or co-authored by someone other than the pull request submitter. We need to confirm that all authors are ok with their commits being contributed to this project. Please have them confirm that here in the pull request.

Note to project maintainer: This is a terminal state, meaning the cla/google commit status will not change from this state. It's up to you to confirm consent of the commit author(s) and merge this pull request when appropriate.

@mfarrugi
Copy link
Collaborator Author

@acmcarther merged, not sure what exactly bot is unhappy with though.

@googlebot
Copy link

A Googler has manually verified that the CLAs look good.

(Googler, please make sure the reason for overriding the CLA status is clearly documented in these comments.)

@googlebot googlebot removed the cla: no label May 23, 2018
@acmcarther
Copy link
Collaborator

#61 (comment)
Merge included 634a6f3 which is already merged to master. CLAs all signed.
@mfarrugi: I think the workflow expects you to rebase, not merge master, so that the upstream commits aren't directly included here. No problem though.


@buchgr: Thank you for the quick attention! I'll remember the collaborator process and use it in the future.

@acmcarther acmcarther merged commit f4b5743 into bazelbuild:master May 23, 2018
@mfarrugi mfarrugi deleted the pr-native-dylib branch October 19, 2020 03:03
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.

10 participants