Skip to content
Merged
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
1 change: 1 addition & 0 deletions REUSE.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ path = [
"examples/package/rpm/hello.source/rpm/packaging.json",
"tine.lock.json",
"git/fixture/**",
"project/fixture/**",
"tools/starlark-fmt.json",
]
SPDX-FileCopyrightText = "Amutable GmbH <https://amutable.com/>"
Expand Down
28 changes: 28 additions & 0 deletions docs/design/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,34 @@ tine exports the pinned binary's path as `BUCK2_BINARY`. A tool running under a
`tine` wrapper, but directly calls the exported binary (`util.buck2_argv()`), inherits the current
mounts, and runs against the shared configuration the invoking command already wrote.

### Project source trees

A directory source cannot include a subdirectory that has a `BUCK` file of its own:

Directory `` of package `tine//project/fixture` may not cover any subpackages, but includes subpackage `sub`.

`project.source_tree()` collects the files of a project the way `prelude/utils/source_listing_impl.bzl`
does. `project.source_listing()` declares a `source_tree_listing` target in every `BUCK` file. The target
lists the files of its package and depends on the `source_tree_listing` target of each direct subpackage.
A build of the tree gets the project path of its package, with the listed files as hidden inputs, and
reads the directory in place. A copy would get new timestamps whenever an edit makes Buck copy the tree
again, and a dev build would then recompile everything after every edit.

The list leaves out a few files that the build still sees:

- `glob()` matches a name that starts with a dot only with a pattern component such as `.*`. The listing
has patterns for up to four such names in a path relative to the `BUCK` file, and leaves out a file with
more.
- A cell that tine adds below the project root has its own packages, and the listing does not include
them.
- The listing of the root package leaves out `.buckconfig.local`. tine writes the mounts and the address of
the cache into it, and as an input it would give a build different cache keys on different machines.

A build of a tree at the project root sees an empty `buck-out`. The sandbox puts the upper and work
directories of the overlay of the tree in the scratch directory of the action, which is in `buck-out`.
overlayfs fails a lookup of them through the overlay with `ELOOP`. `source_overlay()` in `rootfs.py`
therefore mounts a tmpfs over `buck-out` in the overlay.

### Component model

