Skip to content

Commit 35c2ee1

Browse files
committed
Initial scripts commit
1 parent bffa435 commit 35c2ee1

File tree

6 files changed

+228
-44
lines changed

6 files changed

+228
-44
lines changed

binding.gyp

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,44 @@
22
'targets': [
33
{
44
'target_name': 'node-native-ocr',
5-
'cflags!': [ '-fno-exceptions' ],
6-
'cflags_cc!': [ '-fno-exceptions' ],
7-
'xcode_settings': { 'GCC_ENABLE_CPP_EXCEPTIONS': 'YES',
8-
'CLANG_CXX_LIBRARY': 'libc++',
9-
'MACOSX_DEPLOYMENT_TARGET': '10.7',
10-
},
11-
'msvs_settings': {
12-
'VCCLCompilerTool': { 'ExceptionHandling': 1 },
13-
},
145
'sources': [
156
'cc/node-native-ocr.cc',
167
'cc/ocr.cc',
178
'cc/recognize.cc'
189
],
1910
'include_dirs': [
20-
'<!(node -p "require(\'node-addon-api\').include_dir")',
21-
22-
'<!@(pkg-config tesseract --cflags-only-I | sed s/-I//g)',
11+
'tesseract/build/bin/include',
12+
'leptonica/build/bin/include',
13+
'<!(node -p "require(\'node-addon-api\').include_dir")'
2314
],
24-
'libraries': [
25-
'<!@(pkg-config tesseract --libs)'
15+
'conditions': [
16+
[ "OS!='win'", {
17+
'libraries': [
18+
'../tesseract/build/bin/lib/libtesseract.a',
19+
'../leptonica/build/bin/lib/libleptonica.a',
20+
'../libjpeg/build/bin/lib/libjpeg.a'
21+
]
22+
}],
23+
[ "OS=='win'", {
24+
'libraries': [
25+
'../tesseract/build/bin/lib/tesseract41.lib',
26+
'../leptonica/build/bin/lib/leptonica-1.80.0.lib',
27+
'../libjpeg/build/bin/lib/libjpeg.lib'
28+
]
29+
}]
2630
],
2731
'dependencies': [],
2832
'cflags!': [ '-fno-exceptions' ],
2933
'cflags_cc!': [ '-fno-exceptions' ],
30-
'xcode_settings': {
34+
'xcode_settings': {
3135
'GCC_ENABLE_CPP_EXCEPTIONS': 'YES',
3236
'CLANG_CXX_LIBRARY': 'libc++',
33-
'MACOSX_DEPLOYMENT_TARGET': '10.7'
37+
'MACOSX_DEPLOYMENT_TARGET': '10.9'
3438
},
3539
'msvs_settings': {
36-
'VCCLCompilerTool': { 'ExceptionHandling': 1 },
40+
'VCCLCompilerTool': {
41+
'ExceptionHandling': 1
42+
}
3743
}
3844
}
3945
]

package-lock.json

Lines changed: 32 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
},
2525
"dependencies": {
2626
"babel-runtime": "^6.23.0",
27-
"node-addon-api": "3.1.0"
27+
"node-addon-api": "3.1.0",
28+
"shelljs": "^0.8.4"
2829
},
2930
"scripts": {
3031
"build": "npm run build-cc && npm run build-js",

scripts/build-tesseract.js

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
var path = require('path');
2+
const { env } = require('process');
3+
var shell = require('shelljs');
4+
5+
const commonEnvVariables = {
6+
CMAKE_INSTALL_PREFIX: '${PWD}/bin',
7+
BUILD_SHARED_LIBS: 'OFF',
8+
CMAKE_POSITION_INDEPENDENT_CODE: 'ON',
9+
CMAKE_MSVC_RUNTIME_LIBRARY: 'MultiThreaded',
10+
CMAKE_POLICY_DEFAULT_CMP0091: 'NEW',
11+
CMAKE_OSX_DEPLOYMENT_TARGET: '10.9'
12+
}
13+
14+
const cmakeConfig = 'Release';
15+
16+
if (!shell.which('git')) {
17+
shell.echo('This script requires Git.');
18+
shell.exit(1);
19+
}
20+
21+
if (!shell.which('cmake')) {
22+
shell.echo('This script requires CMake.');
23+
shell.exit(1);
24+
}
25+
26+
// ------ startup ------
27+
shell.echo('build-tesseract script start.');
28+
29+
const homeDir = path.resolve(__dirname,'..');
30+
shell.cd(homeDir);
31+
shell.echo(`Working directory: ${homeDir}`);
32+
33+
// ------ submodules ------
34+
shell.echo('Initializing Git submodules.');
35+
shell.exec('git submodule update --init');
36+
37+
// ------ libraries ------
38+
buildLibjpeg ('libjpeg');
39+
buildLeptonica ('leptonica');
40+
buildTesseract ('tesseract');
41+
42+
shell.echo('build-tesseract script end.');
43+
44+
function buildLibjpeg (dirName) {
45+
shell.echo('Building libjpeg.');
46+
47+
if(shell.test('-e', dirName)) {
48+
shell.echo(`The ${dirName} directory already exists.`);
49+
} else {
50+
shell.exec(`git clone https://github.com/tamaskenez/libjpeg-cmake.git ${dirName}`);
51+
}
52+
53+
runCMakeBuild (dirName, cmakeConfig);
54+
}
55+
56+
function buildLeptonica (dirName) {
57+
shell.echo('Building Leptonica.');
58+
59+
runCMakeBuild (dirName, cmakeConfig, {
60+
SW_BUILD: 'OFF',
61+
CMAKE_FIND_USE_CMAKE_SYSTEM_PATH: 'FALSE',
62+
CMAKE_FIND_USE_SYSTEM_ENVIRONMENT_PATH: process.platform === 'darwin' ? 'FALSE' : 'TRUE',
63+
CMAKE_PREFIX_PATH: '${PWD}/../../libjpeg/build',
64+
CMAKE_INCLUDE_PATH: '${PWD}/../../libjpeg/build/bin/include',
65+
CMAKE_LIBRARY_PATH: '${PWD}/../../libjpeg/build/bin/lib'
66+
});
67+
}
68+
69+
function buildTesseract (dirName) {
70+
shell.echo('Building Tesseract.');
71+
72+
runCMakeBuild (dirName, cmakeConfig, {
73+
STATIC: 'ON',
74+
CPPAN_BUILD: 'OFF',
75+
BUILD_TRAINING_TOOLS: 'OFF',
76+
Leptonica_DIR: '../leptonica/build'
77+
});
78+
}
79+
80+
function runCMakeBuild (dirName, cmakeConfig, envVars) {
81+
recreateAndEnterBuildDir(dirName);
82+
83+
let cmakeCmd = 'cmake';
84+
cmakeCmd += formatEnvVars (envVars);
85+
cmakeCmd += formatEnvVars (commonEnvVariables);
86+
cmakeCmd += ' ..';
87+
88+
shell.echo(`Configuring a ${cmakeConfig} build.`)
89+
shell.echo(cmakeCmd);
90+
shell.exec(cmakeCmd);
91+
92+
shell.echo(`Creating a ${cmakeConfig} build.`)
93+
shell.exec(`cmake --build . --config ${cmakeConfig}`);
94+
95+
shell.echo(`Installing a ${cmakeConfig} build.`)
96+
shell.exec('cmake --install .');
97+
98+
leaveBuildDir();
99+
}
100+
101+
function recreateAndEnterBuildDir (dirName) {
102+
shell.pushd('-q', dirName);
103+
shell.rm('-rf','build');
104+
shell.mkdir('build');
105+
shell.pushd('-q', 'build');
106+
}
107+
108+
function leaveBuildDir() {
109+
shell.popd('-q');
110+
shell.popd('-q');
111+
}
112+
113+
function formatEnvVars (envVars) {
114+
const continuation = process.platform === 'win32' ? '' : ' \\\n';
115+
let args = '';
116+
for (key in envVars) {
117+
args += ` -D${key}=${envVars[key]}${continuation}`;
118+
}
119+
return args;
120+
}

0 commit comments

Comments
 (0)