-
Notifications
You must be signed in to change notification settings - Fork 402
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
Support crate_type file extensions more fully #38
Changes from 6 commits
d102ae5
86cab07
291cc15
e6055be
a81e76a
35735d7
e9a6f6d
f684399
ef8879f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,13 +55,6 @@ RUST_FILETYPE = FileType([".rs"]) | |
|
||
A_FILETYPE = FileType([".a"]) | ||
|
||
LIBRARY_CRATE_TYPES = [ | ||
"lib", | ||
"rlib", | ||
"dylib", | ||
"staticlib", | ||
] | ||
|
||
# Used by rust_doc | ||
HTML_MD_FILETYPE = FileType([ | ||
".html", | ||
|
@@ -205,6 +198,27 @@ def _find_crate_root_src(srcs, file_names=["lib.rs"]): | |
return src | ||
fail("No %s source file found." % " or ".join(file_names), "srcs") | ||
|
||
def _determine_lib_name(name, crate_type, toolchain): | ||
extension = None | ||
if crate_type in ("dylib", "cdylib"): | ||
extension = toolchain.dylib_ext | ||
elif crate_type == "staticlib": | ||
extension = toolchain.staticlib_ext | ||
elif crate_type in ("rlib", "lib", "proc-macro"): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. proc-macro should be a dylib (though I don't know if rustc actually cares) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah this is important. We're relying on rustc to "produce the right thing" without us telling it what to make. Indeed, it is producing a dylib in this case, so we need to expect a dylib. Not sure how I became under the impression that this was an rlib -- my local code pre-osx fix used dylib here. Fixed. |
||
# All platforms produce 'rlib' here | ||
extension = ".rlib" | ||
elif crate_type == "bin": | ||
fail("crate_type of 'bin' was detected in a rust_library. Please compile " | ||
+ "this crate as a rust_binary instead.") | ||
|
||
if not extension: | ||
fail(("Unknown crate_type: %s. If this is a cargo-supported crate type, " | ||
+ "please file an issue!") % crate_type) | ||
|
||
|
||
return "lib{name}{extension}".format(name=name, | ||
extension=extension) | ||
|
||
def _crate_root_src(ctx, file_names=["lib.rs"]): | ||
if ctx.file.crate_root == None: | ||
return _find_crate_root_src(ctx.files.srcs, file_names) | ||
|
@@ -219,18 +233,14 @@ def _rust_library_impl(ctx): | |
# Find lib.rs | ||
lib_rs = _crate_root_src(ctx) | ||
|
||
# Validate crate_type | ||
crate_type = "" | ||
if ctx.attr.crate_type != "": | ||
if ctx.attr.crate_type not in LIBRARY_CRATE_TYPES: | ||
fail("Invalid crate_type for rust_library. Allowed crate types are: %s" | ||
% " ".join(LIBRARY_CRATE_TYPES), "crate_type") | ||
crate_type += ctx.attr.crate_type | ||
else: | ||
crate_type += "lib" | ||
# Find toolchain | ||
toolchain = _find_toolchain(ctx) | ||
|
||
# Output library | ||
rust_lib = ctx.outputs.rust_lib | ||
rust_lib_name = _determine_lib_name(ctx.attr.name, | ||
ctx.attr.crate_type, | ||
toolchain); | ||
rust_lib = ctx.actions.declare_file(rust_lib_name) | ||
output_dir = rust_lib.dirname | ||
|
||
# Dependencies | ||
|
@@ -240,12 +250,11 @@ def _rust_library_impl(ctx): | |
allow_cc_deps=True) | ||
|
||
# Build rustc command | ||
toolchain = _find_toolchain(ctx) | ||
cmd = build_rustc_command( | ||
ctx = ctx, | ||
toolchain = toolchain, | ||
crate_name = ctx.label.name, | ||
crate_type = crate_type, | ||
crate_type = ctx.attr.crate_type, | ||
src = lib_rs, | ||
output_dir = output_dir, | ||
depinfo = depinfo) | ||
|
@@ -272,7 +281,7 @@ def _rust_library_impl(ctx): | |
|
||
return struct( | ||
files = depset([rust_lib]), | ||
crate_type = crate_type, | ||
crate_type = ctx.attr.crate_type, | ||
crate_root = lib_rs, | ||
rust_srcs = ctx.files.srcs, | ||
rust_deps = ctx.attr.deps, | ||
|
@@ -535,17 +544,14 @@ _rust_common_attrs = { | |
} | ||
|
||
_rust_library_attrs = { | ||
"crate_type": attr.string(), | ||
"crate_type": attr.string(default = "rlib"), | ||
} | ||
|
||
rust_library = rule( | ||
_rust_library_impl, | ||
attrs = dict(_rust_common_attrs.items() + | ||
_rust_library_attrs.items()), | ||
host_fragments = ["cpp"], | ||
outputs = { | ||
"rust_lib": "lib%{name}.rlib", | ||
}, | ||
toolchains = ["@io_bazel_rules_rust//rust:toolchain"], | ||
) | ||
|
||
|
@@ -564,6 +570,10 @@ Args: | |
|
||
If `crate_root` is not set, then this rule will look for a `lib.rs` file or | ||
the single file in `srcs` if `srcs` contains only one file. | ||
crate_type: The type of linkage to use for building this library. Options | ||
include "lib", "rlib", "dylib", "cdylib", "staticlib", and "proc-macro" | ||
|
||
The exact output file will depend on the toolchain used. | ||
deps: List of other libraries to be linked to this library target. | ||
|
||
These can be either other `rust_library` targets or `cc_library` targets if | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'proc-macro' isn't tested in this PR as the required crate dependencies are impractical to import without
cargo-raze
.For the record, the required crates are
EDIT: oops, github doesn't support comments to selected text.