在 OSX 上为 Anaconda 安装 QScintilla2:@rpath 问题

Installing QScintilla2 for Anaconda on OSX: an @rpath issue

经过大量的血汗和泪水,我成功地编译了 QScintilla2 (https://www.riverbankcomputing.com/software/qscintilla/download) 用于 Anaconda Python Distribution (2.5.0; Python 2.7. 11; PyQt4) Mac OS X El Capitan.

在 Qt4Qt5 和 Python 文件夹中编译和安装所有内容后,没有错误或警告,所有内容似乎都已在 Anaconda 中的正确位置结束。

但是,当我尝试使用

导入 qscintilla2 时
import PyQt4.Qsci

我收到以下错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: dlopen(/Users/daniel/anaconda/lib/python2.7/site-packages/PyQt4/Qsci.so, 2): Library not loaded: @rpath/./libQtGui.4.dylib
  Referenced from: /Users/daniel/anaconda/lib/python2.7/site-packages/PyQt4/Qsci.so
  Reason: image not found

因此该模块似乎无法找到 libQtGui。4.dylib

我已经阅读了优秀博客 https://mikeash.com/pyblog/friday-qa-2009-11-06-linking-and-install-names.html 上的 @rpath 变量,最后知道我必须在尝试解决问题时使用 otoolinstall_name_tool这个问题。

如果我 运行 otool -L 我得到以下输出:

Qsci.so:
    libQsci.dylib (compatibility version 0.0.0, current version 0.0.0)
    /Users/daniel/anaconda/lib/libqscintilla2.11.dylib (compatibility version 11.3.0, current version 11.3.0)
    @rpath/./libQtGui.4.dylib (compatibility version 4.8.0, current version 4.8.7)
    @rpath/./libQtCore.4.dylib (compatibility version 4.8.0, current version 4.8.7)
    /usr/lib/libstdc++.6.dylib (compatibility version 7.0.0, current version 104.1.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1226.10.1)

并且 otool -l 给了我

Qsci.so:
Load command 0
      cmd LC_SEGMENT_64
  cmdsize 632
  segname __TEXT
   vmaddr 0x0000000000000000
   vmsize 0x000000000009b000
  fileoff 0
 filesize 634880
  maxprot 0x00000007
 initprot 0x00000005
   nsects 7
    flags 0x0
Section
  sectname __text
   segname __TEXT
      addr 0x0000000000002450
      size 0x000000000006bcf6
    offset 9296
     align 2^4 (16)
    reloff 0
    nreloc 0
     flags 0x80000400
 reserved1 0
 reserved2 0
Section
  sectname __stubs
   segname __TEXT
      addr 0x000000000006e146
      size 0x0000000000001956
    offset 450886
     align 2^1 (2)
    reloff 0
    nreloc 0
     flags 0x80000408
 reserved1 0 (index into indirect symbol table)
 reserved2 6 (size of stubs)
Section
  sectname __stub_helper
   segname __TEXT
      addr 0x000000000006fa9c
      size 0x00000000000032ac
    offset 457372
     align 2^2 (4)
    reloff 0
    nreloc 0
     flags 0x80000400
 reserved1 0
 reserved2 0
Section
  sectname __const
   segname __TEXT
      addr 0x0000000000072d50
      size 0x0000000000008e47
    offset 470352
     align 2^4 (16)
    reloff 0
    nreloc 0
     flags 0x00000000
 reserved1 0
 reserved2 0
Section
  sectname __cstring
   segname __TEXT
      addr 0x000000000007bb97
      size 0x0000000000000f62
    offset 506775
     align 2^0 (1)
    reloff 0
    nreloc 0
     flags 0x00000002
 reserved1 0
 reserved2 0
Section
  sectname __unwind_info
   segname __TEXT
      addr 0x000000000007cafc
      size 0x00000000000014b4
    offset 510716
     align 2^2 (4)
    reloff 0
    nreloc 0
     flags 0x00000000
 reserved1 0
 reserved2 0
Section
  sectname __eh_frame
   segname __TEXT
      addr 0x000000000007dfb0
      size 0x000000000001d048
    offset 516016
     align 2^3 (8)
    reloff 0
    nreloc 0
     flags 0x00000000
 reserved1 0
 reserved2 0
Load command 1
      cmd LC_SEGMENT_64
  cmdsize 632
  segname __DATA
   vmaddr 0x000000000009b000
   vmsize 0x0000000000027000
  fileoff 634880
 filesize 159744
  maxprot 0x00000007
 initprot 0x00000003
   nsects 7
    flags 0x0
Section
  sectname __dyld
   segname __DATA
      addr 0x000000000009b000
      size 0x0000000000000010
    offset 634880
     align 2^3 (8)
    reloff 0
    nreloc 0
     flags 0x00000000
 reserved1 0
 reserved2 0
Section
  sectname __got
   segname __DATA
      addr 0x000000000009b010
      size 0x0000000000000030
    offset 634896
     align 2^3 (8)
    reloff 0
    nreloc 0
     flags 0x00000006
 reserved1 1081 (index into indirect symbol table)
 reserved2 0
Section
  sectname __la_symbol_ptr
   segname __DATA
      addr 0x000000000009b040
      size 0x00000000000021c8
    offset 634944
     align 2^3 (8)
    reloff 0
    nreloc 0
     flags 0x00000007
 reserved1 1087 (index into indirect symbol table)
 reserved2 0
Section
  sectname __const
   segname __DATA
      addr 0x000000000009d210
      size 0x00000000000050f8
    offset 643600
     align 2^4 (16)
    reloff 0
    nreloc 0
     flags 0x00000000
 reserved1 0
 reserved2 0
Section
  sectname __data
   segname __DATA
      addr 0x00000000000a2310
      size 0x000000000001f091
    offset 664336
     align 2^4 (16)
    reloff 0
    nreloc 0
     flags 0x00000000
 reserved1 0
 reserved2 0
Section
  sectname __bss
   segname __DATA
      addr 0x00000000000c13b0
      size 0x0000000000000430
    offset 0
     align 2^4 (16)
    reloff 0
    nreloc 0
     flags 0x00000001
 reserved1 0
 reserved2 0
Section
  sectname __common
   segname __DATA
      addr 0x00000000000c17e0
      size 0x0000000000000030
    offset 0
     align 2^3 (8)
    reloff 0
    nreloc 0
     flags 0x00000001
 reserved1 0
 reserved2 0
Load command 2
      cmd LC_SEGMENT_64
  cmdsize 72
  segname __LINKEDIT
   vmaddr 0x00000000000c2000
   vmsize 0x0000000000063000
  fileoff 794624
 filesize 402904
  maxprot 0x00000007
 initprot 0x00000001
   nsects 0
    flags 0x0
Load command 3
          cmd LC_ID_DYLIB
      cmdsize 40
         name libQsci.dylib (offset 24)
   time stamp 1 Thu Jan  1 01:00:01 1970
      current version 0.0.0
compatibility version 0.0.0
Load command 4
     cmd LC_SYMTAB
 cmdsize 24
  symoff 863408
   nsyms 5494
  stroff 964424
 strsize 233104
Load command 5
            cmd LC_DYSYMTAB
        cmdsize 80
      ilocalsym 0
      nlocalsym 2227
     iextdefsym 2227
     nextdefsym 2034
      iundefsym 4261
      nundefsym 1233
         tocoff 0
           ntoc 0
      modtaboff 0
        nmodtab 0
   extrefsymoff 0
    nextrefsyms 0
 indirectsymoff 955752
  nindirectsyms 2168
      extreloff 951312
        nextrel 555
      locreloff 794624
        nlocrel 7955
Load command 6
     cmd LC_UUID
 cmdsize 24
    uuid 888BE029-40E0-3D69-83D4-B236B57ADFD4
Load command 7
      cmd LC_VERSION_MIN_MACOSX
  cmdsize 16
  version 10.5
      sdk 10.11
Load command 8
          cmd LC_LOAD_DYLIB
      cmdsize 80
         name /Users/daniel/anaconda/lib/libqscintilla2.11.dylib (offset 24)
   time stamp 2 Thu Jan  1 01:00:02 1970
      current version 11.3.0
compatibility version 11.3.0
Load command 9
          cmd LC_LOAD_DYLIB
      cmdsize 56
         name @rpath/./libQtGui.4.dylib (offset 24)
   time stamp 2 Thu Jan  1 01:00:02 1970
      current version 4.8.7
compatibility version 4.8.0
Load command 10
          cmd LC_LOAD_DYLIB
      cmdsize 56
         name @rpath/./libQtCore.4.dylib (offset 24)
   time stamp 2 Thu Jan  1 01:00:02 1970
      current version 4.8.7
compatibility version 4.8.0
Load command 11
          cmd LC_LOAD_DYLIB
      cmdsize 56
         name /usr/lib/libstdc++.6.dylib (offset 24)
   time stamp 2 Thu Jan  1 01:00:02 1970
      current version 104.1.0
compatibility version 7.0.0
Load command 12
          cmd LC_LOAD_DYLIB
      cmdsize 56
         name /usr/lib/libSystem.B.dylib (offset 24)
   time stamp 2 Thu Jan  1 01:00:02 1970
      current version 1226.10.1
compatibility version 1.0.0
Load command 13
      cmd LC_FUNCTION_STARTS
  cmdsize 16
  dataoff 858264
 datasize 5144
Load command 14
      cmd LC_DATA_IN_CODE
  cmdsize 16
  dataoff 863408
 datasize 0

据我所知,此模块中没有@rpath 条目(或者至少我应该找到一个 LC_RPATH 命令?)。之后我当然可以用 install_name_tool 设置它,但这就是我卡住的地方。

我不明白在这种情况下什么被视为Qsci.so模块的@loader_path和@executable_path。它位于

/Users/daniel/anaconda/lib/python2.7/site-packages/PyQt4/Qsci.so

但它似乎是对

的直接引用
/Users/daniel/anaconda/lib/libqscintilla2.11.dylib

在这种情况下,/Users/daniel/anaconda/lib/python2.7/site-packages/PyQt4/ 或 /Users/daniel/anaconda/lib/ 会是 @loader_path 吗?我假设 @executable_path 只是指向 /Users/daniel/anaconda/bin 中的 Python 解释器的路径,但如果我错了请纠正我。

libQtGui.4.dylib 和 libQtCore.4.dylib 都位于

/Users/daniel/anaconda/lib/

我尝试使用

手动添加我认为正确的@rpaths
install_name_tool -add_rpath @loader_path/../../ Qsci.so
install_name_tool -add_rpath @loader_path/../lib Qsci.so

第一个条目假定 Qsci.so 的包含文件夹是 @loader_path,第二个条目是 libqscintilla2.11.dylib,但无济于事...

如果我将绝对路径添加到@rpath

install_name_tool -add_rpath /Users/daniel/anaconda/lib Qsci.so

它确实解决了问题,所以我知道这是一个 rpath 问题。但是,我确实需要相对位置,因为我打算在正确处理后打包应用程序。

感谢您对此提供的任何帮助,我很抱歉 post 比预期的要长得多!

通过检查其他 PyQt4 模块,我发现我必须在 [=13= 中添加一个 install_name_tool 指向 @loader_path/../../../ 的 rpath 条目]. 您将在下面找到一个 bash 脚本,它在 Mac.

上完成 QScintilla2 for Anaconda 的整个安装过程
#!/bin/bash
# Run this file from within the root of the QScintilla source folder

# Go to Qscintilla source dir
cd Qt4Qt5
# Build the makefile with qmake, specify llvm as the compiler
# The normal g++ compiler causes an __Unwind_Resume error at linking phase
~/anaconda/bin/qmake qscintilla.pro -spec macx-llvm
# Build Qscintilla
make
# and install it
make install

# Go to python folder
cd ../Python

# Configure compilation of Python Qsci module
~/anaconda/bin/python configure.py -q ~/anaconda/bin/qmake --sip ~/anaconda/bin/sip -n ~/anaconda/include -o ~/anaconda/lib
# make it
make
# Add the correct @rpath entry to Qsci.so so it can find the other required Qt modules
install_name_tool -add_rpath @loader_path/../../../ Qsci.so
# Install QSci.so to the site-packages/PyQt4 folder
make install