@@ -14,39 +14,123 @@ PKG_BREW_NAME="tesseract leptonica"
14
14
PKG_CFLAGS=" -I/usr/include/leptonica"
15
15
PKG_LIBS=" -ltesseract"
16
16
17
- # Use pkg-config if available
18
- pkg-config --version > /dev/null 2>&1
19
- if [ $? -eq 0 ]; then
20
- PKGCONFIG_CFLAGS=` pkg-config --cflags --silence-errors ${PKG_CONFIG_NAME} `
21
- PKGCONFIG_LIBS=` pkg-config --libs ${PKG_CONFIG_NAME} `
22
- fi
23
-
24
- # Note that cflags may be empty in case of success
17
+ # Check for custom locations
25
18
if [ " $INCLUDE_DIR " ] || [ " $LIB_DIR " ]; then
26
19
echo " Found INCLUDE_DIR and/or LIB_DIR!"
27
20
PKG_CFLAGS=" -I$INCLUDE_DIR $PKG_CFLAGS "
28
21
PKG_LIBS=" -L$LIB_DIR $PKG_LIBS "
29
- elif [ " $PKGCONFIG_CFLAGS " ] || [ " $PKGCONFIG_LIBS " ]; then
30
- echo " Found pkg-config cflags and libs!"
31
- PKG_CFLAGS=" ${PKGCONFIG_CFLAGS} "
32
- PKG_LIBS=" ${PKGCONFIG_LIBS} "
22
+
23
+ # Use pkg-config if available
33
24
else
25
+ pkg-config --version > /dev/null 2>&1
26
+ if [ $? -eq 0 ]; then
27
+ # Request shared libraries specifically
28
+ PKGCONFIG_CFLAGS=` pkg-config --cflags --silence-errors ${PKG_CONFIG_NAME} `
29
+ PKGCONFIG_LIBS=` pkg-config --libs --silence-errors ${PKG_CONFIG_NAME} `
30
+
31
+ if [ " $PKGCONFIG_CFLAGS " ] || [ " $PKGCONFIG_LIBS " ]; then
32
+ echo " Found pkg-config cflags and libs!"
33
+ PKG_CFLAGS=" ${PKGCONFIG_CFLAGS} "
34
+ PKG_LIBS=" ${PKGCONFIG_LIBS} "
35
+ fi
36
+ fi
37
+
38
+ # If pkg-config failed, try OS-specific fallbacks
39
+ if [ -z " $PKG_LIBS " ] || [ " $PKG_LIBS " = " -ltesseract" ]; then
40
+ # For MacOS, check common Homebrew and Macports locations
41
+ if [ $( uname) = " Darwin" ]; then
42
+ echo " Checking MacOS-specific paths..."
43
+
44
+ # Homebrew ARM64 location (Apple Silicon)
45
+ if [ -d " /opt/homebrew/include/tesseract" ] && [ -d " /opt/homebrew/lib" ]; then
46
+ echo " Found Tesseract in Homebrew (ARM64)"
47
+ PKG_CFLAGS=" -I/opt/homebrew/include -I/opt/homebrew/include/leptonica"
48
+ PKG_LIBS=" -L/opt/homebrew/lib -ltesseract -llept"
49
+ # Homebrew Intel location
50
+ elif [ -d " /usr/local/include/tesseract" ] && [ -d " /usr/local/lib" ]; then
51
+ echo " Found Tesseract in Homebrew (Intel)"
52
+ PKG_CFLAGS=" -I/usr/local/include -I/usr/local/include/leptonica"
53
+ PKG_LIBS=" -L/usr/local/lib -ltesseract -llept"
54
+ # MacPorts location
55
+ elif [ -d " /opt/local/include/tesseract" ] && [ -d " /opt/local/lib" ]; then
56
+ echo " Found Tesseract in MacPorts"
57
+ PKG_CFLAGS=" -I/opt/local/include -I/opt/local/include/leptonica"
58
+ PKG_LIBS=" -L/opt/local/lib -ltesseract -llept"
59
+ fi
60
+ fi
61
+ fi
62
+ fi
63
+
64
+ # Check if shared libraries are available
65
+ TESSERACT_LIB_PATH=$( echo $PKG_LIBS | grep -o -- " -L[^ ]*" | sed ' s/-L//' )
66
+ FOUND_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
+ # 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' )
77
+ fi
78
+ fi
79
+ fi
80
+
81
+ if [ $FOUND_SHARED_LIB -eq 0 ]; then
82
+ # Common locations for shared libraries on Linux
83
+ for LIB_DIR in /usr/lib/x86_64-linux-gnu /usr/lib /usr/lib64 /lib/x86_64-linux-gnu
84
+ do
85
+ if [ -f " ${LIB_DIR} /libtesseract.so" ]; then
86
+ echo " Found shared tesseract library in ${LIB_DIR} "
87
+ FOUND_SHARED_LIB=1
88
+
89
+ # Update PKG_LIBS to use the found path
90
+ PKG_LIBS=" -L${LIB_DIR} -ltesseract"
91
+ break
92
+ fi
93
+ done
94
+ fi
95
+
96
+ if [ $FOUND_SHARED_LIB -eq 0 ]; then
97
+ echo " Warning: Shared tesseract library not found, attempting to use static library"
98
+ echo " If compilation fails with PIC errors, please install the shared version of tesseract"
99
+
100
+ # For distributions like Ubuntu, try to suggest the exact package
101
+ if [ -f " /etc/lsb-release" ] && grep -q " Ubuntu" /etc/lsb-release; then
102
+ echo " On Ubuntu, try: sudo apt-get install libtesseract-dev"
103
+ fi
104
+ fi
105
+
106
+ echo " Using PKG_CFLAGS=${PKG_CFLAGS} "
107
+ echo " Using PKG_LIBS=${PKG_LIBS} "
108
+
109
+ # If we still haven't found the libraries, error out
110
+ if [ -z " $PKG_LIBS " ] || [ " $PKG_LIBS " = " -ltesseract" ]; then
34
111
echo " --------------------------- [ANTICONF] --------------------------------"
35
- echo " Configuration failed to find system libraries . Try installing:"
112
+ echo " Configuration failed to find tesseract library . Try installing:"
36
113
echo " * deb: $PKG_DEB_NAME (Debian, Ubuntu, etc)"
37
114
echo " * rpm: $PKG_RPM_NAME (Fedora, CentOS, RHEL)"
38
115
echo " * brew: $PKG_BREW_NAME (Mac OSX)"
39
- echo " If the libraries are already installed, check that 'pkg-config' is in your"
40
- echo " PATH and PKG_CONFIG_PATH contains the necessary .pc files . If pkg-config"
116
+ echo " If tesseract is already installed, check that 'pkg-config' is in your"
117
+ echo " PATH and PKG_CONFIG_PATH contains the tesseract .pc file . If pkg-config"
41
118
echo " is unavailable you can set INCLUDE_DIR and LIB_DIR manually via:"
42
119
echo " R CMD INSTALL --configure-vars='INCLUDE_DIR=... LIB_DIR=...'"
43
120
echo " -------------------------- [ERROR MESSAGE] ---------------------------"
44
121
exit 1
45
122
fi
46
123
47
- # Print debug information
48
- echo " PKG_CFLAGS: $PKG_CFLAGS "
49
- echo " PKG_LIBS: $PKG_LIBS "
124
+ # Ensure we're using shared libraries not static ones
125
+ # 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
129
+ if [ $( uname) = " Darwin" ]; then
130
+ # Use dylib instead of .so on macOS
131
+ 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' ) ) "
133
+ fi
50
134
51
135
# Check if the compiler is clang
52
136
if [ " $CC " = " clang" ] || [ " $CXX " = " clang++" ]; then
59
143
CPPFLAGS=` ${R_HOME} /bin/R CMD config CPPFLAGS`
60
144
CXX11STD=` ${R_HOME} /bin/R CMD config CXX11STD`
61
145
if [ $? -eq 0 ]; then
62
- CXX11=` ${R_HOME} /bin/R CMD config CXX11`
63
- CXX11CPP=" $CXX11 -E $CXX11STD "
146
+ CXX11=` ${R_HOME} /bin/R CMD config CXX11`
147
+ CXX11CPP=" $CXX11 -E $CXX11STD "
148
+ echo " Using CXX11CPP: ${CXX11CPP} "
64
149
else
65
- CXX11CPP=` ${R_HOME} /bin/R CMD config CXXCPP`
150
+ CXX11CPP=` ${R_HOME} /bin/R CMD config CXXCPP`
151
+ echo " Using CXX11CPP: ${CXX11CPP} "
66
152
fi
67
153
68
- # Create temporary C++ files to test the compatibility with Tesseract
154
+ # Create test file to check which API to use
69
155
cat << EOF > conftest.cpp
70
156
#include <tesseract/baseapi.h>
71
157
#include <string>
72
158
73
159
int main() {
74
160
tesseract::TessBaseAPI api;
75
161
std::string val;
76
- // This fails to link on older Tesseract
77
162
api.GetVariableAsString("test", &val);
78
163
return 0;
79
164
}
80
165
EOF
81
166
82
- if ${CXX11} -std=gnu++11 -c conftest.cpp -o conftest.o ${PKG_CFLAGS} ; then
83
- CXXFLAGS=" $CXXFLAGS -DTESSERACT_HAS_GETVARIABLEASSTRING"
84
- echo " Using GetVariableAsString()"
167
+ # Set default to not using GetVariableAsString
168
+ HAS_GET_VARIABLE_AS_STRING=" #undef TESSERACT_HAS_GETVARIABLEASSTRING"
169
+
170
+ if ${CXX11} -std=gnu++11 -c conftest.cpp -o conftest.o ${PKG_CFLAGS} 2> /dev/null; then
171
+ # If compilation succeeded, GetVariableAsString is available
172
+ HAS_GET_VARIABLE_AS_STRING=" #define TESSERACT_HAS_GETVARIABLEASSTRING 1"
173
+ echo " Using GetVariableAsString() API"
85
174
else
86
- echo " Using GetStringVariable()"
175
+ echo " Using GetStringVariable() API (older version) "
87
176
fi
88
177
178
+ # Create the tesseract config header
179
+ cat << EOF > src/tesseract_config.h
180
+ // Auto-generated by configure
181
+ ${HAS_GET_VARIABLE_AS_STRING}
182
+ EOF
183
+
184
+ # Test Tesseract C++11 compatibility
89
185
cat << EOF > conftest.cpp
90
186
#include <tesseract/baseapi.h>
91
187
#include <allheaders.h>
97
193
98
194
# Test Tesseract
99
195
# Tesseract enforces C++11
100
- if ! ${CXX11} -std=gnu++11 -c conftest.cpp -o conftest.o ${PKG_CFLAGS}
101
- then
196
+ if ! ${CXX11} -std=gnu++11 -c conftest.cpp -o conftest.o ${PKG_CFLAGS} 2> /dev/null; then
102
197
echo " Tesseract requires C++11 support"
103
198
rm -rf conftest.cpp conftest.o
104
199
exit 1
109
204
# Write to Makevars
110
205
sed -e " s|@cflags@|$PKG_CFLAGS |" -e " s|@libs@|$PKG_LIBS |" src/Makevars.in > src/Makevars
111
206
207
+ echo " CXX_STD = CXX11" >> src/Makevars
208
+
112
209
# Success
113
210
exit 0
0 commit comments