Debian Codegraph @

plugins/jieba/node/build.js

raw symbols SCIP
1 const { execFileSync } = require('child_process');
2 const fs = require('fs');
3 const path = require('path');
4 const os = require('os');
5
6 const rootDir = path.resolve(__dirname, '../../..');
7 const packageDir = __dirname;
8
9 function normalizeArch(value) {
10 if (value === 'x64') {
11 return 'x64';
12 }
13 if (value === 'arm64' || value === 'aarch64') {
14 return 'arm64';
15 }
16 return value;
17 }
18
19 function parseTarget(value) {
20 if (!value) {
21 return {
22 platform: os.platform(),
23 arch: normalizeArch(os.arch()),
24 };
25 }
26
27 const index = value.indexOf('-');
28 if (index === -1) {
29 throw new Error(`Invalid target '${value}'. Expected platform-arch.`);
30 }
31
32 return {
33 platform: value.slice(0, index),
34 arch: normalizeArch(value.slice(index + 1)),
35 };
36 }
37
38 function run(command, args, options = {}) {
39 execFileSync(command, args, {
40 cwd: rootDir,
41 stdio: 'inherit',
42 ...options,
43 });
44 }
45
46 function copyForce(src, dest) {
47 fs.mkdirSync(path.dirname(dest), { recursive: true });
48 if (fs.existsSync(dest)) {
49 fs.rmSync(dest, { force: true });
50 }
51 fs.copyFileSync(src, dest);
52 }
53
54 function findFile(dir, filename) {
55 const entries = fs.readdirSync(dir, { withFileTypes: true });
56 for (const entry of entries) {
57 const fullPath = path.join(dir, entry.name);
58 if (entry.isFile() && entry.name === filename) {
59 return fullPath;
60 }
61 if (entry.isDirectory()) {
62 const found = findFile(fullPath, filename);
63 if (found) {
64 return found;
65 }
66 }
67 }
68 return null;
69 }
70
71 const { platform, arch } = parseTarget(process.argv[2]);
72 const hostPlatform = os.platform();
73 const hostArch = normalizeArch(os.arch());
74
75 if (platform !== hostPlatform || arch !== hostArch) {
76 console.error(
77 `Refusing to cross-build ${platform}-${arch} on ${hostPlatform}-${hostArch}. ` +
78 'Run this script on the target platform.'
79 );
80 process.exit(1);
81 }
82
83 const target = `${platform}-${arch}`;
84 const buildDir = path.join(packageDir, 'build', target);
85 const config = 'Release';
86 const libName = platform === 'win32' ? 'opencc-jieba.dll'
87 : platform === 'darwin' ? 'libopencc-jieba.dylib'
88 : 'libopencc-jieba.so';
89
90 console.log(`Building opencc-jieba via CMake for ${target}...`);
91
92 run('cmake', [
93 '-S', rootDir,
94 '-B', buildDir,
95 '-DCMAKE_BUILD_TYPE=Release',
96 '-DBUILD_SHARED_LIBS=OFF',
97 '-DBUILD_OPENCC_JIEBA_PLUGIN=ON',
98 '-DENABLE_GTEST=OFF',
99 '-DENABLE_BENCHMARK=OFF',
100 // Match the minimum macOS version of the Bazel-built opencc npm binaries;
101 // without this the plugin inherits the build host's OS version as minos.
102 ...(platform === 'darwin' ? ['-DCMAKE_OSX_DEPLOYMENT_TARGET=11.0'] : []),
103 ]);
104
105 run('cmake', [
106 '--build', buildDir,
107 '--config', config,
108 '--target', 'opencc_jieba',
109 '--parallel',
110 ]);
111
112 const libSrc = findFile(buildDir, libName);
113 const mergedOcd2 = findFile(buildDir, 'jieba_merged.ocd2');
114
115 if (!libSrc) {
116 console.error(`Library not found under ${buildDir}: ${libName}`);
117 process.exit(1);
118 }
119 if (!mergedOcd2) {
120 console.error(`Dictionary not found under ${buildDir}: jieba_merged.ocd2`);
121 process.exit(1);
122 }
123
124 const prebuildDir = path.join(packageDir, 'prebuilds', target);
125 const dataDir = path.join(packageDir, 'data');
126 const jiebaDictDir = path.join(dataDir, 'jieba_dict');
127
128 copyForce(libSrc, path.join(prebuildDir, libName));
129 console.log(`Copied ${libName} to prebuilds/${target}/`);
130
131 const configSrcDir = path.join(rootDir, 'plugins/jieba/data/config');
132 const configs = [
133 'hk2sp_jieba.json',
134 's2hk_jieba.json',
135 's2hkp_jieba.json',
136 's2t_jieba.json',
137 's2tw_jieba.json',
138 's2twp_jieba.json',
139 'tw2sp_jieba.json',
140 ];
141 for (const configFile of configs) {
142 copyForce(path.join(configSrcDir, configFile), path.join(dataDir, configFile));
143 console.log(`Copied ${configFile} to data/`);
144 }
145
146 copyForce(mergedOcd2, path.join(jiebaDictDir, 'jieba_merged.ocd2'));
147 console.log('Copied jieba_merged.ocd2 to data/jieba_dict/');
148
149 const dictSrcDir = path.join(rootDir, 'plugins/jieba/deps/cppjieba/dict');
150 for (const dictFile of ['hmm_model.utf8', 'idf.utf8', 'stop_words.utf8']) {
151 copyForce(path.join(dictSrcDir, dictFile), path.join(jiebaDictDir, dictFile));
152 console.log(`Copied ${dictFile} to data/jieba_dict/`);
153 }
154
155 console.log('\nPackage directory tree restored successfully!');