feat: lay an executable out as the files/ of a Flatpak
A Linux executable built by the Flatpak crafter-build links the libc++,
libc++abi and libunwind in its /app/lib and the Sdk's glibc, so it runs
inside crafter-build's sandbox and practically nowhere else: a host
needs glibc 2.38 or newer and all three LLVM libraries, which not even
Arch has. A program built here is meant to run in a Flatpak, so it can
now be laid out as one: with cfg.packaging = Packaging::Flatpak, or
--packaging=flatpak where the project.cpp crafter-build was pointed at
calls ApplyStandardArgs, its BinDir() holds files/, what becomes /app in
the app's sandbox, and -r runs the program there against the libraries
the app carries.
The program is linked into files/bin, where its shaders, files and
assets land too (ProgramDir()), with $ORIGIN/../lib as its one run path,
and the new :Flatpak partition writes the rest of files/: files/lib with
the LLVM runtime libraries, located one by one through clang, and
whatever else the program needs, read with llvm-readelf, that the
directories of the shared libraries it is built on hold, the ones an
unpackaged link records as run paths, or the toolchain's own lib dir,
/app/lib in crafter-build's Flatpak, which the link searches too; and
the desktop entry, metainfo and icons under the app-id in files/share,
the entry refused when its Icon= is not the app-id or
desktop-file-validate, where there is one, rejects it, and an icon that
is not an .svg or a square .png. The metadata and export/ of a build
directory are flatpak build-finish's to write, and crafter-build keeps
no copy of its rules: what a permission means and what the host sees of
an app change with flatpak's releases, and a copy would have to follow
every one. The writer copies a file only when its content differs,
compared a block at a time so that a large file costs no more memory
than a small one, and writes one only when what it would write differs,
never deciding by an mtime, which ostree sets to 0 for every file of
/app, and works the program's libraries out on every write, which reads
only their dynamic sections, since a library replaced in place, as by a
toolchain update, can need others, which FlatpakLibraryChange checks; it
prunes from files/bin each top-level entry an earlier build put there
and this one does not, reporting those the program wrote there itself.
What cannot be a Flatpak is refused before anything is built, saying
why: a Windows or wasm executable, a build against a sysroot or for
musl, another architecture than the runtime's, a build against
CRAFTER_BUILD_LIBCXX_DIR, whose libc++ the Flatpak would not carry, and
a build by a native crafter-build, which links the host's glibc: a
native process knows no Flatpak runtime, from /.flatpak-info, unless
CRAFTER_BUILD_FLATPAK_INFO, the tests' hook, names a file with one.
Default stays the executable as linked for now, RunTests runs every test
as linked, whatever its Configuration asks for, and RunLint works out
its compile command so, and a forwarded --packaging= leaves a
dependency's executables alone, since consumers run them by path. A
dependency that packages its own executable gets its desktop file,
metainfo and icons rebased by the loader as its cfg.files are, so they
are its own whatever directory the consumer builds from. Only a Flatpak
is keyed into VariantId, so every other configuration keeps its
directory, but for one given --packaging=, which was a project arg,
hashed into VariantId, and is a standard arg now, a dependency's
included. The app-id and what a Flatpak is installed as are not keyed:
they change files/share, never what is compiled. The asset name map,
kept three times, twice in Build() and once for the browser runtime's
files.json, is one function now, CompressedAssetName; CompressAsset, in
Crafter.Build-Asset.cpp, keeps the list of the extensions it loads,
which has to agree with it. Each copy a Flatpak build makes into the
program's directory, of its shaders, assets, cfg.files and what its
dependencies forward, records the top-level name it places there, the
names of what a directory holds for one copied into it itself ("data/",
"data/."), and those are what the writer keeps in files/bin; a build
that fails after copying adds them to the writer's record all the same,
so the next write removes what no build places any more.
SharedLibraryRuns now also packages its program as a Flatpak, whose
files/lib has to carry libgreet.so for it to start.
2026-11-01 19:20:55 +01:00
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
module Crafter . Build : Flatpak_impl ;
import : Flatpak ;
import : Platform ;
import : Progress ;
import std ;
namespace fs = std : : filesystem ;
using namespace Crafter ;
namespace {
// flatpak's own rule for the characters of a name's segments: a dash is
// allowed in the last segment only, a digit anywhere but first.
bool NameStart ( char c , bool lastSegment ) {
return ( c > = ' a ' & & c < = ' z ' ) | | ( c > = ' A ' & & c < = ' Z ' ) | | c = = ' _ ' | | ( lastSegment & & c = = ' - ' ) ;
}
bool NameChar ( char c , bool lastSegment ) {
return NameStart ( c , lastSegment ) | | ( c > = ' 0 ' & & c < = ' 9 ' ) ;
}
std : : string_view Trim ( std : : string_view s ) {
while ( ! s . empty ( ) & & ( s . front ( ) = = ' ' | | s . front ( ) = = ' \t ' | | s . front ( ) = = ' \n ' | | s . front ( ) = = ' \r ' ) ) s . remove_prefix ( 1 ) ;
while ( ! s . empty ( ) & & ( s . back ( ) = = ' ' | | s . back ( ) = = ' \t ' | | s . back ( ) = = ' \n ' | | s . back ( ) = = ' \r ' ) ) s . remove_suffix ( 1 ) ;
return s ;
}
std : : vector < std : : string_view > Split ( std : : string_view s , char separator ) {
std : : vector < std : : string_view > parts ;
for ( std : : size_t begin = 0 ; ; ) {
const std : : size_t end = s . find ( separator , begin ) ;
parts . push_back ( s . substr ( begin , end = = std : : string_view : : npos ? std : : string_view : : npos : end - begin ) ) ;
if ( end = = std : : string_view : : npos ) return parts ;
begin = end + 1 ;
}
}
// ---- the files ----
std : : string ReadAll ( const fs : : path & path ) {
std : : ifstream in ( path , std : : ios : : binary ) ;
if ( ! in ) throw std : : runtime_error ( std : : format ( " could not read {} " , path . string ( ) ) ) ;
return std : : string ( std : : istreambuf_iterator < char > ( in ) , std : : istreambuf_iterator < char > ( ) ) ;
}
// Whether `b` is a regular file holding exactly what `a` does: by size,
// then a block at a time, so comparing costs the same memory whatever
// the files' size. Reading both whole held twice the largest file on
// every build that had nothing to copy, and failed where that did not
// fit.
bool SameContent ( const fs : : path & a , const fs : : path & b ) {
std : : error_code ec ;
if ( ! fs : : is_regular_file ( b , ec ) | | fs : : is_symlink ( b , ec ) ) return false ;
if ( fs : : file_size ( a ) ! = fs : : file_size ( b , ec ) | | ec ) return false ;
std : : ifstream left ( a , std : : ios : : binary ) ;
if ( ! left ) throw std : : runtime_error ( std : : format ( " could not read {} " , a . string ( ) ) ) ;
std : : ifstream right ( b , std : : ios : : binary ) ;
if ( ! right ) return false ;
constexpr std : : size_t Block = 64 * 1024 ;
std : : vector < char > x ( Block ) ;
std : : vector < char > y ( Block ) ;
while ( left & & right ) {
left . read ( x . data ( ) , static_cast < std : : streamsize > ( Block ) ) ;
right . read ( y . data ( ) , static_cast < std : : streamsize > ( Block ) ) ;
if ( left . gcount ( ) ! = right . gcount ( ) | | ! std : : equal ( x . begin ( ) , x . begin ( ) + left . gcount ( ) , y . begin ( ) ) ) return false ;
}
return left . eof ( ) & & right . eof ( ) ;
}
// Writes `content` to `path` unless the file already holds exactly that,
// so an unchanged build leaves its mtime alone.
void WriteIfDifferent ( const fs : : path & path , std : : string_view content ) {
std : : error_code ec ;
if ( fs : : is_regular_file ( path , ec ) & & ! fs : : is_symlink ( path , ec ) & & fs : : file_size ( path , ec ) = = content . size ( ) & & ReadAll ( path ) = = content ) return ;
fs : : create_directories ( path . parent_path ( ) ) ;
fs : : remove ( path , ec ) ;
std : : ofstream out ( path , std : : ios : : binary | std : : ios : : trunc ) ;
out . write ( content . data ( ) , static_cast < std : : streamsize > ( content . size ( ) ) ) ;
if ( ! out ) throw std : : runtime_error ( std : : format ( " could not write {} " , path . string ( ) ) ) ;
}
// Makes `dir` hold exactly `wanted`, relative path to the file copied
// there, and nothing else: every other file goes, and the directories
// left empty. A copy is made only when its content differs, and anything
// in the way is replaced rather than written through, a link included.
// Returns the relative paths it copied.
std : : set < fs : : path > SyncOwned ( const fs : : path & dir , const std : : map < fs : : path , fs : : path > & wanted ) {
std : : set < fs : : path > copied ;
std : : error_code ec ;
if ( fs : : exists ( dir , ec ) ) {
std : : vector < fs : : path > files ;
std : : vector < fs : : path > dirs ;
for ( const fs : : directory_entry & entry : fs : : recursive_directory_iterator ( dir ) ) {
if ( entry . is_directory ( ) & & ! entry . is_symlink ( ) ) dirs . push_back ( entry . path ( ) ) ;
else if ( ! wanted . contains ( entry . path ( ) . lexically_relative ( dir ) ) ) files . push_back ( entry . path ( ) ) ;
}
for ( const fs : : path & file : files ) fs : : remove ( file ) ;
// Deepest first, so a parent is empty by the time it is looked at.
std : : ranges : : sort ( dirs , std : : greater { } ) ;
for ( const fs : : path & d : dirs ) {
if ( fs : : is_empty ( d ) ) fs : : remove ( d ) ;
}
}
for ( const auto & [ relative , source ] : wanted ) {
const fs : : path target = dir / relative ;
if ( ! fs : : exists ( source ) ) throw std : : runtime_error ( std : : format ( " {} does not exist " , source . string ( ) ) ) ;
if ( SameContent ( source , target ) ) continue ;
fs : : create_directories ( target . parent_path ( ) ) ;
if ( fs : : is_directory ( target , ec ) & & ! fs : : is_symlink ( target , ec ) ) fs : : remove_all ( target ) ;
else fs : : remove ( target , ec ) ;
fs : : copy_file ( source , target ) ;
copied . insert ( relative ) ;
}
return copied ;
}
feat: install a Flatpak's data under /app/share
A program packaged as a Flatpak could only have its files beside it in
files/bin, where cfg.files goes. One that finds its data at <its
directory>/../share/<name>, as Crafter.Review finds its process/
directory, had no way to get it there: files/share belongs to the
writer, which removes anything it did not put there, and a module of
flatpak-builder's would have placed it only in the installed app, never
under -r. cfg.flatpak.share lists files and directories of the project
and the path under /app/share each goes to. The writer copies them into
files/share with the desktop entry, metainfo and icons, keeps them while
they are listed and removes them once they are not, as it does those. -r
runs the program from files/bin, so the same ../share lookup finds them
there. What is exported of them is flatpak build-finish's to decide, by
its own rules, as for any app's files: the app's D-Bus services, MIME
types, search providers and KRunner plugins, which have no field of
their own, are placed like any other file. A library's are not passed on
to the programs built on it, since data a library needs at run time
already goes through cfg.files. A dependency that packages its own
executable has the sources rebased by the loader, as its cfg.files are.
Share entries change files/share and not what is compiled, so they are
not keyed into VariantId, which its test checks.
Every file bound for files/share, the desktop entry, metainfo and icons
included, now goes through one placement check, which refuses two files
for one path, a field's file among them, and a file where another needs
a directory, naming both. A target that is absolute or leaves share/ is
refused too. Any other place is open to a share entry, a second desktop
entry, an add-on's metainfo and any icon included, as for any app's
files: flatpak build-export refuses an exported icon that is not a
square image and a D-Bus service that is not a key file, warns of the
app's own desktop entry where desktop-file-validate rejects it or its
Icon= is not the app's, and exports a second desktop entry, a MIME
package or metainfo as it is. The app's own desktop entry,
applications/<app-id>.desktop, is held to cfg.flatpak.desktopFile's
checks whichever placed it: its Icon= and desktop-file-validate are
keyed to that place now, not to the field. A source directory that holds
what the build writes is refused as well, since it would be copied into
itself, growing each time the app is built or exported: files/, and the
app's directory, where install and bundle put another copy of files/ and
the exported app, compared as written and where links lead, so that the
project's own directory is refused where its bin/ is a link elsewhere
too. So is one that lies in them, the build's own output, which the next
build, install or bundle writes again. The loader leaves an empty source
empty, as it does an empty desktop file.
FlatpakShareLarge holds the writer to comparing an entry with its copy
at constant memory, which the program's data, gigabytes of it in a game,
needs on every build: it rewrites a files/ holding a 32 MiB entry, with
nothing changed, in a child process whose address space may grow by 16
MiB, with a stand-in llvm-readelf, since the real one, which a write
starts for the libraries, would not fit under that limit, and a change
in the entry's last byte, in the short block a comparison reads last, is
still copied.
2026-11-02 20:04:14 +01:00
// Whether `path` is `ancestor` or lies inside it, element by element.
bool Within ( const fs : : path & path , const fs : : path & ancestor ) {
return std : : mismatch ( ancestor . begin ( ) , ancestor . end ( ) , path . begin ( ) , path . end ( ) ) . first = = ancestor . end ( ) ;
}
// Whether `outer` is `inner` or holds it, as written or where their
// links lead: a project whose bin/ is a link elsewhere holds its files/
// as written, and not once the link is followed.
bool Holds ( const fs : : path & outer , const fs : : path & inner ) {
std : : error_code ec ;
if ( Within ( fs : : absolute ( inner , ec ) . lexically_normal ( ) , fs : : absolute ( outer , ec ) . lexically_normal ( ) ) ) return true ;
const fs : : path realOuter = fs : : weakly_canonical ( outer , ec ) ;
if ( ec | | realOuter . empty ( ) ) return false ;
const fs : : path realInner = fs : : weakly_canonical ( inner , ec ) ;
if ( ec | | realInner . empty ( ) ) return false ;
return Within ( realInner , realOuter ) ;
}
// Adds `relative` to the files a directory is to hold, copied from
// `source`, unless that would put two files at one path, or a file where
// another needs a directory: one share entry over another, or two icons
// of one size. SyncOwned would otherwise copy whichever came last, or
// fail halfway with a filesystem error.
std : : string Place ( std : : map < fs : : path , fs : : path > & files , const fs : : path & relative , const fs : : path & source ) {
if ( auto it = files . find ( relative ) ; it ! = files . end ( ) ) {
return std : : format ( " {} and {} both go to share/{} " , it - > second . string ( ) , source . string ( ) , relative . string ( ) ) ;
}
// What lies inside a path sorts right after it, element by element.
if ( auto it = files . upper_bound ( relative ) ; it ! = files . end ( ) & & Within ( it - > first , relative ) ) {
return std : : format ( " {} would make share/{} a file, where {} needs it to be a directory " , source . string ( ) , relative . string ( ) , it - > second . string ( ) ) ;
}
for ( fs : : path parent = relative . parent_path ( ) ; ! parent . empty ( ) ; parent = parent . parent_path ( ) ) {
if ( auto it = files . find ( parent ) ; it ! = files . end ( ) ) {
return std : : format ( " {} needs share/{} to be a directory, where {} makes it a file " , source . string ( ) , parent . string ( ) , it - > second . string ( ) ) ;
}
}
files . emplace ( relative , source ) ;
return " " ;
}
feat: lay an executable out as the files/ of a Flatpak
A Linux executable built by the Flatpak crafter-build links the libc++,
libc++abi and libunwind in its /app/lib and the Sdk's glibc, so it runs
inside crafter-build's sandbox and practically nowhere else: a host
needs glibc 2.38 or newer and all three LLVM libraries, which not even
Arch has. A program built here is meant to run in a Flatpak, so it can
now be laid out as one: with cfg.packaging = Packaging::Flatpak, or
--packaging=flatpak where the project.cpp crafter-build was pointed at
calls ApplyStandardArgs, its BinDir() holds files/, what becomes /app in
the app's sandbox, and -r runs the program there against the libraries
the app carries.
The program is linked into files/bin, where its shaders, files and
assets land too (ProgramDir()), with $ORIGIN/../lib as its one run path,
and the new :Flatpak partition writes the rest of files/: files/lib with
the LLVM runtime libraries, located one by one through clang, and
whatever else the program needs, read with llvm-readelf, that the
directories of the shared libraries it is built on hold, the ones an
unpackaged link records as run paths, or the toolchain's own lib dir,
/app/lib in crafter-build's Flatpak, which the link searches too; and
the desktop entry, metainfo and icons under the app-id in files/share,
the entry refused when its Icon= is not the app-id or
desktop-file-validate, where there is one, rejects it, and an icon that
is not an .svg or a square .png. The metadata and export/ of a build
directory are flatpak build-finish's to write, and crafter-build keeps
no copy of its rules: what a permission means and what the host sees of
an app change with flatpak's releases, and a copy would have to follow
every one. The writer copies a file only when its content differs,
compared a block at a time so that a large file costs no more memory
than a small one, and writes one only when what it would write differs,
never deciding by an mtime, which ostree sets to 0 for every file of
/app, and works the program's libraries out on every write, which reads
only their dynamic sections, since a library replaced in place, as by a
toolchain update, can need others, which FlatpakLibraryChange checks; it
prunes from files/bin each top-level entry an earlier build put there
and this one does not, reporting those the program wrote there itself.
What cannot be a Flatpak is refused before anything is built, saying
why: a Windows or wasm executable, a build against a sysroot or for
musl, another architecture than the runtime's, a build against
CRAFTER_BUILD_LIBCXX_DIR, whose libc++ the Flatpak would not carry, and
a build by a native crafter-build, which links the host's glibc: a
native process knows no Flatpak runtime, from /.flatpak-info, unless
CRAFTER_BUILD_FLATPAK_INFO, the tests' hook, names a file with one.
Default stays the executable as linked for now, RunTests runs every test
as linked, whatever its Configuration asks for, and RunLint works out
its compile command so, and a forwarded --packaging= leaves a
dependency's executables alone, since consumers run them by path. A
dependency that packages its own executable gets its desktop file,
metainfo and icons rebased by the loader as its cfg.files are, so they
are its own whatever directory the consumer builds from. Only a Flatpak
is keyed into VariantId, so every other configuration keeps its
directory, but for one given --packaging=, which was a project arg,
hashed into VariantId, and is a standard arg now, a dependency's
included. The app-id and what a Flatpak is installed as are not keyed:
they change files/share, never what is compiled. The asset name map,
kept three times, twice in Build() and once for the browser runtime's
files.json, is one function now, CompressedAssetName; CompressAsset, in
Crafter.Build-Asset.cpp, keeps the list of the extensions it loads,
which has to agree with it. Each copy a Flatpak build makes into the
program's directory, of its shaders, assets, cfg.files and what its
dependencies forward, records the top-level name it places there, the
names of what a directory holds for one copied into it itself ("data/",
"data/."), and those are what the writer keeps in files/bin; a build
that fails after copying adds them to the writer's record all the same,
so the next write removes what no build places any more.
SharedLibraryRuns now also packages its program as a Flatpak, whose
files/lib has to carry libgreet.so for it to start.
2026-11-01 19:20:55 +01:00
// Where an icon goes under share/: an .svg as scalable, a .png in the
// directory of its own size, read from its header.
fs : : path IconPath ( const fs : : path & icon , std : : string_view appId ) {
std : : string ext = icon . extension ( ) . string ( ) ;
for ( char & c : ext ) c = static_cast < char > ( std : : tolower ( static_cast < std : : uint8_t > ( c ) ) ) ;
if ( ext = = " .svg " ) return fs : : path ( " icons " ) / " hicolor " / " scalable " / " apps " / std : : format ( " {}.svg " , appId ) ;
if ( ext ! = " .png " ) throw std : : runtime_error ( std : : format ( " {}: an icon has to be an .svg or a .png " , icon . string ( ) ) ) ;
const std : : string header = ReadAll ( icon ) . substr ( 0 , 24 ) ;
if ( header . size ( ) < 24 | | ! header . starts_with ( " \x89 PNG \r \n \x1a \n " ) | | header . substr ( 12 , 4 ) ! = " IHDR " ) {
throw std : : runtime_error ( std : : format ( " {}: not a PNG file " , icon . string ( ) ) ) ;
}
const auto bigEndian = [ & ] ( std : : size_t at ) {
std : : uint32_t value = 0 ;
for ( std : : size_t i = 0 ; i < 4 ; + + i ) value = ( value < < 8 ) | static_cast < std : : uint8_t > ( header [ at + i ] ) ;
return value ;
} ;
const std : : uint32_t width = bigEndian ( 16 ) ;
const std : : uint32_t height = bigEndian ( 20 ) ;
if ( width ! = height ) throw std : : runtime_error ( std : : format ( " {}: an icon has to be square, this one is {}x{} " , icon . string ( ) , width , height ) ) ;
return fs : : path ( " icons " ) / " hicolor " / std : : format ( " {}x{} " , width , width ) / " apps " / std : : format ( " {}.png " , appId ) ;
}
// The Icon= of a desktop entry's [Desktop Entry] group; empty without one.
std : : string DesktopIcon ( const fs : : path & desktopFile ) {
bool inEntry = false ;
for ( const std : : string_view line : Split ( ReadAll ( desktopFile ) , ' \n ' ) ) {
const std : : string_view t = Trim ( line ) ;
if ( t . starts_with ( " [ " ) ) {
inEntry = t = = " [Desktop Entry] " ;
continue ;
}
if ( ! inEntry ) continue ;
// The key and the value either side of the first '=', without
// the space the Desktop Entry spec lets surround it: `Icon =
// other` is Icon=other to desktop-file-validate and to flatpak.
const std : : size_t eq = t . find ( ' = ' ) ;
if ( eq ! = std : : string_view : : npos & & Trim ( t . substr ( 0 , eq ) ) = = " Icon " ) return std : : string ( Trim ( t . substr ( eq + 1 ) ) ) ;
}
return " " ;
}
// ---- the libraries ----
// The libraries clang links into every C++ program it builds against its
// libc++. They are looked up one by one, never by searching the
// directory they are in: on a distribution that keeps libc++ in
// /usr/lib64, that directory holds libc.so.6 as well.
constexpr std : : array < std : : string_view , 3 > LlvmRuntimeLibraries = { " libc++.so.1 " , " libc++abi.so.1 " , " libunwind.so.1 " } ;
// The toolchain's own copy of `name`, as the link finds it; empty when
// clang has none (Arch's LLVM ships no libunwind.so.1). Asked once per
// name and process.
fs : : path ToolchainLibrary ( const fs : : path & clangxx , std : : string_view name ) {
static std : : mutex Mutex ;
static std : : map < std : : string , fs : : path , std : : less < > > Found ;
std : : lock_guard lock ( Mutex ) ;
if ( auto it = Found . find ( name ) ; it ! = Found . end ( ) ) return it - > second ;
const CommandResult r = RunCommandChecked ( std : : format ( " {} -print-file-name={} " , ShellQuote ( clangxx . string ( ) ) , name ) ) ;
// clang prints the bare name back when it has no such file.
fs : : path path = Trim ( r . output ) ;
std : : error_code ec ;
if ( r . exitCode ! = 0 | | ! path . is_absolute ( ) | | ! fs : : exists ( path , ec ) ) path . clear ( ) ;
else path = path . lexically_normal ( ) ;
Found . emplace ( std : : string ( name ) , path ) ;
return path ;
}
std : : vector < std : : string > NeededLibraries ( const fs : : path & readelf , const fs : : path & file ) {
const CommandResult r = RunCommandChecked ( std : : format ( " {} --needed-libs {} " , ShellQuote ( readelf . string ( ) ) , ShellQuote ( file . string ( ) ) ) ) ;
if ( r . exitCode ! = 0 ) {
throw std : : runtime_error ( std : : format ( " {} could not read {}: {} " , readelf . filename ( ) . string ( ) , file . string ( ) , Trim ( r . output ) ) ) ;
}
std : : vector < std : : string > names ;
bool inList = false ;
for ( const std : : string_view line : Split ( r . output , ' \n ' ) ) {
const std : : string_view t = Trim ( line ) ;
if ( ! inList ) {
inList = t . starts_with ( " NeededLibraries [ " ) ;
continue ;
}
if ( t = = " ] " ) break ;
if ( ! t . empty ( ) ) names . emplace_back ( t ) ;
}
return names ;
}
// The files `program` needs that are not the runtime's, with what they
// need in turn: the LLVM runtime libraries, and any NEEDED name found in
// one of `dirs`, the first one holding it winning as for the loader.
std : : vector < fs : : path > Libraries ( const FlatpakFiles & d , const fs : : path & program ) {
std : : vector < fs : : path > libraries ;
std : : set < std : : string > seen ;
std : : deque < fs : : path > pending = { program } ;
while ( ! pending . empty ( ) ) {
const fs : : path file = std : : move ( pending . front ( ) ) ;
pending . pop_front ( ) ;
for ( const std : : string & name : NeededLibraries ( d . readelf , file ) ) {
if ( ! seen . insert ( name ) . second ) continue ;
fs : : path found ;
if ( std : : ranges : : contains ( LlvmRuntimeLibraries , std : : string_view ( name ) ) ) found = ToolchainLibrary ( d . clangxx , name ) ;
std : : error_code ec ;
for ( const fs : : path & dir : d . libraryDirs ) {
if ( ! found . empty ( ) ) break ;
if ( fs : : exists ( dir / name , ec ) ) found = dir / name ;
}
if ( found . empty ( ) ) continue ;
libraries . push_back ( found ) ;
pending . push_back ( found ) ;
}
}
return libraries ;
}
// ---- the record ----
// What a write placed, kept so the next one knows which files in
// files/bin were the build's.
struct Record {
std : : set < std : : string > placed ;
} ;
Record ReadRecord ( const fs : : path & file ) {
Record record ;
std : : error_code ec ;
if ( ! fs : : exists ( file , ec ) ) return record ;
for ( const std : : string_view line : Split ( ReadAll ( file ) , ' \n ' ) ) {
const std : : size_t tab = line . find ( ' \t ' ) ;
if ( tab = = std : : string_view : : npos ) continue ;
const std : : string_view kind = line . substr ( 0 , tab ) ;
const std : : string value ( line . substr ( tab + 1 ) ) ;
if ( kind = = " placed " ) record . placed . insert ( value ) ;
}
return record ;
}
void WriteRecord ( const fs : : path & file , const Record & record ) {
std : : string text ;
for ( const std : : string & name : record . placed ) text + = std : : format ( " placed \t {} \n " , name ) ;
WriteIfDifferent ( file , text ) ;
}
}
std : : string Crafter : : DefaultFlatpakAppId ( std : : string_view outputName ) {
std : : string segment ;
for ( const char c : outputName ) segment . push_back ( NameChar ( c , true ) ? c : ' _ ' ) ;
if ( segment . empty ( ) | | ! NameStart ( segment . front ( ) , true ) ) segment . insert ( segment . begin ( ) , ' _ ' ) ;
return std : : format ( " local.crafterbuild.{} " , segment ) ;
}
std : : string Crafter : : FlatpakAppIdError ( std : : string_view appId ) {
if ( appId . empty ( ) ) return " it is empty " ;
if ( appId . size ( ) > 255 ) return " it is longer than 255 characters " ;
const std : : vector < std : : string_view > segments = Split ( appId , ' . ' ) ;
if ( segments . size ( ) < 3 ) return " it needs at least three segments, like org.example.App " ;
for ( std : : size_t i = 0 ; i < segments . size ( ) ; + + i ) {
const std : : string_view segment = segments [ i ] ;
const bool last = i + 1 = = segments . size ( ) ;
if ( segment . empty ( ) ) return " it has an empty segment " ;
for ( std : : size_t j = 0 ; j < segment . size ( ) ; + + j ) {
const char c = segment [ j ] ;
if ( j = = 0 ? NameStart ( c , last ) : NameChar ( c , last ) ) continue ;
if ( c = = ' - ' ) return std : : format ( " its segment '{}' has a '-', which only the last segment may have " , segment ) ;
if ( j = = 0 & & c > = ' 0 ' & & c < = ' 9 ' ) return std : : format ( " its segment '{}' starts with a digit " , segment ) ;
return std : : format ( " it holds '{}', which an application ID cannot " , c ) ;
}
}
return " " ;
}
void Crafter : : RecordFlatpakPlaced ( const fs : : path & record , const std : : set < std : : string > & names ) {
try {
Record merged = ReadRecord ( record ) ;
const std : : size_t before = merged . placed . size ( ) ;
merged . placed . insert ( names . begin ( ) , names . end ( ) ) ;
if ( merged . placed . size ( ) = = before ) return ;
std : : error_code ec ;
fs : : create_directories ( record . parent_path ( ) , ec ) ;
WriteRecord ( record , merged ) ;
} catch ( const std : : exception & ) {
// Not written: the next write takes those names for the program's,
// and says so, which is all a record that cannot be written costs.
}
}
std : : string Crafter : : WriteFlatpakFiles ( const FlatpakFiles & d ) {
if ( const std : : string bad = FlatpakAppIdError ( d . appId ) ; ! bad . empty ( ) ) {
return std : : format ( " {} is not a Flatpak application ID: {} " , d . appId , bad ) ;
}
try {
const fs : : path files = d . root / " files " ;
const fs : : path program = files / " bin " / d . command ;
const Record old = ReadRecord ( d . record ) ;
Record now ;
// Worked out on every write, from the program and what each library
// it carries needs in turn, which reads only their dynamic sections:
// a library replaced in place, as by a toolchain update, can need
// others, and only reading it again finds them. -r would not show a
// library missing, as crafter-build's sandbox has its own /app/lib on
// the loader's path; the installed app would not start.
std : : map < fs : : path , fs : : path > libraries ;
for ( const fs : : path & library : Libraries ( d , program ) ) libraries . emplace ( library . filename ( ) , library ) ;
SyncOwned ( files / " lib " , libraries ) ;
std : : map < fs : : path , fs : : path > share ;
const fs : : path desktopEntry = fs : : path ( " applications " ) / std : : format ( " {}.desktop " , d . appId ) ;
if ( ! d . desktopFile . empty ( ) ) {
if ( ! fs : : exists ( d . desktopFile ) ) return std : : format ( " {}: the desktop file {} does not exist " , d . appId , d . desktopFile . string ( ) ) ;
feat: install a Flatpak's data under /app/share
A program packaged as a Flatpak could only have its files beside it in
files/bin, where cfg.files goes. One that finds its data at <its
directory>/../share/<name>, as Crafter.Review finds its process/
directory, had no way to get it there: files/share belongs to the
writer, which removes anything it did not put there, and a module of
flatpak-builder's would have placed it only in the installed app, never
under -r. cfg.flatpak.share lists files and directories of the project
and the path under /app/share each goes to. The writer copies them into
files/share with the desktop entry, metainfo and icons, keeps them while
they are listed and removes them once they are not, as it does those. -r
runs the program from files/bin, so the same ../share lookup finds them
there. What is exported of them is flatpak build-finish's to decide, by
its own rules, as for any app's files: the app's D-Bus services, MIME
types, search providers and KRunner plugins, which have no field of
their own, are placed like any other file. A library's are not passed on
to the programs built on it, since data a library needs at run time
already goes through cfg.files. A dependency that packages its own
executable has the sources rebased by the loader, as its cfg.files are.
Share entries change files/share and not what is compiled, so they are
not keyed into VariantId, which its test checks.
Every file bound for files/share, the desktop entry, metainfo and icons
included, now goes through one placement check, which refuses two files
for one path, a field's file among them, and a file where another needs
a directory, naming both. A target that is absolute or leaves share/ is
refused too. Any other place is open to a share entry, a second desktop
entry, an add-on's metainfo and any icon included, as for any app's
files: flatpak build-export refuses an exported icon that is not a
square image and a D-Bus service that is not a key file, warns of the
app's own desktop entry where desktop-file-validate rejects it or its
Icon= is not the app's, and exports a second desktop entry, a MIME
package or metainfo as it is. The app's own desktop entry,
applications/<app-id>.desktop, is held to cfg.flatpak.desktopFile's
checks whichever placed it: its Icon= and desktop-file-validate are
keyed to that place now, not to the field. A source directory that holds
what the build writes is refused as well, since it would be copied into
itself, growing each time the app is built or exported: files/, and the
app's directory, where install and bundle put another copy of files/ and
the exported app, compared as written and where links lead, so that the
project's own directory is refused where its bin/ is a link elsewhere
too. So is one that lies in them, the build's own output, which the next
build, install or bundle writes again. The loader leaves an empty source
empty, as it does an empty desktop file.
FlatpakShareLarge holds the writer to comparing an entry with its copy
at constant memory, which the program's data, gigabytes of it in a game,
needs on every build: it rewrites a files/ holding a 32 MiB entry, with
nothing changed, in a child process whose address space may grow by 16
MiB, with a stand-in llvm-readelf, since the real one, which a write
starts for the libraries, would not fit under that limit, and a change
in the entry's last byte, in the short block a comparison reads last, is
still copied.
2026-11-02 20:04:14 +01:00
if ( const std : : string clash = Place ( share , desktopEntry , d . desktopFile ) ; ! clash . empty ( ) ) return std : : format ( " {}: {} " , d . appId , clash ) ;
feat: lay an executable out as the files/ of a Flatpak
A Linux executable built by the Flatpak crafter-build links the libc++,
libc++abi and libunwind in its /app/lib and the Sdk's glibc, so it runs
inside crafter-build's sandbox and practically nowhere else: a host
needs glibc 2.38 or newer and all three LLVM libraries, which not even
Arch has. A program built here is meant to run in a Flatpak, so it can
now be laid out as one: with cfg.packaging = Packaging::Flatpak, or
--packaging=flatpak where the project.cpp crafter-build was pointed at
calls ApplyStandardArgs, its BinDir() holds files/, what becomes /app in
the app's sandbox, and -r runs the program there against the libraries
the app carries.
The program is linked into files/bin, where its shaders, files and
assets land too (ProgramDir()), with $ORIGIN/../lib as its one run path,
and the new :Flatpak partition writes the rest of files/: files/lib with
the LLVM runtime libraries, located one by one through clang, and
whatever else the program needs, read with llvm-readelf, that the
directories of the shared libraries it is built on hold, the ones an
unpackaged link records as run paths, or the toolchain's own lib dir,
/app/lib in crafter-build's Flatpak, which the link searches too; and
the desktop entry, metainfo and icons under the app-id in files/share,
the entry refused when its Icon= is not the app-id or
desktop-file-validate, where there is one, rejects it, and an icon that
is not an .svg or a square .png. The metadata and export/ of a build
directory are flatpak build-finish's to write, and crafter-build keeps
no copy of its rules: what a permission means and what the host sees of
an app change with flatpak's releases, and a copy would have to follow
every one. The writer copies a file only when its content differs,
compared a block at a time so that a large file costs no more memory
than a small one, and writes one only when what it would write differs,
never deciding by an mtime, which ostree sets to 0 for every file of
/app, and works the program's libraries out on every write, which reads
only their dynamic sections, since a library replaced in place, as by a
toolchain update, can need others, which FlatpakLibraryChange checks; it
prunes from files/bin each top-level entry an earlier build put there
and this one does not, reporting those the program wrote there itself.
What cannot be a Flatpak is refused before anything is built, saying
why: a Windows or wasm executable, a build against a sysroot or for
musl, another architecture than the runtime's, a build against
CRAFTER_BUILD_LIBCXX_DIR, whose libc++ the Flatpak would not carry, and
a build by a native crafter-build, which links the host's glibc: a
native process knows no Flatpak runtime, from /.flatpak-info, unless
CRAFTER_BUILD_FLATPAK_INFO, the tests' hook, names a file with one.
Default stays the executable as linked for now, RunTests runs every test
as linked, whatever its Configuration asks for, and RunLint works out
its compile command so, and a forwarded --packaging= leaves a
dependency's executables alone, since consumers run them by path. A
dependency that packages its own executable gets its desktop file,
metainfo and icons rebased by the loader as its cfg.files are, so they
are its own whatever directory the consumer builds from. Only a Flatpak
is keyed into VariantId, so every other configuration keeps its
directory, but for one given --packaging=, which was a project arg,
hashed into VariantId, and is a standard arg now, a dependency's
included. The app-id and what a Flatpak is installed as are not keyed:
they change files/share, never what is compiled. The asset name map,
kept three times, twice in Build() and once for the browser runtime's
files.json, is one function now, CompressedAssetName; CompressAsset, in
Crafter.Build-Asset.cpp, keeps the list of the extensions it loads,
which has to agree with it. Each copy a Flatpak build makes into the
program's directory, of its shaders, assets, cfg.files and what its
dependencies forward, records the top-level name it places there, the
names of what a directory holds for one copied into it itself ("data/",
"data/."), and those are what the writer keeps in files/bin; a build
that fails after copying adds them to the writer's record all the same,
so the next write removes what no build places any more.
SharedLibraryRuns now also packages its program as a Flatpak, whose
files/lib has to carry libgreet.so for it to start.
2026-11-01 19:20:55 +01:00
}
if ( ! d . metainfo . empty ( ) ) {
const fs : : path metainfo = fs : : path ( " metainfo " ) / std : : format ( " {}.metainfo.xml " , d . appId ) ;
feat: install a Flatpak's data under /app/share
A program packaged as a Flatpak could only have its files beside it in
files/bin, where cfg.files goes. One that finds its data at <its
directory>/../share/<name>, as Crafter.Review finds its process/
directory, had no way to get it there: files/share belongs to the
writer, which removes anything it did not put there, and a module of
flatpak-builder's would have placed it only in the installed app, never
under -r. cfg.flatpak.share lists files and directories of the project
and the path under /app/share each goes to. The writer copies them into
files/share with the desktop entry, metainfo and icons, keeps them while
they are listed and removes them once they are not, as it does those. -r
runs the program from files/bin, so the same ../share lookup finds them
there. What is exported of them is flatpak build-finish's to decide, by
its own rules, as for any app's files: the app's D-Bus services, MIME
types, search providers and KRunner plugins, which have no field of
their own, are placed like any other file. A library's are not passed on
to the programs built on it, since data a library needs at run time
already goes through cfg.files. A dependency that packages its own
executable has the sources rebased by the loader, as its cfg.files are.
Share entries change files/share and not what is compiled, so they are
not keyed into VariantId, which its test checks.
Every file bound for files/share, the desktop entry, metainfo and icons
included, now goes through one placement check, which refuses two files
for one path, a field's file among them, and a file where another needs
a directory, naming both. A target that is absolute or leaves share/ is
refused too. Any other place is open to a share entry, a second desktop
entry, an add-on's metainfo and any icon included, as for any app's
files: flatpak build-export refuses an exported icon that is not a
square image and a D-Bus service that is not a key file, warns of the
app's own desktop entry where desktop-file-validate rejects it or its
Icon= is not the app's, and exports a second desktop entry, a MIME
package or metainfo as it is. The app's own desktop entry,
applications/<app-id>.desktop, is held to cfg.flatpak.desktopFile's
checks whichever placed it: its Icon= and desktop-file-validate are
keyed to that place now, not to the field. A source directory that holds
what the build writes is refused as well, since it would be copied into
itself, growing each time the app is built or exported: files/, and the
app's directory, where install and bundle put another copy of files/ and
the exported app, compared as written and where links lead, so that the
project's own directory is refused where its bin/ is a link elsewhere
too. So is one that lies in them, the build's own output, which the next
build, install or bundle writes again. The loader leaves an empty source
empty, as it does an empty desktop file.
FlatpakShareLarge holds the writer to comparing an entry with its copy
at constant memory, which the program's data, gigabytes of it in a game,
needs on every build: it rewrites a files/ holding a 32 MiB entry, with
nothing changed, in a child process whose address space may grow by 16
MiB, with a stand-in llvm-readelf, since the real one, which a write
starts for the libraries, would not fit under that limit, and a change
in the entry's last byte, in the short block a comparison reads last, is
still copied.
2026-11-02 20:04:14 +01:00
if ( const std : : string clash = Place ( share , metainfo , d . metainfo ) ; ! clash . empty ( ) ) return std : : format ( " {}: {} " , d . appId , clash ) ;
feat: lay an executable out as the files/ of a Flatpak
A Linux executable built by the Flatpak crafter-build links the libc++,
libc++abi and libunwind in its /app/lib and the Sdk's glibc, so it runs
inside crafter-build's sandbox and practically nowhere else: a host
needs glibc 2.38 or newer and all three LLVM libraries, which not even
Arch has. A program built here is meant to run in a Flatpak, so it can
now be laid out as one: with cfg.packaging = Packaging::Flatpak, or
--packaging=flatpak where the project.cpp crafter-build was pointed at
calls ApplyStandardArgs, its BinDir() holds files/, what becomes /app in
the app's sandbox, and -r runs the program there against the libraries
the app carries.
The program is linked into files/bin, where its shaders, files and
assets land too (ProgramDir()), with $ORIGIN/../lib as its one run path,
and the new :Flatpak partition writes the rest of files/: files/lib with
the LLVM runtime libraries, located one by one through clang, and
whatever else the program needs, read with llvm-readelf, that the
directories of the shared libraries it is built on hold, the ones an
unpackaged link records as run paths, or the toolchain's own lib dir,
/app/lib in crafter-build's Flatpak, which the link searches too; and
the desktop entry, metainfo and icons under the app-id in files/share,
the entry refused when its Icon= is not the app-id or
desktop-file-validate, where there is one, rejects it, and an icon that
is not an .svg or a square .png. The metadata and export/ of a build
directory are flatpak build-finish's to write, and crafter-build keeps
no copy of its rules: what a permission means and what the host sees of
an app change with flatpak's releases, and a copy would have to follow
every one. The writer copies a file only when its content differs,
compared a block at a time so that a large file costs no more memory
than a small one, and writes one only when what it would write differs,
never deciding by an mtime, which ostree sets to 0 for every file of
/app, and works the program's libraries out on every write, which reads
only their dynamic sections, since a library replaced in place, as by a
toolchain update, can need others, which FlatpakLibraryChange checks; it
prunes from files/bin each top-level entry an earlier build put there
and this one does not, reporting those the program wrote there itself.
What cannot be a Flatpak is refused before anything is built, saying
why: a Windows or wasm executable, a build against a sysroot or for
musl, another architecture than the runtime's, a build against
CRAFTER_BUILD_LIBCXX_DIR, whose libc++ the Flatpak would not carry, and
a build by a native crafter-build, which links the host's glibc: a
native process knows no Flatpak runtime, from /.flatpak-info, unless
CRAFTER_BUILD_FLATPAK_INFO, the tests' hook, names a file with one.
Default stays the executable as linked for now, RunTests runs every test
as linked, whatever its Configuration asks for, and RunLint works out
its compile command so, and a forwarded --packaging= leaves a
dependency's executables alone, since consumers run them by path. A
dependency that packages its own executable gets its desktop file,
metainfo and icons rebased by the loader as its cfg.files are, so they
are its own whatever directory the consumer builds from. Only a Flatpak
is keyed into VariantId, so every other configuration keeps its
directory, but for one given --packaging=, which was a project arg,
hashed into VariantId, and is a standard arg now, a dependency's
included. The app-id and what a Flatpak is installed as are not keyed:
they change files/share, never what is compiled. The asset name map,
kept three times, twice in Build() and once for the browser runtime's
files.json, is one function now, CompressedAssetName; CompressAsset, in
Crafter.Build-Asset.cpp, keeps the list of the extensions it loads,
which has to agree with it. Each copy a Flatpak build makes into the
program's directory, of its shaders, assets, cfg.files and what its
dependencies forward, records the top-level name it places there, the
names of what a directory holds for one copied into it itself ("data/",
"data/."), and those are what the writer keeps in files/bin; a build
that fails after copying adds them to the writer's record all the same,
so the next write removes what no build places any more.
SharedLibraryRuns now also packages its program as a Flatpak, whose
files/lib has to carry libgreet.so for it to start.
2026-11-01 19:20:55 +01:00
}
for ( const fs : : path & icon : d . icons ) {
const fs : : path relative = IconPath ( icon , d . appId ) ;
feat: install a Flatpak's data under /app/share
A program packaged as a Flatpak could only have its files beside it in
files/bin, where cfg.files goes. One that finds its data at <its
directory>/../share/<name>, as Crafter.Review finds its process/
directory, had no way to get it there: files/share belongs to the
writer, which removes anything it did not put there, and a module of
flatpak-builder's would have placed it only in the installed app, never
under -r. cfg.flatpak.share lists files and directories of the project
and the path under /app/share each goes to. The writer copies them into
files/share with the desktop entry, metainfo and icons, keeps them while
they are listed and removes them once they are not, as it does those. -r
runs the program from files/bin, so the same ../share lookup finds them
there. What is exported of them is flatpak build-finish's to decide, by
its own rules, as for any app's files: the app's D-Bus services, MIME
types, search providers and KRunner plugins, which have no field of
their own, are placed like any other file. A library's are not passed on
to the programs built on it, since data a library needs at run time
already goes through cfg.files. A dependency that packages its own
executable has the sources rebased by the loader, as its cfg.files are.
Share entries change files/share and not what is compiled, so they are
not keyed into VariantId, which its test checks.
Every file bound for files/share, the desktop entry, metainfo and icons
included, now goes through one placement check, which refuses two files
for one path, a field's file among them, and a file where another needs
a directory, naming both. A target that is absolute or leaves share/ is
refused too. Any other place is open to a share entry, a second desktop
entry, an add-on's metainfo and any icon included, as for any app's
files: flatpak build-export refuses an exported icon that is not a
square image and a D-Bus service that is not a key file, warns of the
app's own desktop entry where desktop-file-validate rejects it or its
Icon= is not the app's, and exports a second desktop entry, a MIME
package or metainfo as it is. The app's own desktop entry,
applications/<app-id>.desktop, is held to cfg.flatpak.desktopFile's
checks whichever placed it: its Icon= and desktop-file-validate are
keyed to that place now, not to the field. A source directory that holds
what the build writes is refused as well, since it would be copied into
itself, growing each time the app is built or exported: files/, and the
app's directory, where install and bundle put another copy of files/ and
the exported app, compared as written and where links lead, so that the
project's own directory is refused where its bin/ is a link elsewhere
too. So is one that lies in them, the build's own output, which the next
build, install or bundle writes again. The loader leaves an empty source
empty, as it does an empty desktop file.
FlatpakShareLarge holds the writer to comparing an entry with its copy
at constant memory, which the program's data, gigabytes of it in a game,
needs on every build: it rewrites a files/ holding a 32 MiB entry, with
nothing changed, in a child process whose address space may grow by 16
MiB, with a stand-in llvm-readelf, since the real one, which a write
starts for the libraries, would not fit under that limit, and a change
in the entry's last byte, in the short block a comparison reads last, is
still copied.
2026-11-02 20:04:14 +01:00
if ( const std : : string clash = Place ( share , relative , icon ) ; ! clash . empty ( ) ) return std : : format ( " {}: {} " , d . appId , clash ) ;
}
// A share entry's file goes where it says, unless the place is
// another file's (Place), a field's included. Whatever it is, a second
// desktop entry or the metainfo of an add-on, say, flatpak checks
// some of what it exports when install or bundle export the app, as
// for any app: an icon that is not a square image, or a D-Bus
// service that is not a key file, stops the export, the app's own
// desktop entry gets a warning where desktop-file-validate rejects it
// or its Icon= is not the app's, and a second desktop entry, a MIME
// package or metainfo goes as it is. The app's own desktop entry and
// the icons the field places are checked here too, at build time.
for ( const FlatpakShareEntry & entry : d . share ) {
fs : : path target = entry . target . lexically_normal ( ) ;
if ( ! target . empty ( ) & & ! target . has_filename ( ) ) target = target . parent_path ( ) ;
if ( entry . target . is_absolute ( ) | | target . empty ( ) | | target = = " . " | | * target . begin ( ) = = " .. " ) {
return std : : format ( " {}: the share target '{}' of {} has to be a path inside /app/share, relative to it " , d . appId , entry . target . string ( ) , entry . source . string ( ) ) ;
}
std : : error_code ec ;
if ( fs : : is_directory ( entry . source , ec ) ) {
// A directory that holds what the build writes would be
// copied into itself, growing each time the app is built or
// exported: files/, where share/'s copies land, and the
// app's directory, where install and bundle put another copy
// of files/ and the exported app; the project's own directory
// holds both. One that lies in them is the build's own
// output, which the next build, install or bundle writes
// again, and no file of the project's.
for ( const fs : : path & output : { files , d . appDir } ) {
if ( output . empty ( ) ) continue ;
if ( Holds ( entry . source , output ) ) {
return std : : format ( " {}: {} holds {}, which the build writes, so a share entry would copy it into itself, growing each time the app is built or exported: name the files or directories in it instead " , d . appId , entry . source . string ( ) , output . string ( ) ) ;
}
if ( Holds ( output , entry . source ) ) {
return std : : format ( " {}: {} lies in {}, which the build writes and writes again: a share entry names a file of the project's " , d . appId , entry . source . string ( ) , output . string ( ) ) ;
}
}
// Written with a trailing separator, "data/", the directory is
// taken without it, for each file's path inside it.
const fs : : path dir = entry . source . has_filename ( ) ? entry . source : entry . source . parent_path ( ) ;
for ( const fs : : directory_entry & file : fs : : recursive_directory_iterator ( dir ) ) {
if ( ! file . is_regular_file ( ) ) continue ;
if ( const std : : string clash = Place ( share , target / file . path ( ) . lexically_relative ( dir ) , file . path ( ) ) ; ! clash . empty ( ) ) return std : : format ( " {}: {} " , d . appId , clash ) ;
}
} else if ( fs : : is_regular_file ( entry . source , ec ) ) {
if ( const std : : string clash = Place ( share , target , entry . source ) ; ! clash . empty ( ) ) return std : : format ( " {}: {} " , d . appId , clash ) ;
} else {
return std : : format ( " {}: {} does not exist, so it cannot go to share/{} " , d . appId , entry . source . string ( ) , target . string ( ) ) ;
}
}
// The app's desktop entry, applications/<app-id>.desktop, whether
// cfg.flatpak.desktopFile or a share entry placed it: its Icon= has
// to be the app-id, which the app's icons are installed under.
const auto entry = share . find ( desktopEntry ) ;
if ( entry ! = share . end ( ) ) {
if ( const std : : string icon = DesktopIcon ( entry - > second ) ; ! icon . empty ( ) & & icon ! = d . appId ) {
return std : : format ( " {}: its Icon= is {}, and has to be the app-id, {}, which its icons are installed as " , entry - > second . string ( ) , icon , d . appId ) ;
}
feat: lay an executable out as the files/ of a Flatpak
A Linux executable built by the Flatpak crafter-build links the libc++,
libc++abi and libunwind in its /app/lib and the Sdk's glibc, so it runs
inside crafter-build's sandbox and practically nowhere else: a host
needs glibc 2.38 or newer and all three LLVM libraries, which not even
Arch has. A program built here is meant to run in a Flatpak, so it can
now be laid out as one: with cfg.packaging = Packaging::Flatpak, or
--packaging=flatpak where the project.cpp crafter-build was pointed at
calls ApplyStandardArgs, its BinDir() holds files/, what becomes /app in
the app's sandbox, and -r runs the program there against the libraries
the app carries.
The program is linked into files/bin, where its shaders, files and
assets land too (ProgramDir()), with $ORIGIN/../lib as its one run path,
and the new :Flatpak partition writes the rest of files/: files/lib with
the LLVM runtime libraries, located one by one through clang, and
whatever else the program needs, read with llvm-readelf, that the
directories of the shared libraries it is built on hold, the ones an
unpackaged link records as run paths, or the toolchain's own lib dir,
/app/lib in crafter-build's Flatpak, which the link searches too; and
the desktop entry, metainfo and icons under the app-id in files/share,
the entry refused when its Icon= is not the app-id or
desktop-file-validate, where there is one, rejects it, and an icon that
is not an .svg or a square .png. The metadata and export/ of a build
directory are flatpak build-finish's to write, and crafter-build keeps
no copy of its rules: what a permission means and what the host sees of
an app change with flatpak's releases, and a copy would have to follow
every one. The writer copies a file only when its content differs,
compared a block at a time so that a large file costs no more memory
than a small one, and writes one only when what it would write differs,
never deciding by an mtime, which ostree sets to 0 for every file of
/app, and works the program's libraries out on every write, which reads
only their dynamic sections, since a library replaced in place, as by a
toolchain update, can need others, which FlatpakLibraryChange checks; it
prunes from files/bin each top-level entry an earlier build put there
and this one does not, reporting those the program wrote there itself.
What cannot be a Flatpak is refused before anything is built, saying
why: a Windows or wasm executable, a build against a sysroot or for
musl, another architecture than the runtime's, a build against
CRAFTER_BUILD_LIBCXX_DIR, whose libc++ the Flatpak would not carry, and
a build by a native crafter-build, which links the host's glibc: a
native process knows no Flatpak runtime, from /.flatpak-info, unless
CRAFTER_BUILD_FLATPAK_INFO, the tests' hook, names a file with one.
Default stays the executable as linked for now, RunTests runs every test
as linked, whatever its Configuration asks for, and RunLint works out
its compile command so, and a forwarded --packaging= leaves a
dependency's executables alone, since consumers run them by path. A
dependency that packages its own executable gets its desktop file,
metainfo and icons rebased by the loader as its cfg.files are, so they
are its own whatever directory the consumer builds from. Only a Flatpak
is keyed into VariantId, so every other configuration keeps its
directory, but for one given --packaging=, which was a project arg,
hashed into VariantId, and is a standard arg now, a dependency's
included. The app-id and what a Flatpak is installed as are not keyed:
they change files/share, never what is compiled. The asset name map,
kept three times, twice in Build() and once for the browser runtime's
files.json, is one function now, CompressedAssetName; CompressAsset, in
Crafter.Build-Asset.cpp, keeps the list of the extensions it loads,
which has to agree with it. Each copy a Flatpak build makes into the
program's directory, of its shaders, assets, cfg.files and what its
dependencies forward, records the top-level name it places there, the
names of what a directory holds for one copied into it itself ("data/",
"data/."), and those are what the writer keeps in files/bin; a build
that fails after copying adds them to the writer's record all the same,
so the next write removes what no build places any more.
SharedLibraryRuns now also packages its program as a Flatpak, whose
files/lib has to carry libgreet.so for it to start.
2026-11-01 19:20:55 +01:00
}
SyncOwned ( files / " share " , share ) ;
// desktop-file-validate checks the entry here, where it was written,
// on every write that places one, not only one that copies it: a
// write that copied the entry and then failed would leave it
// unchecked for every write after. Where the tool exists: the Sdk has
// it, a development box may not.
feat: install a Flatpak's data under /app/share
A program packaged as a Flatpak could only have its files beside it in
files/bin, where cfg.files goes. One that finds its data at <its
directory>/../share/<name>, as Crafter.Review finds its process/
directory, had no way to get it there: files/share belongs to the
writer, which removes anything it did not put there, and a module of
flatpak-builder's would have placed it only in the installed app, never
under -r. cfg.flatpak.share lists files and directories of the project
and the path under /app/share each goes to. The writer copies them into
files/share with the desktop entry, metainfo and icons, keeps them while
they are listed and removes them once they are not, as it does those. -r
runs the program from files/bin, so the same ../share lookup finds them
there. What is exported of them is flatpak build-finish's to decide, by
its own rules, as for any app's files: the app's D-Bus services, MIME
types, search providers and KRunner plugins, which have no field of
their own, are placed like any other file. A library's are not passed on
to the programs built on it, since data a library needs at run time
already goes through cfg.files. A dependency that packages its own
executable has the sources rebased by the loader, as its cfg.files are.
Share entries change files/share and not what is compiled, so they are
not keyed into VariantId, which its test checks.
Every file bound for files/share, the desktop entry, metainfo and icons
included, now goes through one placement check, which refuses two files
for one path, a field's file among them, and a file where another needs
a directory, naming both. A target that is absolute or leaves share/ is
refused too. Any other place is open to a share entry, a second desktop
entry, an add-on's metainfo and any icon included, as for any app's
files: flatpak build-export refuses an exported icon that is not a
square image and a D-Bus service that is not a key file, warns of the
app's own desktop entry where desktop-file-validate rejects it or its
Icon= is not the app's, and exports a second desktop entry, a MIME
package or metainfo as it is. The app's own desktop entry,
applications/<app-id>.desktop, is held to cfg.flatpak.desktopFile's
checks whichever placed it: its Icon= and desktop-file-validate are
keyed to that place now, not to the field. A source directory that holds
what the build writes is refused as well, since it would be copied into
itself, growing each time the app is built or exported: files/, and the
app's directory, where install and bundle put another copy of files/ and
the exported app, compared as written and where links lead, so that the
project's own directory is refused where its bin/ is a link elsewhere
too. So is one that lies in them, the build's own output, which the next
build, install or bundle writes again. The loader leaves an empty source
empty, as it does an empty desktop file.
FlatpakShareLarge holds the writer to comparing an entry with its copy
at constant memory, which the program's data, gigabytes of it in a game,
needs on every build: it rewrites a files/ holding a 32 MiB entry, with
nothing changed, in a child process whose address space may grow by 16
MiB, with a stand-in llvm-readelf, since the real one, which a write
starts for the libraries, would not fit under that limit, and a change
in the entry's last byte, in the short block a comparison reads last, is
still copied.
2026-11-02 20:04:14 +01:00
if ( entry ! = share . end ( ) & & RunCommandChecked ( " command -v desktop-file-validate " ) . exitCode = = 0 ) {
feat: lay an executable out as the files/ of a Flatpak
A Linux executable built by the Flatpak crafter-build links the libc++,
libc++abi and libunwind in its /app/lib and the Sdk's glibc, so it runs
inside crafter-build's sandbox and practically nowhere else: a host
needs glibc 2.38 or newer and all three LLVM libraries, which not even
Arch has. A program built here is meant to run in a Flatpak, so it can
now be laid out as one: with cfg.packaging = Packaging::Flatpak, or
--packaging=flatpak where the project.cpp crafter-build was pointed at
calls ApplyStandardArgs, its BinDir() holds files/, what becomes /app in
the app's sandbox, and -r runs the program there against the libraries
the app carries.
The program is linked into files/bin, where its shaders, files and
assets land too (ProgramDir()), with $ORIGIN/../lib as its one run path,
and the new :Flatpak partition writes the rest of files/: files/lib with
the LLVM runtime libraries, located one by one through clang, and
whatever else the program needs, read with llvm-readelf, that the
directories of the shared libraries it is built on hold, the ones an
unpackaged link records as run paths, or the toolchain's own lib dir,
/app/lib in crafter-build's Flatpak, which the link searches too; and
the desktop entry, metainfo and icons under the app-id in files/share,
the entry refused when its Icon= is not the app-id or
desktop-file-validate, where there is one, rejects it, and an icon that
is not an .svg or a square .png. The metadata and export/ of a build
directory are flatpak build-finish's to write, and crafter-build keeps
no copy of its rules: what a permission means and what the host sees of
an app change with flatpak's releases, and a copy would have to follow
every one. The writer copies a file only when its content differs,
compared a block at a time so that a large file costs no more memory
than a small one, and writes one only when what it would write differs,
never deciding by an mtime, which ostree sets to 0 for every file of
/app, and works the program's libraries out on every write, which reads
only their dynamic sections, since a library replaced in place, as by a
toolchain update, can need others, which FlatpakLibraryChange checks; it
prunes from files/bin each top-level entry an earlier build put there
and this one does not, reporting those the program wrote there itself.
What cannot be a Flatpak is refused before anything is built, saying
why: a Windows or wasm executable, a build against a sysroot or for
musl, another architecture than the runtime's, a build against
CRAFTER_BUILD_LIBCXX_DIR, whose libc++ the Flatpak would not carry, and
a build by a native crafter-build, which links the host's glibc: a
native process knows no Flatpak runtime, from /.flatpak-info, unless
CRAFTER_BUILD_FLATPAK_INFO, the tests' hook, names a file with one.
Default stays the executable as linked for now, RunTests runs every test
as linked, whatever its Configuration asks for, and RunLint works out
its compile command so, and a forwarded --packaging= leaves a
dependency's executables alone, since consumers run them by path. A
dependency that packages its own executable gets its desktop file,
metainfo and icons rebased by the loader as its cfg.files are, so they
are its own whatever directory the consumer builds from. Only a Flatpak
is keyed into VariantId, so every other configuration keeps its
directory, but for one given --packaging=, which was a project arg,
hashed into VariantId, and is a standard arg now, a dependency's
included. The app-id and what a Flatpak is installed as are not keyed:
they change files/share, never what is compiled. The asset name map,
kept three times, twice in Build() and once for the browser runtime's
files.json, is one function now, CompressedAssetName; CompressAsset, in
Crafter.Build-Asset.cpp, keeps the list of the extensions it loads,
which has to agree with it. Each copy a Flatpak build makes into the
program's directory, of its shaders, assets, cfg.files and what its
dependencies forward, records the top-level name it places there, the
names of what a directory holds for one copied into it itself ("data/",
"data/."), and those are what the writer keeps in files/bin; a build
that fails after copying adds them to the writer's record all the same,
so the next write removes what no build places any more.
SharedLibraryRuns now also packages its program as a Flatpak, whose
files/lib has to carry libgreet.so for it to start.
2026-11-01 19:20:55 +01:00
const CommandResult r = RunCommandChecked ( std : : format ( " desktop-file-validate {} " , ShellQuote ( ( files / " share " / desktopEntry ) . string ( ) ) ) ) ;
if ( r . exitCode ! = 0 ) {
std : : error_code ec ;
fs : : remove ( files / " share " / desktopEntry , ec ) ;
feat: install a Flatpak's data under /app/share
A program packaged as a Flatpak could only have its files beside it in
files/bin, where cfg.files goes. One that finds its data at <its
directory>/../share/<name>, as Crafter.Review finds its process/
directory, had no way to get it there: files/share belongs to the
writer, which removes anything it did not put there, and a module of
flatpak-builder's would have placed it only in the installed app, never
under -r. cfg.flatpak.share lists files and directories of the project
and the path under /app/share each goes to. The writer copies them into
files/share with the desktop entry, metainfo and icons, keeps them while
they are listed and removes them once they are not, as it does those. -r
runs the program from files/bin, so the same ../share lookup finds them
there. What is exported of them is flatpak build-finish's to decide, by
its own rules, as for any app's files: the app's D-Bus services, MIME
types, search providers and KRunner plugins, which have no field of
their own, are placed like any other file. A library's are not passed on
to the programs built on it, since data a library needs at run time
already goes through cfg.files. A dependency that packages its own
executable has the sources rebased by the loader, as its cfg.files are.
Share entries change files/share and not what is compiled, so they are
not keyed into VariantId, which its test checks.
Every file bound for files/share, the desktop entry, metainfo and icons
included, now goes through one placement check, which refuses two files
for one path, a field's file among them, and a file where another needs
a directory, naming both. A target that is absolute or leaves share/ is
refused too. Any other place is open to a share entry, a second desktop
entry, an add-on's metainfo and any icon included, as for any app's
files: flatpak build-export refuses an exported icon that is not a
square image and a D-Bus service that is not a key file, warns of the
app's own desktop entry where desktop-file-validate rejects it or its
Icon= is not the app's, and exports a second desktop entry, a MIME
package or metainfo as it is. The app's own desktop entry,
applications/<app-id>.desktop, is held to cfg.flatpak.desktopFile's
checks whichever placed it: its Icon= and desktop-file-validate are
keyed to that place now, not to the field. A source directory that holds
what the build writes is refused as well, since it would be copied into
itself, growing each time the app is built or exported: files/, and the
app's directory, where install and bundle put another copy of files/ and
the exported app, compared as written and where links lead, so that the
project's own directory is refused where its bin/ is a link elsewhere
too. So is one that lies in them, the build's own output, which the next
build, install or bundle writes again. The loader leaves an empty source
empty, as it does an empty desktop file.
FlatpakShareLarge holds the writer to comparing an entry with its copy
at constant memory, which the program's data, gigabytes of it in a game,
needs on every build: it rewrites a files/ holding a 32 MiB entry, with
nothing changed, in a child process whose address space may grow by 16
MiB, with a stand-in llvm-readelf, since the real one, which a write
starts for the libraries, would not fit under that limit, and a change
in the entry's last byte, in the short block a comparison reads last, is
still copied.
2026-11-02 20:04:14 +01:00
return std : : format ( " {} does not pass desktop-file-validate: \n {} " , entry - > second . string ( ) , Trim ( r . output ) ) ;
feat: lay an executable out as the files/ of a Flatpak
A Linux executable built by the Flatpak crafter-build links the libc++,
libc++abi and libunwind in its /app/lib and the Sdk's glibc, so it runs
inside crafter-build's sandbox and practically nowhere else: a host
needs glibc 2.38 or newer and all three LLVM libraries, which not even
Arch has. A program built here is meant to run in a Flatpak, so it can
now be laid out as one: with cfg.packaging = Packaging::Flatpak, or
--packaging=flatpak where the project.cpp crafter-build was pointed at
calls ApplyStandardArgs, its BinDir() holds files/, what becomes /app in
the app's sandbox, and -r runs the program there against the libraries
the app carries.
The program is linked into files/bin, where its shaders, files and
assets land too (ProgramDir()), with $ORIGIN/../lib as its one run path,
and the new :Flatpak partition writes the rest of files/: files/lib with
the LLVM runtime libraries, located one by one through clang, and
whatever else the program needs, read with llvm-readelf, that the
directories of the shared libraries it is built on hold, the ones an
unpackaged link records as run paths, or the toolchain's own lib dir,
/app/lib in crafter-build's Flatpak, which the link searches too; and
the desktop entry, metainfo and icons under the app-id in files/share,
the entry refused when its Icon= is not the app-id or
desktop-file-validate, where there is one, rejects it, and an icon that
is not an .svg or a square .png. The metadata and export/ of a build
directory are flatpak build-finish's to write, and crafter-build keeps
no copy of its rules: what a permission means and what the host sees of
an app change with flatpak's releases, and a copy would have to follow
every one. The writer copies a file only when its content differs,
compared a block at a time so that a large file costs no more memory
than a small one, and writes one only when what it would write differs,
never deciding by an mtime, which ostree sets to 0 for every file of
/app, and works the program's libraries out on every write, which reads
only their dynamic sections, since a library replaced in place, as by a
toolchain update, can need others, which FlatpakLibraryChange checks; it
prunes from files/bin each top-level entry an earlier build put there
and this one does not, reporting those the program wrote there itself.
What cannot be a Flatpak is refused before anything is built, saying
why: a Windows or wasm executable, a build against a sysroot or for
musl, another architecture than the runtime's, a build against
CRAFTER_BUILD_LIBCXX_DIR, whose libc++ the Flatpak would not carry, and
a build by a native crafter-build, which links the host's glibc: a
native process knows no Flatpak runtime, from /.flatpak-info, unless
CRAFTER_BUILD_FLATPAK_INFO, the tests' hook, names a file with one.
Default stays the executable as linked for now, RunTests runs every test
as linked, whatever its Configuration asks for, and RunLint works out
its compile command so, and a forwarded --packaging= leaves a
dependency's executables alone, since consumers run them by path. A
dependency that packages its own executable gets its desktop file,
metainfo and icons rebased by the loader as its cfg.files are, so they
are its own whatever directory the consumer builds from. Only a Flatpak
is keyed into VariantId, so every other configuration keeps its
directory, but for one given --packaging=, which was a project arg,
hashed into VariantId, and is a standard arg now, a dependency's
included. The app-id and what a Flatpak is installed as are not keyed:
they change files/share, never what is compiled. The asset name map,
kept three times, twice in Build() and once for the browser runtime's
files.json, is one function now, CompressedAssetName; CompressAsset, in
Crafter.Build-Asset.cpp, keeps the list of the extensions it loads,
which has to agree with it. Each copy a Flatpak build makes into the
program's directory, of its shaders, assets, cfg.files and what its
dependencies forward, records the top-level name it places there, the
names of what a directory holds for one copied into it itself ("data/",
"data/."), and those are what the writer keeps in files/bin; a build
that fails after copying adds them to the writer's record all the same,
so the next write removes what no build places any more.
SharedLibraryRuns now also packages its program as a Flatpak, whose
files/lib has to carry libgreet.so for it to start.
2026-11-01 19:20:55 +01:00
}
}
// In files/bin, what an earlier write placed and this one does not
// is a stale build output, a removed cfg.files entry or a renamed
// shader. What neither placed the program wrote, under -r, since it
// runs there.
std : : vector < std : : string > foreign ;
for ( const fs : : directory_entry & entry : fs : : directory_iterator ( files / " bin " ) ) {
const std : : string name = entry . path ( ) . filename ( ) . string ( ) ;
if ( d . programEntries . contains ( name ) ) continue ;
if ( old . placed . contains ( name ) ) fs : : remove_all ( entry . path ( ) ) ;
else foreign . push_back ( name ) ;
}
now . placed = d . programEntries ;
if ( ! foreign . empty ( ) ) {
std : : ranges : : sort ( foreign ) ;
std : : string names ;
for ( const std : : string & name : foreign ) names + = std : : format ( " {}{} " , names . empty ( ) ? " " : " , " , name ) ;
Progress : : Diagnostic ( std : : format ( " {}: {} in {} {} not put there by the build, so the program wrote {}, presumably under -r. {} with the app; installed, /app is read-only and a program cannot write there. " , d . appId , names , ( files / " bin " ) . string ( ) , foreign . size ( ) = = 1 ? " was " : " were " , foreign . size ( ) = = 1 ? " it " : " them " , foreign . size ( ) = = 1 ? " It ships " : " They ship " ) ) ;
}
WriteRecord ( d . record , now ) ;
} catch ( const std : : exception & e ) {
return std : : format ( " {}: {} " , d . appId , e . what ( ) ) ;
}
return " " ;
}
feat: install and bundle a Flatpak through the host's flatpak
An executable packaged as a Flatpak came out as the files/ of one, which
only flatpak build-finish makes an app of, and getting it installed or
onto another machine took flatpak commands on the host, typed by hand.
Two verbs do it where the host's flatpak can be reached:
`crafter-build install` builds the project and installs its app for this
user, and `crafter-build bundle` builds it into bin/<variant>.flatpak, a
single file for `flatpak install --user` that names Flathub's repository
for the runtime. Inside crafter-build's Flatpak they need the
org.freedesktop.Flatpak permission, which its manifest does not grant in
this commit. Both put the app into the project's repo first, in
build/flatpak-apps/<app-id>/ (FlatpakAppDir), one per app rather than
per variant, since flatpak-builder's state there is its cache. Every
variant exports into it, so the export and the install or bundle made
from it hold a lock there, and another crafter-build exporting meanwhile
waits rather than leave this one installing or bundling its program. The
user remote an install re-points is every checkout's that builds the
app-id, so re-pointing it and installing from it hold a second lock,
which every crafter-build installing there takes, natively or in the
Flatpak, whatever its XDG_CACHE_HOME: in
~/.cache/crafter.build/flatpak-remotes/, found by HOME, for the user's
own installation, which flatpak hides from the sandbox behind a tmpfs of
the sandbox's own, and in the directory of a private one,
FLATPAK_USER_DIR, one lock for all of them where the sandbox shares that
directory with the host. Without modules, the host's flatpak
build-finish finishes build/flatpak-apps/<app-id>/build-dir, made afresh
each time from a metadata naming the app, its runtime and the Sdk and a
tree of hard links to the build's files/, copies where a link cannot be
made, across filesystems or on one without links, vfat or exFAT: it
writes the command, the sandbox's permissions and export/ by flatpak's
own rules, those of whichever flatpak the host runs, and refuses a
permission it does not know, and flatpak build-export puts the result
into the repo. When cfg.flatpak.modules names flatpak-builder module
files for what the app needs in /app besides the program,
flatpak-builder does both, from a manifest that lists them and copies
the built program in last, so nothing is compiled twice. That manifest
keeps share/locale in the app ("separate-locales": false):
flatpak-builder would split it into a .Locale extension by default,
which a bundle, holding the app alone, leaves out. The loader rebases a
dependency's module files as it does its cfg.files. install uses a user
remote named after the app-id, re-pointed at the repo each time, and
says how to remove both. The app's tree in
build/flatpak-apps/<app-id>/build-dir, where flatpak-builder installs a
module's files too, is left out of the include walk with every other
build/, so the headers a module installed reach no compile.
cfg.flatpak gets the runtime, the Platform or the Sdk at the branch
crafter-build runs on, and the sandbox's permissions, finish-args as
Flatpak spells them, which any configuration may declare: an executable
gets its dependencies' first and its own last, so it can take one back.
The build reads neither, and a change to either links nothing. install
and bundle print the permissions, with where each came from, and hand
them to build-finish, the one judge of what they mean, so a refusal can
be traced to the configuration that declared it. FlatpakPermissions
checks the order everywhere, and that a change to one neither moves the
build to another directory nor links anything, and, where the host's
flatpak runs, what build-finish makes of them and that it refuses a
socket it does not have; FlatpakBundle that build-finish exports the
desktop entry and the icon, and that the build's own files/ is left as
it was; VariantId's test has the runtime, permissions and modules move
no directory.
The host's flatpak and flatpak-builder are reached only through
HostCommand in :Platform, which builds the command lines of
RunHostFlatpak, RunHostFlatpakBuilder and HostFlatpakUnavailable's
probes: flatpak-spawn --host --watch-bus inside crafter-build's Flatpak,
started in the host's / and passing FLATPAK_USER_DIR on, and the command
itself natively. flatpak-spawn would otherwise start a command in
crafter-build's own working directory, which the host need not have, and
from /app, say, every host command would fail. HostFlatpakUnavailable
says why the host cannot be reached: no org.freedesktop.Flatpak
permission, no session bus (CI's build sandbox has neither), a host that
does not see the project's bin/ and build/ where crafter-build does, as
it does not in the sandbox's own /tmp, /var, /usr, /etc and /app or
where a link leads into one, or no flatpak natively. That, and an
executable that is not packaged as a Flatpak, install and bundle refuse
as soon as the project is loaded, before anything is built;
FlatpakVerbRefusal shows the second. The host is asked rather than the
sandbox's paths modelled, which an override sharing a directory inside
the sandbox's own would defeat: its flatpak is started in a directory
just made in each of bin/ and build/ under a name no other has, which it
enters only where it sees that very directory, and the directory is
removed again. --help gets a section on both, and its Clean section says
the repo an install points at goes with build/. FlatpakModules runs
flatpak-builder only with a flatpak-builder program on the host,
crafter-build falling back to the org.flatpak.Builder app, whose
`flatpak run` would write ~/.var/app in the real home, and with the
Platform and Sdk it builds the app with. The library's install function
is InstallFlatpakApp because project.cpp files define an InstallFlatpak
script of their own under `using namespace Crafter`, this repository's
included, and a library InstallFlatpak would make the name an overload
set, which the scripts table's std::function cannot be built from;
BundleFlatpakApp is named to match. JsonQuote writes the manifest's
strings, and variants.json's, which escaped quotes and backslashes only.
FlatpakExport, FlatpakManifestText, ExportFlatpak and FlatpakPermission,
which only :Clang uses, stay out of the API a project.cpp compiles
against, as RecordFlatpakPlaced does, so their names cannot meet a
project's own under `using namespace Crafter`; ExportFlatpak's comment
names the app's lock, which its callers hold, beside
HostFlatpakUnavailable.
2026-11-02 19:38:30 +01:00
std : : string Crafter : : FlatpakManifestText ( const FlatpakExport & app ) {
std : : string finishArgs ;
for ( const FlatpakPermission & permission : app . permissions ) {
finishArgs + = std : : format ( " {} \n {} " , finishArgs . empty ( ) ? " " : " , " , JsonQuote ( permission . argument ) ) ;
}
std : : string modules ;
for ( const fs : : path & module : app . modules ) modules + = std : : format ( " {}, \n " , JsonQuote ( module . string ( ) ) ) ;
// The program's module is named apart from anything a project would
// call one of its own: module names are flatpak-builder's cache keys.
// "separate-locales": false keeps /app/share/locale in the app. By
// default flatpak-builder moves it into an <app-id>.Locale extension,
// which an install fetches with the app but a bundle, which holds the
// app alone, leaves out.
return std : : format (
" {{ \n "
" \" app-id \" : {}, \n "
" \" runtime \" : {}, \n "
" \" runtime-version \" : {}, \n "
" \" sdk \" : \" org.freedesktop.Sdk \" , \n "
" \" command \" : {}, \n "
" \" separate-locales \" : false, \n "
" \" finish-args \" : [{}{}], \n "
" \" modules \" : [ \n "
" {} "
" {{ \n "
" \" name \" : \" crafter-build-program \" , \n "
" \" buildsystem \" : \" simple \" , \n "
" \" build-commands \" : [ \" cp -a . /app/ \" ], \n "
" \" sources \" : [ {{ \" type \" : \" dir \" , \" path \" : {} }} ] \n "
" }} \n "
" ] \n "
" }} \n " ,
JsonQuote ( app . appId ) ,
app . runtime = = FlatpakRuntime : : Sdk ? " \" org.freedesktop.Sdk \" " : " \" org.freedesktop.Platform \" " ,
JsonQuote ( app . branch ) ,
JsonQuote ( app . command ) ,
finishArgs , finishArgs . empty ( ) ? " " : " \n " ,
modules ,
JsonQuote ( app . files . string ( ) ) ) ;
}
namespace {
// `from` again at `to`: hard links where they can be made, copies where
// they cannot, links as links. flatpak build-finish and build-export
// read files/ and write nothing into it, so a link costs no copy of the
// app's data and leaves the build's files as they are. A link fails
// across filesystems, and on one that has none, vfat or exFAT on a USB
// stick say, with another error; whatever the reason, the copy is made
// instead, and a failure it shares, a full disk say, shows there.
void LinkOrCopyTree ( const fs : : path & from , const fs : : path & to ) {
try {
fs : : copy ( from , to , fs : : copy_options : : recursive | fs : : copy_options : : create_hard_links | fs : : copy_options : : copy_symlinks ) ;
} catch ( const fs : : filesystem_error & ) {
fs : : remove_all ( to ) ;
fs : : copy ( from , to , fs : : copy_options : : recursive | fs : : copy_options : : copy_symlinks ) ;
}
}
}
std : : string Crafter : : ExportFlatpak ( const FlatpakExport & app ) {
// On every install and bundle: the build does not read them, and
// build-finish names only the argument it refuses, not the
// configuration it came from.
if ( ! app . permissions . empty ( ) ) {
std : : string report = std : : format ( " {} has these Flatpak permissions: " , app . appId ) ;
for ( const FlatpakPermission & permission : app . permissions ) report + = std : : format ( " \n {} (from {}) " , permission . argument , permission . source ) ;
Progress : : Diagnostic ( report ) ;
}
try {
fs : : create_directories ( app . work ) ;
const fs : : path repo = app . work / " repo " ;
const fs : : path buildDir = app . work / " build-dir " ;
if ( app . modules . empty ( ) ) {
// Made afresh each time: build-finish refuses a directory it
// finished before, and what a finish wrote, such as the export/
// of a desktop entry the app no longer has, must not reach the
// next one.
fs : : remove_all ( buildDir ) ;
fs : : create_directories ( buildDir ) ;
const std : : string_view runtime = app . runtime = = FlatpakRuntime : : Sdk ? " org.freedesktop.Sdk " : " org.freedesktop.Platform " ;
WriteIfDifferent ( buildDir / " metadata " , std : : format ( " [Application] \n name={} \n runtime={}/{}/{} \n sdk=org.freedesktop.Sdk/{}/{} \n " , app . appId , runtime , app . arch , app . branch , app . arch , app . branch ) ) ;
LinkOrCopyTree ( app . files , buildDir / " files " ) ;
// The permissions before --command, so the program stays the
// app's command whatever they hold.
std : : string finish = " build-finish " ;
for ( const FlatpakPermission & permission : app . permissions ) finish + = std : : format ( " {} " , ShellQuote ( permission . argument ) ) ;
finish + = std : : format ( " --command={} {} " , ShellQuote ( app . command ) , ShellQuote ( buildDir . string ( ) ) ) ;
if ( RunHostFlatpak ( finish ) ! = 0 ) {
return std : : format ( " {}: flatpak build-finish failed{} " , app . appId , app . permissions . empty ( ) ? " " : " ; the permissions it was given are listed above, with where each came from " ) ;
}
if ( RunHostFlatpak ( std : : format ( " build-export {} {} " , ShellQuote ( repo . string ( ) ) , ShellQuote ( buildDir . string ( ) ) ) ) ! = 0 ) {
return std : : format ( " {}: flatpak build-export failed " , app . appId ) ;
}
return " " ;
}
// Written only when it changes, so flatpak-builder sees the same
// manifest, and reuses what it built from it, until it does.
const fs : : path manifest = app . work / std : : format ( " {}.json " , app . appId ) ;
WriteIfDifferent ( manifest , FlatpakManifestText ( app ) ) ;
// --force-clean: the build directory is flatpak-builder's scratch,
// made again from its cache each time; the repo keeps what it
// exported.
const std : : string arguments = std : : format ( " --force-clean --state-dir={} --repo={} {} {} " , ShellQuote ( ( app . work / " state " ) . string ( ) ) , ShellQuote ( repo . string ( ) ) , ShellQuote ( buildDir . string ( ) ) , ShellQuote ( manifest . string ( ) ) ) ;
if ( RunHostFlatpakBuilder ( arguments ) ! = 0 ) return std : : format ( " {}: flatpak-builder failed on {} " , app . appId , manifest . string ( ) ) ;
} catch ( const std : : exception & e ) {
return std : : format ( " {}: {} " , app . appId , e . what ( ) ) ;
}
return " " ;
}