88 lines
4.3 KiB
C++
88 lines
4.3 KiB
C++
|
|
// SPDX-License-Identifier: LGPL-3.0-only
|
||
|
|
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||
|
|
|
||
|
|
import std;
|
||
|
|
import Crafter.Build;
|
||
|
|
namespace fs = std::filesystem;
|
||
|
|
using namespace Crafter;
|
||
|
|
|
||
|
|
namespace {
|
||
|
|
std::int32_t Failures = 0;
|
||
|
|
|
||
|
|
void Check(bool cond, std::string_view msg) {
|
||
|
|
if (!cond) {
|
||
|
|
std::println(std::cerr, "FAIL: {}", msg);
|
||
|
|
++Failures;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Compiles C `source`, written to `dir`/`name`, with the toolchain's
|
||
|
|
// clang and `arguments`; empty when it compiled, else what clang said.
|
||
|
|
std::string Cc(const fs::path& dir, std::string_view name, std::string_view source, std::string_view arguments) {
|
||
|
|
std::ofstream(dir / name) << source;
|
||
|
|
const fs::path log = dir / std::format("{}.log", name);
|
||
|
|
const std::string command = std::format("{} {} {} > {} 2>&1", ShellQuote(GetToolchain().clang.string()), ShellQuote((dir / name).string()), arguments, ShellQuote(log.string()));
|
||
|
|
if (std::system(command.c_str()) == 0) return "";
|
||
|
|
std::ifstream in(log);
|
||
|
|
return std::string(std::istreambuf_iterator<char>(in), std::istreambuf_iterator<char>());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// A packaged program carries in files/lib the shared libraries it needs that
|
||
|
|
// the runtime does not have, worked out from its NEEDED entries and theirs.
|
||
|
|
// A library replaced in place can need another, as a toolchain update can
|
||
|
|
// bring, while the program stays as it was: the writer once reused what it
|
||
|
|
// had worked out for that program, so the new library's contents were
|
||
|
|
// copied and the library it needs was left out, and -r still ran the
|
||
|
|
// program, in crafter-build's sandbox, while the installed app did not
|
||
|
|
// start. It works the libraries out on every write now. A program built on
|
||
|
|
// libA.so is written, then libA.so is built again on libB.so, the program
|
||
|
|
// left as it was, and the next write has to carry libB.so as well. The
|
||
|
|
// writer is called directly, with the libraries' directory as the one it
|
||
|
|
// takes libraries from.
|
||
|
|
int main() {
|
||
|
|
const fs::path scratch = fs::temp_directory_path() / "crafter-build-flatpak-library-change";
|
||
|
|
std::error_code ec;
|
||
|
|
fs::remove_all(scratch, ec);
|
||
|
|
const fs::path libs = scratch / "libs";
|
||
|
|
const fs::path bin = scratch / "app" / "files" / "bin";
|
||
|
|
fs::create_directories(libs);
|
||
|
|
fs::create_directories(bin);
|
||
|
|
fs::create_directories(scratch / "build");
|
||
|
|
const std::string quoted = ShellQuote(libs.string());
|
||
|
|
|
||
|
|
std::string failed = Cc(scratch, "a1.c", "int a(void) { return 1; }\n", std::format("-shared -fPIC -o {}/libA.so", quoted));
|
||
|
|
Check(failed.empty(), std::format("libA.so builds: {}", failed));
|
||
|
|
failed = Cc(scratch, "main.c", "int a(void);\nint main(void) { return a(); }\n", std::format("-o {} -L{} -lA", ShellQuote((bin / "needs").string()), quoted));
|
||
|
|
Check(failed.empty(), std::format("the program builds on it: {}", failed));
|
||
|
|
if (Failures > 0) return 1;
|
||
|
|
|
||
|
|
const Toolchain& tc = GetToolchain();
|
||
|
|
FlatpakFiles d;
|
||
|
|
d.root = scratch / "app";
|
||
|
|
d.record = scratch / "build" / "flatpak-record";
|
||
|
|
d.appId = "net.example.LibraryChange";
|
||
|
|
d.command = "needs";
|
||
|
|
d.clangxx = tc.clangxx;
|
||
|
|
d.readelf = tc.llvmReadelf;
|
||
|
|
d.libraryDirs = {libs};
|
||
|
|
d.programEntries = {"needs"};
|
||
|
|
const fs::path lib = d.root / "files" / "lib";
|
||
|
|
|
||
|
|
const std::string first = WriteFlatpakFiles(d);
|
||
|
|
Check(first.empty() && fs::is_regular_file(lib / "libA.so") && !fs::exists(lib / "libB.so"), std::format("the program's library is carried, and nothing else of the directory: {}", first));
|
||
|
|
|
||
|
|
failed = Cc(scratch, "b.c", "int b(void) { return 2; }\n", std::format("-shared -fPIC -o {}/libB.so", quoted));
|
||
|
|
Check(failed.empty(), std::format("libB.so builds: {}", failed));
|
||
|
|
failed = Cc(scratch, "a2.c", "int b(void);\nint a(void) { return b(); }\n", std::format("-shared -fPIC -o {}/libA.so -L{} -lB", quoted, quoted));
|
||
|
|
Check(failed.empty(), std::format("libA.so is built again, on libB.so: {}", failed));
|
||
|
|
const std::string second = WriteFlatpakFiles(d);
|
||
|
|
Check(second.empty() && fs::is_regular_file(lib / "libA.so") && fs::is_regular_file(lib / "libB.so"), std::format("the next write carries the library the new libA.so needs: {}", second));
|
||
|
|
|
||
|
|
fs::remove_all(scratch, ec);
|
||
|
|
if (Failures > 0) {
|
||
|
|
std::println(std::cerr, "{} assertions failed", Failures);
|
||
|
|
return 1;
|
||
|
|
}
|
||
|
|
return 0;
|
||
|
|
}
|