76 lines
3.1 KiB
C++
76 lines
3.1 KiB
C++
|
|
// SPDX-License-Identifier: LGPL-3.0-only
|
||
|
|
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||
|
|
|
||
|
|
#include <stdlib.h>
|
||
|
|
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;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
BuildResult BuildIt(Configuration& cfg) {
|
||
|
|
std::unordered_map<fs::path, std::shared_future<BuildResult>> depResults;
|
||
|
|
std::mutex depMutex;
|
||
|
|
return Build(cfg, depResults, depMutex);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Only crafter-build running in its Flatpak packages a Flatpak: the program
|
||
|
|
// links that sandbox's glibc, the runtime's, so the app runs on it. A native
|
||
|
|
// crafter-build links against the host's glibc and libc++, which no runtime
|
||
|
|
// matches, so asking it for a Flatpak fails saying so, and Default builds
|
||
|
|
// the executable as linked, as it always has. A .flatpak-info without a
|
||
|
|
// runtime is staged, which is what a native process has, inside the Flatpak
|
||
|
|
// as well as out of it.
|
||
|
|
int main() {
|
||
|
|
const fs::path scratch = fs::temp_directory_path() / "crafter-build-flatpak-native";
|
||
|
|
std::error_code ec;
|
||
|
|
fs::remove_all(scratch, ec);
|
||
|
|
fs::create_directories(scratch);
|
||
|
|
const fs::path info = scratch / "flatpak-info";
|
||
|
|
std::ofstream(info) << "";
|
||
|
|
setenv("CRAFTER_BUILD_FLATPAK_INFO", info.string().c_str(), 1);
|
||
|
|
Check(GetToolchain().flatpakBranch.empty(), "without a runtime in the staged file there is none");
|
||
|
|
|
||
|
|
const fs::path fixture = fs::current_path() / "tests" / "FlatpakNative" / "fixture";
|
||
|
|
fs::remove_all(fixture / "bin", ec);
|
||
|
|
fs::remove_all(fixture / "build", ec);
|
||
|
|
Configuration app;
|
||
|
|
app.path = fixture;
|
||
|
|
app.name = "native-app";
|
||
|
|
app.outputName = "native";
|
||
|
|
app.target = HostTarget();
|
||
|
|
{
|
||
|
|
std::array<fs::path, 0> ifaces = {};
|
||
|
|
std::array<fs::path, 1> impls = { "main" };
|
||
|
|
app.GetInterfacesAndImplementations(ifaces, impls);
|
||
|
|
}
|
||
|
|
|
||
|
|
app.packaging = Packaging::Flatpak;
|
||
|
|
const BuildResult refused = BuildIt(app);
|
||
|
|
Check(refused.result.contains("only the Flatpak crafter-build packages a Flatpak") && refused.result.contains("native-app"), std::format("a native build refuses to package a Flatpak, saying why: {}", refused.result));
|
||
|
|
|
||
|
|
app.packaging = Packaging::Default;
|
||
|
|
const BuildResult built = BuildIt(app);
|
||
|
|
Check(built.result.empty(), std::format("Default builds: {}", built.result));
|
||
|
|
Check(ExpectedOutput(app) == app.BinDir() / "native" && fs::is_regular_file(ExpectedOutput(app)), "the executable is linked into the bin dir, as before");
|
||
|
|
Check(!fs::exists(app.BinDir() / "files"), "and it is not packaged");
|
||
|
|
const auto run = RunCommandWithTimeout(ShellQuote(ExpectedOutput(app).string()), std::chrono::seconds(30));
|
||
|
|
Check(run.exitCode == 0 && run.output.contains("native"), std::format("it runs: {}", run.output));
|
||
|
|
|
||
|
|
fs::remove_all(scratch, ec);
|
||
|
|
if (Failures > 0) {
|
||
|
|
std::println(std::cerr, "{} assertions failed", Failures);
|
||
|
|
return 1;
|
||
|
|
}
|
||
|
|
return 0;
|
||
|
|
}
|