4

I am trying to build an app using opencv and pygame. These two libs have been installed with macports. I also installed py2app in the macports python. I am running a simple py2app script

from setuptools import setup

setup(
    app = [
        '/full/path/to/my_script.py',
    ],
    options={
        "py2app":{
            'argv_emulation': True, 
        }
    },
    setup_requires=['py2app']
)

Unfortunately, py2app fails with the following error :"ValueError: New Mach-O header is too large to relocate"

Is it possible to make an app bundle with opencv and macports?

Thanks for your help

Update:

Here is the answer of ronaldoussoren whoi is the py2app developper:

Shared libraries on OSX contain an absolute path to their location, and likewise all used libraries are also mentioned using an absolute path. Py2app uses macholib to rewrite those mentions to relative paths starting with '@executable_path'. For libraries that are located in /opt/local the new path requires more space than the old one, hence the error message. One way to ensure that there is enough space is the linker flag "headerpad_max_install_names". I don't know how to tell darwinports to add this flag when building opencv.

Please also note that I've a similar issue with pyinstaller.

I've tried to modify the opencv Portfile without any changes. I don't really know where to add this linker flag.

Any idea how to fix it?

Update: Here is my Portfile

# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4
# $Id: Portfile 99165 2012-10-28 22:56:03Z jeremyhu@macports.org $

PortSystem                      1.0
PortGroup                       cmake 1.0

name                            opencv
version                         2.4.2
revision                        2
categories                      graphics science
license                         BSD
platforms                       darwin
maintainers                     nomaintainer

description                     Intel(R) Open Source Computer Vision Library

long_description                opencv is a library that is mainly aimed at real time \
                                computer vision. Some example areas would be \
                                Human-Computer Interaction (HCI), Object Identification, \
                                Segmentation and Recognition, Face Recognition, Gesture \
                                Recognition, Motion Tracking, Ego Motion, Motion \
                                Understanding, Structure From Motion (SFM), and Mobile \
                                Robotics.

homepage                        http://opencv.willowgarage.com/wiki/
master_sites                    sourceforge:project/opencvlibrary/opencv-unix/${version}
use_bzip2                       yes
distname                        OpenCV-${version}

checksums                       rmd160  496964503629b0ff4432fbdd276837ab3f134411 \
                                sha256  690023811e03e21df996291392fcf78e43e42ef557b9307503c74c74b4272c28

depends_build-append            port:pkgconfig

depends_lib-append              port:zlib \
                                path:lib/libavcodec.dylib:ffmpeg \
                                port:libpng \
                                port:tiff \
                                port:jasper \
                                port:jpeg \
                                port:bzip2 \
                                port:openexr \
                                port:ilmbase \
                                port:eigen3

patchfiles                      patch-fix_dirname_case.diff \
                                patch-install_name.diff

configure.args-append           -DBUILD_NEW_PYTHON_SUPPORT=OFF \
                                -DBUILD_EXAMPLES=ON \
                                -DINSTALL_C_EXAMPLES=ON \
                                -DBZIP2_LIBRARIES=${prefix}/lib/libbz2.dylib \
                                -DZLIB_LIBRARY=${prefix}/lib/libz.dylib \
                                -DWITH_OPENEXR=ON \
                                -DWITH_1394=OFF \
                                -DWITH_EIGEN=ON \
                                -DEIGEN_INCLUDE_PATH=${prefix}/include/eigen3 \
                                -DBUILD_JASPER=OFF \
                                -DBUILD_JPEG=OFF \
                                -DBUILD_PNG=OFF \
                                -DBUILD_TIFF=OFF \
                                -DBUILD_ZLIB=OFF
configure.ldflags-append        "-Wl,-headerpad_max_install_names"

# gcc-4.0 exits with a bus error
if {${configure.compiler} == "gcc-4.0"} {
    configure.compiler gcc-4.2
    if {![file executable ${configure.cc}]} {
        depends_build-append port:apple-gcc42
        depends_skip_archcheck-append apple-gcc42
        configure.compiler apple-gcc-4.2
    }
}

platform darwin {
    if {${os.major} <= 9} {
        # Video Decode Acceleration Framework is not available
        patchfiles-append       patch-highgui_CMakeLists.txt.diff
    }
}

post-destroot {
    xinstall -d ${destroot}${prefix}/lib/cmake
    move ${destroot}${prefix}/share/OpenCV/OpenCVConfig-version.cmake \
        ${destroot}${prefix}/share/OpenCV/OpenCVConfig.cmake \
        ${destroot}${prefix}/lib/cmake/
}

variant dc1394 description {Use libdc1394 for FireWire camera. Breaks compatibility with Apple iSight FireWire camera.} {
    depends_lib-append          port:libdc1394
    configure.args-delete       -DWITH_1394=OFF
    configure.args-append       -DWITH_1394=ON \
                                -DHAVE_1394=ON
}

variant qt4 description {Use qt4 backend for graphical interface.} {
    PortGroup                   qt4 1.0
    configure.args-append       -DWITH_QT=ON
}

