Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions rust/private/providers.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ CrateInfo = provider(
"rustc_rmeta_output": "File: The rmeta file produced for this crate. It is optional.",
"srcs": "depset[File]: All source Files that are part of the crate.",
"std_dylib": "File: libstd.so file",
"crate_features": "[str]: A list of feature enabled for this crate.",
"type": (
"str: The type of this crate " +
"(see [rustc --crate-type](https://doc.rust-lang.org/rustc/command-line-arguments.html#--crate-type-a-list-of-types-of-crates-for-the-compiler-to-emit))."
Expand Down
3 changes: 3 additions & 0 deletions rust/private/rust.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ def _rust_library_common(ctx, crate_type):
compile_data = depset(compile_data),
compile_data_targets = depset(ctx.attr.compile_data),
owner = ctx.label,
crate_features = ctx.attr.crate_features,
),
)

Expand Down Expand Up @@ -258,6 +259,7 @@ def _rust_binary_impl(ctx):
compile_data = depset(compile_data),
compile_data_targets = depset(ctx.attr.compile_data),
owner = ctx.label,
crate_features = ctx.attr.crate_features,
),
)

Expand Down Expand Up @@ -422,6 +424,7 @@ def _rust_test_impl(ctx):
compile_data = depset(compile_data),
compile_data_targets = depset(ctx.attr.compile_data),
owner = ctx.label,
crate_features = [],
)

