-
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
// Copyright 2025 Shota FUJI
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
//! This script executes linuxdeploy tool against build output directory.
const std = @import("std");
const ExitCode = enum(u8) {
ok = 0,
generic_error = 1,
incorrect_usage = 2,
pub inline fn to_u8(self: @This()) u8 {
return @intFromEnum(self);
}
};
pub fn main() !u8 {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();
const allocator = arena.allocator();
var args = try std.process.ArgIterator.initWithAllocator(allocator);
defer args.deinit();
var arg_linuxdeploy: ?[]const u8 = null;
var arg_exe: ?[]const u8 = null;
var arg_desktop_entry: ?[]const u8 = null;
var arg_icon: ?[]const u8 = null;
var arg_out: ?[]const u8 = null;
var arg_appdir: ?[]const u8 = null;
// Skip exe name
_ = args.skip();
while (args.next()) |arg| {
var iter = std.mem.splitScalar(u8, arg, '=');
const key = iter.next() orelse {
continue;
};
const value = iter.next() orelse {
std.log.err("Arg must be --key=value format (parsing {s})", .{key});
return ExitCode.incorrect_usage.to_u8();
};
if (std.mem.eql(u8, "--linuxdeploy", key)) {
arg_linuxdeploy = value;
continue;
}
if (std.mem.eql(u8, "--exe", key)) {
arg_exe = value;
continue;
}
if (std.mem.eql(u8, "--desktop-entry", key)) {
arg_desktop_entry = value;
continue;
}
if (std.mem.eql(u8, "--icon", key)) {
arg_icon = value;
continue;
}
if (std.mem.eql(u8, "--out", key)) {
arg_out = value;
continue;
}
if (std.mem.eql(u8, "--appdir", key)) {
arg_appdir = value;
continue;
}
std.log.err("Unknown arg: {s}", .{key});
return ExitCode.incorrect_usage.to_u8();
}
const linuxdeploy = arg_linuxdeploy orelse {
std.log.err("--linuxdeploy is required", .{});
return ExitCode.incorrect_usage.to_u8();
};
const exe = arg_exe orelse {
std.log.err("--exe is required", .{});
return ExitCode.incorrect_usage.to_u8();
};
const desktop_entry = arg_desktop_entry orelse {
std.log.err("--desktop-entry is required", .{});
return ExitCode.incorrect_usage.to_u8();
};
const icon = arg_icon orelse {
std.log.err("--icon is required", .{});
return ExitCode.incorrect_usage.to_u8();
};
const appdir = arg_appdir orelse {
std.log.err("--appdir is required", .{});
return ExitCode.incorrect_usage.to_u8();
};
const out = arg_out orelse {
std.log.err("--out is required", .{});
return ExitCode.incorrect_usage.to_u8();
};
var base = try std.fs.openDirAbsolute(appdir, .{});
defer base.close();
// linuxdeploy tries to extract from exclude-library-stripped binary for some reason.
// We have to manually copy beforehand otherwise nonsensial error ("Could not find dependency")
// prevents build process.
try base.makePath("usr/bin");
try std.fs.copyFileAbsolute(
exe,
try std.fs.path.join(allocator, &.{ appdir, "usr/bin", std.fs.path.basename(exe) }),
.{},
);
const cwd = std.fs.path.dirname(out) orelse {
std.log.err("Unable to obtain cwd from output filename ({s})", .{out});
return ExitCode.generic_error.to_u8();
};
const result = try std.process.Child.run(.{
.allocator = allocator,
.argv = &.{
linuxdeploy,
"--appdir",
appdir,
"--desktop-file",
desktop_entry,
"--icon-file",
icon,
"--output",
"appimage",
},
// linuxdeploy emits to cwd and seems not to have an option to set output path.
.cwd = cwd,
// linuxdeploy logs a lot
.max_output_bytes = std.math.maxInt(usize),
});
// linuxdeploy prints to stdout...
std.io.getStdErr().writeAll(result.stdout) catch {};
if (result.term.Exited != 0) {
std.log.err("linuxdeploy exited with {d}", .{result.term.Exited});
return ExitCode.generic_error.to_u8();
}
copy_appimage: {
var dir = try std.fs.openDirAbsolute(cwd, .{ .iterate = true, .access_sub_paths = false });
defer dir.close();
var iter = dir.iterate();
while (try iter.next()) |entry| {
if (entry.kind != .file) {
continue;
}
if (std.mem.endsWith(u8, entry.name, ".AppImage")) {
try dir.rename(entry.name, std.fs.path.basename(out));
break :copy_appimage;
}
}
std.log.err("linuxdeploy exited 0, but no .AppImage file were generated", .{});
return ExitCode.generic_error.to_u8();
}
return ExitCode.ok.to_u8();
}