kreuzberg 4.0.0.pre.rc.13 → 4.0.0.pre.rc.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +14 -14
- data/.rspec +3 -3
- data/.rubocop.yaml +1 -1
- data/.rubocop.yml +538 -538
- data/Gemfile +8 -8
- data/Gemfile.lock +105 -2
- data/README.md +454 -454
- data/Rakefile +33 -25
- data/Steepfile +47 -47
- data/examples/async_patterns.rb +341 -341
- data/ext/kreuzberg_rb/extconf.rb +45 -45
- data/ext/kreuzberg_rb/native/.cargo/config.toml +2 -2
- data/ext/kreuzberg_rb/native/Cargo.lock +6940 -6941
- data/ext/kreuzberg_rb/native/Cargo.toml +54 -54
- data/ext/kreuzberg_rb/native/README.md +425 -425
- data/ext/kreuzberg_rb/native/build.rs +15 -15
- data/ext/kreuzberg_rb/native/include/ieeefp.h +11 -11
- data/ext/kreuzberg_rb/native/include/msvc_compat/strings.h +14 -14
- data/ext/kreuzberg_rb/native/include/strings.h +20 -20
- data/ext/kreuzberg_rb/native/include/unistd.h +47 -47
- data/ext/kreuzberg_rb/native/src/lib.rs +3158 -3158
- data/extconf.rb +28 -28
- data/kreuzberg.gemspec +214 -214
- data/lib/kreuzberg/api_proxy.rb +142 -142
- data/lib/kreuzberg/cache_api.rb +81 -81
- data/lib/kreuzberg/cli.rb +55 -55
- data/lib/kreuzberg/cli_proxy.rb +127 -127
- data/lib/kreuzberg/config.rb +724 -724
- data/lib/kreuzberg/error_context.rb +80 -80
- data/lib/kreuzberg/errors.rb +118 -118
- data/lib/kreuzberg/extraction_api.rb +340 -340
- data/lib/kreuzberg/mcp_proxy.rb +186 -186
- data/lib/kreuzberg/ocr_backend_protocol.rb +113 -113
- data/lib/kreuzberg/post_processor_protocol.rb +86 -86
- data/lib/kreuzberg/result.rb +279 -279
- data/lib/kreuzberg/setup_lib_path.rb +80 -80
- data/lib/kreuzberg/validator_protocol.rb +89 -89
- data/lib/kreuzberg/version.rb +5 -5
- data/lib/kreuzberg.rb +109 -109
- data/lib/{pdfium.dll → libpdfium.dylib} +0 -0
- data/sig/kreuzberg/internal.rbs +184 -184
- data/sig/kreuzberg.rbs +546 -546
- data/spec/binding/cache_spec.rb +227 -227
- data/spec/binding/cli_proxy_spec.rb +85 -85
- data/spec/binding/cli_spec.rb +55 -55
- data/spec/binding/config_spec.rb +345 -345
- data/spec/binding/config_validation_spec.rb +283 -283
- data/spec/binding/error_handling_spec.rb +213 -213
- data/spec/binding/errors_spec.rb +66 -66
- data/spec/binding/plugins/ocr_backend_spec.rb +307 -307
- data/spec/binding/plugins/postprocessor_spec.rb +269 -269
- data/spec/binding/plugins/validator_spec.rb +274 -274
- data/spec/fixtures/config.toml +39 -39
- data/spec/fixtures/config.yaml +41 -41
- data/spec/fixtures/invalid_config.toml +4 -4
- data/spec/smoke/package_spec.rb +178 -178
- data/spec/spec_helper.rb +42 -42
- data/vendor/Cargo.toml +1 -1
- data/vendor/kreuzberg/Cargo.toml +5 -5
- data/vendor/kreuzberg/README.md +230 -230
- data/vendor/kreuzberg/benches/otel_overhead.rs +48 -48
- data/vendor/kreuzberg/build.rs +843 -843
- data/vendor/kreuzberg/src/api/error.rs +81 -81
- data/vendor/kreuzberg/src/api/handlers.rs +199 -199
- data/vendor/kreuzberg/src/api/mod.rs +79 -79
- data/vendor/kreuzberg/src/api/server.rs +353 -353
- data/vendor/kreuzberg/src/api/types.rs +170 -170
- data/vendor/kreuzberg/src/cache/mod.rs +1167 -1167
- data/vendor/kreuzberg/src/chunking/mod.rs +1877 -1877
- data/vendor/kreuzberg/src/chunking/processor.rs +220 -220
- data/vendor/kreuzberg/src/core/batch_mode.rs +95 -95
- data/vendor/kreuzberg/src/core/config.rs +1080 -1080
- data/vendor/kreuzberg/src/core/extractor.rs +1156 -1156
- data/vendor/kreuzberg/src/core/io.rs +329 -329
- data/vendor/kreuzberg/src/core/mime.rs +605 -605
- data/vendor/kreuzberg/src/core/mod.rs +47 -47
- data/vendor/kreuzberg/src/core/pipeline.rs +1184 -1184
- data/vendor/kreuzberg/src/embeddings.rs +500 -500
- data/vendor/kreuzberg/src/error.rs +431 -431
- data/vendor/kreuzberg/src/extraction/archive.rs +954 -954
- data/vendor/kreuzberg/src/extraction/docx.rs +398 -398
- data/vendor/kreuzberg/src/extraction/email.rs +854 -854
- data/vendor/kreuzberg/src/extraction/excel.rs +688 -688
- data/vendor/kreuzberg/src/extraction/html.rs +601 -601
- data/vendor/kreuzberg/src/extraction/image.rs +491 -491
- data/vendor/kreuzberg/src/extraction/libreoffice.rs +574 -574
- data/vendor/kreuzberg/src/extraction/markdown.rs +213 -213
- data/vendor/kreuzberg/src/extraction/mod.rs +81 -81
- data/vendor/kreuzberg/src/extraction/office_metadata/app_properties.rs +398 -398
- data/vendor/kreuzberg/src/extraction/office_metadata/core_properties.rs +247 -247
- data/vendor/kreuzberg/src/extraction/office_metadata/custom_properties.rs +240 -240
- data/vendor/kreuzberg/src/extraction/office_metadata/mod.rs +130 -130
- data/vendor/kreuzberg/src/extraction/office_metadata/odt_properties.rs +284 -284
- data/vendor/kreuzberg/src/extraction/pptx.rs +3100 -3100
- data/vendor/kreuzberg/src/extraction/structured.rs +490 -490
- data/vendor/kreuzberg/src/extraction/table.rs +328 -328
- data/vendor/kreuzberg/src/extraction/text.rs +269 -269
- data/vendor/kreuzberg/src/extraction/xml.rs +333 -333
- data/vendor/kreuzberg/src/extractors/archive.rs +447 -447
- data/vendor/kreuzberg/src/extractors/bibtex.rs +470 -470
- data/vendor/kreuzberg/src/extractors/docbook.rs +504 -504
- data/vendor/kreuzberg/src/extractors/docx.rs +400 -400
- data/vendor/kreuzberg/src/extractors/email.rs +157 -157
- data/vendor/kreuzberg/src/extractors/epub.rs +708 -708
- data/vendor/kreuzberg/src/extractors/excel.rs +345 -345
- data/vendor/kreuzberg/src/extractors/fictionbook.rs +492 -492
- data/vendor/kreuzberg/src/extractors/html.rs +407 -407
- data/vendor/kreuzberg/src/extractors/image.rs +219 -219
- data/vendor/kreuzberg/src/extractors/jats.rs +1054 -1054
- data/vendor/kreuzberg/src/extractors/jupyter.rs +368 -368
- data/vendor/kreuzberg/src/extractors/latex.rs +653 -653
- data/vendor/kreuzberg/src/extractors/markdown.rs +701 -701
- data/vendor/kreuzberg/src/extractors/mod.rs +429 -429
- data/vendor/kreuzberg/src/extractors/odt.rs +628 -628
- data/vendor/kreuzberg/src/extractors/opml.rs +635 -635
- data/vendor/kreuzberg/src/extractors/orgmode.rs +529 -529
- data/vendor/kreuzberg/src/extractors/pdf.rs +749 -749
- data/vendor/kreuzberg/src/extractors/pptx.rs +267 -267
- data/vendor/kreuzberg/src/extractors/rst.rs +577 -577
- data/vendor/kreuzberg/src/extractors/rtf.rs +809 -809
- data/vendor/kreuzberg/src/extractors/security.rs +484 -484
- data/vendor/kreuzberg/src/extractors/security_tests.rs +367 -367
- data/vendor/kreuzberg/src/extractors/structured.rs +142 -142
- data/vendor/kreuzberg/src/extractors/text.rs +265 -265
- data/vendor/kreuzberg/src/extractors/typst.rs +651 -651
- data/vendor/kreuzberg/src/extractors/xml.rs +147 -147
- data/vendor/kreuzberg/src/image/dpi.rs +164 -164
- data/vendor/kreuzberg/src/image/mod.rs +6 -6
- data/vendor/kreuzberg/src/image/preprocessing.rs +417 -417
- data/vendor/kreuzberg/src/image/resize.rs +89 -89
- data/vendor/kreuzberg/src/keywords/config.rs +154 -154
- data/vendor/kreuzberg/src/keywords/mod.rs +237 -237
- data/vendor/kreuzberg/src/keywords/processor.rs +275 -275
- data/vendor/kreuzberg/src/keywords/rake.rs +293 -293
- data/vendor/kreuzberg/src/keywords/types.rs +68 -68
- data/vendor/kreuzberg/src/keywords/yake.rs +163 -163
- data/vendor/kreuzberg/src/language_detection/mod.rs +985 -985
- data/vendor/kreuzberg/src/language_detection/processor.rs +219 -219
- data/vendor/kreuzberg/src/lib.rs +113 -113
- data/vendor/kreuzberg/src/mcp/mod.rs +35 -35
- data/vendor/kreuzberg/src/mcp/server.rs +2076 -2076
- data/vendor/kreuzberg/src/ocr/cache.rs +469 -469
- data/vendor/kreuzberg/src/ocr/error.rs +37 -37
- data/vendor/kreuzberg/src/ocr/hocr.rs +216 -216
- data/vendor/kreuzberg/src/ocr/mod.rs +58 -58
- data/vendor/kreuzberg/src/ocr/processor.rs +863 -863
- data/vendor/kreuzberg/src/ocr/table/mod.rs +4 -4
- data/vendor/kreuzberg/src/ocr/table/tsv_parser.rs +144 -144
- data/vendor/kreuzberg/src/ocr/tesseract_backend.rs +452 -452
- data/vendor/kreuzberg/src/ocr/types.rs +393 -393
- data/vendor/kreuzberg/src/ocr/utils.rs +47 -47
- data/vendor/kreuzberg/src/ocr/validation.rs +206 -206
- data/vendor/kreuzberg/src/panic_context.rs +154 -154
- data/vendor/kreuzberg/src/pdf/bindings.rs +44 -44
- data/vendor/kreuzberg/src/pdf/bundled.rs +346 -346
- data/vendor/kreuzberg/src/pdf/error.rs +130 -130
- data/vendor/kreuzberg/src/pdf/images.rs +139 -139
- data/vendor/kreuzberg/src/pdf/metadata.rs +489 -489
- data/vendor/kreuzberg/src/pdf/mod.rs +68 -68
- data/vendor/kreuzberg/src/pdf/rendering.rs +368 -368
- data/vendor/kreuzberg/src/pdf/table.rs +420 -420
- data/vendor/kreuzberg/src/pdf/text.rs +240 -240
- data/vendor/kreuzberg/src/plugins/extractor.rs +1044 -1044
- data/vendor/kreuzberg/src/plugins/mod.rs +212 -212
- data/vendor/kreuzberg/src/plugins/ocr.rs +639 -639
- data/vendor/kreuzberg/src/plugins/processor.rs +650 -650
- data/vendor/kreuzberg/src/plugins/registry.rs +1339 -1339
- data/vendor/kreuzberg/src/plugins/traits.rs +258 -258
- data/vendor/kreuzberg/src/plugins/validator.rs +967 -967
- data/vendor/kreuzberg/src/stopwords/mod.rs +1470 -1470
- data/vendor/kreuzberg/src/text/mod.rs +25 -25
- data/vendor/kreuzberg/src/text/quality.rs +697 -697
- data/vendor/kreuzberg/src/text/quality_processor.rs +219 -219
- data/vendor/kreuzberg/src/text/string_utils.rs +217 -217
- data/vendor/kreuzberg/src/text/token_reduction/cjk_utils.rs +164 -164
- data/vendor/kreuzberg/src/text/token_reduction/config.rs +100 -100
- data/vendor/kreuzberg/src/text/token_reduction/core.rs +796 -796
- data/vendor/kreuzberg/src/text/token_reduction/filters.rs +902 -902
- data/vendor/kreuzberg/src/text/token_reduction/mod.rs +160 -160
- data/vendor/kreuzberg/src/text/token_reduction/semantic.rs +619 -619
- data/vendor/kreuzberg/src/text/token_reduction/simd_text.rs +147 -147
- data/vendor/kreuzberg/src/types.rs +1055 -1055
- data/vendor/kreuzberg/src/utils/mod.rs +17 -17
- data/vendor/kreuzberg/src/utils/quality.rs +959 -959
- data/vendor/kreuzberg/src/utils/string_utils.rs +381 -381
- data/vendor/kreuzberg/stopwords/af_stopwords.json +53 -53
- data/vendor/kreuzberg/stopwords/ar_stopwords.json +482 -482
- data/vendor/kreuzberg/stopwords/bg_stopwords.json +261 -261
- data/vendor/kreuzberg/stopwords/bn_stopwords.json +400 -400
- data/vendor/kreuzberg/stopwords/br_stopwords.json +1205 -1205
- data/vendor/kreuzberg/stopwords/ca_stopwords.json +280 -280
- data/vendor/kreuzberg/stopwords/cs_stopwords.json +425 -425
- data/vendor/kreuzberg/stopwords/da_stopwords.json +172 -172
- data/vendor/kreuzberg/stopwords/de_stopwords.json +622 -622
- data/vendor/kreuzberg/stopwords/el_stopwords.json +849 -849
- data/vendor/kreuzberg/stopwords/en_stopwords.json +1300 -1300
- data/vendor/kreuzberg/stopwords/eo_stopwords.json +175 -175
- data/vendor/kreuzberg/stopwords/es_stopwords.json +734 -734
- data/vendor/kreuzberg/stopwords/et_stopwords.json +37 -37
- data/vendor/kreuzberg/stopwords/eu_stopwords.json +100 -100
- data/vendor/kreuzberg/stopwords/fa_stopwords.json +801 -801
- data/vendor/kreuzberg/stopwords/fi_stopwords.json +849 -849
- data/vendor/kreuzberg/stopwords/fr_stopwords.json +693 -693
- data/vendor/kreuzberg/stopwords/ga_stopwords.json +111 -111
- data/vendor/kreuzberg/stopwords/gl_stopwords.json +162 -162
- data/vendor/kreuzberg/stopwords/gu_stopwords.json +226 -226
- data/vendor/kreuzberg/stopwords/ha_stopwords.json +41 -41
- data/vendor/kreuzberg/stopwords/he_stopwords.json +196 -196
- data/vendor/kreuzberg/stopwords/hi_stopwords.json +227 -227
- data/vendor/kreuzberg/stopwords/hr_stopwords.json +181 -181
- data/vendor/kreuzberg/stopwords/hu_stopwords.json +791 -791
- data/vendor/kreuzberg/stopwords/hy_stopwords.json +47 -47
- data/vendor/kreuzberg/stopwords/id_stopwords.json +760 -760
- data/vendor/kreuzberg/stopwords/it_stopwords.json +634 -634
- data/vendor/kreuzberg/stopwords/ja_stopwords.json +136 -136
- data/vendor/kreuzberg/stopwords/kn_stopwords.json +84 -84
- data/vendor/kreuzberg/stopwords/ko_stopwords.json +681 -681
- data/vendor/kreuzberg/stopwords/ku_stopwords.json +64 -64
- data/vendor/kreuzberg/stopwords/la_stopwords.json +51 -51
- data/vendor/kreuzberg/stopwords/lt_stopwords.json +476 -476
- data/vendor/kreuzberg/stopwords/lv_stopwords.json +163 -163
- data/vendor/kreuzberg/stopwords/ml_stopwords.json +1 -1
- data/vendor/kreuzberg/stopwords/mr_stopwords.json +101 -101
- data/vendor/kreuzberg/stopwords/ms_stopwords.json +477 -477
- data/vendor/kreuzberg/stopwords/ne_stopwords.json +490 -490
- data/vendor/kreuzberg/stopwords/nl_stopwords.json +415 -415
- data/vendor/kreuzberg/stopwords/no_stopwords.json +223 -223
- data/vendor/kreuzberg/stopwords/pl_stopwords.json +331 -331
- data/vendor/kreuzberg/stopwords/pt_stopwords.json +562 -562
- data/vendor/kreuzberg/stopwords/ro_stopwords.json +436 -436
- data/vendor/kreuzberg/stopwords/ru_stopwords.json +561 -561
- data/vendor/kreuzberg/stopwords/si_stopwords.json +193 -193
- data/vendor/kreuzberg/stopwords/sk_stopwords.json +420 -420
- data/vendor/kreuzberg/stopwords/sl_stopwords.json +448 -448
- data/vendor/kreuzberg/stopwords/so_stopwords.json +32 -32
- data/vendor/kreuzberg/stopwords/st_stopwords.json +33 -33
- data/vendor/kreuzberg/stopwords/sv_stopwords.json +420 -420
- data/vendor/kreuzberg/stopwords/sw_stopwords.json +76 -76
- data/vendor/kreuzberg/stopwords/ta_stopwords.json +129 -129
- data/vendor/kreuzberg/stopwords/te_stopwords.json +54 -54
- data/vendor/kreuzberg/stopwords/th_stopwords.json +118 -118
- data/vendor/kreuzberg/stopwords/tl_stopwords.json +149 -149
- data/vendor/kreuzberg/stopwords/tr_stopwords.json +506 -506
- data/vendor/kreuzberg/stopwords/uk_stopwords.json +75 -75
- data/vendor/kreuzberg/stopwords/ur_stopwords.json +519 -519
- data/vendor/kreuzberg/stopwords/vi_stopwords.json +647 -647
- data/vendor/kreuzberg/stopwords/yo_stopwords.json +62 -62
- data/vendor/kreuzberg/stopwords/zh_stopwords.json +796 -796
- data/vendor/kreuzberg/stopwords/zu_stopwords.json +31 -31
- data/vendor/kreuzberg/tests/api_extract_multipart.rs +52 -52
- data/vendor/kreuzberg/tests/api_tests.rs +966 -966
- data/vendor/kreuzberg/tests/archive_integration.rs +545 -545
- data/vendor/kreuzberg/tests/batch_orchestration.rs +556 -556
- data/vendor/kreuzberg/tests/batch_processing.rs +318 -318
- data/vendor/kreuzberg/tests/bibtex_parity_test.rs +421 -421
- data/vendor/kreuzberg/tests/concurrency_stress.rs +533 -533
- data/vendor/kreuzberg/tests/config_features.rs +612 -612
- data/vendor/kreuzberg/tests/config_loading_tests.rs +416 -416
- data/vendor/kreuzberg/tests/core_integration.rs +510 -510
- data/vendor/kreuzberg/tests/csv_integration.rs +414 -414
- data/vendor/kreuzberg/tests/docbook_extractor_tests.rs +500 -500
- data/vendor/kreuzberg/tests/docx_metadata_extraction_test.rs +122 -122
- data/vendor/kreuzberg/tests/docx_vs_pandoc_comparison.rs +370 -370
- data/vendor/kreuzberg/tests/email_integration.rs +327 -327
- data/vendor/kreuzberg/tests/epub_native_extractor_tests.rs +275 -275
- data/vendor/kreuzberg/tests/error_handling.rs +402 -402
- data/vendor/kreuzberg/tests/fictionbook_extractor_tests.rs +228 -228
- data/vendor/kreuzberg/tests/format_integration.rs +164 -164
- data/vendor/kreuzberg/tests/helpers/mod.rs +142 -142
- data/vendor/kreuzberg/tests/html_table_test.rs +551 -551
- data/vendor/kreuzberg/tests/image_integration.rs +255 -255
- data/vendor/kreuzberg/tests/instrumentation_test.rs +139 -139
- data/vendor/kreuzberg/tests/jats_extractor_tests.rs +639 -639
- data/vendor/kreuzberg/tests/jupyter_extractor_tests.rs +704 -704
- data/vendor/kreuzberg/tests/keywords_integration.rs +479 -479
- data/vendor/kreuzberg/tests/keywords_quality.rs +509 -509
- data/vendor/kreuzberg/tests/latex_extractor_tests.rs +496 -496
- data/vendor/kreuzberg/tests/markdown_extractor_tests.rs +490 -490
- data/vendor/kreuzberg/tests/mime_detection.rs +429 -429
- data/vendor/kreuzberg/tests/ocr_configuration.rs +514 -514
- data/vendor/kreuzberg/tests/ocr_errors.rs +698 -698
- data/vendor/kreuzberg/tests/ocr_quality.rs +629 -629
- data/vendor/kreuzberg/tests/ocr_stress.rs +469 -469
- data/vendor/kreuzberg/tests/odt_extractor_tests.rs +674 -674
- data/vendor/kreuzberg/tests/opml_extractor_tests.rs +616 -616
- data/vendor/kreuzberg/tests/orgmode_extractor_tests.rs +822 -822
- data/vendor/kreuzberg/tests/pdf_integration.rs +45 -45
- data/vendor/kreuzberg/tests/pdfium_linking.rs +374 -374
- data/vendor/kreuzberg/tests/pipeline_integration.rs +1436 -1436
- data/vendor/kreuzberg/tests/plugin_ocr_backend_test.rs +776 -776
- data/vendor/kreuzberg/tests/plugin_postprocessor_test.rs +560 -560
- data/vendor/kreuzberg/tests/plugin_system.rs +927 -927
- data/vendor/kreuzberg/tests/plugin_validator_test.rs +783 -783
- data/vendor/kreuzberg/tests/registry_integration_tests.rs +587 -587
- data/vendor/kreuzberg/tests/rst_extractor_tests.rs +694 -694
- data/vendor/kreuzberg/tests/rtf_extractor_tests.rs +775 -775
- data/vendor/kreuzberg/tests/security_validation.rs +416 -416
- data/vendor/kreuzberg/tests/stopwords_integration_test.rs +888 -888
- data/vendor/kreuzberg/tests/test_fastembed.rs +631 -631
- data/vendor/kreuzberg/tests/typst_behavioral_tests.rs +1260 -1260
- data/vendor/kreuzberg/tests/typst_extractor_tests.rs +648 -648
- data/vendor/kreuzberg/tests/xlsx_metadata_extraction_test.rs +87 -87
- data/vendor/kreuzberg-ffi/Cargo.toml +1 -1
- data/vendor/kreuzberg-ffi/README.md +851 -851
- data/vendor/kreuzberg-ffi/build.rs +176 -176
- data/vendor/kreuzberg-ffi/cbindgen.toml +27 -27
- data/vendor/kreuzberg-ffi/kreuzberg-ffi.pc.in +12 -12
- data/vendor/kreuzberg-ffi/kreuzberg.h +1087 -1087
- data/vendor/kreuzberg-ffi/src/lib.rs +3616 -3616
- data/vendor/kreuzberg-ffi/src/panic_shield.rs +247 -247
- data/vendor/kreuzberg-ffi/tests.disabled/README.md +48 -48
- data/vendor/kreuzberg-ffi/tests.disabled/config_loading_tests.rs +299 -299
- data/vendor/kreuzberg-ffi/tests.disabled/config_tests.rs +346 -346
- data/vendor/kreuzberg-ffi/tests.disabled/extractor_tests.rs +232 -232
- data/vendor/kreuzberg-ffi/tests.disabled/plugin_registration_tests.rs +470 -470
- data/vendor/kreuzberg-tesseract/.commitlintrc.json +13 -13
- data/vendor/kreuzberg-tesseract/.crate-ignore +2 -2
- data/vendor/kreuzberg-tesseract/Cargo.lock +2933 -2933
- data/vendor/kreuzberg-tesseract/Cargo.toml +2 -2
- data/vendor/kreuzberg-tesseract/LICENSE +22 -22
- data/vendor/kreuzberg-tesseract/README.md +399 -399
- data/vendor/kreuzberg-tesseract/build.rs +1354 -1354
- data/vendor/kreuzberg-tesseract/patches/README.md +71 -71
- data/vendor/kreuzberg-tesseract/patches/tesseract.diff +199 -199
- data/vendor/kreuzberg-tesseract/src/api.rs +1371 -1371
- data/vendor/kreuzberg-tesseract/src/choice_iterator.rs +77 -77
- data/vendor/kreuzberg-tesseract/src/enums.rs +297 -297
- data/vendor/kreuzberg-tesseract/src/error.rs +81 -81
- data/vendor/kreuzberg-tesseract/src/lib.rs +145 -145
- data/vendor/kreuzberg-tesseract/src/monitor.rs +57 -57
- data/vendor/kreuzberg-tesseract/src/mutable_iterator.rs +197 -197
- data/vendor/kreuzberg-tesseract/src/page_iterator.rs +253 -253
- data/vendor/kreuzberg-tesseract/src/result_iterator.rs +286 -286
- data/vendor/kreuzberg-tesseract/src/result_renderer.rs +183 -183
- data/vendor/kreuzberg-tesseract/tests/integration_test.rs +211 -211
- data/vendor/rb-sys/.cargo_vcs_info.json +5 -5
- data/vendor/rb-sys/Cargo.lock +393 -393
- data/vendor/rb-sys/Cargo.toml +70 -70
- data/vendor/rb-sys/Cargo.toml.orig +57 -57
- data/vendor/rb-sys/LICENSE-APACHE +190 -190
- data/vendor/rb-sys/LICENSE-MIT +21 -21
- data/vendor/rb-sys/build/features.rs +111 -111
- data/vendor/rb-sys/build/main.rs +286 -286
- data/vendor/rb-sys/build/stable_api_config.rs +155 -155
- data/vendor/rb-sys/build/version.rs +50 -50
- data/vendor/rb-sys/readme.md +36 -36
- data/vendor/rb-sys/src/bindings.rs +21 -21
- data/vendor/rb-sys/src/hidden.rs +11 -11
- data/vendor/rb-sys/src/lib.rs +35 -35
- data/vendor/rb-sys/src/macros.rs +371 -371
- data/vendor/rb-sys/src/memory.rs +53 -53
- data/vendor/rb-sys/src/ruby_abi_version.rs +38 -38
- data/vendor/rb-sys/src/special_consts.rs +31 -31
- data/vendor/rb-sys/src/stable_api/compiled.c +179 -179
- data/vendor/rb-sys/src/stable_api/compiled.rs +257 -257
- data/vendor/rb-sys/src/stable_api/ruby_2_7.rs +324 -324
- data/vendor/rb-sys/src/stable_api/ruby_3_0.rs +332 -332
- data/vendor/rb-sys/src/stable_api/ruby_3_1.rs +325 -325
- data/vendor/rb-sys/src/stable_api/ruby_3_2.rs +323 -323
- data/vendor/rb-sys/src/stable_api/ruby_3_3.rs +339 -339
- data/vendor/rb-sys/src/stable_api/ruby_3_4.rs +339 -339
- data/vendor/rb-sys/src/stable_api.rs +260 -260
- data/vendor/rb-sys/src/symbol.rs +31 -31
- data/vendor/rb-sys/src/tracking_allocator.rs +330 -330
- data/vendor/rb-sys/src/utils.rs +89 -89
- data/vendor/rb-sys/src/value_type.rs +7 -7
- metadata +73 -4
- data/vendor/kreuzberg-ffi/kreuzberg-ffi-install.pc +0 -12
|
@@ -1,1354 +1,1354 @@
|
|
|
1
|
-
#![allow(clippy::uninlined_format_args)]
|
|
2
|
-
|
|
3
|
-
#[cfg(feature = "build-tesseract")]
|
|
4
|
-
mod build_tesseract {
|
|
5
|
-
use cmake::Config;
|
|
6
|
-
use std::env;
|
|
7
|
-
use std::fs;
|
|
8
|
-
use std::path::{Path, PathBuf};
|
|
9
|
-
|
|
10
|
-
const LEPTONICA_VERSION: &str = "1.86.0";
|
|
11
|
-
const TESSERACT_VERSION: &str = "5.5.1";
|
|
12
|
-
#[allow(dead_code)]
|
|
13
|
-
const EMSDK_COMMIT: &str = "974d5c096bd56e42045d052a47b28198ce75e2a8";
|
|
14
|
-
#[allow(dead_code)]
|
|
15
|
-
const EMSDK_VERSION: &str = "3.1.31";
|
|
16
|
-
#[allow(dead_code)]
|
|
17
|
-
const EMSDK_REPOSITORY: &str = "https://github.com/emscripten-core/emsdk.git";
|
|
18
|
-
|
|
19
|
-
fn leptonica_url() -> String {
|
|
20
|
-
format!(
|
|
21
|
-
"https://codeload.github.com/DanBloomberg/leptonica/zip/refs/tags/{}",
|
|
22
|
-
LEPTONICA_VERSION
|
|
23
|
-
)
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
fn tesseract_url() -> String {
|
|
27
|
-
format!(
|
|
28
|
-
"https://codeload.github.com/tesseract-ocr/tesseract/zip/refs/tags/{}",
|
|
29
|
-
TESSERACT_VERSION
|
|
30
|
-
)
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
fn workspace_cache_dir_from_out_dir() -> Option<PathBuf> {
|
|
34
|
-
let out_dir = env::var_os("OUT_DIR")?;
|
|
35
|
-
let mut path = PathBuf::from(out_dir);
|
|
36
|
-
for _ in 0..4 {
|
|
37
|
-
if !path.pop() {
|
|
38
|
-
return None;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
Some(path.join("tesseract-rs-cache"))
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
fn get_preferred_out_dir() -> PathBuf {
|
|
45
|
-
if let Ok(custom) = env::var("TESSERACT_RS_CACHE_DIR") {
|
|
46
|
-
return PathBuf::from(custom);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
if cfg!(target_os = "windows") {
|
|
50
|
-
return PathBuf::from("C:\\tess");
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
if let Some(workspace_cache) = workspace_cache_dir_from_out_dir() {
|
|
54
|
-
return workspace_cache;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
if cfg!(target_os = "macos") {
|
|
58
|
-
let home_dir = env::var("HOME").unwrap_or_else(|_| {
|
|
59
|
-
env::var("USER")
|
|
60
|
-
.map(|user| format!("/Users/{}", user))
|
|
61
|
-
.expect("Neither HOME nor USER environment variable set")
|
|
62
|
-
});
|
|
63
|
-
PathBuf::from(home_dir)
|
|
64
|
-
.join("Library")
|
|
65
|
-
.join("Application Support")
|
|
66
|
-
.join("tesseract-rs")
|
|
67
|
-
} else if cfg!(target_os = "linux") {
|
|
68
|
-
let home_dir = env::var("HOME").unwrap_or_else(|_| {
|
|
69
|
-
env::var("USER")
|
|
70
|
-
.map(|user| format!("/home/{}", user))
|
|
71
|
-
.expect("Neither HOME nor USER environment variable set")
|
|
72
|
-
});
|
|
73
|
-
PathBuf::from(home_dir).join(".tesseract-rs")
|
|
74
|
-
} else {
|
|
75
|
-
panic!("Unsupported operating system");
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
fn target_triple() -> String {
|
|
80
|
-
env::var("TARGET").unwrap_or_else(|_| env::var("HOST").unwrap_or_default())
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
fn target_matches(target: &str, needle: &str) -> bool {
|
|
84
|
-
target.contains(needle)
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
fn is_windows_target(target: &str) -> bool {
|
|
88
|
-
target_matches(target, "windows")
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
fn is_macos_target(target: &str) -> bool {
|
|
92
|
-
target_matches(target, "apple-darwin")
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
fn is_linux_target(target: &str) -> bool {
|
|
96
|
-
target_matches(target, "linux")
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
fn is_msvc_target(target: &str) -> bool {
|
|
100
|
-
is_windows_target(target) && target_matches(target, "msvc")
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
fn is_mingw_target(target: &str) -> bool {
|
|
104
|
-
is_windows_target(target) && target_matches(target, "gnu")
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
fn is_wasm_target(target: &str) -> bool {
|
|
108
|
-
target_matches(target, "wasm32") || target_matches(target, "wasm64")
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
fn prepare_out_dir() -> PathBuf {
|
|
112
|
-
let preferred = get_preferred_out_dir();
|
|
113
|
-
match fs::create_dir_all(&preferred) {
|
|
114
|
-
Ok(_) => preferred,
|
|
115
|
-
Err(err) => {
|
|
116
|
-
println!(
|
|
117
|
-
"cargo:warning=Failed to create cache dir {:?}: {}. Falling back to temp dir.",
|
|
118
|
-
preferred, err
|
|
119
|
-
);
|
|
120
|
-
let fallback = env::temp_dir().join("tesseract-rs-cache");
|
|
121
|
-
fs::create_dir_all(&fallback).expect("Failed to create fallback cache directory in temp dir");
|
|
122
|
-
fallback
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
// Detect pre-installed EMSDK (installed via brew, apt, or manually)
|
|
128
|
-
#[allow(dead_code)]
|
|
129
|
-
fn find_emsdk() -> Result<PathBuf, String> {
|
|
130
|
-
// Check EMSDK environment variable first
|
|
131
|
-
if let Ok(emsdk_path) = env::var("EMSDK") {
|
|
132
|
-
let path = PathBuf::from(emsdk_path);
|
|
133
|
-
if path.exists() {
|
|
134
|
-
return Ok(path);
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
// Check common installation locations
|
|
139
|
-
let common_paths = vec![
|
|
140
|
-
PathBuf::from("/opt/homebrew/opt/emscripten"), // Homebrew on Apple Silicon
|
|
141
|
-
PathBuf::from("/usr/local/opt/emscripten"), // Homebrew on Intel Mac
|
|
142
|
-
PathBuf::from(env::var("HOME").unwrap_or_default()).join(".emsdk"),
|
|
143
|
-
PathBuf::from("/usr/lib/emscripten"), // Linux package manager
|
|
144
|
-
PathBuf::from("/opt/emsdk"), // Manual installation
|
|
145
|
-
];
|
|
146
|
-
|
|
147
|
-
for path in common_paths {
|
|
148
|
-
if path.exists() {
|
|
149
|
-
return Ok(path);
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
Err("EMSDK not found. Please install via: brew install emscripten (macOS) or apt-get install emscripten (Linux)".to_string())
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
#[allow(dead_code)]
|
|
157
|
-
fn download_emsdk_legacy(emsdk_dir: &Path) -> PathBuf {
|
|
158
|
-
use std::process::Command;
|
|
159
|
-
|
|
160
|
-
println!("cargo:warning=Setting up EMSDK (downloading and installing toolchain)...");
|
|
161
|
-
|
|
162
|
-
// Create parent directory if it doesn't exist
|
|
163
|
-
if let Some(parent) = emsdk_dir.parent() {
|
|
164
|
-
fs::create_dir_all(parent).expect("Failed to create EMSDK parent directory");
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
// Phase 1: Clone the EMSDK repository
|
|
168
|
-
if !emsdk_dir.exists() {
|
|
169
|
-
println!("cargo:warning=Cloning EMSDK repository from {}", EMSDK_REPOSITORY);
|
|
170
|
-
|
|
171
|
-
let clone_output = Command::new("git")
|
|
172
|
-
.arg("clone")
|
|
173
|
-
.arg("--depth")
|
|
174
|
-
.arg("1")
|
|
175
|
-
.arg(EMSDK_REPOSITORY)
|
|
176
|
-
.arg(emsdk_dir)
|
|
177
|
-
.output()
|
|
178
|
-
.expect("Failed to run git clone for EMSDK");
|
|
179
|
-
|
|
180
|
-
if !clone_output.status.success() {
|
|
181
|
-
panic!(
|
|
182
|
-
"EMSDK repository clone failed: {}",
|
|
183
|
-
String::from_utf8_lossy(&clone_output.stderr)
|
|
184
|
-
);
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
println!("cargo:warning=EMSDK repository cloned successfully");
|
|
188
|
-
} else {
|
|
189
|
-
println!("cargo:warning=EMSDK directory already exists, skipping clone");
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
// Phase 2: Checkout specific commit
|
|
193
|
-
println!("cargo:warning=Checking out EMSDK commit: {}", EMSDK_COMMIT);
|
|
194
|
-
|
|
195
|
-
let fetch_output = Command::new("git")
|
|
196
|
-
.current_dir(emsdk_dir)
|
|
197
|
-
.arg("fetch")
|
|
198
|
-
.arg("origin")
|
|
199
|
-
.arg(EMSDK_COMMIT)
|
|
200
|
-
.output()
|
|
201
|
-
.expect("Failed to run git fetch for EMSDK");
|
|
202
|
-
|
|
203
|
-
if !fetch_output.status.success() {
|
|
204
|
-
eprintln!(
|
|
205
|
-
"Warning: Failed to fetch specific commit, attempting checkout anyway: {}",
|
|
206
|
-
String::from_utf8_lossy(&fetch_output.stderr)
|
|
207
|
-
);
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
let checkout_output = Command::new("git")
|
|
211
|
-
.current_dir(emsdk_dir)
|
|
212
|
-
.arg("checkout")
|
|
213
|
-
.arg(EMSDK_COMMIT)
|
|
214
|
-
.output()
|
|
215
|
-
.expect("Failed to run git checkout for EMSDK");
|
|
216
|
-
|
|
217
|
-
if !checkout_output.status.success() {
|
|
218
|
-
eprintln!(
|
|
219
|
-
"Warning: Failed to checkout specific commit: {}",
|
|
220
|
-
String::from_utf8_lossy(&checkout_output.stderr)
|
|
221
|
-
);
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
println!("cargo:warning=EMSDK checked out to commit: {}", EMSDK_COMMIT);
|
|
225
|
-
|
|
226
|
-
// Phase 3: Make emsdk script executable and patch SSL verification (Unix only)
|
|
227
|
-
#[cfg(unix)]
|
|
228
|
-
{
|
|
229
|
-
use std::os::unix::fs::PermissionsExt;
|
|
230
|
-
let emsdk_script = emsdk_dir.join("emsdk");
|
|
231
|
-
if emsdk_script.exists() {
|
|
232
|
-
let perms = fs::Permissions::from_mode(0o755);
|
|
233
|
-
fs::set_permissions(&emsdk_script, perms)
|
|
234
|
-
.expect("Failed to set executable permissions on emsdk script");
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
// Patch the emsdk.py Python script to disable SSL verification
|
|
238
|
-
let emsdk_py = emsdk_dir.join("emsdk.py");
|
|
239
|
-
if emsdk_py.exists() {
|
|
240
|
-
let script_content = fs::read_to_string(&emsdk_py).expect("Failed to read emsdk.py");
|
|
241
|
-
|
|
242
|
-
if !script_content.contains("ssl._create_unverified_context") {
|
|
243
|
-
let patched_content = script_content.replace(
|
|
244
|
-
"#!/usr/bin/env python",
|
|
245
|
-
"#!/usr/bin/env python\nimport ssl\nssl._create_default_https_context = ssl._create_unverified_context"
|
|
246
|
-
);
|
|
247
|
-
fs::write(&emsdk_py, patched_content).expect("Failed to write patched emsdk.py");
|
|
248
|
-
println!("cargo:warning=Patched emsdk.py to disable SSL verification");
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
// Phase 4: Install the EMSDK toolchain (use system Node to avoid SSL download issues)
|
|
254
|
-
println!("cargo:warning=Installing EMSDK toolchain (this may take several minutes)...");
|
|
255
|
-
|
|
256
|
-
// Find system Node.js
|
|
257
|
-
let node_path = Command::new("which")
|
|
258
|
-
.arg("node")
|
|
259
|
-
.output()
|
|
260
|
-
.ok()
|
|
261
|
-
.and_then(|o| String::from_utf8(o.stdout).ok())
|
|
262
|
-
.map(|s| s.trim().to_string())
|
|
263
|
-
.unwrap_or_else(|| "node".to_string());
|
|
264
|
-
|
|
265
|
-
println!("cargo:warning=Using system Node.js: {}", node_path);
|
|
266
|
-
|
|
267
|
-
// Try to install with system Node - allow partial failure if only Node download fails
|
|
268
|
-
#[cfg(unix)]
|
|
269
|
-
let install_output = Command::new("sh")
|
|
270
|
-
.arg("-c")
|
|
271
|
-
.arg(format!(
|
|
272
|
-
"./emsdk install {} 2>&1 | tee /tmp/emsdk-install.log || true",
|
|
273
|
-
EMSDK_VERSION
|
|
274
|
-
))
|
|
275
|
-
.current_dir(emsdk_dir)
|
|
276
|
-
.env("NODE", &node_path)
|
|
277
|
-
.env("EM_NODE_JS", &node_path)
|
|
278
|
-
.output()
|
|
279
|
-
.expect("Failed to run emsdk install");
|
|
280
|
-
|
|
281
|
-
#[cfg(windows)]
|
|
282
|
-
let install_output = Command::new("cmd")
|
|
283
|
-
.args(&["/C", "emsdk.bat", "install", EMSDK_VERSION])
|
|
284
|
-
.current_dir(emsdk_dir)
|
|
285
|
-
.env("NODE", &node_path)
|
|
286
|
-
.env("EM_NODE_JS", &node_path)
|
|
287
|
-
.output()
|
|
288
|
-
.expect("Failed to run emsdk install");
|
|
289
|
-
|
|
290
|
-
// Check if install succeeded or at least got the compiler (Node download may have failed)
|
|
291
|
-
let install_stderr = String::from_utf8_lossy(&install_output.stderr);
|
|
292
|
-
let critical_failure =
|
|
293
|
-
!install_output.status.success() && !install_stderr.contains("node") && !install_stderr.contains("Node");
|
|
294
|
-
|
|
295
|
-
if critical_failure {
|
|
296
|
-
panic!("EMSDK installation failed: {}", install_stderr);
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
if !install_output.status.success() {
|
|
300
|
-
println!(
|
|
301
|
-
"cargo:warning=EMSDK install had warnings (possibly Node download failed, will use system Node): {}",
|
|
302
|
-
install_stderr
|
|
303
|
-
);
|
|
304
|
-
} else {
|
|
305
|
-
println!("cargo:warning=EMSDK toolchain installed successfully");
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
// Phase 5: Activate the installed toolchain
|
|
309
|
-
println!("cargo:warning=Activating EMSDK toolchain...");
|
|
310
|
-
|
|
311
|
-
#[cfg(unix)]
|
|
312
|
-
let activate_output = Command::new("sh")
|
|
313
|
-
.arg("-c")
|
|
314
|
-
.arg(format!("./emsdk activate {}", EMSDK_VERSION))
|
|
315
|
-
.current_dir(emsdk_dir)
|
|
316
|
-
.output()
|
|
317
|
-
.expect("Failed to run emsdk activate");
|
|
318
|
-
|
|
319
|
-
#[cfg(windows)]
|
|
320
|
-
let activate_output = Command::new("cmd")
|
|
321
|
-
.args(&["/C", "emsdk.bat", "activate", EMSDK_VERSION])
|
|
322
|
-
.current_dir(emsdk_dir)
|
|
323
|
-
.output()
|
|
324
|
-
.expect("Failed to run emsdk activate");
|
|
325
|
-
|
|
326
|
-
if !activate_output.status.success() {
|
|
327
|
-
panic!(
|
|
328
|
-
"EMSDK activation failed: {}",
|
|
329
|
-
String::from_utf8_lossy(&activate_output.stderr)
|
|
330
|
-
);
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
println!("cargo:warning=EMSDK activated successfully");
|
|
334
|
-
|
|
335
|
-
// Phase 6: Verify installation (ISSUE #4: Version-aware path checking for EMSDK v3.1.31)
|
|
336
|
-
println!("cargo:warning=Verifying EMSDK installation...");
|
|
337
|
-
|
|
338
|
-
// Check both old and new EMSDK directory structures
|
|
339
|
-
let possible_toolchain_paths = vec![
|
|
340
|
-
"upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake",
|
|
341
|
-
"emscripten/cmake/Modules/Platform/Emscripten.cmake", // Older structure
|
|
342
|
-
];
|
|
343
|
-
|
|
344
|
-
let possible_emcc_paths = vec![
|
|
345
|
-
"upstream/emscripten/emcc",
|
|
346
|
-
"emscripten/emcc", // Older structure
|
|
347
|
-
];
|
|
348
|
-
|
|
349
|
-
let toolchain_file = possible_toolchain_paths
|
|
350
|
-
.iter()
|
|
351
|
-
.find(|p| emsdk_dir.join(p).exists())
|
|
352
|
-
.map(|p| emsdk_dir.join(p))
|
|
353
|
-
.unwrap_or_else(|| emsdk_dir.join("upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake"));
|
|
354
|
-
|
|
355
|
-
let emcc_path = possible_emcc_paths
|
|
356
|
-
.iter()
|
|
357
|
-
.find(|p| emsdk_dir.join(p).exists())
|
|
358
|
-
.map(|p| emsdk_dir.join(p))
|
|
359
|
-
.unwrap_or_else(|| emsdk_dir.join("upstream/emscripten/emcc"));
|
|
360
|
-
|
|
361
|
-
if !toolchain_file.exists() {
|
|
362
|
-
panic!(
|
|
363
|
-
"EMSDK verification failed: toolchain file not found. Checked paths: {:?}",
|
|
364
|
-
possible_toolchain_paths
|
|
365
|
-
);
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
if !emcc_path.exists() {
|
|
369
|
-
panic!(
|
|
370
|
-
"EMSDK verification failed: emcc not found. Checked paths: {:?}",
|
|
371
|
-
possible_emcc_paths
|
|
372
|
-
);
|
|
373
|
-
}
|
|
374
|
-
|
|
375
|
-
println!("cargo:warning=EMSDK verification completed successfully");
|
|
376
|
-
println!("cargo:warning=EMSDK root directory: {}", emsdk_dir.display());
|
|
377
|
-
println!("cargo:warning=EMSDK toolchain: {}", toolchain_file.display());
|
|
378
|
-
println!("cargo:warning=EMSDK emcc: {}", emcc_path.display());
|
|
379
|
-
|
|
380
|
-
emsdk_dir.to_path_buf()
|
|
381
|
-
}
|
|
382
|
-
|
|
383
|
-
pub fn build() {
|
|
384
|
-
let target = target_triple();
|
|
385
|
-
|
|
386
|
-
// Route WASM targets to WASM build
|
|
387
|
-
if is_wasm_target(&target) {
|
|
388
|
-
println!(
|
|
389
|
-
"cargo:warning=Detected WASM target: {}, routing to build_wasm()",
|
|
390
|
-
target
|
|
391
|
-
);
|
|
392
|
-
return build_wasm();
|
|
393
|
-
}
|
|
394
|
-
|
|
395
|
-
let custom_out_dir = prepare_out_dir();
|
|
396
|
-
let windows_target = is_windows_target(&target);
|
|
397
|
-
let msvc_target = is_msvc_target(&target);
|
|
398
|
-
let mingw_target = is_mingw_target(&target);
|
|
399
|
-
|
|
400
|
-
println!("cargo:warning=custom_out_dir: {:?}", custom_out_dir);
|
|
401
|
-
|
|
402
|
-
let cache_dir = custom_out_dir.join("cache");
|
|
403
|
-
|
|
404
|
-
if env::var("CARGO_CLEAN").is_ok() {
|
|
405
|
-
clean_cache(&cache_dir);
|
|
406
|
-
}
|
|
407
|
-
|
|
408
|
-
std::fs::create_dir_all(&cache_dir).expect("Failed to create cache directory");
|
|
409
|
-
|
|
410
|
-
let out_dir = custom_out_dir.clone();
|
|
411
|
-
let project_dir = custom_out_dir.clone();
|
|
412
|
-
let third_party_dir = project_dir.join("third_party");
|
|
413
|
-
|
|
414
|
-
let leptonica_dir = if third_party_dir.join("leptonica").exists() {
|
|
415
|
-
println!("cargo:warning=Using existing leptonica source");
|
|
416
|
-
third_party_dir.join("leptonica")
|
|
417
|
-
} else {
|
|
418
|
-
fs::create_dir_all(&third_party_dir).expect("Failed to create third_party directory");
|
|
419
|
-
download_and_extract(&third_party_dir, &leptonica_url(), "leptonica")
|
|
420
|
-
};
|
|
421
|
-
|
|
422
|
-
let tesseract_dir = if third_party_dir.join("tesseract").exists() {
|
|
423
|
-
println!("cargo:warning=Using existing tesseract source");
|
|
424
|
-
third_party_dir.join("tesseract")
|
|
425
|
-
} else {
|
|
426
|
-
fs::create_dir_all(&third_party_dir).expect("Failed to create third_party directory");
|
|
427
|
-
download_and_extract(&third_party_dir, &tesseract_url(), "tesseract")
|
|
428
|
-
};
|
|
429
|
-
|
|
430
|
-
let (cmake_cxx_flags, additional_defines) = get_os_specific_config();
|
|
431
|
-
|
|
432
|
-
let leptonica_install_dir = out_dir.join("leptonica");
|
|
433
|
-
let leptonica_cache_dir = cache_dir.join("leptonica");
|
|
434
|
-
|
|
435
|
-
build_or_use_cached("leptonica", &leptonica_cache_dir, &leptonica_install_dir, || {
|
|
436
|
-
let mut leptonica_config = Config::new(&leptonica_dir);
|
|
437
|
-
|
|
438
|
-
let leptonica_src_dir = leptonica_dir.join("src");
|
|
439
|
-
let environ_h_path = leptonica_src_dir.join("environ.h");
|
|
440
|
-
|
|
441
|
-
if environ_h_path.exists() {
|
|
442
|
-
let environ_h = std::fs::read_to_string(&environ_h_path)
|
|
443
|
-
.expect("Failed to read environ.h")
|
|
444
|
-
.replace("#define HAVE_LIBZ 1", "#define HAVE_LIBZ 0")
|
|
445
|
-
.replace("#ifdef NO_CONSOLE_IO", "#define NO_CONSOLE_IO\n#ifdef NO_CONSOLE_IO");
|
|
446
|
-
std::fs::write(environ_h_path, environ_h).expect("Failed to write environ.h");
|
|
447
|
-
}
|
|
448
|
-
|
|
449
|
-
let makefile_static_path = leptonica_dir.join("prog").join("makefile.static");
|
|
450
|
-
|
|
451
|
-
let leptonica_src_cmakelists = leptonica_dir.join("src").join("CMakeLists.txt");
|
|
452
|
-
|
|
453
|
-
if leptonica_src_cmakelists.exists() {
|
|
454
|
-
let cmakelists = std::fs::read_to_string(&leptonica_src_cmakelists)
|
|
455
|
-
.expect("Failed to read leptonica src CMakeLists.txt");
|
|
456
|
-
let patched = cmakelists.replace(
|
|
457
|
-
"if(MINGW)\n set_target_properties(\n leptonica PROPERTIES SUFFIX\n \"-${PROJECT_VERSION}${CMAKE_SHARED_LIBRARY_SUFFIX}\")\nendif(MINGW)\n",
|
|
458
|
-
"if(MINGW AND BUILD_SHARED_LIBS)\n set_target_properties(\n leptonica PROPERTIES SUFFIX\n \"-${PROJECT_VERSION}${CMAKE_SHARED_LIBRARY_SUFFIX}\")\nendif()\n",
|
|
459
|
-
);
|
|
460
|
-
if patched != cmakelists {
|
|
461
|
-
std::fs::write(&leptonica_src_cmakelists, patched)
|
|
462
|
-
.expect("Failed to patch leptonica src CMakeLists.txt");
|
|
463
|
-
}
|
|
464
|
-
}
|
|
465
|
-
|
|
466
|
-
if makefile_static_path.exists() {
|
|
467
|
-
let makefile_static = std::fs::read_to_string(&makefile_static_path)
|
|
468
|
-
.expect("Failed to read makefile.static")
|
|
469
|
-
.replace(
|
|
470
|
-
"ALL_LIBS = $(LEPTLIB) -ltiff -ljpeg -lpng -lz -lm",
|
|
471
|
-
"ALL_LIBS = $(LEPTLIB) -lm",
|
|
472
|
-
);
|
|
473
|
-
std::fs::write(makefile_static_path, makefile_static).expect("Failed to write makefile.static");
|
|
474
|
-
}
|
|
475
|
-
|
|
476
|
-
if windows_target {
|
|
477
|
-
if mingw_target {
|
|
478
|
-
leptonica_config.generator("Unix Makefiles");
|
|
479
|
-
leptonica_config.define("CMAKE_MAKE_PROGRAM", "mingw32-make");
|
|
480
|
-
leptonica_config.define("MSYS2_ARG_CONV_EXCL", "/MD;/MDd;/D;-D;-I;-L");
|
|
481
|
-
} else if msvc_target && env::var("VSINSTALLDIR").is_ok() {
|
|
482
|
-
leptonica_config.generator("NMake Makefiles");
|
|
483
|
-
}
|
|
484
|
-
leptonica_config.define("CMAKE_CL_SHOWINCLUDES_PREFIX", "");
|
|
485
|
-
}
|
|
486
|
-
|
|
487
|
-
if env::var("CI").is_err() && env::var("RUSTC_WRAPPER").unwrap_or_default() == "sccache" {
|
|
488
|
-
leptonica_config.env("CC", "sccache cc").env("CXX", "sccache c++");
|
|
489
|
-
}
|
|
490
|
-
|
|
491
|
-
let leptonica_install_dir_cmake = normalize_cmake_path(&leptonica_install_dir);
|
|
492
|
-
|
|
493
|
-
leptonica_config
|
|
494
|
-
.define("CMAKE_POLICY_VERSION_MINIMUM", "3.5")
|
|
495
|
-
.define("CMAKE_BUILD_TYPE", "Release")
|
|
496
|
-
.define("BUILD_PROG", "OFF")
|
|
497
|
-
.define("BUILD_SHARED_LIBS", "OFF")
|
|
498
|
-
.define("ENABLE_ZLIB", "OFF")
|
|
499
|
-
.define("ENABLE_PNG", "OFF")
|
|
500
|
-
.define("ENABLE_JPEG", "OFF")
|
|
501
|
-
.define("ENABLE_TIFF", "OFF")
|
|
502
|
-
.define("ENABLE_WEBP", "OFF")
|
|
503
|
-
.define("ENABLE_OPENJPEG", "OFF")
|
|
504
|
-
.define("ENABLE_GIF", "OFF")
|
|
505
|
-
.define("NO_CONSOLE_IO", "ON")
|
|
506
|
-
.define("CMAKE_CXX_FLAGS", &cmake_cxx_flags)
|
|
507
|
-
.define("MINIMUM_SEVERITY", "L_SEVERITY_NONE")
|
|
508
|
-
.define("SW_BUILD", "OFF")
|
|
509
|
-
.define("HAVE_LIBZ", "0")
|
|
510
|
-
.define("ENABLE_LTO", "OFF")
|
|
511
|
-
.define("CMAKE_INSTALL_PREFIX", &leptonica_install_dir_cmake);
|
|
512
|
-
|
|
513
|
-
if windows_target {
|
|
514
|
-
if msvc_target {
|
|
515
|
-
leptonica_config
|
|
516
|
-
.define("CMAKE_C_FLAGS_RELEASE", "/MD /O2")
|
|
517
|
-
.define("CMAKE_C_FLAGS_DEBUG", "/MDd /Od");
|
|
518
|
-
} else if mingw_target {
|
|
519
|
-
leptonica_config
|
|
520
|
-
.define("CMAKE_C_FLAGS_RELEASE", "-O2 -DNDEBUG")
|
|
521
|
-
.define("CMAKE_C_FLAGS_DEBUG", "-O0 -g");
|
|
522
|
-
} else {
|
|
523
|
-
leptonica_config
|
|
524
|
-
.define("CMAKE_C_FLAGS_RELEASE", "-O2")
|
|
525
|
-
.define("CMAKE_C_FLAGS_DEBUG", "-O0 -g");
|
|
526
|
-
}
|
|
527
|
-
}
|
|
528
|
-
|
|
529
|
-
for (key, value) in &additional_defines {
|
|
530
|
-
leptonica_config.define(key, value);
|
|
531
|
-
}
|
|
532
|
-
|
|
533
|
-
leptonica_config.build();
|
|
534
|
-
});
|
|
535
|
-
|
|
536
|
-
let leptonica_include_dir = leptonica_install_dir.join("include");
|
|
537
|
-
let leptonica_lib_dir = leptonica_install_dir.join("lib");
|
|
538
|
-
let tesseract_install_dir = out_dir.join("tesseract");
|
|
539
|
-
let tesseract_cache_dir = cache_dir.join("tesseract");
|
|
540
|
-
let tessdata_prefix = project_dir.clone();
|
|
541
|
-
|
|
542
|
-
let leptonica_install_dir_cmake = normalize_cmake_path(&leptonica_install_dir);
|
|
543
|
-
let leptonica_include_dir_cmake = normalize_cmake_path(&leptonica_include_dir);
|
|
544
|
-
let leptonica_lib_dir_cmake = normalize_cmake_path(&leptonica_lib_dir);
|
|
545
|
-
let tesseract_install_dir_cmake = normalize_cmake_path(&tesseract_install_dir);
|
|
546
|
-
let tessdata_prefix_cmake = normalize_cmake_path(&tessdata_prefix);
|
|
547
|
-
|
|
548
|
-
build_or_use_cached("tesseract", &tesseract_cache_dir, &tesseract_install_dir, || {
|
|
549
|
-
let cmakelists_path = tesseract_dir.join("CMakeLists.txt");
|
|
550
|
-
let cmakelists = std::fs::read_to_string(&cmakelists_path)
|
|
551
|
-
.expect("Failed to read CMakeLists.txt")
|
|
552
|
-
.replace("set(HAVE_TIFFIO_H ON)", "");
|
|
553
|
-
std::fs::write(&cmakelists_path, cmakelists).expect("Failed to write CMakeLists.txt");
|
|
554
|
-
|
|
555
|
-
let mut tesseract_config = Config::new(&tesseract_dir);
|
|
556
|
-
if windows_target {
|
|
557
|
-
if mingw_target {
|
|
558
|
-
tesseract_config.generator("Unix Makefiles");
|
|
559
|
-
tesseract_config.define("CMAKE_MAKE_PROGRAM", "mingw32-make");
|
|
560
|
-
tesseract_config.define("MSYS2_ARG_CONV_EXCL", "/MD;/MDd;/D;-D;-I;-L");
|
|
561
|
-
} else if msvc_target && env::var("VSINSTALLDIR").is_ok() {
|
|
562
|
-
tesseract_config.generator("NMake Makefiles");
|
|
563
|
-
}
|
|
564
|
-
tesseract_config.define("CMAKE_CL_SHOWINCLUDES_PREFIX", "");
|
|
565
|
-
}
|
|
566
|
-
|
|
567
|
-
if env::var("CI").is_err() && env::var("RUSTC_WRAPPER").unwrap_or_default() == "sccache" {
|
|
568
|
-
tesseract_config.env("CC", "sccache cc").env("CXX", "sccache c++");
|
|
569
|
-
}
|
|
570
|
-
tesseract_config
|
|
571
|
-
.define("CMAKE_POLICY_VERSION_MINIMUM", "3.5")
|
|
572
|
-
.define("CMAKE_BUILD_TYPE", "Release")
|
|
573
|
-
.define("BUILD_TRAINING_TOOLS", "OFF")
|
|
574
|
-
.define("BUILD_SHARED_LIBS", "OFF")
|
|
575
|
-
.define("DISABLE_ARCHIVE", "ON")
|
|
576
|
-
.define("DISABLE_CURL", "ON")
|
|
577
|
-
.define("DISABLE_OPENCL", "ON")
|
|
578
|
-
.define("Leptonica_DIR", &leptonica_install_dir_cmake)
|
|
579
|
-
.define("LEPTONICA_INCLUDE_DIR", &leptonica_include_dir_cmake)
|
|
580
|
-
.define("LEPTONICA_LIBRARY", &leptonica_lib_dir_cmake)
|
|
581
|
-
.define("CMAKE_PREFIX_PATH", &leptonica_install_dir_cmake)
|
|
582
|
-
.define("CMAKE_INSTALL_PREFIX", &tesseract_install_dir_cmake)
|
|
583
|
-
.define("TESSDATA_PREFIX", &tessdata_prefix_cmake)
|
|
584
|
-
.define("DISABLE_TIFF", "ON")
|
|
585
|
-
.define("DISABLE_PNG", "ON")
|
|
586
|
-
.define("DISABLE_JPEG", "ON")
|
|
587
|
-
.define("DISABLE_WEBP", "ON")
|
|
588
|
-
.define("DISABLE_OPENJPEG", "ON")
|
|
589
|
-
.define("DISABLE_ZLIB", "ON")
|
|
590
|
-
.define("DISABLE_LIBXML2", "ON")
|
|
591
|
-
.define("DISABLE_LIBICU", "ON")
|
|
592
|
-
.define("DISABLE_LZMA", "ON")
|
|
593
|
-
.define("DISABLE_GIF", "ON")
|
|
594
|
-
.define("DISABLE_DEBUG_MESSAGES", "ON")
|
|
595
|
-
.define("debug_file", "/dev/null")
|
|
596
|
-
.define("HAVE_LIBARCHIVE", "OFF")
|
|
597
|
-
.define("HAVE_LIBCURL", "OFF")
|
|
598
|
-
.define("HAVE_TIFFIO_H", "OFF")
|
|
599
|
-
.define("GRAPHICS_DISABLED", "ON")
|
|
600
|
-
.define("DISABLED_LEGACY_ENGINE", "OFF")
|
|
601
|
-
.define("USE_OPENCL", "OFF")
|
|
602
|
-
.define("OPENMP_BUILD", "OFF")
|
|
603
|
-
.define("BUILD_TESTS", "OFF")
|
|
604
|
-
.define("ENABLE_LTO", "OFF")
|
|
605
|
-
.define("BUILD_PROG", "OFF")
|
|
606
|
-
.define("SW_BUILD", "OFF")
|
|
607
|
-
.define("LEPT_TIFF_RESULT", "FALSE")
|
|
608
|
-
.define("INSTALL_CONFIGS", "ON")
|
|
609
|
-
.define("USE_SYSTEM_ICU", "ON")
|
|
610
|
-
.define("CMAKE_CXX_FLAGS", &cmake_cxx_flags);
|
|
611
|
-
|
|
612
|
-
for (key, value) in &additional_defines {
|
|
613
|
-
tesseract_config.define(key, value);
|
|
614
|
-
}
|
|
615
|
-
|
|
616
|
-
tesseract_config.build();
|
|
617
|
-
});
|
|
618
|
-
|
|
619
|
-
println!("cargo:rerun-if-changed=build.rs");
|
|
620
|
-
println!("cargo:rerun-if-changed={}", third_party_dir.display());
|
|
621
|
-
println!("cargo:rerun-if-changed={}", leptonica_dir.display());
|
|
622
|
-
println!("cargo:rerun-if-changed={}", tesseract_dir.display());
|
|
623
|
-
|
|
624
|
-
println!("cargo:rustc-link-search=native={}", leptonica_lib_dir.display());
|
|
625
|
-
println!(
|
|
626
|
-
"cargo:rustc-link-search=native={}",
|
|
627
|
-
tesseract_install_dir.join("lib").display()
|
|
628
|
-
);
|
|
629
|
-
|
|
630
|
-
set_os_specific_link_flags();
|
|
631
|
-
|
|
632
|
-
println!("cargo:warning=Leptonica include dir: {:?}", leptonica_include_dir);
|
|
633
|
-
println!("cargo:warning=Leptonica lib dir: {:?}", leptonica_lib_dir);
|
|
634
|
-
println!("cargo:warning=Tesseract install dir: {:?}", tesseract_install_dir);
|
|
635
|
-
println!("cargo:warning=Tessdata dir: {:?}", tessdata_prefix);
|
|
636
|
-
}
|
|
637
|
-
|
|
638
|
-
fn get_os_specific_config() -> (String, Vec<(String, String)>) {
|
|
639
|
-
let mut cmake_cxx_flags = String::new();
|
|
640
|
-
let mut additional_defines = Vec::new();
|
|
641
|
-
let target = target_triple();
|
|
642
|
-
let target_macos = is_macos_target(&target);
|
|
643
|
-
let target_linux = is_linux_target(&target);
|
|
644
|
-
let target_windows = is_windows_target(&target);
|
|
645
|
-
let target_msvc = is_msvc_target(&target);
|
|
646
|
-
let target_mingw = is_mingw_target(&target);
|
|
647
|
-
let target_musl = target.contains("musl");
|
|
648
|
-
|
|
649
|
-
if target_macos {
|
|
650
|
-
cmake_cxx_flags.push_str("-stdlib=libc++ ");
|
|
651
|
-
cmake_cxx_flags.push_str("-std=c++17 ");
|
|
652
|
-
} else if target_linux {
|
|
653
|
-
cmake_cxx_flags.push_str("-std=c++17 ");
|
|
654
|
-
if target_musl || env::var("CC").map(|cc| cc.contains("clang")).unwrap_or(false) {
|
|
655
|
-
cmake_cxx_flags.push_str("-stdlib=libc++ ");
|
|
656
|
-
let cxx_compiler = env::var("CXX").unwrap_or_else(|_| {
|
|
657
|
-
if let Ok(target) = env::var("TARGET") {
|
|
658
|
-
if target != env::var("HOST").unwrap_or_default() {
|
|
659
|
-
format!("{}-clang++", target)
|
|
660
|
-
} else {
|
|
661
|
-
"clang++".to_string()
|
|
662
|
-
}
|
|
663
|
-
} else {
|
|
664
|
-
"clang++".to_string()
|
|
665
|
-
}
|
|
666
|
-
});
|
|
667
|
-
additional_defines.push(("CMAKE_CXX_COMPILER".to_string(), cxx_compiler));
|
|
668
|
-
} else {
|
|
669
|
-
let cxx_compiler = env::var("CXX").unwrap_or_else(|_| {
|
|
670
|
-
if let Ok(target) = env::var("TARGET") {
|
|
671
|
-
if target != env::var("HOST").unwrap_or_default() {
|
|
672
|
-
format!("{}-g++", target)
|
|
673
|
-
} else {
|
|
674
|
-
"g++".to_string()
|
|
675
|
-
}
|
|
676
|
-
} else {
|
|
677
|
-
"g++".to_string()
|
|
678
|
-
}
|
|
679
|
-
});
|
|
680
|
-
additional_defines.push(("CMAKE_CXX_COMPILER".to_string(), cxx_compiler));
|
|
681
|
-
}
|
|
682
|
-
} else if target_windows {
|
|
683
|
-
if target_msvc {
|
|
684
|
-
cmake_cxx_flags.push_str("/EHsc /MP /std:c++17 /DTESSERACT_STATIC ");
|
|
685
|
-
additional_defines.push(("CMAKE_C_FLAGS_RELEASE".to_string(), "/MD /O2".to_string()));
|
|
686
|
-
additional_defines.push(("CMAKE_C_FLAGS_DEBUG".to_string(), "/MDd /Od".to_string()));
|
|
687
|
-
additional_defines.push((
|
|
688
|
-
"CMAKE_CXX_FLAGS_RELEASE".to_string(),
|
|
689
|
-
"/MD /O2 /DTESSERACT_STATIC".to_string(),
|
|
690
|
-
));
|
|
691
|
-
additional_defines.push((
|
|
692
|
-
"CMAKE_CXX_FLAGS_DEBUG".to_string(),
|
|
693
|
-
"/MDd /Od /DTESSERACT_STATIC".to_string(),
|
|
694
|
-
));
|
|
695
|
-
additional_defines.push(("CMAKE_MSVC_RUNTIME_LIBRARY".to_string(), "MultiThreadedDLL".to_string()));
|
|
696
|
-
} else if target_mingw {
|
|
697
|
-
cmake_cxx_flags.push_str("-std=c++17 -DTESSERACT_STATIC ");
|
|
698
|
-
additional_defines.push(("CMAKE_C_FLAGS_RELEASE".to_string(), "-O2 -DNDEBUG".to_string()));
|
|
699
|
-
additional_defines.push(("CMAKE_C_FLAGS_DEBUG".to_string(), "-O0 -g".to_string()));
|
|
700
|
-
additional_defines.push(("CMAKE_C_COMPILER".to_string(), "gcc".to_string()));
|
|
701
|
-
additional_defines.push(("CMAKE_CXX_COMPILER".to_string(), "g++".to_string()));
|
|
702
|
-
additional_defines.push(("CMAKE_SYSTEM_NAME".to_string(), "Windows".to_string()));
|
|
703
|
-
additional_defines.push((
|
|
704
|
-
"CMAKE_CXX_FLAGS_RELEASE".to_string(),
|
|
705
|
-
"-O2 -DNDEBUG -DTESSERACT_STATIC".to_string(),
|
|
706
|
-
));
|
|
707
|
-
additional_defines.push((
|
|
708
|
-
"CMAKE_CXX_FLAGS_DEBUG".to_string(),
|
|
709
|
-
"-O0 -g -DTESSERACT_STATIC".to_string(),
|
|
710
|
-
));
|
|
711
|
-
} else {
|
|
712
|
-
cmake_cxx_flags.push_str("-std=c++17 -DTESSERACT_STATIC ");
|
|
713
|
-
additional_defines.push(("CMAKE_C_FLAGS_RELEASE".to_string(), "-O2 -DNDEBUG".to_string()));
|
|
714
|
-
additional_defines.push(("CMAKE_C_FLAGS_DEBUG".to_string(), "-O0 -g".to_string()));
|
|
715
|
-
additional_defines.push((
|
|
716
|
-
"CMAKE_CXX_FLAGS_RELEASE".to_string(),
|
|
717
|
-
"-O2 -DNDEBUG -DTESSERACT_STATIC".to_string(),
|
|
718
|
-
));
|
|
719
|
-
additional_defines.push((
|
|
720
|
-
"CMAKE_CXX_FLAGS_DEBUG".to_string(),
|
|
721
|
-
"-O0 -g -DTESSERACT_STATIC".to_string(),
|
|
722
|
-
));
|
|
723
|
-
}
|
|
724
|
-
}
|
|
725
|
-
|
|
726
|
-
cmake_cxx_flags.push_str("-DUSE_STD_NAMESPACE ");
|
|
727
|
-
additional_defines.push(("CMAKE_POSITION_INDEPENDENT_CODE".to_string(), "ON".to_string()));
|
|
728
|
-
|
|
729
|
-
if target_windows && target_msvc {
|
|
730
|
-
cmake_cxx_flags.push_str("/permissive- ");
|
|
731
|
-
additional_defines.push(("CMAKE_EXE_LINKER_FLAGS".to_string(), "/INCREMENTAL:NO".to_string()));
|
|
732
|
-
additional_defines.push(("CMAKE_SHARED_LINKER_FLAGS".to_string(), "/INCREMENTAL:NO".to_string()));
|
|
733
|
-
additional_defines.push(("CMAKE_MODULE_LINKER_FLAGS".to_string(), "/INCREMENTAL:NO".to_string()));
|
|
734
|
-
}
|
|
735
|
-
|
|
736
|
-
(cmake_cxx_flags, additional_defines)
|
|
737
|
-
}
|
|
738
|
-
|
|
739
|
-
fn set_os_specific_link_flags() {
|
|
740
|
-
let target = target_triple();
|
|
741
|
-
let target_macos = is_macos_target(&target);
|
|
742
|
-
let target_linux = is_linux_target(&target);
|
|
743
|
-
let target_windows = is_windows_target(&target);
|
|
744
|
-
let target_mingw = is_mingw_target(&target);
|
|
745
|
-
let target_musl = target.contains("musl");
|
|
746
|
-
|
|
747
|
-
if target_macos {
|
|
748
|
-
println!("cargo:rustc-link-lib=c++");
|
|
749
|
-
} else if target_linux {
|
|
750
|
-
if target_musl || env::var("CC").map(|cc| cc.contains("clang")).unwrap_or(false) {
|
|
751
|
-
println!("cargo:rustc-link-lib=c++");
|
|
752
|
-
} else {
|
|
753
|
-
println!("cargo:rustc-link-lib=stdc++");
|
|
754
|
-
println!("cargo:rustc-link-lib=stdc++fs");
|
|
755
|
-
}
|
|
756
|
-
println!("cargo:rustc-link-lib=pthread");
|
|
757
|
-
println!("cargo:rustc-link-lib=m");
|
|
758
|
-
println!("cargo:rustc-link-lib=dl");
|
|
759
|
-
} else if target_windows {
|
|
760
|
-
if target_mingw {
|
|
761
|
-
println!("cargo:rustc-link-lib=stdc++");
|
|
762
|
-
}
|
|
763
|
-
println!("cargo:rustc-link-lib=user32");
|
|
764
|
-
println!("cargo:rustc-link-lib=gdi32");
|
|
765
|
-
println!("cargo:rustc-link-lib=ws2_32");
|
|
766
|
-
println!("cargo:rustc-link-lib=advapi32");
|
|
767
|
-
println!("cargo:rustc-link-lib=shell32");
|
|
768
|
-
}
|
|
769
|
-
|
|
770
|
-
println!("cargo:rustc-link-search=native={}", env::var("OUT_DIR").unwrap());
|
|
771
|
-
}
|
|
772
|
-
|
|
773
|
-
fn download_and_extract(target_dir: &Path, url: &str, name: &str) -> PathBuf {
|
|
774
|
-
use reqwest::blocking::Client;
|
|
775
|
-
use zip::ZipArchive;
|
|
776
|
-
|
|
777
|
-
fs::create_dir_all(target_dir).expect("Failed to create target directory");
|
|
778
|
-
|
|
779
|
-
let client = Client::builder()
|
|
780
|
-
.timeout(std::time::Duration::from_secs(300))
|
|
781
|
-
.build()
|
|
782
|
-
.expect("Failed to create HTTP client");
|
|
783
|
-
|
|
784
|
-
println!("cargo:warning=Downloading {} from {}", name, url);
|
|
785
|
-
let max_attempts = 5;
|
|
786
|
-
let mut response = None;
|
|
787
|
-
|
|
788
|
-
for attempt in 1..=max_attempts {
|
|
789
|
-
let err_msg = match client.get(url).send() {
|
|
790
|
-
Ok(resp) if resp.status().is_success() => {
|
|
791
|
-
response = Some(resp);
|
|
792
|
-
break;
|
|
793
|
-
}
|
|
794
|
-
Ok(resp) => format!("HTTP {}", resp.status()),
|
|
795
|
-
Err(err) => err.to_string(),
|
|
796
|
-
};
|
|
797
|
-
|
|
798
|
-
if attempt == max_attempts {
|
|
799
|
-
panic!(
|
|
800
|
-
"Failed to download {} after {} attempts: {}",
|
|
801
|
-
name, max_attempts, err_msg
|
|
802
|
-
);
|
|
803
|
-
}
|
|
804
|
-
|
|
805
|
-
let backoff = 2u64.pow((attempt - 1).min(4));
|
|
806
|
-
println!(
|
|
807
|
-
"cargo:warning=Download attempt {}/{} for {} failed ({}). Retrying in {}s...",
|
|
808
|
-
attempt, max_attempts, name, err_msg, backoff
|
|
809
|
-
);
|
|
810
|
-
std::thread::sleep(std::time::Duration::from_secs(backoff));
|
|
811
|
-
}
|
|
812
|
-
|
|
813
|
-
let mut response = response.expect("unreachable: download loop must either succeed or panic");
|
|
814
|
-
|
|
815
|
-
let mut content = Vec::new();
|
|
816
|
-
response.copy_to(&mut content).expect("Failed to read archive content");
|
|
817
|
-
|
|
818
|
-
println!("cargo:warning=Downloaded {} bytes for {}", content.len(), name);
|
|
819
|
-
|
|
820
|
-
let temp_file = target_dir.join(format!("{}.zip", name));
|
|
821
|
-
fs::write(&temp_file, content).expect("Failed to write archive to file");
|
|
822
|
-
|
|
823
|
-
let extract_dir = target_dir.join(name);
|
|
824
|
-
if extract_dir.exists() {
|
|
825
|
-
fs::remove_dir_all(&extract_dir).expect("Failed to remove existing directory");
|
|
826
|
-
}
|
|
827
|
-
fs::create_dir_all(&extract_dir).expect("Failed to create extraction directory");
|
|
828
|
-
|
|
829
|
-
let mut archive = ZipArchive::new(fs::File::open(&temp_file).unwrap()).unwrap();
|
|
830
|
-
|
|
831
|
-
for i in 0..archive.len() {
|
|
832
|
-
let mut file = archive.by_index(i).unwrap();
|
|
833
|
-
let file_path = file.mangled_name();
|
|
834
|
-
let file_path = file_path.to_str().unwrap();
|
|
835
|
-
|
|
836
|
-
let path = Path::new(file_path);
|
|
837
|
-
let path = path.strip_prefix(path.components().next().unwrap()).unwrap();
|
|
838
|
-
|
|
839
|
-
if path.as_os_str().is_empty() {
|
|
840
|
-
continue;
|
|
841
|
-
}
|
|
842
|
-
|
|
843
|
-
let target_path = extract_dir.join(path);
|
|
844
|
-
|
|
845
|
-
if file.is_dir() {
|
|
846
|
-
fs::create_dir_all(target_path).unwrap();
|
|
847
|
-
} else {
|
|
848
|
-
if let Some(parent) = target_path.parent() {
|
|
849
|
-
fs::create_dir_all(parent).unwrap();
|
|
850
|
-
}
|
|
851
|
-
let mut outfile = fs::File::create(target_path).unwrap();
|
|
852
|
-
std::io::copy(&mut file, &mut outfile).unwrap();
|
|
853
|
-
}
|
|
854
|
-
}
|
|
855
|
-
|
|
856
|
-
fs::remove_file(temp_file).expect("Failed to remove temporary zip file");
|
|
857
|
-
|
|
858
|
-
extract_dir
|
|
859
|
-
}
|
|
860
|
-
|
|
861
|
-
fn normalize_cmake_path(path: &Path) -> String {
|
|
862
|
-
path.to_string_lossy().replace('\\', "/")
|
|
863
|
-
}
|
|
864
|
-
|
|
865
|
-
fn clean_cache(cache_dir: &Path) {
|
|
866
|
-
println!("Cleaning cache directory: {:?}", cache_dir);
|
|
867
|
-
if cache_dir.exists() {
|
|
868
|
-
fs::remove_dir_all(cache_dir).expect("Failed to remove cache directory");
|
|
869
|
-
}
|
|
870
|
-
}
|
|
871
|
-
|
|
872
|
-
#[allow(dead_code)]
|
|
873
|
-
fn apply_patches(src_dir: &Path, patch_dir: &Path) -> std::io::Result<()> {
|
|
874
|
-
use std::process::Command;
|
|
875
|
-
|
|
876
|
-
let patch_file = patch_dir.join("tesseract.diff");
|
|
877
|
-
if !patch_file.exists() {
|
|
878
|
-
println!("cargo:warning=Patch file not found: {}", patch_file.display());
|
|
879
|
-
return Ok(());
|
|
880
|
-
}
|
|
881
|
-
|
|
882
|
-
println!("cargo:warning=Applying patches from: {}", patch_file.display());
|
|
883
|
-
|
|
884
|
-
let output = Command::new("patch")
|
|
885
|
-
.arg("-p1")
|
|
886
|
-
.current_dir(src_dir)
|
|
887
|
-
.stdin(fs::File::open(&patch_file)?)
|
|
888
|
-
.output()?;
|
|
889
|
-
|
|
890
|
-
if !output.status.success() {
|
|
891
|
-
let stderr = String::from_utf8_lossy(&output.stderr);
|
|
892
|
-
println!("cargo:warning=Patch output: {}", stderr);
|
|
893
|
-
// Don't fail on patch errors, they might be already applied
|
|
894
|
-
}
|
|
895
|
-
|
|
896
|
-
Ok(())
|
|
897
|
-
}
|
|
898
|
-
|
|
899
|
-
// ISSUE #5: Setup EMSDK environment variables before CMake builds
|
|
900
|
-
#[allow(dead_code)]
|
|
901
|
-
fn setup_emsdk_environment(emsdk_root: &Path) {
|
|
902
|
-
let emsdk_root_str = emsdk_root.to_string_lossy().to_string();
|
|
903
|
-
|
|
904
|
-
// Set EMSDK root directory
|
|
905
|
-
unsafe {
|
|
906
|
-
env::set_var("EMSDK", &emsdk_root_str);
|
|
907
|
-
}
|
|
908
|
-
println!("cargo:warning=Set EMSDK={}", emsdk_root_str);
|
|
909
|
-
|
|
910
|
-
// Set EMSCRIPTEN path for Emscripten compiler directory
|
|
911
|
-
let emscripten_path = format!("{}/upstream/emscripten", emsdk_root_str);
|
|
912
|
-
unsafe {
|
|
913
|
-
env::set_var("EMSCRIPTEN", &emscripten_path);
|
|
914
|
-
}
|
|
915
|
-
println!("cargo:warning=Set EMSCRIPTEN={}", emscripten_path);
|
|
916
|
-
|
|
917
|
-
// Set EM_CONFIG for .emscripten config file
|
|
918
|
-
let em_config = format!("{}/.emscripten", emsdk_root_str);
|
|
919
|
-
unsafe {
|
|
920
|
-
env::set_var("EM_CONFIG", &em_config);
|
|
921
|
-
}
|
|
922
|
-
println!("cargo:warning=Set EM_CONFIG={}", em_config);
|
|
923
|
-
|
|
924
|
-
// Update PATH with EMSDK tools directory
|
|
925
|
-
let emsdk_bin_paths = [
|
|
926
|
-
format!("{}/upstream/emscripten", emsdk_root_str),
|
|
927
|
-
format!("{}/upstream/bin", emsdk_root_str),
|
|
928
|
-
];
|
|
929
|
-
|
|
930
|
-
let current_path = env::var("PATH").unwrap_or_default();
|
|
931
|
-
let new_path = format!(
|
|
932
|
-
"{}{}{}",
|
|
933
|
-
emsdk_bin_paths.join(":"),
|
|
934
|
-
if current_path.is_empty() { "" } else { ":" },
|
|
935
|
-
current_path
|
|
936
|
-
);
|
|
937
|
-
unsafe {
|
|
938
|
-
env::set_var("PATH", &new_path);
|
|
939
|
-
}
|
|
940
|
-
println!("cargo:warning=Updated PATH with EMSDK tools");
|
|
941
|
-
}
|
|
942
|
-
|
|
943
|
-
// ISSUE #7: Build Leptonica for WASM
|
|
944
|
-
#[allow(dead_code)]
|
|
945
|
-
fn build_leptonica_wasm(leptonica_src: &Path, leptonica_install: &Path, emsdk_dir: &Path) {
|
|
946
|
-
let toolchain_file = emsdk_dir.join("upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake");
|
|
947
|
-
|
|
948
|
-
let mut config = Config::new(leptonica_src);
|
|
949
|
-
|
|
950
|
-
// Set the EMSDK toolchain file
|
|
951
|
-
config.define("CMAKE_TOOLCHAIN_FILE", &toolchain_file);
|
|
952
|
-
|
|
953
|
-
// Image format support disabled (handled by browser)
|
|
954
|
-
config
|
|
955
|
-
.define("CMAKE_BUILD_TYPE", "Release")
|
|
956
|
-
.define("CMAKE_POLICY_VERSION_MINIMUM", "3.5")
|
|
957
|
-
.define("LIBWEBP_SUPPORT", "OFF")
|
|
958
|
-
.define("OPENJPEG_SUPPORT", "OFF")
|
|
959
|
-
.define("ENABLE_ZLIB", "OFF")
|
|
960
|
-
.define("ENABLE_PNG", "OFF")
|
|
961
|
-
.define("ENABLE_JPEG", "OFF")
|
|
962
|
-
.define("ENABLE_TIFF", "OFF")
|
|
963
|
-
.define("ENABLE_WEBP", "OFF")
|
|
964
|
-
.define("ENABLE_OPENJPEG", "OFF")
|
|
965
|
-
.define("ENABLE_GIF", "OFF")
|
|
966
|
-
// Build configuration
|
|
967
|
-
.define("BUILD_PROG", "OFF")
|
|
968
|
-
.define("BUILD_SHARED_LIBS", "OFF")
|
|
969
|
-
.define("NO_CONSOLE_IO", "ON")
|
|
970
|
-
.define("HAVE_LIBZ", "0")
|
|
971
|
-
.define("ENABLE_LTO", "OFF")
|
|
972
|
-
.define("CMAKE_INSTALL_PREFIX", leptonica_install);
|
|
973
|
-
|
|
974
|
-
config.build();
|
|
975
|
-
}
|
|
976
|
-
|
|
977
|
-
// ISSUE #7: Implement build_wasm() with proper Leptonica and Tesseract integration
|
|
978
|
-
#[allow(dead_code)]
|
|
979
|
-
fn build_wasm() {
|
|
980
|
-
println!("cargo:warning=Building for WASM target with Emscripten SDK");
|
|
981
|
-
|
|
982
|
-
let custom_out_dir = prepare_out_dir();
|
|
983
|
-
let cache_dir = custom_out_dir.join("cache");
|
|
984
|
-
fs::create_dir_all(&cache_dir).expect("Failed to create cache directory");
|
|
985
|
-
|
|
986
|
-
let project_dir = custom_out_dir.clone();
|
|
987
|
-
let third_party_dir = project_dir.join("third_party");
|
|
988
|
-
|
|
989
|
-
// Step 1: Find pre-installed EMSDK
|
|
990
|
-
println!("cargo:warning=Looking for pre-installed Emscripten SDK...");
|
|
991
|
-
let emsdk_dir = match find_emsdk() {
|
|
992
|
-
Ok(path) => {
|
|
993
|
-
println!("cargo:warning=Found EMSDK at: {}", path.display());
|
|
994
|
-
path
|
|
995
|
-
}
|
|
996
|
-
Err(err) => {
|
|
997
|
-
panic!("{}
|
|
998
|
-
|
|
999
|
-
Installation instructions:
|
|
1000
|
-
macOS: brew install emscripten
|
|
1001
|
-
Ubuntu: sudo apt-get install emscripten
|
|
1002
|
-
Arch: sudo pacman -S emscripten
|
|
1003
|
-
Manual: git clone https://github.com/emscripten-core/emsdk.git ~/.emsdk && cd ~/.emsdk && ./emsdk install latest && ./emsdk activate latest
|
|
1004
|
-
|
|
1005
|
-
After installation, set EMSDK environment variable or ensure it's in a standard location.", err);
|
|
1006
|
-
}
|
|
1007
|
-
};
|
|
1008
|
-
|
|
1009
|
-
// Setup EMSDK environment variables
|
|
1010
|
-
setup_emsdk_environment(&emsdk_dir);
|
|
1011
|
-
|
|
1012
|
-
// Step 2: Download Leptonica and Tesseract sources if not already present
|
|
1013
|
-
let leptonica_dir = if third_party_dir.join("leptonica").exists() {
|
|
1014
|
-
println!("cargo:warning=Using existing leptonica source");
|
|
1015
|
-
third_party_dir.join("leptonica")
|
|
1016
|
-
} else {
|
|
1017
|
-
fs::create_dir_all(&third_party_dir).expect("Failed to create third_party directory");
|
|
1018
|
-
download_and_extract(&third_party_dir, &leptonica_url(), "leptonica")
|
|
1019
|
-
};
|
|
1020
|
-
|
|
1021
|
-
let tesseract_dir = if third_party_dir.join("tesseract").exists() {
|
|
1022
|
-
println!("cargo:warning=Using existing tesseract source");
|
|
1023
|
-
third_party_dir.join("tesseract")
|
|
1024
|
-
} else {
|
|
1025
|
-
fs::create_dir_all(&third_party_dir).expect("Failed to create third_party directory");
|
|
1026
|
-
download_and_extract(&third_party_dir, &tesseract_url(), "tesseract")
|
|
1027
|
-
};
|
|
1028
|
-
|
|
1029
|
-
// Step 3: Build Leptonica for WASM
|
|
1030
|
-
let leptonica_install_dir = custom_out_dir.join("leptonica");
|
|
1031
|
-
let leptonica_cache_dir = cache_dir.join("leptonica");
|
|
1032
|
-
|
|
1033
|
-
build_or_use_cached("leptonica", &leptonica_cache_dir, &leptonica_install_dir, || {
|
|
1034
|
-
println!("cargo:warning=Building Leptonica for WASM...");
|
|
1035
|
-
build_leptonica_wasm(&leptonica_dir, &leptonica_install_dir, &emsdk_dir);
|
|
1036
|
-
});
|
|
1037
|
-
|
|
1038
|
-
// Step 4: Build Tesseract for WASM (SIMD version for modern browsers)
|
|
1039
|
-
let tesseract_install_dir = custom_out_dir.join("tesseract");
|
|
1040
|
-
let tesseract_cache_dir = cache_dir.join("tesseract");
|
|
1041
|
-
|
|
1042
|
-
build_or_use_cached("tesseract", &tesseract_cache_dir, &tesseract_install_dir, || {
|
|
1043
|
-
println!("cargo:warning=Building Tesseract for WASM (SIMD enabled)...");
|
|
1044
|
-
build_tesseract_wasm(
|
|
1045
|
-
&tesseract_dir,
|
|
1046
|
-
&tesseract_install_dir,
|
|
1047
|
-
&leptonica_install_dir,
|
|
1048
|
-
&emsdk_dir,
|
|
1049
|
-
true, // enable_simd
|
|
1050
|
-
);
|
|
1051
|
-
});
|
|
1052
|
-
|
|
1053
|
-
// Step 5: Setup linker paths for WASM libraries (static linking)
|
|
1054
|
-
let leptonica_lib_dir = leptonica_install_dir.join("lib");
|
|
1055
|
-
let tesseract_lib_dir = tesseract_install_dir.join("lib");
|
|
1056
|
-
|
|
1057
|
-
println!("cargo:rustc-link-search=native={}", leptonica_lib_dir.display());
|
|
1058
|
-
println!("cargo:rustc-link-search=native={}", tesseract_lib_dir.display());
|
|
1059
|
-
|
|
1060
|
-
// For WASM, always use static linking
|
|
1061
|
-
println!("cargo:rustc-link-lib=static=tesseract");
|
|
1062
|
-
println!("cargo:rustc-link-lib=static=leptonica");
|
|
1063
|
-
|
|
1064
|
-
println!("cargo:warning=WASM build completed successfully!");
|
|
1065
|
-
println!("cargo:warning=Leptonica install dir: {:?}", leptonica_install_dir);
|
|
1066
|
-
println!("cargo:warning=Tesseract install dir: {:?}", tesseract_install_dir);
|
|
1067
|
-
}
|
|
1068
|
-
|
|
1069
|
-
#[allow(dead_code)]
|
|
1070
|
-
fn build_tesseract_wasm(
|
|
1071
|
-
src_dir: &Path,
|
|
1072
|
-
tesseract_install: &Path,
|
|
1073
|
-
leptonica_install: &Path,
|
|
1074
|
-
emsdk_dir: &Path,
|
|
1075
|
-
enable_simd: bool,
|
|
1076
|
-
) {
|
|
1077
|
-
let toolchain_file = emsdk_dir.join("upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake");
|
|
1078
|
-
|
|
1079
|
-
let mut config = Config::new(src_dir);
|
|
1080
|
-
|
|
1081
|
-
// Set CMake toolchain for Emscripten
|
|
1082
|
-
config.define("CMAKE_TOOLCHAIN_FILE", &toolchain_file);
|
|
1083
|
-
|
|
1084
|
-
// Link against WASM Leptonica
|
|
1085
|
-
config.define("Leptonica_DIR", leptonica_install);
|
|
1086
|
-
config.define("CMAKE_PREFIX_PATH", leptonica_install);
|
|
1087
|
-
|
|
1088
|
-
// ISSUE #6: Complete CMAKE_CXX_FLAGS for WASM with all WASM-specific flags
|
|
1089
|
-
let mut cxx_flags = String::from("-DTESSERACT_IMAGEDATA_AS_PIX ");
|
|
1090
|
-
if enable_simd {
|
|
1091
|
-
cxx_flags.push_str("-msimd128 ");
|
|
1092
|
-
}
|
|
1093
|
-
cxx_flags.push_str("-fno-exceptions -fPIC -Os");
|
|
1094
|
-
|
|
1095
|
-
// ISSUE #6: Also add CMAKE_C_FLAGS (was completely missing before)
|
|
1096
|
-
let c_flags = "-fno-exceptions -fPIC -Os";
|
|
1097
|
-
|
|
1098
|
-
// Tesseract WASM configuration (from EMSDK_TECHNICAL_REFERENCE.md)
|
|
1099
|
-
config
|
|
1100
|
-
.define("CMAKE_BUILD_TYPE", "Release")
|
|
1101
|
-
.define("CMAKE_POLICY_VERSION_MINIMUM", "3.5")
|
|
1102
|
-
// Core Tesseract settings
|
|
1103
|
-
.define("BUILD_TESSERACT_BINARY", "OFF")
|
|
1104
|
-
.define("BUILD_TRAINING_TOOLS", "OFF")
|
|
1105
|
-
.define("INSTALL_CONFIGS", "ON")
|
|
1106
|
-
.define("BUILD_TESTS", "OFF")
|
|
1107
|
-
.define("BUILD_PROG", "OFF")
|
|
1108
|
-
.define("SYNTAX_LOG", "OFF")
|
|
1109
|
-
// Disable unnecessary features
|
|
1110
|
-
.define("DISABLE_ARCHIVE", "ON")
|
|
1111
|
-
.define("DISABLE_CURL", "ON")
|
|
1112
|
-
.define("DISABLE_OPENCL", "ON")
|
|
1113
|
-
.define("DISABLE_TIFF", "ON")
|
|
1114
|
-
.define("DISABLE_PNG", "ON")
|
|
1115
|
-
.define("DISABLE_JPEG", "ON")
|
|
1116
|
-
.define("DISABLE_WEBP", "ON")
|
|
1117
|
-
.define("DISABLE_OPENJPEG", "ON")
|
|
1118
|
-
.define("DISABLE_ZLIB", "ON")
|
|
1119
|
-
.define("DISABLE_LIBXML2", "ON")
|
|
1120
|
-
.define("DISABLE_LIBICU", "ON")
|
|
1121
|
-
.define("DISABLE_LZMA", "ON")
|
|
1122
|
-
.define("DISABLE_GIF", "ON")
|
|
1123
|
-
.define("DISABLE_DEBUG_MESSAGES", "ON")
|
|
1124
|
-
// WASM-specific settings
|
|
1125
|
-
.define("GRAPHICS_DISABLED", "ON")
|
|
1126
|
-
.define("DISABLED_LEGACY_ENGINE", "ON")
|
|
1127
|
-
.define("USE_OPENCL", "OFF")
|
|
1128
|
-
.define("OPENMP_BUILD", "OFF")
|
|
1129
|
-
.define("ENABLE_LTO", "ON")
|
|
1130
|
-
// SIMD configuration
|
|
1131
|
-
.define("HAVE_SSE4_1", if enable_simd { "ON" } else { "OFF" })
|
|
1132
|
-
.define("HAVE_AVX", "OFF")
|
|
1133
|
-
.define("HAVE_AVX2", "OFF")
|
|
1134
|
-
.define("HAVE_AVX512F", "OFF")
|
|
1135
|
-
.define("HAVE_FMA", "OFF")
|
|
1136
|
-
// Installation directory
|
|
1137
|
-
.define("CMAKE_INSTALL_PREFIX", tesseract_install)
|
|
1138
|
-
// Compiler flags with WASM-specific settings
|
|
1139
|
-
.define("CMAKE_CXX_FLAGS", &cxx_flags)
|
|
1140
|
-
.define("CMAKE_C_FLAGS", c_flags);
|
|
1141
|
-
|
|
1142
|
-
// Build the library
|
|
1143
|
-
config.build();
|
|
1144
|
-
}
|
|
1145
|
-
|
|
1146
|
-
fn build_or_use_cached<F>(name: &str, cache_dir: &Path, install_dir: &Path, build_fn: F)
|
|
1147
|
-
where
|
|
1148
|
-
F: FnOnce(),
|
|
1149
|
-
{
|
|
1150
|
-
let target_env = env::var("CARGO_CFG_TARGET_ENV").unwrap_or_default();
|
|
1151
|
-
let target_triple = env::var("TARGET")
|
|
1152
|
-
.unwrap_or_else(|_| env::var("CARGO_CFG_TARGET_ARCH").unwrap_or_else(|_| "unknown".to_string()));
|
|
1153
|
-
let is_windows = target_triple.contains("windows");
|
|
1154
|
-
let is_windows_gnu = is_windows && target_env == "gnu";
|
|
1155
|
-
|
|
1156
|
-
let lib_name = if is_windows && !is_windows_gnu {
|
|
1157
|
-
format!("{}.lib", name)
|
|
1158
|
-
} else {
|
|
1159
|
-
format!("lib{}.a", name)
|
|
1160
|
-
};
|
|
1161
|
-
|
|
1162
|
-
let cached_path = cache_dir.join(&lib_name);
|
|
1163
|
-
let marker_path = cache_dir.join(format!("{}.target", name));
|
|
1164
|
-
let out_path = install_dir.join("lib").join(&lib_name);
|
|
1165
|
-
|
|
1166
|
-
let possible_lib_names: Vec<String> = if is_windows {
|
|
1167
|
-
let mut base = match name {
|
|
1168
|
-
"leptonica" => vec![
|
|
1169
|
-
"leptonica.lib".to_string(),
|
|
1170
|
-
"libleptonica.lib".to_string(),
|
|
1171
|
-
"leptonica-static.lib".to_string(),
|
|
1172
|
-
"leptonica-1.84.1.lib".to_string(),
|
|
1173
|
-
"leptonica-1.86.0.lib".to_string(),
|
|
1174
|
-
"leptonicad.lib".to_string(),
|
|
1175
|
-
"libleptonica_d.lib".to_string(),
|
|
1176
|
-
"leptonica-1.84.1d.lib".to_string(),
|
|
1177
|
-
"leptonica-1.86.0d.lib".to_string(),
|
|
1178
|
-
],
|
|
1179
|
-
"tesseract" => vec![
|
|
1180
|
-
"tesseract.lib".to_string(),
|
|
1181
|
-
"libtesseract.lib".to_string(),
|
|
1182
|
-
"tesseract-static.lib".to_string(),
|
|
1183
|
-
"tesseract53.lib".to_string(),
|
|
1184
|
-
"tesseract54.lib".to_string(),
|
|
1185
|
-
"tesseract55.lib".to_string(),
|
|
1186
|
-
"tesseractd.lib".to_string(),
|
|
1187
|
-
"libtesseract_d.lib".to_string(),
|
|
1188
|
-
"tesseract53d.lib".to_string(),
|
|
1189
|
-
"tesseract54d.lib".to_string(),
|
|
1190
|
-
"tesseract55d.lib".to_string(),
|
|
1191
|
-
],
|
|
1192
|
-
_ => vec![format!("{}.lib", name)],
|
|
1193
|
-
};
|
|
1194
|
-
|
|
1195
|
-
if is_windows_gnu {
|
|
1196
|
-
match name {
|
|
1197
|
-
"leptonica" => {
|
|
1198
|
-
base.push(format!("libleptonica-{}.a", LEPTONICA_VERSION));
|
|
1199
|
-
base.push("libleptonica.a".to_string());
|
|
1200
|
-
}
|
|
1201
|
-
"tesseract" => {
|
|
1202
|
-
base.push(format!("libtesseract{}.a", TESSERACT_VERSION.replace('.', "")));
|
|
1203
|
-
base.push("libtesseract.a".to_string());
|
|
1204
|
-
base.push("libtesseract55.a".to_string());
|
|
1205
|
-
}
|
|
1206
|
-
_ => {
|
|
1207
|
-
base.push(format!("lib{}.a", name));
|
|
1208
|
-
}
|
|
1209
|
-
}
|
|
1210
|
-
}
|
|
1211
|
-
|
|
1212
|
-
base
|
|
1213
|
-
} else {
|
|
1214
|
-
vec![format!("lib{}.a", name)]
|
|
1215
|
-
};
|
|
1216
|
-
|
|
1217
|
-
fs::create_dir_all(cache_dir).expect("Failed to create cache directory");
|
|
1218
|
-
fs::create_dir_all(out_path.parent().unwrap()).expect("Failed to create output directory");
|
|
1219
|
-
|
|
1220
|
-
let candidate_lib_dirs = [
|
|
1221
|
-
install_dir.join("lib"),
|
|
1222
|
-
install_dir.join("lib64"),
|
|
1223
|
-
install_dir.join("lib").join("tesseract"),
|
|
1224
|
-
];
|
|
1225
|
-
|
|
1226
|
-
let cache_valid = cached_path.exists()
|
|
1227
|
-
&& {
|
|
1228
|
-
match fs::read_to_string(&marker_path) {
|
|
1229
|
-
Ok(cached_target) => {
|
|
1230
|
-
let valid = cached_target.trim() == target_triple;
|
|
1231
|
-
if !valid {
|
|
1232
|
-
println!(
|
|
1233
|
-
"cargo:warning=Cached {} library is for wrong architecture (cached: {}, current: {}), rebuilding",
|
|
1234
|
-
name,
|
|
1235
|
-
cached_target.trim(),
|
|
1236
|
-
target_triple
|
|
1237
|
-
);
|
|
1238
|
-
let _ = fs::remove_file(&cached_path);
|
|
1239
|
-
let _ = fs::remove_file(&marker_path);
|
|
1240
|
-
}
|
|
1241
|
-
valid
|
|
1242
|
-
}
|
|
1243
|
-
Err(_) => {
|
|
1244
|
-
println!(
|
|
1245
|
-
"cargo:warning=Cached {} library missing target marker, rebuilding",
|
|
1246
|
-
name
|
|
1247
|
-
);
|
|
1248
|
-
let _ = fs::remove_file(&cached_path);
|
|
1249
|
-
false
|
|
1250
|
-
}
|
|
1251
|
-
}
|
|
1252
|
-
};
|
|
1253
|
-
|
|
1254
|
-
let link_name_to_use = if cache_valid {
|
|
1255
|
-
println!("cargo:warning=Using cached {} library for {}", name, target_triple);
|
|
1256
|
-
if let Err(e) = fs::copy(&cached_path, &out_path) {
|
|
1257
|
-
println!("cargo:warning=Failed to copy cached library: {}", e);
|
|
1258
|
-
build_fn();
|
|
1259
|
-
}
|
|
1260
|
-
name.to_string()
|
|
1261
|
-
} else {
|
|
1262
|
-
println!("Building {} library", name);
|
|
1263
|
-
build_fn();
|
|
1264
|
-
|
|
1265
|
-
let mut found_lib_name = None;
|
|
1266
|
-
'search: for lib_name in &possible_lib_names {
|
|
1267
|
-
for dir in &candidate_lib_dirs {
|
|
1268
|
-
let lib_path = dir.join(lib_name);
|
|
1269
|
-
if lib_path.exists() {
|
|
1270
|
-
println!("cargo:warning=Found {} library at: {}", name, lib_path.display());
|
|
1271
|
-
let link_name = if lib_name.ends_with(".lib") {
|
|
1272
|
-
lib_name.strip_suffix(".lib").unwrap_or(lib_name).to_string()
|
|
1273
|
-
} else if lib_name.ends_with(".a") {
|
|
1274
|
-
lib_name
|
|
1275
|
-
.strip_prefix("lib")
|
|
1276
|
-
.and_then(|s| s.strip_suffix(".a"))
|
|
1277
|
-
.unwrap_or(lib_name)
|
|
1278
|
-
.to_string()
|
|
1279
|
-
} else {
|
|
1280
|
-
lib_name.to_string()
|
|
1281
|
-
};
|
|
1282
|
-
found_lib_name = Some((lib_path, link_name));
|
|
1283
|
-
break 'search;
|
|
1284
|
-
}
|
|
1285
|
-
}
|
|
1286
|
-
}
|
|
1287
|
-
|
|
1288
|
-
if let Some((lib_path, link_name)) = found_lib_name {
|
|
1289
|
-
if out_path.exists() {
|
|
1290
|
-
println!(
|
|
1291
|
-
"cargo:warning=Library already available at expected location: {}",
|
|
1292
|
-
out_path.display()
|
|
1293
|
-
);
|
|
1294
|
-
} else if let Err(e) = fs::copy(&lib_path, &out_path) {
|
|
1295
|
-
println!("cargo:warning=Failed to copy library to standard location: {}", e);
|
|
1296
|
-
}
|
|
1297
|
-
if let Err(e) = fs::copy(&lib_path, &cached_path) {
|
|
1298
|
-
println!("cargo:warning=Failed to cache library: {}", e);
|
|
1299
|
-
} else if let Err(e) = fs::write(&marker_path, &target_triple) {
|
|
1300
|
-
println!("cargo:warning=Failed to write cache marker: {}", e);
|
|
1301
|
-
} else {
|
|
1302
|
-
println!("cargo:warning=Cached {} library for {}", name, target_triple);
|
|
1303
|
-
}
|
|
1304
|
-
link_name
|
|
1305
|
-
} else {
|
|
1306
|
-
println!(
|
|
1307
|
-
"cargo:warning=Library {} not found! Searched for: {:?}",
|
|
1308
|
-
name, possible_lib_names
|
|
1309
|
-
);
|
|
1310
|
-
for dir in &candidate_lib_dirs {
|
|
1311
|
-
println!("cargo:warning=Checked directory: {}", dir.display());
|
|
1312
|
-
if let Ok(entries) = fs::read_dir(dir) {
|
|
1313
|
-
println!("cargo:warning=Files in {}:", dir.display());
|
|
1314
|
-
for entry in entries.flatten() {
|
|
1315
|
-
println!("cargo:warning= - {}", entry.file_name().to_string_lossy());
|
|
1316
|
-
}
|
|
1317
|
-
} else {
|
|
1318
|
-
println!("cargo:warning=Directory not accessible: {}", dir.display());
|
|
1319
|
-
}
|
|
1320
|
-
}
|
|
1321
|
-
name.to_string()
|
|
1322
|
-
}
|
|
1323
|
-
};
|
|
1324
|
-
|
|
1325
|
-
for dir in candidate_lib_dirs.iter().filter(|d| d.exists()) {
|
|
1326
|
-
println!("cargo:rustc-link-search=native={}", dir.display());
|
|
1327
|
-
}
|
|
1328
|
-
|
|
1329
|
-
#[cfg(feature = "dynamic-linking")]
|
|
1330
|
-
let link_type = "dylib";
|
|
1331
|
-
#[cfg(not(feature = "dynamic-linking"))]
|
|
1332
|
-
let link_type = "static";
|
|
1333
|
-
|
|
1334
|
-
println!("cargo:rustc-link-lib={}={}", link_type, link_name_to_use);
|
|
1335
|
-
println!(
|
|
1336
|
-
"cargo:warning=Linking with library ({} linking): {}",
|
|
1337
|
-
link_type, link_name_to_use
|
|
1338
|
-
);
|
|
1339
|
-
}
|
|
1340
|
-
}
|
|
1341
|
-
|
|
1342
|
-
fn main() {
|
|
1343
|
-
#[cfg(feature = "build-tesseract")]
|
|
1344
|
-
{
|
|
1345
|
-
build_tesseract::build();
|
|
1346
|
-
}
|
|
1347
|
-
|
|
1348
|
-
#[cfg(all(feature = "dynamic-linking", not(feature = "build-tesseract")))]
|
|
1349
|
-
{
|
|
1350
|
-
println!("cargo:warning=Using dynamic linking with system-installed Tesseract libraries");
|
|
1351
|
-
println!("cargo:rustc-link-lib=dylib=tesseract");
|
|
1352
|
-
println!("cargo:rustc-link-lib=dylib=leptonica");
|
|
1353
|
-
}
|
|
1354
|
-
}
|
|
1
|
+
#![allow(clippy::uninlined_format_args)]
|
|
2
|
+
|
|
3
|
+
#[cfg(feature = "build-tesseract")]
|
|
4
|
+
mod build_tesseract {
|
|
5
|
+
use cmake::Config;
|
|
6
|
+
use std::env;
|
|
7
|
+
use std::fs;
|
|
8
|
+
use std::path::{Path, PathBuf};
|
|
9
|
+
|
|
10
|
+
const LEPTONICA_VERSION: &str = "1.86.0";
|
|
11
|
+
const TESSERACT_VERSION: &str = "5.5.1";
|
|
12
|
+
#[allow(dead_code)]
|
|
13
|
+
const EMSDK_COMMIT: &str = "974d5c096bd56e42045d052a47b28198ce75e2a8";
|
|
14
|
+
#[allow(dead_code)]
|
|
15
|
+
const EMSDK_VERSION: &str = "3.1.31";
|
|
16
|
+
#[allow(dead_code)]
|
|
17
|
+
const EMSDK_REPOSITORY: &str = "https://github.com/emscripten-core/emsdk.git";
|
|
18
|
+
|
|
19
|
+
fn leptonica_url() -> String {
|
|
20
|
+
format!(
|
|
21
|
+
"https://codeload.github.com/DanBloomberg/leptonica/zip/refs/tags/{}",
|
|
22
|
+
LEPTONICA_VERSION
|
|
23
|
+
)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
fn tesseract_url() -> String {
|
|
27
|
+
format!(
|
|
28
|
+
"https://codeload.github.com/tesseract-ocr/tesseract/zip/refs/tags/{}",
|
|
29
|
+
TESSERACT_VERSION
|
|
30
|
+
)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
fn workspace_cache_dir_from_out_dir() -> Option<PathBuf> {
|
|
34
|
+
let out_dir = env::var_os("OUT_DIR")?;
|
|
35
|
+
let mut path = PathBuf::from(out_dir);
|
|
36
|
+
for _ in 0..4 {
|
|
37
|
+
if !path.pop() {
|
|
38
|
+
return None;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
Some(path.join("tesseract-rs-cache"))
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
fn get_preferred_out_dir() -> PathBuf {
|
|
45
|
+
if let Ok(custom) = env::var("TESSERACT_RS_CACHE_DIR") {
|
|
46
|
+
return PathBuf::from(custom);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if cfg!(target_os = "windows") {
|
|
50
|
+
return PathBuf::from("C:\\tess");
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if let Some(workspace_cache) = workspace_cache_dir_from_out_dir() {
|
|
54
|
+
return workspace_cache;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if cfg!(target_os = "macos") {
|
|
58
|
+
let home_dir = env::var("HOME").unwrap_or_else(|_| {
|
|
59
|
+
env::var("USER")
|
|
60
|
+
.map(|user| format!("/Users/{}", user))
|
|
61
|
+
.expect("Neither HOME nor USER environment variable set")
|
|
62
|
+
});
|
|
63
|
+
PathBuf::from(home_dir)
|
|
64
|
+
.join("Library")
|
|
65
|
+
.join("Application Support")
|
|
66
|
+
.join("tesseract-rs")
|
|
67
|
+
} else if cfg!(target_os = "linux") {
|
|
68
|
+
let home_dir = env::var("HOME").unwrap_or_else(|_| {
|
|
69
|
+
env::var("USER")
|
|
70
|
+
.map(|user| format!("/home/{}", user))
|
|
71
|
+
.expect("Neither HOME nor USER environment variable set")
|
|
72
|
+
});
|
|
73
|
+
PathBuf::from(home_dir).join(".tesseract-rs")
|
|
74
|
+
} else {
|
|
75
|
+
panic!("Unsupported operating system");
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
fn target_triple() -> String {
|
|
80
|
+
env::var("TARGET").unwrap_or_else(|_| env::var("HOST").unwrap_or_default())
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
fn target_matches(target: &str, needle: &str) -> bool {
|
|
84
|
+
target.contains(needle)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
fn is_windows_target(target: &str) -> bool {
|
|
88
|
+
target_matches(target, "windows")
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
fn is_macos_target(target: &str) -> bool {
|
|
92
|
+
target_matches(target, "apple-darwin")
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
fn is_linux_target(target: &str) -> bool {
|
|
96
|
+
target_matches(target, "linux")
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
fn is_msvc_target(target: &str) -> bool {
|
|
100
|
+
is_windows_target(target) && target_matches(target, "msvc")
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
fn is_mingw_target(target: &str) -> bool {
|
|
104
|
+
is_windows_target(target) && target_matches(target, "gnu")
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
fn is_wasm_target(target: &str) -> bool {
|
|
108
|
+
target_matches(target, "wasm32") || target_matches(target, "wasm64")
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
fn prepare_out_dir() -> PathBuf {
|
|
112
|
+
let preferred = get_preferred_out_dir();
|
|
113
|
+
match fs::create_dir_all(&preferred) {
|
|
114
|
+
Ok(_) => preferred,
|
|
115
|
+
Err(err) => {
|
|
116
|
+
println!(
|
|
117
|
+
"cargo:warning=Failed to create cache dir {:?}: {}. Falling back to temp dir.",
|
|
118
|
+
preferred, err
|
|
119
|
+
);
|
|
120
|
+
let fallback = env::temp_dir().join("tesseract-rs-cache");
|
|
121
|
+
fs::create_dir_all(&fallback).expect("Failed to create fallback cache directory in temp dir");
|
|
122
|
+
fallback
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// Detect pre-installed EMSDK (installed via brew, apt, or manually)
|
|
128
|
+
#[allow(dead_code)]
|
|
129
|
+
fn find_emsdk() -> Result<PathBuf, String> {
|
|
130
|
+
// Check EMSDK environment variable first
|
|
131
|
+
if let Ok(emsdk_path) = env::var("EMSDK") {
|
|
132
|
+
let path = PathBuf::from(emsdk_path);
|
|
133
|
+
if path.exists() {
|
|
134
|
+
return Ok(path);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// Check common installation locations
|
|
139
|
+
let common_paths = vec![
|
|
140
|
+
PathBuf::from("/opt/homebrew/opt/emscripten"), // Homebrew on Apple Silicon
|
|
141
|
+
PathBuf::from("/usr/local/opt/emscripten"), // Homebrew on Intel Mac
|
|
142
|
+
PathBuf::from(env::var("HOME").unwrap_or_default()).join(".emsdk"),
|
|
143
|
+
PathBuf::from("/usr/lib/emscripten"), // Linux package manager
|
|
144
|
+
PathBuf::from("/opt/emsdk"), // Manual installation
|
|
145
|
+
];
|
|
146
|
+
|
|
147
|
+
for path in common_paths {
|
|
148
|
+
if path.exists() {
|
|
149
|
+
return Ok(path);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
Err("EMSDK not found. Please install via: brew install emscripten (macOS) or apt-get install emscripten (Linux)".to_string())
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
#[allow(dead_code)]
|
|
157
|
+
fn download_emsdk_legacy(emsdk_dir: &Path) -> PathBuf {
|
|
158
|
+
use std::process::Command;
|
|
159
|
+
|
|
160
|
+
println!("cargo:warning=Setting up EMSDK (downloading and installing toolchain)...");
|
|
161
|
+
|
|
162
|
+
// Create parent directory if it doesn't exist
|
|
163
|
+
if let Some(parent) = emsdk_dir.parent() {
|
|
164
|
+
fs::create_dir_all(parent).expect("Failed to create EMSDK parent directory");
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// Phase 1: Clone the EMSDK repository
|
|
168
|
+
if !emsdk_dir.exists() {
|
|
169
|
+
println!("cargo:warning=Cloning EMSDK repository from {}", EMSDK_REPOSITORY);
|
|
170
|
+
|
|
171
|
+
let clone_output = Command::new("git")
|
|
172
|
+
.arg("clone")
|
|
173
|
+
.arg("--depth")
|
|
174
|
+
.arg("1")
|
|
175
|
+
.arg(EMSDK_REPOSITORY)
|
|
176
|
+
.arg(emsdk_dir)
|
|
177
|
+
.output()
|
|
178
|
+
.expect("Failed to run git clone for EMSDK");
|
|
179
|
+
|
|
180
|
+
if !clone_output.status.success() {
|
|
181
|
+
panic!(
|
|
182
|
+
"EMSDK repository clone failed: {}",
|
|
183
|
+
String::from_utf8_lossy(&clone_output.stderr)
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
println!("cargo:warning=EMSDK repository cloned successfully");
|
|
188
|
+
} else {
|
|
189
|
+
println!("cargo:warning=EMSDK directory already exists, skipping clone");
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// Phase 2: Checkout specific commit
|
|
193
|
+
println!("cargo:warning=Checking out EMSDK commit: {}", EMSDK_COMMIT);
|
|
194
|
+
|
|
195
|
+
let fetch_output = Command::new("git")
|
|
196
|
+
.current_dir(emsdk_dir)
|
|
197
|
+
.arg("fetch")
|
|
198
|
+
.arg("origin")
|
|
199
|
+
.arg(EMSDK_COMMIT)
|
|
200
|
+
.output()
|
|
201
|
+
.expect("Failed to run git fetch for EMSDK");
|
|
202
|
+
|
|
203
|
+
if !fetch_output.status.success() {
|
|
204
|
+
eprintln!(
|
|
205
|
+
"Warning: Failed to fetch specific commit, attempting checkout anyway: {}",
|
|
206
|
+
String::from_utf8_lossy(&fetch_output.stderr)
|
|
207
|
+
);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
let checkout_output = Command::new("git")
|
|
211
|
+
.current_dir(emsdk_dir)
|
|
212
|
+
.arg("checkout")
|
|
213
|
+
.arg(EMSDK_COMMIT)
|
|
214
|
+
.output()
|
|
215
|
+
.expect("Failed to run git checkout for EMSDK");
|
|
216
|
+
|
|
217
|
+
if !checkout_output.status.success() {
|
|
218
|
+
eprintln!(
|
|
219
|
+
"Warning: Failed to checkout specific commit: {}",
|
|
220
|
+
String::from_utf8_lossy(&checkout_output.stderr)
|
|
221
|
+
);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
println!("cargo:warning=EMSDK checked out to commit: {}", EMSDK_COMMIT);
|
|
225
|
+
|
|
226
|
+
// Phase 3: Make emsdk script executable and patch SSL verification (Unix only)
|
|
227
|
+
#[cfg(unix)]
|
|
228
|
+
{
|
|
229
|
+
use std::os::unix::fs::PermissionsExt;
|
|
230
|
+
let emsdk_script = emsdk_dir.join("emsdk");
|
|
231
|
+
if emsdk_script.exists() {
|
|
232
|
+
let perms = fs::Permissions::from_mode(0o755);
|
|
233
|
+
fs::set_permissions(&emsdk_script, perms)
|
|
234
|
+
.expect("Failed to set executable permissions on emsdk script");
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// Patch the emsdk.py Python script to disable SSL verification
|
|
238
|
+
let emsdk_py = emsdk_dir.join("emsdk.py");
|
|
239
|
+
if emsdk_py.exists() {
|
|
240
|
+
let script_content = fs::read_to_string(&emsdk_py).expect("Failed to read emsdk.py");
|
|
241
|
+
|
|
242
|
+
if !script_content.contains("ssl._create_unverified_context") {
|
|
243
|
+
let patched_content = script_content.replace(
|
|
244
|
+
"#!/usr/bin/env python",
|
|
245
|
+
"#!/usr/bin/env python\nimport ssl\nssl._create_default_https_context = ssl._create_unverified_context"
|
|
246
|
+
);
|
|
247
|
+
fs::write(&emsdk_py, patched_content).expect("Failed to write patched emsdk.py");
|
|
248
|
+
println!("cargo:warning=Patched emsdk.py to disable SSL verification");
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// Phase 4: Install the EMSDK toolchain (use system Node to avoid SSL download issues)
|
|
254
|
+
println!("cargo:warning=Installing EMSDK toolchain (this may take several minutes)...");
|
|
255
|
+
|
|
256
|
+
// Find system Node.js
|
|
257
|
+
let node_path = Command::new("which")
|
|
258
|
+
.arg("node")
|
|
259
|
+
.output()
|
|
260
|
+
.ok()
|
|
261
|
+
.and_then(|o| String::from_utf8(o.stdout).ok())
|
|
262
|
+
.map(|s| s.trim().to_string())
|
|
263
|
+
.unwrap_or_else(|| "node".to_string());
|
|
264
|
+
|
|
265
|
+
println!("cargo:warning=Using system Node.js: {}", node_path);
|
|
266
|
+
|
|
267
|
+
// Try to install with system Node - allow partial failure if only Node download fails
|
|
268
|
+
#[cfg(unix)]
|
|
269
|
+
let install_output = Command::new("sh")
|
|
270
|
+
.arg("-c")
|
|
271
|
+
.arg(format!(
|
|
272
|
+
"./emsdk install {} 2>&1 | tee /tmp/emsdk-install.log || true",
|
|
273
|
+
EMSDK_VERSION
|
|
274
|
+
))
|
|
275
|
+
.current_dir(emsdk_dir)
|
|
276
|
+
.env("NODE", &node_path)
|
|
277
|
+
.env("EM_NODE_JS", &node_path)
|
|
278
|
+
.output()
|
|
279
|
+
.expect("Failed to run emsdk install");
|
|
280
|
+
|
|
281
|
+
#[cfg(windows)]
|
|
282
|
+
let install_output = Command::new("cmd")
|
|
283
|
+
.args(&["/C", "emsdk.bat", "install", EMSDK_VERSION])
|
|
284
|
+
.current_dir(emsdk_dir)
|
|
285
|
+
.env("NODE", &node_path)
|
|
286
|
+
.env("EM_NODE_JS", &node_path)
|
|
287
|
+
.output()
|
|
288
|
+
.expect("Failed to run emsdk install");
|
|
289
|
+
|
|
290
|
+
// Check if install succeeded or at least got the compiler (Node download may have failed)
|
|
291
|
+
let install_stderr = String::from_utf8_lossy(&install_output.stderr);
|
|
292
|
+
let critical_failure =
|
|
293
|
+
!install_output.status.success() && !install_stderr.contains("node") && !install_stderr.contains("Node");
|
|
294
|
+
|
|
295
|
+
if critical_failure {
|
|
296
|
+
panic!("EMSDK installation failed: {}", install_stderr);
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
if !install_output.status.success() {
|
|
300
|
+
println!(
|
|
301
|
+
"cargo:warning=EMSDK install had warnings (possibly Node download failed, will use system Node): {}",
|
|
302
|
+
install_stderr
|
|
303
|
+
);
|
|
304
|
+
} else {
|
|
305
|
+
println!("cargo:warning=EMSDK toolchain installed successfully");
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
// Phase 5: Activate the installed toolchain
|
|
309
|
+
println!("cargo:warning=Activating EMSDK toolchain...");
|
|
310
|
+
|
|
311
|
+
#[cfg(unix)]
|
|
312
|
+
let activate_output = Command::new("sh")
|
|
313
|
+
.arg("-c")
|
|
314
|
+
.arg(format!("./emsdk activate {}", EMSDK_VERSION))
|
|
315
|
+
.current_dir(emsdk_dir)
|
|
316
|
+
.output()
|
|
317
|
+
.expect("Failed to run emsdk activate");
|
|
318
|
+
|
|
319
|
+
#[cfg(windows)]
|
|
320
|
+
let activate_output = Command::new("cmd")
|
|
321
|
+
.args(&["/C", "emsdk.bat", "activate", EMSDK_VERSION])
|
|
322
|
+
.current_dir(emsdk_dir)
|
|
323
|
+
.output()
|
|
324
|
+
.expect("Failed to run emsdk activate");
|
|
325
|
+
|
|
326
|
+
if !activate_output.status.success() {
|
|
327
|
+
panic!(
|
|
328
|
+
"EMSDK activation failed: {}",
|
|
329
|
+
String::from_utf8_lossy(&activate_output.stderr)
|
|
330
|
+
);
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
println!("cargo:warning=EMSDK activated successfully");
|
|
334
|
+
|
|
335
|
+
// Phase 6: Verify installation (ISSUE #4: Version-aware path checking for EMSDK v3.1.31)
|
|
336
|
+
println!("cargo:warning=Verifying EMSDK installation...");
|
|
337
|
+
|
|
338
|
+
// Check both old and new EMSDK directory structures
|
|
339
|
+
let possible_toolchain_paths = vec![
|
|
340
|
+
"upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake",
|
|
341
|
+
"emscripten/cmake/Modules/Platform/Emscripten.cmake", // Older structure
|
|
342
|
+
];
|
|
343
|
+
|
|
344
|
+
let possible_emcc_paths = vec![
|
|
345
|
+
"upstream/emscripten/emcc",
|
|
346
|
+
"emscripten/emcc", // Older structure
|
|
347
|
+
];
|
|
348
|
+
|
|
349
|
+
let toolchain_file = possible_toolchain_paths
|
|
350
|
+
.iter()
|
|
351
|
+
.find(|p| emsdk_dir.join(p).exists())
|
|
352
|
+
.map(|p| emsdk_dir.join(p))
|
|
353
|
+
.unwrap_or_else(|| emsdk_dir.join("upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake"));
|
|
354
|
+
|
|
355
|
+
let emcc_path = possible_emcc_paths
|
|
356
|
+
.iter()
|
|
357
|
+
.find(|p| emsdk_dir.join(p).exists())
|
|
358
|
+
.map(|p| emsdk_dir.join(p))
|
|
359
|
+
.unwrap_or_else(|| emsdk_dir.join("upstream/emscripten/emcc"));
|
|
360
|
+
|
|
361
|
+
if !toolchain_file.exists() {
|
|
362
|
+
panic!(
|
|
363
|
+
"EMSDK verification failed: toolchain file not found. Checked paths: {:?}",
|
|
364
|
+
possible_toolchain_paths
|
|
365
|
+
);
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
if !emcc_path.exists() {
|
|
369
|
+
panic!(
|
|
370
|
+
"EMSDK verification failed: emcc not found. Checked paths: {:?}",
|
|
371
|
+
possible_emcc_paths
|
|
372
|
+
);
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
println!("cargo:warning=EMSDK verification completed successfully");
|
|
376
|
+
println!("cargo:warning=EMSDK root directory: {}", emsdk_dir.display());
|
|
377
|
+
println!("cargo:warning=EMSDK toolchain: {}", toolchain_file.display());
|
|
378
|
+
println!("cargo:warning=EMSDK emcc: {}", emcc_path.display());
|
|
379
|
+
|
|
380
|
+
emsdk_dir.to_path_buf()
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
pub fn build() {
|
|
384
|
+
let target = target_triple();
|
|
385
|
+
|
|
386
|
+
// Route WASM targets to WASM build
|
|
387
|
+
if is_wasm_target(&target) {
|
|
388
|
+
println!(
|
|
389
|
+
"cargo:warning=Detected WASM target: {}, routing to build_wasm()",
|
|
390
|
+
target
|
|
391
|
+
);
|
|
392
|
+
return build_wasm();
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
let custom_out_dir = prepare_out_dir();
|
|
396
|
+
let windows_target = is_windows_target(&target);
|
|
397
|
+
let msvc_target = is_msvc_target(&target);
|
|
398
|
+
let mingw_target = is_mingw_target(&target);
|
|
399
|
+
|
|
400
|
+
println!("cargo:warning=custom_out_dir: {:?}", custom_out_dir);
|
|
401
|
+
|
|
402
|
+
let cache_dir = custom_out_dir.join("cache");
|
|
403
|
+
|
|
404
|
+
if env::var("CARGO_CLEAN").is_ok() {
|
|
405
|
+
clean_cache(&cache_dir);
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
std::fs::create_dir_all(&cache_dir).expect("Failed to create cache directory");
|
|
409
|
+
|
|
410
|
+
let out_dir = custom_out_dir.clone();
|
|
411
|
+
let project_dir = custom_out_dir.clone();
|
|
412
|
+
let third_party_dir = project_dir.join("third_party");
|
|
413
|
+
|
|
414
|
+
let leptonica_dir = if third_party_dir.join("leptonica").exists() {
|
|
415
|
+
println!("cargo:warning=Using existing leptonica source");
|
|
416
|
+
third_party_dir.join("leptonica")
|
|
417
|
+
} else {
|
|
418
|
+
fs::create_dir_all(&third_party_dir).expect("Failed to create third_party directory");
|
|
419
|
+
download_and_extract(&third_party_dir, &leptonica_url(), "leptonica")
|
|
420
|
+
};
|
|
421
|
+
|
|
422
|
+
let tesseract_dir = if third_party_dir.join("tesseract").exists() {
|
|
423
|
+
println!("cargo:warning=Using existing tesseract source");
|
|
424
|
+
third_party_dir.join("tesseract")
|
|
425
|
+
} else {
|
|
426
|
+
fs::create_dir_all(&third_party_dir).expect("Failed to create third_party directory");
|
|
427
|
+
download_and_extract(&third_party_dir, &tesseract_url(), "tesseract")
|
|
428
|
+
};
|
|
429
|
+
|
|
430
|
+
let (cmake_cxx_flags, additional_defines) = get_os_specific_config();
|
|
431
|
+
|
|
432
|
+
let leptonica_install_dir = out_dir.join("leptonica");
|
|
433
|
+
let leptonica_cache_dir = cache_dir.join("leptonica");
|
|
434
|
+
|
|
435
|
+
build_or_use_cached("leptonica", &leptonica_cache_dir, &leptonica_install_dir, || {
|
|
436
|
+
let mut leptonica_config = Config::new(&leptonica_dir);
|
|
437
|
+
|
|
438
|
+
let leptonica_src_dir = leptonica_dir.join("src");
|
|
439
|
+
let environ_h_path = leptonica_src_dir.join("environ.h");
|
|
440
|
+
|
|
441
|
+
if environ_h_path.exists() {
|
|
442
|
+
let environ_h = std::fs::read_to_string(&environ_h_path)
|
|
443
|
+
.expect("Failed to read environ.h")
|
|
444
|
+
.replace("#define HAVE_LIBZ 1", "#define HAVE_LIBZ 0")
|
|
445
|
+
.replace("#ifdef NO_CONSOLE_IO", "#define NO_CONSOLE_IO\n#ifdef NO_CONSOLE_IO");
|
|
446
|
+
std::fs::write(environ_h_path, environ_h).expect("Failed to write environ.h");
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
let makefile_static_path = leptonica_dir.join("prog").join("makefile.static");
|
|
450
|
+
|
|
451
|
+
let leptonica_src_cmakelists = leptonica_dir.join("src").join("CMakeLists.txt");
|
|
452
|
+
|
|
453
|
+
if leptonica_src_cmakelists.exists() {
|
|
454
|
+
let cmakelists = std::fs::read_to_string(&leptonica_src_cmakelists)
|
|
455
|
+
.expect("Failed to read leptonica src CMakeLists.txt");
|
|
456
|
+
let patched = cmakelists.replace(
|
|
457
|
+
"if(MINGW)\n set_target_properties(\n leptonica PROPERTIES SUFFIX\n \"-${PROJECT_VERSION}${CMAKE_SHARED_LIBRARY_SUFFIX}\")\nendif(MINGW)\n",
|
|
458
|
+
"if(MINGW AND BUILD_SHARED_LIBS)\n set_target_properties(\n leptonica PROPERTIES SUFFIX\n \"-${PROJECT_VERSION}${CMAKE_SHARED_LIBRARY_SUFFIX}\")\nendif()\n",
|
|
459
|
+
);
|
|
460
|
+
if patched != cmakelists {
|
|
461
|
+
std::fs::write(&leptonica_src_cmakelists, patched)
|
|
462
|
+
.expect("Failed to patch leptonica src CMakeLists.txt");
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
if makefile_static_path.exists() {
|
|
467
|
+
let makefile_static = std::fs::read_to_string(&makefile_static_path)
|
|
468
|
+
.expect("Failed to read makefile.static")
|
|
469
|
+
.replace(
|
|
470
|
+
"ALL_LIBS = $(LEPTLIB) -ltiff -ljpeg -lpng -lz -lm",
|
|
471
|
+
"ALL_LIBS = $(LEPTLIB) -lm",
|
|
472
|
+
);
|
|
473
|
+
std::fs::write(makefile_static_path, makefile_static).expect("Failed to write makefile.static");
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
if windows_target {
|
|
477
|
+
if mingw_target {
|
|
478
|
+
leptonica_config.generator("Unix Makefiles");
|
|
479
|
+
leptonica_config.define("CMAKE_MAKE_PROGRAM", "mingw32-make");
|
|
480
|
+
leptonica_config.define("MSYS2_ARG_CONV_EXCL", "/MD;/MDd;/D;-D;-I;-L");
|
|
481
|
+
} else if msvc_target && env::var("VSINSTALLDIR").is_ok() {
|
|
482
|
+
leptonica_config.generator("NMake Makefiles");
|
|
483
|
+
}
|
|
484
|
+
leptonica_config.define("CMAKE_CL_SHOWINCLUDES_PREFIX", "");
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
if env::var("CI").is_err() && env::var("RUSTC_WRAPPER").unwrap_or_default() == "sccache" {
|
|
488
|
+
leptonica_config.env("CC", "sccache cc").env("CXX", "sccache c++");
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
let leptonica_install_dir_cmake = normalize_cmake_path(&leptonica_install_dir);
|
|
492
|
+
|
|
493
|
+
leptonica_config
|
|
494
|
+
.define("CMAKE_POLICY_VERSION_MINIMUM", "3.5")
|
|
495
|
+
.define("CMAKE_BUILD_TYPE", "Release")
|
|
496
|
+
.define("BUILD_PROG", "OFF")
|
|
497
|
+
.define("BUILD_SHARED_LIBS", "OFF")
|
|
498
|
+
.define("ENABLE_ZLIB", "OFF")
|
|
499
|
+
.define("ENABLE_PNG", "OFF")
|
|
500
|
+
.define("ENABLE_JPEG", "OFF")
|
|
501
|
+
.define("ENABLE_TIFF", "OFF")
|
|
502
|
+
.define("ENABLE_WEBP", "OFF")
|
|
503
|
+
.define("ENABLE_OPENJPEG", "OFF")
|
|
504
|
+
.define("ENABLE_GIF", "OFF")
|
|
505
|
+
.define("NO_CONSOLE_IO", "ON")
|
|
506
|
+
.define("CMAKE_CXX_FLAGS", &cmake_cxx_flags)
|
|
507
|
+
.define("MINIMUM_SEVERITY", "L_SEVERITY_NONE")
|
|
508
|
+
.define("SW_BUILD", "OFF")
|
|
509
|
+
.define("HAVE_LIBZ", "0")
|
|
510
|
+
.define("ENABLE_LTO", "OFF")
|
|
511
|
+
.define("CMAKE_INSTALL_PREFIX", &leptonica_install_dir_cmake);
|
|
512
|
+
|
|
513
|
+
if windows_target {
|
|
514
|
+
if msvc_target {
|
|
515
|
+
leptonica_config
|
|
516
|
+
.define("CMAKE_C_FLAGS_RELEASE", "/MD /O2")
|
|
517
|
+
.define("CMAKE_C_FLAGS_DEBUG", "/MDd /Od");
|
|
518
|
+
} else if mingw_target {
|
|
519
|
+
leptonica_config
|
|
520
|
+
.define("CMAKE_C_FLAGS_RELEASE", "-O2 -DNDEBUG")
|
|
521
|
+
.define("CMAKE_C_FLAGS_DEBUG", "-O0 -g");
|
|
522
|
+
} else {
|
|
523
|
+
leptonica_config
|
|
524
|
+
.define("CMAKE_C_FLAGS_RELEASE", "-O2")
|
|
525
|
+
.define("CMAKE_C_FLAGS_DEBUG", "-O0 -g");
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
for (key, value) in &additional_defines {
|
|
530
|
+
leptonica_config.define(key, value);
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
leptonica_config.build();
|
|
534
|
+
});
|
|
535
|
+
|
|
536
|
+
let leptonica_include_dir = leptonica_install_dir.join("include");
|
|
537
|
+
let leptonica_lib_dir = leptonica_install_dir.join("lib");
|
|
538
|
+
let tesseract_install_dir = out_dir.join("tesseract");
|
|
539
|
+
let tesseract_cache_dir = cache_dir.join("tesseract");
|
|
540
|
+
let tessdata_prefix = project_dir.clone();
|
|
541
|
+
|
|
542
|
+
let leptonica_install_dir_cmake = normalize_cmake_path(&leptonica_install_dir);
|
|
543
|
+
let leptonica_include_dir_cmake = normalize_cmake_path(&leptonica_include_dir);
|
|
544
|
+
let leptonica_lib_dir_cmake = normalize_cmake_path(&leptonica_lib_dir);
|
|
545
|
+
let tesseract_install_dir_cmake = normalize_cmake_path(&tesseract_install_dir);
|
|
546
|
+
let tessdata_prefix_cmake = normalize_cmake_path(&tessdata_prefix);
|
|
547
|
+
|
|
548
|
+
build_or_use_cached("tesseract", &tesseract_cache_dir, &tesseract_install_dir, || {
|
|
549
|
+
let cmakelists_path = tesseract_dir.join("CMakeLists.txt");
|
|
550
|
+
let cmakelists = std::fs::read_to_string(&cmakelists_path)
|
|
551
|
+
.expect("Failed to read CMakeLists.txt")
|
|
552
|
+
.replace("set(HAVE_TIFFIO_H ON)", "");
|
|
553
|
+
std::fs::write(&cmakelists_path, cmakelists).expect("Failed to write CMakeLists.txt");
|
|
554
|
+
|
|
555
|
+
let mut tesseract_config = Config::new(&tesseract_dir);
|
|
556
|
+
if windows_target {
|
|
557
|
+
if mingw_target {
|
|
558
|
+
tesseract_config.generator("Unix Makefiles");
|
|
559
|
+
tesseract_config.define("CMAKE_MAKE_PROGRAM", "mingw32-make");
|
|
560
|
+
tesseract_config.define("MSYS2_ARG_CONV_EXCL", "/MD;/MDd;/D;-D;-I;-L");
|
|
561
|
+
} else if msvc_target && env::var("VSINSTALLDIR").is_ok() {
|
|
562
|
+
tesseract_config.generator("NMake Makefiles");
|
|
563
|
+
}
|
|
564
|
+
tesseract_config.define("CMAKE_CL_SHOWINCLUDES_PREFIX", "");
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
if env::var("CI").is_err() && env::var("RUSTC_WRAPPER").unwrap_or_default() == "sccache" {
|
|
568
|
+
tesseract_config.env("CC", "sccache cc").env("CXX", "sccache c++");
|
|
569
|
+
}
|
|
570
|
+
tesseract_config
|
|
571
|
+
.define("CMAKE_POLICY_VERSION_MINIMUM", "3.5")
|
|
572
|
+
.define("CMAKE_BUILD_TYPE", "Release")
|
|
573
|
+
.define("BUILD_TRAINING_TOOLS", "OFF")
|
|
574
|
+
.define("BUILD_SHARED_LIBS", "OFF")
|
|
575
|
+
.define("DISABLE_ARCHIVE", "ON")
|
|
576
|
+
.define("DISABLE_CURL", "ON")
|
|
577
|
+
.define("DISABLE_OPENCL", "ON")
|
|
578
|
+
.define("Leptonica_DIR", &leptonica_install_dir_cmake)
|
|
579
|
+
.define("LEPTONICA_INCLUDE_DIR", &leptonica_include_dir_cmake)
|
|
580
|
+
.define("LEPTONICA_LIBRARY", &leptonica_lib_dir_cmake)
|
|
581
|
+
.define("CMAKE_PREFIX_PATH", &leptonica_install_dir_cmake)
|
|
582
|
+
.define("CMAKE_INSTALL_PREFIX", &tesseract_install_dir_cmake)
|
|
583
|
+
.define("TESSDATA_PREFIX", &tessdata_prefix_cmake)
|
|
584
|
+
.define("DISABLE_TIFF", "ON")
|
|
585
|
+
.define("DISABLE_PNG", "ON")
|
|
586
|
+
.define("DISABLE_JPEG", "ON")
|
|
587
|
+
.define("DISABLE_WEBP", "ON")
|
|
588
|
+
.define("DISABLE_OPENJPEG", "ON")
|
|
589
|
+
.define("DISABLE_ZLIB", "ON")
|
|
590
|
+
.define("DISABLE_LIBXML2", "ON")
|
|
591
|
+
.define("DISABLE_LIBICU", "ON")
|
|
592
|
+
.define("DISABLE_LZMA", "ON")
|
|
593
|
+
.define("DISABLE_GIF", "ON")
|
|
594
|
+
.define("DISABLE_DEBUG_MESSAGES", "ON")
|
|
595
|
+
.define("debug_file", "/dev/null")
|
|
596
|
+
.define("HAVE_LIBARCHIVE", "OFF")
|
|
597
|
+
.define("HAVE_LIBCURL", "OFF")
|
|
598
|
+
.define("HAVE_TIFFIO_H", "OFF")
|
|
599
|
+
.define("GRAPHICS_DISABLED", "ON")
|
|
600
|
+
.define("DISABLED_LEGACY_ENGINE", "OFF")
|
|
601
|
+
.define("USE_OPENCL", "OFF")
|
|
602
|
+
.define("OPENMP_BUILD", "OFF")
|
|
603
|
+
.define("BUILD_TESTS", "OFF")
|
|
604
|
+
.define("ENABLE_LTO", "OFF")
|
|
605
|
+
.define("BUILD_PROG", "OFF")
|
|
606
|
+
.define("SW_BUILD", "OFF")
|
|
607
|
+
.define("LEPT_TIFF_RESULT", "FALSE")
|
|
608
|
+
.define("INSTALL_CONFIGS", "ON")
|
|
609
|
+
.define("USE_SYSTEM_ICU", "ON")
|
|
610
|
+
.define("CMAKE_CXX_FLAGS", &cmake_cxx_flags);
|
|
611
|
+
|
|
612
|
+
for (key, value) in &additional_defines {
|
|
613
|
+
tesseract_config.define(key, value);
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
tesseract_config.build();
|
|
617
|
+
});
|
|
618
|
+
|
|
619
|
+
println!("cargo:rerun-if-changed=build.rs");
|
|
620
|
+
println!("cargo:rerun-if-changed={}", third_party_dir.display());
|
|
621
|
+
println!("cargo:rerun-if-changed={}", leptonica_dir.display());
|
|
622
|
+
println!("cargo:rerun-if-changed={}", tesseract_dir.display());
|
|
623
|
+
|
|
624
|
+
println!("cargo:rustc-link-search=native={}", leptonica_lib_dir.display());
|
|
625
|
+
println!(
|
|
626
|
+
"cargo:rustc-link-search=native={}",
|
|
627
|
+
tesseract_install_dir.join("lib").display()
|
|
628
|
+
);
|
|
629
|
+
|
|
630
|
+
set_os_specific_link_flags();
|
|
631
|
+
|
|
632
|
+
println!("cargo:warning=Leptonica include dir: {:?}", leptonica_include_dir);
|
|
633
|
+
println!("cargo:warning=Leptonica lib dir: {:?}", leptonica_lib_dir);
|
|
634
|
+
println!("cargo:warning=Tesseract install dir: {:?}", tesseract_install_dir);
|
|
635
|
+
println!("cargo:warning=Tessdata dir: {:?}", tessdata_prefix);
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
fn get_os_specific_config() -> (String, Vec<(String, String)>) {
|
|
639
|
+
let mut cmake_cxx_flags = String::new();
|
|
640
|
+
let mut additional_defines = Vec::new();
|
|
641
|
+
let target = target_triple();
|
|
642
|
+
let target_macos = is_macos_target(&target);
|
|
643
|
+
let target_linux = is_linux_target(&target);
|
|
644
|
+
let target_windows = is_windows_target(&target);
|
|
645
|
+
let target_msvc = is_msvc_target(&target);
|
|
646
|
+
let target_mingw = is_mingw_target(&target);
|
|
647
|
+
let target_musl = target.contains("musl");
|
|
648
|
+
|
|
649
|
+
if target_macos {
|
|
650
|
+
cmake_cxx_flags.push_str("-stdlib=libc++ ");
|
|
651
|
+
cmake_cxx_flags.push_str("-std=c++17 ");
|
|
652
|
+
} else if target_linux {
|
|
653
|
+
cmake_cxx_flags.push_str("-std=c++17 ");
|
|
654
|
+
if target_musl || env::var("CC").map(|cc| cc.contains("clang")).unwrap_or(false) {
|
|
655
|
+
cmake_cxx_flags.push_str("-stdlib=libc++ ");
|
|
656
|
+
let cxx_compiler = env::var("CXX").unwrap_or_else(|_| {
|
|
657
|
+
if let Ok(target) = env::var("TARGET") {
|
|
658
|
+
if target != env::var("HOST").unwrap_or_default() {
|
|
659
|
+
format!("{}-clang++", target)
|
|
660
|
+
} else {
|
|
661
|
+
"clang++".to_string()
|
|
662
|
+
}
|
|
663
|
+
} else {
|
|
664
|
+
"clang++".to_string()
|
|
665
|
+
}
|
|
666
|
+
});
|
|
667
|
+
additional_defines.push(("CMAKE_CXX_COMPILER".to_string(), cxx_compiler));
|
|
668
|
+
} else {
|
|
669
|
+
let cxx_compiler = env::var("CXX").unwrap_or_else(|_| {
|
|
670
|
+
if let Ok(target) = env::var("TARGET") {
|
|
671
|
+
if target != env::var("HOST").unwrap_or_default() {
|
|
672
|
+
format!("{}-g++", target)
|
|
673
|
+
} else {
|
|
674
|
+
"g++".to_string()
|
|
675
|
+
}
|
|
676
|
+
} else {
|
|
677
|
+
"g++".to_string()
|
|
678
|
+
}
|
|
679
|
+
});
|
|
680
|
+
additional_defines.push(("CMAKE_CXX_COMPILER".to_string(), cxx_compiler));
|
|
681
|
+
}
|
|
682
|
+
} else if target_windows {
|
|
683
|
+
if target_msvc {
|
|
684
|
+
cmake_cxx_flags.push_str("/EHsc /MP /std:c++17 /DTESSERACT_STATIC ");
|
|
685
|
+
additional_defines.push(("CMAKE_C_FLAGS_RELEASE".to_string(), "/MD /O2".to_string()));
|
|
686
|
+
additional_defines.push(("CMAKE_C_FLAGS_DEBUG".to_string(), "/MDd /Od".to_string()));
|
|
687
|
+
additional_defines.push((
|
|
688
|
+
"CMAKE_CXX_FLAGS_RELEASE".to_string(),
|
|
689
|
+
"/MD /O2 /DTESSERACT_STATIC".to_string(),
|
|
690
|
+
));
|
|
691
|
+
additional_defines.push((
|
|
692
|
+
"CMAKE_CXX_FLAGS_DEBUG".to_string(),
|
|
693
|
+
"/MDd /Od /DTESSERACT_STATIC".to_string(),
|
|
694
|
+
));
|
|
695
|
+
additional_defines.push(("CMAKE_MSVC_RUNTIME_LIBRARY".to_string(), "MultiThreadedDLL".to_string()));
|
|
696
|
+
} else if target_mingw {
|
|
697
|
+
cmake_cxx_flags.push_str("-std=c++17 -DTESSERACT_STATIC ");
|
|
698
|
+
additional_defines.push(("CMAKE_C_FLAGS_RELEASE".to_string(), "-O2 -DNDEBUG".to_string()));
|
|
699
|
+
additional_defines.push(("CMAKE_C_FLAGS_DEBUG".to_string(), "-O0 -g".to_string()));
|
|
700
|
+
additional_defines.push(("CMAKE_C_COMPILER".to_string(), "gcc".to_string()));
|
|
701
|
+
additional_defines.push(("CMAKE_CXX_COMPILER".to_string(), "g++".to_string()));
|
|
702
|
+
additional_defines.push(("CMAKE_SYSTEM_NAME".to_string(), "Windows".to_string()));
|
|
703
|
+
additional_defines.push((
|
|
704
|
+
"CMAKE_CXX_FLAGS_RELEASE".to_string(),
|
|
705
|
+
"-O2 -DNDEBUG -DTESSERACT_STATIC".to_string(),
|
|
706
|
+
));
|
|
707
|
+
additional_defines.push((
|
|
708
|
+
"CMAKE_CXX_FLAGS_DEBUG".to_string(),
|
|
709
|
+
"-O0 -g -DTESSERACT_STATIC".to_string(),
|
|
710
|
+
));
|
|
711
|
+
} else {
|
|
712
|
+
cmake_cxx_flags.push_str("-std=c++17 -DTESSERACT_STATIC ");
|
|
713
|
+
additional_defines.push(("CMAKE_C_FLAGS_RELEASE".to_string(), "-O2 -DNDEBUG".to_string()));
|
|
714
|
+
additional_defines.push(("CMAKE_C_FLAGS_DEBUG".to_string(), "-O0 -g".to_string()));
|
|
715
|
+
additional_defines.push((
|
|
716
|
+
"CMAKE_CXX_FLAGS_RELEASE".to_string(),
|
|
717
|
+
"-O2 -DNDEBUG -DTESSERACT_STATIC".to_string(),
|
|
718
|
+
));
|
|
719
|
+
additional_defines.push((
|
|
720
|
+
"CMAKE_CXX_FLAGS_DEBUG".to_string(),
|
|
721
|
+
"-O0 -g -DTESSERACT_STATIC".to_string(),
|
|
722
|
+
));
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
cmake_cxx_flags.push_str("-DUSE_STD_NAMESPACE ");
|
|
727
|
+
additional_defines.push(("CMAKE_POSITION_INDEPENDENT_CODE".to_string(), "ON".to_string()));
|
|
728
|
+
|
|
729
|
+
if target_windows && target_msvc {
|
|
730
|
+
cmake_cxx_flags.push_str("/permissive- ");
|
|
731
|
+
additional_defines.push(("CMAKE_EXE_LINKER_FLAGS".to_string(), "/INCREMENTAL:NO".to_string()));
|
|
732
|
+
additional_defines.push(("CMAKE_SHARED_LINKER_FLAGS".to_string(), "/INCREMENTAL:NO".to_string()));
|
|
733
|
+
additional_defines.push(("CMAKE_MODULE_LINKER_FLAGS".to_string(), "/INCREMENTAL:NO".to_string()));
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
(cmake_cxx_flags, additional_defines)
|
|
737
|
+
}
|
|
738
|
+
|
|
739
|
+
fn set_os_specific_link_flags() {
|
|
740
|
+
let target = target_triple();
|
|
741
|
+
let target_macos = is_macos_target(&target);
|
|
742
|
+
let target_linux = is_linux_target(&target);
|
|
743
|
+
let target_windows = is_windows_target(&target);
|
|
744
|
+
let target_mingw = is_mingw_target(&target);
|
|
745
|
+
let target_musl = target.contains("musl");
|
|
746
|
+
|
|
747
|
+
if target_macos {
|
|
748
|
+
println!("cargo:rustc-link-lib=c++");
|
|
749
|
+
} else if target_linux {
|
|
750
|
+
if target_musl || env::var("CC").map(|cc| cc.contains("clang")).unwrap_or(false) {
|
|
751
|
+
println!("cargo:rustc-link-lib=c++");
|
|
752
|
+
} else {
|
|
753
|
+
println!("cargo:rustc-link-lib=stdc++");
|
|
754
|
+
println!("cargo:rustc-link-lib=stdc++fs");
|
|
755
|
+
}
|
|
756
|
+
println!("cargo:rustc-link-lib=pthread");
|
|
757
|
+
println!("cargo:rustc-link-lib=m");
|
|
758
|
+
println!("cargo:rustc-link-lib=dl");
|
|
759
|
+
} else if target_windows {
|
|
760
|
+
if target_mingw {
|
|
761
|
+
println!("cargo:rustc-link-lib=stdc++");
|
|
762
|
+
}
|
|
763
|
+
println!("cargo:rustc-link-lib=user32");
|
|
764
|
+
println!("cargo:rustc-link-lib=gdi32");
|
|
765
|
+
println!("cargo:rustc-link-lib=ws2_32");
|
|
766
|
+
println!("cargo:rustc-link-lib=advapi32");
|
|
767
|
+
println!("cargo:rustc-link-lib=shell32");
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
println!("cargo:rustc-link-search=native={}", env::var("OUT_DIR").unwrap());
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
fn download_and_extract(target_dir: &Path, url: &str, name: &str) -> PathBuf {
|
|
774
|
+
use reqwest::blocking::Client;
|
|
775
|
+
use zip::ZipArchive;
|
|
776
|
+
|
|
777
|
+
fs::create_dir_all(target_dir).expect("Failed to create target directory");
|
|
778
|
+
|
|
779
|
+
let client = Client::builder()
|
|
780
|
+
.timeout(std::time::Duration::from_secs(300))
|
|
781
|
+
.build()
|
|
782
|
+
.expect("Failed to create HTTP client");
|
|
783
|
+
|
|
784
|
+
println!("cargo:warning=Downloading {} from {}", name, url);
|
|
785
|
+
let max_attempts = 5;
|
|
786
|
+
let mut response = None;
|
|
787
|
+
|
|
788
|
+
for attempt in 1..=max_attempts {
|
|
789
|
+
let err_msg = match client.get(url).send() {
|
|
790
|
+
Ok(resp) if resp.status().is_success() => {
|
|
791
|
+
response = Some(resp);
|
|
792
|
+
break;
|
|
793
|
+
}
|
|
794
|
+
Ok(resp) => format!("HTTP {}", resp.status()),
|
|
795
|
+
Err(err) => err.to_string(),
|
|
796
|
+
};
|
|
797
|
+
|
|
798
|
+
if attempt == max_attempts {
|
|
799
|
+
panic!(
|
|
800
|
+
"Failed to download {} after {} attempts: {}",
|
|
801
|
+
name, max_attempts, err_msg
|
|
802
|
+
);
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
let backoff = 2u64.pow((attempt - 1).min(4));
|
|
806
|
+
println!(
|
|
807
|
+
"cargo:warning=Download attempt {}/{} for {} failed ({}). Retrying in {}s...",
|
|
808
|
+
attempt, max_attempts, name, err_msg, backoff
|
|
809
|
+
);
|
|
810
|
+
std::thread::sleep(std::time::Duration::from_secs(backoff));
|
|
811
|
+
}
|
|
812
|
+
|
|
813
|
+
let mut response = response.expect("unreachable: download loop must either succeed or panic");
|
|
814
|
+
|
|
815
|
+
let mut content = Vec::new();
|
|
816
|
+
response.copy_to(&mut content).expect("Failed to read archive content");
|
|
817
|
+
|
|
818
|
+
println!("cargo:warning=Downloaded {} bytes for {}", content.len(), name);
|
|
819
|
+
|
|
820
|
+
let temp_file = target_dir.join(format!("{}.zip", name));
|
|
821
|
+
fs::write(&temp_file, content).expect("Failed to write archive to file");
|
|
822
|
+
|
|
823
|
+
let extract_dir = target_dir.join(name);
|
|
824
|
+
if extract_dir.exists() {
|
|
825
|
+
fs::remove_dir_all(&extract_dir).expect("Failed to remove existing directory");
|
|
826
|
+
}
|
|
827
|
+
fs::create_dir_all(&extract_dir).expect("Failed to create extraction directory");
|
|
828
|
+
|
|
829
|
+
let mut archive = ZipArchive::new(fs::File::open(&temp_file).unwrap()).unwrap();
|
|
830
|
+
|
|
831
|
+
for i in 0..archive.len() {
|
|
832
|
+
let mut file = archive.by_index(i).unwrap();
|
|
833
|
+
let file_path = file.mangled_name();
|
|
834
|
+
let file_path = file_path.to_str().unwrap();
|
|
835
|
+
|
|
836
|
+
let path = Path::new(file_path);
|
|
837
|
+
let path = path.strip_prefix(path.components().next().unwrap()).unwrap();
|
|
838
|
+
|
|
839
|
+
if path.as_os_str().is_empty() {
|
|
840
|
+
continue;
|
|
841
|
+
}
|
|
842
|
+
|
|
843
|
+
let target_path = extract_dir.join(path);
|
|
844
|
+
|
|
845
|
+
if file.is_dir() {
|
|
846
|
+
fs::create_dir_all(target_path).unwrap();
|
|
847
|
+
} else {
|
|
848
|
+
if let Some(parent) = target_path.parent() {
|
|
849
|
+
fs::create_dir_all(parent).unwrap();
|
|
850
|
+
}
|
|
851
|
+
let mut outfile = fs::File::create(target_path).unwrap();
|
|
852
|
+
std::io::copy(&mut file, &mut outfile).unwrap();
|
|
853
|
+
}
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
fs::remove_file(temp_file).expect("Failed to remove temporary zip file");
|
|
857
|
+
|
|
858
|
+
extract_dir
|
|
859
|
+
}
|
|
860
|
+
|
|
861
|
+
fn normalize_cmake_path(path: &Path) -> String {
|
|
862
|
+
path.to_string_lossy().replace('\\', "/")
|
|
863
|
+
}
|
|
864
|
+
|
|
865
|
+
fn clean_cache(cache_dir: &Path) {
|
|
866
|
+
println!("Cleaning cache directory: {:?}", cache_dir);
|
|
867
|
+
if cache_dir.exists() {
|
|
868
|
+
fs::remove_dir_all(cache_dir).expect("Failed to remove cache directory");
|
|
869
|
+
}
|
|
870
|
+
}
|
|
871
|
+
|
|
872
|
+
#[allow(dead_code)]
|
|
873
|
+
fn apply_patches(src_dir: &Path, patch_dir: &Path) -> std::io::Result<()> {
|
|
874
|
+
use std::process::Command;
|
|
875
|
+
|
|
876
|
+
let patch_file = patch_dir.join("tesseract.diff");
|
|
877
|
+
if !patch_file.exists() {
|
|
878
|
+
println!("cargo:warning=Patch file not found: {}", patch_file.display());
|
|
879
|
+
return Ok(());
|
|
880
|
+
}
|
|
881
|
+
|
|
882
|
+
println!("cargo:warning=Applying patches from: {}", patch_file.display());
|
|
883
|
+
|
|
884
|
+
let output = Command::new("patch")
|
|
885
|
+
.arg("-p1")
|
|
886
|
+
.current_dir(src_dir)
|
|
887
|
+
.stdin(fs::File::open(&patch_file)?)
|
|
888
|
+
.output()?;
|
|
889
|
+
|
|
890
|
+
if !output.status.success() {
|
|
891
|
+
let stderr = String::from_utf8_lossy(&output.stderr);
|
|
892
|
+
println!("cargo:warning=Patch output: {}", stderr);
|
|
893
|
+
// Don't fail on patch errors, they might be already applied
|
|
894
|
+
}
|
|
895
|
+
|
|
896
|
+
Ok(())
|
|
897
|
+
}
|
|
898
|
+
|
|
899
|
+
// ISSUE #5: Setup EMSDK environment variables before CMake builds
|
|
900
|
+
#[allow(dead_code)]
|
|
901
|
+
fn setup_emsdk_environment(emsdk_root: &Path) {
|
|
902
|
+
let emsdk_root_str = emsdk_root.to_string_lossy().to_string();
|
|
903
|
+
|
|
904
|
+
// Set EMSDK root directory
|
|
905
|
+
unsafe {
|
|
906
|
+
env::set_var("EMSDK", &emsdk_root_str);
|
|
907
|
+
}
|
|
908
|
+
println!("cargo:warning=Set EMSDK={}", emsdk_root_str);
|
|
909
|
+
|
|
910
|
+
// Set EMSCRIPTEN path for Emscripten compiler directory
|
|
911
|
+
let emscripten_path = format!("{}/upstream/emscripten", emsdk_root_str);
|
|
912
|
+
unsafe {
|
|
913
|
+
env::set_var("EMSCRIPTEN", &emscripten_path);
|
|
914
|
+
}
|
|
915
|
+
println!("cargo:warning=Set EMSCRIPTEN={}", emscripten_path);
|
|
916
|
+
|
|
917
|
+
// Set EM_CONFIG for .emscripten config file
|
|
918
|
+
let em_config = format!("{}/.emscripten", emsdk_root_str);
|
|
919
|
+
unsafe {
|
|
920
|
+
env::set_var("EM_CONFIG", &em_config);
|
|
921
|
+
}
|
|
922
|
+
println!("cargo:warning=Set EM_CONFIG={}", em_config);
|
|
923
|
+
|
|
924
|
+
// Update PATH with EMSDK tools directory
|
|
925
|
+
let emsdk_bin_paths = [
|
|
926
|
+
format!("{}/upstream/emscripten", emsdk_root_str),
|
|
927
|
+
format!("{}/upstream/bin", emsdk_root_str),
|
|
928
|
+
];
|
|
929
|
+
|
|
930
|
+
let current_path = env::var("PATH").unwrap_or_default();
|
|
931
|
+
let new_path = format!(
|
|
932
|
+
"{}{}{}",
|
|
933
|
+
emsdk_bin_paths.join(":"),
|
|
934
|
+
if current_path.is_empty() { "" } else { ":" },
|
|
935
|
+
current_path
|
|
936
|
+
);
|
|
937
|
+
unsafe {
|
|
938
|
+
env::set_var("PATH", &new_path);
|
|
939
|
+
}
|
|
940
|
+
println!("cargo:warning=Updated PATH with EMSDK tools");
|
|
941
|
+
}
|
|
942
|
+
|
|
943
|
+
// ISSUE #7: Build Leptonica for WASM
|
|
944
|
+
#[allow(dead_code)]
|
|
945
|
+
fn build_leptonica_wasm(leptonica_src: &Path, leptonica_install: &Path, emsdk_dir: &Path) {
|
|
946
|
+
let toolchain_file = emsdk_dir.join("upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake");
|
|
947
|
+
|
|
948
|
+
let mut config = Config::new(leptonica_src);
|
|
949
|
+
|
|
950
|
+
// Set the EMSDK toolchain file
|
|
951
|
+
config.define("CMAKE_TOOLCHAIN_FILE", &toolchain_file);
|
|
952
|
+
|
|
953
|
+
// Image format support disabled (handled by browser)
|
|
954
|
+
config
|
|
955
|
+
.define("CMAKE_BUILD_TYPE", "Release")
|
|
956
|
+
.define("CMAKE_POLICY_VERSION_MINIMUM", "3.5")
|
|
957
|
+
.define("LIBWEBP_SUPPORT", "OFF")
|
|
958
|
+
.define("OPENJPEG_SUPPORT", "OFF")
|
|
959
|
+
.define("ENABLE_ZLIB", "OFF")
|
|
960
|
+
.define("ENABLE_PNG", "OFF")
|
|
961
|
+
.define("ENABLE_JPEG", "OFF")
|
|
962
|
+
.define("ENABLE_TIFF", "OFF")
|
|
963
|
+
.define("ENABLE_WEBP", "OFF")
|
|
964
|
+
.define("ENABLE_OPENJPEG", "OFF")
|
|
965
|
+
.define("ENABLE_GIF", "OFF")
|
|
966
|
+
// Build configuration
|
|
967
|
+
.define("BUILD_PROG", "OFF")
|
|
968
|
+
.define("BUILD_SHARED_LIBS", "OFF")
|
|
969
|
+
.define("NO_CONSOLE_IO", "ON")
|
|
970
|
+
.define("HAVE_LIBZ", "0")
|
|
971
|
+
.define("ENABLE_LTO", "OFF")
|
|
972
|
+
.define("CMAKE_INSTALL_PREFIX", leptonica_install);
|
|
973
|
+
|
|
974
|
+
config.build();
|
|
975
|
+
}
|
|
976
|
+
|
|
977
|
+
// ISSUE #7: Implement build_wasm() with proper Leptonica and Tesseract integration
|
|
978
|
+
#[allow(dead_code)]
|
|
979
|
+
fn build_wasm() {
|
|
980
|
+
println!("cargo:warning=Building for WASM target with Emscripten SDK");
|
|
981
|
+
|
|
982
|
+
let custom_out_dir = prepare_out_dir();
|
|
983
|
+
let cache_dir = custom_out_dir.join("cache");
|
|
984
|
+
fs::create_dir_all(&cache_dir).expect("Failed to create cache directory");
|
|
985
|
+
|
|
986
|
+
let project_dir = custom_out_dir.clone();
|
|
987
|
+
let third_party_dir = project_dir.join("third_party");
|
|
988
|
+
|
|
989
|
+
// Step 1: Find pre-installed EMSDK
|
|
990
|
+
println!("cargo:warning=Looking for pre-installed Emscripten SDK...");
|
|
991
|
+
let emsdk_dir = match find_emsdk() {
|
|
992
|
+
Ok(path) => {
|
|
993
|
+
println!("cargo:warning=Found EMSDK at: {}", path.display());
|
|
994
|
+
path
|
|
995
|
+
}
|
|
996
|
+
Err(err) => {
|
|
997
|
+
panic!("{}
|
|
998
|
+
|
|
999
|
+
Installation instructions:
|
|
1000
|
+
macOS: brew install emscripten
|
|
1001
|
+
Ubuntu: sudo apt-get install emscripten
|
|
1002
|
+
Arch: sudo pacman -S emscripten
|
|
1003
|
+
Manual: git clone https://github.com/emscripten-core/emsdk.git ~/.emsdk && cd ~/.emsdk && ./emsdk install latest && ./emsdk activate latest
|
|
1004
|
+
|
|
1005
|
+
After installation, set EMSDK environment variable or ensure it's in a standard location.", err);
|
|
1006
|
+
}
|
|
1007
|
+
};
|
|
1008
|
+
|
|
1009
|
+
// Setup EMSDK environment variables
|
|
1010
|
+
setup_emsdk_environment(&emsdk_dir);
|
|
1011
|
+
|
|
1012
|
+
// Step 2: Download Leptonica and Tesseract sources if not already present
|
|
1013
|
+
let leptonica_dir = if third_party_dir.join("leptonica").exists() {
|
|
1014
|
+
println!("cargo:warning=Using existing leptonica source");
|
|
1015
|
+
third_party_dir.join("leptonica")
|
|
1016
|
+
} else {
|
|
1017
|
+
fs::create_dir_all(&third_party_dir).expect("Failed to create third_party directory");
|
|
1018
|
+
download_and_extract(&third_party_dir, &leptonica_url(), "leptonica")
|
|
1019
|
+
};
|
|
1020
|
+
|
|
1021
|
+
let tesseract_dir = if third_party_dir.join("tesseract").exists() {
|
|
1022
|
+
println!("cargo:warning=Using existing tesseract source");
|
|
1023
|
+
third_party_dir.join("tesseract")
|
|
1024
|
+
} else {
|
|
1025
|
+
fs::create_dir_all(&third_party_dir).expect("Failed to create third_party directory");
|
|
1026
|
+
download_and_extract(&third_party_dir, &tesseract_url(), "tesseract")
|
|
1027
|
+
};
|
|
1028
|
+
|
|
1029
|
+
// Step 3: Build Leptonica for WASM
|
|
1030
|
+
let leptonica_install_dir = custom_out_dir.join("leptonica");
|
|
1031
|
+
let leptonica_cache_dir = cache_dir.join("leptonica");
|
|
1032
|
+
|
|
1033
|
+
build_or_use_cached("leptonica", &leptonica_cache_dir, &leptonica_install_dir, || {
|
|
1034
|
+
println!("cargo:warning=Building Leptonica for WASM...");
|
|
1035
|
+
build_leptonica_wasm(&leptonica_dir, &leptonica_install_dir, &emsdk_dir);
|
|
1036
|
+
});
|
|
1037
|
+
|
|
1038
|
+
// Step 4: Build Tesseract for WASM (SIMD version for modern browsers)
|
|
1039
|
+
let tesseract_install_dir = custom_out_dir.join("tesseract");
|
|
1040
|
+
let tesseract_cache_dir = cache_dir.join("tesseract");
|
|
1041
|
+
|
|
1042
|
+
build_or_use_cached("tesseract", &tesseract_cache_dir, &tesseract_install_dir, || {
|
|
1043
|
+
println!("cargo:warning=Building Tesseract for WASM (SIMD enabled)...");
|
|
1044
|
+
build_tesseract_wasm(
|
|
1045
|
+
&tesseract_dir,
|
|
1046
|
+
&tesseract_install_dir,
|
|
1047
|
+
&leptonica_install_dir,
|
|
1048
|
+
&emsdk_dir,
|
|
1049
|
+
true, // enable_simd
|
|
1050
|
+
);
|
|
1051
|
+
});
|
|
1052
|
+
|
|
1053
|
+
// Step 5: Setup linker paths for WASM libraries (static linking)
|
|
1054
|
+
let leptonica_lib_dir = leptonica_install_dir.join("lib");
|
|
1055
|
+
let tesseract_lib_dir = tesseract_install_dir.join("lib");
|
|
1056
|
+
|
|
1057
|
+
println!("cargo:rustc-link-search=native={}", leptonica_lib_dir.display());
|
|
1058
|
+
println!("cargo:rustc-link-search=native={}", tesseract_lib_dir.display());
|
|
1059
|
+
|
|
1060
|
+
// For WASM, always use static linking
|
|
1061
|
+
println!("cargo:rustc-link-lib=static=tesseract");
|
|
1062
|
+
println!("cargo:rustc-link-lib=static=leptonica");
|
|
1063
|
+
|
|
1064
|
+
println!("cargo:warning=WASM build completed successfully!");
|
|
1065
|
+
println!("cargo:warning=Leptonica install dir: {:?}", leptonica_install_dir);
|
|
1066
|
+
println!("cargo:warning=Tesseract install dir: {:?}", tesseract_install_dir);
|
|
1067
|
+
}
|
|
1068
|
+
|
|
1069
|
+
#[allow(dead_code)]
|
|
1070
|
+
fn build_tesseract_wasm(
|
|
1071
|
+
src_dir: &Path,
|
|
1072
|
+
tesseract_install: &Path,
|
|
1073
|
+
leptonica_install: &Path,
|
|
1074
|
+
emsdk_dir: &Path,
|
|
1075
|
+
enable_simd: bool,
|
|
1076
|
+
) {
|
|
1077
|
+
let toolchain_file = emsdk_dir.join("upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake");
|
|
1078
|
+
|
|
1079
|
+
let mut config = Config::new(src_dir);
|
|
1080
|
+
|
|
1081
|
+
// Set CMake toolchain for Emscripten
|
|
1082
|
+
config.define("CMAKE_TOOLCHAIN_FILE", &toolchain_file);
|
|
1083
|
+
|
|
1084
|
+
// Link against WASM Leptonica
|
|
1085
|
+
config.define("Leptonica_DIR", leptonica_install);
|
|
1086
|
+
config.define("CMAKE_PREFIX_PATH", leptonica_install);
|
|
1087
|
+
|
|
1088
|
+
// ISSUE #6: Complete CMAKE_CXX_FLAGS for WASM with all WASM-specific flags
|
|
1089
|
+
let mut cxx_flags = String::from("-DTESSERACT_IMAGEDATA_AS_PIX ");
|
|
1090
|
+
if enable_simd {
|
|
1091
|
+
cxx_flags.push_str("-msimd128 ");
|
|
1092
|
+
}
|
|
1093
|
+
cxx_flags.push_str("-fno-exceptions -fPIC -Os");
|
|
1094
|
+
|
|
1095
|
+
// ISSUE #6: Also add CMAKE_C_FLAGS (was completely missing before)
|
|
1096
|
+
let c_flags = "-fno-exceptions -fPIC -Os";
|
|
1097
|
+
|
|
1098
|
+
// Tesseract WASM configuration (from EMSDK_TECHNICAL_REFERENCE.md)
|
|
1099
|
+
config
|
|
1100
|
+
.define("CMAKE_BUILD_TYPE", "Release")
|
|
1101
|
+
.define("CMAKE_POLICY_VERSION_MINIMUM", "3.5")
|
|
1102
|
+
// Core Tesseract settings
|
|
1103
|
+
.define("BUILD_TESSERACT_BINARY", "OFF")
|
|
1104
|
+
.define("BUILD_TRAINING_TOOLS", "OFF")
|
|
1105
|
+
.define("INSTALL_CONFIGS", "ON")
|
|
1106
|
+
.define("BUILD_TESTS", "OFF")
|
|
1107
|
+
.define("BUILD_PROG", "OFF")
|
|
1108
|
+
.define("SYNTAX_LOG", "OFF")
|
|
1109
|
+
// Disable unnecessary features
|
|
1110
|
+
.define("DISABLE_ARCHIVE", "ON")
|
|
1111
|
+
.define("DISABLE_CURL", "ON")
|
|
1112
|
+
.define("DISABLE_OPENCL", "ON")
|
|
1113
|
+
.define("DISABLE_TIFF", "ON")
|
|
1114
|
+
.define("DISABLE_PNG", "ON")
|
|
1115
|
+
.define("DISABLE_JPEG", "ON")
|
|
1116
|
+
.define("DISABLE_WEBP", "ON")
|
|
1117
|
+
.define("DISABLE_OPENJPEG", "ON")
|
|
1118
|
+
.define("DISABLE_ZLIB", "ON")
|
|
1119
|
+
.define("DISABLE_LIBXML2", "ON")
|
|
1120
|
+
.define("DISABLE_LIBICU", "ON")
|
|
1121
|
+
.define("DISABLE_LZMA", "ON")
|
|
1122
|
+
.define("DISABLE_GIF", "ON")
|
|
1123
|
+
.define("DISABLE_DEBUG_MESSAGES", "ON")
|
|
1124
|
+
// WASM-specific settings
|
|
1125
|
+
.define("GRAPHICS_DISABLED", "ON")
|
|
1126
|
+
.define("DISABLED_LEGACY_ENGINE", "ON")
|
|
1127
|
+
.define("USE_OPENCL", "OFF")
|
|
1128
|
+
.define("OPENMP_BUILD", "OFF")
|
|
1129
|
+
.define("ENABLE_LTO", "ON")
|
|
1130
|
+
// SIMD configuration
|
|
1131
|
+
.define("HAVE_SSE4_1", if enable_simd { "ON" } else { "OFF" })
|
|
1132
|
+
.define("HAVE_AVX", "OFF")
|
|
1133
|
+
.define("HAVE_AVX2", "OFF")
|
|
1134
|
+
.define("HAVE_AVX512F", "OFF")
|
|
1135
|
+
.define("HAVE_FMA", "OFF")
|
|
1136
|
+
// Installation directory
|
|
1137
|
+
.define("CMAKE_INSTALL_PREFIX", tesseract_install)
|
|
1138
|
+
// Compiler flags with WASM-specific settings
|
|
1139
|
+
.define("CMAKE_CXX_FLAGS", &cxx_flags)
|
|
1140
|
+
.define("CMAKE_C_FLAGS", c_flags);
|
|
1141
|
+
|
|
1142
|
+
// Build the library
|
|
1143
|
+
config.build();
|
|
1144
|
+
}
|
|
1145
|
+
|
|
1146
|
+
fn build_or_use_cached<F>(name: &str, cache_dir: &Path, install_dir: &Path, build_fn: F)
|
|
1147
|
+
where
|
|
1148
|
+
F: FnOnce(),
|
|
1149
|
+
{
|
|
1150
|
+
let target_env = env::var("CARGO_CFG_TARGET_ENV").unwrap_or_default();
|
|
1151
|
+
let target_triple = env::var("TARGET")
|
|
1152
|
+
.unwrap_or_else(|_| env::var("CARGO_CFG_TARGET_ARCH").unwrap_or_else(|_| "unknown".to_string()));
|
|
1153
|
+
let is_windows = target_triple.contains("windows");
|
|
1154
|
+
let is_windows_gnu = is_windows && target_env == "gnu";
|
|
1155
|
+
|
|
1156
|
+
let lib_name = if is_windows && !is_windows_gnu {
|
|
1157
|
+
format!("{}.lib", name)
|
|
1158
|
+
} else {
|
|
1159
|
+
format!("lib{}.a", name)
|
|
1160
|
+
};
|
|
1161
|
+
|
|
1162
|
+
let cached_path = cache_dir.join(&lib_name);
|
|
1163
|
+
let marker_path = cache_dir.join(format!("{}.target", name));
|
|
1164
|
+
let out_path = install_dir.join("lib").join(&lib_name);
|
|
1165
|
+
|
|
1166
|
+
let possible_lib_names: Vec<String> = if is_windows {
|
|
1167
|
+
let mut base = match name {
|
|
1168
|
+
"leptonica" => vec![
|
|
1169
|
+
"leptonica.lib".to_string(),
|
|
1170
|
+
"libleptonica.lib".to_string(),
|
|
1171
|
+
"leptonica-static.lib".to_string(),
|
|
1172
|
+
"leptonica-1.84.1.lib".to_string(),
|
|
1173
|
+
"leptonica-1.86.0.lib".to_string(),
|
|
1174
|
+
"leptonicad.lib".to_string(),
|
|
1175
|
+
"libleptonica_d.lib".to_string(),
|
|
1176
|
+
"leptonica-1.84.1d.lib".to_string(),
|
|
1177
|
+
"leptonica-1.86.0d.lib".to_string(),
|
|
1178
|
+
],
|
|
1179
|
+
"tesseract" => vec![
|
|
1180
|
+
"tesseract.lib".to_string(),
|
|
1181
|
+
"libtesseract.lib".to_string(),
|
|
1182
|
+
"tesseract-static.lib".to_string(),
|
|
1183
|
+
"tesseract53.lib".to_string(),
|
|
1184
|
+
"tesseract54.lib".to_string(),
|
|
1185
|
+
"tesseract55.lib".to_string(),
|
|
1186
|
+
"tesseractd.lib".to_string(),
|
|
1187
|
+
"libtesseract_d.lib".to_string(),
|
|
1188
|
+
"tesseract53d.lib".to_string(),
|
|
1189
|
+
"tesseract54d.lib".to_string(),
|
|
1190
|
+
"tesseract55d.lib".to_string(),
|
|
1191
|
+
],
|
|
1192
|
+
_ => vec![format!("{}.lib", name)],
|
|
1193
|
+
};
|
|
1194
|
+
|
|
1195
|
+
if is_windows_gnu {
|
|
1196
|
+
match name {
|
|
1197
|
+
"leptonica" => {
|
|
1198
|
+
base.push(format!("libleptonica-{}.a", LEPTONICA_VERSION));
|
|
1199
|
+
base.push("libleptonica.a".to_string());
|
|
1200
|
+
}
|
|
1201
|
+
"tesseract" => {
|
|
1202
|
+
base.push(format!("libtesseract{}.a", TESSERACT_VERSION.replace('.', "")));
|
|
1203
|
+
base.push("libtesseract.a".to_string());
|
|
1204
|
+
base.push("libtesseract55.a".to_string());
|
|
1205
|
+
}
|
|
1206
|
+
_ => {
|
|
1207
|
+
base.push(format!("lib{}.a", name));
|
|
1208
|
+
}
|
|
1209
|
+
}
|
|
1210
|
+
}
|
|
1211
|
+
|
|
1212
|
+
base
|
|
1213
|
+
} else {
|
|
1214
|
+
vec![format!("lib{}.a", name)]
|
|
1215
|
+
};
|
|
1216
|
+
|
|
1217
|
+
fs::create_dir_all(cache_dir).expect("Failed to create cache directory");
|
|
1218
|
+
fs::create_dir_all(out_path.parent().unwrap()).expect("Failed to create output directory");
|
|
1219
|
+
|
|
1220
|
+
let candidate_lib_dirs = [
|
|
1221
|
+
install_dir.join("lib"),
|
|
1222
|
+
install_dir.join("lib64"),
|
|
1223
|
+
install_dir.join("lib").join("tesseract"),
|
|
1224
|
+
];
|
|
1225
|
+
|
|
1226
|
+
let cache_valid = cached_path.exists()
|
|
1227
|
+
&& {
|
|
1228
|
+
match fs::read_to_string(&marker_path) {
|
|
1229
|
+
Ok(cached_target) => {
|
|
1230
|
+
let valid = cached_target.trim() == target_triple;
|
|
1231
|
+
if !valid {
|
|
1232
|
+
println!(
|
|
1233
|
+
"cargo:warning=Cached {} library is for wrong architecture (cached: {}, current: {}), rebuilding",
|
|
1234
|
+
name,
|
|
1235
|
+
cached_target.trim(),
|
|
1236
|
+
target_triple
|
|
1237
|
+
);
|
|
1238
|
+
let _ = fs::remove_file(&cached_path);
|
|
1239
|
+
let _ = fs::remove_file(&marker_path);
|
|
1240
|
+
}
|
|
1241
|
+
valid
|
|
1242
|
+
}
|
|
1243
|
+
Err(_) => {
|
|
1244
|
+
println!(
|
|
1245
|
+
"cargo:warning=Cached {} library missing target marker, rebuilding",
|
|
1246
|
+
name
|
|
1247
|
+
);
|
|
1248
|
+
let _ = fs::remove_file(&cached_path);
|
|
1249
|
+
false
|
|
1250
|
+
}
|
|
1251
|
+
}
|
|
1252
|
+
};
|
|
1253
|
+
|
|
1254
|
+
let link_name_to_use = if cache_valid {
|
|
1255
|
+
println!("cargo:warning=Using cached {} library for {}", name, target_triple);
|
|
1256
|
+
if let Err(e) = fs::copy(&cached_path, &out_path) {
|
|
1257
|
+
println!("cargo:warning=Failed to copy cached library: {}", e);
|
|
1258
|
+
build_fn();
|
|
1259
|
+
}
|
|
1260
|
+
name.to_string()
|
|
1261
|
+
} else {
|
|
1262
|
+
println!("Building {} library", name);
|
|
1263
|
+
build_fn();
|
|
1264
|
+
|
|
1265
|
+
let mut found_lib_name = None;
|
|
1266
|
+
'search: for lib_name in &possible_lib_names {
|
|
1267
|
+
for dir in &candidate_lib_dirs {
|
|
1268
|
+
let lib_path = dir.join(lib_name);
|
|
1269
|
+
if lib_path.exists() {
|
|
1270
|
+
println!("cargo:warning=Found {} library at: {}", name, lib_path.display());
|
|
1271
|
+
let link_name = if lib_name.ends_with(".lib") {
|
|
1272
|
+
lib_name.strip_suffix(".lib").unwrap_or(lib_name).to_string()
|
|
1273
|
+
} else if lib_name.ends_with(".a") {
|
|
1274
|
+
lib_name
|
|
1275
|
+
.strip_prefix("lib")
|
|
1276
|
+
.and_then(|s| s.strip_suffix(".a"))
|
|
1277
|
+
.unwrap_or(lib_name)
|
|
1278
|
+
.to_string()
|
|
1279
|
+
} else {
|
|
1280
|
+
lib_name.to_string()
|
|
1281
|
+
};
|
|
1282
|
+
found_lib_name = Some((lib_path, link_name));
|
|
1283
|
+
break 'search;
|
|
1284
|
+
}
|
|
1285
|
+
}
|
|
1286
|
+
}
|
|
1287
|
+
|
|
1288
|
+
if let Some((lib_path, link_name)) = found_lib_name {
|
|
1289
|
+
if out_path.exists() {
|
|
1290
|
+
println!(
|
|
1291
|
+
"cargo:warning=Library already available at expected location: {}",
|
|
1292
|
+
out_path.display()
|
|
1293
|
+
);
|
|
1294
|
+
} else if let Err(e) = fs::copy(&lib_path, &out_path) {
|
|
1295
|
+
println!("cargo:warning=Failed to copy library to standard location: {}", e);
|
|
1296
|
+
}
|
|
1297
|
+
if let Err(e) = fs::copy(&lib_path, &cached_path) {
|
|
1298
|
+
println!("cargo:warning=Failed to cache library: {}", e);
|
|
1299
|
+
} else if let Err(e) = fs::write(&marker_path, &target_triple) {
|
|
1300
|
+
println!("cargo:warning=Failed to write cache marker: {}", e);
|
|
1301
|
+
} else {
|
|
1302
|
+
println!("cargo:warning=Cached {} library for {}", name, target_triple);
|
|
1303
|
+
}
|
|
1304
|
+
link_name
|
|
1305
|
+
} else {
|
|
1306
|
+
println!(
|
|
1307
|
+
"cargo:warning=Library {} not found! Searched for: {:?}",
|
|
1308
|
+
name, possible_lib_names
|
|
1309
|
+
);
|
|
1310
|
+
for dir in &candidate_lib_dirs {
|
|
1311
|
+
println!("cargo:warning=Checked directory: {}", dir.display());
|
|
1312
|
+
if let Ok(entries) = fs::read_dir(dir) {
|
|
1313
|
+
println!("cargo:warning=Files in {}:", dir.display());
|
|
1314
|
+
for entry in entries.flatten() {
|
|
1315
|
+
println!("cargo:warning= - {}", entry.file_name().to_string_lossy());
|
|
1316
|
+
}
|
|
1317
|
+
} else {
|
|
1318
|
+
println!("cargo:warning=Directory not accessible: {}", dir.display());
|
|
1319
|
+
}
|
|
1320
|
+
}
|
|
1321
|
+
name.to_string()
|
|
1322
|
+
}
|
|
1323
|
+
};
|
|
1324
|
+
|
|
1325
|
+
for dir in candidate_lib_dirs.iter().filter(|d| d.exists()) {
|
|
1326
|
+
println!("cargo:rustc-link-search=native={}", dir.display());
|
|
1327
|
+
}
|
|
1328
|
+
|
|
1329
|
+
#[cfg(feature = "dynamic-linking")]
|
|
1330
|
+
let link_type = "dylib";
|
|
1331
|
+
#[cfg(not(feature = "dynamic-linking"))]
|
|
1332
|
+
let link_type = "static";
|
|
1333
|
+
|
|
1334
|
+
println!("cargo:rustc-link-lib={}={}", link_type, link_name_to_use);
|
|
1335
|
+
println!(
|
|
1336
|
+
"cargo:warning=Linking with library ({} linking): {}",
|
|
1337
|
+
link_type, link_name_to_use
|
|
1338
|
+
);
|
|
1339
|
+
}
|
|
1340
|
+
}
|
|
1341
|
+
|
|
1342
|
+
fn main() {
|
|
1343
|
+
#[cfg(feature = "build-tesseract")]
|
|
1344
|
+
{
|
|
1345
|
+
build_tesseract::build();
|
|
1346
|
+
}
|
|
1347
|
+
|
|
1348
|
+
#[cfg(all(feature = "dynamic-linking", not(feature = "build-tesseract")))]
|
|
1349
|
+
{
|
|
1350
|
+
println!("cargo:warning=Using dynamic linking with system-installed Tesseract libraries");
|
|
1351
|
+
println!("cargo:rustc-link-lib=dylib=tesseract");
|
|
1352
|
+
println!("cargo:rustc-link-lib=dylib=leptonica");
|
|
1353
|
+
}
|
|
1354
|
+
}
|