providers = rustc_compile_action(
Expand Down
7 changes: 7 additions & 0 deletions rust/private/rustdoc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,9 @@ def _rust_doc_impl(ctx):

rustdoc_flags.extend(ctx.attr.rustdoc_flags)

if ctx.attr.include_features:
rustdoc_flags.extend(["--cfg=feature=\"{}\"".format(feature) for feature in crate_info.crate_features])

action = rustdoc_compile_action(
ctx = ctx,
toolchain = find_toolchain(ctx),
Expand Down Expand Up @@ -350,6 +353,10 @@ rust_doc = rule(
file of arguments to rustc: `@$(location //package:target)`.
"""),
),
"include_features": attr.bool(
doc = "Include the features defined by `crate_features` when building the doc tests.",
default = True,
),
"_dir_zipper": attr.label(
doc = "A tool that orchestrates the creation of zip archives for rustdoc outputs.",
default = Label("//rust/private/rustdoc/dir_zipper"),
Expand Down
8 changes: 8 additions & 0 deletions rust/private/rustdoc_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ def _rust_doc_test_impl(ctx):
compile_data = crate.compile_data,
compile_data_targets = crate.compile_data_targets,
wrapped_crate_type = crate.type,
crate_features = crate.crate_features,
owner = ctx.label,
)

Expand All @@ -150,6 +151,9 @@ def _rust_doc_test_impl(ctx):
"--test",
]

if ctx.attr.include_features:
rustdoc_flags.extend(["--cfg=feature=\"{}\"".format(feature) for feature in crate_info.crate_features])

action = rustdoc_compile_action(
ctx = ctx,
toolchain = toolchain,
Expand Down Expand Up @@ -228,6 +232,10 @@ rust_doc_test = rule(
default = Label("//rust/private/rustdoc:rustdoc_test_writer"),
executable = True,
),
"include_features": attr.bool(
doc = "Include the features defined by `crate_features` when building the doc tests.",
default = True,
),
},
test = True,
fragments = ["cpp"],
Expand Down
29 changes: 29 additions & 0 deletions test/unit/rustdoc/rustdoc_features.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2022 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#![cfg_attr(not(no_feature_test), deny(rustdoc::broken_intra_doc_links))]
#![cfg_attr(no_feature_test, allow(rustdoc::broken_intra_doc_links))]

//!
//! Checkout [inc]
//!

#[cfg(all(no_feature_test, feature = "docs"))]
compiler_error!("cannot have both no_feature_test and feature=\"docs\" enabled");

/// Increments the input.
#[cfg(feature = "docs")]
pub fn inc(n: u32) -> u32 {
n + 1
}
66 changes: 66 additions & 0 deletions test/unit/rustdoc/rustdoc_unit_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,30 @@ def _rustdoc_for_bin_with_transitive_proc_macro_test_impl(ctx):

return analysistest.end(env)

def _rustdoc_for_lib_with_features_test_impl(ctx):
env = analysistest.begin(ctx)
tut = analysistest.target_under_test(env)

_common_rustdoc_checks(env, tut)

action = _get_rustdoc_action(env, tut)

assert_argv_contains(env, action, "--cfg=feature=\"docs\"")

return analysistest.end(env)

def _rustdoc_for_lib_without_features_test_impl(ctx):
env = analysistest.begin(ctx)
tut = analysistest.target_under_test(env)

_common_rustdoc_checks(env, tut)

action = _get_rustdoc_action(env, tut)

assert_argv_contains_prefix_not(env, action, "--cfg=feature")

return analysistest.end(env)

def _rustdoc_for_lib_with_cc_lib_test_impl(ctx):
env = analysistest.begin(ctx)
tut = analysistest.target_under_test(env)
Expand Down Expand Up @@ -164,6 +188,8 @@ rustdoc_for_lib_with_proc_macro_in_docs_test = analysistest.make(_rustdoc_for_li
rustdoc_for_bin_with_transitive_proc_macro_test = analysistest.make(_rustdoc_for_bin_with_transitive_proc_macro_test_impl)
rustdoc_for_lib_with_cc_lib_test = analysistest.make(_rustdoc_for_lib_with_cc_lib_test_impl)
rustdoc_with_args_test = analysistest.make(_rustdoc_with_args_test_impl)
rustdoc_for_lib_with_features_test = analysistest.make(_rustdoc_for_lib_with_features_test_impl)
rustdoc_for_lib_without_features_test = analysistest.make(_rustdoc_for_lib_without_features_test_impl)
rustdoc_zip_output_test = analysistest.make(_rustdoc_zip_output_test_impl)
rustdoc_with_json_error_format_test = analysistest.make(_rustdoc_with_json_error_format_test_impl, config_settings = {
str(Label("//rust/settings:error_format")): "json",
Expand Down Expand Up @@ -283,6 +309,34 @@ def _define_targets():
crate_features = ["with_proc_macro"],
)

_target_maker(
rust_library,
name = "lib_with_features",
srcs = ["rustdoc_features.rs"],
crate_features = ["docs"],
)

rust_doc(
name = "rustdoc_lib_with_features",
crate = ":lib_with_features",
)

_target_maker(
rust_library,
name = "lib_without_features",
srcs = ["rustdoc_features.rs"],
crate_features = ["docs"],
)

rust_doc(
name = "rustdoc_lib_without_features",
crate = ":lib_with_features",
include_features = False,
rustdoc_flags = [
"--cfg=no_feature_test"
]
)

cc_library(
name = "cc_lib",
hdrs = ["rustdoc.h"],
Expand Down Expand Up @@ -424,6 +478,16 @@ def rustdoc_test_suite(name):
target_under_test = ":lib_with_cc_doc",
)

rustdoc_for_lib_with_features_test(
name = "rustdoc_for_lib_with_features_test",
target_under_test = ":rustdoc_lib_with_features",
)

rustdoc_for_lib_without_features_test(
name = "rustdoc_for_lib_without_features_test",
target_under_test = ":rustdoc_lib_without_features",
)

rustdoc_with_args_test(
name = "rustdoc_with_args_test",
target_under_test = ":rustdoc_with_args",
Expand Down Expand Up @@ -455,6 +519,8 @@ def rustdoc_test_suite(name):
":rustdoc_for_lib_with_proc_macro_in_docs_test",
":rustdoc_for_lib_with_proc_macro_test",
":rustdoc_for_lib_with_cc_lib_test",
":rustdoc_for_lib_with_features_test",
":rustdoc_for_lib_without_features_test",
":rustdoc_with_args_test",
":rustdoc_with_json_error_format_test",
":rustdoc_zip_output_test",
Expand Down
Loading