8
8
#
9
9
# ##############################################################################
10
10
11
- cmake_minimum_required (VERSION 3.10 FATAL_ERROR )
11
+ # Require CMake 3.18 for modern features like precompiled headers, unity builds, and better target management
12
+ cmake_minimum_required (VERSION 3.18 FATAL_ERROR )
12
13
13
14
# In-source builds are disabled.
14
15
if ("${CMAKE_CURRENT_SOURCE_DIR} " STREQUAL "${CMAKE_CURRENT_BINARY_DIR} " )
@@ -100,11 +101,12 @@ option(DISABLE_ARCHIVE "Disable build with libarchive (if available)" OFF)
100
101
option (DISABLE_CURL "Disable build with libcurl (if available)" OFF )
101
102
option (INSTALL_CONFIGS "Install tesseract configs" ON )
102
103
103
- if (NOT ${CMAKE_VERSION} VERSION_LESS "3.15.0" )
104
- if (WIN32 AND MSVC )
105
- option (WIN32_MT_BUILD "Build with MT flag for MSVC" OFF )
106
- endif ()
107
- endif ()
104
+ # Build optimization options
105
+ option (ENABLE_UNITY_BUILD "Enable Unity/Jumbo builds for faster compilation" OFF )
106
+ option (ENABLE_PRECOMPILED_HEADERS "Enable precompiled headers for faster compilation" ON )
107
+ option (ENABLE_CCACHE "Enable ccache for faster incremental builds" ON )
108
+ option (ENABLE_NINJA_POOL "Enable Ninja job pools to manage parallelism" ON )
109
+
108
110
109
111
# ##############################################################################
110
112
#
@@ -286,6 +288,8 @@ if(CMAKE_COMPILER_IS_GNUCXX OR MINGW)
286
288
elseif (MSVC )
287
289
add_definitions (-D_CRT_SECURE_NO_WARNINGS )
288
290
add_definitions (-D_CRT_NONSTDC_NO_DEPRECATE ) # strdup
291
+ add_definitions (-D_USE_MATH_DEFINES ) # Enable M_PI and other math constants
292
+ add_definitions (-DNOMINMAX ) # Prevent min/max macro conflicts
289
293
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /utf-8" )
290
294
if (NOT CLANG )
291
295
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP" )
@@ -376,6 +380,47 @@ endif()
376
380
377
381
add_definitions ("-DCMAKE_BUILD" )
378
382
383
+ # ##############################################################################
384
+ #
385
+ # Build optimizations
386
+ #
387
+ # ##############################################################################
388
+
389
+ # Setup ccache if available and enabled
390
+ if (ENABLE_CCACHE )
391
+ find_program (CCACHE_PROGRAM ccache )
392
+ if (CCACHE_PROGRAM )
393
+ message (STATUS "Found ccache: ${CCACHE_PROGRAM} " )
394
+ set (CMAKE_CXX_COMPILER_LAUNCHER "${CCACHE_PROGRAM} " )
395
+ set (CMAKE_C_COMPILER_LAUNCHER "${CCACHE_PROGRAM} " )
396
+ # Configure ccache for better performance
397
+ set (ENV{CCACHE_SLOPPINESS} "pch_defines,time_macros" )
398
+ set (ENV{CCACHE_CPP2} "true" )
399
+ else ()
400
+ message (STATUS "ccache not found, disabling ccache support" )
401
+ set (ENABLE_CCACHE OFF )
402
+ endif ()
403
+ endif ()
404
+
405
+ # Setup Ninja job pools for better resource management
406
+ if (ENABLE_NINJA_POOL AND CMAKE_GENERATOR STREQUAL "Ninja" )
407
+ include (ProcessorCount )
408
+ ProcessorCount (N )
409
+ if (N GREATER 1 )
410
+ # Use 75% of available cores for compilation, rest for linking
411
+ math (EXPR COMPILE_JOBS "${N} * 3 / 4" )
412
+ math (EXPR LINK_JOBS "${N} - ${COMPILE_JOBS} " )
413
+ if (LINK_JOBS LESS 1 )
414
+ set (LINK_JOBS 1 )
415
+ endif ()
416
+
417
+ set_property (GLOBAL PROPERTY JOB_POOLS "compile=${COMPILE_JOBS} ;link=${LINK_JOBS} " )
418
+ set (CMAKE_JOB_POOL_COMPILE compile )
419
+ set (CMAKE_JOB_POOL_LINK link )
420
+ message (STATUS "Ninja job pools: compile=${COMPILE_JOBS} , link=${LINK_JOBS} " )
421
+ endif ()
422
+ endif ()
423
+
379
424
# ##############################################################################
380
425
#
381
426
# packages
@@ -569,6 +614,16 @@ message(STATUS "Use system ICU Library [USE_SYSTEM_ICU]: ${USE_SYSTEM_ICU}")
569
614
message (
570
615
STATUS "Install tesseract configs [INSTALL_CONFIGS]: ${INSTALL_CONFIGS} " )
571
616
message (STATUS "--------------------------------------------------------" )
617
+ message (STATUS "Modern build optimizations:" )
618
+ message (STATUS "Unity build [ENABLE_UNITY_BUILD]: ${ENABLE_UNITY_BUILD} " )
619
+ message (STATUS "Precompiled headers [ENABLE_PRECOMPILED_HEADERS]: ${ENABLE_PRECOMPILED_HEADERS} " )
620
+ message (STATUS "ccache [ENABLE_CCACHE]: ${ENABLE_CCACHE} " )
621
+ if (CMAKE_GENERATOR STREQUAL "Ninja" )
622
+ message (STATUS "Ninja job pools [ENABLE_NINJA_POOL]: ${ENABLE_NINJA_POOL} " )
623
+ else ()
624
+ message (STATUS "Ninja job pools [ENABLE_NINJA_POOL]: Disabled (not using Ninja)" )
625
+ endif ()
626
+ message (STATUS "--------------------------------------------------------" )
572
627
message (STATUS )
573
628
574
629
# ##############################################################################
@@ -593,19 +648,11 @@ endif()
593
648
# LIBRARY tesseract
594
649
# ##############################################################################
595
650
596
- file (
597
- GLOB
598
- TESSERACT_SRC
599
- src/ccmain/*.cpp
600
- src/ccstruct/*.cpp
601
- src/ccutil/*.cpp
602
- src/classify/*.cpp
603
- src/cutil/*.cpp
604
- src/dict/*.cpp
605
- src/lstm/*.cpp
606
- src/textord/*.cpp
607
- src/viewer/*.cpp
608
- src/wordrec/*.cpp )
651
+ # Include source file lists
652
+ include (cmake/SourceLists.cmake )
653
+
654
+ # Build the core source file list
655
+ set (TESSERACT_SRC ${TESSERACT_SRC_CORE} )
609
656
610
657
if (DISABLED_LEGACY_ENGINE )
611
658
# prepend path to list of source files
@@ -686,8 +733,8 @@ if(DISABLED_LEGACY_ENGINE)
686
733
list (REMOVE_ITEM TESSERACT_SRC ${TESSERACT_SRC_LEGACY} )
687
734
endif (DISABLED_LEGACY_ENGINE )
688
735
689
- list ( APPEND arch_files src/arch/dotproduct.cpp src/arch/simddetect.cpp
690
- src/arch/intsimdmatrix.cpp )
736
+ # Use architecture files from SourceLists.cmake
737
+ set ( arch_files ${TESSERACT_SRC_ARCH} )
691
738
692
739
if (DOTPRODUCT_FLAGS )
693
740
set_source_files_properties (src/arch/dotproduct.cpp
@@ -731,21 +778,8 @@ if(HAVE_NEON)
731
778
endif ()
732
779
endif (HAVE_NEON )
733
780
734
- file (
735
- GLOB_RECURSE
736
- TESSERACT_HDR
737
- include /*
738
- src/arch/*.h
739
- src/ccmain/*.h
740
- src/ccstruct/*.h
741
- src/ccutil/*.h
742
- src/classify/*.h
743
- src/cutil/*.h
744
- src/dict/*.h
745
- src/lstm/*.h
746
- src/textord/*.h
747
- src/viewer/*.h
748
- src/wordrec/*.h )
781
+ # Use explicit header file lists from SourceLists.cmake
782
+ set (TESSERACT_HDR ${TESSERACT_HDR_INCLUDE} ${TESSERACT_HDR_INTERNAL} )
749
783
750
784
set (TESSERACT_SRC
751
785
${TESSERACT_SRC}
@@ -799,6 +833,55 @@ set(LIBTESSFILES ${TESSERACT_SRC} ${arch_files} ${arch_files_opt}
799
833
source_group (TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${LIBTESSFILES} )
800
834
801
835
add_library (libtesseract ${LIBTESSFILES} )
836
+
837
+ # Apply modern optimizations to the main library
838
+ if (ENABLE_UNITY_BUILD )
839
+ set_target_properties (libtesseract PROPERTIES UNITY_BUILD ON )
840
+ set_target_properties (libtesseract PROPERTIES UNITY_BUILD_BATCH_SIZE 16 )
841
+ message (STATUS "Unity build enabled for libtesseract with batch size 16" )
842
+ endif ()
843
+
844
+ # Apply precompiled headers to reduce compilation time
845
+ if (ENABLE_PRECOMPILED_HEADERS )
846
+ target_precompile_headers (libtesseract PRIVATE
847
+ <vector>
848
+ <string>
849
+ <memory>
850
+ <algorithm>
851
+ <iostream>
852
+ <cstdlib>
853
+ <cstring>
854
+ <cmath>
855
+ )
856
+
857
+ # Exclude architecture-specific files from PCH due to custom compiler flags
858
+ set (ARCH_FILES_NO_PCH
859
+ src/arch/dotproduct.cpp
860
+ src/arch/dotproductavx.cpp
861
+ src/arch/dotproductavx512.cpp
862
+ src/arch/dotproductfma.cpp
863
+ src/arch/dotproductsse.cpp
864
+ src/arch/dotproductneon.cpp
865
+ src/arch/intsimdmatrixavx2.cpp
866
+ src/arch/intsimdmatrixsse.cpp
867
+ src/arch/intsimdmatrixneon.cpp
868
+ )
869
+
870
+ foreach (file ${ARCH_FILES_NO_PCH} )
871
+ if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR} /${file} " )
872
+ set_source_files_properties ("${file} " PROPERTIES SKIP_PRECOMPILE_HEADERS ON )
873
+ endif ()
874
+ endforeach ()
875
+
876
+ message (STATUS "Precompiled headers enabled for libtesseract (excluding architecture-specific files)" )
877
+ endif ()
878
+
879
+ # Configure build pools for Ninja
880
+ if (ENABLE_NINJA_POOL AND CMAKE_GENERATOR STREQUAL "Ninja" )
881
+ set_target_properties (libtesseract PROPERTIES JOB_POOL_COMPILE compile )
882
+ set_target_properties (libtesseract PROPERTIES JOB_POOL_LINK link )
883
+ endif ()
884
+
802
885
target_include_directories (
803
886
libtesseract BEFORE
804
887
PRIVATE src
0 commit comments