forked from bazelbuild/rules_rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add extra_exec_rustc_flags build config (bazelbuild#1113)
- Loading branch information
1 parent
9c9358d
commit 0946fe9
Showing
10 changed files
with
157 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
load("@bazel_skylib//rules:build_test.bzl", "build_test") | ||
load("@rules_rust//rust:defs.bzl", "rust_library") | ||
load("defs.bzl", "with_exec_cfg", "with_extra_exec_rustc_flags_cfg") | ||
|
||
package(default_visibility = ["//test:__subpackages__"]) | ||
|
||
# Checks that extra_exec_rustc_flags are passed in exec configuration. | ||
# lib.rs is a sample source file that requires a `--cfg=bazel_exec` flag to build. | ||
# These targets set up transitions so that building :lib triggers building | ||
# lib.rs in exec configuration with //:extra_exec_rustc_flags=[--cfg=bazel_exec]. | ||
# The intermediate targets are tagged "manual" as they are not meant to be built | ||
# on their own. | ||
|
||
rust_library( | ||
name = "lib_do_not_build_directly", | ||
srcs = ["lib.rs"], | ||
tags = ["manual"], | ||
) | ||
|
||
with_extra_exec_rustc_flags_cfg( | ||
name = "lib_with_exec_flags_do_not_build_directly", | ||
srcs = ["lib_do_not_build_directly"], | ||
extra_exec_rustc_flags = ["--cfg=bazel_exec"], | ||
tags = ["manual"], | ||
) | ||
|
||
with_exec_cfg( | ||
name = "lib", | ||
srcs = ["lib_with_exec_flags_do_not_build_directly"], | ||
) | ||
|
||
# Checks that extra_exec_rustc_flags are not passed in non-exec configurations. | ||
# lib_no_exec.rs is a sample source file that fails to build if | ||
# `--cfg=bazel_exec` is present. The targets below are built in non-exec configurations, | ||
# so they should build just fine with //:extra_exec_rustc_flags=[--cfg=bazel_exec]. | ||
rust_library( | ||
name = "lib_no_exec", | ||
srcs = ["lib_no_exec.rs"], | ||
) | ||
|
||
with_extra_exec_rustc_flags_cfg( | ||
name = "lib_no_exec_with_extra_build_flags", | ||
srcs = ["lib_no_exec"], | ||
extra_exec_rustc_flags = ["--cfg=bazel_exec"], | ||
) | ||
|
||
build_test( | ||
name = "lib_build", | ||
targets = [ | ||
":lib", | ||
":lib_no_exec", | ||
":lib_no_exec_with_extra_build_flags", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
"""Test transitions to test extra_exec_rustc_flags.""" | ||
|
||
def _extra_exec_rustc_flags_transition_impl(settings, attr): | ||
return { | ||
"//:extra_exec_rustc_flags": attr.extra_exec_rustc_flags, | ||
} | ||
|
||
_extra_exec_rustc_flags_transition = transition( | ||
implementation = _extra_exec_rustc_flags_transition_impl, | ||
inputs = [], | ||
outputs = ["//:extra_exec_rustc_flags"], | ||
) | ||
|
||
def _with_extra_exec_rustc_flags_cfg_impl(ctx): | ||
return [DefaultInfo(files = depset(ctx.files.srcs))] | ||
|
||
with_extra_exec_rustc_flags_cfg = rule( | ||
implementation = _with_extra_exec_rustc_flags_cfg_impl, | ||
attrs = { | ||
"extra_exec_rustc_flags": attr.string_list( | ||
mandatory = True, | ||
), | ||
"srcs": attr.label_list( | ||
allow_files = True, | ||
cfg = _extra_exec_rustc_flags_transition, | ||
), | ||
"_allowlist_function_transition": attr.label( | ||
default = Label("//tools/allowlists/function_transition_allowlist"), | ||
), | ||
}, | ||
) | ||
|
||
def _with_exec_cfg_impl(ctx): | ||
return [DefaultInfo(files = depset(ctx.files.srcs))] | ||
|
||
with_exec_cfg = rule( | ||
implementation = _with_exec_cfg_impl, | ||
attrs = { | ||
"srcs": attr.label_list( | ||
allow_files = True, | ||
cfg = "exec", | ||
), | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// Sample source that fails to compile unless `--cfg=bazel_exec` is passed to rustc. | ||
#[cfg(bazel_exec)] | ||
fn exec() {} | ||
|
||
pub fn f() { | ||
exec(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// Sample source that fails to compile if `--cfg=bazel_exec` is passed to rustc. | ||
#[cfg(not(bazel_exec))] | ||
fn exec() {} | ||
|
||
pub fn f() { | ||
exec(); | ||
} |