Skip to content

Commit

Permalink
Fix support for crate_type w.r.t. output file extension
Browse files Browse the repository at this point in the history
  • Loading branch information
acmcarther committed Mar 31, 2017
1 parent 0f4e196 commit ebad356
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
9 changes: 9 additions & 0 deletions examples/hello_lib/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ rust_library(
],
)

rust_library(
name = "hello_dylib",
srcs = [
"src/greeter.rs",
"src/lib.rs",
],
crate_type = "dylib",
)

rust_test(
name = "hello_lib_test",
deps = [":hello_lib"],
Expand Down
48 changes: 44 additions & 4 deletions rust/rust.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,27 @@ LIBRARY_CRATE_TYPES = [
"staticlib",
]

LIBRARY_AND_PLATFORM_TO_EXTENSION = {
("dylib", "linux"): ".so",
("dylib", "darwin"): ".dylib",
("dylib", "windows"): ".dll",
("cdylib", "linux"): ".so",
("cdylib", "darwin"): ".dylib",
("cdylib", "windows"): ".dll",
("staticlib", "linux"): ".a",
("staticlib", "darwin"): ".a",
("staticlib", "windows"): ".lib",
("bin", "linux"): "",
("bin", "darwin"): "",
("bin", "windows"): ".exe",
("lib", "linux"): ".rlib",
("lib", "darwin"): ".rlib",
("lib", "windows"): ".rlib",
("rlib", "linux"): ".rlib",
("rlib", "darwin"): ".rlib",
("rlib", "windows"): ".rlib",
}

# Used by rust_doc
HTML_MD_FILETYPE = FileType([
".html",
Expand Down Expand Up @@ -203,6 +224,20 @@ def _get_features_flags(features):
features_flags += ["--cfg feature=\\\"%s\\\"" % feature]
return features_flags

def _get_crate_type_and_target_outputs(name, crate_type, platform):
crate_type = crate_type or "rlib"
platform = platform or "linux"
extension = LIBRARY_AND_PLATFORM_TO_EXTENSION[crate_type, platform]

if crate_type == "bin":
return {
"rust_lib": "%{{name}}{}".format(extension)
}
else:
return {
"rust_lib": "lib%{{name}}{}".format(extension)
}

def _get_dirname(short_path):
return short_path[0:short_path.rfind('/')]

Expand Down Expand Up @@ -659,12 +694,13 @@ _rust_toolchain_attrs = {
single_file = True,
),
"_crosstool": attr.label(
default = Label("//tools/defaults:crosstool")
default = Label("//tools/defaults:crosstool"),
),
}

_rust_library_attrs = {
"crate_type": attr.string(),
"platform": attr.string(),
}

rust_library = rule(
Expand All @@ -673,10 +709,9 @@ rust_library = rule(
_rust_library_attrs.items() +
_rust_toolchain_attrs.items()),
fragments = ["cpp"],
outputs = {
"rust_lib": "lib%{name}.rlib",
},
outputs = _get_crate_type_and_target_outputs,
)

"""Builds a Rust library crate.
Args:
Expand Down Expand Up @@ -781,6 +816,7 @@ rust_binary = rule(
executable = True,
fragments = ["cpp"],
)

"""Builds a Rust binary crate.
Args:
Expand Down Expand Up @@ -905,6 +941,7 @@ rust_test = rule(
fragments = ["cpp"],
test = True,
)

"""Builds a Rust test crate.
Args:
Expand Down Expand Up @@ -1067,6 +1104,7 @@ rust_bench_test = rule(
fragments = ["cpp"],
test = True,
)

"""Builds a Rust benchmark test.
**Warning**: This rule is currently experimental. [Rust Benchmark
Expand Down Expand Up @@ -1200,6 +1238,7 @@ rust_doc = rule(
"rust_doc_zip": "%{name}-docs.zip",
},
)

"""Generates code documentation.
Args:
Expand Down Expand Up @@ -1258,6 +1297,7 @@ rust_doc_test = rule(
executable = True,
test = True,
)

"""Runs Rust documentation tests.
Args:
Expand Down

0 comments on commit ebad356

Please sign in to comment.