fork/rules_rust

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

kalcuttercargo: Fix handling of relative sysroots (#1371)7adf7210

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