1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
mod bundle;
mod cache;
mod compress;
mod extract;
mod info;
mod list;
mod metadata;
mod pack;
use std::path::PathBuf;
use clap::{Parser, Subcommand, ValueEnum};
use onelf_format::WorkingDir;
const RUNTIME_BINARY: &[u8] = include_bytes!(env!("ONELF_RT_PATH"));
#[derive(Parser)]
#[command(name = "onelf", about = "Single-binary packaging tool", version)]
struct Cli {
#[command(subcommand)]
command: Commands,
}
#[derive(Subcommand)]
enum Commands {
/// Pack a directory into a single executable
Pack {
/// Directory to pack
directory: PathBuf,
/// Output file path
#[arg(short, long)]
output: PathBuf,
/// Relative path to the command within the directory
#[arg(long)]
command: String,
/// Package name (for identification, defaults to command basename)
#[arg(long)]
name: Option<String>,
/// Additional entrypoints (name=path)
#[arg(long, value_parser = parse_entrypoint)]
entrypoint: Vec<(String, String)>,
/// Default entrypoint name
#[arg(long)]
default_entrypoint: Option<String>,
/// Library directories to add to LD_LIBRARY_PATH (repeatable)
#[arg(long)]
lib_dir: Vec<String>,
/// Zstd compression level (0-22)
#[arg(long, default_value = "12")]
level: i32,
/// Build a shared zstd dictionary
#[arg(long)]
dict: bool,
/// Mark default entrypoint as memfd-eligible
#[arg(long)]
memfd: bool,
/// Force cache mode (disable memfd)
#[arg(long)]
no_memfd: bool,
/// Working directory strategy
#[arg(long, default_value = "inherit")]
working_dir: WorkingDirArg,
/// Base URL for delta updates (stored in .onelf/update-url)
#[arg(long)]
update_url: Option<String>,
/// Exclude files matching glob patterns (repeatable, e.g. "*.a", "__pycache__")
#[arg(long)]
exclude: Vec<String>,
},
/// Show metadata about a packed binary
Info {
/// Path to the onelf binary
binary: PathBuf,
},
/// List all files in a packed binary
List {
/// Path to the onelf binary
binary: PathBuf,
},
/// Extract files from a packed binary
Extract {
/// Path to the onelf binary
binary: PathBuf,
/// Output path (directory, file, or "-" for stdout)
#[arg(short, long)]
output: Option<PathBuf>,
/// Extract only specific files by path (repeatable)
#[arg(long)]
file: Vec<String>,
},
/// Extract icon from a packed binary
Icon {
/// Path to the onelf binary
binary: PathBuf,
/// Entrypoint name (default: default entrypoint)
#[arg(long)]
entrypoint: Option<String>,
/// Output path (default: stdout)
#[arg(short, long)]
output: Option<PathBuf>,
},
/// Extract desktop file from a packed binary
Desktop {
/// Path to the onelf binary
binary: PathBuf,
/// Entrypoint name (default: default entrypoint)
#[arg(long)]
entrypoint: Option<String>,
/// Output path (default: stdout)
#[arg(short, long)]
output: Option<PathBuf>,
},
/// Manage the onelf cache
Cache {
#[command(subcommand)]
action: CacheAction,
},
/// Bundle shared library dependencies into a directory
BundleLibs {
/// Directory containing the application to bundle
directory: PathBuf,
/// Specific binary to analyze (default: scan all ELF files)
#[arg(long)]
target: Option<PathBuf>,
/// Where to copy libs, relative to DIRECTORY
#[arg(long, default_value = "lib")]
lib_dir: PathBuf,
/// Exclude libs matching pattern (prefix match, comma-separated or repeatable)
#[arg(long, value_delimiter = ',')]
exclude: Vec<String>,
/// Additional libraries to include by soname (e.g. dlopen'd libs, comma-separated or repeatable)
#[arg(long, value_delimiter = ',')]
include: Vec<String>,
/// Additional directories to search for libraries (repeatable)
#[arg(long)]
search_path: Vec<PathBuf>,
/// Show what would be copied without copying
#[arg(long)]
dry_run: bool,
/// Don't resolve transitive dependencies
#[arg(long)]
no_recursive: bool,
/// Bundle Mesa GL/EGL/GBM libraries
#[arg(long)]
gl: bool,
/// Bundle DRI drivers (architecture-filtered)
#[arg(long)]
dri: bool,
/// Bundle Vulkan ICD drivers (architecture-filtered)
#[arg(long)]
vulkan: bool,
/// Bundle Wayland client libraries (libwayland, libdecor, libxkbcommon)
#[arg(long)]
wayland: bool,
/// Bundle GSettings schemas for GTK apps
#[arg(long)]
gtk: bool,
/// Strip debug symbols from copied libraries
#[arg(long)]
strip: bool,
},
}
#[derive(Subcommand)]
enum CacheAction {
/// List cached packages
List,
/// Remove all cached data
Clear,
/// Garbage collect old cache entries
Gc {
/// Maximum age in days
#[arg(long, default_value = "30")]
max_age: u64,
},
}
#[derive(Clone, ValueEnum)]
enum WorkingDirArg {
Inherit,
Package,
Command,
}
fn parse_entrypoint(s: &str) -> Result<(String, String), String> {
let (name, path) = s.split_once('=').ok_or("expected format: name=path")?;
Ok((name.to_string(), path.to_string()))
}
fn main() {
let cli = Cli::parse();
let result = match cli.command {
Commands::Pack {
directory,
output,
command,
name,
entrypoint,
default_entrypoint,
lib_dir,
level,
dict,
memfd,
no_memfd,
working_dir,
update_url,
exclude,
} => {
let memfd_opt = if no_memfd {
Some(false)
} else if memfd {
Some(true)
} else {
None
};
let wd = match working_dir {
WorkingDirArg::Inherit => WorkingDir::Inherit,
WorkingDirArg::Package => WorkingDir::PackageRoot,
WorkingDirArg::Command => WorkingDir::EntrypointParent,
};
pack::pack(
&pack::PackOptions {
directory,
output,
command,
name,
entrypoints: entrypoint,
default_entrypoint,
lib_dirs: lib_dir,
level,
use_dict: dict,
memfd: memfd_opt,
working_dir: wd,
update_url,
exclude,
},
RUNTIME_BINARY,
)
}
Commands::Info { binary } => info::info(&binary),
Commands::List { binary } => list::list(&binary),
Commands::Extract {
binary,
output,
file,
} => extract::extract(&binary, output.as_deref(), &file),
Commands::Icon {
binary,
entrypoint,
output,
} => metadata::icon(&binary, entrypoint.as_deref(), output.as_deref()),
Commands::Desktop {
binary,
entrypoint,
output,
} => metadata::desktop(&binary, entrypoint.as_deref(), output.as_deref()),
Commands::Cache { action } => match action {
CacheAction::List => cache::cache_list(),
CacheAction::Clear => cache::cache_clear(),
CacheAction::Gc { max_age } => cache::cache_gc(max_age),
},
Commands::BundleLibs {
directory,
target,
lib_dir,
exclude,
include,
search_path,
dry_run,
no_recursive,
gl,
dri,
vulkan,
wayland,
gtk,
strip,
} => bundle::bundle_libs(&bundle::BundleOptions {
directory,
target,
lib_dir,
exclude,
include,
search_path,
dry_run,
recursive: !no_recursive,
gl,
dri,
vulkan,
wayland,
gtk,
strip,
}),
};
if let Err(e) = result {
eprintln!("error: {e}");
std::process::exit(1);
}
}