Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate package license metadata #2476

Merged
merged 16 commits into from
Feb 14, 2024

Conversation

nickcollier
Copy link
Contributor

Copy link

google-cla bot commented Feb 12, 2024

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

Copy link
Collaborator

@UebelAndre UebelAndre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Can you show the rendered output of a BUILD file after this change?

@nickcollier
Copy link
Contributor Author

nickcollier commented Feb 12, 2024

Here is the BUILD file rendered from the rand crate when passing render_config(generate_package_metadata = True) into crates_repository

###############################################################################
# @generated
# DO NOT MODIFY: This file is auto-generated by a crate_universe tool. To 
# regenerate this file, run the following:
#
#     bazel sync --only=guessing_game_crates
###############################################################################

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

# buildifier: disable=bzl-visibility
load("@rules_rust//crate_universe/private:selects.bzl", "selects")

load("@rules_license//rules:package_info.bzl", "package_info")

load("@rules_license//rules:license.bzl", "license")

package(
    default_package_metadata = [
        ":license",
        ":package_info",
    ],
    default_visibility = ["//visibility:public"],
)

package_info(
    name = "package_info",
    package_name = "rand",
    package_version = "0.8.5",
    package_url = "https://github.com/rust-random/rand",
)

license(
    name = "license",
    license_kinds = [
        "@rules_license//licenses/spdx:Apache-2.0",
        "@rules_license//licenses/spdx:MIT",
    ],
)

# licenses([
#     "TODO",  # MIT OR Apache-2.0
# ])

