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

fix: r_binary should respect --stamp flag #37

Closed
Show file tree
Hide file tree
Changes from all 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
19 changes: 19 additions & 0 deletions R/internal/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
load("//R/internal:context.bzl", "r_context_data")

package(default_visibility = ["//visibility:public"])

# Detect if the build is running under --stamp
config_setting(
name = "stamp",
values = {"stamp": "true"},
)

# Modelled after go_context_data from rules_go
# passes config values to starlark via a provider
r_context_data(
name = "r_context_data",
stamp = select({
"@com_grail_rules_r//R/internal:stamp": True,
"//conditions:default": False,
}),
)
13 changes: 11 additions & 2 deletions R/internal/binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ load(
_library_deps = "library_deps",
_runtime_path_export = "runtime_path_export",
)
load("@com_grail_rules_r//R:providers.bzl", "RBinary", "RLibrary", "RPackage")
load("@com_grail_rules_r//R:providers.bzl", "RBinary", "RLibrary", "RPackage", "RContextInfo")

def _r_markdown_stub(ctx):
stub = ctx.actions.declare_file(ctx.label.name + "_stub.R")
Expand Down Expand Up @@ -103,7 +103,8 @@ def _r_binary_impl(ctx):
)

layered_lib_files = _layer_library_deps(ctx, library_deps)
stamp_files = [ctx.info_file, ctx.version_file]
stamp = ctx.attr.stamp == 1 if ctx.attr.stamp >= 0 else ctx.attr._r_context_data[RContextInfo].stamp
stamp_files = [ctx.info_file, ctx.version_file] if stamp else []
runfiles = ctx.runfiles(
files = library_deps.lib_dirs + stamp_files,
transitive_files = depset(transitive = [srcs, exe, tools]),
Expand Down Expand Up @@ -159,10 +160,18 @@ _R_BINARY_ATTRS = {
"script_args": attr.string_list(
doc = "A list of arguments to pass to the src script",
),
"stamp": attr.int(
default = -1,
doc = "Enable link stamping. Whether to encode build information into the binary. Default -1: Embedding of build information is controlled by the --[no]stamp flag.",
),
"_binary_sh_tpl": attr.label(
allow_single_file = True,
default = "@com_grail_rules_r//R/scripts:binary.sh.tpl",
),
"_r_context_data": attr.label(
default = "@com_grail_rules_r//R/internal:r_context_data",
providers = [RContextInfo],
),
}

_R_MARKDOWN_ATTRS = dict(_R_BINARY_ATTRS)
Expand Down
19 changes: 19 additions & 0 deletions R/internal/context.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"r_context_data rule"

load("@com_grail_rules_r//R:providers.bzl", "RContextInfo")

_DOC = """r_context_data gathers information about the build configuration.
It is a common dependency of all binary targets."""

def _impl(ctx):
return [RContextInfo(stamp = ctx.attr.stamp)]

# Modelled after go_context_data in rules_go
# Works around github.com/bazelbuild/bazel/issues/1054
r_context_data = rule(
implementation = _impl,
attrs = {
"stamp": attr.bool(mandatory = True),
},
doc = _DOC,
)
6 changes: 6 additions & 0 deletions R/providers.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,9 @@ RBinary = provider(
"pkg_deps": "list of direct package dependencies for this and other binary dependencies",
},
)

#Modelled after _GoContextData in rules_go/go/private/context.bzl
RContextInfo = provider(
doc = "Provides data about the build context, like config_setting's",
fields = ["stamp"],
)
2 changes: 1 addition & 1 deletion R/scripts/system_state.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ printf "\\n=== R environment ===\\n"
# https://stat.ethz.ch/R-manual/R-devel/library/base/html/EnvVar.html
# But in build actions, bazel will mask most of the ones not coming from R, so we can ignore those.
# shellcheck disable=SC2086
"${RSCRIPT:-"Rscript"}" --no-init-file ${ARGS:-} -e 'Sys.getenv()' | grep "^R_"
"${RSCRIPT:-"Rscript"}" --no-init-file ${ARGS:-} -e 'Sys.getenv()' | grep "^R_" | grep -v "^R_SESSION_TMPDIR"
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,18 @@ the runfiles of the root executable.
<p>A list of arguments to pass to the src script.</p>
</td>
</tr>
<tr>
<td><code>stamp</code></td>
<td>
<p><code>Integer; optional; default is -1</code></p>
<p>Enable link stamping. Whether to encode build information into the binary. Possible values:</p>
<ul>
<li>stamp = 1: Stamp the build information into the binary. Stamped binaries are only rebuilt when their dependencies change. Use this if there are tests that depend on the build information.</li>
<li>stamp = 0: Always replace build information by constant values. This gives good build result caching.</li>
<li>stamp = -1: Embedding of build information is controlled by the --[no]stamp flag.</li>
</ul>
</td>
</tr>
</tbody>
</table>

Expand Down
7 changes: 6 additions & 1 deletion tests/exampleA/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

load("@com_grail_rules_r//R:defs.bzl", "r_pkg")
load("@com_grail_rules_r//R:defs.bzl", "r_pkg", "r_test")

r_pkg(
name = "exampleA",
Expand All @@ -24,3 +24,8 @@ r_pkg(
),
visibility = ["//exampleB:__pkg__"],
)

r_test(
name = "test",
src = "//exampleA/R:fn.R",
)
1 change: 1 addition & 0 deletions tests/exampleA/R/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exports_files(["fn.R"])