fork/rules_rust

git clone https://git.julia.blue/fork/rules_rust

Brian SilvermanAdd a `cargo_dep_env` rule for setting build.rs environment variables (#1415)937bdc9d

0.8.0
18.7 KiB498 linesraw
1# buildifier: disable=module-docstring
2load("@bazel_skylib//lib:paths.bzl", "paths")
3load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "CPP_COMPILE_ACTION_NAME", "C_COMPILE_ACTION_NAME")
4load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain")
5load("//rust:defs.bzl", "rust_binary", "rust_common")
6
7# buildifier: disable=bzl-visibility
8load("//rust/private:providers.bzl", _DepInfo = "DepInfo")
9
10# buildifier: disable=bzl-visibility
11load("//rust/private:rustc.bzl", "BuildInfo", "get_compilation_mode_opts", "get_linker_and_args")
12
13# buildifier: disable=bzl-visibility
14load("//rust/private:utils.bzl", "dedent", "expand_dict_value_locations", "find_cc_toolchain", "find_toolchain", "name_to_crate_name")
15
16def get_cc_compile_args_and_env(cc_toolchain, feature_configuration):
17    """Gather cc environment variables from the given `cc_toolchain`
18
19    Args:
20        cc_toolchain (cc_toolchain): The current rule's `cc_toolchain`.
21        feature_configuration (FeatureConfiguration): Class used to construct command lines from CROSSTOOL features.
22
23    Returns:
24        tuple: A tuple of the following items:
25            - (sequence): A flattened C command line flags for given action.
26            - (sequence): A flattened CXX command line flags for given action.
27            - (dict): C environment variables to be set for given action.
28    """
29    compile_variables = cc_common.create_compile_variables(
30        feature_configuration = feature_configuration,
31        cc_toolchain = cc_toolchain,
32    )
33    cc_c_args = cc_common.get_memory_inefficient_command_line(
34        feature_configuration = feature_configuration,
35        action_name = C_COMPILE_ACTION_NAME,
36        variables = compile_variables,
37    )
38    cc_cxx_args = cc_common.get_memory_inefficient_command_line(
39        feature_configuration = feature_configuration,
40        action_name = CPP_COMPILE_ACTION_NAME,
41        variables = compile_variables,
42    )
43    cc_env = cc_common.get_environment_variables(
44        feature_configuration = feature_configuration,
45        action_name = C_COMPILE_ACTION_NAME,
46        variables = compile_variables,
47    )
48    return cc_c_args, cc_cxx_args, cc_env
49
50def _pwd_flags(args):
51    """Prefix execroot-relative paths of known arguments with ${pwd}.
52
53    Args:
54        args (list): List of tool arguments.
55
56    Returns:
57        list: The modified argument list.
58    """
59    res = []
60    for arg in args:
61        s, opt, path = arg.partition("--sysroot=")
62        if s == "" and not paths.is_absolute(path):
63            res.append("{}${{pwd}}/{}".format(opt, path))
64        else:
65            res.append(arg)
66    return res
67
68def _build_script_impl(ctx):
69    """The implementation for the `_build_script_run` rule.
70
71    Args:
72        ctx (ctx): The rules context object
73
74    Returns:
75        list: A list containing a BuildInfo provider
76    """
77    script = ctx.executable.script
78    toolchain = find_toolchain(ctx)
79    out_dir = ctx.actions.declare_directory(ctx.label.name + ".out_dir")
80    env_out = ctx.actions.declare_file(ctx.label.name + ".env")
81    dep_env_out = ctx.actions.declare_file(ctx.label.name + ".depenv")
82    flags_out = ctx.actions.declare_file(ctx.label.name + ".flags")
83    link_flags = ctx.actions.declare_file(ctx.label.name + ".linkflags")
84    link_search_paths = ctx.actions.declare_file(ctx.label.name + ".linksearchpaths")  # rustc-link-search, propagated from transitive dependencies
85    manifest_dir = "%s.runfiles/%s/%s" % (script.path, ctx.label.workspace_name or ctx.workspace_name, ctx.label.package)
86    compilation_mode_opt_level = get_compilation_mode_opts(ctx, toolchain).opt_level
87
88    streams = struct(
89        stdout = ctx.actions.declare_file(ctx.label.name + ".stdout.log"),
90        stderr = ctx.actions.declare_file(ctx.label.name + ".stderr.log"),
91    )
92
93    pkg_name = _name_to_pkg_name(ctx.label.name)
94
95    toolchain_tools = [toolchain.all_files]
96
97    cc_toolchain = find_cpp_toolchain(ctx)
98
99    # Start with the default shell env, which contains any --action_env
100    # settings passed in on the command line.
101    env = dict(ctx.configuration.default_shell_env)
102
103    env.update({
104        "CARGO_CRATE_NAME": name_to_crate_name(pkg_name),
105        "CARGO_MANIFEST_DIR": manifest_dir,
106        "CARGO_PKG_NAME": pkg_name,
107        "HOST": toolchain.exec_triple,
108        "NUM_JOBS": "1",
109        "OPT_LEVEL": compilation_mode_opt_level,
110        "RUSTC": toolchain.rustc.path,
111        "TARGET": toolchain.target_flag_value,
112        # OUT_DIR is set by the runner itself, rather than on the action.
113    })
114
115    # This isn't exactly right, but Bazel doesn't have exact views of "debug" and "release", so...
116    env.update({
117        "DEBUG": {"dbg": "true", "fastbuild": "true", "opt": "false"}.get(ctx.var["COMPILATION_MODE"], "true"),
118        "PROFILE": {"dbg": "debug", "fastbuild": "debug", "opt": "release"}.get(ctx.var["COMPILATION_MODE"], "unknown"),
119    })
120
121    if ctx.attr.version:
122        version = ctx.attr.version.split("+")[0].split(".")
123        patch = version[2].split("-") if len(version) > 2 else [""]
124        env["CARGO_PKG_VERSION_MAJOR"] = version[0]
125        env["CARGO_PKG_VERSION_MINOR"] = version[1] if len(version) > 1 else ""
126        env["CARGO_PKG_VERSION_PATCH"] = patch[0]
127        env["CARGO_PKG_VERSION_PRE"] = patch[1] if len(patch) > 1 else ""
128        env["CARGO_PKG_VERSION"] = ctx.attr.version
129
130    # Pull in env vars which may be required for the cc_toolchain to work (e.g. on OSX, the SDK version).
131    # We hope that the linker env is sufficient for the whole cc_toolchain.
132    cc_toolchain, feature_configuration = find_cc_toolchain(ctx)
133    linker, link_args, linker_env = get_linker_and_args(ctx, ctx.attr, cc_toolchain, feature_configuration, None)
134    env.update(**linker_env)
135    env["LD"] = linker
136    env["LDFLAGS"] = " ".join(_pwd_flags(link_args))
137
138    # MSVC requires INCLUDE to be set
139    cc_c_args, cc_cxx_args, cc_env = get_cc_compile_args_and_env(cc_toolchain, feature_configuration)
140    include = cc_env.get("INCLUDE")
141    if include:
142        env["INCLUDE"] = include
143
144    if cc_toolchain:
145        toolchain_tools.append(cc_toolchain.all_files)
146
147        cc_executable = cc_toolchain.compiler_executable
148        if cc_executable:
149            env["CC"] = cc_executable
150            env["CXX"] = cc_executable
151        ar_executable = cc_toolchain.ar_executable
152        if ar_executable:
153            env["AR"] = ar_executable
154
155        # Populate CFLAGS and CXXFLAGS that cc-rs relies on when building from source, in particular
156        # to determine the deployment target when building for apple platforms (`macosx-version-min`
157        # for example, itself derived from the `macos_minimum_os` Bazel argument).
158        env["CFLAGS"] = " ".join(_pwd_flags(cc_c_args))
159        env["CXXFLAGS"] = " ".join(_pwd_flags(cc_cxx_args))
160
161    # Inform build scripts of rustc flags
162    # https://github.com/rust-lang/cargo/issues/9600
163    env["CARGO_ENCODED_RUSTFLAGS"] = "\\x1f".join([
164        # Allow build scripts to locate the generated sysroot
165        "--sysroot=${{pwd}}/{}".format(toolchain.sysroot),
166    ] + ctx.attr.rustc_flags)
167
168    for f in ctx.attr.crate_features:
169        env["CARGO_FEATURE_" + f.upper().replace("-", "_")] = "1"
170
171    # Add environment variables from the Rust toolchain.
172    env.update(toolchain.env)
173
174    env.update(expand_dict_value_locations(
175        ctx,
176        ctx.attr.build_script_env,
177        getattr(ctx.attr, "data", []) +
178        getattr(ctx.attr, "compile_data", []) +
179        getattr(ctx.attr, "tools", []),
180    ))
181
182    tools = depset(
183        direct = [
184            script,
185            ctx.executable._cargo_build_script_runner,
186        ] + ctx.files.data + ctx.files.tools + ([toolchain.target_json] if toolchain.target_json else []),
187        transitive = toolchain_tools,
188    )
189
190    links = ctx.attr.links or ""
191
192    # dep_env_file contains additional environment variables coming from
193    # direct dependency sys-crates' build scripts. These need to be made
194    # available to the current crate build script.
195    # See https://doc.rust-lang.org/cargo/reference/build-scripts.html#-sys-packages
196    # for details.
197    args = ctx.actions.args()
198    args.add_all([
199        script.path,
200        links,
201        out_dir.path,
202        env_out.path,
203        flags_out.path,
204        link_flags.path,
205        link_search_paths.path,
206        dep_env_out.path,
207        streams.stdout.path,
208        streams.stderr.path,
209    ])
210    build_script_inputs = []
211    for dep in ctx.attr.deps:
212        if rust_common.dep_info in dep and dep[rust_common.dep_info].dep_env:
213            dep_env_file = dep[rust_common.dep_info].dep_env
214            args.add(dep_env_file.path)
215            build_script_inputs.append(dep_env_file)
216            for dep_build_info in dep[rust_common.dep_info].transitive_build_infos.to_list():
217                build_script_inputs.append(dep_build_info.out_dir)
218
219    ctx.actions.run(
220        executable = ctx.executable._cargo_build_script_runner,
221        arguments = [args],
222        outputs = [out_dir, env_out, flags_out, link_flags, link_search_paths, dep_env_out, streams.stdout, streams.stderr],
223        tools = tools,
224        inputs = build_script_inputs,
225        mnemonic = "CargoBuildScriptRun",
226        progress_message = "Running Cargo build script {}".format(pkg_name),
227        env = env,
228    )
229
230    return [
231        BuildInfo(
232            out_dir = out_dir,
233            rustc_env = env_out,
234            dep_env = dep_env_out,
235            flags = flags_out,
236            link_flags = link_flags,
237            link_search_paths = link_search_paths,
238        ),
239        OutputGroupInfo(streams = depset([streams.stdout, streams.stderr])),
240    ]
241
242_build_script_run = rule(
243    doc = (
244        "A rule for running a crate's `build.rs` files to generate build information " +
245        "which is then used to determine how to compile said crate."
246    ),
247    implementation = _build_script_impl,
248    attrs = {
249        "build_script_env": attr.string_dict(
250            doc = "Environment variables for build scripts.",
251        ),
252        "crate_features": attr.string_list(
253            doc = "The list of rust features that the build script should consider activated.",
254        ),
255        "data": attr.label_list(
256            doc = "Data required by the build script.",
257            allow_files = True,
258        ),
259        "deps": attr.label_list(
260            doc = "The Rust dependencies of the crate",
261            providers = [rust_common.dep_info],
262        ),
263        "links": attr.string(
264            doc = "The name of the native library this crate links against.",
265        ),
266        "rustc_flags": attr.string_list(
267            doc = dedent("""\
268                List of compiler flags passed to `rustc`.
269
270                These strings are subject to Make variable expansion for predefined
271                source/output path variables like `$location`, `$execpath`, and 
272                `$rootpath`. This expansion is useful if you wish to pass a generated
273                file of arguments to rustc: `@$(location //package:target)`.
274            """),
275        ),
276        # The source of truth will be the `cargo_build_script` macro until stardoc
277        # implements documentation inheritence. See https://github.com/bazelbuild/stardoc/issues/27
278        "script": attr.label(
279            doc = "The binary script to run, generally a `rust_binary` target.",
280            executable = True,
281            allow_files = True,
282            mandatory = True,
283            cfg = "exec",
284        ),
285        "tools": attr.label_list(
286            doc = "Tools required by the build script.",
287            allow_files = True,
288            cfg = "exec",
289        ),
290        "version": attr.string(
291            doc = "The semantic version (semver) of the crate",
292        ),
293        "_cargo_build_script_runner": attr.label(
294            executable = True,
295            allow_files = True,
296            default = Label("//cargo/cargo_build_script_runner:cargo_build_script_runner"),
297            cfg = "exec",
298        ),
299        "_cc_toolchain": attr.label(
300            default = Label("@bazel_tools//tools/cpp:current_cc_toolchain"),
301        ),
302    },
303    fragments = ["cpp"],
304    toolchains = [
305        str(Label("//rust:toolchain")),
306        "@bazel_tools//tools/cpp:toolchain_type",
307    ],
308    incompatible_use_toolchain_transition = True,
309)
310
311def cargo_build_script(
312        name,
313        crate_features = [],
314        version = None,
315        deps = [],
316        build_script_env = {},
317        data = [],
318        tools = [],
319        links = None,
320        rustc_env = {},
321        rustc_flags = [],
322        visibility = None,
323        tags = None,
324        **kwargs):
325    """Compile and execute a rust build script to generate build attributes
326
327    This rules take the same arguments as rust_binary.
328
329    Example:
330
331    Suppose you have a crate with a cargo build script `build.rs`:
332
333    ```output
334    [workspace]/
335        hello_lib/
336            BUILD
337            build.rs
338            src/
339                lib.rs
340    ```
341
342    Then you want to use the build script in the following:
343
344    `hello_lib/BUILD`:
345    ```python
346    package(default_visibility = ["//visibility:public"])
347
348    load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_library")
349    load("@rules_rust//cargo:cargo_build_script.bzl", "cargo_build_script")
350
351    # This will run the build script from the root of the workspace, and
352    # collect the outputs.
353    cargo_build_script(
354        name = "build_script",
355        srcs = ["build.rs"],
356        # Optional environment variables passed during build.rs compilation
357        rustc_env = {
358           "CARGO_PKG_VERSION": "0.1.2",
359        },
360        # Optional environment variables passed during build.rs execution.
361        # Note that as the build script's working directory is not execroot,
362        # execpath/location will return an absolute path, instead of a relative
363        # one.
364        build_script_env = {
365            "SOME_TOOL_OR_FILE": "$(execpath @tool//:binary)"
366        }
367        # Optional data/tool dependencies
368        data = ["@tool//:binary"],
369    )
370
371    rust_library(
372        name = "hello_lib",
373        srcs = [
374            "src/lib.rs",
375        ],
376        deps = [":build_script"],
377    )
378    ```
379
380    The `hello_lib` target will be build with the flags and the environment variables declared by the \
381    build script in addition to the file generated by it.
382
383    Args:
384        name (str): The name for the underlying rule. This should be the name of the package being compiled, optionally with a suffix of _build_script.
385        crate_features (list, optional): A list of features to enable for the build script.
386        version (str, optional): The semantic version (semver) of the crate.
387        deps (list, optional): The dependencies of the crate.
388        build_script_env (dict, optional): Environment variables for build scripts.
389        data (list, optional): Files needed by the build script.
390        tools (list, optional): Tools (executables) needed by the build script.
391        links (str, optional): Name of the native library this crate links against.
392        rustc_env (dict, optional): Environment variables to set in rustc when compiling the build script.
393        rustc_flags (list, optional): List of compiler flags passed to `rustc`.
394        visibility (list of label, optional): Visibility to apply to the generated build script output.
395        tags: (list of str, optional): Tags to apply to the generated build script output.
396        **kwargs: Forwards to the underlying `rust_binary` rule.
397    """
398
399    # This duplicates the code in _build_script_impl because we need to make these available both when we invoke rustc (this code) and when we run the compiled build script (_build_script_impl).
400    # https://github.com/bazelbuild/rules_rust/issues/661 will hopefully remove this duplication.
401    rustc_env = dict(rustc_env)
402    if "CARGO_PKG_NAME" not in rustc_env:
403        rustc_env["CARGO_PKG_NAME"] = _name_to_pkg_name(name)
404    if "CARGO_CRATE_NAME" not in rustc_env:
405        rustc_env["CARGO_CRATE_NAME"] = name_to_crate_name(_name_to_pkg_name(name))
406
407    binary_tags = [tag for tag in tags or []]
408    if "manual" not in binary_tags:
409        binary_tags.append("manual")
410
411    rust_binary(
412        name = name + "_",
413        crate_features = crate_features,
414        version = version,
415        deps = deps,
416        data = data,
417        rustc_env = rustc_env,
418        rustc_flags = rustc_flags,
419        tags = binary_tags,
420        **kwargs
421    )
422    _build_script_run(
423        name = name,
424        script = ":{}_".format(name),
425        crate_features = crate_features,
426        version = version,
427        build_script_env = build_script_env,
428        links = links,
429        deps = deps,
430        data = data,
431        tools = tools,
432        rustc_flags = rustc_flags,
433        visibility = visibility,
434        tags = tags,
435    )
436
437def _name_to_pkg_name(name):
438    if name.endswith("_build_script"):
439        return name[:-len("_build_script")]
440    return name
441
442def _cargo_dep_env_implementation(ctx):
443    empty_file = ctx.actions.declare_file(ctx.label.name + ".empty_file")
444    empty_dir = ctx.actions.declare_directory(ctx.label.name + ".empty_dir")
445    ctx.actions.write(
446        output = empty_file,
447        content = "",
448    )
449    ctx.actions.run(
450        outputs = [empty_dir],
451        executable = "true",
452    )
453    return [
454        DefaultInfo(files = depset(ctx.files.src)),
455        BuildInfo(
456            dep_env = empty_file,
457            flags = empty_file,
458            link_flags = empty_file,
459            link_search_paths = empty_file,
460            out_dir = empty_dir,
461            rustc_env = empty_file,
462        ),
463        _DepInfo(
464            dep_env = ctx.file.src,
465            direct_crates = depset(),
466            link_search_path_files = depset(),
467            transitive_build_infos = depset(),
468            transitive_crate_outputs = depset(),
469            transitive_crates = depset(),
470            transitive_noncrates = depset(),
471        ),
472    ]
473
474cargo_dep_env = rule(
475    implementation = _cargo_dep_env_implementation,
476    doc = (
477        "A rule for generating variables for dependent `cargo_build_script`s " +
478        "without a build script. This is useful for using Bazel rules instead " +
479        "of a build script, while also generating configuration information " +
480        "for build scripts which depend on this crate."
481    ),
482    attrs = {
483        "src": attr.label(
484            doc = dedent("""\
485                File containing additional environment variables to set for build scripts of direct dependencies.
486
487                This has the same effect as a `cargo_build_script` which prints
488                `cargo:VAR=VALUE` lines, but without requiring a build script.
489
490                This files should  contain a single variable per line, of format
491                `NAME=value`, and newlines may be included in a value by ending a
492                line with a trailing back-slash (`\\\\`).
493            """),
494            allow_single_file = True,
495            mandatory = True,
496        ),
497    },
498)