fork/rules_rust

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

Brian SilvermanExpose the output directory from cargo_build_script (#1588)1cc37c26

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