-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathmake.ts
More file actions
596 lines (496 loc) · 20.9 KB
/
make.ts
File metadata and controls
596 lines (496 loc) · 20.9 KB
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
/// <reference types="zx/globals" />
import { access } from 'node:fs/promises';
import { isAbsolute, resolve } from 'node:path';
import process from 'node:process';
import { js2xml } from "npm:xml-js";
import packageJson from './package.json' with { type: 'json' };
async function exists(path: string): Promise<boolean> {
try {
await access(path);
return true;
} catch {
return false;
}
}
async function applyPatches(target: string, ...patches: string[]): Promise<void> {
for (const patch of await glob(patches)) {
await $`patch -Nsp1 -d ${target} -i ${resolve(patch)}`;
}
}
function parseArgv(argv: string[]) {
return minimist(argv, {
boolean: ['enable-bootstrap', 'enable-update', 'enable-artifact-build'],
string: ['dist-dir', 'cache-dir', 'edition', 'target', 'with-buildid2', 'with-moz-build-date', 'with-dist', 'with-mozconfig', 'save-dist-host-bin', 'with-dist-host-bin'],
unknown(arg) {
if (arg.startsWith('-')) {
throw `Unknown arguments: ${arg}`
}
return true;
},
'--': true,
});
}
async function sourceMozconfigs(buildDir: string, ...mozconfigs: string[]) {
for (const mozconfig of mozconfigs) {
const path = isAbsolute(mozconfig) ? mozconfig : `$topsrcdir/${mozconfig}`;
await $`echo -e '. "'${path}'"' >> ${buildDir}/mozconfig`;
}
}
async function acAddOptions(buildDir: string, ...options: string[]) {
for (const option of options) {
await $`echo -e 'ac_add_options '${option} >> ${buildDir}/mozconfig`;
}
}
interface Config {
appName: string;
appBasename: string;
repoUrl: string;
sourcePath: string;
version: string;
firefox: string;
tmpDir: string;
distDir: string;
cacheDir: string;
sourceBasename: string;
edition: (typeof EDITIONS)[keyof typeof EDITIONS];
basename: string;
basenameRuntime: string;
target: (typeof TARGETS)[keyof typeof TARGETS];
enableBootstrap: boolean;
enableUpdate: boolean;
enableArtifactBuild: boolean;
withMozBuildDate: string | null;
withBuildID2: string | null;
withDist: string | null;
withMozconfig: string | null;
saveDistHostBin: string | null;
withDistHostBin: string | null;
}
async function getFirefoxSource(config: Config): Promise<string> {
const { firefox, cacheDir } = config;
const source = `firefox-${firefox}.source.tar.xz`;
const tarball = `${cacheDir}/${source}`;
if (!await exists(tarball)) {
await $`curl -fL https://archive.mozilla.org/pub/firefox/releases/${firefox}/source/${source} -o ${tarball}`
}
return tarball;
}
async function getFiredragonRuntime(config: Config, withUpdateFrameworkArtifacts: boolean = false): Promise<string> {
const { repoUrl, version, distDir, cacheDir, edition, basenameRuntime, target } = config;
const files = [
{
filename: `${basenameRuntime}-${target.buildSuffix}.${target.buildRuntimeOutputFormat}`,
url: `${repoUrl}/-/releases/v${version}/downloads/${edition.basename}-runtime-${target.buildSuffix}.${target.buildRuntimeOutputFormat}`,
},
];
if (withUpdateFrameworkArtifacts) {
files.push({
filename: `${basenameRuntime}-${target.buildSuffix}.update_framework_artifacts.zip`,
url: `${repoUrl}/-/releases/v${version}/downloads/${edition.basename}-runtime-${target.buildSuffix}.update_framework_artifacts.zip`,
});
}
const output = [];
for (const file of files) {
if (await exists(`${distDir}/${file.filename}`)) {
output.push(`${distDir}/${file.filename}`);
} else {
if (!await exists(`${cacheDir}/${file.filename}`)) {
await $`curl -fL ${file.url} -o ${cacheDir}/${file.filename}`;
}
output.push(`${cacheDir}/${file.filename}`);
}
}
return output.join(':');
}
async function prepareSource(config: Config, dir: string): Promise<void> {
const { sourcePath, version } = config;
// Extract floorp runtime and inject this repo
const runtimeTarball = await getFirefoxSource(config);
await $`mkdir ${dir}`;
await $`tar -xf ${runtimeTarball} --strip-components=1 -C ${dir}`;
await $`rsync -a --exclude=/.git --filter=':- .gitignore' ./ ${dir}/${sourcePath}/`;
// Copy branding
await $`cp -r gecko/branding/* ${dir}/browser/branding/`;
// Set display version
await $`echo -e ${version} > ${dir}/browser/config/version_display.txt`;
await applyPatches(dir, `patches/**/*.patch`);
}
async function extractSource(config: Config, buildDir: string): Promise<void> {
const { distDir, sourceBasename } = config;
const sourceTarball = `${distDir}/${sourceBasename}.tar.zst`;
if (!await exists(sourceTarball)) {
await source(config);
}
// Extract source
await $`mkdir ${buildDir}`;
await $`tar -xf ${sourceTarball} --strip-components=1 -C ${buildDir}`;
}
async function prepareBuild(config: Config, buildDir: string) {
const { sourcePath, edition, target, withBuildID2, withMozconfig } = config;
// Install deno dependencies
await $`cd ${buildDir}/${sourcePath} && deno install --allow-scripts --frozen`;
// Ensure buildid2 exists
if (withBuildID2) {
await $`mkdir -p ${buildDir}/${sourcePath}/_dist`;
await $`cat ${withBuildID2} > ${buildDir}/${sourcePath}/_dist/buildid2`;
} else if (!await exists(`${buildDir}/${sourcePath}/_dist/buildid2`)) {
await $`cd ${buildDir}/${sourcePath} && deno task build --write-buildid2`;
}
// Combine mozconfigs
await sourceMozconfigs(buildDir, edition.mozconfig, target.mozconfig);
if (withMozconfig) {
await sourceMozconfigs(buildDir, resolve(withMozconfig))
}
}
async function doBuild(config: Config, buildDir: string) {
const { enableBootstrap, enableArtifactBuild, withMozBuildDate } = config;
// Potentially set MOZ_BUILD_DATE
if (withMozBuildDate) {
const moz_build_date = (await Deno.readTextFile(withMozBuildDate)).trim();
Deno.env.set('MOZ_BUILD_DATE', moz_build_date);
}
// Potentially run bootstrap
if (enableBootstrap) {
await $`cd ${buildDir} && ./mach --no-interactive bootstrap --application-choice ${enableArtifactBuild ? 'browser_artifact_mode' : 'browser'}`;
await acAddOptions(buildDir, '--enable-bootstrap');
} else {
await acAddOptions(buildDir, '--disable-bootstrap');
}
// Run configure
await $`${buildDir}/mach configure`;
// Run build
await $`${buildDir}/mach build`;
}
function getCommonBuildDirs(config: Config, buildDir: string): { objDistDir: string, objDistBinDir: string } {
const { target } = config;
const objDistDir = `${buildDir}/obj-artifact-build-output/dist`;
const objDistBinDir = `${objDistDir}/${target.objDistBinPath}`;
return {
objDistDir,
objDistBinDir,
};
}
async function cloneObjDistBin(config: Config, buildDir: string) {
const { objDistBinDir } = getCommonBuildDirs(config, buildDir);
// Resolve symlinks (https://www.spinics.net/lists/git/msg391750.html)
const objDistBinTmpDir = tmpdir();
await $`rsync -aL ${objDistBinDir}/ ${objDistBinTmpDir}/`;
await $`rm -rf ${objDistBinDir}`;
await $`mv ${objDistBinTmpDir} ${objDistBinDir}`;
}
async function packageBuild(config: Config, outputFormat: string, buildBasename: string, buildDir: string) {
const { distDir, saveDistHostBin } = config;
const { objDistDir, objDistBinDir } = getCommonBuildDirs(config, buildDir);
// Remove references to the build directory
for (const file of await $`rg -Fl ${buildDir} ${objDistBinDir}`.nothrow().lines()) {
await $`sed -i 's#${buildDir}##g' ${file}`;
}
// Run package
await $`${buildDir}/mach package`;
// Move package to dist dir
let packageName = (await $`cat ${objDistDir}/package_name.txt`.lines())[0];
if (outputFormat === 'exe') {
packageName = packageName.replace(/.zip$/, '.installer.exe');
}
await $`mv ${objDistDir}/${packageName} ${distDir}/${buildBasename}.${outputFormat}`;
if (outputFormat === 'dmg') {
const updateFrameworkArtifacts = packageName.replace(/.dmg$/, '.update_framework_artifacts.zip');
await $`mv ${objDistDir}/${updateFrameworkArtifacts} ${distDir}/${buildBasename}.update_framework_artifacts.zip`;
}
// Save dist/host/bin
if (saveDistHostBin) {
await $`tar --zstd -cf ${saveDistHostBin} -C ${objDistDir} host/bin`;
}
}
function getUpdateUrls(config: Config) {
const { repoUrl, version, edition, target } = config;
return {
updateXml: `${repoUrl}/-/releases/permalink/latest/downloads/${edition.basename}-${target.buildSuffix}.update.xml`,
mar: `${repoUrl}/-/releases/v${version}/downloads/${edition.basename}-${target.buildSuffix}.mar`,
details: `${repoUrl}/-/releases/v${version}`,
};
}
async function createUpdate(config: Config, buildBasename: string, buildDir: string) {
const { version, distDir, target, withDistHostBin } = config;
const { objDistDir, objDistBinDir } = getCommonBuildDirs(config, buildDir);
// Use provided dist/host/bin
if (withDistHostBin) {
await $`tar -xf ${withDistHostBin} -C ${objDistDir}`;
}
const appVersion = (await $`cat ${buildDir}/browser/config/version.txt`).lines()[0];
await $`MAR=${objDistDir}/host/bin/mar MOZ_PRODUCT_VERSION=${appVersion} MAR_CHANNEL_ID=release ${buildDir}/tools/update-packaging/make_full_update.sh ${distDir}/${buildBasename}.mar ${objDistDir}/${target.updatePath}`;
const [
buildID,
buildID2,
hashValue,
size,
] = await Promise.all([
(async () => (await $`awk -F '=' '/BuildID/ {print $2}' ${objDistBinDir}/application.ini`).lines()[0])(),
(async () => (await $`cat ${objDistBinDir}/browser/buildid2`).lines()[0])(),
(async () => (await $`sha512sum ${distDir}/${buildBasename}.mar | cut -c 1-128`).lines()[0])(),
(async () => (await $`stat -c '%s' ${distDir}/${buildBasename}.mar`).lines()[0])(),
]);
const { mar, details } = getUpdateUrls(config);
const update = {
_declaration: {
_attributes: {
version: "1.0",
encoding: "UTF-8",
},
},
updates: {
update: {
_attributes: {
type: 'minor',
displayVersion: version,
appVersion: appVersion,
platformVersion: appVersion,
buildID: buildID,
appVersion2: version,
buildID2: buildID2,
detailsURL: details,
},
patch: [
{
_attributes: {
type: "complete",
URL: mar,
hashFunction: 'sha512',
hashValue: hashValue,
size: size,
},
},
],
},
},
};
await $`echo -e ${js2xml(update, { compact: true, spaces: 4 })} > ${distDir}/${buildBasename}.update.xml`;
}
async function source(config: Config) {
const { tmpDir, distDir, sourceBasename } = config;
await prepareSource(config, `${tmpDir}/${sourceBasename}`);
await $`tar --zstd -cf ${distDir}/${sourceBasename}.tar.zst -C ${tmpDir} ${sourceBasename}`;
}
async function buildRuntime(config: Config) {
const { tmpDir, basenameRuntime, target } = config;
const buildDevBasename = `${basenameRuntime}-${target.buildSuffix}`
const buildDevDir = `${tmpDir}/${buildDevBasename}`;
await extractSource(config, buildDevDir);
await prepareBuild(config, buildDevDir);
await acAddOptions(buildDevDir, '--enable-chrome-format=flat', `--enable-firedragon-debug`);
await doBuild(config, buildDevDir);
await cloneObjDistBin(config, buildDevDir);
await packageBuild(config, target.buildRuntimeOutputFormat, buildDevBasename, buildDevDir);
}
async function build(config: Config) {
const { sourcePath, tmpDir, basename, target, enableUpdate, enableArtifactBuild, withDist } = config;
const buildBasename = `${basename}-${target.buildSuffix}`;
const buildDir = `${tmpDir}/${buildBasename}`
await extractSource(config, buildDir);
await prepareBuild(config, buildDir);
// Use provided dist or run release build before
if (withDist) {
await $`cp -r ${withDist}/noraneko ${buildDir}/${sourcePath}/_dist/noraneko`;
} else {
await $`cd ${buildDir}/${sourcePath} && NODE_ENV=production deno task build --release-build-before`;
}
// Set noraneko dist
await acAddOptions(buildDir, `--with-noraneko-dist=${sourcePath}/_dist/noraneko`, '--enable-firedragon-settings');
if (enableUpdate) {
await acAddOptions(buildDir, `--with-firedragon-update=${getUpdateUrls(config).updateXml}`);
} else {
await acAddOptions(buildDir, '--disable-updater');
}
if (enableArtifactBuild) {
await acAddOptions(buildDir, '--enable-artifact-builds');
const firedragonRuntime = await getFiredragonRuntime(config, target.buildOutputFormat === 'dmg');
Deno.env.set('MOZ_ARTIFACT_FILE', firedragonRuntime.split(':').map((path) => resolve(path)).join(':'));
// Remove mozconfig lines tagged with #[NotOnArtifactBuild]
for (const file of await $`rg -Fl '#[NotOnArtifactBuild]' ${buildDir}/${sourcePath}/gecko/mozconfigs`.nothrow().lines()) {
await $`sed -i '/#\\[NotOnArtifactBuild\\]/d' ${file}`;
}
}
await doBuild(config, buildDir);
// Run release build after
await $`cd ${buildDir}/${sourcePath} && deno task build --release-build-after`;
await packageBuild(config, target.buildOutputFormat, buildBasename, buildDir);
if (enableUpdate) {
await createUpdate(config, buildBasename, buildDir);
}
}
async function appimage(config: Config) {
const { appName, tmpDir, distDir, basename, target } = config;
if (!target.appimageSuffix) {
throw `Target does not support appimage build.`;
}
const appimageBasename = `${basename}-${target.appimageSuffix}`;
const appimageDir = `${tmpDir}/${appimageBasename}`;
const buildTarball = `${distDir}/${basename}-${target.buildSuffix}.${target.buildOutputFormat}`;
if (!await exists(buildTarball)) {
await build(config);
}
// Extract binary tarball
await $`mkdir ${appimageDir}`;
await $`tar -xf ${buildTarball} --strip-components=1 -C ${appimageDir}`;
// Copy desktop and logo
await $`sed 's#/usr/lib/${appName}/${appName}#${appName}#' assets/${appName}.desktop > ${appimageDir}/${appName}.desktop`;
await $`cp ${appimageDir}/browser/chrome/icons/default/default128.png ${appimageDir}/${appName}.png`;
// Copy AppRun and make executable
await $`cp assets/AppRun ${appimageDir}/AppRun`;
await $`chmod a+x ${appimageDir}/AppRun`;
// Download appimagetool and make executable
await $`curl -L https://github.com/AppImage/appimagetool/releases/download/continuous/appimagetool-x86_64.AppImage -o ${tmpDir}/appimagetool-x86_64.AppImage`;
await $`chmod a+x ${tmpDir}/appimagetool-x86_64.AppImage`;
// Make AppImage
await $`${tmpDir}/appimagetool-x86_64.AppImage ${appimageDir} ${distDir}/${appimageBasename}.AppImage`;
}
const APP_NAME = 'firedragon';
const APP_BASENAME = 'FireDragon';
const REPO_URL = 'https://gitlab.com/garuda-linux/firedragon/firedragon12';
const SOURCE_PATH = 'firedragon';
const EDITIONS = {
dr460nized: {
mozconfig: `${SOURCE_PATH}/gecko/mozconfigs/edition/firedragon-dr460nized.mozconfig`,
branding: 'firedragon',
basename: APP_NAME,
},
catppuccin: {
mozconfig: `${SOURCE_PATH}/gecko/mozconfigs/edition/firedragon-catppuccin.mozconfig`,
branding: 'firedragon-catppuccin',
basename: `${APP_NAME}-catppuccin`,
},
};
const TARGETS = {
'linux-x64': {
mozconfig: `${SOURCE_PATH}/gecko/mozconfigs/target/linux-x64.mozconfig`,
objDistBinPath: 'bin',
buildSuffix: 'linux-x64',
buildOutputFormat: 'tar.xz',
buildRuntimeOutputFormat: 'tar.xz',
appimageSuffix: 'appimage-x64',
updatePath: APP_NAME,
},
'linux-arm64': {
mozconfig: `${SOURCE_PATH}/gecko/mozconfigs/target/linux-arm64.mozconfig`,
objDistBinPath: 'bin',
buildSuffix: 'linux-arm64',
buildOutputFormat: 'tar.xz',
buildRuntimeOutputFormat: 'tar.xz',
appimageSuffix: 'appimage-arm64',
updatePath: APP_NAME,
},
'win32-x64': {
mozconfig: `${SOURCE_PATH}/gecko/mozconfigs/target/win32-x64.mozconfig`,
objDistBinPath: 'bin',
buildSuffix: 'win32-x64',
buildOutputFormat: 'exe',
buildRuntimeOutputFormat: 'zip',
appimageSuffix: null,
updatePath: APP_NAME,
},
'win32-arm64': {
mozconfig: `${SOURCE_PATH}/gecko/mozconfigs/target/win32-arm64.mozconfig`,
objDistBinPath: 'bin',
buildSuffix: 'win32-arm64',
buildOutputFormat: 'exe',
buildRuntimeOutputFormat: 'zip',
appimageSuffix: null,
updatePath: APP_NAME,
},
'darwin-x64': {
mozconfig: `${SOURCE_PATH}/gecko/mozconfigs/target/darwin-x64.mozconfig`,
objDistBinPath: `${APP_BASENAME}.app/Contents/Resources`,
buildSuffix: 'darwin-x64',
buildOutputFormat: 'dmg',
buildRuntimeOutputFormat: 'dmg',
appimageSuffix: null,
updatePath: `${APP_NAME}/${APP_BASENAME}.app`,
},
'darwin-arm64': {
mozconfig: `${SOURCE_PATH}/gecko/mozconfigs/target/darwin-arm64.mozconfig`,
objDistBinPath: `${APP_BASENAME}.app/Contents/Resources`,
buildSuffix: 'darwin-arm64',
buildOutputFormat: 'dmg',
buildRuntimeOutputFormat: 'dmg',
appimageSuffix: null,
updatePath: `${APP_NAME}/${APP_BASENAME}.app`,
},
};
let tmpDir;
try {
tmpDir = tmpdir();
echo(`Using temporary directory: ${tmpDir}`);
await $`mkdir -p ${tmpDir}`;
let argv = parseArgv(process.argv.slice(4));
while (true) {
const distDir = argv['dist-dir'] ?? '.dist';
if (!await exists(distDir)) {
await $`mkdir -p ${distDir}`;
}
const cacheDir = argv['cache-dir'] ?? '.cache';
if (!await exists(cacheDir)) {
await $`mkdir -p ${cacheDir}`;
}
const edition = EDITIONS[(argv['edition'] ?? 'dr460nized') as keyof typeof EDITIONS];
if (!edition) {
throw `Unsupported edition ${argv['edition']}, must be one of [${Object.keys(EDITIONS).join(', ')}].`;
}
const target = TARGETS[(argv['target'] ?? `${process.platform}-${process.arch}`) as keyof typeof TARGETS];
if (!target){
throw `Unsupported target ${argv['target']}, must be one of [${Object.keys(TARGETS).join(', ')}].`;
}
const { version } = packageJson;
const sourceBasename = `${APP_NAME}-source-v${version}`;
const basename = `${edition.basename}-v${version}`;
const basenameRuntime = `${edition.basename}-runtime-v${version}`;
const config: Config = {
appName: APP_NAME,
appBasename: APP_BASENAME,
repoUrl: REPO_URL,
sourcePath: SOURCE_PATH,
version,
firefox: packageJson.firefox,
tmpDir,
distDir,
cacheDir,
sourceBasename,
edition,
basename,
basenameRuntime,
target,
enableBootstrap: argv['enable-bootstrap'] ?? false,
enableUpdate: argv['enable-update'] ?? false,
enableArtifactBuild: argv['enable-artifact-build'] ?? false,
withMozBuildDate: argv['with-moz-build-date'] ?? null,
withBuildID2: argv['with-buildid2'] ?? null,
withDist: argv['with-dist'] ?? null,
withMozconfig: argv['with-mozconfig'] ?? null,
saveDistHostBin: argv['save-dist-host-bin'] ?? null,
withDistHostBin: argv['with-dist-host-bin'] ?? null,
};
for (const command of argv._) {
switch (command) {
case 'source':
await source(config);
break;
case 'build-runtime':
await buildRuntime(config);
break;
case 'build':
await build(config);
break;
case 'appimage':
await appimage(config);
break;
default:
throw `Unsupported command ${command}, must be one of [source, build-runtime, build, appimage]`;
}
}
if (!argv['--']?.length) {
break;
}
argv = parseArgv(argv['--']);
}
} finally {
await $`rm -rf ${tmpDir}`;
}