6161 fi
6262fi
6363
64+ # Clean up PKG_CFLAGS and PKG_LIBS - remove newlines and ensure proper escaping
65+ PKG_CFLAGS=$( echo " $PKG_CFLAGS " | tr ' \n' ' ' )
66+ PKG_LIBS=$( echo " $PKG_LIBS " | tr ' \n' ' ' )
67+
6468# Check if shared libraries are available
6569TESSERACT_LIB_PATH=$( echo $PKG_LIBS | grep -o -- " -L[^ ]*" | sed ' s/-L//' )
6670FOUND_SHARED_LIB=0
67- if [ -n " $TESSERACT_LIB_PATH " ]; then
68- if [ -f " ${TESSERACT_LIB_PATH} /libtesseract.so" ] || [ -f " ${TESSERACT_LIB_PATH} /libtesseract.dylib" ]; then
69- echo " Found shared tesseract library in ${TESSERACT_LIB_PATH} "
70- FOUND_SHARED_LIB=1
71+
72+ # For MacOS, find Homebrew's installation directly if needed
73+ if [ $( uname) = " Darwin" ]; then
74+ # Add explicit Leptonica include path for macOS
75+ if echo " $PKG_CFLAGS " | grep -q " /include/leptonica" ; then
76+ echo " Leptonica include path already present"
77+ else
78+ if [ -d " /opt/homebrew/include/leptonica" ]; then
79+ echo " Adding explicit Leptonica include path for macOS ARM"
80+ PKG_CFLAGS=" $PKG_CFLAGS -I/opt/homebrew/include/leptonica"
81+ elif [ -d " /usr/local/include/leptonica" ]; then
82+ echo " Adding explicit Leptonica include path for macOS Intel"
83+ PKG_CFLAGS=" $PKG_CFLAGS -I/usr/local/include/leptonica"
84+ fi
85+ fi
86+
87+ # Find Homebrew's Cellar directly
88+ if [ -d " /opt/homebrew/Cellar/tesseract" ]; then
89+ TESSERACT_PATH=$( ls -d /opt/homebrew/Cellar/tesseract/* | sort -V | tail -1)
90+ LEPTONICA_PATH=$( ls -d /opt/homebrew/Cellar/leptonica/* | sort -V | tail -1)
7191
72- # Force use of .so or .dylib
73- if [ $( uname) = " Darwin" ]; then
74- PKG_LIBS=$( echo " $PKG_LIBS " | sed ' s/libtesseract\.a/libtesseract.dylib/g' )
75- else
76- PKG_LIBS=$( echo " $PKG_LIBS " | sed ' s/libtesseract\.a/libtesseract.so/g' )
92+ if [ -n " $TESSERACT_PATH " ] && [ -n " $LEPTONICA_PATH " ]; then
93+ echo " Using Homebrew tesseract at $TESSERACT_PATH "
94+ echo " Using Homebrew leptonica at $LEPTONICA_PATH "
95+
96+ # Use specific Homebrew Cellar paths
97+ PKG_CFLAGS=" -I$TESSERACT_PATH /include -I$TESSERACT_PATH /include/tesseract -I$LEPTONICA_PATH /include -I$LEPTONICA_PATH /include/leptonica"
98+ PKG_LIBS=" -L$TESSERACT_PATH /lib -ltesseract -L$LEPTONICA_PATH /lib -lleptonica"
99+
100+ # Check for shared library
101+ if [ -f " $TESSERACT_PATH /lib/libtesseract.dylib" ]; then
102+ echo " Found shared tesseract library in $TESSERACT_PATH /lib"
103+ FOUND_SHARED_LIB=1
104+ fi
105+ fi
106+ fi
107+
108+ # Check for shared library in standard path
109+ if [ -n " $TESSERACT_LIB_PATH " ] && [ $FOUND_SHARED_LIB -eq 0 ]; then
110+ if [ -f " ${TESSERACT_LIB_PATH} /libtesseract.dylib" ]; then
111+ echo " Found shared tesseract library in ${TESSERACT_LIB_PATH} "
112+ FOUND_SHARED_LIB=1
113+ fi
114+ fi
115+ else
116+ # Linux/other OS
117+ if [ -n " $TESSERACT_LIB_PATH " ]; then
118+ if [ -f " ${TESSERACT_LIB_PATH} /libtesseract.so" ]; then
119+ echo " Found shared tesseract library in ${TESSERACT_LIB_PATH} "
120+ FOUND_SHARED_LIB=1
77121 fi
78122 fi
79123fi
80124
81- if [ $FOUND_SHARED_LIB -eq 0 ]; then
125+ # If not found yet on Linux, check standard system paths
126+ if [ $FOUND_SHARED_LIB -eq 0 ] && [ $( uname) != " Darwin" ]; then
82127 # Common locations for shared libraries on Linux
83128 for LIB_DIR in /usr/lib/x86_64-linux-gnu /usr/lib /usr/lib64 /lib/x86_64-linux-gnu
84129 do
@@ -100,6 +145,8 @@ if [ $FOUND_SHARED_LIB -eq 0 ]; then
100145 # For distributions like Ubuntu, try to suggest the exact package
101146 if [ -f " /etc/lsb-release" ] && grep -q " Ubuntu" /etc/lsb-release; then
102147 echo " On Ubuntu, try: sudo apt-get install libtesseract-dev"
148+ elif [ $( uname) = " Darwin" ]; then
149+ echo " On macOS, try: brew install tesseract"
103150 fi
104151fi
105152
123170
124171# Ensure we're using shared libraries not static ones
125172# Replace any static library references with dynamic ones
126- PKG_LIBS=$( echo " $PKG_LIBS " | sed ' s/\.a/.so/g' )
127-
128- # For Mac, ensure we're getting the right compiler flags for linking
129173if [ $( uname) = " Darwin" ]; then
130- # Use dylib instead of .so on macOS
174+ PKG_LIBS= $( echo " $PKG_LIBS " | sed ' s/\.a/.dylib/g ' )
131175 PKG_LIBS=$( echo " $PKG_LIBS " | sed ' s/\.so/.dylib/g' )
132- PKG_LIBS=" $PKG_LIBS -Wl,-rpath,$( dirname $( echo $PKG_LIBS | grep -o ' \-L[^ ]*' | sed ' s/-L//g' ) ) "
176+
177+ # Add rpath for dylib on macOS
178+ LIB_PATH=$( echo $PKG_LIBS | grep -o ' \-L[^ ]*' | head -1 | sed ' s/-L//' )
179+ if [ -n " $LIB_PATH " ]; then
180+ if ! echo " $PKG_LIBS " | grep -q ' \-Wl,-rpath' ; then
181+ PKG_LIBS=" $PKG_LIBS -Wl,-rpath,$LIB_PATH "
182+ fi
183+ fi
184+ else
185+ PKG_LIBS=$( echo " $PKG_LIBS " | sed ' s/\.a/.so/g' )
133186fi
134187
135188# Check if the compiler is clang
183236
184237# Test Tesseract C++11 compatibility
185238cat << EOF > conftest.cpp
239+ #if __APPLE__
240+ #if __has_include(<leptonica/allheaders.h>)
241+ #include <leptonica/allheaders.h>
242+ #elif __has_include(<allheaders.h>)
243+ #include <allheaders.h>
244+ #else
245+ #error "Could not find allheaders.h"
246+ #endif
247+ #else
248+ #include <allheaders.h>
249+ #endif
186250#include <tesseract/baseapi.h>
187- #include <allheaders.h>
188251int main() {
189252 tesseract::TessBaseAPI api;
190253 return 0;
@@ -201,9 +264,26 @@ else
201264 rm -rf conftest.cpp conftest.o
202265fi
203266
204- # Write to Makevars
205- sed -e " s|@cflags@|$PKG_CFLAGS |" -e " s|@libs@|$PKG_LIBS |" src/Makevars.in > src/Makevars
267+ # Also create a compatible version of cpp11tesseract_types.h with conditional includes
268+ cat << EOF > src/allheaders_include.h
269+ // Auto-generated by configure
270+ #if __APPLE__
271+ #if __has_include(<leptonica/allheaders.h>)
272+ #include <leptonica/allheaders.h>
273+ #elif __has_include(<allheaders.h>)
274+ #include <allheaders.h>
275+ #else
276+ #error "Could not find allheaders.h"
277+ #endif
278+ #else
279+ #include <allheaders.h>
280+ #endif
281+ EOF
206282
283+ # Write to Makevars - use printf to avoid issues with special characters
284+ rm -f src/Makevars
285+ printf " PKG_CPPFLAGS=%s\n" " $PKG_CFLAGS " > src/Makevars
286+ printf " PKG_LIBS=%s\n\n" " $PKG_LIBS " >> src/Makevars
207287echo " CXX_STD = CXX11" >> src/Makevars
208288
209289# Success
0 commit comments