variant python26 conflicts python27 description {Add Python 2.6 bindings} {
    depends_lib-append          port:python26 \
                                port:py26-numpy
    configure.args-delete       -DBUILD_NEW_PYTHON_SUPPORT=OFF
    configure.args-append       -DINSTALL_PYTHON_EXAMPLES=ON \
                                -DBUILD_NEW_PYTHON_SUPPORT=ON \
                                -DPYTHON_EXECUTABLE=${prefix}/bin/python2.6 \
                                -DPYTHON_LIBRARY=${prefix}/lib/libpython2.6.dylib \
                                -DPYTHON_INCLUDE_DIR=${frameworks_dir}/Python.framework/Versions/2.6/Headers \
                                -DPYTHON_PACKAGES_PATH=${frameworks_dir}/Python.framework/Versions/2.6/lib/python2.6/site-packages
}

variant python27 conflicts python26 description {Add Python 2.7 bindings} {
    depends_lib-append          port:python27 \
                                port:py27-numpy
    configure.args-delete       -DBUILD_NEW_PYTHON_SUPPORT=OFF
    configure.args-append       -DINSTALL_PYTHON_EXAMPLES=ON \
                                -DBUILD_NEW_PYTHON_SUPPORT=ON \
                                -DPYTHON_EXECUTABLE=${prefix}/bin/python2.7 \
                                -DPYTHON_LIBRARY=${prefix}/lib/libpython2.7.dylib \
                                -DPYTHON_INCLUDE_DIR=${frameworks_dir}/Python.framework/Versions/2.7/Headers \
                                -DPYTHON_PACKAGES_PATH=${frameworks_dir}/Python.framework/Versions/2.7/lib/python2.7/site-packages
    configure.ldflags-append    "-Wl,-headerpad_max_install_names"
}

variant tbb description {Use Intel TBB} {
    depends_lib-append          port:tbb
    configure.args-append       -DWITH_TBB=ON \
                                -DHAVE_TBB=ON \
                                -DTBB_INCLUDE_DIRS=${prefix}/include \
                                -DOPENCV_LINKER_LIBS="-ltbb -ltbbmalloc"
}

livecheck.type                  sourceforge
livecheck.name                  opencvlibrary
livecheck.regex                 /OpenCV-(\[0-9a-z.\]+)${extract.suffix}

Update : The macport team worked on the problem but unfortunately their fix don't solve my problem. See http://trac.macports.org/ticket/36841

luc
  • 41,928
  • 25
  • 127
  • 172
  • Please provide some feedback about the provided answer if it isn't working for you. – Vortexfive Nov 05 '12 at 10:17
  • Sorry, I didn't have time to test it yet. Some others hot potatoes. :) I'll give feedback asap. – luc Nov 05 '12 at 15:05
  • Are you trying to create a redistributable bundle or just a launcher for your system? In the latter case you can use _-A_ py2app option to alias libraries, e.g. `python setup.py py2app -A` – anddam Nov 17 '12 at 08:06
  • I need to create a redistribuable bundle – luc Nov 17 '12 at 20:31

2 Answers2

4

You need to add

configure.ldflags-append "-Wl,-headerpad_max_install_names"

in the OpenCV Portfile somewhere near the configure.args-append line. (Documentation)

I hope this fixes your problem. Otherwise you might have to compile all dependencies of OpenCV with that flag as well, which might be a lot of work. You may want to have a look at https://trac.macports.org/ticket/29838 if that's the case and you want the headerpad_max_install_names flag to be enabled by default.

If you want the changes to your Portfile to be preserved, and not undone on the next update of OpenCV, you can follow the instructions at Sneaky patching source with Macports. However, this will prevent updates of OpenCV to be automatically installed!

Community
  • 1
  • 1
Vortexfive
  • 1,889
  • 13
  • 19
  • Unfortunately, configure.ldflags-append "-Wl,-headerpad_max_install_names" doesn't fix the problem. Anyway, +1 and many thanks for this answer – luc Nov 08 '12 at 09:15
  • @luc: Ok, sorry to hear that. I just want to note that `configure.ldflags-append "-Wl,-headerpad_max_install_names"` should be outside of the `variant tbb description {Use Intel TBB} {...}` block if you don't use the tbb variant (see the Portfile in the question). – Vortexfive Nov 08 '12 at 15:49
  • I've modified again the Portfile (i've also updated it in the question) and I still have the same error. Thanks for your help (it worths the bounty award :) – luc Nov 08 '12 at 20:57
  • If you run port with the `-vv` option you can check if the `"-Wl,-headerpad_max_install_names"` is correctly passed to gcc in the linking stage. – Vortexfive Nov 09 '12 at 13:18
1

Yesterday I found a solution with guidance from the py2app developer.

First, know that the -Xlinker,-Wl and. -headpad... flags did not fix the problem for me, though I still keep them in macports.conf. The key for me was increasing the macports file depth and decreasing the build directory file depth. Specifically, I install macports with --prefix=/opt/localdepth/localdepth/localdepth/localdepth/local, and I build in /opt/build. Now py2app runs successfully.

See also: https://bitbucket.org/ronaldoussoren/py2app/issue/93/mach-o-header-may-be-too-large-to-relocate https://trac.macports.org/ticket/38163

D. Smith
  • 21
  • 5