native-search-tools-config.mjs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. import { dirname, join, resolve } from "node:path";
  2. import process from "node:process";
  3. import { fileURLToPath } from "node:url";
  4. const repoRoot = resolve(dirname(fileURLToPath(import.meta.url)), "..");
  5. export const NATIVE_SEARCH_DEPENDENCIES_DIR = join(
  6. repoRoot,
  7. "apps/zcode-cli/dependencies/native-search",
  8. );
  9. export const MACOS_NATIVE_SEARCH_DEPLOYMENT_TARGET = "12.0";
  10. export const LINUX_NATIVE_SEARCH_GLIBC_BASELINE = "2.28";
  11. export const NATIVE_SEARCH_RIPGREP_REVISION = "4649aa9700";
  12. export const NATIVE_SEARCH_BFS_CONFIGURE_ARGS = Object.freeze([
  13. "--enable-release",
  14. "--with-oniguruma",
  15. "--without-libselinux",
  16. "--without-libacl",
  17. "--without-liburing",
  18. "--without-libcap",
  19. ]);
  20. export const NATIVE_SEARCH_TOOL_VERSIONS = Object.freeze({
  21. bfs: "4.1.1",
  22. ripgrep: "14.1.1",
  23. ugrep: "7.8.4",
  24. });
  25. export const NATIVE_SEARCH_PREBUILT_RELEASES = Object.freeze({
  26. bfs: `v${NATIVE_SEARCH_TOOL_VERSIONS.bfs}-1`,
  27. ripgrep: `v${NATIVE_SEARCH_TOOL_VERSIONS.ripgrep}-1`,
  28. ugrep: `v${NATIVE_SEARCH_TOOL_VERSIONS.ugrep}-1`,
  29. });
  30. const NATIVE_SEARCH_PREBUILT_RELEASE_OVERRIDES = Object.freeze({
  31. linux: Object.freeze({
  32. bfs: `v${NATIVE_SEARCH_TOOL_VERSIONS.bfs}-2`,
  33. }),
  34. });
  35. export const NATIVE_SEARCH_SOURCE_ARCHIVES = Object.freeze([
  36. {
  37. id: "bfs",
  38. version: NATIVE_SEARCH_TOOL_VERSIONS.bfs,
  39. url: "https://codeload.github.com/tavianator/bfs/tar.gz/f220fb5afd8dd7f46b1d1a2ae9c36261eaccf3cd",
  40. sha256: "26a122dbafc81f42a60f32d842ca5b4f30c943ba2efff1d61db827bda5efe742",
  41. },
  42. {
  43. id: "ugrep",
  44. version: NATIVE_SEARCH_TOOL_VERSIONS.ugrep,
  45. url: "https://codeload.github.com/Genivia/ugrep/tar.gz/550599a6434fc5315fb6ecd415a5d859e6d846a8",
  46. sha256: "0b3ed2dc7c3902d89f2ef8b66961db2e31a32f240fa2a2e0eb8d559827f36dab",
  47. },
  48. {
  49. id: "oniguruma",
  50. version: "6.9.10",
  51. url: "https://github.com/kkos/oniguruma/releases/download/v6.9.10/onig-6.9.10.tar.gz",
  52. sha256: "2a5cfc5ae259e4e97f86b68dfffc152cdaffe94e2060b770cb827238d769fc05",
  53. },
  54. {
  55. id: "pcre2",
  56. version: "10.43",
  57. url: "https://github.com/PCRE2Project/pcre2/releases/download/pcre2-10.43/pcre2-10.43.tar.gz",
  58. sha256: "889d16be5abb8d05400b33c25e151638b8d4bac0e2d9c76e9d6923118ae8a34e",
  59. },
  60. {
  61. id: "zlib",
  62. version: "1.3.1",
  63. url: "https://github.com/madler/zlib/releases/download/v1.3.1/zlib-1.3.1.tar.gz",
  64. sha256: "9a93b2b7dfdac77ceba5a558a580e74667dd6fede4585b91eefb60f03b72df23",
  65. },
  66. {
  67. id: "bzip2",
  68. version: "1.0.8",
  69. url: "https://sourceware.org/pub/bzip2/bzip2-1.0.8.tar.gz",
  70. sha256: "ab5a03176ee106d3f0fa90e381da478ddae405918153cca248e682cd0c4a2269",
  71. },
  72. {
  73. id: "zstd",
  74. version: "1.5.6",
  75. url: "https://codeload.github.com/facebook/zstd/tar.gz/794ea1b0afca0f020f4e57b6732332231fb23c70",
  76. sha256: "33eaa5611d330dbc037e32554521d4e4a8edf2358df7d9b74d586c510a4bcec6",
  77. },
  78. {
  79. id: "brotli",
  80. version: "1.1.0",
  81. url: "https://codeload.github.com/google/brotli/tar.gz/ed738e842d2fbdf2d6459e39267a633c4a9b2f5d",
  82. sha256: "aaa739962a45b508b2e783b915e6b2b57ed3b12bd4b0feac73acfb144dffa54f",
  83. },
  84. ]);
  85. const UGREP_ARCHIVE_FEATURES = "-z:zlib,bzip2,zstd,brotli,7z,tar/pax/cpio/zip";
  86. const NATIVE_SEARCH_RUNTIME_TOOL_IDS_BY_PLATFORM = Object.freeze({
  87. darwin: Object.freeze(["bfs", "ugrep", "ripgrep"]),
  88. linux: Object.freeze(["bfs", "ugrep", "ripgrep"]),
  89. win32: Object.freeze(["ugrep", "ripgrep"]),
  90. });
  91. const NATIVE_SEARCH_PRODUCER_OUTPUT_IDS_BY_TARGET = Object.freeze({
  92. "darwin-arm64": Object.freeze(["bfs", "ugrep"]),
  93. "darwin-x64": Object.freeze(["bfs", "ugrep"]),
  94. "linux-arm64": Object.freeze(["bfs", "ugrep"]),
  95. "linux-x64": Object.freeze(["bfs", "ugrep"]),
  96. "win32-arm64": Object.freeze(["ugrep"]),
  97. "win32-x64": Object.freeze(["ugrep"]),
  98. });
  99. export const NATIVE_SEARCH_OFFICIAL_RIPGREP_ASSETS = Object.freeze({
  100. "darwin-arm64": Object.freeze({
  101. archiveExt: "tar.gz",
  102. releaseFileName: "ripgrep-v14.1.1-1-aarch64-apple-darwin.tar.gz",
  103. revision: NATIVE_SEARCH_RIPGREP_REVISION,
  104. sha256: "84ff9b227b9aad651be0ac4cf7802a190015bfba3dfa6b015cc7aa89dd8c5c2b",
  105. }),
  106. "darwin-x64": Object.freeze({
  107. archiveExt: "tar.gz",
  108. releaseFileName: "ripgrep-v14.1.1-1-x86_64-apple-darwin.tar.gz",
  109. revision: NATIVE_SEARCH_RIPGREP_REVISION,
  110. sha256: "cfedd7993342bc5f90310ce73a00cbf5d3b5237654c9bfc18b383f0e829f3b11",
  111. }),
  112. "linux-arm64": Object.freeze({
  113. archiveExt: "tar.gz",
  114. releaseFileName: "ripgrep-v14.1.1-1-aarch64-unknown-linux-musl.tar.gz",
  115. revision: null,
  116. sha256: "98c0371366a9db920ed6c196083e8030511055e5f128b3bde2ca56bbddf85b9a",
  117. }),
  118. "linux-x64": Object.freeze({
  119. archiveExt: "tar.gz",
  120. releaseFileName: "ripgrep-v14.1.1-1-x86_64-unknown-linux-musl.tar.gz",
  121. revision: NATIVE_SEARCH_RIPGREP_REVISION,
  122. sha256: "1154dd91f7b144cee490b91f1ab27ce04f8f01d8876cde2d9c0eff845c8ab012",
  123. }),
  124. "win32-x64": Object.freeze({
  125. archiveExt: "zip",
  126. releaseFileName: "ripgrep-v14.1.1-1-x86_64-pc-windows-msvc.zip",
  127. revision: NATIVE_SEARCH_RIPGREP_REVISION,
  128. sha256: "ede1d7f533f30d7e2870f77139d0fb9d7591daad5260b80d6a525e0bb5bd440e",
  129. }),
  130. "win32-arm64": Object.freeze({
  131. archiveExt: "zip",
  132. releaseFileName: "ripgrep-v14.1.1-1-aarch64-pc-windows-msvc.zip",
  133. revision: NATIVE_SEARCH_RIPGREP_REVISION,
  134. sha256: "88660d96f822d2e0329e254031068e82028f9c13efcec9b2d78c5a19d3f9ac47",
  135. }),
  136. });
  137. export const NATIVE_SEARCH_PRODUCER_ARCHIVE_SHA256_BY_TARGET = Object.freeze({
  138. "darwin-arm64": Object.freeze({
  139. bfs: "696f73eaff50d3c3de8a8ee36746a89693dd6ff0d010b05cd3b4b3eb2a369781",
  140. ugrep: "01ea803e3fc3b94e796a9376e4d062a5490fe08e5760c6173e72b8f31546a4f3",
  141. }),
  142. "darwin-x64": Object.freeze({
  143. bfs: "d993d72530749fa777339a7546f03338499abf235e2147d4adcd086641c6376f",
  144. ugrep: "6a2bedd9ccf53a2ac574d2fa498a36dcbe21e957b40442f9493180c6dad4dea9",
  145. }),
  146. "linux-arm64": Object.freeze({
  147. bfs: "dabdde935a02f89dd0c48c5cd0ec756a87d300fd5ace85a78e6a9eb51d771118",
  148. ugrep: "bf3b99c0af41f50c0ec3031b32ab8f67fa95b5581add18fad5163ece978d31ba",
  149. }),
  150. "linux-x64": Object.freeze({
  151. bfs: "9adf5759000021dd8fcd99670461cc43863f9172fb16f2f9a3e49c700311e7a0",
  152. ugrep: "13732f50fe63da07f3472f627f611b75359153163d07a0eb01e7c54125ae930e",
  153. }),
  154. "win32-arm64": Object.freeze({
  155. ugrep: "e9de73f1b542105203d586b19b4d57b8ecf2bf780f23e86c66a0fb7f56d51024",
  156. }),
  157. "win32-x64": Object.freeze({
  158. ugrep: "7bbd8e56540d2019a343688f1fc0ddb2ff5f0988090c577d2b9d6f4a8a86be21",
  159. }),
  160. });
  161. const NATIVE_SEARCH_BINARY_NAMES = Object.freeze({
  162. bfs: "bfs",
  163. ripgrep: "rg",
  164. ugrep: "ugrep",
  165. });
  166. function resolveNativeSearchArchiveTarget(platform, arch) {
  167. switch (platform) {
  168. case "darwin":
  169. return `${arch === "arm64" ? "aarch64" : "x86_64"}-apple-darwin.tar.gz`;
  170. case "linux":
  171. return `${arch === "arm64" ? "aarch64" : "x86_64"}-unknown-linux-gnu.tar.gz`;
  172. case "win32":
  173. return `${arch === "arm64" ? "aarch64" : "x86_64"}-pc-windows-msvc.zip`;
  174. }
  175. }
  176. // 只有完成对应构建与发布校验的 platform key 才能进入 desktop 发行链路。
  177. const ENABLED_NATIVE_SEARCH_PLATFORM_KEYS = Object.freeze([
  178. "darwin-arm64",
  179. "darwin-x64",
  180. "linux-arm64",
  181. "linux-x64",
  182. "win32-arm64",
  183. "win32-x64",
  184. ]);
  185. const UGREP_SOURCE_IDS = Object.freeze(["ugrep", "pcre2", "zlib", "bzip2", "zstd", "brotli"]);
  186. export function normalizeNativeSearchPlatform(rawPlatform = process.platform) {
  187. switch (rawPlatform.toLowerCase()) {
  188. case "mac":
  189. case "macos":
  190. case "darwin":
  191. case "osx":
  192. return "darwin";
  193. case "win":
  194. case "windows":
  195. case "win32":
  196. return "win32";
  197. case "linux":
  198. return "linux";
  199. default:
  200. throw new Error(`unsupported native search platform ${rawPlatform}`);
  201. }
  202. }
  203. export function normalizeNativeSearchArch(rawArch = process.arch) {
  204. switch (rawArch.toLowerCase()) {
  205. case "x64":
  206. case "amd64":
  207. case "x86_64":
  208. return "x64";
  209. case "arm64":
  210. case "aarch64":
  211. return "arm64";
  212. default:
  213. throw new Error(`unsupported native search architecture ${rawArch}`);
  214. }
  215. }
  216. export function resolveNativeSearchPrebuiltRelease(toolId, rawPlatform = process.platform) {
  217. const platform = normalizeNativeSearchPlatform(rawPlatform);
  218. const release =
  219. NATIVE_SEARCH_PREBUILT_RELEASE_OVERRIDES[platform]?.[toolId] ??
  220. NATIVE_SEARCH_PREBUILT_RELEASES[toolId];
  221. if (!release) {
  222. throw new Error(`unsupported native search tool ${toolId}`);
  223. }
  224. return release;
  225. }
  226. export function isMacosRosettaTarget({
  227. platform,
  228. arch,
  229. hostPlatform = process.platform,
  230. hostArch = process.arch,
  231. }) {
  232. return (
  233. normalizeNativeSearchPlatform(platform) === "darwin" &&
  234. normalizeNativeSearchPlatform(hostPlatform) === "darwin" &&
  235. normalizeNativeSearchArch(hostArch) === "arm64" &&
  236. normalizeNativeSearchArch(arch) === "x64"
  237. );
  238. }
  239. export function getNativeSearchRuntimeToolIdsForPlatform(rawPlatform) {
  240. const platform = normalizeNativeSearchPlatform(rawPlatform);
  241. return NATIVE_SEARCH_RUNTIME_TOOL_IDS_BY_PLATFORM[platform];
  242. }
  243. export function getNativeSearchProducerOutputIds({ platform, arch }) {
  244. const normalizedPlatform = normalizeNativeSearchPlatform(platform);
  245. const normalizedArch = normalizeNativeSearchArch(arch);
  246. const platformKey = `${normalizedPlatform}-${normalizedArch}`;
  247. const outputIds = NATIVE_SEARCH_PRODUCER_OUTPUT_IDS_BY_TARGET[platformKey];
  248. if (!outputIds) {
  249. throw new Error(`unsupported native search producer target ${platformKey}`);
  250. }
  251. return outputIds;
  252. }
  253. export function getExpectedUgrepFeatureContract(rawPlatform) {
  254. const platform = normalizeNativeSearchPlatform(rawPlatform);
  255. const pcre2Feature = platform === "darwin" ? "-P:pcre2" : "-P:pcre2jit";
  256. return `; ${pcre2Feature}; ${UGREP_ARCHIVE_FEATURES}`;
  257. }
  258. export function resolveNativeSearchReleasePlan({
  259. platform = process.platform,
  260. arch = process.arch,
  261. } = {}) {
  262. const normalizedPlatform = normalizeNativeSearchPlatform(platform);
  263. const normalizedArch = normalizeNativeSearchArch(arch);
  264. const platformKey = `${normalizedPlatform}-${normalizedArch}`;
  265. const enabled = ENABLED_NATIVE_SEARCH_PLATFORM_KEYS.includes(platformKey);
  266. const runtimeToolIds = enabled
  267. ? [...getNativeSearchRuntimeToolIdsForPlatform(normalizedPlatform)]
  268. : [];
  269. return {
  270. platform: normalizedPlatform,
  271. arch: normalizedArch,
  272. platformKey,
  273. enabled,
  274. runtimeToolIds,
  275. extraResourceToolIds: runtimeToolIds.filter((toolId) => toolId !== "ripgrep"),
  276. };
  277. }
  278. export function getNativeSearchSourceArchivesForTarget({ platform, arch }) {
  279. const producerOutputIds = getNativeSearchProducerOutputIds({ platform, arch });
  280. const requiredSourceIds = new Set();
  281. if (producerOutputIds.includes("ugrep")) {
  282. for (const sourceId of UGREP_SOURCE_IDS) requiredSourceIds.add(sourceId);
  283. }
  284. if (producerOutputIds.includes("bfs")) {
  285. requiredSourceIds.add("bfs");
  286. requiredSourceIds.add("oniguruma");
  287. }
  288. return NATIVE_SEARCH_SOURCE_ARCHIVES.filter((source) => requiredSourceIds.has(source.id));
  289. }
  290. export function resolveNativeSearchBuildPlan({
  291. platform = process.platform,
  292. arch = process.arch,
  293. outputDir,
  294. } = {}) {
  295. const normalizedPlatform = normalizeNativeSearchPlatform(platform);
  296. const normalizedArch = normalizeNativeSearchArch(arch);
  297. const platformKey = `${normalizedPlatform}-${normalizedArch}`;
  298. const resolvedOutputDir = resolve(
  299. outputDir ?? join(repoRoot, "packages/desktop/bundled-tools", platformKey),
  300. );
  301. const runtimeToolIds = getNativeSearchRuntimeToolIdsForPlatform(normalizedPlatform);
  302. const producerOutputIds = getNativeSearchProducerOutputIds({
  303. platform: normalizedPlatform,
  304. arch: normalizedArch,
  305. });
  306. const executableSuffix = normalizedPlatform === "win32" ? ".exe" : "";
  307. const binaries = Object.fromEntries(
  308. runtimeToolIds.map((toolId) => [
  309. toolId,
  310. join(resolvedOutputDir, toolId, `${NATIVE_SEARCH_BINARY_NAMES[toolId]}${executableSuffix}`),
  311. ]),
  312. );
  313. return {
  314. platform: normalizedPlatform,
  315. arch: normalizedArch,
  316. platformKey,
  317. outputDir: resolvedOutputDir,
  318. runtimeToolIds,
  319. producerOutputIds: [...producerOutputIds],
  320. binaries,
  321. bfsPath: binaries.bfs,
  322. rgPath: binaries.ripgrep,
  323. ugrepPath: binaries.ugrep,
  324. };
  325. }
  326. export function resolveNativeSearchPrebuiltPlan({
  327. platform = process.platform,
  328. arch = process.arch,
  329. outputDir,
  330. dependenciesDir = NATIVE_SEARCH_DEPENDENCIES_DIR,
  331. } = {}) {
  332. const buildPlan = resolveNativeSearchBuildPlan({ platform, arch, outputDir });
  333. const archiveTarget = resolveNativeSearchArchiveTarget(buildPlan.platform, buildPlan.arch);
  334. const officialRipgrepAsset = NATIVE_SEARCH_OFFICIAL_RIPGREP_ASSETS[buildPlan.platformKey];
  335. return {
  336. ...buildPlan,
  337. artifacts: buildPlan.runtimeToolIds.map((toolId) => {
  338. if (toolId === "ripgrep") {
  339. if (!officialRipgrepAsset) {
  340. throw new Error(`missing Microsoft ripgrep asset for ${buildPlan.platformKey}`);
  341. }
  342. return {
  343. toolId,
  344. version: NATIVE_SEARCH_TOOL_VERSIONS[toolId],
  345. release: NATIVE_SEARCH_PREBUILT_RELEASES[toolId],
  346. releaseFileName: officialRipgrepAsset.releaseFileName,
  347. archiveExt: officialRipgrepAsset.archiveExt,
  348. archiveSha256: officialRipgrepAsset.sha256,
  349. binaryName: `${NATIVE_SEARCH_BINARY_NAMES[toolId]}${buildPlan.platform === "win32" ? ".exe" : ""}`,
  350. binaryPath: buildPlan.binaries[toolId],
  351. archivePath: resolve(
  352. dependenciesDir,
  353. `ripgrep-${NATIVE_SEARCH_PREBUILT_RELEASES.ripgrep}`,
  354. officialRipgrepAsset.releaseFileName,
  355. ),
  356. source: "official",
  357. };
  358. }
  359. const release = resolveNativeSearchPrebuiltRelease(toolId, buildPlan.platform);
  360. const releaseFileName = `${toolId}-${release}-${archiveTarget}`;
  361. const archiveSha256 =
  362. NATIVE_SEARCH_PRODUCER_ARCHIVE_SHA256_BY_TARGET[buildPlan.platformKey]?.[toolId];
  363. if (!archiveSha256) {
  364. throw new Error(
  365. `missing producer archive SHA-256 for ${buildPlan.platformKey}/${releaseFileName}`,
  366. );
  367. }
  368. return {
  369. toolId,
  370. version: NATIVE_SEARCH_TOOL_VERSIONS[toolId],
  371. release,
  372. releaseFileName,
  373. archiveExt: archiveTarget.endsWith(".zip") ? "zip" : "tar.gz",
  374. archiveSha256,
  375. binaryName: `${NATIVE_SEARCH_BINARY_NAMES[toolId]}${buildPlan.platform === "win32" ? ".exe" : ""}`,
  376. binaryPath: buildPlan.binaries[toolId],
  377. archivePath: resolve(dependenciesDir, `${toolId}-${release}`, releaseFileName),
  378. source: "producer",
  379. };
  380. }),
  381. };
  382. }