Skip to content

Instantly share code, notes, and snippets.

@brendano
Created June 12, 2012 20:03
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save brendano/2919800 to your computer and use it in GitHub Desktop.
Save brendano/2919800 to your computer and use it in GitHub Desktop.
Patches to compile ocropus on Mac OSX 10.6 -- see explanation at NOTES.md at bottom https://gist.github.com/2919800#file_notes.md
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/ocroinst.osx Tue Jun 12 18:25:15 2012 -0400
@@ -0,0 +1,87 @@
+#!/bin/bash
+
+# Vaguely following ocroinst
+# Tested on Mac OSX 10.6
+# Assume we're using Homebrew: http://mxcl.github.com/homebrew/
+# Assume we're using pip, e.g.
+# https://github.com/mxcl/homebrew/wiki/Homebrew-and-Python
+# (I did this with Homebrew Python 2.7. I don't know whether system python
+# would work.)
+# Warning, I haven't tried running this whole thing at once, so a few small
+# things may be missing.
+
+set -eux
+
+## Prerequisities
+brew install scons libtiff jpeg sdl sdl_gfx sdl_image
+
+## Build and install the components.
+## C++ libraries are installed into a Homebrew-friendly prefix,
+## then "brew link"'d.
+## Python extension are "pip install"'d.
+
+cd iulib
+scons
+scons install
+brew link iulib
+cd ..
+
+## Need a newer swig, apparently?
+## OSX 10.6's /usr/bin/swig is 1.3, seems to cause problems
+## Homebrew swig is currently 2.0.6
+brew install swig
+
+brew install tesseract
+
+
+cd iulib/pyswig
+python setup.py build
+pip install .
+cd ../..
+
+cd ocrolseg
+make
+python install .
+cd ..
+
+cd ocrorast
+make
+python setup.py build_ext
+pip install .
+cd ..
+
+cd ocropy
+pip install .
+cd ..
+
+cd llpy
+pip install .
+cd ..
+
+mkdir -p /usr/local/Cellar/ocropus/0.5
+
+cd ocrofst/oscrofstll
+scons
+scons install
+brew link ocropus
+cd ../..
+
+cd ocrofst
+python setup.py build
+pip install .
+
+# Don't use Homebrew's current openfst formula because it's a too-new version.
+tar xf DIST/openfst-1.1.tar.gz
+cd openfst-1.1
+./configure --prefix=/usr/local/Cellar/openfst/1.1 --disable-dependency-tracking
+make install
+brew switch openfst 1.1
+cd ..
+
+cd pyopenfst
+python setup.py build
+pip install .
+
+
+## Skipping FLANN for now
+
====== DIRECTORY iulib
diff -r de8f89afa5e1 pyswig/setup.py
--- a/pyswig/setup.py Tue Jun 12 15:56:06 2012 -0400
+++ b/pyswig/setup.py Tue Jun 12 18:25:15 2012 -0400
@@ -1,19 +1,29 @@
#!/usr/bin/env python
-import os
+import os, sys
from distutils.core import setup, Extension
from numpy.distutils.misc_util import get_numpy_include_dirs
baselibs = ['tiff','png','jpeg','SDL','SDL_gfx','SDL_image','m']
include_dirs = ['/usr/local/include'] + get_numpy_include_dirs()
+
+if sys.platform=='darwin':
+ include_dirs += ['hack']
+ os.system("mkdir -p hack; touch hack/malloc.h") ## accomodate SWIG's lame #include <malloc.h>
+
swig_opts = ["-c++"] + ["-I" + d for d in include_dirs]
swiglib = os.popen("swig -swiglib").read()[:-1]
+library_dirs = ['/usr/local/lib']
+if sys.platform=='darwin':
+ library_dirs += ['/usr/X11/lib']
+
iulib = Extension('_iulib',
libraries = ['iulib']+baselibs,
swig_opts = swig_opts,
include_dirs = include_dirs,
+ library_dirs = library_dirs,
sources=['iulib.i'])
setup (name = 'iulib',
====== DIRECTORY llpy
====== DIRECTORY ocrofst
diff -r 329aed1e71ff ocrofstll/SConstruct
--- a/ocrofstll/SConstruct Mon May 14 11:44:03 2012 +0200
+++ b/ocrofstll/SConstruct Tue Jun 12 18:25:16 2012 -0400
@@ -51,7 +51,7 @@
" -D__warn_unused_result__=__far__"+
" -D_BACKWARD_BACKWARD_WARNING_H=1")
### path options
-opts.Add(PathVariable('prefix', 'The installation root for OCRopus ', "/usr/local"))
+opts.Add(PathVariable('prefix', 'The installation root for OCRopus ', "/usr/local/Cellar/ocropus/0.5"))
opts.Add(PathVariable('iulib', 'The installation root of iulib', "/usr/local"))
opts.Add(PathVariable('destdir', 'Destination root directory', "", PathVariable.PathAccept))
#opts.Add(PathVariable('leptonica', 'The installation root of leptonica', "/usr/local")) #not required
@@ -205,22 +205,22 @@
################################################################
penv = env.Clone()
-penv.Append(LIBS=[File("libocrofst.so")])
+penv.Append(LIBS=[File("libocrofst.dylib")])
penv.Prepend(LINKFLAGS=["-ldl"])
penv.Prepend(CPPPATH=["/usr/local/include"])
-penv.Append(CCFLAGS=["-Xlinker","-rpath=${iulib}/lib"]) #not sure about these linkers??
-penv.Append(LINKFLAGS=["-Xlinker","-rpath=${iulib}/lib"])
+#penv.Append(CCFLAGS=["-Xlinker","-rpath=${iulib}/lib"]) #not sure about these linkers??
+#penv.Append(LINKFLAGS=["-Xlinker","-rpath=${iulib}/lib"])
#for is not needed? instead need to compile our main.cc file ??
for cmd in glob('test-*.cc'):
- penv.Program(cmd,LIBS=File("libocrofst.so"))
+ penv.Program(cmd,LIBS=File("libocrofst.dylib"))
penv.Install(destdir+bindir,re.sub('.cc$','',cmd))
####################################
### To build test-fst
####################################
penv1 = Environment()
-penv1.Prepend(LINKFLAGS=["/usr/local/lib/libfst.so","-ldl"])
+penv1.Prepend(LINKFLAGS=["/usr/local/lib/libfst.dylib","-ldl"])
penv1.Prepend(CPPPATH=["/usr/local/include"])
'''
for cmd in glob('test-*.cc'):
diff -r 329aed1e71ff setup.py
--- a/setup.py Mon May 14 11:44:03 2012 +0200
+++ b/setup.py Tue Jun 12 18:25:16 2012 -0400
@@ -1,4 +1,4 @@
-import os,glob
+import os,glob,sys
from distutils.core import setup, Extension
from numpy.distutils.misc_util import get_numpy_include_dirs
@@ -6,10 +6,18 @@
baselibs = ['png','m']
+library_dirs = []
include_dirs = ['/usr/local/include'] + get_numpy_include_dirs()
+if sys.platform=='darwin':
+ library_dirs += ['/usr/X11/lib']
+ include_dirs += ['/usr/X11/include']
+ include_dirs += ['hack']
+ os.system("mkdir -p hack; touch hack/malloc.h") ## accomodate SWIG's lame #include <malloc.h>
+
swig_opts = ["-c++"] + ["-I" + d for d in include_dirs]
swiglib = os.popen("swig -swiglib").read()[:-1]
+
scripts = ["ocrofst-search","ocropus-lalign","ocropus-lmodel"]
scripts += glob.glob("ocropus-lm-*[a-z]")
@@ -17,6 +25,7 @@
libraries = ['iulib','ocrofst']+baselibs,
swig_opts = swig_opts,
include_dirs = include_dirs,
+ library_dirs = library_dirs,
extra_link_args = ["-L.","-Locrofstll/."],
sources = ['ocrofstll/ocrofstll.i'])
====== DIRECTORY ocrolseg
diff -r a1dc37915521 setup.py
--- a/setup.py Mon Mar 05 01:34:31 2012 +0100
+++ b/setup.py Tue Jun 12 18:25:16 2012 -0400
@@ -1,10 +1,15 @@
#!/usr/bin/env python
-import os
+import os,sys
from distutils.core import setup, Extension
from numpy.distutils.misc_util import get_numpy_include_dirs
include_dirs = ['/usr/local/include'] + get_numpy_include_dirs()
+include_dirs = ['/usr/local/include'] + get_numpy_include_dirs()
+if sys.platform=='darwin':
+ include_dirs += ['hack']
+ os.system("mkdir -p hack; touch hack/malloc.h") ## accomodate SWIG's lame #include <malloc.h>
+
swig_opts = ["-c++"] + ["-I" + d for d in include_dirs]
ocrolseg = Extension('_ocrolseg',
====== DIRECTORY ocropy
diff -r 72d56fe2c4b9 ocropus
--- a/ocropus Sun Jun 03 02:19:53 2012 +0200
+++ b/ocropus Tue Jun 12 18:25:16 2012 -0400
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/env python
### Simple command line tool driving the ocropus pipeline.
====== DIRECTORY ocrorast
====== DIRECTORY pyopenfst
# HG changeset patch
# User Brendan O'Connor <brenocon@gmail.com>
# Date 1339530966 14400
# Node ID de8f89afa5e19450c3f7b6c028b2c57e0d5d2996
# Parent b79bfb1a39fd8b07f153497dfeb02e4faa30cc93
Compile on OSX 10.6
diff -r b79bfb1a39fd -r de8f89afa5e1 SConstruct
--- a/SConstruct Thu May 10 16:16:25 2012 +0200
+++ b/SConstruct Tue Jun 12 15:56:06 2012 -0400
@@ -38,7 +38,12 @@
opts = Variables('custom.py')
opts.Add('opt', 'Compiler flags for optimization/debugging', "-g -O3 -fPIC")
opts.Add('warn', 'Compiler flags for warnings', "-Wall -D__warn_unused_result__=__far__")
-opts.Add('prefix', 'The installation root for iulib', "/usr/local")
+if sys.platform=='darwin':
+ # Homebrew path; should make this a command-line option
+ prefix = '/usr/local/Cellar/iulib/0.4'
+else:
+ prefix = '/usr/local'
+opts.Add('prefix', 'The installation root for iulib', prefix)
opts.Add(BoolVariable('sdl', "provide SDL-based graphics routines", "yes"))
opts.Add(BoolVariable('vidio', "provide video I/O functionality", "no"))
@@ -47,7 +52,18 @@
opts.Add(BoolVariable('test', "Run some tests after the build", "no"))
# opts.Add(BoolVariable('style', 'Check style', "no"))
-env = Environment(options=opts, CXXFLAGS=["${opt}","${warn}"])
+if sys.platform=='darwin':
+ # /usr/X11 is pre-installed. /usr/local refers to Homebrew.
+ # BTO: scons has horrible documentation. had to read this to figure it out:
+ # http://mu2e.fnal.gov/public/hep/computing/scons.shtml
+ extra_args = dict(
+ CPPPATH=['/usr/X11/include', '/usr/local/include'],
+ LIBPATH=['/usr/X11/lib', '/usr/local/lib'],
+ )
+else:
+ extra_args = {}
+
+env = Environment(options=opts, CXXFLAGS=["${opt}","${warn}"], **extra_args)
Help(opts.GenerateHelpText(env))
conf = Configure(env)
@@ -61,7 +77,14 @@
assert conf.CheckLibWithHeader('png', 'png.h', 'C', 'png_byte;', 1),"please install: libpng12-dev"
assert conf.CheckLibWithHeader('jpeg', 'jconfig.h', 'C', 'jpeg_std_error();', 1),"please install: libjpeg62-dev"
-assert conf.CheckLibWithHeader('tiff', 'tiff.h', 'C', 'TIFFHeader;', 1), "please install: libtiff4-dev"
+if sys.platform=='darwin':
+ # BTO: It appears my libtiff 4.0.1 (from OSX Homebrew) does not have
+ # "TIFFHeader" but it does have TIFFHeaderCommon, TIFFHeaderClassic, and
+ # TIFFHeaderBig. Arbitrarily choosing one.
+ tiff_header_name = 'TIFFHeaderClassic;'
+else:
+ tiff_header_name = 'TIFFHeader;'
+assert conf.CheckLibWithHeader('tiff', 'tiff.h', 'C', tiff_header_name, 1), "please install: libtiff4-dev"
### check for optional parts
diff -r b79bfb1a39fd -r de8f89afa5e1 components/components.cc
--- a/components/components.cc Thu May 10 16:16:25 2012 +0200
+++ b/components/components.cc Tue Jun 12 15:56:06 2012 -0400
@@ -27,6 +27,13 @@
#include "iulib.h"
#include "components.h"
+#ifdef __APPLE__ && __MACH__
+// this issue was reported in 2009 and still not fixed...
+// http://code.google.com/p/ocropus/issues/detail?id=158
+#include <crt_externs.h>
+#define environ (*_NSGetEnviron())
+#endif
+
using namespace colib;
namespace {
exporting patch:
<fdopen>

by Brendan O'Connor (http://brenocon.com)

I got all of ocropus to compile on Mac OSX 10.6, though I haven't tested it much yet. This is the current version inside the ocropus hg repository, so approximately version 0.5, with iulib perhaps 0.4ish.

See ocroinst.osx -- the first file in "everything_besides_iulib.diff" -- for line-by-line instructions; the script may even just run. We're assuming Homebrew and pip (see the comments).

iulib.diff was generated from

cd iulib
hg export tip

everything_besides_iulib.diff was generated from

(hg diff; for d in */.hg; { d=$(dirname $d); echo "====== DIRECTORY $d"; (cd $d && hg diff) })  > everything_besides_iulib.diff

which is hacky, but the mercurial setup is too complicated for me to figure out anything else. Is everything divided into tiny little sub-repositories or something? It's inconvenient.

@karanlyons
Copy link

Regarding TIFFHeader, it seems that it was deprecated in favor of the three new headers, each an exact binary representation of TIFF header structures: TIFFHeaderClassic for ClassicTIFF, TIFFHeaderBig for BigTIFF, and TIFFHeaderCommon for the parts of the TIFF header they both share. So it may be best to use TIFFHeaderCommon. Maybe. Who knows. I'm still just trying to install the thing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment