fork/rules_rust

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

Daniel Wagner-HallAdd NUM_JOBS env var to cargo build scripts (#1216)8df4517d

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