|
| 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