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

feat: support flat repos #97

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module(
compatibility_level = 1,
)

bazel_dep(name = "bazel_skylib", version = "1.5.0")
bazel_dep(name = "bazel_skylib", version = "1.7.1")
bazel_dep(name = "aspect_bazel_lib", version = "2.7.9")

bazel_lib_toolchains = use_extension("@aspect_bazel_lib//lib:extensions.bzl", "toolchains")
Expand All @@ -21,7 +21,7 @@ use_repo(bazel_lib_toolchains, "yq_linux_s390x")
use_repo(bazel_lib_toolchains, "yq_windows_amd64")

bazel_dep(name = "gazelle", version = "0.34.0", dev_dependency = True, repo_name = "bazel_gazelle")
bazel_dep(name = "bazel_skylib_gazelle_plugin", version = "1.5.0", dev_dependency = True)
bazel_dep(name = "bazel_skylib_gazelle_plugin", version = "1.7.1", dev_dependency = True)
bazel_dep(name = "buildifier_prebuilt", version = "6.1.2", dev_dependency = True)
bazel_dep(name = "platforms", version = "0.0.10", dev_dependency = True)
bazel_dep(name = "rules_oci", version = "2.0.0-rc0", dev_dependency = True)
Expand Down
12 changes: 6 additions & 6 deletions MODULE.bazel.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions WORKSPACE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,28 @@ load("@bullseye_nolock//:packages.bzl", "bullseye_nolock_packages")

bullseye_nolock_packages()

# bazel run @bullseye_rproject//:lock
deb_index(
name = "bullseye_rproject",
lock = "//examples/debian_flat_repo:bullseye_rproject.lock.json",
manifest = "//examples/debian_flat_repo:bullseye_rproject.yaml",
)

load("@bullseye_rproject//:packages.bzl", "bullseye_rproject_packages")

bullseye_rproject_packages()

# bazel run @nvidia_ubuntu2404_cuda//:lock
deb_index(
name = "nvidia_ubuntu2404_cuda",
lock = "//examples/debian_flat_repo:nvidia_ubuntu2404_cuda.lock.json",
manifest = "//examples/debian_flat_repo:nvidia_ubuntu2404_cuda.yaml",
)

load("@nvidia_ubuntu2404_cuda//:packages.bzl", "nvidia_ubuntu2404_cuda_packages")

nvidia_ubuntu2404_cuda_packages()

deb_index(
name = "apt_security",
manifest = "//examples/debian_snapshot_security:security.yaml",
Expand Down
1 change: 1 addition & 0 deletions apt/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ bzl_library(
"//apt/private:deb_import",
"//apt/private:index",
"//apt/private:lockfile",
"//apt/private:manifest",
"//apt/private:resolve",
],
)
37 changes: 18 additions & 19 deletions apt/extensions.bzl
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
"apt extensions"

load("//apt/private:deb_import.bzl", "deb_import")
load("//apt/private:deb_import.bzl", "deb_import", "make_deb_import_key")
load("//apt/private:index.bzl", "deb_package_index")
load("//apt/private:lockfile.bzl", "lockfile")
load("//apt/private:resolve.bzl", "deb_resolve", "internal_resolve")
load("//apt/private:manifest.bzl", "manifest")
load("//apt/private:resolve.bzl", "deb_resolve")

def _distroless_extension(module_ctx):
root_direct_deps = []
Expand All @@ -13,31 +14,29 @@ def _distroless_extension(module_ctx):
for install in mod.tags.install:
lockf = None
if not install.lock:
lockf = internal_resolve(
lockf = manifest.lock(
module_ctx,
"yq",
install.manifest,
install.resolve_transitive,
)

if not install.nolock:
# buildifier: disable=print
print("\nNo lockfile was given, please run `bazel run @%s//:lock` to create the lockfile." % install.name)
print(
"\nNo lockfile was given. To create one please run " +
"`bazel run @{}//:lock`".format(install.name),
)
else:
lockf = lockfile.from_json(module_ctx, module_ctx.read(install.lock))

for (package) in lockf.packages():
package_key = lockfile.make_package_key(
package["name"],
package["version"],
package["arch"],
)
for architectures in lockf.packages.values():
for package in architectures.values():
deb_import_key = make_deb_import_key(install.name, package)

deb_import(
name = "%s_%s" % (install.name, package_key),
urls = [package["url"]],
sha256 = package["sha256"],
)
deb_import(
name = deb_import_key,
url = package.url,
sha256 = package.sha256,
)

deb_resolve(
name = install.name + "_resolve",
Expand All @@ -49,7 +48,7 @@ def _distroless_extension(module_ctx):
name = install.name,
lock = install.lock,
lock_content = lockf.as_json(),
package_template = install.package_template,
package_arch_build_template = install.package_arch_build_template,
)

if mod.is_root:
Expand All @@ -75,7 +74,7 @@ install = tag_class(attrs = {
doc = """Whether dependencies of dependencies should be resolved and added to the lockfile.""",
default = True,
),
"package_template": attr.label(doc = "(EXPERIMENTAL!) a template file for generated BUILD files."),
"package_arch_build_template": attr.label(doc = "(EXPERIMENTAL!) a template file for the generated package BUILD files per architecture."),
})