The package model separates identity and policy from the exact inputs used by an action:
Expand Down
69 changes: 69 additions & 0 deletions docs/user/rpm.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,75 @@ rpm.package(
`package` is the `Name:` of the spec, and `subpackages` lists the binary packages that the build produces.
Each subpackage is a sub-target, such as `:hello[hello]`.

## Building a project from its own checkout

An upstream project, such as systemd, can build RPMs of itself from the checkout that contains its `BUCK`
files. If your project has no `BUCK` file in a subdirectory, pass the directory of the root `BUCK` file as
`source_tree`:

```starlark
load("@tine//package_system/rpm:defs.bzl", "rpm")

rpm.package(
name = "systemd.rpm",
package = "systemd",
spec = "systemd.spec",
source_tree = ".",
buildroot = "catalog//:fedora.rawhide.buildroot",
release = "1",
source_date_epoch = 1744207869,
subpackages = [],
)
```

Buck rejects `.` as soon as a subdirectory contains a `BUCK` file, because a directory source cannot include
another Buck package. In that case, declare the tree with `project.source_tree()` in the root `BUCK` file and
pass its label instead:

```starlark
load("@tine//package_system/rpm:defs.bzl", "rpm")
load("@tine//project:defs.bzl", "project")

project.source_tree(name = "source")
Comment thread
daandemeyer marked this conversation as resolved.
Resolved

rpm.package(
name = "systemd.rpm",
package = "systemd",
spec = "systemd.spec",
source_tree = ":source",
buildroot = "catalog//:fedora.rawhide.buildroot",
release = "1",
source_date_epoch = 1744207869,
subpackages = [],
)
```

`project.source_tree()` lists the files of its own directory. It cannot list the files below another `BUCK`
file, so every other `BUCK` file in the project lists its own files with `project.source_listing()`:

```starlark
load("@tine//project:defs.bzl", "project")

project.source_listing()
Comment thread
daandemeyer marked this conversation as resolved.
Resolved
```

If a `BUCK` file lacks the call, the build fails with an ``Unknown target `source_tree_listing` `` error that
names the directory of that `BUCK` file.

The build reads the tree as it is on disk, like a local checkout; see [Building from a local checkout
instead](git.md#building-from-a-local-checkout-instead) for which files are inputs. `tine buck build
--show-output :source` prints the path of a file that lists the inputs. [Project source
trees](../design/architecture.md#project-source-trees) describes the few files that the list leaves out.

A dev build keeps its build directory between builds, and an edit recompiles only what it changes. Pass
`dev = True` to `rpm.package()` for a dev build, or list the directory of the `BUCK` file that declares the
tree under `[tine] dev`, with `.` for the project root:

```toml
[buckconfig.tine]
dev = "."
```

## Build dependencies

The build installs the BuildRequires of the spec over the shared base buildroot. `build_requires` lists
Expand Down
2 changes: 2 additions & 0 deletions git/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

load("@prelude//:rules.bzl", "git_fetch")
load("//project:defs.bzl", "project")
load("//project:source.bzl", "SourceTreeInfo")

_MOUNT_TARGET_LABEL = "tine:mount-target"

Expand All @@ -21,6 +22,7 @@ def _checkout_impl(ctx: AnalysisContext) -> list[Provider]:
default_output = tree,
sub_targets = {path: [DefaultInfo(default_output = tree.project(path))] for path in ctx.attrs.sub_targets},
),
SourceTreeInfo(dev = False, path = tree, sources = None),
]

_checkout = rule(
Expand Down
23 changes: 15 additions & 8 deletions package/build.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ load("//:specs.bzl", "specs")
load("//box:defs.bzl", "box")
load("//git:defs.bzl", "git")
load("//project:defs.bzl", "project")
load("//project:source.bzl", "SourceTreeInfo")
load(":buildroot.bzl", "BuildrootInfo")
load(":manager.bzl", "PackageManagerInfo")
load(":repository.bzl", "LocalPackageInfo")
Expand All @@ -29,8 +30,10 @@ PACKAGE_BUILD_ATTRS = {
default = [],
doc = "our packages that the solves of the BuildRequires prefer over upstream ones",
),
"configured_dev": attrs.bool(
doc = "whether project configuration selects this package for dev mode",
"configured_dev": attrs.option(
attrs.bool(),
default = None,
doc = "whether `dev` or `[tine] dev` selects dev mode for this package, or None when neither does",
),
"in_place_packaging": attrs.option(
attrs.string(),
Expand All @@ -48,7 +51,8 @@ PACKAGE_BUILD_ATTRS = {
doc = "the SOURCE_DATE_EPOCH of the build, or None to take it from the package's changelog where the recipe has one",
),
"source_tree": attrs.option(
attrs.source(allow_directory = True),
# A label names a `project.source_tree()` or a checkout, and a directory of the package coerces as a source.
attrs.one_of(attrs.dep(providers = [SourceTreeInfo]), attrs.source(allow_directory = True)),
default = None,
doc = "a prepared tree built in place of the declared sources",
),
Expand Down Expand Up @@ -147,12 +151,13 @@ def package_build_source(
in_place_recipe_value: str | None,
in_place_packaging: str | None,
in_place_packaging_distro: str | Select | None,
) -> (str | None, bool):
) -> (str | None, bool | None):
"""Check the in-place arguments of a package-build macro, and return its source tree and dev mode.

`in_place_recipe` names the macro's argument for a recipe in the source tree, and
`in_place_recipe_value` is its value. Without `source_tree`, the macro builds the checkout slot
`<name>.source` once the slot contains files. `owner` starts a failure message.
The dev mode is None when neither `dev` nor `[tine] dev` selects it. `in_place_recipe` names the macro's
argument for a recipe in the source tree, and `in_place_recipe_value` is its value. Without `source_tree`,
the macro builds the checkout slot `<name>.source` once the slot contains files. `owner` starts a failure
message.
"""
if in_place_packaging != None and in_place_recipe_value != None:
fail("{}: {} sets both in_place_packaging and {}".format(owner, name, in_place_recipe))
Expand All @@ -162,4 +167,6 @@ def package_build_source(
source = name + ".source"
populated = git.checkout(name = source)
source_tree = ":" + source if populated else None
return source_tree, project.is_dev(name, source = source_tree, override = dev)
if dev == None and project.is_dev(name, source = source_tree):
dev = True
return source_tree, dev
32 changes: 26 additions & 6 deletions package_system/rpm/build_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def specification(
*,
build_dir: Path | None = None,
spec_file: Path | None = None,
in_place_spec: str | None = None,
subpackages: list[str] | None = None,
sources: list[Path] | None = None,
strict_subpackages: bool = True,
Expand All @@ -65,6 +66,7 @@ def specification(
build_dir=str(build_dir) if build_dir is not None else None,
lower=["buildroot"],
spec_file=str(spec_file or self.spec_file),
in_place_spec=in_place_spec,
sources=[str(source) for source in sources] if sources is not None else [str(self.auxiliary)],
dist=".test",
# rpm_package keeps the build directory of every dev build, and only of a dev build.
Expand Down Expand Up @@ -175,7 +177,7 @@ def test_in_place_spec_and_extras_come_from_the_source_tree(self) -> None:
local_spec.write_text("Name: from-checkout\nSource1: helper\n")
(packaging / "helper").write_text("checkout helper\n")

specification = self.specification(source, spec_file=local_spec)
specification = self.specification(source, in_place_spec="packaging/fedora/local.spec")

def inspect(topdir: Path) -> None:
frozen = (topdir / "SPECS" / "local.spec").read_text()
Expand All @@ -201,6 +203,24 @@ def inspect(topdir: Path) -> None:
self.assertFalse((topdir / "SOURCES" / "auxiliary").exists())
self.assertEqual(local_spec.read_text(), "Name: from-checkout\nSource1: helper\n")

def test_a_declared_spec_in_the_source_tree_is_not_in_place(self) -> None:
# A source tree can be the project root, which also contains the declared spec. Without
# `in_place_spec`, the build must stage the declared spec and sources as for an archive build.
# Pass a source tree that contains the spec and check that rpmbuild takes the sources from
# SOURCES.
source = self.scratch / "checkout"
source.mkdir()
(source / "declared.spec").write_text("Name: declared\n")

command, cwd, _environment, topdir, _binds = self.invoke(
self.specification(source, spec_file=source / "declared.spec")
)

self.assertEqual(command[-1], "/build/SPECS/declared.spec")
self.assertFalse([argument for argument in command if argument.startswith("_sourcedir")])
self.assertEqual(cwd, Path("/build/CHECKOUT"))
self.assertEqual(sorted(path.name for path in (topdir / "SOURCES").iterdir()), ["auxiliary"])

def pinned_packaging(self) -> tuple[Path, Path]:
"""Return a checkout and a dist-git tree with a spec, a patch and a directory of tests."""
source = self.scratch / "checkout"
Expand Down Expand Up @@ -264,7 +284,7 @@ def test_imported_packaging_fails_on_a_subpackage_mismatch(self) -> None:
def test_spec_path_cannot_escape_the_source_tree(self) -> None:
source = self.scratch / "checkout"
source.mkdir()
spec = self.specification(source, spec_file=source / ".." / self.spec_file.name)
spec = self.specification(source, in_place_spec="../" + self.spec_file.name)
with (
contextlib.chdir(self.project),
mock.patch.object(tempfile, "tempdir", str(self.scratch)),
Expand All @@ -276,9 +296,9 @@ def test_spec_path_cannot_escape_the_source_tree(self) -> None:
def test_missing_spec_has_a_useful_error(self) -> None:
source = self.scratch / "checkout"
source.mkdir()
for spec_file in (source / "missing.spec", self.scratch / "missing.spec"):
with self.subTest(spec_file=spec_file):
spec = self.specification(source, spec_file=spec_file)
for in_place_spec, spec_file in (("missing.spec", None), (None, self.scratch / "missing.spec")):
with self.subTest(in_place_spec=in_place_spec, spec_file=spec_file):
spec = self.specification(source, spec_file=spec_file, in_place_spec=in_place_spec)
case = tempfile.mkdtemp(dir=self.scratch)
with (
contextlib.chdir(self.project),
Expand Down Expand Up @@ -425,7 +445,7 @@ def test_spec_symlink_cannot_escape_the_staged_checkout(self) -> None:
source = self.scratch / "checkout"
source.mkdir()
(source / "escape.spec").symlink_to(self.spec_file)
spec = self.specification(source, spec_file=source / "escape.spec")
spec = self.specification(source, in_place_spec="escape.spec")
topdir = self.scratch / "topdir"
with (
contextlib.chdir(self.project),
Expand Down
12 changes: 7 additions & 5 deletions package_system/rpm/buildrequires_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ def specification(
self,
*,
spec_file: Path | None = None,
in_place_spec: str | None = None,
sources: list[Path] | None = None,
source_tree: Path | None = None,
rpmbuild_options: list[str] | None = None,
Expand All @@ -111,6 +112,7 @@ def specification(
return buildrequires.Spec(
lower=["buildroot"],
spec_file=str(spec_file or self.spec_file),
in_place_spec=in_place_spec,
sources=[str(source) for source in (sources if sources is not None else [self.extra])],
source_tree=str(source_tree) if source_tree is not None else None,
dist=".test",
Expand Down Expand Up @@ -168,7 +170,7 @@ def test_in_place_queries_define_the_debug_prefix_map(self) -> None:
)
)
found = buildrequires.query(
self.specification(spec_file=checkout / "example.spec", sources=[], source_tree=checkout)
self.specification(in_place_spec="example.spec", sources=[], source_tree=checkout)
)
self.assertIn("flags-devel", found)

Expand Down Expand Up @@ -207,7 +209,7 @@ def test_only_an_in_place_build_defines_build_in_place(self) -> None:
self.assertIn(
"in-place-devel",
buildrequires.query(
self.specification(spec_file=checkout / "example.spec", sources=[], source_tree=checkout)
self.specification(in_place_spec="example.spec", sources=[], source_tree=checkout)
),
)

Expand Down Expand Up @@ -239,7 +241,7 @@ def test_an_in_place_spec_parses_in_the_checkout_like_the_build(self) -> None:
(checkout / "rpm" / "example.spec").write_text(spec)

found = buildrequires.query(
self.specification(spec_file=checkout / "rpm" / "example.spec", sources=[], source_tree=checkout)
self.specification(in_place_spec="rpm/example.spec", sources=[], source_tree=checkout)
)

self.assertIn("include-devel", found)
Expand All @@ -258,7 +260,7 @@ def test_parse_time_writes_do_not_modify_the_checkout(self) -> None:
(checkout / "example.spec").write_text(spec)

found = buildrequires.query(
self.specification(spec_file=checkout / "example.spec", sources=[], source_tree=checkout)
self.specification(in_place_spec="example.spec", sources=[], source_tree=checkout)
)
self.assertIn("generated-devel", found)

Expand Down Expand Up @@ -325,7 +327,7 @@ def test_an_in_place_spec_needs_no_source_archive(self) -> None:
)
)
found = buildrequires.query(
self.specification(spec_file=checkout / "example.spec", sources=[], source_tree=checkout)
self.specification(in_place_spec="example.spec", sources=[], source_tree=checkout)
)
self.assertIn("example-compiler", found)

Expand Down
12 changes: 8 additions & 4 deletions package_system/rpm/rpmbuild.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ class Parse(TypedDict):
"""The fields of a driver spec that select how rpm parses the RPM spec."""

spec_file: str
# `in_place_spec` is the path of the spec relative to the source tree, or None for a spec that the build
# doesn't read from the source tree.
in_place_spec: str | None
# `sources` lists the Source and Patch files, and directories whose top-level files are Source and Patch
# files.
sources: list[str]
Expand Down Expand Up @@ -85,12 +88,13 @@ def stage(topdir: Path, parse: Parse, source_tree: str | None) -> Staged:
# readonly_project() already created BUILD if it mounted the persistent build directory there.
(topdir / builder.BUILD).mkdir(exist_ok=True)
checkout = topdir / builder.CHECKOUT
spec_file = Path(parse["spec_file"])
in_place_spec = parse["in_place_spec"] if source_tree is not None else None
spec_file = Path(in_place_spec if in_place_spec is not None else parse["spec_file"])
staged_spec = topdir / "SPECS" / spec_file.name
chroot_spec = builder.CHROOT_TOPDIR / "SPECS" / spec_file.name
if source_tree is not None and spec_file.is_relative_to(source_tree):
relative_spec = spec_file.relative_to(source_tree)
if ".." in relative_spec.parts:
if in_place_spec is not None:
relative_spec = spec_file
if relative_spec.is_absolute() or ".." in relative_spec.parts:
util.fail(f"rpmbuild: RPM spec must stay within the source tree: {relative_spec}")
source_spec = checkout / relative_spec
# stage() reads the spec outside the chroot. Without the check, a spec symlink that points out of
Expand Down
1 change: 1 addition & 0 deletions package_system/rpm/rpmbuild_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def setUp(self) -> None:
def stage(self, *, spec_file: Path | None = None, sources: list[Path] | None = None) -> rpmbuild.Staged:
parse = rpmbuild.Parse(
spec_file=str(spec_file or self.spec_file),
in_place_spec=None,
sources=[str(source) for source in (sources if sources is not None else [self.source])],
dist=".test",
source_date_epoch=1754006400,
Expand Down
Loading
Loading