Commit Graph

111 Commits

Author SHA1 Message Date
Daniel Wagner-Hall c603a7710d
Add per-target link flags in the right place (#2963)
Previously we were adding these early on in the link args.

This was a bug - if some transitive dep foo has a linkopt of `-lbar`,
the `-lbar` needs to be later in the link args list than `-lfoo`.

Rather than arbitrarily adding them early on and hoping things work,
this adds the args for each library just after we add a dependency on
that library.
2024-10-26 20:36:01 +00:00
UebelAndre 7639ba7c3a
Fix fuchsia platform triple (#2945)
This change aligns with https://github.com/rust-lang/rust/issues/130661
and addresses issues with `crate_universe` in newer versions of Rust.
2024-10-20 15:33:14 +00:00
Marcel Hlopko 966678bf17
Allowlist more clang flags in bindgen (#2902)
Especially `--target` is very usable for cross-compilation use cases.
2024-10-01 08:57:35 +00:00
UebelAndre 59464fe1d9
Update `cargo_build_script` to work without runfiles support. (#2887)
Partially addresses https://github.com/bazelbuild/rules_rust/issues/1156

This pull-request implements an additional change to enable this
behavior which aggregates all transitive `BuildInfo.compile_data` into
`Rustc` actions. While this seems to bloat these actions with
unnecessary data, it addresses a catastrophic flaw in how Windows works
at all.

As of Bazel 7.3.1, Windows does not run any actions in a sandbox
(https://github.com/bazelbuild/bazel/discussions/18401), this means that
references to the current working directory will be consistent since
they always refer to the execroot. On top of this fact,
`cargo_build_script` will assign
[CARGO_MANIFEST_DIR](https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-crates)
to a [path within the runfiles directory of the build
script](d9ee6172d1/cargo/private/cargo_build_script.bzl (L218)).
The combination of these two facts leads crates like
[windows_x86_64_msvc](https://crates.io/crates/windows_x86_64_msvc),
which assign a linker path using `CARGO_MANIFEST_DIR`
([@windows_x86_64_msvc//build.rs](https://github.com/microsoft/windows-rs/blob/0.59.0/crates/targets/x86_64_msvc/build.rs#L1-L8))
to introduce un-tracked dependencies into dependent `Rustc` actions.
This then leads to build failures if the `windows_x86_64_msvc` crate is
ever a remote cache hit for the dependents as runfiles (or
`.cargo_runfiles` in the case of this PR) are not fetched and will not
exist on the host at link time.

This change addresses this issue of untracked dependencies by ensuring
the runfiles of `cargo_build_script.script` targets are aggregated into
`Rustc` actions.
2024-09-19 19:57:07 +00:00
UebelAndre 51c9d3bbea
Update bindgen version to 0.70.1 (#2872) 2024-09-18 16:01:06 +00:00
UebelAndre 350d249ca2
Update crate_universe to determine dep+feature trees per host (#2877)
This change expands the use of `cargo tree` to resolve features for
different combinations of `host -> target` platform triples where before
`cargo-bazel` was only capable of resolving host dependencies for the
current host platform and not a foreign platform.

A lengthy description of how this was done can be found at
`crate_universe/src/metadata/cargo_tree_resolver.rs -
TreeResolver::create_rustc_wrapper`.

closes https://github.com/bazelbuild/rules_rust/issues/2212

---------

Co-authored-by: Daniel Wagner-Hall <dawagner@gmail.com>
2024-09-18 12:06:02 +00:00
UebelAndre 3bae148709
Update crate universe to generate build script targets with compile data (#2855)
https://github.com/bazelbuild/rules_rust/pull/2826 ended up introducing
a regression for crates with build scripts that require extra data at
compile time. This change adds a default glob for all
`cargo_build_script` targets generated by `crate_universe` and also
introduces an `annotation.build_script_compile_data` attribute to add
custom targets to generated outputs.
2024-09-11 13:38:53 +00:00
Cornelius Riemenschneider 9510ab4eec
crate_universe: Only compile label regex once. (#2751)
I hit this when profiling for something else, we spent 1.1sec of 1.3sec
of `cargo-bazel generate` compiling this regular expression. It seems to
be quite bad on the regex compiler, as even compiling it once takes 8ms.

I had to add the `once_cell` crate to unwrap the Result type - the
corresponding stdlib function is still nightly-only unfortunately.
However, that crate was already in the transitive dependency tree, so
it's a net-same change.
2024-07-31 20:35:49 +00:00
Daniel Wagner-Hall 813669bc2e
Add version to bindgen dep (#2768)
I don't know how this ever worked before. I've picked the version that
happens to be in the lockfile at the moment.

Fixes #2752
2024-07-30 20:41:18 +00:00
Marcel Hlopko 6a140b21a8
Make rustfmt toolchain optional in bindgen (#2744) 2024-07-17 14:00:46 +00:00
Marcel Hlopko 147886f6db
Make it possible to not specify clang binary in bindgen (#2746)
Co-authored-by: Krasimir Georgiev <krasimir@google.com>
2024-07-17 13:10:55 +00:00
Marcel Hlopko db89d83ce5
Pass toolchain param in ctx.action.run in bindgen (#2745) 2024-07-17 12:43:38 +00:00
Marcel Hlopko 6c2bd67af0
Add support for --wrap-extern-fns to bindgen rules (#2743) 2024-07-16 07:42:40 +00:00
Marcel Hlopko 116f6db8fa
Update bindgen.bzl to use toolchain system include directories as well (#2727)
This is needed when using toolchains that use custom include
directories.
2024-07-08 09:06:59 +00:00
Oliver Graff 2a0698fb65
Clean up downloads that go through the API instead of the CDN (#2718)
This is useful as it seems crates.io is starting to deny some direct
requests, not sure if its based off user agent or remote how IP.
2024-06-29 13:37:23 +00:00
UebelAndre f22926a2b4
Use `cargo tree` to determine feature dependent optional deps (#2636)
The Cargo [Feature Resolver version
2](https://doc.rust-lang.org/cargo/reference/resolver.html#feature-resolver-version-2)
behavior is currently not supported by the `cargo metadata` sub command
(https://github.com/rust-lang/cargo/issues/9863) which `crate_universe`
uses to determine the dependencies of a target, leading to inaccuracies
when dependencies are introduced via feature resolution for a particular
configuration.

In https://github.com/bazelbuild/rules_rust/pull/1710 functionality was
added to use `cargo tree` to perform feature resolution for each
supported platform. This change expands on this trick to collect
dependency information at the same time and use that to determine
whether or not to include optional dependencies located in standard
`cargo metadata` output in the rendered Bazel targets. Non optional or
`target.cfg` (conditional) dependencies behave as they did before this
change.

Implementation details:
- `FeatureGenerator` was replaced by `TreeResolver`
- Optional dependencies are now rendered as selects on explicit
platforms. This will expand the size of `cargo-bazel-lock.json` files
but is expected to be more correct.
2024-05-08 17:53:05 +00:00
UebelAndre 1afc1ebf20
Re-vendor crate_universe outputs (#2609) 2024-04-10 14:13:09 +00:00
UebelAndre 0cb20f516f
Deprecated `rust_bindgen.leak_symbols` (#2590)
This also plumbs `target_compatible_with` and `exec_compatible_with`
into the `rust_bindgen` target from `rust_bindgen_library` to account
for cases where the `"manual"` tag is not sufficient in disabling the
target (e.g. `cquery`).
2024-04-02 14:37:07 +00:00
Cameron Martin 33fdddd03c
Fix bzlmod examples on windows (#2454)
On windows (and some other platforms), the file extension of cargo,
rustc, etc have an extension. The module extension for loading crates
did not take this into account, causing it to error on windows.

Additionally, when using bzlmod to build vendored crates, the runfiles
tree would exceed the 260 char windows path limit. To mitigate this, I
have shortened the internal_deps module extension to just `i` and
changed the build script suffix to `_bs` from `_build_script`. This
makes the path names below the 260 char limit.

This makes the bzlmod CI run on windows, to avoid regressing this.
Currently gen_rust_project does not run on windows for other reasons,
although we do build this.
2024-02-23 12:55:58 +00:00
Cameron Martin cf5f0bd888
Re-vendor all crates (#2514)
It looks like the vendored crates got out of sync again. This updates
them all, using the new script `//crate_universe/tools:vendor`.
2024-02-23 10:03:02 +00:00
Vinh Tran 8fd0904a58
Fix linkopts set in cc deps of bindgen (#2422)
### Problem

As of now (before this PR), we seem to mix `link_flags` file into using
for two purposes.

For
```
cc_library(
    name = "simple",
    srcs = ["simple.cc"],
    hdrs = ["simple.h"],
    linkopts = ["-framework"],
)
rust_bindgen_library(
    name = "simple_bindgen",
    cc_lib = ":simple",
    header = "simple.h",
)
rust_binary(
    name = "simple_example",
    srcs = ["main.rs"],
    deps = [":simple_bindgen"],
)
```
`rust_bindgen_library` produces a `link_flags` file with

```
-lstatic=simple
-C
link-args=-framework
```

`-lstatic=simple` is consumed by `rustc` whereas `-framework` is
expected to be consumed by an actual linker (either invoked by `rustc`
or `cc_common.link`)

The flags are then passed to `rustc` command to compile
`libsimple_bindgen.rlib`. It does not yield any error because
`-lstatic=simple` is correctly used whereas `-framework` is no-op (there
is no linker being invoked for producing `rlib`). However, the rustc
***doesn't*** pass `-framework` to the linker that link the
`rust_binary` (which is where the cc linkopts matters).

Another problem is that this approach is not compatible with
`experimental_use_cc_common_link` option which let `cc_common.link`
instead of `rustc` invoke the linker. See #2413

### Solution

We're referring "link" as at least two things (which I think what causes
problem here):

1.
https://doc.rust-lang.org/rustc/command-line-arguments.html#-l-link-the-generated-crate-to-a-native-library
2. https://doc.rust-lang.org/rustc/codegen-options/index.html#linker
https://doc.rust-lang.org/rustc/codegen-options/index.html#link-args

As proposed in
https://github.com/bazelbuild/rules_rust/pull/2415#issuecomment-1890109092,
this PR splits `<rust_library_bindgen>__bindgen.link_flags` produced by
`rust_bindgen` rule into two files:

1. `rustc_flags`
```
-lstatic=simple
```
2. `linker_flags`
```
-framework
```

We "sort-of" (but not perfectly) had it correct before
https://github.com/bazelbuild/rules_rust/pull/2361 when we link the
binary directly with the cc_library (aka both kinds of flags).

But since we want to support the Cargo style of linking
```
cc_lib -> bindgen -> lib_a  -> bin
```
instead of
```
cc_lib -> bindgen -> lib_a  -> bin
    \___________________________^
```
we can pass `-lstatic=simple` to the `rustc` command that builds
`simple_bindgen` (rust_library) and propagate `linkopts` to
`simple_example` (rust_binary) so that the linker can use it.

```
cc_library -> rust_bindgen ->    rust_library        -> rust_binary
                                                              
                                    -lstatic=simple
                                                            -Clink-args="-framework"
``` 

This is long and sounds like a proposal. I'm open for suggestion
@UebelAndre @illicitonion @krasimirgg

Fixes #2413
2024-02-15 23:04:34 -05:00
Nick Collier 172b34aabd
Generate package license metadata (#2476)
#2475
2024-02-14 00:37:38 +00:00
thomas 90e4505c19
Fix #2406: use codegen option `link-args` to forward `user_link_flags` in bindgen. (#2407)
This commit fixes issue #2406 by using the [`link-args`] codegen option
of `rustc`:

```
-C
link-args=<user_link_flags>
```

where `<user_link_flags>` are the flags from the [`user_link_flags`]
field of [`LinkerInput`].

[`user_link_flags`]:
https://bazel.build/rules/lib/builtins/LinkerInput#user_link_flags
[`LinkerInput`]: https://bazel.build/rules/lib/builtins/LinkerInput
[`link-args`]:
https://doc.rust-lang.org/rustc/codegen-options/index.html#link-args
2024-01-09 08:29:18 -08:00
thomas 012af3ba8f
Fix #2402: use `get_lib_name_default(lib)` instead of `lib.owner.name`. (#2405)
This commit fixes #2402 by using [`get_lib_name_default`] to pass
libraries names to `rustc` through `-lstatic` instead of relying on the
owner's name of the `File` object.

[`get_lib_name_default`]:
a1e9f9600c/rust/private/utils.bzl (L101)

Co-authored-by: UebelAndre <github@uebelandre.com>
2024-01-09 12:17:25 +00:00
UebelAndre 44a0161693
Allow `RustBindgen` action to get `--gcc-toolchain` from cc toolchains (#2400) 2024-01-08 16:31:36 +00:00
Krasimir Georgiev 53830aa0f4
remove incompatible_override_toolchain_transition (#2344)
This has been a no-op since Bazel 5. We're at min Bazel version 6.3.
2024-01-02 19:07:51 +01:00
UebelAndre 01dadedf6c
Add cc_toolchain files to bindgen actions (#2375)
Now that sysroots are correctly detected, bindgen is going to need
sources from them.
2023-12-28 18:34:37 +00:00
UebelAndre 23fcf67118
Update `rust_bindgen_library` to only link into the direct consumer (#2361)
Most uses of [bindgen](https://github.com/rust-lang/rust-bindgen) are
via [build
scripts](https://doc.rust-lang.org/cargo/reference/build-scripts.html)
which typically use either `-Lnative=` to specify linker paths for the
current target being built. The way the rules are currently defined, has
the bindgen library linked directly into the final library which leads
to some annoying ordering errors compared to the same project building
with Cargo. This PR aims to support the Cargo style of linking via a
`leak_symbols = False` flag, thus changing the dependency graph when
linking from:
```
cc_lib -> bindgen -> lib_a -> lib_b -> bin
    \__________________________________^
```
to:
```
cc_lib -> bindgen -> lib_a -> lib_b -> bin
```

Note that `leak_symbols` is intended to be used as an escape hatch to
restore the original behavior and will be deleted in the future.
2023-12-28 16:29:33 +00:00
UebelAndre fce8961dbb
Updated Bindgen to version `0.69.1` (#2367)
https://github.com/rust-lang/rust-bindgen/blob/v0.69.1/CHANGELOG.md#0691-2023-11-02
2023-12-28 13:38:27 +00:00
UebelAndre 5507a61e42
Remove legacy bindgen.bzl interface module (#2366) 2023-12-28 13:17:05 +00:00
UebelAndre c85ce76f1e
Include more cc_toolchain flags in RustBindgen actions (#2358)
This change parses `cc_toolchain` flags for `c++-compile` actions for
use in bindgen. This change aims to allow the action to gain toolchain
specific include paths as well as a more accurate `--sysroot`.
2023-12-27 08:05:49 -08:00
Matt 44ffccb91c
Allow additional targets to compile with bzlmod (#2314)
See #2246 for how we declare dependencies automatically for third party
crates. I'm using the same mechanism for all other repositories.

Note that the MODULE.bazel changes in use_repo are autogenerated, and if
you change the third party crates you depend on, you should get an error
message saying something like "run this command to fix it" on your
bzlmod presubmit environment.
2023-12-12 13:52:58 +00:00
Matt 3fd4987269
Declare dependencies in vendored crates (#2246)
Currently, when you run `bazel build --enable_bzlmod
//crate_universe:bin`, you get the error `undefined repo @cui`.

When you add a dependency on CUI, you get errors like `undefined repo
@cui__<crate>-<version>` (the repo for the third party crate).

This change will allow us to automatically declare dependencies on our
third party crates from bzlmod (see
https://docs.google.com/document/d/1dj8SN5L6nwhNOufNqjBhYkk5f-BJI_FPYWKxlB3GAmA/edit#heading=h.5mcn15i0e1ch).

---------

Co-authored-by: UebelAndre <github@uebelandre.com>
2023-12-04 07:50:52 -08:00
Matt 5d89c9245d
Run "bazel run //*:crates_vendor" (#2245)
This avoids very large diffs from some an unrelated PR I have coming.
2023-11-08 06:18:30 -08:00
UebelAndre 7d1cc0950f
Export bindgen rules through `@rules_rust//bindgen:defs.bzl` (#2054) 2023-09-22 09:53:16 +01:00
Daniel Wagner-Hall 1f9b63a408
cargo_build_scripts can be run from custom directories (#2152)
While running from CARGO_MANIFEST_DIR is the simplest thing for
compatibility, there are cases where a cargo build script may be easier
to run from the exec root as most bazel actions do (e.g. where a C++
toolchain specifies in-repo include paths).

This mirrors the `rundir` attribute of `go_test`, which has similar
concerns:
https://github.com/bazelbuild/rules_go/blob/master/docs/go/core/rules.md#go_test-rundir
2023-09-06 18:38:35 +02:00
David Zbarsky 3e8f19e47b
Cleanup args construction (#2122)
Some minor optimizations to analysis time memory usage (avoid creating
some list temporaries, some extra strings, etc.)
Some of the usage might be simpler as `args.add("foo", foo)` instead of
`args.add(foo, format="foo=%s")` but I opted to keep the usage the same
as before for safety.
2023-08-23 14:36:27 +02:00
UebelAndre 4e8041e1c0
Enable rustfmt in bindgen toolchains by default (#2055) 2023-07-07 13:59:35 +00:00
Cameron Martin 36f8251f97
Exclude .tmp_git_root from globs (#1948)
* Exclude .tmp_git_root from globs

This directory contains the original git repo when the crate is from git. Including this directory currently makes these rules re-run whenever the repository rule re-runs, although this is fixed by https://github.com/bazelbuild/bazel/pull/18271. Even after this fix, excluding this directory avoids depending on unnecessary files.

Fixes #1927.

* Regenerate vendored crates
2023-06-23 04:27:16 -07:00
UebelAndre ca750fa83d
Eliminate Rustfmt action in Bindgen rules. Bindgen can run rustfmt (#2025)
* Eliminate Rustfmt action in Bindgen rules. Bindgen can run rustfmt

* Remove conflicting attributes.

* Regenerate documentation
2023-06-21 14:23:57 +00:00
UebelAndre c55ec0cfb7
Allow sysroots from cc_toolchains to be added to bindgen actions (#2024) 2023-06-21 06:04:44 -07:00
UebelAndre 367f90ef05
Update bindgen version to 0.65.1 (#2008)
* Update bindgen version to 0.65.1

* Regenerate documentation

* Disable Bindgen examples on Windows
2023-06-14 05:43:55 -07:00
UebelAndre 3e2ee941ae
Update bindgen rules to build clang from source. (#1998)
* Update bindgen rules to build clang from source.

* Regenerate documentation
2023-06-13 11:52:39 +00:00
David Tolnay 56e7604879
Fix typo in crate_universe-generated defs.bzl comment (#1981) 2023-05-30 10:45:18 +01:00
Erik Gilling a5853fd370
Use correct dynamic link args fro proc-macro crates (#1803)
Some toolchains, notably https://github.com/bazelembedded/rules_cc_toolchain,
use different and incompatbile link arguments for executables and
shared libraries.

Co-authored-by: Daniel Wagner-Hall <dwagnerhall@apple.com>
2023-02-01 17:53:37 -08:00
UebelAndre 0fe742bff6
Updated `rust_bindgen` to use `rustfmt_toolchain` (#1770)
* Updated `rust_bindgen` to use `rustfmt_toolchain`

* Regenerate documentation
2023-01-19 06:06:04 -08:00
David Tolnay a31490d9a8
Make loads from @rules_rust//rust:defs.bzl come out on one line (#1753)
At first I was confused why some of the generated files were coming out with loads formatted like this:

4ef3d4aaa2/crate_universe/3rdparty/crates/BUILD.anyhow-1.0.68.bazel (L9-L13)

Surely this should be as follows? The wrapped line is even shorter than the unwrapped line.

```bzl
load("@rules_rust//cargo:defs.bzl", "cargo_build_script")
load("@rules_rust//rust:defs.bzl", "rust_library")
```

It turned out to be because crate_universe unconditionally generates _all_ the loads that might be needed:

```bzl
load("@rules_rust//cargo:defs.bzl", "cargo_build_script")
load(
    "@rules_rust//rust:defs.bzl",
    "rust_library",
    "rust_binary",
    "rust_proc_macro",
)
```

and then Buildifier deletes any that are unused, but without rewrapping any lines:

```bzl
load("@rules_rust//cargo:defs.bzl", "cargo_build_script")
load(
    "@rules_rust//rust:defs.bzl",
    "rust_library",
)
```

This PR tweaks crate_universe to generate only loads that are used, so that we retain control over how they are wrapped. After this PR, single loads get put on one line while multiple loads get put on multiple lines.

e987f2041d/crate_universe/3rdparty/crates/BUILD.anyhow-1.0.68.bazel (L9-L10)

e987f2041d/bindgen/3rdparty/crates/BUILD.bindgen-0.60.1.bazel (L9-L14)
2023-01-05 11:08:45 +00:00
David Tolnay 7ebad4d501
Generate only the needed subset of binaries for bindgen and proto (#1751) 2023-01-04 11:25:56 -08:00
David Tolnay d6e3003594
Regenerate BUILD files using serde_starlark renderer (#1746) 2023-01-04 09:33:08 -08:00
UebelAndre 92977d1bfb
Re-pinned all dependencies managed by crate_universe (#1735) 2023-01-03 18:06:40 +00:00