sea-targets.mjs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. import { resolve } from "node:path";
  2. export const supportedTargets = Object.freeze([
  3. "darwin-arm64",
  4. "darwin-x64",
  5. "linux-arm64",
  6. "linux-x64",
  7. "win-arm64",
  8. "win-x64",
  9. ]);
  10. const supportedTargetSet = new Set(supportedTargets);
  11. export const hostTarget = ({ arch = process.arch, platform = process.platform } = {}) =>
  12. `${platform === "win32" ? "win" : platform}-${arch}`;
  13. export const normalizeTarget = (target) => {
  14. const normalized = target.trim().toLowerCase();
  15. const aliases = {
  16. "win32-arm64": "win-arm64",
  17. "win32-x64": "win-x64",
  18. "windows-arm64": "win-arm64",
  19. "windows-x64": "win-x64",
  20. };
  21. return aliases[normalized] ?? normalized;
  22. };
  23. export const assertSupportedTarget = (target) => {
  24. if (!supportedTargetSet.has(target)) {
  25. throw new Error(
  26. `Unsupported SEA target "${target}". Supported targets: ${supportedTargets.join(", ")}`,
  27. );
  28. }
  29. };
  30. export const targetParts = (target) => {
  31. assertSupportedTarget(target);
  32. const [releasePlatform, arch] = target.split("-");
  33. return {
  34. arch,
  35. // Keep Windows release artifacts compatible with downstream release downloaders,
  36. // which request zcode-windows-<arch>.exe while internal target keys stay win/win32.
  37. outputPlatform: releasePlatform === "win" ? "windows" : releasePlatform,
  38. releasePlatform,
  39. };
  40. };
  41. export const outputBinaryName = (target) => {
  42. const { arch, outputPlatform, releasePlatform } = targetParts(target);
  43. const extension = releasePlatform === "win" ? ".exe" : "";
  44. return `zcode-${outputPlatform}-${arch}${extension}`;
  45. };
  46. export const nodeReleaseArtifact = (target, nodeVersion) => {
  47. const { arch, releasePlatform } = targetParts(target);
  48. if (releasePlatform === "win") {
  49. return `${releasePlatform}-${arch}/node.exe`;
  50. }
  51. const extension = releasePlatform === "linux" ? "tar.xz" : "tar.gz";
  52. return `node-v${nodeVersion}-${releasePlatform}-${arch}.${extension}`;
  53. };
  54. export const nodeReleaseUrl = (target, nodeVersion) =>
  55. new URL(nodeReleaseArtifact(target, nodeVersion), `https://nodejs.org/dist/v${nodeVersion}/`)
  56. .href;
  57. export const isHostTarget = (target, { arch = process.arch, platform = process.platform } = {}) =>
  58. normalizeTarget(target) === hostTarget({ arch, platform });
  59. export const postjectArgsForTarget = ({ binaryPath, seaBlob, sentinelFuse, target }) => {
  60. const { outputPlatform } = targetParts(target);
  61. const args = [binaryPath, "NODE_SEA_BLOB", seaBlob, "--sentinel-fuse", sentinelFuse];
  62. if (outputPlatform === "darwin") {
  63. args.push("--macho-segment-name", "NODE_SEA");
  64. }
  65. return args;
  66. };
  67. export const shouldAdHocSignMacTarget = (target, { platform = process.platform } = {}) => {
  68. const { outputPlatform } = targetParts(target);
  69. return outputPlatform === "darwin" && platform === "darwin";
  70. };
  71. export const adHocCodesignArgs = (binaryPath) => ["--force", "--sign", "-", binaryPath];
  72. export const parseBuildSeaArgs = (
  73. argv,
  74. { hostArch = process.arch, hostPlatform = process.platform } = {},
  75. ) => {
  76. const selectedTargets = [];
  77. const nodeBinaries = {};
  78. let includeAll = false;
  79. let sawTargetSelector = false;
  80. let help = false;
  81. const readValue = (arg, index) => {
  82. if (arg.includes("=")) {
  83. return {
  84. nextIndex: index,
  85. value: arg.slice(arg.indexOf("=") + 1),
  86. };
  87. }
  88. const value = argv[index + 1];
  89. if (!value || value.startsWith("--")) {
  90. throw new Error(`Missing value for ${arg}`);
  91. }
  92. return {
  93. nextIndex: index + 1,
  94. value,
  95. };
  96. };
  97. const addTargetList = (value) => {
  98. let added = false;
  99. for (const rawTarget of value.split(",")) {
  100. const target = normalizeTarget(rawTarget);
  101. if (!target) continue;
  102. assertSupportedTarget(target);
  103. selectedTargets.push(target);
  104. added = true;
  105. }
  106. if (!added) {
  107. throw new Error("Missing SEA target value");
  108. }
  109. };
  110. for (let index = 0; index < argv.length; index += 1) {
  111. const arg = argv[index];
  112. if (arg === "--") continue;
  113. if (arg === "--help" || arg === "-h") {
  114. help = true;
  115. continue;
  116. }
  117. if (arg === "--all") {
  118. sawTargetSelector = true;
  119. includeAll = true;
  120. continue;
  121. }
  122. if (arg === "--target" || arg.startsWith("--target=")) {
  123. sawTargetSelector = true;
  124. const { nextIndex, value } = readValue(arg, index);
  125. addTargetList(value);
  126. index = nextIndex;
  127. continue;
  128. }
  129. if (arg === "--targets" || arg.startsWith("--targets=")) {
  130. sawTargetSelector = true;
  131. const { nextIndex, value } = readValue(arg, index);
  132. addTargetList(value);
  133. index = nextIndex;
  134. continue;
  135. }
  136. if (arg === "--node-binary" || arg.startsWith("--node-binary=")) {
  137. const { nextIndex, value } = readValue(arg, index);
  138. const separatorIndex = value.indexOf("=");
  139. if (separatorIndex <= 0) {
  140. throw new Error(
  141. "Expected --node-binary to use <target>=<path>, for example linux-x64=/opt/node/bin/node",
  142. );
  143. }
  144. const target = normalizeTarget(value.slice(0, separatorIndex));
  145. const binaryPath = value.slice(separatorIndex + 1);
  146. assertSupportedTarget(target);
  147. if (!binaryPath) {
  148. throw new Error(`Missing path for --node-binary ${target}`);
  149. }
  150. nodeBinaries[target] = resolve(binaryPath);
  151. index = nextIndex;
  152. continue;
  153. }
  154. throw new Error(`Unknown option "${arg}". Run with --help for usage.`);
  155. }
  156. let targets = [];
  157. if (includeAll) targets.push(...supportedTargets);
  158. targets.push(...selectedTargets);
  159. if (targets.length === 0) {
  160. const nodeBinaryTargets = Object.keys(nodeBinaries);
  161. targets = sawTargetSelector
  162. ? []
  163. : nodeBinaryTargets.length > 0
  164. ? nodeBinaryTargets
  165. : [hostTarget({ arch: hostArch, platform: hostPlatform })];
  166. }
  167. targets = [...new Set(targets)];
  168. for (const target of targets) assertSupportedTarget(target);
  169. return {
  170. help,
  171. nodeBinaries,
  172. targets,
  173. };
  174. };