@tsplc/build 0.2.16 → 0.2.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli.js +628 -352
- package/dist/cli.js.map +30 -28
- package/dist/index.js +548 -387
- package/dist/index.js.map +27 -25
- package/dist/libraries.js +68 -27
- package/dist/libraries.js.map +3 -3
- package/docker/Dockerfile +2 -0
- package/docker/Dockerfile.dockerignore +4 -1
- package/docker/Dockerfile.source +3 -0
- package/docker/config.ts +10 -5
- package/docker/runner.ts +140 -127
- package/package.json +54 -53
- package/python/build.py +183 -124
- package/python/codesys_online.py +44 -45
- package/python/hardware.py +9 -9
- package/python/test_build.py +3 -3
- package/python/test_codesys_online.py +18 -12
- package/python/test_installer_packages.py +1 -0
- package/python/worker.py +11 -7
- package/src/artifacts.ts +82 -60
- package/src/build.ts +71 -62
- package/src/cli.ts +109 -100
- package/src/controller-online.ts +400 -425
- package/src/controller-protocol.ts +140 -0
- package/src/controller-session.ts +466 -397
- package/src/device-tree.ts +19 -15
- package/src/diagnostics.ts +59 -49
- package/src/docker-build.ts +217 -156
- package/src/docker-context.ts +49 -27
- package/src/docker-run.ts +137 -111
- package/src/engineering-settings.ts +42 -0
- package/src/host/codesys-command.ts +35 -34
- package/src/host/codesys-host.ts +157 -129
- package/src/host/codesys-worker.ts +318 -301
- package/src/host/windows-path.ts +63 -59
- package/src/import-request.ts +24 -0
- package/src/index.ts +37 -31
- package/src/io.ts +152 -116
- package/src/libraries.ts +214 -146
- package/src/package-path.ts +3 -1
- package/src/run.ts +132 -108
- package/src/runtime-status.ts +25 -19
- package/src/sources.ts +264 -225
- package/src/st/declarations.ts +181 -193
- package/src/st/lexer.ts +102 -118
- package/src/st/parse.ts +193 -171
- package/src/st/reader.ts +47 -42
- package/src/__tests__/fixtures/xml/fb-simple.xml +0 -40
- package/src/__tests__/fixtures/xml/fb-state-machine.xml +0 -86
- package/src/__tests__/fixtures/xml/prg-cyclic.xml +0 -44
- package/src/config.ts +0 -12
package/src/host/codesys-host.ts
CHANGED
|
@@ -4,142 +4,170 @@ import { access, open, readFile, rm, writeFile } from "node:fs/promises";
|
|
|
4
4
|
import { delimiter, resolve } from "node:path";
|
|
5
5
|
import { promisify } from "node:util";
|
|
6
6
|
import { z } from "zod";
|
|
7
|
-
import type { ResolvedConfig } from "
|
|
8
|
-
import {
|
|
7
|
+
import type { ResolvedConfig } from "@tsplc/config/build";
|
|
8
|
+
import {
|
|
9
|
+
codesysCommand,
|
|
10
|
+
codesysCommandFilename,
|
|
11
|
+
getHostCommand,
|
|
12
|
+
} from "./codesys-command";
|
|
9
13
|
import { toWindowsPath } from "./windows-path";
|
|
10
14
|
|
|
11
15
|
const execute = promisify(execFile);
|
|
12
16
|
const exitSchema = z.object({
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
17
|
+
pid: z.int().positive(),
|
|
18
|
+
code: z.int().nullable(),
|
|
19
|
+
signal: z.string().nullable(),
|
|
16
20
|
});
|
|
17
21
|
|
|
18
22
|
export const createCodesysHost = (config: ResolvedConfig) => {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
23
|
+
const native = process.platform === "win32";
|
|
24
|
+
const { codesys, timeouts } = config;
|
|
25
|
+
const hostEnv = {
|
|
26
|
+
// Child processes inherit OS settings such as PATH and DISPLAY.
|
|
27
|
+
...process.env,
|
|
28
|
+
WINEPREFIX: codesys.wine?.prefix,
|
|
29
|
+
WINEDEBUG: codesys.wine?.debug,
|
|
30
|
+
CODESYS_BUILD_NATIVE_EXIT: String(codesys.wine?.nativeExit ?? false),
|
|
31
|
+
};
|
|
28
32
|
|
|
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
|
-
|
|
33
|
+
/** Start the headless worker script; its launcher outlives this CLI process. */
|
|
34
|
+
const startWorker = async (directory: string) => {
|
|
35
|
+
if (!native) {
|
|
36
|
+
await execute(codesys.wine?.binary ?? "wine", ["--version"], {
|
|
37
|
+
env: hostEnv,
|
|
38
|
+
timeout: timeouts.hostCommand,
|
|
39
|
+
}).catch((cause: unknown) => {
|
|
40
|
+
throw new Error(
|
|
41
|
+
"Wine is missing. Install Wine; Winetricks alone cannot run CODESYS",
|
|
42
|
+
{
|
|
43
|
+
cause,
|
|
44
|
+
},
|
|
45
|
+
);
|
|
46
|
+
});
|
|
47
|
+
const candidates = (process.env["PATH"] ?? "").split(delimiter);
|
|
48
|
+
const available = await Promise.all(
|
|
49
|
+
candidates.map((path) =>
|
|
50
|
+
access(resolve(path, "script"), constants.X_OK).then(
|
|
51
|
+
() => true,
|
|
52
|
+
() => false,
|
|
53
|
+
),
|
|
54
|
+
),
|
|
55
|
+
);
|
|
56
|
+
if (!available.some(Boolean)) {
|
|
57
|
+
throw new Error(
|
|
58
|
+
"Headless Wine builds require the script utility (install util-linux on Linux)",
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
await writeFile(resolve(directory, codesysCommandFilename), codesysCommand);
|
|
63
|
+
const env = {
|
|
64
|
+
...hostEnv,
|
|
65
|
+
CODESYS_BUILD_SCRIPT: await toWindowsPath(
|
|
66
|
+
resolve(directory, "worker.py"),
|
|
67
|
+
codesys.wine,
|
|
68
|
+
),
|
|
69
|
+
CODESYS_BUILD_SESSION: await toWindowsPath(directory, codesys.wine),
|
|
70
|
+
CODESYS_BUILD_EXE: await toWindowsPath(codesys.executable, codesys.wine),
|
|
71
|
+
CODESYS_BUILD_PROFILE: codesys.profile,
|
|
72
|
+
};
|
|
73
|
+
const launcher = getHostCommand(codesys);
|
|
74
|
+
const logPath = resolve(directory, "codesys.log");
|
|
75
|
+
const exitPath = resolve(directory, "launcher-exit.json");
|
|
76
|
+
await rm(exitPath, { force: true });
|
|
77
|
+
const log = await open(logPath, "w");
|
|
78
|
+
try {
|
|
79
|
+
const child = spawn(launcher.executable, launcher.args, {
|
|
80
|
+
cwd: directory,
|
|
81
|
+
detached: true,
|
|
82
|
+
windowsHide: true,
|
|
83
|
+
env,
|
|
84
|
+
stdio: ["ignore", log.fd, log.fd],
|
|
85
|
+
});
|
|
86
|
+
child.once("exit", (code, signal) => {
|
|
87
|
+
try {
|
|
88
|
+
appendFileSync(
|
|
89
|
+
logPath,
|
|
90
|
+
`\nCODESYS launcher exit status: ${signal ?? code}\n`,
|
|
91
|
+
);
|
|
92
|
+
} catch {
|
|
93
|
+
// A failed diagnostic write must preserve the original process failure.
|
|
94
|
+
}
|
|
95
|
+
try {
|
|
96
|
+
const temporary = `${exitPath}.${child.pid}.tmp`;
|
|
97
|
+
writeFileSync(
|
|
98
|
+
temporary,
|
|
99
|
+
JSON.stringify({ pid: child.pid, code, signal }),
|
|
100
|
+
);
|
|
101
|
+
renameSync(temporary, exitPath);
|
|
102
|
+
} catch {
|
|
103
|
+
// The log still records the exit if the optional status file cannot be published.
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
await new Promise<void>((resolve, reject) => {
|
|
107
|
+
child.once("error", reject);
|
|
108
|
+
child.once("spawn", resolve);
|
|
109
|
+
});
|
|
110
|
+
if (!child.pid) {
|
|
111
|
+
throw new Error("CODESYS launcher has no process ID");
|
|
112
|
+
}
|
|
113
|
+
child.unref();
|
|
114
|
+
return child.pid;
|
|
115
|
+
} finally {
|
|
116
|
+
await log.close();
|
|
117
|
+
}
|
|
118
|
+
};
|
|
103
119
|
|
|
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
|
-
|
|
120
|
+
return {
|
|
121
|
+
exitStatus: async (directory: string, pid: number) => {
|
|
122
|
+
const path = resolve(directory, "launcher-exit.json");
|
|
123
|
+
const value = await readFile(path, "utf8").catch((error: unknown) => {
|
|
124
|
+
if (
|
|
125
|
+
error instanceof Error &&
|
|
126
|
+
"code" in error &&
|
|
127
|
+
error.code === "ENOENT"
|
|
128
|
+
) {
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
throw error;
|
|
132
|
+
});
|
|
133
|
+
if (value === undefined) {
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
const status = exitSchema.parse(JSON.parse(value));
|
|
137
|
+
if (status.pid !== pid) {
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
if (!native && status.code === 0 && status.signal === null) {
|
|
141
|
+
const logPath = resolve(directory, "codesys.log");
|
|
142
|
+
const log = (await readFile(logPath, "utf8"))
|
|
143
|
+
// Terminal color escapes must not hide a native assertion signature.
|
|
144
|
+
// eslint-disable-next-line no-control-regex
|
|
145
|
+
.replace(/\u001b\[[0-?]*[ -/]*[@-~]/g, "")
|
|
146
|
+
.replace(/\r/g, "");
|
|
147
|
+
// A Wine helper can abort under Rosetta while the main launcher returns 0.
|
|
148
|
+
// That command still failed; publishing its artifacts would hide the fault.
|
|
149
|
+
const assertion =
|
|
150
|
+
/^assertion failed \[[^\n]+\]:[^\n]*\n[ \t]*\(ThreadContext[^\n]*\)/m.exec(
|
|
151
|
+
log,
|
|
152
|
+
);
|
|
153
|
+
if (assertion) {
|
|
154
|
+
throw new Error(
|
|
155
|
+
`Wine translation aborted: ${assertion[0].replace(/\s+/g, " ")}. See ${logPath}`,
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return status;
|
|
160
|
+
},
|
|
161
|
+
startWorker,
|
|
162
|
+
terminate: async (pid: number) => {
|
|
163
|
+
if (native) {
|
|
164
|
+
await execute("taskkill.exe", ["/pid", String(pid), "/t", "/f"], {
|
|
165
|
+
env: hostEnv,
|
|
166
|
+
timeout: timeouts.hostCommand,
|
|
167
|
+
});
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
process.kill(-pid, "SIGKILL");
|
|
171
|
+
},
|
|
172
|
+
};
|
|
145
173
|
};
|