Skip to content

Commit 7b9f6e5

Browse files
committed
Fix macOS build configuration for FFmpeg and OCR libraries
- Add proper include and library paths for Homebrew-installed FFmpeg - Add Leptonica and Tesseract library paths for Apple Silicon Macs - Fix library linking names (use -lleptonica instead of -llept) - Add ENABLE_OCR flag when building with hardsubx support - Add FFmpeg include path detection in Rust build.rs for bindgen
1 parent 198ac56 commit 7b9f6e5

File tree

2 files changed

+116
-1
lines changed

2 files changed

+116
-1
lines changed

mac/build.command

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ while [[ $# -gt 0 ]]; do
2020
;;
2121
-hardsubx)
2222
HARDSUBX=true
23+
ENABLE_OCR=true
2324
RUST_FEATURES="--features hardsubx_ocr"
2425
shift
2526
;;
@@ -49,6 +50,53 @@ fi
4950

5051
BLD_INCLUDE="-I../src/ -I../src/lib_ccx -I../src/lib_hash -I../src/thirdparty/libpng -I../src/thirdparty -I../src/thirdparty/zlib -I../src/thirdparty/freetype/include `pkg-config --cflags --silence-errors gpac`"
5152

53+
# Add FFmpeg include path for Mac
54+
if [[ -d "/opt/homebrew/Cellar/ffmpeg" ]]; then
55+
FFMPEG_VERSION=$(ls -1 /opt/homebrew/Cellar/ffmpeg | head -1)
56+
if [[ -n "$FFMPEG_VERSION" ]]; then
57+
BLD_INCLUDE="$BLD_INCLUDE -I/opt/homebrew/Cellar/ffmpeg/$FFMPEG_VERSION/include"
58+
fi
59+
elif [[ -d "/usr/local/Cellar/ffmpeg" ]]; then
60+
FFMPEG_VERSION=$(ls -1 /usr/local/Cellar/ffmpeg | head -1)
61+
if [[ -n "$FFMPEG_VERSION" ]]; then
62+
BLD_INCLUDE="$BLD_INCLUDE -I/usr/local/Cellar/ffmpeg/$FFMPEG_VERSION/include"
63+
fi
64+
fi
65+
66+
# Add Leptonica include path for Mac
67+
if [[ -d "/opt/homebrew/Cellar/leptonica" ]]; then
68+
LEPT_VERSION=$(ls -1 /opt/homebrew/Cellar/leptonica | head -1)
69+
if [[ -n "$LEPT_VERSION" ]]; then
70+
BLD_INCLUDE="$BLD_INCLUDE -I/opt/homebrew/Cellar/leptonica/$LEPT_VERSION/include"
71+
fi
72+
elif [[ -d "/usr/local/Cellar/leptonica" ]]; then
73+
LEPT_VERSION=$(ls -1 /usr/local/Cellar/leptonica | head -1)
74+
if [[ -n "$LEPT_VERSION" ]]; then
75+
BLD_INCLUDE="$BLD_INCLUDE -I/usr/local/Cellar/leptonica/$LEPT_VERSION/include"
76+
fi
77+
elif [[ -d "/opt/homebrew/include/leptonica" ]]; then
78+
BLD_INCLUDE="$BLD_INCLUDE -I/opt/homebrew/include"
79+
elif [[ -d "/usr/local/include/leptonica" ]]; then
80+
BLD_INCLUDE="$BLD_INCLUDE -I/usr/local/include"
81+
fi
82+
83+
# Add Tesseract include path for Mac
84+
if [[ -d "/opt/homebrew/Cellar/tesseract" ]]; then
85+
TESS_VERSION=$(ls -1 /opt/homebrew/Cellar/tesseract | head -1)
86+
if [[ -n "$TESS_VERSION" ]]; then
87+
BLD_INCLUDE="$BLD_INCLUDE -I/opt/homebrew/Cellar/tesseract/$TESS_VERSION/include"
88+
fi
89+
elif [[ -d "/usr/local/Cellar/tesseract" ]]; then
90+
TESS_VERSION=$(ls -1 /usr/local/Cellar/tesseract | head -1)
91+
if [[ -n "$TESS_VERSION" ]]; then
92+
BLD_INCLUDE="$BLD_INCLUDE -I/usr/local/Cellar/tesseract/$TESS_VERSION/include"
93+
fi
94+
elif [[ -d "/opt/homebrew/include/tesseract" ]]; then
95+
BLD_INCLUDE="$BLD_INCLUDE -I/opt/homebrew/include"
96+
elif [[ -d "/usr/local/include/tesseract" ]]; then
97+
BLD_INCLUDE="$BLD_INCLUDE -I/usr/local/include"
98+
fi
99+
52100
if [[ "$ENABLE_OCR" == "true" ]]; then
53101
BLD_INCLUDE="$BLD_INCLUDE `pkg-config --cflags --silence-errors tesseract`"
54102
fi
@@ -109,7 +157,42 @@ if [[ "$ENABLE_OCR" == "true" ]]; then
109157
fi
110158

111159
if [[ "$HARDSUBX" == "true" ]]; then
112-
BLD_LINKER="$BLD_LINKER -lswscale -lavutil -pthread -lavformat -lavcodec -lavfilter"
160+
# Add FFmpeg library path for Mac
161+
if [[ -d "/opt/homebrew/Cellar/ffmpeg" ]]; then
162+
FFMPEG_VERSION=$(ls -1 /opt/homebrew/Cellar/ffmpeg | head -1)
163+
if [[ -n "$FFMPEG_VERSION" ]]; then
164+
BLD_LINKER="$BLD_LINKER -L/opt/homebrew/Cellar/ffmpeg/$FFMPEG_VERSION/lib"
165+
fi
166+
elif [[ -d "/usr/local/Cellar/ffmpeg" ]]; then
167+
FFMPEG_VERSION=$(ls -1 /usr/local/Cellar/ffmpeg | head -1)
168+
if [[ -n "$FFMPEG_VERSION" ]]; then
169+
BLD_LINKER="$BLD_LINKER -L/usr/local/Cellar/ffmpeg/$FFMPEG_VERSION/lib"
170+
fi
171+
fi
172+
173+
# Add library paths for Leptonica and Tesseract from Cellar
174+
if [[ -d "/opt/homebrew/Cellar/leptonica" ]]; then
175+
LEPT_VERSION=$(ls -1 /opt/homebrew/Cellar/leptonica | head -1)
176+
if [[ -n "$LEPT_VERSION" ]]; then
177+
BLD_LINKER="$BLD_LINKER -L/opt/homebrew/Cellar/leptonica/$LEPT_VERSION/lib"
178+
fi
179+
fi
180+
181+
if [[ -d "/opt/homebrew/Cellar/tesseract" ]]; then
182+
TESS_VERSION=$(ls -1 /opt/homebrew/Cellar/tesseract | head -1)
183+
if [[ -n "$TESS_VERSION" ]]; then
184+
BLD_LINKER="$BLD_LINKER -L/opt/homebrew/Cellar/tesseract/$TESS_VERSION/lib"
185+
fi
186+
fi
187+
188+
# Also add homebrew lib path as fallback
189+
if [[ -d "/opt/homebrew/lib" ]]; then
190+
BLD_LINKER="$BLD_LINKER -L/opt/homebrew/lib"
191+
elif [[ -d "/usr/local/lib" ]]; then
192+
BLD_LINKER="$BLD_LINKER -L/usr/local/lib"
193+
fi
194+
195+
BLD_LINKER="$BLD_LINKER -lswscale -lavutil -pthread -lavformat -lavcodec -lavfilter -lleptonica -ltesseract"
113196
fi
114197

115198
echo "Running pre-build script..."

src/rust/build.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,38 @@ fn main() {
6565
#[cfg(feature = "hardsubx_ocr")]
6666
{
6767
builder = builder.clang_arg("-DENABLE_HARDSUBX");
68+
69+
// Add FFmpeg include paths for Mac
70+
if cfg!(target_os = "macos") {
71+
// Try common Homebrew paths
72+
if std::path::Path::new("/opt/homebrew/include").exists() {
73+
builder = builder.clang_arg("-I/opt/homebrew/include");
74+
} else if std::path::Path::new("/usr/local/include").exists() {
75+
builder = builder.clang_arg("-I/usr/local/include");
76+
}
77+
78+
// Check Homebrew Cellar for FFmpeg
79+
let cellar_ffmpeg = "/opt/homebrew/Cellar/ffmpeg";
80+
if std::path::Path::new(cellar_ffmpeg).exists() {
81+
// Find the FFmpeg version directory
82+
if let Ok(entries) = std::fs::read_dir(cellar_ffmpeg) {
83+
for entry in entries {
84+
if let Ok(entry) = entry {
85+
let include_path = entry.path().join("include");
86+
if include_path.exists() {
87+
builder = builder.clang_arg(format!("-I{}", include_path.display()));
88+
break;
89+
}
90+
}
91+
}
92+
}
93+
}
94+
95+
// Also check environment variable
96+
if let Ok(ffmpeg_include) = env::var("FFMPEG_INCLUDE_DIR") {
97+
builder = builder.clang_arg(format!("-I{}", ffmpeg_include));
98+
}
99+
}
68100
}
69101

70102
// Tell cargo to invalidate the built crate whenever any of the

0 commit comments

Comments
 (0)