rust_library(
    name = "rand",
    deps = [
        "@guessing_game_crates__rand_chacha-0.3.1//:rand_chacha",
        "@guessing_game_crates__rand_core-0.6.4//:rand_core",
    ] + select({
        "@rules_rust//rust/platform:aarch64-apple-darwin": [
            "@guessing_game_crates__libc-0.2.152//:libc",  # cfg(unix)
        ],
        "@rules_rust//rust/platform:aarch64-apple-ios": [
            "@guessing_game_crates__libc-0.2.152//:libc",  # cfg(unix)
        ],
        "@rules_rust//rust/platform:aarch64-apple-ios-sim": [
            "@guessing_game_crates__libc-0.2.152//:libc",  # cfg(unix)
        ],
        "@rules_rust//rust/platform:aarch64-fuchsia": [
            "@guessing_game_crates__libc-0.2.152//:libc",  # cfg(unix)
        ],
        "@rules_rust//rust/platform:aarch64-linux-android": [
            "@guessing_game_crates__libc-0.2.152//:libc",  # cfg(unix)
        ],
        "@rules_rust//rust/platform:aarch64-unknown-linux-gnu": [
            "@guessing_game_crates__libc-0.2.152//:libc",  # cfg(unix)
        ],
        "@rules_rust//rust/platform:aarch64-unknown-nixos-gnu": [
            "@guessing_game_crates__libc-0.2.152//:libc",  # cfg(unix)
        ],
        "@rules_rust//rust/platform:aarch64-unknown-nto-qnx710": [
            "@guessing_game_crates__libc-0.2.152//:libc",  # cfg(unix)
        ],
        "@rules_rust//rust/platform:arm-unknown-linux-gnueabi": [
            "@guessing_game_crates__libc-0.2.152//:libc",  # cfg(unix)
        ],
        "@rules_rust//rust/platform:armv7-linux-androideabi": [
            "@guessing_game_crates__libc-0.2.152//:libc",  # cfg(unix)
        ],
        "@rules_rust//rust/platform:armv7-unknown-linux-gnueabi": [
            "@guessing_game_crates__libc-0.2.152//:libc",  # cfg(unix)
        ],
        "@rules_rust//rust/platform:i686-apple-darwin": [
            "@guessing_game_crates__libc-0.2.152//:libc",  # cfg(unix)
        ],
        "@rules_rust//rust/platform:i686-linux-android": [
            "@guessing_game_crates__libc-0.2.152//:libc",  # cfg(unix)
        ],
        "@rules_rust//rust/platform:i686-unknown-freebsd": [
            "@guessing_game_crates__libc-0.2.152//:libc",  # cfg(unix)
        ],
        "@rules_rust//rust/platform:i686-unknown-linux-gnu": [
            "@guessing_game_crates__libc-0.2.152//:libc",  # cfg(unix)
        ],
        "@rules_rust//rust/platform:powerpc-unknown-linux-gnu": [
            "@guessing_game_crates__libc-0.2.152//:libc",  # cfg(unix)
        ],
        "@rules_rust//rust/platform:s390x-unknown-linux-gnu": [
            "@guessing_game_crates__libc-0.2.152//:libc",  # cfg(unix)
        ],
        "@rules_rust//rust/platform:x86_64-apple-darwin": [
            "@guessing_game_crates__libc-0.2.152//:libc",  # cfg(unix)
        ],
        "@rules_rust//rust/platform:x86_64-apple-ios": [
            "@guessing_game_crates__libc-0.2.152//:libc",  # cfg(unix)
        ],
        "@rules_rust//rust/platform:x86_64-fuchsia": [
            "@guessing_game_crates__libc-0.2.152//:libc",  # cfg(unix)
        ],
        "@rules_rust//rust/platform:x86_64-linux-android": [
            "@guessing_game_crates__libc-0.2.152//:libc",  # cfg(unix)
        ],
        "@rules_rust//rust/platform:x86_64-unknown-freebsd": [
            "@guessing_game_crates__libc-0.2.152//:libc",  # cfg(unix)
        ],
        "@rules_rust//rust/platform:x86_64-unknown-linux-gnu": [
            "@guessing_game_crates__libc-0.2.152//:libc",  # cfg(unix)
        ],
        "@rules_rust//rust/platform:x86_64-unknown-nixos-gnu": [
            "@guessing_game_crates__libc-0.2.152//:libc",  # cfg(unix)
        ],
        "//conditions:default": [],
    }),
    compile_data = glob(
        include = ["**"],
        exclude = [
            "**/* *",
            ".tmp_git_root/**/*",
            "BUILD",
            "BUILD.bazel",
            "WORKSPACE",
            "WORKSPACE.bazel",
        ],
    ),
    crate_features = [
        "alloc",
        "default",
        "getrandom",
        "libc",
        "rand_chacha",
        "std",
        "std_rng",
    ],
    crate_root = "src/lib.rs",
    edition = "2018",
    rustc_flags = [
        "--cap-lints=allow",
    ],
    srcs = glob(["**/*.rs"]),
    tags = [
        "cargo-bazel",
        "crate-name=rand",
        "manual",
        "noclippy",
        "norustfmt",
    ],
    target_compatible_with = select({
        "@rules_rust//rust/platform:aarch64-apple-darwin": [],
        "@rules_rust//rust/platform:aarch64-apple-ios": [],
        "@rules_rust//rust/platform:aarch64-apple-ios-sim": [],
        "@rules_rust//rust/platform:aarch64-fuchsia": [],
        "@rules_rust//rust/platform:aarch64-linux-android": [],
        "@rules_rust//rust/platform:aarch64-pc-windows-msvc": [],
        "@rules_rust//rust/platform:aarch64-unknown-linux-gnu": [],
        "@rules_rust//rust/platform:aarch64-unknown-nixos-gnu": [],
        "@rules_rust//rust/platform:aarch64-unknown-nto-qnx710": [],
        "@rules_rust//rust/platform:arm-unknown-linux-gnueabi": [],
        "@rules_rust//rust/platform:armv7-linux-androideabi": [],
        "@rules_rust//rust/platform:armv7-unknown-linux-gnueabi": [],
        "@rules_rust//rust/platform:i686-apple-darwin": [],
        "@rules_rust//rust/platform:i686-linux-android": [],
        "@rules_rust//rust/platform:i686-pc-windows-msvc": [],
        "@rules_rust//rust/platform:i686-unknown-freebsd": [],
        "@rules_rust//rust/platform:i686-unknown-linux-gnu": [],
        "@rules_rust//rust/platform:powerpc-unknown-linux-gnu": [],
        "@rules_rust//rust/platform:riscv32imc-unknown-none-elf": [],
        "@rules_rust//rust/platform:riscv64gc-unknown-none-elf": [],
        "@rules_rust//rust/platform:s390x-unknown-linux-gnu": [],
        "@rules_rust//rust/platform:thumbv7em-none-eabi": [],
        "@rules_rust//rust/platform:thumbv8m.main-none-eabi": [],
        "@rules_rust//rust/platform:wasm32-unknown-unknown": [],
        "@rules_rust//rust/platform:wasm32-wasi": [],
        "@rules_rust//rust/platform:x86_64-apple-darwin": [],
        "@rules_rust//rust/platform:x86_64-apple-ios": [],
        "@rules_rust//rust/platform:x86_64-fuchsia": [],
        "@rules_rust//rust/platform:x86_64-linux-android": [],
        "@rules_rust//rust/platform:x86_64-pc-windows-msvc": [],
        "@rules_rust//rust/platform:x86_64-unknown-freebsd": [],
        "@rules_rust//rust/platform:x86_64-unknown-linux-gnu": [],
        "@rules_rust//rust/platform:x86_64-unknown-nixos-gnu": [],
        "@rules_rust//rust/platform:x86_64-unknown-none": [],
        "//conditions:default": ["@platforms//:incompatible"],
    }),
    version = "0.8.5",
)