apt = module_extension(
Expand Down
12 changes: 7 additions & 5 deletions apt/index.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def deb_index(
manifest,
lock = None,
nolock = False,
package_template = None,
package_arch_build_template = None,
resolve_transitive = True):
"""A convience repository macro for apt rules.

Expand Down Expand Up @@ -81,7 +81,7 @@ def deb_index(
manifest: label to a `manifest.yaml`
lock: label to a `lock.json`
nolock: bool, set to True if you explicitly want to run without a lock and avoid the DEBUG messages.
package_template: (EXPERIMENTAL!) a template file for generated BUILD files.
package_arch_build_template: (EXPERIMENTAL!) a template file for the generated package BUILD files per architecture.
Available template replacement keys are: `{target_name}`, `{deps}`, `{urls}`, `{name}`, `{arch}`, `{sha256}`, `{repo_name}`
resolve_transitive: whether dependencies of dependencies should be resolved and added to the lockfile.
"""
Expand All @@ -92,11 +92,13 @@ def deb_index(
)

if not lock and not nolock:
# buildifier: disable=print
print("\nNo lockfile was given, please run `bazel run @%s//:lock` to create the lockfile." % name)
print(
"\nNo lockfile was given. To create one please run " +
"`bazel run @{}//:lock`".format(name),
)

_deb_package_index(
name = name,
lock = lock if lock else "@" + name + "_resolve//:lock.json",
package_template = package_template,
package_arch_build_template = package_arch_build_template,
)
47 changes: 35 additions & 12 deletions apt/private/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
exports_files([
"dpkg_statusd.sh",
"dpkg_status.sh",
"package.BUILD.tmpl",
"copy.sh.tmpl",
"package_arch.BUILD.tmpl",
])

bzl_library(
Expand All @@ -25,31 +26,44 @@ bzl_library(
srcs = ["index.bzl"],
visibility = ["//apt:__subpackages__"],
deps = [
":deb_import",
":lockfile",
"@bazel_tools//tools/build_defs/repo:http.bzl",
"@bazel_tools//tools/build_defs/repo:utils.bzl",
],
)

bzl_library(
name = "lockfile",
srcs = ["lockfile.bzl"],
visibility = ["//apt:__subpackages__"],
deps = [":util"],
deps = [":pkg"],
)

bzl_library(
name = "package_index",
srcs = ["package_index.bzl"],
name = "pkg",
srcs = ["pkg.bzl"],
visibility = ["//apt:__subpackages__"],
deps = [":util"],
)

bzl_library(
name = "package_resolution",
srcs = ["package_resolution.bzl"],
name = "manifest",
srcs = ["manifest.bzl"],
visibility = ["//apt:__subpackages__"],
deps = [":version"],
deps = [
":lockfile",
":package_index",
":util",
"@aspect_bazel_lib//lib:repo_utils",
],
)

bzl_library(
name = "package_index",
srcs = ["package_index.bzl"],
visibility = ["//apt:__subpackages__"],
deps = [
":version",
"@bazel_skylib//lib:paths",
],
)

bzl_library(
Expand All @@ -58,8 +72,8 @@ bzl_library(
visibility = ["//apt:__subpackages__"],
deps = [
":lockfile",
":manifest",
":package_index",
":package_resolution",
"@aspect_bazel_lib//lib:repo_utils",
],
)
Expand All @@ -75,7 +89,16 @@ bzl_library(
name = "deb_import",
srcs = ["deb_import.bzl"],
visibility = ["//apt:__subpackages__"],
deps = ["@bazel_tools//tools/build_defs/repo:http.bzl"],
deps = [
":util",
# NOTE: I had to add cache.bzl because bazel test //docs:update_1_test was failing with
# Stardoc documentation generation failed:
# File <SANDBOX>/bin/docs/apt_stardoc.runfiles/bazel_tools/tools/build_defs/repo/http.bzl imported ':cache.bzl',
# yet <SANDBOX>/bin/docs/apt_stardoc.runfiles/bazel_tools/tools/build_defs/repo/cache.bzl was not found.
"@bazel_tools//tools/build_defs/repo:cache.bzl",
"@bazel_tools//tools/build_defs/repo:http.bzl",
"@bazel_tools//tools/build_defs/repo:utils.bzl",
],
)

bzl_library(
Expand Down
51 changes: 51 additions & 0 deletions apt/private/copy.sh.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env bash

set -o pipefail -o errexit -o nounset

lock=$(realpath "$1")
autofix=${{2:-}}

cd "$BUILD_WORKING_DIRECTORY"

echo
echo "Writing lockfile to {workspace_relative_path}"
cp "$lock" "{workspace_relative_path}"

# Detect which file we wish the user to edit
if [ -e "$BUILD_WORKSPACE_DIRECTORY/WORKSPACE" ]; then
wksp_file="WORKSPACE"
elif [ -e "$BUILD_WORKSPACE_DIRECTORY/WORKSPACE.bazel" ]; then
wksp_file="WORKSPACE.bazel"
elif [ -e "$BUILD_WORKSPACE_DIRECTORY/MODULE.bazel" ]; then
wksp_file="MODULE.bazel"
else
echo>&2 "Error: no MODULE.bazel or WORKSPACE file was found"
exit 1
fi

# Detect a vendored buildozer binary in canonical location (tools/buildozer)
if [ -e "$BUILD_WORKSPACE_DIRECTORY/tools/buildozer" ]; then
buildozer="tools/buildozer"
else
# Assume it's on the $PATH
buildozer="buildozer"
fi

echo

cmd="$buildozer 'set lock \"{lock_label}\"' $wksp_file:{repo_name}"

if [[ "$autofix" == "--autofix" ]]; then
eval "$cmd"
else
cat <<EOF
Run the following command to add the 'lock' attribute
to the "{repo_name}" repo in $wksp_file:

$cmd

or run the following command to do it automatically:

bazel run @{repo_name}//:lock -- --autofix
EOF
fi
Loading