Skip to content

Commit b96f1e7

Browse files
authored
Enable compiler.mjs to take output filename. NFC (emscripten-core#23564)
This is an internal-only change mostly useful for debugging. When the compiler is run internally we always use stdout.
1 parent 08d2cc3 commit b96f1e7

File tree

3 files changed

+23
-15
lines changed

3 files changed

+23
-15
lines changed

src/compiler.mjs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@ loadDefaultSettings();
1616
const options = {
1717
help: {type: 'boolean', short: 'h'},
1818
'symbols-only': {type: 'boolean'},
19+
output: {type: 'string', short: 'o'},
1920
};
2021
const {values, positionals} = parseArgs({options, allowPositionals: true});
2122

2223
if (values.help) {
2324
console.log(`\
2425
Main entry point for JS compiler
2526
27+
If no -o file is specified then the generated code is written to stdout.
28+
2629
Usage: compiler.mjs <settings.json> [-o out.js] [--symbols-only]`);
2730
process.exit(0);
2831
}
@@ -81,7 +84,7 @@ const jsifier = await import('./jsifier.mjs');
8184
const B = new Benchmarker();
8285

8386
try {
84-
jsifier.runJSify(symbolsOnly);
87+
await jsifier.runJSify(values.output, symbolsOnly);
8588

8689
B.print('glue');
8790
} catch (err) {

src/jsifier.mjs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// before this stage, which just does the final conversion to JavaScript.
99

1010
import assert from 'node:assert';
11+
import * as fs from 'node:fs/promises';
1112
import * as path from 'node:path';
1213
import {
1314
ATEXITS,
@@ -28,7 +29,6 @@ import {
2829
isDecorator,
2930
isJsOnlySymbol,
3031
compileTimeContext,
31-
print,
3232
printErr,
3333
readFile,
3434
warn,
@@ -164,14 +164,23 @@ function preJS() {
164164
return result;
165165
}
166166

167-
export function runJSify(symbolsOnly) {
167+
export async function runJSify(outputFile, symbolsOnly) {
168168
const libraryItems = [];
169169
const symbolDeps = {};
170170
const asyncFuncs = [];
171171
let postSets = [];
172172

173173
LibraryManager.load();
174174

175+
let outputHandle = process.stdout;
176+
if (outputFile) {
177+
outputHandle = await fs.open(outputFile, 'w');
178+
}
179+
180+
async function writeOutput(str) {
181+
await outputHandle.write(str + '\n');
182+
}
183+
175184
const symbolsNeeded = DEFAULT_LIBRARY_FUNCS_TO_INCLUDE;
176185
symbolsNeeded.push(...extraLibraryFuncs);
177186
for (const sym of EXPORTED_RUNTIME_METHODS) {
@@ -702,7 +711,7 @@ function(${args}) {
702711
}
703712

704713
function includeFile(fileName, alwaysPreprocess = true) {
705-
print(getIncludeFile(fileName, alwaysPreprocess));
714+
writeOutput(getIncludeFile(fileName, alwaysPreprocess));
706715
}
707716

708717
function finalCombiner() {
@@ -734,11 +743,11 @@ function(${args}) {
734743
includeFile(preFile);
735744

736745
for (const item of libraryItems.concat(postSets)) {
737-
print(indentify(item || '', 2));
746+
writeOutput(indentify(item || '', 2));
738747
}
739748

740749
if (PTHREADS) {
741-
print(`
750+
writeOutput(`
742751
// proxiedFunctionTable specifies the list of functions that can be called
743752
// either synchronously or asynchronously from other threads in postMessage()d
744753
// or internally queued events. This way a pthread in a Worker can synchronously
@@ -753,7 +762,7 @@ var proxiedFunctionTable = [
753762
// that we have here, together with the rest of the output
754763
// that we started to print out earlier (see comment on the
755764
// "Final shape that will be created").
756-
print('// EMSCRIPTEN_END_FUNCS\n');
765+
writeOutput('// EMSCRIPTEN_END_FUNCS\n');
757766

758767
const postFile = MINIMAL_RUNTIME ? 'postamble_minimal.js' : 'postamble.js';
759768
includeFile(postFile);
@@ -770,7 +779,7 @@ var proxiedFunctionTable = [
770779
throw Error('Aborting compilation due to previous errors');
771780
}
772781

773-
print(
782+
writeOutput(
774783
'//FORWARDED_DATA:' +
775784
JSON.stringify({
776785
librarySymbols,
@@ -789,7 +798,7 @@ var proxiedFunctionTable = [
789798
}
790799

791800
if (symbolsOnly) {
792-
print(
801+
writeOutput(
793802
JSON.stringify({
794803
deps: symbolDeps,
795804
asyncFuncs,
@@ -803,6 +812,8 @@ var proxiedFunctionTable = [
803812
if (errorOccured()) {
804813
throw Error('Aborting compilation due to previous errors');
805814
}
815+
816+
if (outputFile) await outputHandle.close();
806817
}
807818

808819
addToCompileTimeContext({

src/utility.mjs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -249,12 +249,6 @@ function read(filename) {
249249
return readFile(filename);
250250
}
251251

252-
// Anything needed by the script that we load below must be added to the
253-
// global object. These, for example, are all needed by parseTools.js.
254-
export function print(x) {
255-
process.stdout.write(x + '\n');
256-
}
257-
258252
export function printErr(x) {
259253
process.stderr.write(x + '\n');
260254
}

0 commit comments

Comments
 (0)