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
7 changes: 5 additions & 2 deletions docs/spec/xpkg.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# The xpkg format

> Status: in force. This is the specification of libxpkg 0.0.68.
> Status: in force. This is the specification of libxpkg 0.0.69.
> Source: libxpkg `docs/spec/xpkg.md` (https://github.com/openxlings/libxpkg),
> where it is edited. A copy, byte for byte the one of the libxpkg version
> xlings builds with, is xlings `docs/specs/xpkg.md`
Expand Down Expand Up @@ -410,7 +410,10 @@ its own:
signature, what holds after, the version that added it and what differs by
platform). A module that is not in the interface is an error, not a stub.
- `import("xim.pkgindex.<lib>")` runs the index's `libs/<lib>.lua` in an
environment of the same rules and gives what it returns.
environment of the same rules and gives what it returns. A recipe in no
index with libs of its own (one given by path, a host's local overlay)
reads the libs of the index the host names, when it is read and in its
hooks alike.
- The modules: `fs` (files), `system` (programs and the install's
directories), `pkginfo` (what is installed), `path` (path strings), `log`,
`xvm`, `subos`, `pkgmanager`, `elfpatch`, `json`, `base64`.
Expand Down
2 changes: 1 addition & 1 deletion mcpp.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
namespace = "mcpplibs"
name = "xpkg"
version = "0.0.68"
version = "0.0.69"
description = "C++23 reference implementation of the xpkg format"
license = "Apache-2.0"
repo = "https://github.com/openxlings/libxpkg"
Expand Down
2 changes: 1 addition & 1 deletion src/xpkg-loader-boundary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ build_index_impl(const fs::path& repo_dir, const std::string& defaultNamespace,

for (auto& packagePath : packagePaths) {
auto result = boundaries ? load_package(packagePath, context, boundaries->metadata)
: load_package(packagePath);
: load_package(packagePath, context);
if (!result) {
if (boundaries) return std::unexpected(packagePath.string() + ": " + result.error());
continue; // legacy callers skip malformed packages
Expand Down
31 changes: 26 additions & 5 deletions src/xpkg-loader.cppm
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ using BuildOutput = std::function<void(std::string_view)>;
struct LoaderContext {
std::string platform;
std::string arch;
// The index whose libs a recipe in no index with libs of its own imports
// at its top level (a recipe given by path, a host's local overlay): the
// host's answer, as the executor's ReadTarget::pkgindex_dir. A recipe's
// own index comes first.
std::string pkgindex_dir;
};

struct MetadataInvocation {
Expand Down Expand Up @@ -1137,7 +1142,8 @@ std::expected<fs::path, std::string> template_for(const fs::path& recipe) {
// -- path, json and base64 are the real ones -- and index libs read as they are
// imported. What fails there is a load error of the recipe: the same answer
// search, an index build and an install get.
std::optional<std::string> check_spec3_top_level(const fs::path& pkg_path, const fs::path& recipe_template) {
std::optional<std::string> check_spec3_top_level(const fs::path& pkg_path, const fs::path& recipe_template,
const fs::path& host_index) {
lua::State* L = lua::L_newstate();
if (!L) return "failed to create lua state";
lua::L_openlibs(L);
Expand Down Expand Up @@ -1185,12 +1191,18 @@ std::optional<std::string> check_spec3_top_level(const fs::path& pkg_path, const
if (!problem) run(detail::spec3_lua, "the spec 3 environment", 0);
if (!problem) {
// Index libs are read from the recipe's index (pkgs/<letter>/<file>),
// three levels up the absolute path (a relative one has no parent there).
// three levels up the absolute path (a relative one has no parent there);
// a recipe in no index with libs reads the host's (host_index), as the
// executor's top level does.
std::error_code ec;
const auto absolute = fs::absolute(pkg_path, ec).lexically_normal();
const auto index = (ec ? pkg_path : absolute).parent_path().parent_path().parent_path();
auto index = (ec ? pkg_path : absolute).parent_path().parent_path().parent_path();
ec.clear();
if (fs::is_directory(index / "libs", ec)) {
if (!fs::is_directory(index / "libs", ec)) {
index = host_index;
ec.clear();
}
if (!index.empty() && fs::is_directory(index / "libs", ec)) {
lua::pushstring(L, index.string().c_str());
lua::setglobal(L, "_PKGINDEX_DIR");
}
Expand Down Expand Up @@ -1363,7 +1375,7 @@ load_package(const fs::path& pkg_path, const LoaderContext& context) {
}
}
if (const auto spec = spec_number(p.spec); spec && *spec == 3) {
if (auto problem = loader_detail::check_spec3_top_level(pkg_path, *recipe_template))
if (auto problem = loader_detail::check_spec3_top_level(pkg_path, *recipe_template, context.pkgindex_dir))
return std::unexpected("lua error: " + *problem);
}
return p;
Expand Down Expand Up @@ -1410,6 +1422,15 @@ build_index(const fs::path& repo_dir, const std::string& defaultNamespace,
return loader_detail::build_index_impl(repo_dir, defaultNamespace, buildOutput, {}, nullptr);
}

// The same builder, each recipe read with the host's context: the platform
// and architecture its top level is told, and the index whose libs a recipe
// in an index without libs imports (a host's local overlay).
std::expected<PackageIndex, std::string>
build_index(const fs::path& repo_dir, const std::string& defaultNamespace,
const LoaderContext& context, const BuildOutput& buildOutput) {
return loader_detail::build_index_impl(repo_dir, defaultNamespace, buildOutput, context, nullptr);
}

// Both executable paths (metadata and pkgindex-build.lua) cross the supplied
// boundaries. Unlike the legacy builder, a boundary failure fails the build.
std::expected<PackageIndex, std::string>
Expand Down
56 changes: 56 additions & 0 deletions tests/test_spec3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,62 @@ function install() return probelib.word() == "lib" end
EXPECT_TRUE(dotted.success) << dotted.error;
}

// A recipe in no index with libs of its own (a recipe given by path, a host's
// local overlay) is read with the libs of the index the host names
// (LoaderContext::pkgindex_dir), as its executor's top level is; its own
// index comes first. Without one, a top-level import is a load error.
TEST(Spec3EnvTest, ALoadedRecipeOutsideAnIndexReadsTheHostsIndexLibs) {
Index host;
host.lib("probelib", "local M = {}\nfunction M.word() return 'host' end\nreturn M\n");
const auto body = std::string(R"LUA(package = { spec = "3", name = "loose", xpm = { linux = { ["1.0.0"] = {} } } }
import("xim.pkgindex.probelib")
local word = probelib.word()
function install() return true end
)LUA");
const auto loose = host.root / "loose.lua"; // two levels from any index
std::ofstream(loose, std::ios::binary) << body;

auto read = load_package(loose, LoaderContext{"linux", "x86_64", host.root.string()});
EXPECT_TRUE(read.has_value()) << read.error();

auto alone = load_package(loose, LoaderContext{"linux", "x86_64", ""});
ASSERT_FALSE(alone.has_value());
EXPECT_NE(alone.error().find("not in an index"), std::string::npos) << alone.error();

// Its own index's lib is the one read: here it raises when called.
Index own;
own.lib("probelib", "local M = {}\nfunction M.word() error('own lib read') end\nreturn M\n");
const auto inside = own.root / "pkgs" / "p" / "loose.lua";
std::ofstream(inside, std::ios::binary) << body;
auto first = load_package(inside, LoaderContext{"linux", "x86_64", host.root.string()});
ASSERT_FALSE(first.has_value());
EXPECT_NE(first.error().find("own lib read"), std::string::npos) << first.error();
}

// An index without libs of its own (a host's local overlay) built in process
// reads each recipe with the context it is given: a recipe that imports a lib
// of the host's index at its top level is in the index. The builder without a
// context still skips what it cannot read.
TEST(Spec3EnvTest, AnIndexWithoutLibsIsBuiltWithTheHostsIndexLibs) {
Index host;
host.lib("probelib", "local M = {}\nfunction M.word() return 'host' end\nreturn M\n");
const auto overlay = host.root / "overlay";
fs::create_directories(overlay / "pkgs" / "l");
std::ofstream(overlay / "pkgs" / "l" / "loose.lua", std::ios::binary)
<< R"LUA(package = { spec = "3", name = "loose", xpm = { linux = { ["1.0.0"] = {} } } }
import("xim.pkgindex.probelib")
local word = probelib.word()
function install() return true end
)LUA";
auto built = build_index(overlay, "local", LoaderContext{"linux", "x86_64", host.root.string()}, BuildOutput{});
ASSERT_TRUE(built.has_value()) << built.error();
EXPECT_EQ(built->entries.count("local:loose"), 1u);

auto blind = build_index(overlay, "local");
ASSERT_TRUE(blind.has_value()) << blind.error();
EXPECT_EQ(blind->entries.count("local:loose"), 0u);
}

TEST(Spec3EnvTest, TheSameTextWithSpec2HasTheCompatibilityLayer) {
Index index;
auto recipe = index.recipe(R"LUA(
Expand Down
Loading