native-search-tools-windows.mjs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import { copyFileSync, mkdirSync, mkdtempSync, rmSync } from "node:fs";
  2. import { cpus, tmpdir } from "node:os";
  3. import { dirname, join, resolve } from "node:path";
  4. import process from "node:process";
  5. import {
  6. getNativeSearchSourceArchivesForTarget,
  7. normalizeNativeSearchArch,
  8. normalizeNativeSearchPlatform,
  9. resolveNativeSearchBuildPlan,
  10. } from "./native-search-tools-config.mjs";
  11. import { downloadAndExtractSources, run } from "./native-search-tools-process.mjs";
  12. import { verifyBuiltNativeSearchProducerOutputs } from "./native-search-tools-verify.mjs";
  13. const windowsCmakeProject = resolve(import.meta.dirname, "native-search-tools-windows");
  14. function toCmakePath(path) {
  15. return path.replaceAll("\\", "/");
  16. }
  17. export function resolveNativeWindowsBuildConfig({
  18. platform = process.platform,
  19. arch = process.arch,
  20. hostPlatform = process.platform,
  21. hostArch = process.arch,
  22. env = process.env,
  23. } = {}) {
  24. const normalizedPlatform = normalizeNativeSearchPlatform(platform);
  25. const normalizedArch = normalizeNativeSearchArch(arch);
  26. const normalizedHostPlatform = normalizeNativeSearchPlatform(hostPlatform);
  27. const normalizedHostArch = normalizeNativeSearchArch(hostArch);
  28. if (normalizedPlatform !== "win32") {
  29. throw new Error(`native Windows search build does not support platform ${normalizedPlatform}`);
  30. }
  31. if (normalizedHostPlatform !== "win32") {
  32. throw new Error(
  33. `native Windows search build must run on Windows; host is ${normalizedHostPlatform}-${normalizedHostArch}`,
  34. );
  35. }
  36. return {
  37. arch: normalizedArch,
  38. cmake: env.CMAKE?.trim() || "cmake",
  39. generator: "Visual Studio 17 2022",
  40. generatorArchitecture: normalizedArch === "arm64" ? "ARM64" : "x64",
  41. hostArch: normalizedHostArch,
  42. platform: normalizedPlatform,
  43. };
  44. }
  45. export function buildNativeSearchToolsWindows({
  46. platform = process.platform,
  47. arch = process.arch,
  48. hostPlatform = process.platform,
  49. hostArch = process.arch,
  50. outputDir,
  51. jobs = Math.max(1, Math.min(cpus().length, 8)),
  52. keepWorkdir = false,
  53. quiet = false,
  54. processEnv = process.env,
  55. } = {}) {
  56. const config = resolveNativeWindowsBuildConfig({
  57. platform,
  58. arch,
  59. hostPlatform,
  60. hostArch,
  61. env: processEnv,
  62. });
  63. const plan = resolveNativeSearchBuildPlan({
  64. platform: config.platform,
  65. arch: config.arch,
  66. outputDir,
  67. });
  68. const workDir = mkdtempSync(join(tmpdir(), "zcode-native-search-build-"));
  69. const cmakeBuildDir = join(workDir, "cmake-build");
  70. const cmakeOutputDir = join(workDir, "cmake-output");
  71. const env = {
  72. ...processEnv,
  73. LANG: "C",
  74. LC_ALL: "C",
  75. };
  76. console.log("==> ZCode native search build");
  77. console.log(` target: ${plan.platformKey}`);
  78. console.log(` output: ${plan.outputDir}`);
  79. console.log(` workdir: ${workDir}`);
  80. try {
  81. const sources = downloadAndExtractSources({
  82. sources: getNativeSearchSourceArchivesForTarget({
  83. platform: plan.platform,
  84. arch: plan.arch,
  85. }),
  86. workDir,
  87. env,
  88. quiet,
  89. });
  90. mkdirSync(cmakeBuildDir, { recursive: true });
  91. mkdirSync(cmakeOutputDir, { recursive: true });
  92. console.log("==> Configure ugrep with MSVC/CMake");
  93. run(
  94. config.cmake,
  95. [
  96. "-S",
  97. windowsCmakeProject,
  98. "-B",
  99. cmakeBuildDir,
  100. "-G",
  101. config.generator,
  102. "-A",
  103. config.generatorArchitecture,
  104. `-DZCODE_BUILD_PATH=${toCmakePath(workDir)}`,
  105. `-DZCODE_UGREP_OUTPUT_DIR=${toCmakePath(cmakeOutputDir)}`,
  106. `-DZCODE_UGREP_SOURCE_DIR=${toCmakePath(sources.ugrep)}`,
  107. `-DZCODE_PCRE2_SOURCE_DIR=${toCmakePath(sources.pcre2)}`,
  108. `-DZCODE_ZLIB_SOURCE_DIR=${toCmakePath(sources.zlib)}`,
  109. `-DZCODE_BZIP2_SOURCE_DIR=${toCmakePath(sources.bzip2)}`,
  110. `-DZCODE_ZSTD_SOURCE_DIR=${toCmakePath(sources.zstd)}`,
  111. `-DZCODE_BROTLI_SOURCE_DIR=${toCmakePath(sources.brotli)}`,
  112. ],
  113. { env, quiet },
  114. );
  115. run(
  116. config.cmake,
  117. [
  118. "--build",
  119. cmakeBuildDir,
  120. "--config",
  121. "Release",
  122. "--target",
  123. "ugrep",
  124. "--parallel",
  125. String(jobs),
  126. ],
  127. { env, quiet },
  128. );
  129. const builtUgrepPath = join(cmakeOutputDir, "ugrep.exe");
  130. verifyBuiltNativeSearchProducerOutputs({
  131. ugrepPath: builtUgrepPath,
  132. platform: plan.platform,
  133. arch: plan.arch,
  134. hostArch: config.hostArch,
  135. producerOutputIds: plan.producerOutputIds,
  136. });
  137. mkdirSync(dirname(plan.ugrepPath), { recursive: true });
  138. copyFileSync(builtUgrepPath, plan.ugrepPath);
  139. verifyBuiltNativeSearchProducerOutputs({
  140. ugrepPath: plan.ugrepPath,
  141. platform: plan.platform,
  142. arch: plan.arch,
  143. hostArch: config.hostArch,
  144. producerOutputIds: plan.producerOutputIds,
  145. });
  146. console.log(`==> Built ${plan.ugrepPath}`);
  147. return plan;
  148. } finally {
  149. if (keepWorkdir) {
  150. console.log(`==> Preserved workdir ${workDir}`);
  151. } else {
  152. rmSync(workDir, { recursive: true, force: true });
  153. }
  154. }
  155. }