sea-targets.mjs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. // 平台参数只筛选,见 apps/zcode-cli/docs/design/v2/sea-cross-target-packaging.md「用户接口」。
  79. const selectedPlatforms = new Set();
  80. let includeAll = false;
  81. let sawTargetSelector = false;
  82. let help = false;
  83. const readValue = (arg, index) => {
  84. if (arg.includes("=")) {
  85. return {
  86. nextIndex: index,
  87. value: arg.slice(arg.indexOf("=") + 1),
  88. };
  89. }
  90. const value = argv[index + 1];
  91. if (!value || value.startsWith("--")) {
  92. throw new Error(`Missing value for ${arg}`);
  93. }
  94. return {
  95. nextIndex: index + 1,
  96. value,
  97. };
  98. };
  99. const addTargetList = (value) => {
  100. let added = false;
  101. for (const rawTarget of value.split(",")) {
  102. const target = normalizeTarget(rawTarget);
  103. if (!target) continue;
  104. assertSupportedTarget(target);
  105. selectedTargets.push(target);
  106. added = true;
  107. }
  108. if (!added) {
  109. throw new Error("Missing SEA target value");
  110. }
  111. };
  112. for (let index = 0; index < argv.length; index += 1) {
  113. const arg = argv[index];
  114. if (arg === "--") continue;
  115. if (arg === "--help" || arg === "-h") {
  116. help = true;
  117. continue;
  118. }
  119. if (arg === "--all") {
  120. sawTargetSelector = true;
  121. includeAll = true;
  122. continue;
  123. }
  124. if (arg === "--darwin" || arg === "--linux" || arg === "--win") {
  125. selectedPlatforms.add(arg.slice(2));
  126. continue;
  127. }
  128. if (arg === "--target" || arg.startsWith("--target=")) {
  129. sawTargetSelector = true;
  130. const { nextIndex, value } = readValue(arg, index);
  131. addTargetList(value);
  132. index = nextIndex;
  133. continue;
  134. }
  135. if (arg === "--targets" || arg.startsWith("--targets=")) {
  136. sawTargetSelector = true;
  137. const { nextIndex, value } = readValue(arg, index);
  138. addTargetList(value);
  139. index = nextIndex;
  140. continue;
  141. }
  142. if (arg === "--node-binary" || arg.startsWith("--node-binary=")) {
  143. const { nextIndex, value } = readValue(arg, index);
  144. const separatorIndex = value.indexOf("=");
  145. if (separatorIndex <= 0) {
  146. throw new Error(
  147. "Expected --node-binary to use <target>=<path>, for example linux-x64=/opt/node/bin/node",
  148. );
  149. }
  150. const target = normalizeTarget(value.slice(0, separatorIndex));
  151. const binaryPath = value.slice(separatorIndex + 1);
  152. assertSupportedTarget(target);
  153. if (!binaryPath) {
  154. throw new Error(`Missing path for --node-binary ${target}`);
  155. }
  156. nodeBinaries[target] = resolve(binaryPath);
  157. index = nextIndex;
  158. continue;
  159. }
  160. throw new Error(`Unknown option "${arg}". Run with --help for usage.`);
  161. }
  162. let targets = [];
  163. if (includeAll) targets.push(...supportedTargets);
  164. targets.push(...selectedTargets);
  165. if (targets.length === 0 && !sawTargetSelector) {
  166. const nodeBinaryTargets = Object.keys(nodeBinaries);
  167. if (nodeBinaryTargets.length > 0) targets = nodeBinaryTargets;
  168. else if (selectedPlatforms.size > 0) targets = [...supportedTargets];
  169. else targets = [hostTarget({ arch: hostArch, platform: hostPlatform })];
  170. }
  171. targets = [...new Set(targets)];
  172. for (const target of targets) assertSupportedTarget(target);
  173. if (selectedPlatforms.size > 0) {
  174. targets = targets.filter((target) => selectedPlatforms.has(targetParts(target).releasePlatform));
  175. if (targets.length === 0) {
  176. throw new Error(
  177. `No SEA target left after ${[...selectedPlatforms].map((platform) => `--${platform}`).join(" ")}.`,
  178. );
  179. }
  180. }
  181. return {
  182. help,
  183. nodeBinaries,
  184. targets,
  185. };
  186. };