@UebelAndre
Copy link
Collaborator

Thanks! Should rules_license be added as a dependency in places?

@nickcollier
Copy link
Contributor Author

If you don't have a @rules_license repo and you pass in render_config(generate_package_metadata = True) then this will fail.
I wasn't too sure about adding extra dependencies but I am happy to do it if you think that is OK?
I assume the best place to do it would be in crate_universe_dependencies?

@nickcollier
Copy link
Contributor Author

I have added a dependency on rules_license in both rules_rust_dependencies and MODULE.bazel
All CI checks are now passing, please let me know if you would like any other changes.

Copy link
Collaborator

@UebelAndre UebelAndre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove the old

# licenses([
#     "TODO",  # MIT OR Apache-2.0
# ])

snippets from the generated BUILD files. They should be unnecessary in light of rules_license

@nickcollier
Copy link
Contributor Author

I've update crate_universe/src/rendering.rs to no longer render the commented out licenses snippet
I then ran the regen command listed at the top of each BUILD file in the repo that contained the old snippet.
This has resulted in a more changes than just removing the snippet - if you prefer I can revert this commit and only make the change to crate_universe/src/rendering.rs

@UebelAndre UebelAndre changed the title Generate package metadata Generate package license metadata Feb 13, 2024
Copy link
Collaborator

@UebelAndre UebelAndre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good so far! Question though, the only metadata that's generated is for licensing right? I renamed the PR to Generate package license metadata, is that a more accurate description or is there more data being generated here? If not I think the config flag should also include the word "license" if possible

@nickcollier
Copy link
Contributor Author

It generates two metadata items:

Although both are provided by rules_license only the license metadata item is explicitly related to licensing. The package_info really is metadata about the package itself and not it's license (it is useful for enabling use of rules_license SBOM generation tools!)

As these two metaitems are the only ones that currently exist, the flag does decide if the BUILD file package has default_package_metadata specified or not so I feel like the current name is valid.

However if you are concerned that you may include additional default_package_metadata items in the future and don't want them coupled with this config flag perhaps specifying that it is going to generate the metadata providers from rules_license would a good choice? Perhaps: render_config(generate_rules_license_package_metadata = True) or render_config(generate_rules_license_metadata = True)

@UebelAndre
Copy link
Collaborator

I like the latter option if you don’t mind :)

Copy link
Collaborator

@UebelAndre UebelAndre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thank you so much!

@UebelAndre UebelAndre enabled auto-merge (squash) February 14, 2024 00:30
@UebelAndre UebelAndre merged commit 172b34a into bazelbuild:main Feb 14, 2024
3 checks passed
github-merge-queue bot pushed a commit that referenced this pull request Feb 26, 2024
…#2521)

#2476 added rules_license license metadata to crate BUILD files but many
crates to do not have a license file specified in their cargo metadata.
This PR adds a fallback that attempts to locate a license file in the
crate package root directory.
qtica added a commit to qtica/rules_rust that referenced this pull request Apr 1, 2024
…bazelbuild#2521)

bazelbuild#2476 added rules_license license metadata to crate BUILD files but many
crates to do not have a license file specified in their cargo metadata.
This PR adds a fallback that attempts to locate a license file in the
crate package root directory.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants